change proposal to transaction object in bitcoin_transaction_sign_operation

This commit is contained in:
gladcow 2020-03-03 15:24:56 +03:00
parent 610426836b
commit a06af5bf17
7 changed files with 38 additions and 45 deletions

View file

@ -13,6 +13,8 @@ namespace graphene { namespace chain {
account_id_type payer;
peerplays_sidechain::bytes unsigned_tx;
peerplays_sidechain::bytes redeem_script;
std::vector<uint64_t> in_amounts;
fc::flat_map<son_id_type, std::vector<peerplays_sidechain::bytes>> signatures;
account_id_type fee_payer()const { return payer; }
@ -26,7 +28,7 @@ namespace graphene { namespace chain {
asset fee;
account_id_type payer;
proposal_id_type proposal_id;
bitcoin_transaction_id_type tx_id;
std::vector<peerplays_sidechain::bytes> signatures;
account_id_type fee_payer()const { return payer; }
@ -51,10 +53,10 @@ namespace graphene { namespace chain {
} } // graphene::chain
FC_REFLECT( graphene::chain::bitcoin_transaction_send_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::bitcoin_transaction_send_operation, (fee)(payer)(unsigned_tx)(signatures) )
FC_REFLECT( graphene::chain::bitcoin_transaction_send_operation, (fee)(payer)(unsigned_tx)(redeem_script)(in_amounts)(signatures) )
FC_REFLECT( graphene::chain::bitcoin_transaction_sign_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::bitcoin_transaction_sign_operation, (fee)(payer)(proposal_id)(signatures) )
FC_REFLECT( graphene::chain::bitcoin_transaction_sign_operation, (fee)(payer)(tx_id)(signatures) )
FC_REFLECT( graphene::chain::bitcoin_send_transaction_process_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::bitcoin_send_transaction_process_operation, (fee)(payer)(bitcoin_transaction_id) )

View file

@ -20,7 +20,6 @@ public:
void_result do_evaluate(const bitcoin_transaction_sign_operation& o);
object_id_type do_apply(const bitcoin_transaction_sign_operation& o);
void update_proposal( const bitcoin_transaction_sign_operation& o );
};
class bitcoin_send_transaction_process_evaluator : public evaluator<bitcoin_send_transaction_process_evaluator>
@ -32,4 +31,4 @@ public:
object_id_type do_apply(const bitcoin_send_transaction_process_operation& o);
};
} } // namespace graphene::chain
} } // namespace graphene::chain

View file

@ -17,6 +17,9 @@ namespace graphene { namespace chain {
static const uint8_t type_id = bitcoin_transaction_object_type;
// Bitcoin structs go here.
bool processed = false;
peerplays_sidechain::bytes unsigned_tx;
peerplays_sidechain::bytes redeem_script;
std::vector<uint64_t> in_amounts;
fc::flat_map<son_id_type, std::vector<peerplays_sidechain::bytes>> signatures;
};
@ -36,4 +39,4 @@ namespace graphene { namespace chain {
} } // graphene::chain
FC_REFLECT_DERIVED( graphene::chain::bitcoin_transaction_object, (graphene::db::object),
(processed)(signatures) )
(processed)(unsigned_tx)(redeem_script)(in_amounts)(signatures) )

View file

@ -28,6 +28,9 @@ object_id_type bitcoin_transaction_send_evaluator::do_apply(const bitcoin_transa
{
const auto &new_bitcoin_transaction_object = db().create<bitcoin_transaction_object>([&](bitcoin_transaction_object &obj) {
obj.processed = false;
obj.unsigned_tx = op.unsigned_tx;
obj.redeem_script = op.redeem_script;
obj.in_amounts = op.in_amounts;
obj.signatures = op.signatures;
});
return new_bitcoin_transaction_object.id;
@ -40,22 +43,26 @@ void_result bitcoin_transaction_sign_evaluator::do_evaluate(const bitcoin_transa
try
{
FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK"); // can be removed after HF date pass
const auto &proposal_idx = db().get_index_type<proposal_index>().indices().get<by_id>();
const auto &proposal_itr = proposal_idx.find(op.proposal_id);
FC_ASSERT(proposal_idx.end() != proposal_itr, "proposal not found");
// Checks can this SON approve this proposal
auto can_this_son_approve_this_proposal = [&]() {
const auto &tx_idx = db().get_index_type<bitcoin_transaction_index>().indices().get<by_id>();
const auto &tx_itr = tx_idx.find(op.tx_id);
FC_ASSERT(tx_idx.end() != tx_itr, "bitcoin transaction not found");
// Checks can this SON sign this tx
auto can_this_son_sign_this_tx = [&]() {
const auto &sidx = db().get_index_type<son_index>().indices().get<graphene::chain::by_account>();
auto son_obj = sidx.find(op.payer);
if (son_obj == sidx.end())
{
return false;
}
// TODO: Check if the SON is included in the PW script.
return true;
auto it = tx_itr->signatures.find(son_obj->id);
if (it == tx_itr->signatures.end())
return false;
// tx is not signed with this son already
return it->second.empty();
};
FC_ASSERT(can_this_son_approve_this_proposal(), "Invalid approval received");
FC_ASSERT(can_this_son_sign_this_tx(), "Invalid approval received");
return void_result();
}
FC_CAPTURE_AND_RETHROW((op))
@ -65,44 +72,23 @@ object_id_type bitcoin_transaction_sign_evaluator::do_apply(const bitcoin_transa
{
try
{
const auto &proposal = op.proposal_id(db());
const auto &bitcoin_tx = op.tx_id(db());
const auto &sidx = db().get_index_type<son_index>().indices().get<graphene::chain::by_account>();
auto son_obj = sidx.find(op.payer);
db().modify(proposal, [&](proposal_object &po) {
auto bitcoin_transaction_send_op = po.proposed_transaction.operations[0].get<bitcoin_transaction_send_operation>();
bitcoin_transaction_send_op.signatures[son_obj->id] = op.signatures;
po.proposed_transaction.operations[0] = bitcoin_transaction_send_op;
db().modify(bitcoin_tx, [&](bitcoin_transaction_object &btx) {
btx.signatures[son_obj->id] = op.signatures;
});
db().modify( son_obj->statistics( db() ), [&]( son_statistics_object& sso ) {
sso.txs_signed += 1;
} );
update_proposal(op);
return bitcoin_tx.id;
}
FC_CAPTURE_AND_RETHROW((op))
}
void bitcoin_transaction_sign_evaluator::update_proposal(const bitcoin_transaction_sign_operation &op)
{
database &d = db();
proposal_update_operation update_op;
update_op.fee_paying_account = op.payer;
update_op.proposal = op.proposal_id;
update_op.active_approvals_to_add = {op.payer};
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;
d.apply_operation(*trx_state, update_op);
trx_state->skip_fee = skip_fee_old;
trx_state->skip_fee_schedule_check = skip_fee_schedule_check_old;
}
void_result bitcoin_send_transaction_process_evaluator::do_evaluate(const bitcoin_send_transaction_process_operation &op)
{

View file

@ -14,6 +14,7 @@
#include <graphene/chain/son_wallet_object.hpp>
#include <graphene/chain/son_wallet_withdraw_object.hpp>
#include <graphene/peerplays_sidechain/sidechain_net_manager.hpp>
#include <graphene/peerplays_sidechain/bitcoin_utils.hpp>
#include <graphene/utilities/key_conversion.hpp>
namespace bpo = boost::program_options;
@ -540,13 +541,13 @@ void peerplays_sidechain_plugin_impl::create_son_deregister_proposals() {
bitcoin_transaction_sign_operation op;
son_object s_obj= get_son_object(son_id);
op.payer = s_obj.son_account;
// op.tx_id = object_id;
op.tx_id = tx_object->id;
fc::ecc::private_key k = get_private_key(son_id);
//op.signatures = signatures_for_raw_transaction(unsigned_tx, amounts, redeem_script, k);
op.signatures = signatures_for_raw_transaction(tx_object->unsigned_tx, tx_object->in_amounts, tx_object->redeem_script, k);
signed_transaction trx = database().create_signed_transaction(get_private_key(plugin.get_current_son_id()), op);
signed_transaction trx = plugin.database().create_signed_transaction(k, op);
try {
database().push_transaction(trx, database::validation_steps::skip_block_size_check);
plugin.database().push_transaction(trx, database::validation_steps::skip_block_size_check);
if(plugin.app().p2p_node())
plugin.app().p2p_node()->broadcast(net::trx_message(trx));
} catch(fc::exception e){

View file

@ -764,6 +764,8 @@ void sidechain_net_handler_bitcoin::transfer_all_btc(const std::string& from_add
bitcoin_transaction_send_operation op;
op.payer = GRAPHENE_SON_ACCOUNT;
op.redeem_script = from_redeem_script;
op.in_amounts = amounts;
tx.to_bytes(op.unsigned_tx);
// add signatures
std::set<son_id_type> plugin_sons = plugin.get_sons();

View file

@ -231,7 +231,7 @@ BOOST_AUTO_TEST_CASE(bitcoin_transaction_send_test)
bitcoin_transaction_sign_operation sign_op;
sign_op.payer = alice_id;
sign_op.proposal_id = proposal_id_type(0);
//sign_op.proposal_id = proposal_id_type(0);
sign_op.signatures.push_back(a1);
sign_op.signatures.push_back(a2);
sign_op.signatures.push_back(a3);
@ -270,7 +270,7 @@ BOOST_AUTO_TEST_CASE(bitcoin_transaction_send_test)
bitcoin_transaction_sign_operation sign_op;
sign_op.payer = bob_id;
sign_op.proposal_id = proposal_id_type(0);
//sign_op.proposal_id = proposal_id_type(0);
sign_op.signatures.push_back(b1);
sign_op.signatures.push_back(b2);
sign_op.signatures.push_back(b3);