Skip to content

Commit

Permalink
Replace --bcd-trigger with --max-block-age #345
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Sep 2, 2016
1 parent f19a72f commit cb3af54
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 104 deletions.
32 changes: 0 additions & 32 deletions doc/bcd-trigger.md

This file was deleted.

69 changes: 11 additions & 58 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace steemit { namespace app {
network_broadcast_api::network_broadcast_api(const api_context& a):_app(a.app)
{
/// NOTE: cannot register callbacks in constructor because shared_from_this() is not valid.
_app.get_bcd_trigger( _bcd_trigger );
_app.get_max_block_age( _max_block_age );
}

void network_broadcast_api::on_api_startup()
Expand All @@ -123,68 +123,21 @@ namespace steemit { namespace app {
_applied_block_connection = connect_signal( _app.chain_database()->applied_block, *this, &network_broadcast_api::on_applied_block );
}

bool network_broadcast_api::check_bcd_trigger( const std::vector< std::pair< uint32_t, uint32_t > >& bcd_trigger )
bool network_broadcast_api::check_max_block_age( int32_t max_block_age )
{
FC_ASSERT( bcd_trigger.size() < 16 );
if( max_block_age < 0 )
return false;

fc::time_point_sec now = graphene::time::now();
std::shared_ptr< database > db = _app.chain_database();
const dynamic_global_property_object& dgpo = db->get_dynamic_global_properties();
fc::uint128_t slots = db->get_dynamic_global_properties().recent_slots_filled;
fc::time_point_sec now = graphene::time::now();

ilog( "Entering check_bcd_trigger: now=${now}", ("now", now) );

for( const std::pair< uint32_t, uint32_t >& trig : bcd_trigger )
{
// trig_slots is the number of slots which will be checked by the trigger.
uint32_t trig_slots = (trig.second + STEEMIT_BLOCK_INTERVAL - 1) / STEEMIT_BLOCK_INTERVAL;
ilog( "Testing bcd trigger: ${trig} trig_slots=${trig_slots}", ("trig", trig)("trig_slots", trig_slots) );
if( trig_slots > 128 )
{
elog( "Bad --bcd-trigger parameter specified (trig_slots=${s})", ("s", trig_slots) );
continue;
}

fc::uint128_t temp_slots = slots;

fc::time_point_sec db_now = db->head_block_time();
int64_t delta = (now - db_now).to_seconds();
ilog( "delta: ${delta}", ("delta", delta) );
if( delta > 0 )
delta = std::max( delta-STEEMIT_BLOCK_INTERVAL*2, int64_t(0) );
int64_t delta_slots = delta / STEEMIT_BLOCK_INTERVAL;
if( delta_slots < 0 )
{
uint64_t discarded_slots = uint64_t( -delta_slots );
// now is in the past, so rewind temp_slots by discarding the slots that will happen in the future
if( delta_slots >= 128 - trig_slots )
{
elog( "Bailed in check_bcd_trigger because the blockchain extends too far into the future" );
continue;
}
temp_slots >>= discarded_slots;
}
else
{
// now is in the future, so add temp_slots by shifting in new empty places
uint64_t empty_slots = delta_slots;
// if now is so far in the future that all slots are empty, all triggers should trip
if( empty_slots >= 128 )
return true;
temp_slots <<= empty_slots;
}
fc::uint128_t mask = 1;
mask <<= trig_slots;
mask -= 1;
temp_slots &= mask;
if( temp_slots.popcount() <= trig.first )
return true;
}
return false;
return ( dgpo.time < now - fc::seconds( max_block_age ) );
}

void network_broadcast_api::set_bcd_trigger( const std::vector< std::pair< uint32_t, uint32_t > > bcd_trigger )
void network_broadcast_api::set_max_block_age( int32_t max_block_age )
{
_bcd_trigger = bcd_trigger;
_max_block_age = max_block_age;
}

void network_broadcast_api::on_applied_block( const signed_block& b )
Expand Down Expand Up @@ -234,7 +187,7 @@ namespace steemit { namespace app {
void network_broadcast_api::broadcast_transaction(const signed_transaction& trx)
{
trx.validate();
FC_ASSERT( !check_bcd_trigger( _bcd_trigger ) );
FC_ASSERT( !check_max_block_age( _max_block_age ) );
_app.chain_database()->push_transaction(trx);
_app.p2p_node()->broadcast_transaction(trx);
}
Expand All @@ -255,7 +208,7 @@ namespace steemit { namespace app {

void network_broadcast_api::broadcast_transaction_with_callback(confirmation_callback cb, const signed_transaction& trx)
{
FC_ASSERT( !check_bcd_trigger( _bcd_trigger ) );
FC_ASSERT( !check_max_block_age( _max_block_age ) );
trx.validate();
_callbacks[trx.id()] = cb;
_callbacks_expirations[trx.expiration].push_back(trx.id());
Expand Down
16 changes: 7 additions & 9 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ namespace detail {
fc::create_directories(_data_dir / "blockchain/dblock");
fc::create_directories(_data_dir / "node/transaction_history");

fc::variant v_bcd_trigger = fc::json::from_string( _options->at("bcd-trigger").as<string>() );
fc::from_variant( v_bcd_trigger, _bcd_trigger );
_max_block_age =_options->at("max-block-age").as<int32_t>();
register_builtin_apis();

if( _options->count("resync-blockchain") )
Expand Down Expand Up @@ -806,10 +805,9 @@ namespace detail {
// notify GUI or something cool
}

void get_bcd_trigger( std::vector< std::pair< uint32_t, uint32_t > >& result )
void get_max_block_age( int32_t& result )
{
for( const std::pair< uint32_t, uint32_t >& p : _bcd_trigger )
result.push_back(p);
result = _max_block_age;
return;
}

Expand All @@ -829,7 +827,7 @@ namespace detail {
std::map<string, std::shared_ptr<abstract_plugin> > _plugins_enabled;
flat_map< std::string, std::function< fc::api_ptr( const api_context& ) > > _api_factories_by_name;
std::vector< std::string > _public_apis;
std::vector< std::pair< uint32_t, uint32_t > > _bcd_trigger;
int32_t _max_block_age = -1;

uint32_t allow_future_time = 5;
};
Expand Down Expand Up @@ -882,7 +880,7 @@ void application::set_program_options(boost::program_options::options_descriptio
("api-user", bpo::value< vector<string> >()->composing(), "API user specification, may be specified multiple times")
("public-api", bpo::value< vector<string> >()->composing()->default_value(default_apis, str_default_apis), "Set an API to be publicly available, may be specified multiple times")
("enable-plugin", bpo::value< vector<string> >()->composing()->default_value(default_plugins, str_default_plugins), "Plugin(s) to enable, may be specified multiple times")
("bcd-trigger,b", bpo::value< string >()->default_value("[[0,10],[85,300]]"), "JSON list of [nblocks,nseconds] pairs, see doc/bcd-trigger.md")
("max-block-age", bpo::value< int32_t >()->default_value(200), "Maximum age of head block when broadcasting tx via API")
;
command_line_options.add(configuration_file_options);
command_line_options.add_options()
Expand Down Expand Up @@ -963,9 +961,9 @@ fc::api_ptr application::create_api_by_name( const api_context& ctx )
return my->create_api_by_name( ctx );
}

void application::get_bcd_trigger( std::vector< std::pair< uint32_t, uint32_t > >& result )
void application::get_max_block_age( int32_t& result )
{
my->get_bcd_trigger( result );
my->get_max_block_age( result );
}

void application::shutdown_plugins()
Expand Down
8 changes: 4 additions & 4 deletions libraries/app/include/steemit/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ namespace steemit { namespace app {

void broadcast_block( const signed_block& block );

void set_bcd_trigger( const std::vector< std::pair< uint32_t, uint32_t > > bcd_trigger );
void set_max_block_age( int32_t max_block_age );

// implementation detail, not reflected
bool check_bcd_trigger( const std::vector< std::pair< uint32_t, uint32_t > >& bcd_trigger );
bool check_max_block_age( int32_t max_block_age );

/**
* @brief Not reflected, thus not accessible to API clients.
Expand All @@ -116,7 +116,7 @@ namespace steemit { namespace app {
map<transaction_id_type,confirmation_callback> _callbacks;
map<time_point_sec, vector<transaction_id_type> > _callbacks_expirations;

std::vector< std::pair< uint32_t, uint32_t > > _bcd_trigger;
int32_t _max_block_age = -1;

application& _app;
};
Expand Down Expand Up @@ -213,7 +213,7 @@ FC_API(steemit::app::network_broadcast_api,
(broadcast_transaction_with_callback)
(broadcast_transaction_synchronous)
(broadcast_block)
(set_bcd_trigger)
(set_max_block_age)
)
FC_API(steemit::app::network_node_api,
(get_info)
Expand Down
2 changes: 1 addition & 1 deletion libraries/app/include/steemit/app/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace steemit { namespace app {
*/
fc::api_ptr create_api_by_name( const api_context& ctx );

void get_bcd_trigger( std::vector< std::pair< uint32_t, uint32_t > >& result );
void get_max_block_age( int32_t& result );

private:
std::shared_ptr<detail::application_impl> my;
Expand Down

0 comments on commit cb3af54

Please sign in to comment.