From 6f0c025462ac965dd5e63cf2653caa31e0bd5a32 Mon Sep 17 00:00:00 2001 From: Srdjan Obucina Date: Fri, 17 Jan 2020 05:29:23 +0100 Subject: [PATCH] son_wallet_object operations --- libraries/app/impacted.cpp | 6 +++ libraries/chain/db_init.cpp | 4 +- libraries/chain/db_notify.cpp | 6 +++ .../graphene/chain/protocol/operations.hpp | 2 + .../graphene/chain/protocol/son_wallet.hpp | 26 +++++++++++ .../graphene/chain/son_wallet_evaluator.hpp | 18 ++++++++ .../graphene/chain/son_wallet_object.hpp | 14 ++++-- libraries/chain/son_wallet_evaluator.cpp | 35 ++++++++++++++ tests/tests/son_wallet_tests.cpp | 46 +++++++++++++++++++ 9 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 tests/tests/son_wallet_tests.cpp diff --git a/libraries/app/impacted.cpp b/libraries/app/impacted.cpp index cd92969b..c2d83b1c 100644 --- a/libraries/app/impacted.cpp +++ b/libraries/app/impacted.cpp @@ -316,6 +316,12 @@ struct get_impacted_account_visitor void operator()( const son_wallet_create_operation& op ){ _impacted.insert( op.payer ); } + void operator()( const son_wallet_update_operation& op ){ + _impacted.insert( op.payer ); + } + void operator()( const son_wallet_close_operation& op ){ + _impacted.insert( op.payer ); + } void operator()( const sidechain_address_add_operation& op ){ _impacted.insert( op.sidechain_address_account ); } diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index d336bd43..48bbf7bc 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -253,7 +253,9 @@ void database::initialize_evaluators() register_evaluator(); register_evaluator(); register_evaluator(); - register_evaluator(); + register_evaluator(); + register_evaluator(); + register_evaluator(); register_evaluator(); register_evaluator(); register_evaluator(); diff --git a/libraries/chain/db_notify.cpp b/libraries/chain/db_notify.cpp index b3aa366e..50250fee 100644 --- a/libraries/chain/db_notify.cpp +++ b/libraries/chain/db_notify.cpp @@ -303,6 +303,12 @@ struct get_impacted_account_visitor void operator()( const son_wallet_create_operation& op ) { _impacted.insert( op.payer ); } + void operator()( const son_wallet_update_operation& op ) { + _impacted.insert( op.payer ); + } + void operator()( const son_wallet_close_operation& op ) { + _impacted.insert( op.payer ); + } void operator()( const sidechain_address_add_operation& op ) { _impacted.insert( op.sidechain_address_account ); } diff --git a/libraries/chain/include/graphene/chain/protocol/operations.hpp b/libraries/chain/include/graphene/chain/protocol/operations.hpp index 5837c2d9..e33de47d 100644 --- a/libraries/chain/include/graphene/chain/protocol/operations.hpp +++ b/libraries/chain/include/graphene/chain/protocol/operations.hpp @@ -145,6 +145,8 @@ namespace graphene { namespace chain { son_heartbeat_operation, son_report_down_operation, son_wallet_create_operation, + son_wallet_update_operation, + son_wallet_close_operation, sidechain_address_add_operation, sidechain_address_update_operation, sidechain_address_delete_operation diff --git a/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp b/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp index 386f010c..b95ebabd 100644 --- a/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp +++ b/libraries/chain/include/graphene/chain/protocol/son_wallet.hpp @@ -14,7 +14,33 @@ namespace graphene { namespace chain { share_type calculate_fee(const fee_parameters_type& k)const { return 0; } }; + struct son_wallet_update_operation : public base_operation + { + struct fee_parameters_type { uint64_t fee = 0; }; + + asset fee; + account_id_type payer; + + account_id_type fee_payer()const { return payer; } + share_type calculate_fee(const fee_parameters_type& k)const { return 0; } + }; + + struct son_wallet_close_operation : public base_operation + { + struct fee_parameters_type { uint64_t fee = 0; }; + + asset fee; + account_id_type payer; + + account_id_type fee_payer()const { return payer; } + share_type calculate_fee(const fee_parameters_type& k)const { return 0; } + }; + } } // namespace graphene::chain FC_REFLECT(graphene::chain::son_wallet_create_operation::fee_parameters_type, (fee) ) FC_REFLECT(graphene::chain::son_wallet_create_operation, (fee)(payer) ) +FC_REFLECT(graphene::chain::son_wallet_update_operation::fee_parameters_type, (fee) ) +FC_REFLECT(graphene::chain::son_wallet_update_operation, (fee)(payer) ) +FC_REFLECT(graphene::chain::son_wallet_close_operation::fee_parameters_type, (fee) ) +FC_REFLECT(graphene::chain::son_wallet_close_operation, (fee)(payer) ) diff --git a/libraries/chain/include/graphene/chain/son_wallet_evaluator.hpp b/libraries/chain/include/graphene/chain/son_wallet_evaluator.hpp index e2689765..692fca05 100644 --- a/libraries/chain/include/graphene/chain/son_wallet_evaluator.hpp +++ b/libraries/chain/include/graphene/chain/son_wallet_evaluator.hpp @@ -13,4 +13,22 @@ public: object_id_type do_apply(const son_wallet_create_operation& o); }; +class update_son_wallet_evaluator : public evaluator +{ +public: + typedef son_wallet_update_operation operation_type; + + void_result do_evaluate(const son_wallet_update_operation& o); + object_id_type do_apply(const son_wallet_update_operation& o); +}; + +class close_son_wallet_evaluator : public evaluator +{ +public: + typedef son_wallet_close_operation operation_type; + + void_result do_evaluate(const son_wallet_close_operation& o); + object_id_type do_apply(const son_wallet_close_operation& o); +}; + } } // namespace graphene::chain diff --git a/libraries/chain/include/graphene/chain/son_wallet_object.hpp b/libraries/chain/include/graphene/chain/son_wallet_object.hpp index 3d546355..aec28342 100644 --- a/libraries/chain/include/graphene/chain/son_wallet_object.hpp +++ b/libraries/chain/include/graphene/chain/son_wallet_object.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include namespace graphene { namespace chain { @@ -20,15 +21,22 @@ namespace graphene { namespace chain { time_point_sec expires; flat_map addresses; + vector sons; }; - struct by_sidechain_type; - struct by_address; + struct by_valid_from; + struct by_expires; using son_wallet_multi_index_type = multi_index_container< son_wallet_object, indexed_by< ordered_unique< tag, member + >, + ordered_unique< tag, + member + >, + ordered_unique< tag, + member > > >; @@ -36,4 +44,4 @@ namespace graphene { namespace chain { } } // graphene::chain FC_REFLECT_DERIVED( graphene::chain::son_wallet_object, (graphene::db::object), - (valid_from) (expires) (addresses) ) + (valid_from) (expires) (addresses) (sons) ) diff --git a/libraries/chain/son_wallet_evaluator.cpp b/libraries/chain/son_wallet_evaluator.cpp index c64f884e..c7226bac 100644 --- a/libraries/chain/son_wallet_evaluator.cpp +++ b/libraries/chain/son_wallet_evaluator.cpp @@ -17,6 +17,41 @@ object_id_type create_son_wallet_evaluator::do_apply(const son_wallet_create_ope 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; +} FC_CAPTURE_AND_RETHROW( (op) ) } + +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."); + 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; +} 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."); + 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; } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/tests/tests/son_wallet_tests.cpp b/tests/tests/son_wallet_tests.cpp new file mode 100644 index 00000000..e693e562 --- /dev/null +++ b/tests/tests/son_wallet_tests.cpp @@ -0,0 +1,46 @@ +#include + +#include "../common/database_fixture.hpp" + +#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 ) { + + BOOST_TEST_MESSAGE("son_wallet_create_test"); + + generate_block(); + set_expiration(db, trx); + + ACTORS((alice)); + + generate_block(); + set_expiration(db, trx); + + { + BOOST_TEST_MESSAGE("Send son_wallet_create_operation"); + + son_wallet_create_operation op; + + op.payer = gpo.parameters.get_son_btc_account_id(); + + trx.operations.push_back(op); + sign(trx, alice_private_key); + PUSH_TX(db, trx, ~0); + } + generate_block(); + + BOOST_TEST_MESSAGE("Check son_wallet_create_operation results"); + + const auto& idx = db.get_index_type().indices().get(); + BOOST_REQUIRE( idx.size() == 1 ); +} + +BOOST_AUTO_TEST_SUITE_END()