Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
EduMenges committed May 6, 2024
1 parent 35c6c11 commit f47d660
Show file tree
Hide file tree
Showing 21 changed files with 210 additions and 171 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Checks: >
cppcoreguidelines-*,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast,
Expand All @@ -39,6 +40,7 @@ Checks: >
readability-*,
-readability-identifier-length,
-readability-magic-numbers,
-readability-named-parameter
FormatStyle: file
Expand Down
2 changes: 1 addition & 1 deletion example/processing_dapp/processing_dapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ int main(int argc, char* argv[])
std::vector<std::string> pubsubBootstrapPeers;
if (options->remote)
{
pubsubBootstrapPeers = std::move(std::vector({ *options->remote }));
pubsubBootstrapPeers = std::vector( { *options->remote } );
}
pubs->Start(40001, pubsubBootstrapPeers);

Expand Down
1 change: 0 additions & 1 deletion src/account/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @author Henrique A. Klein ([email protected])
*/
#include "account/AccountManager.hpp"
#include "processing/processing_imagesplit.hpp"

namespace sgns
{
Expand Down
1 change: 0 additions & 1 deletion src/account/AccountManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "ipfs_pubsub/gossip_pubsub.hpp"
#include "crdt/globaldb/globaldb.hpp"
#include "crypto/hasher/hasher_impl.hpp"
#include "blockchain/impl/common.hpp"
#include "blockchain/impl/key_value_block_header_repository.hpp"
#include "blockchain/impl/key_value_block_storage.hpp"
#include "integration/IComponent.hpp"
Expand Down
17 changes: 9 additions & 8 deletions src/account/GeniusAccount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#ifndef _GENIUS_ACCOUNT_HPP_
#define _GENIUS_ACCOUNT_HPP_
#include <string>
#include <vector>
#include <cstdint>
#include <boost/multiprecision/cpp_int.hpp>

Expand All @@ -29,28 +28,30 @@ namespace sgns
template<typename T>
const T GetAddress() const;

template<>
const std::string GetAddress() const
template <> [[nodiscard]] const std::string GetAddress() const
{
std::ostringstream oss;
oss << std::hex << address;

return ( "0x" + oss.str() );
}
template<>
const uint256_t GetAddress() const

template <> [[nodiscard]] const uint256_t GetAddress() const
{
return address;
}
const std::string GetBalance() const

[[nodiscard]] std::string GetBalance() const
{
return std::to_string(balance);
}
const std::string GetToken() const

[[nodiscard]] std::string GetToken() const
{
return "GNUS Token";
}
const std::string GetNonce() const

[[nodiscard]] std::string GetNonce() const
{
return std::to_string(nonce);
}
Expand Down
21 changes: 10 additions & 11 deletions src/account/IGeniusTransactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef _IGENIUS_TRANSACTIONS_HPP_
#define _IGENIUS_TRANSACTIONS_HPP_

#include <utility>
#include <vector>
#include <string>
#include <boost/optional.hpp>
Expand All @@ -23,13 +24,14 @@ namespace sgns
class IGeniusTransactions
{
public:
IGeniusTransactions( const std::string &type, const SGTransaction::DAGStruct &dag ) :
transaction_type( type ), //
dag_st( dag ) //
IGeniusTransactions( std::string type, const SGTransaction::DAGStruct &dag ) :
dag_st( dag ), transaction_type( std::move( type ) )
{
}

virtual ~IGeniusTransactions() = default;
const std::string GetType() const

[[nodiscard]] std::string GetType() const
{
return transaction_type;
}
Expand Down Expand Up @@ -66,21 +68,18 @@ namespace sgns
return full_path.str();
}

template <typename T>
const T GetSrcAddress() const;
template <typename T> const T GetSrcAddress() const;

template <>
const std::string GetSrcAddress<std::string>() const
template <> const std::string GetSrcAddress<std::string>() const
{

//std::string address(bytes_data.begin(), bytes_data.end());
//std::ostringstream oss;
//oss << std::hex << src_address;

return dag_st.source_addr();
}
template <>
const uint256_t GetSrcAddress<uint256_t>() const

template <> const uint256_t GetSrcAddress<uint256_t>() const
{
return uint256_t{ dag_st.source_addr() };
}
Expand Down
12 changes: 7 additions & 5 deletions src/account/MintTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ namespace sgns
amount( new_amount ) //
{
auto hasher_ = std::make_shared<sgns::crypto::HasherImpl>();
auto hash = hasher_->blake2b_256(SerializeByteVector());
dag_st.set_data_hash(hash.toReadableString());
auto hash = hasher_->blake2b_256( SerializeByteVector() );
dag_st.set_data_hash( hash.toReadableString() );
}

std::vector<uint8_t> MintTransaction::SerializeByteVector()
{
SGTransaction::MintTx tx_struct;
Expand All @@ -28,9 +29,9 @@ namespace sgns
tx_struct.SerializeToArray( serialized_proto.data(), serialized_proto.size() );
return serialized_proto;
}

MintTransaction MintTransaction::DeSerializeByteVector( const std::vector<uint8_t> &data )
{

SGTransaction::MintTx tx_struct;
if ( !tx_struct.ParseFromArray( data.data(), data.size() ) )
{
Expand All @@ -39,10 +40,11 @@ namespace sgns
uint64_t v64 = tx_struct.amount();
//std::memcpy( &v64, &( *data.begin() ), sizeof( v64 ) );

return MintTransaction( v64, tx_struct.dag_struct() ); // Return new instance
return { v64, tx_struct.dag_struct() }; // Return new instance
}

uint64_t MintTransaction::GetAmount() const
{
return amount;
}
}
}
1 change: 1 addition & 0 deletions src/account/MintTransaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#ifndef _MINT_TRANSACTION_HPP_
#define _MINT_TRANSACTION_HPP_

#include <vector>
#include <cstdint>
#include "account/IGeniusTransactions.hpp"
Expand Down
10 changes: 6 additions & 4 deletions src/account/ProcessingTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
*/

#include "account/ProcessingTransaction.hpp"

#include <utility>
#include "crypto/hasher/hasher_impl.hpp"

namespace sgns
{
ProcessingTransaction::ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag) :
IGeniusTransactions( "processing", SetDAGWithType(dag,"processing")), //
hash_process_data( hash ) //
ProcessingTransaction::ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag ) :
IGeniusTransactions( "processing", SetDAGWithType( dag, "processing" ) ), //
hash_process_data( std::move( hash ) )
{
auto hasher_ = std::make_shared<sgns::crypto::HasherImpl>();
auto hash_data = hasher_->blake2b_256(SerializeByteVector());
Expand All @@ -32,6 +34,6 @@ namespace sgns
uint256_t hash;
import_bits( hash, data.begin(), data.end() );

return ProcessingTransaction( hash, {} ); // Return new instance
return { hash, {} }; // Return new instance
}
}
3 changes: 2 additions & 1 deletion src/account/ProcessingTransaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;

namespace sgns
{

class ProcessingTransaction : public IGeniusTransactions
{
public:
ProcessingTransaction( uint256_t hash, const SGTransaction::DAGStruct &dag );
~ProcessingTransaction() = default;
~ProcessingTransaction() override = default;

std::vector<uint8_t> SerializeByteVector() override;
static ProcessingTransaction DeSerializeByteVector( const std::vector<uint8_t> &data );
Expand Down
32 changes: 22 additions & 10 deletions src/account/TransactionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

namespace sgns
{
TransactionManager::TransactionManager( std::shared_ptr<crdt::GlobalDB> db, std::shared_ptr<boost::asio::io_context> ctx,
TransactionManager::TransactionManager( std::shared_ptr<crdt::GlobalDB> db,
std::shared_ptr<boost::asio::io_context> ctx,
std::shared_ptr<GeniusAccount> account,
std::shared_ptr<blockchain::BlockStorage> block_storage ) :
db_m( std::move( db ) ), //
ctx_m( std::move( ctx ) ), //
account_m( std::move( account ) ), //
block_storage_m( std::move( block_storage ) ), //
timer_m( std::make_shared<boost::asio::steady_timer>( *ctx_m, boost::asio::chrono::milliseconds( 300 ) ) ), //
last_block_id_m( 0 ), //
last_trans_on_block_id( 0 )
last_trans_on_block_id( 0 ), //
block_storage_m( std::move( block_storage ) )

{
m_logger->set_level( spdlog::level::debug );
Expand All @@ -39,7 +40,8 @@ namespace sgns
this->Update();
this->timer_m->expires_after( boost::asio::chrono::milliseconds( 300 ) );

this->timer_m->async_wait( [this, task]( const boost::system::error_code & ) { this->ctx_m->post( *task ); } );
this->timer_m->async_wait( [this, task]( const boost::system::error_code & )
{ this->ctx_m->post( *task ); } );
};
ctx_m->post( *task );
}
Expand All @@ -56,16 +58,19 @@ namespace sgns
{
return *account_m;
}

void TransactionManager::TransferFunds( const uint256_t &amount, const uint256_t &destination )
{
auto transfer_transaction = std::make_shared<sgns::TransferTransaction>( amount, destination, FillDAGStruct() );
this->EnqueueTransaction( transfer_transaction );
}

void TransactionManager::MintFunds( const uint64_t &amount )
{
auto mint_transaction = std::make_shared<sgns::MintTransaction>( amount, FillDAGStruct() );
this->EnqueueTransaction( mint_transaction );
}

void TransactionManager::HoldEscrow( const uint64_t &amount )
{
auto mint_transaction = std::make_shared<sgns::MintTransaction>( amount, FillDAGStruct() );
Expand All @@ -77,11 +82,13 @@ namespace sgns
SendTransaction();
CheckBlockchain();
}

void TransactionManager::EnqueueTransaction( std::shared_ptr<IGeniusTransactions> element )
{
std::lock_guard<std::mutex> lock( mutex_m );
out_transactions.emplace_back( std::move( element ) );
}

//TODO - Fill hash stuff on DAGStruct
SGTransaction::DAGStruct TransactionManager::FillDAGStruct()
{
Expand All @@ -102,7 +109,6 @@ namespace sgns
std::unique_lock<std::mutex> lock( mutex_m );
if ( !out_transactions.empty() )
{

auto elem = out_transactions.front();
out_transactions.pop_front();
boost::format tx_key{ std::string( TRANSACTION_BASE_FORMAT ) };
Expand Down Expand Up @@ -136,12 +142,14 @@ namespace sgns

block_storage_m->putBlockData( header.number, block_data );

m_logger->debug( "Putting on " + transaction_path + " the data: " + std::string( data_transaction.toString() ) );
m_logger->debug( "Putting on " + transaction_path +
" the data: " + std::string( data_transaction.toString() ) );
m_logger->debug( "Recording Block with number " + std::to_string( header.number ) );
block_storage_m->setLastFinalizedBlockHash( new_hash.value() );
}
lock.unlock(); // Manual unlock, no need to wait to run out of scope
}

bool TransactionManager::GetTransactionsFromBlock( const primitives::BlockNumber &block_number )
{
outcome::result<primitives::BlockBody> retval = outcome::failure( boost::system::error_code{} );
Expand Down Expand Up @@ -179,6 +187,7 @@ namespace sgns
//while ( !retval );
return ret;
}

void TransactionManager::ParseTransaction( std::string transaction_key )
{
auto maybe_transaction_data = db_m->Get( { transaction_key } );
Expand All @@ -196,7 +205,8 @@ namespace sgns
if ( maybe_dag.value().type() == "transfer" )
{
m_logger->info( "Transfer transaction" );
TransferTransaction tx = TransferTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );
TransferTransaction tx =
TransferTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );
if ( tx.GetDstAddress<uint256_t>() == account_m->GetAddress<uint256_t>() )
{
account_m->balance += static_cast<uint64_t>( tx.GetAmount<uint256_t>() );
Expand All @@ -211,7 +221,8 @@ namespace sgns
else if ( maybe_dag.value().type() == "mint" )
{
m_logger->info( "Mint transaction" );
MintTransaction tx = MintTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );
MintTransaction tx =
MintTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );

//std::cout << tx.GetAddress<std::string>() << std::endl;
if ( tx.GetSrcAddress<uint256_t>() == account_m->GetAddress<uint256_t>() )
Expand All @@ -226,7 +237,8 @@ namespace sgns
else if ( maybe_dag.value().type() == "process" )
{
m_logger->info( "Process transaction" );
ProcessingTransaction tx = ProcessingTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );
ProcessingTransaction tx =
ProcessingTransaction::DeSerializeByteVector( maybe_transaction_data.value().toVector() );

//std::cout << tx.GetAddress<std::string>() << std::endl;
/*if ( tx.GetSrcAddress<uint256_t>() == account_m->GetAddress<uint256_t>() )
Expand Down Expand Up @@ -277,4 +289,4 @@ namespace sgns
} while ( retval );
}

}
}
Loading

0 comments on commit f47d660

Please sign in to comment.