Extend sidechain_address_object to contain withdrawal addresses

- Withdrawal address is the address where system will send sidechain currencies
This commit is contained in:
Srdjan Obucina 2020-02-20 02:28:53 +01:00
parent 5a37d3ecd1
commit 9ac8bbe06a
10 changed files with 160 additions and 116 deletions

View file

@ -12,9 +12,8 @@ namespace graphene { namespace chain {
asset fee;
account_id_type sidechain_address_account;
graphene::peerplays_sidechain::sidechain_type sidechain;
string address;
string private_key;
string public_key;
string deposit_address;
string withdrawal_address;
account_id_type fee_payer()const { return sidechain_address_account; }
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }
@ -28,9 +27,8 @@ namespace graphene { namespace chain {
sidechain_address_id_type sidechain_address_id;
account_id_type sidechain_address_account;
graphene::peerplays_sidechain::sidechain_type sidechain;
optional<string> address;
optional<string> private_key;
optional<string> public_key;
optional<string> deposit_address;
optional<string> withdrawal_address;
account_id_type fee_payer()const { return sidechain_address_account; }
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }
@ -53,12 +51,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)(address)(private_key)(public_key) )
(sidechain_address_account)(sidechain)(deposit_address)(withdrawal_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)(address)(private_key)(public_key) )
(sidechain_address_account)(sidechain)(deposit_address)(withdrawal_address) )
FC_REFLECT(graphene::chain::sidechain_address_delete_operation::fee_parameters_type, (fee) )
FC_REFLECT(graphene::chain::sidechain_address_delete_operation, (fee)

View file

@ -21,21 +21,20 @@ namespace graphene { namespace chain {
account_id_type sidechain_address_account;
graphene::peerplays_sidechain::sidechain_type sidechain;
string address;
string private_key;
string public_key;
string deposit_address;
string withdrawal_address;
sidechain_address_object() :
sidechain(graphene::peerplays_sidechain::sidechain_type::bitcoin),
address(""),
private_key(""),
public_key("") {}
deposit_address(""),
withdrawal_address("") {}
};
struct by_account;
struct by_sidechain;
struct by_account_and_sidechain;
struct by_sidechain_and_address;
struct by_sidechain_and_deposit_address;
struct by_sidechain_and_withdrawal_address;
using sidechain_address_multi_index_type = multi_index_container<
sidechain_address_object,
indexed_by<
@ -54,10 +53,16 @@ namespace graphene { namespace chain {
member<sidechain_address_object, peerplays_sidechain::sidechain_type, &sidechain_address_object::sidechain>
>
>,
ordered_unique< tag<by_sidechain_and_address>,
ordered_unique< tag<by_sidechain_and_deposit_address>,
composite_key<sidechain_address_object,
member<sidechain_address_object, peerplays_sidechain::sidechain_type, &sidechain_address_object::sidechain>,
member<sidechain_address_object, std::string, &sidechain_address_object::address>
member<sidechain_address_object, std::string, &sidechain_address_object::deposit_address>
>
>,
ordered_unique< tag<by_sidechain_and_withdrawal_address>,
composite_key<sidechain_address_object,
member<sidechain_address_object, peerplays_sidechain::sidechain_type, &sidechain_address_object::sidechain>,
member<sidechain_address_object, std::string, &sidechain_address_object::withdrawal_address>
>
>
>
@ -67,4 +72,4 @@ namespace graphene { namespace chain {
} } // graphene::chain
FC_REFLECT_DERIVED( graphene::chain::sidechain_address_object, (graphene::db::object),
(sidechain_address_account)(sidechain)(address)(private_key)(public_key) )
(sidechain_address_account) (sidechain) (deposit_address) (withdrawal_address) )

View file

@ -19,9 +19,8 @@ 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.address = op.address;
obj.private_key = op.private_key;
obj.public_key = op.public_key;
obj.deposit_address = op.deposit_address;
obj.withdrawal_address = op.withdrawal_address;
});
return new_sidechain_address_object.id;
} FC_CAPTURE_AND_RETHROW( (op) ) }
@ -41,9 +40,8 @@ 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.address.valid()) sao.address = *op.address;
if(op.private_key.valid()) sao.private_key = *op.private_key;
if(op.public_key.valid()) sao.public_key = *op.public_key;
if(op.deposit_address.valid()) sao.deposit_address = *op.deposit_address;
if(op.withdrawal_address.valid()) sao.withdrawal_address = *op.withdrawal_address;
});
}
return op.sidechain_address_id;

View file

@ -16,7 +16,8 @@ public:
virtual ~sidechain_net_handler();
graphene::peerplays_sidechain::sidechain_type get_sidechain();
std::vector<std::string> get_sidechain_addresses();
std::vector<std::string> get_sidechain_deposit_addresses();
std::vector<std::string> get_sidechain_withdrawal_addresses();
void sidechain_event_data_received(const sidechain_event_data& sed);

View file

@ -19,7 +19,7 @@ graphene::peerplays_sidechain::sidechain_type sidechain_net_handler::get_sidecha
return sidechain;
}
std::vector<std::string> sidechain_net_handler::get_sidechain_addresses() {
std::vector<std::string> sidechain_net_handler::get_sidechain_deposit_addresses() {
std::vector<std::string> result;
switch (sidechain) {
@ -30,7 +30,29 @@ std::vector<std::string> sidechain_net_handler::get_sidechain_addresses() {
const auto& sidechain_addresses_by_sidechain_range = sidechain_addresses_by_sidechain_idx.equal_range(sidechain);
std::for_each(sidechain_addresses_by_sidechain_range.first, sidechain_addresses_by_sidechain_range.second,
[&result] (const sidechain_address_object& sao) {
result.push_back(sao.address);
result.push_back(sao.deposit_address);
});
break;
}
default:
assert(false);
}
return result;
}
std::vector<std::string> sidechain_net_handler::get_sidechain_withdrawal_addresses() {
std::vector<std::string> result;
switch (sidechain) {
case sidechain_type::bitcoin:
{
const auto& sidechain_addresses_idx = database.get_index_type<sidechain_address_index>();
const auto& sidechain_addresses_by_sidechain_idx = sidechain_addresses_idx.indices().get<by_sidechain>();
const auto& sidechain_addresses_by_sidechain_range = sidechain_addresses_by_sidechain_idx.equal_range(sidechain);
std::for_each(sidechain_addresses_by_sidechain_range.first, sidechain_addresses_by_sidechain_range.second,
[&result] (const sidechain_address_object& sao) {
result.push_back(sao.withdrawal_address);
});
break;
}
@ -57,39 +79,83 @@ void sidechain_net_handler::sidechain_event_data_received(const sidechain_event_
const chain::global_property_object& gpo = database.get_global_properties();
son_wallet_deposit_create_operation op;
op.payer = GRAPHENE_SON_ACCOUNT;
op.timestamp = sed.timestamp;
op.sidechain = sed.sidechain;
op.sidechain_uid = sed.sidechain_uid;
op.sidechain_transaction_id = sed.sidechain_transaction_id;
op.sidechain_from = sed.sidechain_from;
op.sidechain_to = sed.sidechain_to;
op.sidechain_currency = sed.sidechain_currency;
op.sidechain_amount = sed.sidechain_amount;
op.peerplays_from = sed.peerplays_from;
op.peerplays_to = sed.peerplays_to;
op.peerplays_asset = sed.peerplays_asset;
// Deposit request
if ((sed.peerplays_to == GRAPHENE_SON_ACCOUNT) && (sed.sidechain_currency.compare("1.3.0") != 0)) {
son_wallet_deposit_create_operation op;
op.payer = GRAPHENE_SON_ACCOUNT;
op.timestamp = sed.timestamp;
op.sidechain = sed.sidechain;
op.sidechain_uid = sed.sidechain_uid;
op.sidechain_transaction_id = sed.sidechain_transaction_id;
op.sidechain_from = sed.sidechain_from;
op.sidechain_to = sed.sidechain_to;
op.sidechain_currency = sed.sidechain_currency;
op.sidechain_amount = sed.sidechain_amount;
op.peerplays_from = sed.peerplays_from;
op.peerplays_to = sed.peerplays_to;
op.peerplays_asset = sed.peerplays_asset;
for (son_id_type son_id : plugin.get_sons()) {
if (plugin.is_active_son(son_id)) {
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_son_object(son_id).son_account;
proposal_op.proposed_ops.emplace_back( op_wrapper( op ) );
uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3;
proposal_op.expiration_time = time_point_sec( database.head_block_time().sec_since_epoch() + lifetime );
for (son_id_type son_id : plugin.get_sons()) {
if (plugin.is_active_son(son_id)) {
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_son_object(son_id).son_account;
proposal_op.proposed_ops.emplace_back( op_wrapper( op ) );
uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3;
proposal_op.expiration_time = time_point_sec( database.head_block_time().sec_since_epoch() + lifetime );
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son}", ("son", son_id));
signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(son_id), proposal_op);
try {
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){
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son} failed with exception ${e}", ("son", son_id) ("e", e.what()));
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son}", ("son", son_id));
signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(son_id), proposal_op);
try {
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){
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son} failed with exception ${e}", ("son", son_id) ("e", e.what()));
}
}
}
return;
}
// Withdrawal request
if ((sed.peerplays_to == GRAPHENE_SON_ACCOUNT) && (sed.sidechain_currency.compare("1.3.0") == 0)) {
son_wallet_withdrawal_create_operation op;
op.payer = GRAPHENE_SON_ACCOUNT;
op.timestamp = sed.timestamp;
op.sidechain = sed.sidechain;
op.sidechain_uid = sed.sidechain_uid;
op.sidechain_transaction_id = sed.sidechain_transaction_id;
op.sidechain_from = sed.sidechain_from;
op.sidechain_to = sed.sidechain_to;
op.sidechain_currency = sed.sidechain_currency;
op.sidechain_amount = sed.sidechain_amount;
op.peerplays_from = sed.peerplays_from;
op.peerplays_to = sed.peerplays_to;
op.peerplays_asset = sed.peerplays_asset;
for (son_id_type son_id : plugin.get_sons()) {
if (plugin.is_active_son(son_id)) {
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_son_object(son_id).son_account;
proposal_op.proposed_ops.emplace_back( op_wrapper( op ) );
uint32_t lifetime = ( gpo.parameters.block_interval * gpo.active_witnesses.size() ) * 3;
proposal_op.expiration_time = time_point_sec( database.head_block_time().sec_since_epoch() + lifetime );
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son}", ("son", son_id));
signed_transaction trx = plugin.database().create_signed_transaction(plugin.get_private_key(son_id), proposal_op);
try {
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){
ilog("sidechain_net_handler: sending proposal for son wallet transfer create operation by ${son} failed with exception ${e}", ("son", son_id) ("e", e.what()));
}
}
}
return;
}
FC_ASSERT(false, "Invalid sidechain event");
}
} } // graphene::peerplays_sidechain

View file

@ -447,7 +447,7 @@ void sidechain_net_handler_bitcoin::handle_event( const std::string& event_data
if( block != "" ) {
const auto& vins = extract_info_from_block( block );
const auto& sidechain_addresses_idx = database.get_index_type<sidechain_address_index>().indices().get<by_sidechain_and_address>();
const auto& sidechain_addresses_idx = database.get_index_type<sidechain_address_index>().indices().get<by_sidechain_and_deposit_address>();
for( const auto& v : vins ) {
const auto& addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain_type::bitcoin, v.address));

View file

@ -71,19 +71,10 @@ void sidechain_net_handler_peerplays::on_block_applied(const signed_block& b) {
sed.sidechain_to = fc::to_string(transfer_op.to.space_id) + "." + fc::to_string(transfer_op.to.type_id) + "." + fc::to_string((uint64_t)transfer_op.to.instance);
sed.sidechain_currency = transfer_op.amount.asset_id(plugin.database()).symbol;
sed.sidechain_amount = transfer_op.amount.amount;
if (transfer_op.amount.asset_id == asset_id_type(0)) {
// User is returning CORE/TEST to the SON wallet
// This is start of withdrawal process
// We need to return BTC
} else {
// User deposits other Peerplays asset
// We need to pay CORE/TEST
sed.peerplays_from = transfer_op.from;
sed.peerplays_to = transfer_op.to;
// We should calculate exchange rate between CORE/TEST and other Peerplays asset
sed.peerplays_asset = asset(transfer_op.amount.amount); // It i 1:1 for now
}
sed.peerplays_from = transfer_op.from;
sed.peerplays_to = transfer_op.to;
// We should calculate exchange rate between CORE/TEST and other Peerplays asset
sed.peerplays_asset = asset(transfer_op.amount.amount / transfer_op.amount.asset_id(plugin.database()).options.core_exchange_rate.quote.amount);
sidechain_event_data_received(sed);
}
}

View file

@ -1416,17 +1416,15 @@ 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 address sidechain address
* @param private_key private key for sidechain address
* @param public_key public key for sidechain address
* @param deposit_address sidechain address for deposits
* @param withdrawal_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,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast = false);
/** Updates existing sidechain address owned by the given account for a given sidechain.
@ -1435,17 +1433,15 @@ 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 address sidechain address
* @param private_key private key for sidechain address
* @param public_key public key for sidechain address
* @param deposit_address sidechain address for deposits
* @param withdrawal_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,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast = false);
/** Deletes existing sidechain address owned by the given account for a given sidechain.

View file

@ -2022,9 +2022,8 @@ public:
signed_transaction add_sidechain_address(string account,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast /* = false */)
{ try {
account_id_type sidechain_address_account_id = get_account_id(account);
@ -2032,9 +2031,8 @@ public:
sidechain_address_add_operation op;
op.sidechain_address_account = sidechain_address_account_id;
op.sidechain = sidechain;
op.address = address;
op.private_key = private_key;
op.public_key = public_key;
op.deposit_address = deposit_address;
op.withdrawal_address = withdrawal_address;
signed_transaction tx;
tx.operations.push_back( op );
@ -2046,9 +2044,8 @@ public:
signed_transaction update_sidechain_address(string account,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast /* = false */)
{ try {
account_id_type sidechain_address_account_id = get_account_id(account);
@ -2060,9 +2057,8 @@ public:
op.sidechain_address_id = sao->id;
op.sidechain_address_account = sidechain_address_account_id;
op.sidechain = sidechain;
op.address = address;
op.private_key = private_key;
op.public_key = public_key;
op.deposit_address = deposit_address;
op.withdrawal_address = withdrawal_address;
signed_transaction tx;
tx.operations.push_back( op );
@ -4501,22 +4497,20 @@ vector<optional<son_wallet_object>> wallet_api::get_son_wallets(uint32_t limit)
signed_transaction wallet_api::add_sidechain_address(string account,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast /* = false */)
{
return my->add_sidechain_address(account, sidechain, address, private_key, public_key, broadcast);
return my->add_sidechain_address(account, sidechain, deposit_address, withdrawal_address, broadcast);
}
signed_transaction wallet_api::update_sidechain_address(string account,
peerplays_sidechain::sidechain_type sidechain,
string address,
string private_key,
string public_key,
string deposit_address,
string withdrawal_address,
bool broadcast /* = false */)
{
return my->update_sidechain_address(account, sidechain, address, private_key, public_key, broadcast);
return my->update_sidechain_address(account, sidechain, deposit_address, withdrawal_address, broadcast);
}
signed_transaction wallet_api::delete_sidechain_address(string account,

View file

@ -30,9 +30,8 @@ BOOST_AUTO_TEST_CASE( sidechain_address_add_test ) {
op.sidechain_address_account = alice_id;
op.sidechain = graphene::peerplays_sidechain::sidechain_type::bitcoin;
op.address = "address";
op.private_key = "private_key";
op.public_key = "public_key";
op.deposit_address = "deposit_address";
op.withdrawal_address = "withdrawal_address";
trx.operations.push_back(op);
sign(trx, alice_private_key);
@ -48,9 +47,8 @@ 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 == graphene::peerplays_sidechain::sidechain_type::bitcoin );
BOOST_CHECK( obj->address == "address" );
BOOST_CHECK( obj->private_key == "private_key" );
BOOST_CHECK( obj->public_key == "public_key" );
BOOST_CHECK( obj->deposit_address == "deposit_address" );
BOOST_CHECK( obj->withdrawal_address == "withdrawal_address" );
}
BOOST_AUTO_TEST_CASE( sidechain_address_update_test ) {
@ -66,9 +64,8 @@ BOOST_AUTO_TEST_CASE( sidechain_address_update_test ) {
auto obj = idx.find( boost::make_tuple( alice_id, graphene::peerplays_sidechain::sidechain_type::bitcoin ) );
BOOST_REQUIRE( obj != idx.end() );
std::string new_address = "new_address";
std::string new_private_key = "new_private_key";
std::string new_public_key = "new_public_key";
std::string new_deposit_address = "new_deposit_address";
std::string new_withdrawal_address = "new_withdrawal_address";
{
BOOST_TEST_MESSAGE("Send sidechain_address_update_operation");
@ -77,9 +74,8 @@ 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.address = new_address;
op.private_key = new_private_key;
op.public_key = new_public_key;
op.deposit_address = new_deposit_address;
op.withdrawal_address = new_withdrawal_address;
trx.operations.push_back(op);
sign(trx, alice_private_key);
@ -96,9 +92,8 @@ 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->address == new_address );
BOOST_CHECK( obj->private_key == new_private_key );
BOOST_CHECK( obj->public_key == new_public_key );
BOOST_CHECK( obj->deposit_address == new_deposit_address );
BOOST_CHECK( obj->withdrawal_address == new_withdrawal_address );
}
}