From 93a108487d6499bb84ab2c8815ae463055e9d767 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Fri, 2 Oct 2015 17:19:03 -0400 Subject: [PATCH] Implement last_irreversible_block_num --- libraries/chain/db_block.cpp | 1 + libraries/chain/db_update.cpp | 36 +++++++++++++++++++ .../chain/include/graphene/chain/config.hpp | 4 ++- .../chain/include/graphene/chain/database.hpp | 1 + .../graphene/chain/global_property_object.hpp | 3 ++ .../include/graphene/chain/witness_object.hpp | 4 ++- 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/libraries/chain/db_block.cpp b/libraries/chain/db_block.cpp index 0d83b6e9..e19c218d 100644 --- a/libraries/chain/db_block.cpp +++ b/libraries/chain/db_block.cpp @@ -479,6 +479,7 @@ void database::_apply_block( const signed_block& next_block ) update_global_dynamic_data(next_block); update_signing_witness(signing_witness, next_block); + update_last_irreversible_block(); // Are we at the maintenance interval? if( maint_needed ) diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index 5dbb9116..c1c1eb5d 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -108,9 +108,45 @@ void database::update_signing_witness(const witness_object& signing_witness, con modify( signing_witness, [&]( witness_object& _wit ) { _wit.last_aslot = new_block_aslot; + _wit.last_confirmed_block_num = new_block.block_num(); } ); } +void database::update_last_irreversible_block() +{ + const global_property_object& gpo = get_global_properties(); + const dynamic_global_property_object& dpo = get_dynamic_global_properties(); + + 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 .7*10 = 7 + // 1 1 1 1 1 1 1 2 2 2 -> 1 + // 3 3 3 3 3 3 3 3 3 3 -> 3 + + 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; + + if( new_last_irreversible_block_num > dpo.last_irreversible_block_num ) + { + modify( dpo, [&]( dynamic_global_property_object& _dpo ) + { + _dpo.last_irreversible_block_num = new_last_irreversible_block_num; + } ); + } +} + void database::clear_expired_transactions() { //Look for expired transactions in the deduplication list, and remove them. diff --git a/libraries/chain/include/graphene/chain/config.hpp b/libraries/chain/include/graphene/chain/config.hpp index a16e6705..1f1bda1c 100644 --- a/libraries/chain/include/graphene/chain/config.hpp +++ b/libraries/chain/include/graphene/chain/config.hpp @@ -135,7 +135,9 @@ #define GRAPHENE_RECENTLY_MISSED_COUNT_INCREMENT 4 #define GRAPHENE_RECENTLY_MISSED_COUNT_DECREMENT 3 -#define GRAPHENE_CURRENT_DB_VERSION "test3d" +#define GRAPHENE_CURRENT_DB_VERSION "test5b" + +#define GRAPHENE_IRREVERSIBLE_THRESHOLD (70 * GRAPHENE_1_PERCENT) /** * Reserved Account IDs with special meaning diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index c099e1ef..be39b48a 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -422,6 +422,7 @@ namespace graphene { namespace chain { //////////////////// db_update.cpp //////////////////// void update_global_dynamic_data( const signed_block& b ); void update_signing_witness(const witness_object& signing_witness, const signed_block& new_block); + void update_last_irreversible_block(); void clear_expired_transactions(); void clear_expired_proposals(); void clear_expired_orders(); diff --git a/libraries/chain/include/graphene/chain/global_property_object.hpp b/libraries/chain/include/graphene/chain/global_property_object.hpp index 568ded67..ac272156 100644 --- a/libraries/chain/include/graphene/chain/global_property_object.hpp +++ b/libraries/chain/include/graphene/chain/global_property_object.hpp @@ -101,6 +101,8 @@ namespace graphene { namespace chain { */ uint32_t dynamic_flags = 0; + uint32_t last_irreversible_block_num = 0; + enum dynamic_flag_bits { /** @@ -129,6 +131,7 @@ FC_REFLECT_DERIVED( graphene::chain::dynamic_global_property_object, (graphene:: (current_aslot) (recent_slots_filled) (dynamic_flags) + (last_irreversible_block_num) ) FC_REFLECT_DERIVED( graphene::chain::global_property_object, (graphene::db::object), diff --git a/libraries/chain/include/graphene/chain/witness_object.hpp b/libraries/chain/include/graphene/chain/witness_object.hpp index 20df80fe..e3ac8fba 100644 --- a/libraries/chain/include/graphene/chain/witness_object.hpp +++ b/libraries/chain/include/graphene/chain/witness_object.hpp @@ -38,7 +38,8 @@ namespace graphene { namespace chain { vote_id_type vote_id; uint64_t total_votes = 0; string url; - int64_t total_missed = 0; + int64_t total_missed = 0; + uint32_t last_confirmed_block_num = 0; witness_object() : vote_id(vote_id_type::witness) {} }; @@ -72,4 +73,5 @@ FC_REFLECT_DERIVED( graphene::chain::witness_object, (graphene::db::object), (total_votes) (url) (total_missed) + (last_confirmed_block_num) )