Skip to content

Commit

Permalink
Merge pull request #2641 from bitshares/pr-2588-signed-block-header-api
Browse files Browse the repository at this point in the history
Extend get_block_header and get_block_header_batch APIs to return witness signatures
  • Loading branch information
abitmore authored Sep 16, 2022
2 parents 408153f + ade248e commit 60ef74b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
13 changes: 7 additions & 6 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,28 @@ void database_api_impl::cancel_all_subscriptions( bool reset_callback, bool rese
// //
//////////////////////////////////////////////////////////////////////

optional<block_header> database_api::get_block_header(uint32_t block_num)const
optional<signed_block_header> database_api::get_block_header(uint32_t block_num)const
{
return my->get_block_header( block_num );
}

optional<block_header> database_api_impl::get_block_header(uint32_t block_num) const
optional<signed_block_header> database_api_impl::get_block_header(uint32_t block_num) const
{
auto result = _db.fetch_block_by_number(block_num);
if(result)
return *result;
return {};
}
map<uint32_t, optional<block_header>> database_api::get_block_header_batch(const vector<uint32_t> block_nums)const
map<uint32_t, optional<signed_block_header>> database_api::get_block_header_batch(
const vector<uint32_t>& block_nums) const
{
return my->get_block_header_batch( block_nums );
}

map<uint32_t, optional<block_header>> database_api_impl::get_block_header_batch(
const vector<uint32_t> block_nums) const
map<uint32_t, optional<signed_block_header>> database_api_impl::get_block_header_batch(
const vector<uint32_t>& block_nums) const
{
map<uint32_t, optional<block_header>> results;
map<uint32_t, optional<signed_block_header>> results;
for (const uint32_t block_num : block_nums)
{
results[block_num] = get_block_header(block_num);
Expand Down
4 changes: 2 additions & 2 deletions libraries/app/database_api_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
void cancel_all_subscriptions(bool reset_callback, bool reset_market_subscriptions);

// Blocks and transactions
optional<block_header> get_block_header(uint32_t block_num)const;
map<uint32_t, optional<block_header>> get_block_header_batch(const vector<uint32_t> block_nums)const;
optional<signed_block_header> get_block_header(uint32_t block_num)const;
map<uint32_t, optional<signed_block_header>> get_block_header_batch(const vector<uint32_t>& block_nums)const;
optional<signed_block> get_block(uint32_t block_num)const;
processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const;

Expand Down
10 changes: 5 additions & 5 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ class database_api
/////////////////////////////

/**
* @brief Retrieve a block header
* @brief Retrieve a signed block header
* @param block_num Height of the block whose header should be returned
* @return header of the referenced block, or null if no matching block was found
*/
optional<block_header> get_block_header(uint32_t block_num)const;
optional<signed_block_header> get_block_header(uint32_t block_num)const;

/**
* @brief Retrieve multiple block header by block numbers
* @param block_nums vector containing heights of the block whose header should be returned
* @brief Retrieve multiple signed block headers by block numbers
* @param block_nums vector containing heights of the blocks whose headers should be returned
* @return array of headers of the referenced blocks, or null if no matching block was found
*/
map<uint32_t, optional<block_header>> get_block_header_batch(const vector<uint32_t> block_nums)const;
map<uint32_t, optional<signed_block_header>> get_block_header_batch(const vector<uint32_t>& block_nums)const;

/**
* @brief Retrieve a full, signed block
Expand Down
69 changes: 69 additions & 0 deletions tests/tests/database_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,75 @@ BOOST_AUTO_TEST_CASE( get_transaction_hex )

} FC_LOG_AND_RETHROW() }

/// Tests get_block, get_block_header, get_block_header_batch
BOOST_AUTO_TEST_CASE( get_block_tests )
{ try {

generate_block();

ACTORS( (nathan) );
auto block1 = generate_block( ~graphene::chain::database::skip_witness_signature );
auto block2 = generate_block( ~graphene::chain::database::skip_witness_signature );

fund( nathan_id(db) );
auto block3 = generate_block( ~graphene::chain::database::skip_witness_signature );

idump( (block1)(block2)(block3) );

uint32_t head_block_num = db.head_block_num();

graphene::app::database_api db_api(db);
auto head_block = db_api.get_block( head_block_num );
idump( (head_block) );
BOOST_REQUIRE( head_block.valid() );
BOOST_CHECK_EQUAL( head_block->block_num(), head_block_num );
BOOST_CHECK_EQUAL( head_block->transactions.size(), 1U );
BOOST_CHECK( head_block->witness_signature != signature_type() );
BOOST_CHECK( head_block->id() == block3.id() );

auto head_block_header = db_api.get_block_header( head_block_num );
BOOST_REQUIRE( head_block_header.valid() );
BOOST_CHECK_EQUAL( head_block_header->block_num(), head_block_num );
BOOST_CHECK( head_block_header->witness_signature == head_block->witness_signature );
BOOST_CHECK( head_block_header->id() == head_block->id() );

auto previous_block = db_api.get_block( head_block_num - 1 );
BOOST_REQUIRE( previous_block.valid() );
BOOST_CHECK_EQUAL( previous_block->block_num(), head_block_num - 1 );
BOOST_CHECK_EQUAL( previous_block->transactions.size(), 0 );
BOOST_CHECK( previous_block->id() == head_block->previous );
BOOST_CHECK( previous_block->witness_signature != signature_type() );
BOOST_CHECK( previous_block->witness_signature != head_block->witness_signature );
BOOST_CHECK( previous_block->id() == block2.id() );

auto previous_block_header = db_api.get_block_header( head_block_num - 1 );
BOOST_REQUIRE( previous_block_header.valid() );
BOOST_CHECK_EQUAL( previous_block_header->block_num(), head_block_num - 1 );
BOOST_CHECK( previous_block_header->witness_signature == previous_block->witness_signature );
BOOST_CHECK( previous_block_header->id() == previous_block->id() );

auto next_block = db_api.get_block( head_block_num + 1 );
BOOST_CHECK( !next_block.valid() );

auto next_block_header = db_api.get_block_header( head_block_num + 1 );
BOOST_CHECK( !next_block_header.valid() );

const auto block_headers = db_api.get_block_header_batch( { head_block_num, head_block_num + 1,
head_block_num - 1 } );
BOOST_REQUIRE_EQUAL( block_headers.size(), 3U );
BOOST_CHECK_THROW( block_headers.at( head_block_num + 2 ), std::out_of_range );
BOOST_CHECK( !block_headers.at( head_block_num + 1 ).valid() );
BOOST_REQUIRE( block_headers.at( head_block_num ).valid() );
BOOST_CHECK( block_headers.at( head_block_num )->block_num() == head_block_header->block_num() );
BOOST_CHECK( block_headers.at( head_block_num )->id() == head_block_header->id() );
BOOST_CHECK( block_headers.at( head_block_num )->witness_signature == head_block_header->witness_signature );
BOOST_CHECK( block_headers.at( head_block_num - 1 )->block_num() == previous_block_header->block_num() );
BOOST_CHECK( block_headers.at( head_block_num - 1 )->id() == previous_block_header->id() );
BOOST_CHECK( block_headers.at( head_block_num - 1 )->witness_signature
== previous_block_header->witness_signature );

} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE(verify_account_authority)
{
try {
Expand Down

0 comments on commit 60ef74b

Please sign in to comment.