From 2739b56b94ba42a4960a02523e96e590f3960082 Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Fri, 17 Apr 2020 02:47:23 +0200 Subject: [PATCH] Refactor sidechain address mapping --- .../chain/protocol/sidechain_address.hpp | 8 ++++++-- .../graphene/chain/sidechain_address_object.hpp | 14 +++++++++++++- libraries/chain/sidechain_address_evaluator.cpp | 4 ++++ .../sidechain_net_handler_bitcoin.cpp | 1 + .../wallet/include/graphene/wallet/wallet.hpp | 8 ++++++++ libraries/wallet/wallet.cpp | 16 ++++++++++++++-- tests/tests/sidechain_addresses_test.cpp | 10 ++++++++++ 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/libraries/chain/include/graphene/chain/protocol/sidechain_address.hpp b/libraries/chain/include/graphene/chain/protocol/sidechain_address.hpp index 2702ce49..a9cbcc73 100644 --- a/libraries/chain/include/graphene/chain/protocol/sidechain_address.hpp +++ b/libraries/chain/include/graphene/chain/protocol/sidechain_address.hpp @@ -12,7 +12,9 @@ namespace graphene { namespace chain { asset fee; account_id_type sidechain_address_account; sidechain_type sidechain; + string deposit_public_key; string deposit_address; + string withdraw_public_key; string withdraw_address; account_id_type fee_payer()const { return sidechain_address_account; } @@ -27,7 +29,9 @@ namespace graphene { namespace chain { sidechain_address_id_type sidechain_address_id; account_id_type sidechain_address_account; sidechain_type sidechain; + optional deposit_public_key; optional deposit_address; + optional withdraw_public_key; optional withdraw_address; account_id_type fee_payer()const { return sidechain_address_account; } @@ -51,12 +55,12 @@ namespace graphene { namespace chain { FC_REFLECT(graphene::chain::sidechain_address_add_operation::fee_parameters_type, (fee) ) FC_REFLECT(graphene::chain::sidechain_address_add_operation, (fee) - (sidechain_address_account)(sidechain)(deposit_address)(withdraw_address) ) + (sidechain_address_account)(sidechain)(deposit_public_key)(deposit_address)(withdraw_public_key)(withdraw_address) ) FC_REFLECT(graphene::chain::sidechain_address_update_operation::fee_parameters_type, (fee) ) FC_REFLECT(graphene::chain::sidechain_address_update_operation, (fee) (sidechain_address_id) - (sidechain_address_account)(sidechain)(deposit_address)(withdraw_address) ) + (sidechain_address_account)(sidechain)(deposit_public_key)(deposit_address)(withdraw_public_key)(withdraw_address) ) FC_REFLECT(graphene::chain::sidechain_address_delete_operation::fee_parameters_type, (fee) ) FC_REFLECT(graphene::chain::sidechain_address_delete_operation, (fee) diff --git a/libraries/chain/include/graphene/chain/sidechain_address_object.hpp b/libraries/chain/include/graphene/chain/sidechain_address_object.hpp index ca28231b..b9bcda5a 100644 --- a/libraries/chain/include/graphene/chain/sidechain_address_object.hpp +++ b/libraries/chain/include/graphene/chain/sidechain_address_object.hpp @@ -22,17 +22,23 @@ namespace graphene { namespace chain { account_id_type sidechain_address_account; sidechain_type sidechain; + string deposit_public_key; string deposit_address; + string withdraw_public_key; string withdraw_address; sidechain_address_object() : sidechain(sidechain_type::bitcoin), + deposit_public_key(""), deposit_address(""), + withdraw_public_key(""), withdraw_address("") {} }; struct by_account; struct by_sidechain; + struct by_deposit_public_key; + struct by_withdraw_public_key; struct by_account_and_sidechain; struct by_sidechain_and_deposit_address; using sidechain_address_multi_index_type = multi_index_container< @@ -47,6 +53,12 @@ namespace graphene { namespace chain { ordered_non_unique< tag, member >, + ordered_non_unique< tag, + member + >, + ordered_non_unique< tag, + member + >, ordered_unique< tag, composite_key, @@ -66,4 +78,4 @@ namespace graphene { namespace chain { } } // graphene::chain FC_REFLECT_DERIVED( graphene::chain::sidechain_address_object, (graphene::db::object), - (sidechain_address_account) (sidechain) (deposit_address) (withdraw_address) ) + (sidechain_address_account) (sidechain) (deposit_public_key) (deposit_address) (withdraw_public_key) (withdraw_address) ) diff --git a/libraries/chain/sidechain_address_evaluator.cpp b/libraries/chain/sidechain_address_evaluator.cpp index 5382195d..45d1a32a 100644 --- a/libraries/chain/sidechain_address_evaluator.cpp +++ b/libraries/chain/sidechain_address_evaluator.cpp @@ -19,7 +19,9 @@ object_id_type add_sidechain_address_evaluator::do_apply(const sidechain_address const auto& new_sidechain_address_object = db().create( [&]( sidechain_address_object& obj ){ obj.sidechain_address_account = op.sidechain_address_account; obj.sidechain = op.sidechain; + obj.deposit_public_key = op.deposit_public_key; obj.deposit_address = op.deposit_address; + obj.withdraw_public_key = op.withdraw_public_key; obj.withdraw_address = op.withdraw_address; }); return new_sidechain_address_object.id; @@ -40,7 +42,9 @@ object_id_type update_sidechain_address_evaluator::do_apply(const sidechain_addr if(itr != idx.end()) { db().modify(*itr, [&op](sidechain_address_object &sao) { + if(op.deposit_public_key.valid()) sao.deposit_public_key = *op.deposit_public_key; if(op.deposit_address.valid()) sao.deposit_address = *op.deposit_address; + if(op.withdraw_public_key.valid()) sao.withdraw_public_key = *op.withdraw_public_key; if(op.withdraw_address.valid()) sao.withdraw_address = *op.withdraw_address; }); } diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index 37aaffa3..00aae0af 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -1997,6 +1997,7 @@ void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data) const auto &sidechain_addresses_idx = database.get_index_type().indices().get(); for (const auto &v : vins) { + // !!! EXTRACT DEPOSIT ADDRESS FROM SIDECHAIN ADDRESS OBJECT const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain, v.address)); if (addr_itr == sidechain_addresses_idx.end()) continue; diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index df4bfc3c..7d1a5e45 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -1451,14 +1451,18 @@ class wallet_api * * @param account the name or id of the account who owns the address * @param sidechain a sidechain to whom address belongs + * @param deposit_public_key sidechain public key used for deposit address * @param deposit_address sidechain address for deposits + * @param withdraw_public_key sidechain public key used for withdraw address * @param withdraw_address sidechain address for withdrawals * @param broadcast true to broadcast the transaction on the network * @returns the signed transaction adding sidechain address */ signed_transaction add_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast = false); @@ -1468,14 +1472,18 @@ class wallet_api * * @param account the name or id of the account who owns the address * @param sidechain a sidechain to whom address belongs + * @param deposit_public_key sidechain public key used for deposit address * @param deposit_address sidechain address for deposits + * @param withdraw_public_key sidechain public key used for withdraw address * @param withdraw_address sidechain address for withdrawals * @param broadcast true to broadcast the transaction on the network * @returns the signed transaction updating sidechain address */ signed_transaction update_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast = false); diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index d4146c92..cfbb2223 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -2097,7 +2097,9 @@ public: signed_transaction add_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast /* = false */) { try { @@ -2106,7 +2108,9 @@ public: sidechain_address_add_operation op; op.sidechain_address_account = sidechain_address_account_id; op.sidechain = sidechain; + op.deposit_public_key = deposit_public_key; op.deposit_address = deposit_address; + op.withdraw_public_key = withdraw_public_key; op.withdraw_address = withdraw_address; signed_transaction tx; @@ -2119,7 +2123,9 @@ public: signed_transaction update_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast /* = false */) { try { @@ -2132,7 +2138,9 @@ public: op.sidechain_address_id = sao->id; op.sidechain_address_account = sidechain_address_account_id; op.sidechain = sidechain; + op.deposit_public_key = deposit_public_key; op.deposit_address = deposit_address; + op.withdraw_public_key = withdraw_public_key; op.withdraw_address = withdraw_address; signed_transaction tx; @@ -4816,20 +4824,24 @@ vector> wallet_api::get_son_wallets(uint32_t limit) signed_transaction wallet_api::add_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast /* = false */) { - return my->add_sidechain_address(account, sidechain, deposit_address, withdraw_address, broadcast); + return my->add_sidechain_address(account, sidechain, deposit_public_key, deposit_address, withdraw_public_key, withdraw_address, broadcast); } signed_transaction wallet_api::update_sidechain_address(string account, sidechain_type sidechain, + string deposit_public_key, string deposit_address, + string withdraw_public_key, string withdraw_address, bool broadcast /* = false */) { - return my->update_sidechain_address(account, sidechain, deposit_address, withdraw_address, broadcast); + return my->update_sidechain_address(account, sidechain, deposit_public_key, deposit_address, withdraw_public_key, withdraw_address, broadcast); } signed_transaction wallet_api::delete_sidechain_address(string account, diff --git a/tests/tests/sidechain_addresses_test.cpp b/tests/tests/sidechain_addresses_test.cpp index e3fb115f..b06e3b74 100644 --- a/tests/tests/sidechain_addresses_test.cpp +++ b/tests/tests/sidechain_addresses_test.cpp @@ -30,7 +30,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_add_test ) { op.sidechain_address_account = alice_id; op.sidechain = sidechain_type::bitcoin; + op.deposit_public_key = "deposit_public_key"; op.deposit_address = "deposit_address"; + op.withdraw_public_key = "withdraw_public_key"; op.withdraw_address = "withdraw_address"; trx.operations.push_back(op); @@ -47,7 +49,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_add_test ) { BOOST_REQUIRE( obj != idx.end() ); BOOST_CHECK( obj->sidechain_address_account == alice_id ); BOOST_CHECK( obj->sidechain == sidechain_type::bitcoin ); + BOOST_CHECK( obj->deposit_public_key == "deposit_public_key" ); BOOST_CHECK( obj->deposit_address == "deposit_address" ); + BOOST_CHECK( obj->withdraw_public_key == "withdraw_public_key" ); BOOST_CHECK( obj->withdraw_address == "withdraw_address" ); } @@ -64,7 +68,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_update_test ) { auto obj = idx.find( boost::make_tuple( alice_id, sidechain_type::bitcoin ) ); BOOST_REQUIRE( obj != idx.end() ); + std::string new_deposit_public_key = "new_deposit_public_key"; std::string new_deposit_address = "new_deposit_address"; + std::string new_withdraw_public_key = "new_withdraw_public_key"; std::string new_withdraw_address = "new_withdraw_address"; { @@ -74,7 +80,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_update_test ) { op.sidechain_address_id = sidechain_address_id_type(0); op.sidechain_address_account = obj->sidechain_address_account; op.sidechain = obj->sidechain; + op.deposit_public_key = new_deposit_public_key; op.deposit_address = new_deposit_address; + op.withdraw_public_key = new_withdraw_public_key; op.withdraw_address = new_withdraw_address; trx.operations.push_back(op); @@ -92,7 +100,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_update_test ) { BOOST_REQUIRE( obj != idx.end() ); BOOST_CHECK( obj->sidechain_address_account == obj->sidechain_address_account ); BOOST_CHECK( obj->sidechain == obj->sidechain ); + BOOST_CHECK( obj->deposit_public_key == new_deposit_public_key ); BOOST_CHECK( obj->deposit_address == new_deposit_address ); + BOOST_CHECK( obj->withdraw_public_key == new_withdraw_public_key ); BOOST_CHECK( obj->withdraw_address == new_withdraw_address ); } }