Skip to content

Commit

Permalink
Change implementation from function pointer to abstract base class us…
Browse files Browse the repository at this point in the history
…ing shared pointers, minor fix for CMake build system #2717
  • Loading branch information
sgerbino committed Sep 12, 2018
1 parent 06e03bc commit ca6f5bc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions libraries/plugins/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file(GLOB HEADERS "include/steem/chain_plugin/*.hpp")
file(GLOB HEADERS "include/steem/plugins/chain/*.hpp")
add_library( chain_plugin
chain_plugin.cpp
${HEADERS} )
Expand All @@ -20,4 +20,4 @@ install( TARGETS
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
install( FILES ${HEADERS} DESTINATION "include/steem/chain_plugin" )
install( FILES ${HEADERS} DESTINATION "include/steem/plugins/chain" )
17 changes: 9 additions & 8 deletions libraries/plugins/chain/chain_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <steem/chain/database_exceptions.hpp>

#include <steem/plugins/chain/abstract_block_producer.hpp>
#include <steem/plugins/chain/chain_plugin.hpp>
#include <steem/plugins/statsd/utility.hpp>

Expand Down Expand Up @@ -91,7 +92,7 @@ class chain_plugin_impl

database db;
std::string block_generator_registrant;
fc::optional< chain_plugin::block_generator_func > block_generator;
std::shared_ptr< abstract_block_producer > block_generator;
};

struct write_request_visitor
Expand All @@ -101,7 +102,7 @@ struct write_request_visitor
database* db;
uint32_t skip = 0;
fc::optional< fc::exception >* except;
fc::optional< chain_plugin::block_generator_func > block_generator;
std::shared_ptr< abstract_block_producer > block_generator;

typedef bool result_type;

Expand Down Expand Up @@ -159,11 +160,11 @@ struct write_request_visitor

try
{
if( !block_generator.valid() )
if( !block_generator )
FC_THROW_EXCEPTION( chain_exception, "Received a generate block request, but no block generator has been registered." );

STATSD_START_TIMER( "chain", "write_time", "generate_block", 1.0f )
req->block = (*block_generator)(
req->block = block_generator->generate_block(
req->when,
req->witness_owner,
req->block_signing_private_key,
Expand Down Expand Up @@ -637,15 +638,15 @@ void chain_plugin::check_time_in_block( const steem::chain::signed_block& block
FC_ASSERT( block.timestamp.sec_since_epoch() <= max_accept_time );
}

void chain_plugin::register_block_generator( const std::string& plugin_name, block_generator_func func )
void chain_plugin::register_block_generator( const std::string& plugin_name, std::shared_ptr< abstract_block_producer > block_producer )
{
FC_ASSERT( get_state() != appbase::abstract_plugin::state::started, "Can only register a block generator when the chain_plugin is not started." );
FC_ASSERT( get_state() == appbase::abstract_plugin::state::initialized, "Can only register a block generator when the chain_plugin is initialized." );

if ( my->block_generator.valid() )
if ( my->block_generator )
wlog( "Overriding a previously registered block generator by: ${registrant}", ("registrant", my->block_generator_registrant) );

my->block_generator_registrant = plugin_name;
my->block_generator = func;
my->block_generator = block_producer;
}

} } } // namespace steem::plugis::chain::chain_apis
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <fc/time.hpp>

#include <steem/chain/database.hpp>

namespace steem { namespace plugins { namespace chain {

class abstract_block_producer {
public:
virtual steem::chain::signed_block generate_block(
fc::time_point_sec when,
const steem::chain::account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key,
uint32_t skip = steem::chain::database::skip_nothing) = 0;
};

} } } // steem::plugins::chain
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include <appbase/application.hpp>
#include <steem/chain/database.hpp>
#include <steem/plugins/chain/abstract_block_producer.hpp>

#include <boost/signals2.hpp>
#include <functional>

#define STEEM_CHAIN_PLUGIN_NAME "chain"

namespace steem { namespace plugins { namespace chain {
Expand Down Expand Up @@ -42,22 +43,14 @@ class chain_plugin : public plugin< chain_plugin >
uint32_t skip = database::skip_nothing
);

typedef std::function
< signed_block (
fc::time_point_sec,
const account_name_type&,
const fc::ecc::private_key&,
uint32_t ) >
block_generator_func;

/**
* Set a function to be called for block generation.
* Set a class to be called for block generation.
*
* This function must be called during abtract_plugin::plugin_initialize().
* Calling this during abstract_plugin::plugin_startup() will be too late
* and will not take effect.
*/
void register_block_generator( const std::string& plugin_name, block_generator_func func );
void register_block_generator( const std::string& plugin_name, std::shared_ptr< abstract_block_producer > block_producer );

/**
* Sets the time (in ms) that the write thread will hold the lock for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#include <fc/time.hpp>

#include <steem/plugins/chain/abstract_block_producer.hpp>
#include <steem/plugins/chain/chain_plugin.hpp>

namespace steem { namespace plugins { namespace witness {

class block_producer {
class block_producer : public chain::abstract_block_producer {
public:
block_producer( chain::database& db ) : _db( db ) {}

Expand Down
15 changes: 4 additions & 11 deletions libraries/plugins/witness/witness_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace detail {
_timer(io),
_chain_plugin( appbase::app().get_plugin< steem::plugins::chain::chain_plugin >() ),
_db( appbase::app().get_plugin< steem::plugins::chain::chain_plugin >().db() ),
_block_producer( _db )
_block_producer( std::make_shared< witness::block_producer >( _db ) )
{}

void on_pre_apply_block( const chain::block_notification& note );
Expand Down Expand Up @@ -107,12 +107,13 @@ namespace detail {

plugins::chain::chain_plugin& _chain_plugin;
chain::database& _db;
witness::block_producer _block_producer;
boost::signals2::connection _pre_apply_block_conn;
boost::signals2::connection _post_apply_block_conn;
boost::signals2::connection _pre_apply_transaction_conn;
boost::signals2::connection _pre_apply_operation_conn;
boost::signals2::connection _post_apply_operation_conn;

std::shared_ptr< witness::block_producer > _block_producer;
};

struct comment_options_extension_visitor
Expand Down Expand Up @@ -648,15 +649,7 @@ void witness_plugin::plugin_initialize(const boost::program_options::variables_m
ilog( "Initializing witness plugin" );
my = std::make_unique< detail::witness_plugin_impl >( appbase::app().get_io_service() );

my->_chain_plugin.register_block_generator(
get_name(),
[this] (fc::time_point_sec when,
const chain::account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key,
uint32_t skip) -> chain::signed_block
{
return my->_block_producer.generate_block(when, witness_owner, block_signing_private_key, skip);
});
my->_chain_plugin.register_block_generator( get_name(), my->_block_producer );

block_data_export_plugin* export_plugin = appbase::app().find_plugin< block_data_export_plugin >();
if( export_plugin != nullptr )
Expand Down

0 comments on commit ca6f5bc

Please sign in to comment.