Skip to content

Commit

Permalink
make debug_node_api to be responsible for deciphering call arguments
Browse files Browse the repository at this point in the history
Debug node plugin itself now offers normal internal interface. The change eliminates constant back and forth conversion between
private key and its WIF string form, very annoying when under debugger in unit tests.
  • Loading branch information
ABW authored and vogel76 committed Dec 16, 2024
1 parent 6275763 commit c632ca1
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 93 deletions.
21 changes: 17 additions & 4 deletions libraries/plugins/apis/debug_node_api/debug_node_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class debug_node_api_impl
(debug_throw_exception)
)

fc::optional<fc::ecc::private_key> get_debug_private_key( const std::string& debug_key_str ) const;

chain::chain_plugin& _chain;
chain::database& _db;
const hive::chain::block_read_i& _block_reader;
Expand All @@ -45,16 +47,27 @@ class debug_node_api_impl
appbase::application& theApp;
};

fc::optional<fc::ecc::private_key> debug_node_api_impl::get_debug_private_key( const std::string& debug_key_str ) const
{
fc::optional<fc::ecc::private_key> debug_private_key;
if( debug_key_str != "" )
{
debug_private_key = fc::ecc::private_key::wif_to_key( debug_key_str );
FC_ASSERT( debug_private_key.valid() );
}
return debug_private_key;
}

DEFINE_API_IMPL( debug_node_api_impl, debug_generate_blocks )
{
debug_generate_blocks_return ret;
_debug_node.debug_generate_blocks( ret, args, true ); // We can't use chain_plugin like generation because we need to be under lock and then chain plugin route does not work (because it has its own lock)
return ret;
return { _debug_node.debug_generate_blocks( get_debug_private_key( args.debug_key ), args.count, args.skip,
args.miss_blocks, true ) }; // We can't use chain_plugin-like generation because we need to be under lock and then chain plugin route does not work (because it has its own lock)
}

DEFINE_API_IMPL( debug_node_api_impl, debug_generate_blocks_until )
{
return { _debug_node.debug_generate_blocks_until( args.debug_key, args.head_block_time, args.generate_sparsely, chain::database::skip_nothing, true ) };
return { _debug_node.debug_generate_blocks_until( get_debug_private_key( args.debug_key ), args.head_block_time, args.generate_sparsely,
chain::database::skip_nothing, true ) }; // We can't use chain_plugin-like generation because we need to be under lock and then chain plugin route does not work (because it has its own lock)
}

DEFINE_API_IMPL( debug_node_api_impl, debug_get_head_block )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ using json_rpc::void_type;

namespace detail { class debug_node_api_impl; }

struct debug_generate_blocks_until_args
struct debug_generate_blocks_args
{
std::string debug_key;
fc::time_point_sec head_block_time;
bool generate_sparsely = true;
uint32_t count = 0;
uint32_t skip = hive::chain::database::skip_nothing;
uint32_t miss_blocks = 0;
};

struct debug_generate_blocks_return
{
uint32_t blocks = 0;
};

struct debug_generate_blocks_until_return
struct debug_generate_blocks_until_args
{
uint32_t blocks;
std::string debug_key;
fc::time_point_sec head_block_time;
bool generate_sparsely = true;
};

typedef debug_generate_blocks_return debug_generate_blocks_until_return;

typedef void_type debug_get_head_block_args;

Expand Down Expand Up @@ -172,12 +181,16 @@ class debug_node_api

} } } // hive::plugins::debug_node

FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_until_args,
(debug_key)(head_block_time)(generate_sparsely) )

FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_until_return,
FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_args,
(debug_key)(count)(skip)(miss_blocks) )

FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_return,
(blocks) )

FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_until_args,
(debug_key)(head_block_time)(generate_sparsely) )

FC_REFLECT( hive::plugins::debug_node::debug_get_head_block_return,
(block) )

Expand Down
53 changes: 15 additions & 38 deletions libraries/plugins/debug_node/debug_node_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,54 +326,31 @@ void debug_node_plugin::debug_set_vest_price( const hive::protocol::price& new_p
ilog( "Final price=${p}", ( "p", hive::protocol::price( dgpo.total_vesting_fund_hive, dgpo.total_vesting_shares ) ) );
}

uint32_t debug_node_plugin::debug_generate_blocks(const std::string& debug_key, uint32_t count, uint32_t skip, uint32_t miss_blocks, bool immediate_generation)
uint32_t debug_node_plugin::debug_generate_blocks( fc::optional<fc::ecc::private_key> debug_private_key,
uint32_t count, uint32_t skip, uint32_t miss_blocks, bool immediate_generation )
{
debug_generate_blocks_args args;
debug_generate_blocks_return ret;

args.debug_key = debug_key;
args.count = count;
args.skip = skip;
args.miss_blocks = miss_blocks;
debug_generate_blocks( ret, args, immediate_generation );
return ret.blocks;
}

void debug_node_plugin::debug_generate_blocks(debug_generate_blocks_return& ret, const debug_generate_blocks_args& args, bool immediate_generation)
{
if( args.count == 0 )
{
ret.blocks = 0;
return;
}
if( count == 0 )
return 0;

fc::optional<fc::ecc::private_key> debug_private_key;
chain::public_key_type debug_public_key;
const witness::witness_plugin::t_signing_keys* signing_keys = nullptr;
if( args.debug_key != "" )
{
debug_private_key = fc::ecc::private_key::wif_to_key( args.debug_key );
FC_ASSERT( debug_private_key.valid() );
if( debug_private_key.valid() )
debug_public_key = debug_private_key->get_public_key();
}
if( my->_witness_plugin_ptr )
{
signing_keys = &my->_witness_plugin_ptr->get_signing_keys();
}
if( not debug_private_key.valid() and ( signing_keys == nullptr or signing_keys->empty() ) )
{
elog( "Skipping generation because I don't know the private key" );
FC_ASSERT( false, "Skipping generation because I don't know the private key" );
ret.blocks = 0;
return;
return 0;
}

chain::database& db = database();

uint32_t slot = args.miss_blocks+1, produced = 0;
while( produced < args.count )
uint32_t slot = miss_blocks+1, produced = 0;
while( produced < count )
{
uint32_t new_slot = args.miss_blocks+1;
uint32_t new_slot = miss_blocks+1;
std::string scheduled_witness_name = db.get_scheduled_witness( slot );
fc::time_point_sec scheduled_time = db.get_slot_time( slot );
const chain::witness_object& scheduled_witness = db.get_witness( scheduled_witness_name );
Expand All @@ -396,7 +373,7 @@ void debug_node_plugin::debug_generate_blocks(debug_generate_blocks_return& ret,
}

auto generate_block_ctrl = std::make_shared< detail::debug_generate_block_flow_control >(scheduled_time,
scheduled_witness_name, private_key, args.skip, logging);
scheduled_witness_name, private_key, skip, logging);

if( immediate_generation ) // use this mode when called from debug node API (it takes write lock) - also in most unit tests
{
Expand Down Expand Up @@ -427,11 +404,11 @@ void debug_node_plugin::debug_generate_blocks(debug_generate_blocks_return& ret,
slot = new_slot;
}

ret.blocks = produced;
return produced;
}

uint32_t debug_node_plugin::debug_generate_blocks_until(
const std::string& debug_key,
fc::optional<fc::ecc::private_key> debug_private_key,
const fc::time_point_sec& head_block_time,
bool generate_sparsely,
uint32_t skip,
Expand All @@ -447,18 +424,18 @@ uint32_t debug_node_plugin::debug_generate_blocks_until(

if( generate_sparsely )
{
new_blocks += debug_generate_blocks( debug_key, 1, skip, 0, immediate_generation );
new_blocks += debug_generate_blocks( debug_private_key, 1, skip, 0, immediate_generation );
auto slots_to_miss = db.get_slot_at_time( head_block_time );
if( slots_to_miss > 1 )
{
slots_to_miss--;
new_blocks += debug_generate_blocks( debug_key, 1, skip, slots_to_miss, immediate_generation );
new_blocks += debug_generate_blocks( debug_private_key, 1, skip, slots_to_miss, immediate_generation );
}
}
else
{
while( db.head_block_time() < head_block_time )
new_blocks += debug_generate_blocks( debug_key, 1, skip, 0, immediate_generation );
new_blocks += debug_generate_blocks( debug_private_key, 1, skip, 0, immediate_generation );
}

return new_blocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ using namespace appbase;

namespace detail { class debug_node_plugin_impl; }

struct debug_generate_blocks_args
{
std::string debug_key;
uint32_t count = 0;
uint32_t skip = hive::chain::database::skip_nothing;
uint32_t miss_blocks = 0;
};

struct debug_generate_blocks_return
{
uint32_t blocks = 0;
};

class debug_node_plugin : public plugin< debug_node_plugin >
{
public:
Expand Down Expand Up @@ -95,20 +82,15 @@ class debug_node_plugin : public plugin< debug_node_plugin >
const hive::protocol::price& new_price,
fc::optional<protocol::transaction_id_type> transaction_id = fc::optional<protocol::transaction_id_type>() );

void debug_generate_blocks(
debug_generate_blocks_return& ret,
const debug_generate_blocks_args& args,
bool immediate_generation );

uint32_t debug_generate_blocks(
const std::string& debug_key,
fc::optional<fc::ecc::private_key> debug_key,
uint32_t count,
uint32_t skip,
uint32_t miss_blocks,
bool immediate_generation
);
uint32_t debug_generate_blocks_until(
const std::string& debug_key,
fc::optional<fc::ecc::private_key> debug_key,
const fc::time_point_sec& head_block_time,
bool generate_sparsely,
uint32_t skip,
Expand Down Expand Up @@ -150,13 +132,3 @@ class debug_node_plugin : public plugin< debug_node_plugin >
};

} } }

FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_args,
(debug_key)
(count)
(skip)
(miss_blocks)
)
FC_REFLECT( hive::plugins::debug_node::debug_generate_blocks_return,
(blocks)
)
12 changes: 6 additions & 6 deletions tests/unit/db_fixture/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,29 @@ asset_symbol_type database_fixture::get_new_smt_symbol( uint8_t token_decimal_pl
void database_fixture::generate_block(uint32_t skip, const fc::ecc::private_key& key, int miss_blocks)
{
skip |= default_skip;
db_plugin->debug_generate_blocks( key.key_to_wif(), 1, skip, miss_blocks, true );
db_plugin->debug_generate_blocks( key, 1, skip, miss_blocks, true );
}

uint32_t database_fixture::generate_blocks(const std::string& debug_key, uint32_t count, uint32_t skip)
uint32_t database_fixture::generate_blocks(const fc::ecc::private_key& debug_key, uint32_t count, uint32_t skip)
{
return db_plugin->debug_generate_blocks(debug_key, count, skip, 0, true);
}

uint32_t database_fixture::generate_blocks_until(const std::string& debug_key, const fc::time_point_sec& head_block_time,
uint32_t database_fixture::generate_blocks_until( const fc::ecc::private_key& key, const fc::time_point_sec& head_block_time,
bool generate_sparsely, uint32_t skip)
{
return db_plugin->debug_generate_blocks_until(debug_key, head_block_time, generate_sparsely, skip, true);
return db_plugin->debug_generate_blocks_until(key, head_block_time, generate_sparsely, skip, true);
}

void database_fixture::generate_blocks( uint32_t block_count )
{
auto produced = db_plugin->debug_generate_blocks( debug_key, block_count, default_skip, 0, true );
auto produced = db_plugin->debug_generate_blocks( init_account_priv_key, block_count, default_skip, 0, true );
BOOST_REQUIRE( produced == block_count );
}

void database_fixture::generate_blocks(fc::time_point_sec timestamp, bool miss_intermediate_blocks)
{
db_plugin->debug_generate_blocks_until( debug_key, timestamp, miss_intermediate_blocks, default_skip, true );
db_plugin->debug_generate_blocks_until( init_account_priv_key, timestamp, miss_intermediate_blocks, default_skip, true );
BOOST_REQUIRE( ( db->head_block_time() - timestamp ).to_seconds() < HIVE_BLOCK_INTERVAL );
}

Expand Down
5 changes: 2 additions & 3 deletions tests/unit/db_fixture/database_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ struct database_fixture {
chain::database* db = nullptr;
fc::ecc::private_key private_key = fc::ecc::private_key::generate();
fc::ecc::private_key init_account_priv_key = fc::ecc::private_key::regenerate( fc::sha256::hash( string( "init_key" ) ) );
string debug_key = init_account_priv_key.key_to_wif();
public_key_type init_account_pub_key = init_account_priv_key.get_public_key();
uint32_t default_skip = database::skip_nothing;

Expand Down Expand Up @@ -276,9 +275,9 @@ struct database_fixture {
const fc::ecc::private_key& key = committee,
int miss_blocks = 0);

uint32_t generate_blocks( const std::string& debug_key, uint32_t count, uint32_t skip );
uint32_t generate_blocks( const fc::ecc::private_key& key, uint32_t count, uint32_t skip );

uint32_t generate_blocks_until( const std::string& debug_key, const fc::time_point_sec& head_block_time,
uint32_t generate_blocks_until( const fc::ecc::private_key& key, const fc::time_point_sec& head_block_time,
bool generate_sparsely, uint32_t skip );

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/db_fixture/hived_proxy_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ class hived_proxy_fixture
return fixture->generate_block(skip, key, miss_blocks);
}

uint32_t generate_blocks( const std::string& debug_key, uint32_t count, uint32_t skip )
uint32_t generate_blocks( const fc::ecc::private_key& debug_key, uint32_t count, uint32_t skip )
{
FC_ASSERT(fixture);
return fixture->generate_blocks(debug_key, count, skip);
}

uint32_t generate_blocks_until( const std::string& debug_key, const fc::time_point_sec& head_block_time,
uint32_t generate_blocks_until( const fc::ecc::private_key& key, const fc::time_point_sec& head_block_time,
bool generate_sparsely, uint32_t skip )
{
FC_ASSERT(fixture);
return fixture->generate_blocks_until(debug_key, head_block_time, generate_sparsely, skip);
return fixture->generate_blocks_until(key, head_block_time, generate_sparsely, skip);
}

uint32_t get_last_irreversible_block_num()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugin_tests/witness_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct witness_fixture : public hived_fixture

void schedule_blocks( uint32_t count ) const
{
db_plugin->debug_generate_blocks( init_account_priv_key.key_to_wif(), count, default_skip, 0, false );
db_plugin->debug_generate_blocks( init_account_priv_key, count, default_skip, 0, false );
}

void schedule_block() const
Expand Down

0 comments on commit c632ca1

Please sign in to comment.