Skip to content

Commit

Permalink
dpos pbft optimise (EOSIO#121)
Browse files Browse the repository at this point in the history
* optimise for return value.

* reset timer upon state transition; refactor code;

* add global object to control pbft behavior; optimise checkpoint logic

* refactor pbft db persistence

* fix tests

* reset view change timer when global timeout is changed.

* reserve prepare only if my_prepare is on forks

* reserve prepare only if my_prepare is on forks

* Modify reserve prepare test case
  • Loading branch information
oldcold authored and qianxiaofeng committed Aug 20, 2019
1 parent c94fa01 commit a4e89a2
Show file tree
Hide file tree
Showing 19 changed files with 1,083 additions and 1,157 deletions.
5 changes: 5 additions & 0 deletions libraries/chain/chain_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ void chain_config2::validate() const{
EOS_ASSERT(std::numeric_limits<decltype(resource_greylist.size())>::max() > resource_greylist.size(), action_validate_exception, "Overflow in greylistwhen adding resource greylist!");
}

void chain_config3::validate() const{
EOS_ASSERT( view_change_timeout >= 1, action_validate_exception, "view change timeout must be at least 1");
EOS_ASSERT( pbft_checkpoint_granularity >= 100 && pbft_checkpoint_granularity % 100 == 0, action_validate_exception, "pbft checkpoint granularity must be multiple of 100 blocks");
}

} } // namespace eosio::chain
23 changes: 19 additions & 4 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using controller_index_set = index_set<
global_property2_multi_index,
dynamic_global_property_multi_index,
upgrade_property_multi_index,
global_property3_multi_index,
block_summary_multi_index,
transaction_multi_index,
generated_transaction_multi_index,
Expand Down Expand Up @@ -455,6 +456,13 @@ struct controller_impl {
wlog("no upo found, generating...");
db.create<upgrade_property_object>([](auto&){});
}

try {
db.get<global_property3_object>();
} catch( const boost::exception& e) {
wlog("no gpo3 found, generating...");
db.create<global_property3_object>([](auto&){});
}
}

~controller_impl() {
Expand Down Expand Up @@ -746,6 +754,7 @@ struct controller_impl {

// *bos end*

db.create<global_property3_object>([](auto&) {});

authorization.initialize_database();
resource_limits.initialize_database();
Expand Down Expand Up @@ -2526,13 +2535,15 @@ void controller::set_pbft_my_prepare(const block_id_type& id) {
}

block_id_type controller::get_pbft_prepared() const {
if (my->pbft_prepared) return my->pbft_prepared->id;
return block_id_type{};
block_id_type pp;
if (my->pbft_prepared) pp = my->pbft_prepared->id;
return pp;
}

block_id_type controller::get_pbft_my_prepare() const {
if (my->my_prepare) return my->my_prepare->id;
return block_id_type{};
block_id_type mp;
if (my->my_prepare) mp = my->my_prepare->id;
return mp;
}

void controller::reset_pbft_my_prepare() {
Expand Down Expand Up @@ -2701,6 +2712,10 @@ const upgrade_property_object& controller::get_upgrade_properties()const {
return my->db.get<upgrade_property_object>();
}

const global_property3_object& controller::get_pbft_properties()const {
return my->db.get<global_property3_object>();
}

bool controller::is_pbft_enabled() const {
return my->pbft_enabled;
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ namespace eosio { namespace chain {
*
* This will require a search over all forks
*/
void fork_database::set_bft_irreversible( block_id_type id ) {
void fork_database::set_bft_irreversible( const block_id_type& id ) {
auto b = get_block( id );
EOS_ASSERT( b, fork_db_block_not_found, "unable to find block id ${id}", ("id",id));

Expand Down Expand Up @@ -569,7 +569,7 @@ namespace eosio { namespace chain {
my->head = *my->index.get<by_lib_block_num>().begin();
}

void fork_database::set_latest_checkpoint( block_id_type id) {
void fork_database::set_latest_checkpoint( const block_id_type& id) {
auto b = get_block( id );
EOS_ASSERT( b, fork_db_block_not_found, "unable to find block id ${id}", ("id",id));

Expand Down
10 changes: 10 additions & 0 deletions libraries/chain/include/eosio/chain/chain_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ struct chain_config2 {
void validate()const;
};

struct chain_config3 {

uint16_t view_change_timeout = 6; /// the actual wait time will be `num * 5`
uint16_t pbft_checkpoint_granularity = 100; /// the interval of normal checkpoints must be a multiple of 100 * n;

void validate()const;
};

// *bos*
struct guaranteed_minimum_resources {
uint64_t ram_byte;
Expand All @@ -140,3 +148,5 @@ FC_REFLECT(eosio::chain::chain_config,
// *bos*
FC_REFLECT( eosio::chain::chain_config2, (actor_blacklist)(contract_blacklist)(resource_greylist) )
FC_REFLECT( eosio::chain::guaranteed_minimum_resources, (ram_byte)(cpu_us)(net_byte) )

FC_REFLECT( eosio::chain::chain_config3, (view_change_timeout) )
4 changes: 3 additions & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace eosio { namespace chain {
class global_property_object;
class global_property2_object; // *bos*
class upgrade_property_object;
class global_property3_object; // *bos*
class permission_object;
class account_object;
using resource_limits::resource_limits_manager;
Expand Down Expand Up @@ -305,7 +306,8 @@ namespace eosio { namespace chain {
signal<void(const header_confirmation&)> accepted_confirmation;
signal<void(const int&)> bad_alloc;

const upgrade_property_object& get_upgrade_properties()const;
const upgrade_property_object& get_upgrade_properties()const;
const global_property3_object& get_pbft_properties()const;
bool is_pbft_enabled()const;
bool under_maintenance()const;
void set_upo(uint32_t target_block_num);
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ namespace eosio { namespace chain {
*/
signal<void(block_state_ptr)> irreversible;

void set_bft_irreversible( block_id_type id );
void set_bft_irreversible( const block_id_type& id );

void set_latest_checkpoint( block_id_type id);
void set_latest_checkpoint( const block_id_type& id);

void mark_pbft_prepared_fork(const block_state_ptr& h);

Expand Down
23 changes: 22 additions & 1 deletion libraries/chain/include/eosio/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ namespace eosio { namespace chain {
block_num_type upgrade_complete_block_num = 0;
};

class global_property3_object : public chainbase::object<global_property3_object_type, global_property3_object>
{
OBJECT_CTOR(global_property3_object)

id_type id;
chain_config3 configuration;
};


/**
* @class dynamic_global_property_object
Expand Down Expand Up @@ -108,6 +116,15 @@ namespace eosio { namespace chain {
>
>
>;

using global_property3_multi_index = chainbase::shared_multi_index_container<
global_property3_object,
indexed_by<
ordered_unique<tag<by_id>,
BOOST_MULTI_INDEX_MEMBER(global_property3_object, global_property3_object::id_type, id)
>
>
>;
}}

CHAINBASE_SET_INDEX_TYPE(eosio::chain::global_property_object, eosio::chain::global_property_multi_index)
Expand All @@ -116,6 +133,7 @@ CHAINBASE_SET_INDEX_TYPE(eosio::chain::dynamic_global_property_object,
// *bos*
CHAINBASE_SET_INDEX_TYPE(eosio::chain::global_property2_object, eosio::chain::global_property2_multi_index)
CHAINBASE_SET_INDEX_TYPE(eosio::chain::upgrade_property_object, eosio::chain::upgrade_property_multi_index)
CHAINBASE_SET_INDEX_TYPE(eosio::chain::global_property3_object, eosio::chain::global_property3_multi_index)

FC_REFLECT(eosio::chain::dynamic_global_property_object,
(global_action_sequence)
Expand All @@ -130,4 +148,7 @@ FC_REFLECT(eosio::chain::global_property2_object,
)
FC_REFLECT(eosio::chain::upgrade_property_object,
(upgrade_target_block_num)(upgrade_complete_block_num)
)
)
FC_REFLECT(eosio::chain::global_property3_object,
(configuration)
)
Loading

0 comments on commit a4e89a2

Please sign in to comment.