Added reserve_issue and check_amount_higher_than_fee into withdraw_pbtc_evaluator

This commit is contained in:
Alexander Suslikov 2019-02-06 15:11:54 +03:00 committed by Anzhy Cherrnyavski
parent f6c2e8c92d
commit 7c02821b84
4 changed files with 52 additions and 7 deletions

View file

@ -13,13 +13,13 @@ class withdraw_pbtc_evaluator : public evaluator<withdraw_pbtc_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;
};

View file

@ -1,6 +1,9 @@
#include <graphene/chain/withdraw_pbtc_evaluator.hpp>
#include <graphene/chain/info_for_vout_object.hpp>
#include <graphene/chain/bitcoin_address_object.hpp>
#include <sidechain/sidechain_condensing_tx.hpp>
#include <sidechain/input_withdrawal_info.hpp>
#include <sidechain/bitcoin_address.hpp>
#include <sidechain/utils.hpp>
@ -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;
}

View file

@ -49,7 +49,7 @@ struct by_confirmed_and_not_used;
using btc_tx_confirmations_index = boost::multi_index_container<bitcoin_transaction_confirmations,
indexed_by<
ordered_unique<tag<by_hash>, member<bitcoin_transaction_confirmations, fc::sha256, &bitcoin_transaction_confirmations::transaction_id>>,
ordered_non_unique<tag<by_confirmed_and_not_used>, const_mem_fun< bitcoin_transaction_confirmations, bool, &bitcoin_transaction_confirmations::is_confirmed_and_not_used >>
ordered_non_unique<tag<by_confirmed_and_not_used>, identity< bitcoin_transaction_confirmations >, bitcoin_transaction_confirmations::comparer >
>
>;

View file

@ -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;