Skip to content

Commit

Permalink
tests: Better structure for header tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EduMenges committed May 7, 2024
1 parent 968c9f3 commit d5500cf
Showing 1 changed file with 72 additions and 67 deletions.
139 changes: 72 additions & 67 deletions test/src/blockchain/block_header_repository_test.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@

#include "blockchain/block_header_repository.hpp"

#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>

#include <iostream>

#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_rocksdb_test.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::numberAndHashToLookupKey;
using sgns::blockchain::NumberToBuffer;
using sgns::blockchain::prependPrefix;
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 };
}

static BlockHeader defaultHeader()
{
return defaultHeader( 42 );
}

class BlockHeaderRepositoryFixture : public test::CRDTFixture
{
public:
Expand All @@ -43,9 +49,8 @@ class BlockHeaderRepositoryFixture : public test::CRDTFixture
header_repo_ = std::make_shared<KeyValueBlockHeaderRepository>( db_, hasher, db_path_ );
}

outcome::result<Hash256> storeHeader( BlockNumber num, BlockHeader &&header )
outcome::result<Hash256> storeHeader( BlockHeader &&header )
{
header.number = num;
OUTCOME_TRY( ( auto &&, enc_header ), sgns::scale::encode( header ) );

auto hash = hasher->blake2b_256( enc_header );
Expand All @@ -54,14 +59,6 @@ class BlockHeaderRepositoryFixture : public test::CRDTFixture
return hash;
}

static BlockHeader getDefaultHeader()
{
return { .parent_hash = "ABCDEF"_hash256,
.number = 42,
.state_root = "010203"_hash256,
.extrinsics_root = "DEADBEEF"_hash256 };
}

std::shared_ptr<sgns::crypto::Hasher> hasher;
std::shared_ptr<BlockHeaderRepository> header_repo_;
};
Expand All @@ -73,35 +70,6 @@ class BlockHeaderRepositoryNumberParametrizedFixture : public BlockHeaderReposit

const std::vector<BlockNumber> ParamValues = { 1, 42, 12345, 0, 0xFFFFFFFF };

/**
* @given HeaderBackend instance with several headers in the storage
* @when accessing a header that wasn't put into storage
* @then result is error
*/
TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader )
{
// auto chosen_number = ParamValues[0];
// for ( auto c : ParamValues )
// {
// if ( c != chosen_number )
// {
// EXPECT_OUTCOME_TRUE_1( storeHeader( c, getDefaultHeader() ) );
// }
// }
// BlockHeader not_in_storage = getDefaultHeader();
// not_in_storage.number = chosen_number;
// EXPECT_OUTCOME_TRUE( enc_header, sgns::scale::encode( not_in_storage ) );
// auto hash = hasher->blake2b_256( enc_header );
// EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( chosen_number ) );
// EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( hash ) );
// EXPECT_OUTCOME_FALSE_1( header_repo_->getHashById( chosen_number ) );
// EXPECT_OUTCOME_FALSE_1( header_repo_->getNumberById( hash ) );

// // doesn't require access to storage, as it basically returns its argument
// EXPECT_OUTCOME_TRUE_1( header_repo_->getHashById( hash ) );
// EXPECT_OUTCOME_TRUE_1( header_repo_->getNumberById( chosen_number ) );
}

/**
* @given HeaderBackend instance
* @when learning block hash by its number through HeaderBackend
Expand All @@ -110,12 +78,12 @@ TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader )
*/
TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHashByNumber )
{
//EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader()));
//EXPECT_OUTCOME_TRUE(maybe_hash, header_repo_->getHashByNumber(GetParam()));
//ASSERT_THAT(hash, testing::ContainerEq(maybe_hash));
//EXPECT_OUTCOME_TRUE(maybe_another_hash,
// header_repo_->getHashById(GetParam()));
//ASSERT_THAT(hash, testing::ContainerEq(maybe_another_hash));
EXPECT_OUTCOME_TRUE( hash, storeHeader( defaultHeader( GetParam() ) ) );
EXPECT_OUTCOME_TRUE( maybe_hash, header_repo_->getHashByNumber( GetParam() ) );
ASSERT_THAT( hash, testing::ContainerEq( maybe_hash ) );

EXPECT_OUTCOME_TRUE( maybe_another_hash, header_repo_->getHashById( GetParam() ) );
ASSERT_THAT( hash, testing::ContainerEq( maybe_another_hash ) );
}

/**
Expand All @@ -126,12 +94,12 @@ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHashByNumber )
*/
TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetNumberByHash )
{
//EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader()));
//EXPECT_OUTCOME_TRUE(maybe_number, header_repo_->getNumberByHash(hash));
//ASSERT_EQ(GetParam(), maybe_number);
//EXPECT_OUTCOME_TRUE(maybe_another_number,
// header_repo_->getNumberById(GetParam()));
//ASSERT_EQ(GetParam(), maybe_another_number);
EXPECT_OUTCOME_TRUE( hash, storeHeader( defaultHeader( GetParam() ) ) );
EXPECT_OUTCOME_TRUE( maybe_number, header_repo_->getNumberByHash( hash ) );
ASSERT_EQ( GetParam(), maybe_number );

EXPECT_OUTCOME_TRUE( maybe_another_number, header_repo_->getNumberById( GetParam() ) );
ASSERT_EQ( GetParam(), maybe_another_number );
}

/**
Expand All @@ -142,13 +110,50 @@ TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetNumberByHash )
*/
TEST_P( BlockHeaderRepositoryNumberParametrizedFixture, GetHeader )
{
//EXPECT_OUTCOME_TRUE(hash, storeHeader(GetParam(), getDefaultHeader()));
//EXPECT_OUTCOME_TRUE(header_by_num, header_repo_->getBlockHeader(GetParam()));
//EXPECT_OUTCOME_TRUE(header_by_hash, header_repo_->getBlockHeader(hash));
//auto header_should_be = getDefaultHeader();
//header_should_be.number = GetParam();
//ASSERT_EQ(header_by_hash, header_should_be);
//ASSERT_EQ(header_by_num, header_should_be);
auto header = defaultHeader( GetParam() );

EXPECT_OUTCOME_TRUE( hash, storeHeader( BlockHeader( header ) ) );

EXPECT_OUTCOME_TRUE( header_by_num, header_repo_->getBlockHeader( GetParam() ) );
EXPECT_OUTCOME_TRUE( header_by_hash, header_repo_->getBlockHeader( hash ) );

ASSERT_EQ( header_by_hash, header );
ASSERT_EQ( header_by_num, header );
}

/**
* @given HeaderBackend instance with several headers in the storage
* @when accessing a header that wasn't put into storage
* @then result is error
*/
TEST_F( BlockHeaderRepositoryFixture, UnexistingHeader )
{
auto block_number = ParamValues[0];

for ( auto number : ParamValues )
{
if ( number != block_number )
{
EXPECT_OUTCOME_TRUE_1( storeHeader( defaultHeader( number ) ) );
}
}

BlockHeader not_in_storage = defaultHeader();
not_in_storage.number = block_number;

EXPECT_OUTCOME_TRUE( enc_header, sgns::scale::encode( not_in_storage ) );
auto hash = hasher->blake2b_256( enc_header );

EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( block_number ) );
EXPECT_OUTCOME_FALSE_1( header_repo_->getBlockHeader( hash ) );

EXPECT_OUTCOME_FALSE_1( header_repo_->getHashById( block_number ) );
EXPECT_OUTCOME_FALSE_1( header_repo_->getNumberById( hash ) );

// These methods work because they don't have to access the storage, they just return the params.
// This happens because id is a variant of both hash and number.
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 d5500cf

Please sign in to comment.