From c34415b403871f1042fee7d093465d4945b3db12 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Fri, 9 Jun 2023 08:10:26 +0000 Subject: [PATCH] Fixes for public testnet --- .gitlab-ci.yml | 6 +- libraries/chain/db_block.cpp | 18 +-- libraries/chain/db_witness_schedule.cpp | 140 ++++++++---------- .../chain/include/graphene/chain/database.hpp | 4 +- libraries/chain/son_wallet_evaluator.cpp | 28 +++- 5 files changed, 94 insertions(+), 102 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c443fe98..f300e82b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,8 +29,6 @@ build-mainnet: - build/libraries/ - build/programs/ - build/tests/ - rules: - - if: $CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "beatrice" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "beatrice" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" tags: - builder @@ -81,9 +79,7 @@ build-testnet: - build/libraries/ - build/programs/ - build/tests/ - rules: - - if: $CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "beatrice" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "beatrice" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" - when: manual + when: manual tags: - builder diff --git a/libraries/chain/db_block.cpp b/libraries/chain/db_block.cpp index adabcffe..79b24464 100644 --- a/libraries/chain/db_block.cpp +++ b/libraries/chain/db_block.cpp @@ -739,13 +739,11 @@ void database::_apply_block( const signed_block& next_block ) if (global_props.parameters.witness_schedule_algorithm == GRAPHENE_WITNESS_SCHEDULED_ALGORITHM) { update_witness_schedule(next_block); - bool need_to_update_son_schedule = false; - for(const auto& active_sons : global_props.active_sons){ - if(!active_sons.second.empty()) - need_to_update_son_schedule = true; - } - if(need_to_update_son_schedule) { - update_son_schedule(next_block); + + for(const auto& active_sons : global_props.active_sons) { + if(!active_sons.second.empty()) { + update_son_schedule(active_sons.first, next_block); + } } } @@ -783,15 +781,11 @@ void database::_apply_block( const signed_block& next_block ) if (global_props.parameters.witness_schedule_algorithm == GRAPHENE_WITNESS_SHUFFLED_ALGORITHM) { update_witness_schedule(); - bool need_update_son_schedule = false; for(const auto& active_sidechain_type : active_sidechain_types(dynamic_global_props.time)) { if(global_props.active_sons.at(active_sidechain_type).size() > 0) { - need_update_son_schedule = true; + update_son_schedule(active_sidechain_type); } } - if(need_update_son_schedule) { - update_son_schedule(); - } } if( !_node_property_object.debug_updates.empty() ) diff --git a/libraries/chain/db_witness_schedule.cpp b/libraries/chain/db_witness_schedule.cpp index 12d4a6dd..d4874b29 100644 --- a/libraries/chain/db_witness_schedule.cpp +++ b/libraries/chain/db_witness_schedule.cpp @@ -200,44 +200,41 @@ void database::update_witness_schedule() } } -void database::update_son_schedule() +void database::update_son_schedule(sidechain_type type) { const global_property_object& gpo = get_global_properties(); - for(const auto& active_sidechain_type : active_sidechain_types(head_block_time())) + const son_schedule_object& sidechain_sso = get(son_schedule_id_type(get_son_schedule_id(type))); + if( gpo.active_sons.at(type).size() != 0 && + head_block_num() % gpo.active_sons.at(type).size() == 0) { - const son_schedule_object& sidechain_sso = get(son_schedule_id_type(get_son_schedule_id(active_sidechain_type))); - if( gpo.active_sons.at(active_sidechain_type).size() != 0 && - head_block_num() % gpo.active_sons.at(active_sidechain_type).size() == 0) + modify( sidechain_sso, [&]( son_schedule_object& _sso ) { - modify( sidechain_sso, [&]( son_schedule_object& _sso ) + _sso.current_shuffled_sons.clear(); + _sso.current_shuffled_sons.reserve( gpo.active_sons.at(type).size() ); + + for ( const auto &w : gpo.active_sons.at(type) ) { + _sso.current_shuffled_sons.push_back(w.son_id); + } + + auto now_hi = uint64_t(head_block_time().sec_since_epoch()) << 32; + + for (uint32_t i = 0; i < _sso.current_shuffled_sons.size(); ++i) { - _sso.current_shuffled_sons.clear(); - _sso.current_shuffled_sons.reserve( gpo.active_sons.at(active_sidechain_type).size() ); + /// High performance random generator + /// http://xorshift.di.unimi.it/ + uint64_t k = now_hi + uint64_t(i) * 2685821657736338717ULL; + k ^= (k >> 12); + k ^= (k << 25); + k ^= (k >> 27); + k *= 2685821657736338717ULL; - for ( const auto &w : gpo.active_sons.at(active_sidechain_type) ) { - _sso.current_shuffled_sons.push_back(w.son_id); - } - - auto now_hi = uint64_t(head_block_time().sec_since_epoch()) << 32; - - for (uint32_t i = 0; i < _sso.current_shuffled_sons.size(); ++i) - { - /// High performance random generator - /// http://xorshift.di.unimi.it/ - uint64_t k = now_hi + uint64_t(i) * 2685821657736338717ULL; - k ^= (k >> 12); - k ^= (k << 25); - k ^= (k >> 27); - k *= 2685821657736338717ULL; - - uint32_t jmax = _sso.current_shuffled_sons.size() - i; - uint32_t j = i + k % jmax; - std::swap(_sso.current_shuffled_sons[i], - _sso.current_shuffled_sons[j]); - } - }); - } + uint32_t jmax = _sso.current_shuffled_sons.size() - i; + uint32_t j = i + k % jmax; + std::swap(_sso.current_shuffled_sons[i], + _sso.current_shuffled_sons[j]); + } + }); } } @@ -321,23 +318,15 @@ void database::update_witness_schedule(const signed_block& next_block) idump( ( double(total_time/1000000.0)/calls) ); } -void database::update_son_schedule(const signed_block& next_block) +void database::update_son_schedule(sidechain_type type, const signed_block& next_block) { auto start = fc::time_point::now(); #ifndef NDEBUG const son_schedule_object& sso = get(son_schedule_id_type()); #endif const global_property_object& gpo = get_global_properties(); - const flat_map schedule_needs_filled = [&gpo]() - { - flat_map schedule_needs_filled; - for(const auto& sidechain_active_sons : gpo.active_sons) - { - schedule_needs_filled[sidechain_active_sons.first] = sidechain_active_sons.second.size(); - } - return schedule_needs_filled; - }(); - uint32_t schedule_slot = get_slot_at_time(next_block.timestamp); + const uint32_t schedule_needs_filled = gpo.active_sons.at(type).size(); + const uint32_t schedule_slot = get_slot_at_time(next_block.timestamp); // We shouldn't be able to generate _pending_block with timestamp // in the past, and incoming blocks from the network with timestamp @@ -351,46 +340,43 @@ void database::update_son_schedule(const signed_block& next_block) assert( dpo.random.data_size() == witness_scheduler_rng::seed_length ); assert( witness_scheduler_rng::seed_length == sso.rng_seed.size() ); - for(const auto& active_sidechain_type : active_sidechain_types(head_block_time())) + const son_schedule_object& sidechain_sso = get(son_schedule_id_type(get_son_schedule_id(type))); + son_id_type first_son; + bool slot_is_near = sidechain_sso.scheduler.get_slot( schedule_slot-1, first_son ); + son_id_type son_id; + + modify(sidechain_sso, [&](son_schedule_object& _sso) { - const son_schedule_object& sidechain_sso = get(son_schedule_id_type(get_son_schedule_id(active_sidechain_type))); - son_id_type first_son; - bool slot_is_near = sidechain_sso.scheduler.get_slot( schedule_slot-1, first_son ); - son_id_type son_id; + _sso.slots_since_genesis += schedule_slot; + witness_scheduler_rng rng(_sso.rng_seed.data, _sso.slots_since_genesis); - modify(sidechain_sso, [&](son_schedule_object& _sso) - { - _sso.slots_since_genesis += schedule_slot; - witness_scheduler_rng rng(_sso.rng_seed.data, _sso.slots_since_genesis); + _sso.scheduler._min_token_count = std::max(int(gpo.active_sons.at(type).size()) / 2, 1); - _sso.scheduler._min_token_count = std::max(int(gpo.active_sons.at(active_sidechain_type).size()) / 2, 1); - - if( slot_is_near ) + if( slot_is_near ) + { + uint32_t drain = schedule_slot; + while( drain > 0 ) { - uint32_t drain = schedule_slot; - while( drain > 0 ) - { - if( _sso.scheduler.size() == 0 ) - break; - _sso.scheduler.consume_schedule(); - --drain; - } + if( _sso.scheduler.size() == 0 ) + break; + _sso.scheduler.consume_schedule(); + --drain; } - else - { - _sso.scheduler.reset_schedule( first_son ); - } - while( !_sso.scheduler.get_slot(schedule_needs_filled.at(active_sidechain_type), son_id) ) - { - if( _sso.scheduler.produce_schedule(rng) & emit_turn ) - memcpy(_sso.rng_seed.begin(), dpo.random.data(), dpo.random.data_size()); - } - _sso.last_scheduling_block = next_block.block_num(); - _sso.recent_slots_filled = ( - (_sso.recent_slots_filled << 1) - + 1) << (schedule_slot - 1); - }); - } + } + else + { + _sso.scheduler.reset_schedule( first_son ); + } + while( !_sso.scheduler.get_slot(schedule_needs_filled, son_id) ) + { + if( _sso.scheduler.produce_schedule(rng) & emit_turn ) + memcpy(_sso.rng_seed.begin(), dpo.random.data(), dpo.random.data_size()); + } + _sso.last_scheduling_block = next_block.block_num(); + _sso.recent_slots_filled = ( + (_sso.recent_slots_filled << 1) + + 1) << (schedule_slot - 1); + }); auto end = fc::time_point::now(); static uint64_t total_time = 0; diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index c8e1f214..48be59f0 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -292,8 +292,8 @@ namespace graphene { namespace chain { vector get_near_witness_schedule()const; void update_witness_schedule(); void update_witness_schedule(const signed_block& next_block); - void update_son_schedule(); - void update_son_schedule(const signed_block& next_block); + void update_son_schedule(sidechain_type type); + void update_son_schedule(sidechain_type type, const signed_block& next_block); void check_lottery_end_by_participants( asset_id_type asset_id ); void check_ending_lotteries(); diff --git a/libraries/chain/son_wallet_evaluator.cpp b/libraries/chain/son_wallet_evaluator.cpp index 138f8e57..03e2edc1 100644 --- a/libraries/chain/son_wallet_evaluator.cpp +++ b/libraries/chain/son_wallet_evaluator.cpp @@ -97,10 +97,18 @@ void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_ope FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK"); FC_ASSERT( op.payer == db().get_global_properties().parameters.son_account(), "SON paying account must be set as payer." ); + const son_wallet_id_type son_wallet_id = [&]{ + if(db().head_block_time() >= HARDFORK_SON_FOR_ETHEREUM_TIME) + { + const auto ast = active_sidechain_types(db().head_block_time()); + const auto id = (op.son_wallet_id.instance.value - std::distance(ast.begin(), ast.find(op.sidechain))) / ast.size(); + return son_wallet_id_type{ id }; + } + + return op.son_wallet_id; + }(); + const auto& idx = db().get_index_type().indices().get(); - const auto ast = active_sidechain_types(db().head_block_time()); - const auto id = (op.son_wallet_id.instance.value - std::distance(ast.begin(), ast.find(op.sidechain))) / ast.size(); - const son_wallet_id_type son_wallet_id{ id }; FC_ASSERT( idx.find(son_wallet_id) != idx.end() ); //auto itr = idx.find(op.son_wallet_id); //FC_ASSERT( itr->addresses.find(op.sidechain) == itr->addresses.end() || @@ -110,10 +118,18 @@ void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_ope object_id_type update_son_wallet_evaluator::do_apply(const son_wallet_update_operation& op) { try { + const son_wallet_id_type son_wallet_id = [&]{ + if(db().head_block_time() >= HARDFORK_SON_FOR_ETHEREUM_TIME) + { + const auto ast = active_sidechain_types(db().head_block_time()); + const auto id = (op.son_wallet_id.instance.value - std::distance(ast.begin(), ast.find(op.sidechain))) / ast.size(); + return son_wallet_id_type{ id }; + } + + return op.son_wallet_id; + }(); + const auto& idx = db().get_index_type().indices().get(); - const auto ast = active_sidechain_types(db().head_block_time()); - const auto id = (op.son_wallet_id.instance.value - std::distance(ast.begin(), ast.find(op.sidechain))) / ast.size(); - const son_wallet_id_type son_wallet_id{ id }; auto itr = idx.find(son_wallet_id); if (itr != idx.end()) {