From 340c0eae8d35b22143c098beec58bbae86f9181d Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:25:17 -0300 Subject: [PATCH 1/8] feat: Broke rocksdb encapsulation due to terrible abstraction --- src/crdt/crdt_datastore.hpp | 6 +++++- src/crdt/globaldb/globaldb.hpp | 5 +++++ src/storage/rocksdb/rocksdb.hpp | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/crdt/crdt_datastore.hpp b/src/crdt/crdt_datastore.hpp index 93167d38..20229f9c 100644 --- a/src/crdt/crdt_datastore.hpp +++ b/src/crdt/crdt_datastore.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -209,6 +208,11 @@ namespace sgns::crdt */ outcome::result> CreateDeltaToRemove(const std::string& key); + auto GetDB() + { + return dataStore_->getDB(); + } + protected: /** DAG jobs structure used by DAG worker threads to send new jobs diff --git a/src/crdt/globaldb/globaldb.hpp b/src/crdt/globaldb/globaldb.hpp index 4cf4cc24..6760a1c9 100644 --- a/src/crdt/globaldb/globaldb.hpp +++ b/src/crdt/globaldb/globaldb.hpp @@ -62,6 +62,11 @@ class GlobalDB */ std::shared_ptr BeginTransaction(); + auto GetDB() + { + return m_crdtDatastore->GetDB(); + } + private: std::shared_ptr m_context; std::string m_databasePath; diff --git a/src/storage/rocksdb/rocksdb.hpp b/src/storage/rocksdb/rocksdb.hpp index 5dc419f9..ee7055b9 100644 --- a/src/storage/rocksdb/rocksdb.hpp +++ b/src/storage/rocksdb/rocksdb.hpp @@ -86,7 +86,10 @@ namespace sgns::storage return "rocksdb"; } - inline std::shared_ptr getDB() const { return db_; } + [[nodiscard]] std::shared_ptr getDB() const + { + return db_; + } private: std::shared_ptr db_; From 27b22dfd2d6d51ad4a80c71723024a9ec16ccd4d Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:26:21 -0300 Subject: [PATCH 2/8] refactor: Removed unnecessary thread include --- .../processing_dapp_processor.cpp | 16 +++++++--------- src/crdt/crdt_datastore.hpp | 1 - src/crdt/impl/crdt_datastore.cpp | 1 + 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/example/processing_dapp/processing_dapp_processor.cpp b/example/processing_dapp/processing_dapp_processor.cpp index f3dfbd7f..87dc2201 100644 --- a/example/processing_dapp/processing_dapp_processor.cpp +++ b/example/processing_dapp/processing_dapp_processor.cpp @@ -14,6 +14,8 @@ #include #include +#include +#include using namespace sgns::processing; @@ -32,14 +34,10 @@ namespace class ProcessingCoreImpl : public ProcessingCore { public: - ProcessingCoreImpl( - std::shared_ptr db, - size_t subTaskProcessingTime, - size_t maximalProcessingSubTaskCount) - : m_db(db) - , m_subTaskProcessingTime(subTaskProcessingTime) - , m_maximalProcessingSubTaskCount(maximalProcessingSubTaskCount) - , m_processingSubTaskCount(0) + ProcessingCoreImpl( std::shared_ptr db, size_t subTaskProcessingTime, + size_t maximalProcessingSubTaskCount ) : + m_db( std::move( db ) ), m_subTaskProcessingTime( subTaskProcessingTime ), + m_maximalProcessingSubTaskCount( maximalProcessingSubTaskCount ), m_processingSubTaskCount( 0 ) { } bool SetProcessingTypeFromJson(std::string jsondata) override @@ -276,7 +274,7 @@ int main(int argc, char* argv[]) std::vector pubsubBootstrapPeers; if (options->remote) { - pubsubBootstrapPeers = std::move(std::vector({ *options->remote })); + pubsubBootstrapPeers = std::vector( { *options->remote } ); } pubs->Start(40001, pubsubBootstrapPeers); diff --git a/src/crdt/crdt_datastore.hpp b/src/crdt/crdt_datastore.hpp index 20229f9c..838b18fd 100644 --- a/src/crdt/crdt_datastore.hpp +++ b/src/crdt/crdt_datastore.hpp @@ -15,7 +15,6 @@ #include #include #include -#include namespace sgns::crdt { diff --git a/src/crdt/impl/crdt_datastore.cpp b/src/crdt/impl/crdt_datastore.cpp index 5ca5693d..6b3525b1 100644 --- a/src/crdt/impl/crdt_datastore.cpp +++ b/src/crdt/impl/crdt_datastore.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace sgns::crdt { From 724025c7532866afd3802907794babca1aa90400 Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:26:36 -0300 Subject: [PATCH 3/8] refactor: Removed else after return --- src/blockchain/impl/types.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/blockchain/impl/types.cpp b/src/blockchain/impl/types.cpp index 4141f294..e5bde1f7 100644 --- a/src/blockchain/impl/types.cpp +++ b/src/blockchain/impl/types.cpp @@ -32,6 +32,7 @@ namespace sgns::blockchain { } return key; } + outcome::result idToStringKey(crdt::GlobalDB &db, const primitives::BlockId &id) { auto key = visit_in_place( @@ -41,14 +42,14 @@ namespace sgns::blockchain { }, [&db](const base::Hash256 &hash) { auto key = db.Get({hash.toReadableString()}); + if (key) { return std::to_string(BufferToNumber(key.value()).value()); } - else - { - return std::string{}; - } + + return std::string{}; + }); if (key.empty()) { From 8d5b014eee5b0d10f0095c5d6ed70802d10b68de Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:27:09 -0300 Subject: [PATCH 4/8] refactor: Removed unnecessary c_str calls in subtask result storage --- .../processing_subtask_result_storage_impl.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/processing/impl/processing_subtask_result_storage_impl.cpp b/src/processing/impl/processing_subtask_result_storage_impl.cpp index bbe78ad0..6fca3cbd 100644 --- a/src/processing/impl/processing_subtask_result_storage_impl.cpp +++ b/src/processing/impl/processing_subtask_result_storage_impl.cpp @@ -1,10 +1,11 @@ #include "processing_subtask_result_storage_impl.hpp" #include +#include namespace sgns::processing { - SubTaskResultStorageImpl::SubTaskResultStorageImpl(std::shared_ptr db) - : m_db(db) + SubTaskResultStorageImpl::SubTaskResultStorageImpl( std::shared_ptr db ) : + m_db( std::move( db ) ) { } @@ -13,16 +14,13 @@ namespace sgns::processing sgns::crdt::GlobalDB::Buffer data; data.put(result.SerializeAsString()); - auto taskId = - m_db->Put( - sgns::crdt::HierarchicalKey((boost::format("results/%s") % result.subtaskid()).str().c_str()), - data); + auto taskId = m_db->Put( + sgns::crdt::HierarchicalKey( ( boost::format( "results/%s" ) % result.subtaskid() ).str() ), data ); } void SubTaskResultStorageImpl::RemoveSubTaskResult(const std::string& subTaskId) { - m_db->Remove( - sgns::crdt::HierarchicalKey((boost::format("results/%s") % subTaskId).str().c_str())); + m_db->Remove( sgns::crdt::HierarchicalKey( ( boost::format( "results/%s" ) % subTaskId ).str() ) ); } void SubTaskResultStorageImpl::GetSubTaskResults( @@ -31,7 +29,7 @@ namespace sgns::processing { for (const auto& subTaskId : subTaskIds) { - auto data = m_db->Get(sgns::crdt::HierarchicalKey((boost::format("results/%s") % subTaskId).str().c_str())); + auto data = m_db->Get( sgns::crdt::HierarchicalKey( ( boost::format( "results/%s" ) % subTaskId ).str() ) ); if (data) { SGProcessing::SubTaskResult result; From bf9d1f0a1546aae3d6a21b4ae61f1cc546621084 Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:30:33 -0300 Subject: [PATCH 5/8] fix: Block hashing is done on the protobuf serialized output --- .../impl/key_value_block_header_repository.cpp | 18 ++++++++---------- .../impl/key_value_block_header_repository.hpp | 5 +++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/blockchain/impl/key_value_block_header_repository.cpp b/src/blockchain/impl/key_value_block_header_repository.cpp index dfceafb7..9869eb2c 100644 --- a/src/blockchain/impl/key_value_block_header_repository.cpp +++ b/src/blockchain/impl/key_value_block_header_repository.cpp @@ -4,7 +4,6 @@ #include "blockchain/impl/common.hpp" #include "blockchain/impl/storage_util.hpp" -#include "scale/scale.hpp" #include "blockchain/impl/proto/SGBlocks.pb.h" using sgns::base::Hash256; @@ -34,9 +33,9 @@ namespace sgns::blockchain const primitives::BlockNumber &number ) const { OUTCOME_TRY( ( auto &&, header ), getBlockHeader( number ) ); - OUTCOME_TRY( ( auto &&, enc_header ), scale::encode( header ) ); + auto serializedHeader = GetHeaderSerializedData( header ); - return hasher_->blake2b_256( enc_header ); + return hasher_->blake2b_256( serializedHeader ); } outcome::result KeyValueBlockHeaderRepository::getBlockHeader( const BlockId &id ) const @@ -49,14 +48,12 @@ namespace sgns::blockchain return ( isNotFoundError( header_res.error() ) ) ? Error::BLOCK_NOT_FOUND : header_res.error(); } - //return scale::decode(header_res.value()); return GetBlockHeaderFromSerialized( header_res.value().toVector() ); } outcome::result KeyValueBlockHeaderRepository::putBlockHeader( const primitives::BlockHeader &header ) { - //OUTCOME_TRY((auto &&, encoded_header), scale::encode(header)); auto encoded_header = GetHeaderSerializedData( header ); auto header_hash = hasher_->blake2b_256( encoded_header ); @@ -92,25 +89,26 @@ namespace sgns::blockchain header_proto.set_block_number( header.number ); header_proto.set_state_root( header.state_root.toReadableString() ); header_proto.set_extrinsics_root( header.extrinsics_root.toReadableString() ); - //header_proto.set_digest(header.digest.toReadableString()); size_t size = header_proto.ByteSizeLong(); std::vector serialized_proto( size ); - header_proto.SerializeToArray( serialized_proto.data(), serialized_proto.size() ); + header_proto.SerializeToArray( serialized_proto.data(), static_cast( serialized_proto.size() ) ); return serialized_proto; } primitives::BlockHeader KeyValueBlockHeaderRepository::GetBlockHeaderFromSerialized( - const std::vector &serialized_data ) const + const std::vector &serialized_data ) { - primitives::BlockHeader block_header; SGBlocks::BlockHeaderData header_proto; - if ( !header_proto.ParseFromArray( serialized_data.data(), serialized_data.size() ) ) + + if ( !header_proto.ParseFromArray( serialized_data.data(), static_cast( serialized_data.size() ) ) ) { std::cerr << "Failed to parse BlockHeaderData from array." << std::endl; } + + primitives::BlockHeader block_header; block_header.parent_hash = ( Hash256::fromReadableString( header_proto.parent_hash() ) ).value(); block_header.number = header_proto.block_number(); block_header.state_root = ( Hash256::fromReadableString( header_proto.state_root() ) ).value(); diff --git a/src/blockchain/impl/key_value_block_header_repository.hpp b/src/blockchain/impl/key_value_block_header_repository.hpp index e485d804..343ab2e7 100644 --- a/src/blockchain/impl/key_value_block_header_repository.hpp +++ b/src/blockchain/impl/key_value_block_header_repository.hpp @@ -35,9 +35,10 @@ namespace sgns::blockchain } std::string GetHeaderPath() const; - std::vector GetHeaderSerializedData(const primitives::BlockHeader &header); - primitives::BlockHeader GetBlockHeaderFromSerialized(const std::vector &serialized_data) const ; + static std::vector GetHeaderSerializedData( const primitives::BlockHeader &header ); + + static primitives::BlockHeader GetBlockHeaderFromSerialized( const std::vector &serialized_data ); private: static constexpr std::string_view BLOCKCHAIN_PATH = "blockchain/"; From 8a0da26765cd68c8544f2e0da32096ec7d691c8b Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:32:21 -0300 Subject: [PATCH 6/8] tests: Fixed base CRDT by using proper initialization and destruction of keys --- test/testutil/storage/base_crdt_test.cpp | 109 +++++++++++++---------- test/testutil/storage/base_crdt_test.hpp | 22 +++-- 2 files changed, 77 insertions(+), 54 deletions(-) diff --git a/test/testutil/storage/base_crdt_test.cpp b/test/testutil/storage/base_crdt_test.cpp index 61b46eac..189559b3 100644 --- a/test/testutil/storage/base_crdt_test.cpp +++ b/test/testutil/storage/base_crdt_test.cpp @@ -1,19 +1,28 @@ +#include "testutil/storage/base_crdt_test.hpp" +#include -#include "testutil/storage/base_crdt_test.hpp" +#include #include #include "ipfs_pubsub/gossip_pubsub.hpp" #include -#include #include +#include #include #include #include #include #include -using GossipPubSub = sgns::ipfs_pubsub::GossipPubSub; -const std::string logger_config(R"( +using boost::asio::io_context; +using sgns::crdt::GlobalDB; +using sgns::crdt::KeyPairFileStorage; +using sgns::ipfs_pubsub::GossipPubSub; +using sgns::ipfs_pubsub::GossipPubSubTopic; + +namespace +{ + const std::string logger_config( R"( # ---------------- sinks: - name: console @@ -27,60 +36,70 @@ const std::string logger_config(R"( - name: libp2p - name: Gossip # ---------------- - )"); + )" ); +} + namespace test { + const std::string CRDTFixture::basePath = "CRDT.Datastore.TEST"; + bool CRDTFixture::initializedDb = false; + std::shared_ptr CRDTFixture::io_; + std::shared_ptr CRDTFixture::pubs_; + std::shared_ptr CRDTFixture::db_; - void CRDTFixture::open() + CRDTFixture::CRDTFixture( fs::path path ) : FSFixture( std::move( path ) ) { + } - auto logging_system = std::make_shared( std::make_shared( - // // Original LibP2P logging config - std::make_shared(), - // // Additional logging config for application - logger_config ) ); - logging_system->configure(); + CRDTFixture::~CRDTFixture() + { + auto it = std::unique_ptr( db_->GetDB()->NewIterator( rocksdb::ReadOptions() ) ); - libp2p::log::setLoggingSystem( logging_system ); - libp2p::log::setLevelOfGroup( "account_handling_test", soralog::Level::ERROR_ ); + // @todo Use DeleteRange once rocksdb gers and update + for ( it->SeekToFirst(); it->Valid(); it->Next() ) + { + BOOST_ASSERT( db_->GetDB()->Delete( rocksdb::WriteOptions(), it->key() ).ok() ); + } + } - auto loggerGlobalDB = sgns::base::createLogger( "GlobalDB" ); - loggerGlobalDB->set_level( spdlog::level::debug ); + void CRDTFixture::SetUpTestSuite() + { + // Logging antics + { + auto logging_system = + std::make_shared( std::make_shared( + std::make_shared(), logger_config ) ); - auto loggerDAGSyncer = sgns::base::createLogger( "GraphsyncDAGSyncer" ); - loggerDAGSyncer->set_level( spdlog::level::debug ); - pubs_ = std::make_shared( - sgns::crdt::KeyPairFileStorage( "CRDT.Datastore.TEST/unit_test" ).GetKeyPair().value() ); //Make Host Pubsubs - //std::vector receivedMessages; + BOOST_ASSERT( !logging_system->configure().has_error ); - //Start Pubsubs, add peers of other addresses. We'll probably use DHT Discovery bootstrapping in the future. - pubs_->Start( 40001, {pubs_->GetLocalAddress()}); + libp2p::log::setLoggingSystem( std::move( logging_system ) ); + libp2p::log::setLevelOfGroup( "account_handling_test", soralog::Level::ERROR_ ); - //Asio Context - io_ = std::make_shared(); + auto loggerGlobalDB = sgns::base::createLogger( "GlobalDB" ); + loggerGlobalDB->set_level( spdlog::level::debug ); - //Add to GlobalDB - auto globalDB = - std::make_shared( io_,"CRDT.Datastore.TEST.unit" , 40010, - std::make_shared( pubs_, "CRDT.Datastore.TEST.Channel" ) ); + auto loggerDAGSyncer = sgns::base::createLogger( "GraphsyncDAGSyncer" ); + loggerDAGSyncer->set_level( spdlog::level::debug ); + } - auto crdtOptions = sgns::crdt::CrdtOptions::DefaultOptions(); - globalDB->Init( crdtOptions ); - db_ = std::move(globalDB); - ASSERT_TRUE(db_) << "BaseCRDT_Test: db is nullptr"; - } + if ( !initializedDb ) + { + io_ = std::make_shared(); - CRDTFixture::CRDTFixture( fs::path path ) : FSFixture( std::move( path ) ) - { - } + pubs_ = + std::make_shared( KeyPairFileStorage( basePath + "/unit_test" ).GetKeyPair().value() ); - void CRDTFixture::SetUp() - { - open(); - } + db_ = std::make_shared( + io_, basePath + ".unit", 40010, + std::make_shared( pubs_, "CRDT.Datastore.TEST.Channel" ) ); - void CRDTFixture::TearDown() - { - // clear(); + pubs_->Start( 40001, { pubs_->GetLocalAddress() } ); + + auto crdtOptions = sgns::crdt::CrdtOptions::DefaultOptions(); + + BOOST_ASSERT( db_->Init( crdtOptions ).has_value() ); + + initializedDb = true; + } } -} // namespace test +} diff --git a/test/testutil/storage/base_crdt_test.hpp b/test/testutil/storage/base_crdt_test.hpp index 483d9795..3581e7c3 100644 --- a/test/testutil/storage/base_crdt_test.hpp +++ b/test/testutil/storage/base_crdt_test.hpp @@ -7,30 +7,34 @@ #ifndef _BASE_CRDT_TEST_HPP_ #define _BASE_CRDT_TEST_HPP_ + #include "testutil/storage/base_fs_test.hpp" #include +#include +#include +#include +#include #include "crdt/globaldb/globaldb.hpp" namespace test { - struct CRDTFixture : public FSFixture { CRDTFixture( fs::path path ); - void open(); - - void SetUp() override; + ~CRDTFixture() override; - void TearDown() override; + static void SetUpTestSuite(); - std::shared_ptr db_; - std::shared_ptr io_; - std::shared_ptr pubs_; + static const std::string basePath; + static bool initializedDb; + static std::shared_ptr io_; + static std::shared_ptr pubs_; + static std::shared_ptr db_; }; -} // namespace test +} #endif From df82209b711565d59f711d01ef4c3bbbdda663ed Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 07:33:03 -0300 Subject: [PATCH 7/8] tests: Fixed header tests by using header repo directly --- .../block_header_repository_test.cpp | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/test/src/blockchain/block_header_repository_test.cpp b/test/src/blockchain/block_header_repository_test.cpp index daf7bd9b..784aacb9 100644 --- a/test/src/blockchain/block_header_repository_test.cpp +++ b/test/src/blockchain/block_header_repository_test.cpp @@ -3,7 +3,6 @@ #include "blockchain/block_header_repository.hpp" #include "blockchain/impl/key_value_block_header_repository.hpp" -#include "blockchain/impl/storage_util.hpp" #include "crypto/hasher/hasher_impl.hpp" #include "primitives/block_header.hpp" #include "scale/scale.hpp" @@ -11,22 +10,23 @@ #include "testutil/outcome.hpp" #include "testutil/storage/base_crdt_test.hpp" -using sgns::base::Buffer; using sgns::base::Hash256; using sgns::blockchain::BlockHeaderRepository; using sgns::blockchain::KeyValueBlockHeaderRepository; -using sgns::blockchain::putWithPrefix; -using sgns::blockchain::prefix::Prefix; using sgns::primitives::BlockHeader; using sgns::primitives::BlockId; using sgns::primitives::BlockNumber; static BlockHeader defaultHeader( BlockNumber number ) { - return { .parent_hash = "ABCDEF"_hash256, - .number = number, - .state_root = "010203"_hash256, - .extrinsics_root = "DEADBEEF"_hash256 }; + BlockHeader header; + + header.parent_hash = "ABCDEF"_hash256; + header.number = number; + header.state_root = "010203"_hash256; + header.extrinsics_root = "DEADBEEF"_hash256; + + return header; } static BlockHeader defaultHeader() @@ -37,26 +37,10 @@ static BlockHeader defaultHeader() class BlockHeaderRepositoryFixture : public test::CRDTFixture { public: - BlockHeaderRepositoryFixture() : CRDTFixture( fs::path( "blockheaderrepotest.lvldb" ) ) - { - } - - void SetUp() override + BlockHeaderRepositoryFixture() : + CRDTFixture( fs::path( "blockheaderrepotest.lvldb" ) ), hasher( std::make_shared() ), + header_repo_( std::make_shared( db_, hasher, "testheader-963/" ) ) { - open(); - hasher = std::make_shared(); - std::string db_path_ = "testheader-963/"; - header_repo_ = std::make_shared( db_, hasher, db_path_ ); - } - - outcome::result storeHeader( BlockHeader &&header ) - { - OUTCOME_TRY( ( auto &&, enc_header ), sgns::scale::encode( header ) ); - - auto hash = hasher->blake2b_256( enc_header ); - BOOST_OUTCOME_TRYV2( auto &&, - putWithPrefix( *db_, Prefix::HEADER, header.number, hash, Buffer{ enc_header } ) ); - return hash; } std::shared_ptr hasher; @@ -78,11 +62,16 @@ const std::vector ParamValues = { 1, 42, 12345, 0, 0xFFFFFFFF }; */ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHashByNumber ) { - EXPECT_OUTCOME_TRUE( hash, storeHeader( defaultHeader( GetParam() ) ) ); - EXPECT_OUTCOME_TRUE( maybe_hash, header_repo_->getHashByNumber( GetParam() ) ); + auto number = GetParam(); + + auto header = defaultHeader( number ); + + EXPECT_OUTCOME_TRUE( hash, header_repo_->putBlockHeader( header ) ); + + EXPECT_OUTCOME_TRUE( maybe_hash, header_repo_->getHashByNumber( number ) ); ASSERT_THAT( hash, testing::ContainerEq( maybe_hash ) ); - EXPECT_OUTCOME_TRUE( maybe_another_hash, header_repo_->getHashById( GetParam() ) ); + EXPECT_OUTCOME_TRUE( maybe_another_hash, header_repo_->getHashById( number ) ); ASSERT_THAT( hash, testing::ContainerEq( maybe_another_hash ) ); } @@ -94,7 +83,9 @@ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHashByNumber ) */ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetNumberByHash ) { - EXPECT_OUTCOME_TRUE( hash, storeHeader( defaultHeader( GetParam() ) ) ); + auto header = defaultHeader( GetParam() ); + + EXPECT_OUTCOME_TRUE( hash, header_repo_->putBlockHeader( header ) ); EXPECT_OUTCOME_TRUE( maybe_number, header_repo_->getNumberByHash( hash ) ); ASSERT_EQ( GetParam(), maybe_number ); @@ -112,7 +103,7 @@ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHeader ) { auto header = defaultHeader( GetParam() ); - EXPECT_OUTCOME_TRUE( hash, storeHeader( BlockHeader( header ) ) ); + EXPECT_OUTCOME_TRUE( hash, header_repo_->putBlockHeader( header ) ); EXPECT_OUTCOME_TRUE( header_by_num, header_repo_->getBlockHeader( GetParam() ) ); EXPECT_OUTCOME_TRUE( header_by_hash, header_repo_->getBlockHeader( hash ) ); @@ -121,6 +112,8 @@ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHeader ) ASSERT_EQ( header_by_num, header ); } +INSTANTIATE_TEST_SUITE_P( Numbers, BlockHeaderRepositoryNumberParametrizedFixture, testing::ValuesIn( ParamValues ) ); + /** * @given HeaderBackend instance with several headers in the storage * @when accessing a header that wasn't put into storage @@ -134,12 +127,11 @@ TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader ) { if ( number != block_number ) { - EXPECT_OUTCOME_TRUE_1( storeHeader( defaultHeader( number ) ) ); + EXPECT_OUTCOME_TRUE_1( header_repo_->putBlockHeader( defaultHeader( number ) ) ); } } - BlockHeader not_in_storage = defaultHeader(); - not_in_storage.number = block_number; + BlockHeader not_in_storage = defaultHeader( block_number ); EXPECT_OUTCOME_TRUE( enc_header, sgns::scale::encode( not_in_storage ) ); auto hash = hasher->blake2b_256( enc_header ); @@ -155,5 +147,3 @@ TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader ) EXPECT_OUTCOME_TRUE_1( header_repo_->getHashById( hash ) ); EXPECT_OUTCOME_TRUE_1( header_repo_->getNumberById( block_number ) ); } - -INSTANTIATE_TEST_SUITE_P( Numbers, BlockHeaderRepositoryNumberParametrizedFixture, testing::ValuesIn( ParamValues ) ); From 1945adf50952e0f5e90c69a110839b7391e59668 Mon Sep 17 00:00:00 2001 From: Eduardo Menges Mattje Date: Fri, 10 May 2024 10:50:54 -0300 Subject: [PATCH 8/8] fix: rocksdb closes in destruction --- src/storage/rocksdb/rocksdb.cpp | 5 +++++ src/storage/rocksdb/rocksdb.hpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/storage/rocksdb/rocksdb.cpp b/src/storage/rocksdb/rocksdb.cpp index 3b95f203..e8508a7a 100644 --- a/src/storage/rocksdb/rocksdb.cpp +++ b/src/storage/rocksdb/rocksdb.cpp @@ -15,6 +15,11 @@ namespace sgns::storage { using BlockBasedTableOptions = ::ROCKSDB_NAMESPACE::BlockBasedTableOptions; + rocksdb::~rocksdb() + { + db_->Close(); + } + outcome::result> rocksdb::create( std::string_view path, Options options) { diff --git a/src/storage/rocksdb/rocksdb.hpp b/src/storage/rocksdb/rocksdb.hpp index ee7055b9..59b515b5 100644 --- a/src/storage/rocksdb/rocksdb.hpp +++ b/src/storage/rocksdb/rocksdb.hpp @@ -33,7 +33,7 @@ namespace sgns::storage using Slice = ::ROCKSDB_NAMESPACE::Slice; using QueryResult = std::map; - ~rocksdb() override = default; + ~rocksdb() override; /** * @brief Factory method to create an instance of rocksdb class.