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 ) );