Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Progress #6: Port producer plugin
Browse files Browse the repository at this point in the history
The producer plugin (now called producer_plugin) is now adapted to the
new appbase system, and appears to be working correctly.

Also, eliminate some boilerplate around OBJECT_CTOR macro! Now you can
OBJECT_CTOR(objname, (field1)(field2)) to indicate that field1 and field2
are dynamically allocated objects and need to be initialized with the
allocator.
  • Loading branch information
nathanielhourt committed Apr 12, 2017
1 parent 9c76e96 commit ca39631
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 208 deletions.
1 change: 0 additions & 1 deletion libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ add_subdirectory( egenesis )
add_subdirectory( net )
add_subdirectory( utilities )
add_subdirectory( appbase )
add_subdirectory( plugins )
18 changes: 9 additions & 9 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ void database::init_genesis(const genesis_state_type& genesis_state)
// Create global properties
create<global_property_object>([&](global_property_object& p) {
p.parameters = genesis_state.initial_parameters;
p.active_producers = initial_producers;
p.active_producers.resize(initial_producers.size());
std::copy(initial_producers.begin(), initial_producers.end(), p.active_producers.begin());
});
create<dynamic_global_property_object>([&](dynamic_global_property_object& p) {
p.time = genesis_state.initial_timestamp;
Expand Down Expand Up @@ -811,14 +812,13 @@ void database::open(const fc::path& data_dir, uint64_t shared_file_size,
init_genesis(genesis_loader());
});

fc::optional<signed_block> last_block = _block_id_to_block.last();
if( last_block.valid() )
{
// Rewind the database to the last irreversible block
with_write_lock([this] {
undo_all();
});
// Rewind the database to the last irreversible block
with_write_lock([this] {
undo_all();
});

fc::optional<signed_block> last_block = _block_id_to_block.last();
if(last_block.valid()) {
_fork_db.start_block( *last_block );
idump((last_block->id())(last_block->block_num()));
idump((head_block_id())(head_block_num()));
Expand Down Expand Up @@ -989,7 +989,7 @@ producer_id_type database::get_scheduled_producer(uint32_t slot_num)const
const dynamic_global_property_object& dpo = get_dynamic_global_properties();
uint64_t current_aslot = dpo.current_aslot + slot_num;
const auto& gpo = get<global_property_object>();
return gpo.active_producers[ current_aslot % gpo.active_producers.size() ];
return gpo.active_producers[current_aslot % gpo.active_producers.size()];
}

fc::time_point_sec database::get_slot_time(uint32_t slot_num)const
Expand Down
16 changes: 5 additions & 11 deletions libraries/chain/include/eos/chain/account_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,12 @@
namespace eos { namespace chain {
class account_object : public chainbase::object<account_object_type, account_object>
{
account_object() = delete;
public:
template<typename Constructor, typename Allocator>
account_object(Constructor&& c, chainbase::allocator<Allocator> a)
: name(a) {
c(*this);
}
OBJECT_CTOR(account_object, (name))

id_type id;
shared_string name;
public_key_type owner_key;
public_key_type active_key;
id_type id;
shared_string name;
public_key_type owner_key;
public_key_type active_key;
};

struct by_name;
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eos/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ namespace eos { namespace chain {
*/
class global_property_object : public chainbase::object<global_property_object_type, global_property_object>
{
OBJECT_CTOR(global_property_object)
OBJECT_CTOR(global_property_object, (active_producers))

id_type id;
chain_parameters parameters;
optional<chain_parameters> pending_parameters;

vector<producer_id_type> active_producers;
shared_vector<producer_id_type> active_producers;
};

/**
Expand Down
22 changes: 8 additions & 14 deletions libraries/chain/include/eos/chain/producer_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,15 @@
namespace eos { namespace chain {
class producer_object : public chainbase::object<producer_object_type, producer_object>
{
producer_object() = delete;
public:
template<typename Constructor, typename Allocator>
producer_object(Constructor&& c, chainbase::allocator<Allocator> a)
: owner_name(a), url(a) {
c(*this);
}
OBJECT_CTOR(producer_object, (owner_name)(url))

id_type id;
shared_string owner_name;
uint64_t last_aslot = 0;
public_key_type signing_key;
shared_string url;
int64_t total_missed = 0;
uint32_t last_confirmed_block_num = 0;
id_type id;
shared_string owner_name;
uint64_t last_aslot = 0;
public_key_type signing_key;
shared_string url;
int64_t total_missed = 0;
uint32_t last_confirmed_block_num = 0;
};

struct by_key;
Expand Down
13 changes: 12 additions & 1 deletion libraries/chain/include/eos/chain/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@

#include <chainbase/chainbase.hpp>

#define OBJECT_CTOR(NAME) \
#define OBJECT_CTOR1(NAME) \
NAME() = delete; \
public: \
template<typename Constructor, typename Allocator> \
NAME(Constructor&& c, chainbase::allocator<Allocator> a) \
{ c(*this); }
#define OBJECT_CTOR2_MACRO(x, y, field) ,field(a)
#define OBJECT_CTOR2(NAME, FIELDS) \
NAME() = delete; \
public: \
template<typename Constructor, typename Allocator> \
NAME(Constructor&& c, chainbase::allocator<Allocator> a) \
: id(0) BOOST_PP_SEQ_FOR_EACH(OBJECT_CTOR2_MACRO, _, FIELDS) \
{ c(*this); }
#define OBJECT_CTOR(...) BOOST_PP_OVERLOAD(OBJECT_CTOR, __VA_ARGS__)(__VA_ARGS__)

namespace eos { namespace chain {
using std::map;
Expand Down Expand Up @@ -89,6 +98,8 @@ namespace eos { namespace chain {

using chainbase::allocator;
using shared_string = boost::interprocess::basic_string<char, std::char_traits<char>, allocator<char>>;
template<typename T>
using shared_vector = boost::interprocess::vector<T, allocator<T>>;

using private_key_type = fc::ecc::private_key;
using chain_id_type = fc::sha256;
Expand Down
1 change: 0 additions & 1 deletion libraries/plugins/CMakeLists.txt

This file was deleted.

17 changes: 0 additions & 17 deletions libraries/plugins/producer/CMakeLists.txt

This file was deleted.

7 changes: 4 additions & 3 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory( chain_plugin )
add_subdirectory( net_plugin )
add_subdirectory( http_plugin )
add_subdirectory(net_plugin)
add_subdirectory(http_plugin)
add_subdirectory(chain_plugin)
add_subdirectory(producer_plugin)
12 changes: 6 additions & 6 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ void chain_plugin::set_program_options(options_description& cli, options_descrip
"flush shared memory changes to disk every N blocks")
;
cli.add_options()
("replay-blockchain", bpo::value<bool>()->default_value(false),
("replay-blockchain", bpo::bool_switch()->default_value(false),
"clear chain database and replay all blocks")
("resync-blockchain", bpo::value<bool>()->default_value(false),
("resync-blockchain", bpo::bool_switch()->default_value(false),
"clear chain database and block log")
;
}
Expand Down Expand Up @@ -97,17 +97,17 @@ void chain_plugin::plugin_startup() {
// my->db.set_flush_interval(my->flush_interval);
my->db.add_checkpoints(my->loaded_checkpoints);

if( my->replay ) {
if(my->replay) {
ilog("Replaying blockchain on user request.");
my->db.reindex(my->shared_memory_dir, my->shared_memory_size);
} else {
try {
ilog("opening shared memory from ${path}", ("path",my->shared_memory_dir.generic_string()));
ilog("Opening shared memory from ${path}", ("path",my->shared_memory_dir.generic_string()));
my->db.open(my->shared_memory_dir,
my->shared_memory_size,
[this] { return fc::json::from_file(my->genesis_file).as<eos::chain::genesis_state_type>(); });
} catch (const fc::assert_exception& e) {
wlog("Error opening database, attempting to replay blockchain");
} catch (const fc::exception& e) {
wlog("Error opening database, attempting to replay blockchain. Error: ${e}", ("e", e));
my->db.reindex(my->shared_memory_dir, my->shared_memory_size);
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/net_plugin/include/eos/net_plugin/net_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace eos {
net_plugin();
~net_plugin();

APPBASE_PLUGIN_REQUIRES((chain_plugin));
APPBASE_PLUGIN_REQUIRES((chain_plugin))
virtual void set_program_options(options_description& cli, options_description& cfg) override;

void plugin_initialize(const variables_map& options);
Expand Down
17 changes: 17 additions & 0 deletions plugins/producer_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
file(GLOB HEADERS "include/eos/producer_plugin/*.hpp")

add_library( producer_plugin
producer_plugin.cpp
)

target_link_libraries( producer_plugin chain_plugin appbase eos_chain eos_utilities )
target_include_directories( producer_plugin
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

install( TARGETS
producer_plugin

RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
*/
#pragma once

#include <eos/chain_plugin/chain_plugin.hpp>

#include <eos/chain/database.hpp>

#include <appbase/application.hpp>

#include <fc/thread/future.hpp>

namespace eos { namespace producer_plugin {
namespace eos {

namespace block_production_condition
{
namespace block_production_condition {
enum block_production_condition_enum
{
produced = 0,
Expand All @@ -49,43 +50,22 @@ namespace block_production_condition

class producer_plugin : public appbase::plugin<producer_plugin> {
public:
APPBASE_PLUGIN_REQUIRES()
APPBASE_PLUGIN_REQUIRES((chain_plugin))

virtual ~producer_plugin() {
try {
if( _block_production_task.valid() )
_block_production_task.cancel_and_wait(__FUNCTION__);
} catch(fc::canceled_exception&) {
//Expected exception. Move along.
} catch(fc::exception& e) {
edump((e.to_detail_string()));
}
}
producer_plugin();
virtual ~producer_plugin();

virtual void set_program_options(
boost::program_options::options_description &command_line_options,
boost::program_options::options_description &config_file_options
) override;

void set_block_production(bool allow) { _production_enabled = allow; }

virtual void plugin_initialize(const boost::program_options::variables_map& options);
virtual void plugin_startup();
virtual void plugin_shutdown();

private:
void schedule_production_loop();
block_production_condition::block_production_condition_enum block_production_loop();
block_production_condition::block_production_condition_enum maybe_produce_block(fc::mutable_variant_object& capture);

boost::program_options::variables_map _options;
bool _production_enabled = false;
uint32_t _required_producer_participation = 33 * EOS_1_PERCENT;
uint32_t _production_skip_flags = eos::chain::database::skip_nothing;

std::map<chain::public_key_type, fc::ecc::private_key> _private_keys;
std::set<chain::producer_id_type> _producers;
fc::future<void> _block_production_task;
std::unique_ptr<class producer_plugin_impl> my;
};

} } //eos::producer_plugin
} //eos
Loading

0 comments on commit ca39631

Please sign in to comment.