Skip to content

Commit

Permalink
tests: Fixed header tests by using header repo directly
Browse files Browse the repository at this point in the history
  • Loading branch information
EduMenges committed May 10, 2024
1 parent 8a0da26 commit df82209
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions test/src/blockchain/block_header_repository_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@

#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"
#include "testutil/literals.hpp"
#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()
Expand All @@ -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<sgns::crypto::HasherImpl>() ),
header_repo_( std::make_shared<KeyValueBlockHeaderRepository>( db_, hasher, "testheader-963/" ) )
{
open();
hasher = std::make_shared<sgns::crypto::HasherImpl>();
std::string db_path_ = "testheader-963/";
header_repo_ = std::make_shared<KeyValueBlockHeaderRepository>( db_, hasher, db_path_ );
}

outcome::result<Hash256> 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<sgns::crypto::Hasher> hasher;
Expand All @@ -78,11 +62,16 @@ const std::vector<BlockNumber> 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 ) );
}

Expand All @@ -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 );

Expand All @@ -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 ) );
Expand All @@ -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
Expand All @@ -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 );
Expand All @@ -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 ) );

0 comments on commit df82209

Please sign in to comment.