From 000db834642866b4c8e5fa5de1b142e6c6c42cc8 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Mon, 4 Nov 2019 18:32:21 +0100 Subject: [PATCH 1/2] Add index on witness_object::last_confirmed_block_num + use it for lib calculation --- libraries/chain/db_init.cpp | 2 ++ libraries/chain/db_maint.cpp | 15 +++++++++++++++ libraries/chain/db_update.cpp | 11 +++++++++-- .../include/graphene/chain/witness_object.hpp | 9 +++++++++ libraries/chain/small_objects.cpp | 1 + 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index bc7167d8bb..68492bacc0 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -680,6 +680,8 @@ void database::init_genesis(const genesis_state_type& genesis_state) p.active_witnesses.insert(witness_id_type(i)); } }); + for( const auto witness : get_global_properties().active_witnesses ) + modify( get( witness ), [] ( witness_object& w ) { w.is_active = true; } ); // Enable fees modify(get_global_properties(), [&genesis_state](global_property_object& p) { diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index fdf756f184..219aca7f92 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -286,6 +286,15 @@ void database::update_active_witnesses() } } ); + const witness_object& last_witness = wits.back(); + for( const auto witness_id : gpo.active_witnesses ) + { + const auto& witness = get( witness_id ); + if( _vote_tally_buffer[witness.vote_id] < _vote_tally_buffer[last_witness.vote_id] + || (_vote_tally_buffer[witness.vote_id] == _vote_tally_buffer[last_witness.vote_id] + && witness.id > last_witness.id) ) + modify( witness, [] ( witness_object& w ) { w.is_active = false; } ); + } modify( gpo, [&wits]( global_property_object& gp ) { gp.active_witnesses.clear(); @@ -296,6 +305,12 @@ void database::update_active_witnesses() return w.id; }); }); + for( const auto witness_id : gpo.active_witnesses ) + { + const auto& witness = get( witness_id ); + if( !witness.is_active ) + modify( witness, [] ( witness_object& w ) { w.is_active = true; } ); + } } FC_CAPTURE_AND_RETHROW() } diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index f98f09de6b..b715e7c7a5 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -120,7 +120,7 @@ void database::update_last_irreversible_block() // 3 3 3 3 3 3 3 3 3 3 -> 3 // 3 3 3 4 4 4 4 4 4 4 -> 4 - size_t offset = ((GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD) * wit_objs.size() / GRAPHENE_100_PERCENT); + const size_t offset = ((GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD) * wit_objs.size() / GRAPHENE_100_PERCENT); std::nth_element( wit_objs.begin(), wit_objs.begin() + offset, wit_objs.end(), []( const witness_object* a, const witness_object* b ) @@ -130,9 +130,16 @@ void database::update_last_irreversible_block() uint32_t new_last_irreversible_block_num = wit_objs[offset]->last_confirmed_block_num; + const auto& witness_idx = get_index_type().indices().get(); + auto itr = witness_idx.end(); + for( uint32_t i = gpo.active_witnesses.size() - offset; i > 0; --i ) + --itr; + if( new_last_irreversible_block_num != itr->last_confirmed_block_num ) + elog( "Mismatch: nlib = ${n} != lcb ${l}", ("n",new_last_irreversible_block_num)("l",itr->last_confirmed_block_num) ); + if( new_last_irreversible_block_num > dpo.last_irreversible_block_num ) { - modify( dpo, [&]( dynamic_global_property_object& _dpo ) + modify( dpo, [new_last_irreversible_block_num]( dynamic_global_property_object& _dpo ) { _dpo.last_irreversible_block_num = new_last_irreversible_block_num; } ); diff --git a/libraries/chain/include/graphene/chain/witness_object.hpp b/libraries/chain/include/graphene/chain/witness_object.hpp index 0c48f3bc17..9fac52c742 100644 --- a/libraries/chain/include/graphene/chain/witness_object.hpp +++ b/libraries/chain/include/graphene/chain/witness_object.hpp @@ -26,6 +26,8 @@ #include #include +#include + namespace graphene { namespace chain { using namespace graphene::db; @@ -44,6 +46,7 @@ namespace graphene { namespace chain { string url; int64_t total_missed = 0; uint32_t last_confirmed_block_num = 0; + bool is_active = false; witness_object() : vote_id(vote_id_type::witness) {} }; @@ -62,6 +65,12 @@ namespace graphene { namespace chain { >, ordered_unique< tag, member + >, + ordered_non_unique< tag, // non-unique because multiple can have last == 0 + composite_key, + member + > > > >; diff --git a/libraries/chain/small_objects.cpp b/libraries/chain/small_objects.cpp index ed68a4eaef..63ac56b97c 100644 --- a/libraries/chain/small_objects.cpp +++ b/libraries/chain/small_objects.cpp @@ -163,6 +163,7 @@ FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::witness_object, (graphene::db:: (url) (total_missed) (last_confirmed_block_num) + (is_active) ) FC_REFLECT_DERIVED_NO_TYPENAME( From 567798a04fd01ee3832385e3e312d315d678fa17 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Tue, 5 Nov 2019 08:26:09 +0100 Subject: [PATCH 2/2] Remove old calculation method --- libraries/chain/db_update.cpp | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index b715e7c7a5..fb0fca9a39 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -107,35 +107,15 @@ void database::update_last_irreversible_block() const global_property_object& gpo = get_global_properties(); const dynamic_global_property_object& dpo = get_dynamic_global_properties(); - // TODO for better performance, move this to db_maint, because only need to do it once per maintenance interval - vector< const witness_object* > wit_objs; - wit_objs.reserve( gpo.active_witnesses.size() ); - for( const witness_id_type& wid : gpo.active_witnesses ) - wit_objs.push_back( &(wid(*this)) ); - static_assert( GRAPHENE_IRREVERSIBLE_THRESHOLD > 0, "irreversible threshold must be nonzero" ); - // 1 1 1 2 2 2 2 2 2 2 -> 2 .3*10 = 3 - // 1 1 1 1 1 1 1 2 2 2 -> 1 - // 3 3 3 3 3 3 3 3 3 3 -> 3 - // 3 3 3 4 4 4 4 4 4 4 -> 4 - - const size_t offset = ((GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD) * wit_objs.size() / GRAPHENE_100_PERCENT); - - std::nth_element( wit_objs.begin(), wit_objs.begin() + offset, wit_objs.end(), - []( const witness_object* a, const witness_object* b ) - { - return a->last_confirmed_block_num < b->last_confirmed_block_num; - } ); - - uint32_t new_last_irreversible_block_num = wit_objs[offset]->last_confirmed_block_num; - + const size_t offset = (GRAPHENE_100_PERCENT - GRAPHENE_IRREVERSIBLE_THRESHOLD) + * gpo.active_witnesses.size() / GRAPHENE_100_PERCENT; const auto& witness_idx = get_index_type().indices().get(); auto itr = witness_idx.end(); for( uint32_t i = gpo.active_witnesses.size() - offset; i > 0; --i ) --itr; - if( new_last_irreversible_block_num != itr->last_confirmed_block_num ) - elog( "Mismatch: nlib = ${n} != lcb ${l}", ("n",new_last_irreversible_block_num)("l",itr->last_confirmed_block_num) ); + const auto new_last_irreversible_block_num = itr->last_confirmed_block_num; if( new_last_irreversible_block_num > dpo.last_irreversible_block_num ) {