Skip to content

Commit

Permalink
SON202 - Maintenance improvements (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
sierra19XX committed Mar 6, 2020
1 parent dbf7350 commit 2e8c074
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
10 changes: 9 additions & 1 deletion libraries/chain/include/graphene/chain/protocol/son.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@ namespace graphene { namespace chain {
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }
};

enum class son_maintenance_request_type
{
request_maintenance,
cancel_request_maintenance
};

struct son_maintenance_operation : public base_operation
{
struct fee_parameters_type { uint64_t fee = 0; };

asset fee;
son_id_type son_id;
account_id_type owner_account;
son_maintenance_request_type request_type = son_maintenance_request_type::request_maintenance;

account_id_type fee_payer()const { return owner_account; }
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }
Expand All @@ -108,5 +115,6 @@ FC_REFLECT(graphene::chain::son_heartbeat_operation, (fee)(son_id)(owner_account
FC_REFLECT(graphene::chain::son_report_down_operation::fee_parameters_type, (fee) )
FC_REFLECT(graphene::chain::son_report_down_operation, (fee)(son_id)(payer)(down_ts) )

FC_REFLECT_ENUM( graphene::chain::son_maintenance_request_type, (request_maintenance)(cancel_request_maintenance) )
FC_REFLECT(graphene::chain::son_maintenance_operation::fee_parameters_type, (fee) )
FC_REFLECT(graphene::chain::son_maintenance_operation, (fee)(son_id)(owner_account) )
FC_REFLECT(graphene::chain::son_maintenance_operation, (fee)(son_id)(owner_account)(request_type) )
15 changes: 9 additions & 6 deletions libraries/chain/son_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ object_id_type update_son_evaluator::do_apply(const son_update_operation& op)
void_result delete_son_evaluator::do_evaluate(const son_delete_operation& op)
{ try {
FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON_HARDFORK"); // can be removed after HF date pass
// Get the current block witness signatory
witness_id_type wit_id = db().get_scheduled_witness(1);
const witness_object& current_witness = wit_id(db());
// Either owner can remove or consensus son account
FC_ASSERT(op.payer == db().get(op.son_id).son_account || (db().is_son_dereg_valid(op.son_id) && op.payer == GRAPHENE_SON_ACCOUNT));
const auto& idx = db().get_index_type<son_index>().indices().get<by_id>();
Expand Down Expand Up @@ -204,7 +201,13 @@ void_result son_maintenance_evaluator::do_evaluate(const son_maintenance_operati
auto itr = idx.find(op.son_id);
FC_ASSERT( itr != idx.end() );
// Inactive SONs can't go to maintenance, toggle between active and request_maintenance states
FC_ASSERT(itr->status == son_status::active || itr->status == son_status::request_maintenance, "Inactive SONs can't go to maintenance");
if(op.request_type == son_maintenance_request_type::request_maintenance) {
FC_ASSERT(itr->status == son_status::active, "Inactive SONs can't request for maintenance");
} else if(op.request_type == son_maintenance_request_type::cancel_request_maintenance) {
FC_ASSERT(itr->status == son_status::request_maintenance, "Only maintenance requested SONs can cancel the request");
} else {
FC_ASSERT(false, "Invalid maintenance operation");
}
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }

Expand All @@ -214,11 +217,11 @@ object_id_type son_maintenance_evaluator::do_apply(const son_maintenance_operati
auto itr = idx.find(op.son_id);
if(itr != idx.end())
{
if(itr->status == son_status::active) {
if(itr->status == son_status::active && op.request_type == son_maintenance_request_type::request_maintenance) {
db().modify(*itr, [](son_object &so) {
so.status = son_status::request_maintenance;
});
} else if(itr->status == son_status::request_maintenance) {
} else if(itr->status == son_status::request_maintenance && op.request_type == son_maintenance_request_type::cancel_request_maintenance) {
db().modify(*itr, [](son_object &so) {
so.status = son_status::active;
});
Expand Down
8 changes: 4 additions & 4 deletions libraries/wallet/include/graphene/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ class wallet_api
* @param broadcast true to broadcast the transaction on the network
* @returns the signed transaction
*/
signed_transaction start_son_maintenance(string owner_account,
signed_transaction request_son_maintenance(string owner_account,
bool broadcast = false);

/** Modify status of the SON owned by the given account back to active.
Expand All @@ -1365,7 +1365,7 @@ class wallet_api
* @param broadcast true to broadcast the transaction on the network
* @returns the signed transaction
*/
signed_transaction stop_son_maintenance(string owner_account,
signed_transaction cancel_request_son_maintenance(string owner_account,
bool broadcast = false);

/** Lists all SONs in the blockchain.
Expand Down Expand Up @@ -2237,8 +2237,8 @@ FC_API( graphene::wallet::wallet_api,
(delete_son)
(list_sons)
(list_active_sons)
(start_son_maintenance)
(stop_son_maintenance)
(request_son_maintenance)
(cancel_request_son_maintenance)
(get_active_son_wallet)
(get_son_wallet_by_time_point)
(get_son_wallets)
Expand Down
14 changes: 8 additions & 6 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1942,14 +1942,15 @@ class wallet_api_impl
return sign_transaction( tx, broadcast );
} FC_CAPTURE_AND_RETHROW( (owner_account)(broadcast) ) }

signed_transaction start_son_maintenance(string owner_account,
signed_transaction request_son_maintenance(string owner_account,
bool broadcast)
{ try {
son_object son = get_son(owner_account);

son_maintenance_operation op;
op.owner_account = son.son_account;
op.son_id = son.id;
op.request_type = son_maintenance_request_type::request_maintenance;

signed_transaction tx;
tx.operations.push_back( op );
Expand All @@ -1959,14 +1960,15 @@ class wallet_api_impl
return sign_transaction( tx, broadcast );
} FC_CAPTURE_AND_RETHROW( (owner_account) ) }

signed_transaction stop_son_maintenance(string owner_account,
signed_transaction cancel_request_son_maintenance(string owner_account,
bool broadcast)
{ try {
son_object son = get_son(owner_account);

son_maintenance_operation op;
op.owner_account = son.son_account;
op.son_id = son.id;
op.request_type = son_maintenance_request_type::cancel_request_maintenance;

signed_transaction tx;
tx.operations.push_back( op );
Expand Down Expand Up @@ -4463,14 +4465,14 @@ signed_transaction wallet_api::delete_son(string owner_account,
return my->delete_son(owner_account, broadcast);
}

signed_transaction wallet_api::start_son_maintenance(string owner_account, bool broadcast)
signed_transaction wallet_api::request_son_maintenance(string owner_account, bool broadcast)
{
return my->start_son_maintenance(owner_account, broadcast);
return my->request_son_maintenance(owner_account, broadcast);
}

signed_transaction wallet_api::stop_son_maintenance(string owner_account, bool broadcast)
signed_transaction wallet_api::cancel_request_son_maintenance(string owner_account, bool broadcast)
{
return my->stop_son_maintenance(owner_account, broadcast);
return my->cancel_request_son_maintenance(owner_account, broadcast);
}

map<string, son_id_type> wallet_api::list_sons(const string& lowerbound, uint32_t limit)
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/son.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,23 +699,23 @@ BOOST_AUTO_TEST_CASE( maintenance_test )
BOOST_CHECK(son_obj.status == son_status::active);

// put SON in maintenance mode
con.wallet_api_ptr->start_son_maintenance(name, true);
con.wallet_api_ptr->request_son_maintenance(name, true);
BOOST_CHECK(generate_block());

// check SON is in request_maintenance
son_obj = con.wallet_api_ptr->get_son(name);
BOOST_CHECK(son_obj.status == son_status::request_maintenance);

// restore SON activity
con.wallet_api_ptr->stop_son_maintenance(name, true);
con.wallet_api_ptr->cancel_request_son_maintenance(name, true);
BOOST_CHECK(generate_block());

// check SON is active
son_obj = con.wallet_api_ptr->get_son(name);
BOOST_CHECK(son_obj.status == son_status::active);

// put SON in maintenance mode
con.wallet_api_ptr->start_son_maintenance(name, true);
con.wallet_api_ptr->request_son_maintenance(name, true);
BOOST_CHECK(generate_block());

// check SON is in request_maintenance
Expand Down
21 changes: 20 additions & 1 deletion tests/tests/son_operations_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ BOOST_AUTO_TEST_CASE( son_heartbeat_test ) {
son_maintenance_operation op;
op.owner_account = alice_id;
op.son_id = son_id_type(0);
op.request_type = son_maintenance_request_type::request_maintenance;

trx.operations.push_back(op);
set_expiration(db, trx);
Expand Down Expand Up @@ -586,10 +587,11 @@ BOOST_AUTO_TEST_CASE( son_heartbeat_test ) {

{
generate_block();
// Put SON in maintenance
// Request SON Maintenance
son_maintenance_operation op;
op.owner_account = alice_id;
op.son_id = son_id_type(0);
op.request_type = son_maintenance_request_type::request_maintenance;

trx.operations.push_back(op);
set_expiration(db, trx);
Expand All @@ -600,6 +602,23 @@ BOOST_AUTO_TEST_CASE( son_heartbeat_test ) {
BOOST_CHECK( obj->status == son_status::request_maintenance);
}

{
generate_block();
// Cancel SON Maintenance request
son_maintenance_operation op;
op.owner_account = alice_id;
op.son_id = son_id_type(0);
op.request_type = son_maintenance_request_type::cancel_request_maintenance;

trx.operations.push_back(op);
set_expiration(db, trx);
sign(trx, alice_private_key);
PUSH_TX( db, trx, ~0);
generate_block();
trx.clear();
BOOST_CHECK( obj->status == son_status::active);
}

// Modify SON's status to in_maintenance
db.modify( *obj, [&]( son_object& _s)
{
Expand Down

0 comments on commit 2e8c074

Please sign in to comment.