From 7c02821b843905df91cbc75653a034c3459cf714 Mon Sep 17 00:00:00 2001 From: Alexander Suslikov Date: Wed, 6 Feb 2019 15:11:54 +0300 Subject: [PATCH] Added reserve_issue and check_amount_higher_than_fee into withdraw_pbtc_evaluator --- .../chain/withdraw_pbtc_evaluator.hpp | 6 +-- libraries/chain/withdraw_pbtc_evaluator.cpp | 49 ++++++++++++++++++- .../bitcoin_transaction_confirmations.hpp | 2 +- .../sidechain/sidechain_condensing_tx.cpp | 2 +- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/libraries/chain/include/graphene/chain/withdraw_pbtc_evaluator.hpp b/libraries/chain/include/graphene/chain/withdraw_pbtc_evaluator.hpp index 3a72a91d..2812d675 100644 --- a/libraries/chain/include/graphene/chain/withdraw_pbtc_evaluator.hpp +++ b/libraries/chain/include/graphene/chain/withdraw_pbtc_evaluator.hpp @@ -13,13 +13,13 @@ class withdraw_pbtc_evaluator : public evaluator public: typedef withdraw_pbtc_operation operation_type; - void_result do_evaluate(const withdraw_pbtc_operation& op); + void_result do_evaluate( const withdraw_pbtc_operation& op ); - void_result do_apply(const withdraw_pbtc_operation& op); + void_result do_apply( const withdraw_pbtc_operation& op ); void reserve_issue( const withdraw_pbtc_operation& op ); - bool check_amount( const withdraw_pbtc_operation& op ); + bool check_amount_higher_than_fee( const withdraw_pbtc_operation& op ); payment_type type; }; diff --git a/libraries/chain/withdraw_pbtc_evaluator.cpp b/libraries/chain/withdraw_pbtc_evaluator.cpp index ef177b16..ac256fb2 100644 --- a/libraries/chain/withdraw_pbtc_evaluator.cpp +++ b/libraries/chain/withdraw_pbtc_evaluator.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include +#include #include #include @@ -14,7 +17,7 @@ void_result withdraw_pbtc_evaluator::do_evaluate(const withdraw_pbtc_operation& FC_ASSERT( op.data.size() > 0 ); type = bitcoin_address( op.data ).get_type(); FC_ASSERT( type != payment_type::NULLDATA , "Invalid address type." ); - FC_ASSERT( check_amount( op ) ); + FC_ASSERT( check_amount_higher_than_fee( op ) ); asset acc_balance = db().get_balance( op.payer, d.get_sidechain_asset_id() ); FC_ASSERT( acc_balance.amount.value >= op.amount ); @@ -30,10 +33,52 @@ void_result withdraw_pbtc_evaluator::do_apply(const withdraw_pbtc_operation& op) void withdraw_pbtc_evaluator::reserve_issue( const withdraw_pbtc_operation& op ) { + database& d = db(); + bool skip_fee_old = trx_state->skip_fee; + bool skip_fee_schedule_check_old = trx_state->skip_fee_schedule_check; + trx_state->skip_fee = true; + trx_state->skip_fee_schedule_check = true; + + asset_reserve_operation reserve_op; + reserve_op.amount_to_reserve = asset( op.amount, d.get_sidechain_asset_id() ); + reserve_op.payer = op.payer; + + d.apply_operation( *trx_state, reserve_op ); + + trx_state->skip_fee = skip_fee_old; + trx_state->skip_fee_schedule_check = skip_fee_schedule_check_old; } -bool withdraw_pbtc_evaluator::check_amount( const withdraw_pbtc_operation& op ) { +bool withdraw_pbtc_evaluator::check_amount_higher_than_fee( const withdraw_pbtc_operation& op ) { + database& d = db(); + + info_for_vout_object obj; + obj.payer = op.payer; + obj.address = op.data; + obj.amount = op.amount; + obj.used = false; + + const auto& pw_address = d.get_latest_PW().address; + + sidechain::info_for_vin pw_vin; + pw_vin.identifier = fc::sha256( std::string(64, '1') ); + pw_vin.out.hash_tx = std::string(64, '1'); + pw_vin.out.n_vout = 0; + pw_vin.out.amount = 2*op.amount; + pw_vin.address = pw_address.get_address(); + pw_vin.script = pw_address.get_witness_script(); + + const auto& mock_trx = d.create_btc_transaction( {}, { obj }, pw_vin ); + + if( op.amount < mock_trx.second ) + return false; + + auto fee_for_witnesses = ( op.amount - mock_trx.second ) * SIDECHAIN_DEFAULT_PERCENTAGE_PAYMENT_TO_WIT; + + if( op.amount < mock_trx.second + fee_for_witnesses ) + return false; + return true; } diff --git a/libraries/sidechain/include/sidechain/bitcoin_transaction_confirmations.hpp b/libraries/sidechain/include/sidechain/bitcoin_transaction_confirmations.hpp index 42ae8edc..860f9c44 100644 --- a/libraries/sidechain/include/sidechain/bitcoin_transaction_confirmations.hpp +++ b/libraries/sidechain/include/sidechain/bitcoin_transaction_confirmations.hpp @@ -49,7 +49,7 @@ struct by_confirmed_and_not_used; using btc_tx_confirmations_index = boost::multi_index_container, member>, - ordered_non_unique, const_mem_fun< bitcoin_transaction_confirmations, bool, &bitcoin_transaction_confirmations::is_confirmed_and_not_used >> + ordered_non_unique, identity< bitcoin_transaction_confirmations >, bitcoin_transaction_confirmations::comparer > > >; diff --git a/libraries/sidechain/sidechain_condensing_tx.cpp b/libraries/sidechain/sidechain_condensing_tx.cpp index 879df790..54665e61 100644 --- a/libraries/sidechain/sidechain_condensing_tx.cpp +++ b/libraries/sidechain/sidechain_condensing_tx.cpp @@ -79,7 +79,7 @@ void sidechain_condensing_tx::subtract_fee( const uint64_t& fee, const double& w bool is_pw_vin = tx.vout.size() > ( count_witness_vout + count_transfer_vout ); if( is_pw_vin ) { - tx.vout[0].value = tx.vout[0].value - fee_size; + tx.vout[0].value = tx.vout[0].value - fee_size * count_transfer_vin; } uint64_t fee_witnesses = 0;