From d4e9b35ca848cec4928f7e53d8cd901e761cd163 Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Thu, 5 Feb 2015 20:03:01 -0500 Subject: [PATCH] Track delegate ids and other ids separately in slate_records; #1358 --- libraries/blockchain/account_operations.cpp | 2 +- libraries/blockchain/chain_database.cpp | 2 +- .../include/bts/blockchain/config.hpp | 2 +- .../include/bts/blockchain/slate_record.hpp | 7 +++--- .../transaction_evaluation_state.hpp | 23 ++++++++++--------- libraries/blockchain/slate_operations.cpp | 5 +++- libraries/blockchain/slate_record.cpp | 7 +++--- .../transaction_evaluation_state.cpp | 16 ++++--------- libraries/client/blockchain_api.cpp | 5 +++- libraries/wallet/wallet.cpp | 12 +++++----- 10 files changed, 41 insertions(+), 40 deletions(-) diff --git a/libraries/blockchain/account_operations.cpp b/libraries/blockchain/account_operations.cpp index adedb6152..cf5bf79c7 100644 --- a/libraries/blockchain/account_operations.cpp +++ b/libraries/blockchain/account_operations.cpp @@ -155,7 +155,7 @@ namespace bts { namespace blockchain { FC_CAPTURE_AND_THROW( missing_signature, (*this) ); account->delegate_info->pay_balance -= this->amount; - eval_state.delta_votes[ account_id ] -= this->amount; + eval_state.delegate_vote_deltas[ account_id ] -= this->amount; eval_state.add_balance( asset( this->amount, 0 ) ); eval_state._current_state->store_account_record( *account ); diff --git a/libraries/blockchain/chain_database.cpp b/libraries/blockchain/chain_database.cpp index ef133dbe4..2be851e56 100644 --- a/libraries/blockchain/chain_database.cpp +++ b/libraries/blockchain/chain_database.cpp @@ -2409,7 +2409,7 @@ namespace bts { namespace blockchain { oslate_record slate_record = get_slate_record( balance.slate_id() ); FC_ASSERT(slate_record.valid(), "Unknown slate ID found in balance."); - for (account_id_type delegate : slate_record->slate) + for (account_id_type delegate : slate_record->delegate_slate) calculated_balances[delegate] += balance.balance; } } diff --git a/libraries/blockchain/include/bts/blockchain/config.hpp b/libraries/blockchain/include/bts/blockchain/config.hpp index 03f55a7f0..f7238cfc1 100644 --- a/libraries/blockchain/include/bts/blockchain/config.hpp +++ b/libraries/blockchain/include/bts/blockchain/config.hpp @@ -7,7 +7,7 @@ #define BTS_TEST_NETWORK_VERSION 82 // autogenerated -#define BTS_BLOCKCHAIN_DATABASE_VERSION 191 +#define BTS_BLOCKCHAIN_DATABASE_VERSION 192 #define BTS_ADDRESS_PREFIX "XTS" #define BTS_BLOCKCHAIN_SYMBOL "XTS" diff --git a/libraries/blockchain/include/bts/blockchain/slate_record.hpp b/libraries/blockchain/include/bts/blockchain/slate_record.hpp index 68b40ccb8..d4e0a918e 100644 --- a/libraries/blockchain/include/bts/blockchain/slate_record.hpp +++ b/libraries/blockchain/include/bts/blockchain/slate_record.hpp @@ -8,7 +8,8 @@ class chain_interface; struct slate_db_interface; struct slate_record { - set slate; + set delegate_slate; + set other_slate; slate_id_type id()const; @@ -16,7 +17,7 @@ struct slate_record void sanity_check( const chain_interface& )const; }; typedef optional oslate_record; - + struct slate_db_interface { std::function lookup_by_id; @@ -30,4 +31,4 @@ struct slate_db_interface } } // bts::blockchain -FC_REFLECT( bts::blockchain::slate_record, (slate) ) +FC_REFLECT( bts::blockchain::slate_record, (delegate_slate)(other_slate) ) diff --git a/libraries/blockchain/include/bts/blockchain/transaction_evaluation_state.hpp b/libraries/blockchain/include/bts/blockchain/transaction_evaluation_state.hpp index 2522944e1..79922b15a 100644 --- a/libraries/blockchain/include/bts/blockchain/transaction_evaluation_state.hpp +++ b/libraries/blockchain/include/bts/blockchain/transaction_evaluation_state.hpp @@ -25,28 +25,27 @@ namespace bts { namespace blockchain { public: transaction_evaluation_state( pending_chain_state* current_state = nullptr ); - virtual ~transaction_evaluation_state(); - virtual share_type get_fees( asset_id_type id = 0)const; + ~transaction_evaluation_state(); + share_type get_fees( asset_id_type id = 0 )const; - virtual void evaluate( const signed_transaction& trx ); - virtual void evaluate_operation( const operation& op ); - virtual bool verify_authority( const multisig_meta_info& siginfo ); + void evaluate( const signed_transaction& trx ); + void evaluate_operation( const operation& op ); + bool verify_authority( const multisig_meta_info& siginfo ); /** perform any final operations based upon the current state of * the operation such as updating fees paid etc. */ - virtual void post_evaluate(); + void post_evaluate(); /** can be specalized to handle many different forms of * fee payment. */ - virtual void validate_required_fee(); + void validate_required_fee(); /** * apply collected vote changes */ - virtual void update_delegate_votes(); - virtual void verify_delegate_id( const account_id_type id )const; + void update_delegate_votes(); bool check_signature( const address& a )const; bool check_multisig( const multisig_condition& a )const; @@ -101,7 +100,7 @@ namespace bts { namespace blockchain { */ map balance; - unordered_map delta_votes; + unordered_map delegate_vote_deltas; // Not serialized chain_interface* _current_state = nullptr; @@ -110,7 +109,9 @@ namespace bts { namespace blockchain { bool _enforce_canonical_signatures = false; bool _skip_vote_adjustment = false; + // For pay_fee op unordered_map _max_fee; + uint32_t current_op_index = 0; }; typedef shared_ptr transaction_evaluation_state_ptr; @@ -128,5 +129,5 @@ FC_REFLECT( bts::blockchain::transaction_evaluation_state, (required_fees) (alt_fees_paid) (balance) - (delta_votes) + (delegate_vote_deltas) ) diff --git a/libraries/blockchain/slate_operations.cpp b/libraries/blockchain/slate_operations.cpp index e6a0db80a..ac64776ea 100644 --- a/libraries/blockchain/slate_operations.cpp +++ b/libraries/blockchain/slate_operations.cpp @@ -6,6 +6,7 @@ namespace bts { namespace blockchain { void define_slate_operation::evaluate( transaction_evaluation_state& eval_state )const { try { + FC_ASSERT( !slate.empty() ); if( this->slate.size() > BTS_BLOCKCHAIN_MAX_SLATE_SIZE ) FC_CAPTURE_AND_THROW( too_may_delegates_in_slate, (slate.size()) ); @@ -13,7 +14,9 @@ void define_slate_operation::evaluate( transaction_evaluation_state& eval_state for( const signed_int id : this->slate ) { FC_ASSERT( id >= 0 ); - record.slate.insert( id ); + const oaccount_record delegate_record = eval_state._current_state->get_account_record( id ); + if( delegate_record.valid() && delegate_record->is_delegate() ) record.delegate_slate.insert( id ); + else record.other_slate.insert( id ); } const slate_id_type slate_id = record.id(); diff --git a/libraries/blockchain/slate_record.cpp b/libraries/blockchain/slate_record.cpp index a40f48285..53ed2333f 100644 --- a/libraries/blockchain/slate_record.cpp +++ b/libraries/blockchain/slate_record.cpp @@ -8,9 +8,10 @@ namespace bts { namespace blockchain { slate_id_type slate_record::id()const { - if( slate.empty() ) return 0; + if( delegate_slate.empty() && other_slate.empty() ) return 0; fc::sha256::encoder enc; - fc::raw::pack( enc, this->slate ); + if( !delegate_slate.empty() ) fc::raw::pack( enc, delegate_slate ); + if( !other_slate.empty() ) fc::raw::pack( enc, other_slate ); return enc.result()._hash[ 0 ]; } @@ -21,7 +22,7 @@ const slate_db_interface& slate_record::db_interface( const chain_interface& db void slate_record::sanity_check( const chain_interface& db )const { try { - FC_ASSERT( !slate.empty() ); + FC_ASSERT( !delegate_slate.empty() || !other_slate.empty() ); } FC_CAPTURE_AND_RETHROW( (*this) ) } oslate_record slate_db_interface::lookup( const slate_id_type id )const diff --git a/libraries/blockchain/transaction_evaluation_state.cpp b/libraries/blockchain/transaction_evaluation_state.cpp index 28da203b6..07a9f746e 100644 --- a/libraries/blockchain/transaction_evaluation_state.cpp +++ b/libraries/blockchain/transaction_evaluation_state.cpp @@ -95,21 +95,13 @@ namespace bts { namespace blockchain { return any_parent_has_signed( record.name ); } FC_CAPTURE_AND_RETHROW( (record) ) } - void transaction_evaluation_state::verify_delegate_id( const account_id_type id )const - { try { - auto current_account = _current_state->get_account_record( id ); - if( !current_account ) FC_CAPTURE_AND_THROW( unknown_account_id, (id) ); - if( !current_account->is_delegate() ) FC_CAPTURE_AND_THROW( not_a_delegate, (id) ); - } FC_CAPTURE_AND_RETHROW( (id) ) } - void transaction_evaluation_state::update_delegate_votes() { try { - for( const auto& item : delta_votes ) + for( const auto& item : delegate_vote_deltas ) { const account_id_type id = item.first; oaccount_record delegate_record = _current_state->get_account_record( id ); - if( !delegate_record.valid() || !delegate_record->is_delegate() ) - continue; + FC_ASSERT( delegate_record.valid() && delegate_record->is_delegate() ); const share_type amount = item.second; delegate_record->adjust_votes_for( amount ); @@ -259,8 +251,8 @@ namespace bts { namespace blockchain { if( !slate_record.valid() ) FC_CAPTURE_AND_THROW( unknown_delegate_slate, (slate_id) ); - for( const account_id_type id : slate_record->slate ) - delta_votes[ id ] += amount; + for( const account_id_type id : slate_record->delegate_slate ) + delegate_vote_deltas[ id ] += amount; } FC_CAPTURE_AND_RETHROW( (slate_id)(amount) ) } share_type transaction_evaluation_state::get_fees( asset_id_type id )const diff --git a/libraries/client/blockchain_api.cpp b/libraries/client/blockchain_api.cpp index f83d7aba9..262d4827c 100644 --- a/libraries/client/blockchain_api.cpp +++ b/libraries/client/blockchain_api.cpp @@ -183,13 +183,16 @@ map detail::client_impl::blockchain_get_slate( const st const oslate_record slate_record = _chain_db->get_slate_record( slate_id ); FC_ASSERT( slate_record.valid() ); - for( const account_id_type id : slate_record->slate ) + for( const account_id_type id : slate_record->delegate_slate ) { const oaccount_record delegate_record = _chain_db->get_account_record( id ); if( delegate_record.valid() ) delegates[ id ] = delegate_record->name; else delegates[ id ] = std::to_string( id ); } + for( const account_id_type id : slate_record->other_slate ) + delegates[ id ] = std::to_string( id ); + return delegates; } diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 420f69592..dca0384f5 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -706,7 +706,7 @@ namespace detail { slate_id = record.id(); if( slate_id == 0 ) return slate_id; - if( !_blockchain->get_slate_record( slate_id ).valid() ) transaction.define_slate( record.slate ); + if( !_blockchain->get_slate_record( slate_id ).valid() ) transaction.define_slate( record.delegate_slate ); transaction.set_slates( slate_id ); return slate_id; } FC_CAPTURE_AND_RETHROW( (transaction)(strategy) ) } @@ -816,7 +816,7 @@ namespace detail { continue; } - for( const auto recommended_candidate : recommendations->slate ) + for( const auto recommended_candidate : recommendations->delegate_slate ) ++recommended_candidate_ranks[recommended_candidate]; } @@ -864,8 +864,8 @@ namespace detail { } slate_record record; - for( const account_id_type id : for_candidates ) record.slate.insert( id ); - FC_ASSERT( record.slate.size() <= BTS_BLOCKCHAIN_MAX_SLATE_SIZE ); + for( const account_id_type id : for_candidates ) record.delegate_slate.insert( id ); + FC_ASSERT( record.delegate_slate.size() <= BTS_BLOCKCHAIN_MAX_SLATE_SIZE ); return record; } @@ -1655,7 +1655,7 @@ namespace detail { summary.up_to_date_with_recommendation = false; auto oslate = my->_blockchain->get_slate_record( record.slate_id() ); if( oslate.valid() ) - total += record.get_spendable_balance( my->_blockchain->now() ).amount * oslate->slate.size(); + total += record.get_spendable_balance( my->_blockchain->now() ).amount * oslate->delegate_slate.size(); total_possible += record.get_spendable_balance( my->_blockchain->now() ).amount * BTS_BLOCKCHAIN_MAX_SLATE_SIZE; } } @@ -4502,7 +4502,7 @@ namespace detail { const auto slate_record = pending_state->get_slate_record( slate_id ); if( !slate_record.valid() ) FC_THROW_EXCEPTION( unknown_slate, "Unknown slate!" ); - for( const account_id_type delegate_id : slate_record->slate ) + for( const account_id_type delegate_id : slate_record->delegate_slate ) { if( raw_votes.count( delegate_id ) <= 0 ) raw_votes[ delegate_id ] = balance.amount; else raw_votes[ delegate_id ] += balance.amount;