From 2615c41224c305025b6882b9583ca45ebbc77444 Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Fri, 17 Jan 2020 14:56:44 +0100 Subject: [PATCH] son_wallet_object operations completed, basic tests added --- .../graphene/chain/protocol/son_wallet.hpp | 4 + libraries/chain/son_wallet_evaluator.cpp | 38 ++++++---- tests/tests/son_wallet_tests.cpp | 76 ++++++++++++++++++- 3 files changed, 100 insertions(+), 18 deletions(-) diff --git a/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp b/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp index b95ebabd..8338e916 100644 --- a/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp +++ b/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp @@ -20,6 +20,9 @@ namespace graphene { namespace chain { asset fee; account_id_type payer; + son_wallet_id_type son_wallet_id; + graphene::peerplays_sidechain::sidechain_type sidechain; + string address; account_id_type fee_payer()const { return payer; } share_type calculate_fee(const fee_parameters_type& k)const { return 0; } @@ -31,6 +34,7 @@ namespace graphene { namespace chain { asset fee; account_id_type payer; + son_wallet_id_type son_wallet_id; account_id_type fee_payer()const { return payer; } share_type calculate_fee(const fee_parameters_type& k)const { return 0; } diff --git a/libraries/chain/son_wallet_evaluator.cpp b/libraries/chain/son_wallet_evaluator.cpp index c7226bac..08744862 100644 --- a/libraries/chain/son_wallet_evaluator.cpp +++ b/libraries/chain/son_wallet_evaluator.cpp @@ -25,35 +25,45 @@ object_id_type create_son_wallet_evaluator::do_apply(const son_wallet_create_ope void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_operation& op) { try{ FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK"); - FC_ASSERT(db().get_global_properties().parameters.get_son_btc_account_id() != GRAPHENE_NULL_ACCOUNT, "SON paying account not set."); + const auto& idx = db().get_index_type().indices().get(); + FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() ); + auto itr = idx.find(op.son_wallet_id); + //FC_ASSERT( itr.addresses[op.sidechain] == "", "Sidechain wallet address already set"); return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } object_id_type update_son_wallet_evaluator::do_apply(const son_wallet_update_operation& op) { try { - const auto& new_son_wallet_object = db().create( [&]( son_wallet_object& obj ){ - obj.valid_from = db().head_block_time(); - obj.expires = time_point_sec::maximum(); - obj.sons = db().get_global_properties().active_sons; - }); - return new_son_wallet_object.id; + const auto& idx = db().get_index_type().indices().get(); + auto itr = idx.find(op.son_wallet_id); + if(itr != idx.end()) + { + db().modify(*itr, [&op](son_wallet_object &swo) { + swo.addresses[op.sidechain] = op.address; + }); + } + return op.son_wallet_id; } FC_CAPTURE_AND_RETHROW( (op) ) } void_result close_son_wallet_evaluator::do_evaluate(const son_wallet_close_operation& op) { try{ FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK"); - FC_ASSERT(db().get_global_properties().parameters.get_son_btc_account_id() != GRAPHENE_NULL_ACCOUNT, "SON paying account not set."); + const auto& idx = db().get_index_type().indices().get(); + FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() ); return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } object_id_type close_son_wallet_evaluator::do_apply(const son_wallet_close_operation& op) { try { - const auto& new_son_wallet_object = db().create( [&]( son_wallet_object& obj ){ - obj.valid_from = db().head_block_time(); - obj.expires = time_point_sec::maximum(); - obj.sons = db().get_global_properties().active_sons; - }); - return new_son_wallet_object.id; + const auto& idx = db().get_index_type().indices().get(); + auto itr = idx.find(op.son_wallet_id); + if(itr != idx.end()) + { + db().modify(*itr, [&, &op](son_wallet_object &swo) { + swo.expires = db().head_block_time(); + }); + } + return op.son_wallet_id; } FC_CAPTURE_AND_RETHROW( (op) ) } } } // namespace graphene::chain diff --git a/tests/tests/son_wallet_tests.cpp b/tests/tests/son_wallet_tests.cpp index e693e562..e2c403b2 100644 --- a/tests/tests/son_wallet_tests.cpp +++ b/tests/tests/son_wallet_tests.cpp @@ -4,12 +4,11 @@ #include #include -#include +#include using namespace graphene::chain; using namespace graphene::chain::test; - BOOST_FIXTURE_TEST_SUITE( son_wallet_tests, database_fixture ) BOOST_AUTO_TEST_CASE( son_wallet_create_test ) { @@ -29,7 +28,7 @@ BOOST_AUTO_TEST_CASE( son_wallet_create_test ) { son_wallet_create_operation op; - op.payer = gpo.parameters.get_son_btc_account_id(); + op.payer = db.get_global_properties().parameters.get_son_btc_account_id(); trx.operations.push_back(op); sign(trx, alice_private_key); @@ -39,8 +38,77 @@ BOOST_AUTO_TEST_CASE( son_wallet_create_test ) { BOOST_TEST_MESSAGE("Check son_wallet_create_operation results"); - const auto& idx = db.get_index_type().indices().get(); + const auto& idx = db.get_index_type().indices().get(); BOOST_REQUIRE( idx.size() == 1 ); + auto obj = idx.find(son_wallet_id_type(0)); + BOOST_REQUIRE( obj->addresses.at(graphene::peerplays_sidechain::sidechain_type::bitcoin) == "" ); + BOOST_REQUIRE( obj->expires == time_point_sec::maximum() ); +} + +BOOST_AUTO_TEST_CASE( son_wallet_update_test ) { + + BOOST_TEST_MESSAGE("son_wallet_update_test"); + + INVOKE(son_wallet_create_test); + GET_ACTOR(alice); + + { + BOOST_TEST_MESSAGE("Send son_wallet_update_operation"); + + son_wallet_update_operation op; + + op.payer = db.get_global_properties().parameters.get_son_btc_account_id(); + op.son_wallet_id = son_wallet_id_type(0); + op.sidechain = graphene::peerplays_sidechain::sidechain_type::bitcoin; + op.address = "bitcoin address"; + + trx.operations.push_back(op); + sign(trx, alice_private_key); + PUSH_TX(db, trx, ~0); + } + generate_block(); + + { + BOOST_TEST_MESSAGE("Check son_wallet_update_operation results"); + + const auto& idx = db.get_index_type().indices().get(); + BOOST_REQUIRE( idx.size() == 1 ); + auto obj = idx.find(son_wallet_id_type(0)); + BOOST_REQUIRE( obj->addresses.at(graphene::peerplays_sidechain::sidechain_type::bitcoin) == "bitcoin address" ); + } + +} + +BOOST_AUTO_TEST_CASE( son_wallet_close_test ) { + + BOOST_TEST_MESSAGE("son_wallet_close_test"); + + INVOKE(son_wallet_create_test); + GET_ACTOR(alice); + + { + BOOST_TEST_MESSAGE("Send son_wallet_close_operation"); + + son_wallet_close_operation op; + + op.payer = db.get_global_properties().parameters.get_son_btc_account_id(); + op.son_wallet_id = son_wallet_id_type(0); + + trx.operations.push_back(op); + sign(trx, alice_private_key); + PUSH_TX(db, trx, ~0); + } + generate_block(); + + { + BOOST_TEST_MESSAGE("Check son_wallet_close_operation results"); + + const auto& idx = db.get_index_type().indices().get(); + BOOST_REQUIRE( idx.size() == 1 ); + auto obj = idx.find(son_wallet_id_type(0)); + BOOST_REQUIRE( obj->expires != time_point_sec::maximum() ); + } + } BOOST_AUTO_TEST_SUITE_END()