Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom id plumbing #3173 #3174

Merged
merged 2 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2590,14 +2590,15 @@ void database::initialize_evaluators()
}


void database::set_custom_operation_interpreter( const std::string& id, std::shared_ptr< custom_operation_interpreter > registry )
void database::register_custom_operation_interpreter( std::shared_ptr< custom_operation_interpreter > interpreter )
{
bool inserted = _custom_operation_interpreters.emplace( id, registry ).second;
FC_ASSERT( interpreter );
bool inserted = _custom_operation_interpreters.emplace( interpreter->get_custom_id(), interpreter ).second;
// This assert triggering means we're mis-configured (multiple registrations of custom JSON evaluator for same ID)
FC_ASSERT( inserted );
}

std::shared_ptr< custom_operation_interpreter > database::get_custom_json_evaluator( const std::string& id )
std::shared_ptr< custom_operation_interpreter > database::get_custom_json_evaluator( const custom_id_type& id )
{
auto it = _custom_operation_interpreters.find( id );
if( it != _custom_operation_interpreters.end() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <memory>

#include <steem/protocol/types.hpp>

namespace steem { namespace schema {
struct abstract_schema;
} }
Expand All @@ -18,6 +20,7 @@ class custom_operation_interpreter
public:
virtual void apply( const protocol::custom_json_operation& op ) = 0;
virtual void apply( const protocol::custom_binary_operation & op ) = 0;
virtual steem::protocol::custom_id_type get_custom_id() = 0;
virtual std::shared_ptr< steem::schema::abstract_schema > get_operation_schema() = 0;
};

Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/include/steem/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ namespace steem { namespace chain {
//////////////////// db_init.cpp ////////////////////

void initialize_evaluators();
void set_custom_operation_interpreter( const std::string& id, std::shared_ptr< custom_operation_interpreter > registry );
std::shared_ptr< custom_operation_interpreter > get_custom_json_evaluator( const std::string& id );
void register_custom_operation_interpreter( std::shared_ptr< custom_operation_interpreter > interpreter );
std::shared_ptr< custom_operation_interpreter > get_custom_json_evaluator( const custom_id_type& id );

/// Reset the object graph in-memory
void initialize_indexes();
Expand Down Expand Up @@ -585,7 +585,7 @@ namespace steem { namespace chain {
uint16_t _shared_file_full_threshold = 0;
uint16_t _shared_file_scale_rate = 0;

flat_map< std::string, std::shared_ptr< custom_operation_interpreter > > _custom_operation_interpreters;
flat_map< custom_id_type, std::shared_ptr< custom_operation_interpreter > > _custom_operation_interpreters;
std::string _json_schema;

util::advanced_benchmark_dumper _benchmark_dumper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace steem { namespace chain {
using protocol::operation;
using protocol::authority;
using protocol::account_name_type;
using protocol::custom_id_type;

class database;

Expand Down Expand Up @@ -131,7 +132,8 @@ class generic_custom_operation_interpreter
: public custom_operation_interpreter, public evaluator_registry< CustomOperationType >
{
public:
generic_custom_operation_interpreter( database& db ) : evaluator_registry< CustomOperationType >(db) {}
generic_custom_operation_interpreter( database& db, const custom_id_type& cid )
: evaluator_registry< CustomOperationType >(db), custom_id(cid) {}
virtual ~generic_custom_operation_interpreter() = default;

void apply_operations( const vector< CustomOperationType >& custom_operations, const operation& outer_o )
Expand Down Expand Up @@ -222,6 +224,14 @@ class generic_custom_operation_interpreter
{
return steem::schema::get_schema_for_type< CustomOperationType >();
}

virtual custom_id_type get_custom_id() override
{
return custom_id;
}

private:
custom_id_type custom_id;
};

} }
6 changes: 3 additions & 3 deletions libraries/plugins/follow/follow_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class follow_plugin_impl
void pre_operation( const operation_notification& op_obj );
void post_operation( const operation_notification& op_obj );

chain::database& _db;
chain::database& _db;
follow_plugin& _self;
boost::signals2::connection _pre_apply_operation_conn;
boost::signals2::connection _post_apply_operation_conn;
Expand Down Expand Up @@ -342,14 +342,14 @@ void follow_plugin::plugin_initialize( const boost::program_options::variables_m
my = std::make_unique< detail::follow_plugin_impl >( *this );

// Each plugin needs its own evaluator registry.
_custom_operation_interpreter = std::make_shared< generic_custom_operation_interpreter< steem::plugins::follow::follow_plugin_operation > >( my->_db );
_custom_operation_interpreter = std::make_shared< generic_custom_operation_interpreter< steem::plugins::follow::follow_plugin_operation > >( my->_db, name() );

// Add each operation evaluator to the registry
_custom_operation_interpreter->register_evaluator< follow_evaluator >( this );
_custom_operation_interpreter->register_evaluator< reblog_evaluator >( this );

// Add the registry to the database so the database can delegate custom ops to the plugin
my->_db.set_custom_operation_interpreter( name(), _custom_operation_interpreter );
my->_db.register_custom_operation_interpreter( _custom_operation_interpreter );

my->_pre_apply_operation_conn = my->_db.add_pre_apply_operation_handler( [&]( const operation_notification& note ){ my->pre_operation( note ); }, *this, 0 );
my->_post_apply_operation_conn = my->_db.add_post_apply_operation_handler( [&]( const operation_notification& note ){ my->post_operation( note ); }, *this, 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ namespace steem { namespace protocol {
{
flat_set< account_name_type > required_auths;
flat_set< account_name_type > required_posting_auths;
string id; ///< must be less than 32 characters long
custom_id_type id; ///< must be less than 32 characters long
string json; ///< must be proper utf8 / JSON string.

void validate()const;
Expand All @@ -587,7 +587,7 @@ namespace steem { namespace protocol {
flat_set< account_name_type > required_posting_auths;
vector< authority > required_auths;

string id; ///< must be less than 32 characters long
custom_id_type id; ///< must be less than 32 characters long
vector< char > data;

void validate()const;
Expand Down
1 change: 1 addition & 0 deletions libraries/protocol/include/steem/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace steem {
typedef safe<int64_t> share_type;
typedef uint16_t weight_type;
typedef uint32_t contribution_id_type;
typedef fixed_string<32> custom_id_type;


struct public_key_type
Expand Down