Refactor sidechain address mapping

This commit is contained in:
Srdjan Obucina 2020-04-17 02:47:23 +02:00
parent 4d7454a98a
commit 2739b56b94
7 changed files with 56 additions and 5 deletions

View file

@ -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<string> deposit_public_key;
optional<string> deposit_address;
optional<string> withdraw_public_key;
optional<string> 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)

View file

@ -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<by_sidechain>,
member<sidechain_address_object, sidechain_type, &sidechain_address_object::sidechain>
>,
ordered_non_unique< tag<by_deposit_public_key>,
member<sidechain_address_object, std::string, &sidechain_address_object::deposit_public_key>
>,
ordered_non_unique< tag<by_withdraw_public_key>,
member<sidechain_address_object, std::string, &sidechain_address_object::withdraw_public_key>
>,
ordered_unique< tag<by_account_and_sidechain>,
composite_key<sidechain_address_object,
member<sidechain_address_object, account_id_type, &sidechain_address_object::sidechain_address_account>,
@ -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) )

View file

@ -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>( [&]( 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;
});
}

View file

@ -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<sidechain_address_index>().indices().get<by_sidechain_and_deposit_address>();
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;

View file

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

View file

@ -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<optional<son_wallet_object>> 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,

View file

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