From 22f76a04c01c0fe68ef8346bacae3c6d1e95ec90 Mon Sep 17 00:00:00 2001 From: gladcow Date: Wed, 6 Nov 2019 17:58:32 +0300 Subject: [PATCH 1/4] list_active_sons api call implementation --- .../wallet/include/graphene/wallet/wallet.hpp | 7 +++++ libraries/wallet/wallet.cpp | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 349ccea8..2b511ba7 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -1363,6 +1363,13 @@ class wallet_api */ map list_sons(const string& lowerbound, uint32_t limit); + /** Lists active at the moment SONs. + * This returns a list of all account names that own active SON, and the associated SON id, + * sorted by name. + * @returns a list of active SONs mapping SON names to SON ids + */ + map list_active_sons(); + /** Creates a witness object owned by the given account. * * An account can have at most one witness object. diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 69f3cb7a..c427f373 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1932,6 +1932,32 @@ public: return sign_transaction( tx, broadcast ); } FC_CAPTURE_AND_RETHROW( (owner_account)(broadcast) ) } + map list_active_sons() + { try { + global_property_object gpo = get_global_properties(); + std::vector> son_objects = + _remote_db->get_sons(gpo.active_sons); + vector owners; + owners.resize(son_objects.size()); + std::transform(son_objects.begin(), son_objects.end(), owners.begin(), + [](const fc::optional& obj) { + if(!obj) + FC_THROW("Invalid active SONs list in global properties."); + return obj->son_account; + }); + vector> accs = _remote_db->get_accounts(owners); + map result; + std::transform(accs.begin(), accs.end(), gpo.active_sons.begin(), + std::inserter(result, result.end()), + [](fc::optional& acct, + son_id_type& sid) { + if(!acct) + FC_THROW("Invalid active SONs list in global properties."); + return std::make_pair(string(acct->name), std::move(sid)); + }); + return result; + } FC_CAPTURE_AND_RETHROW() } + signed_transaction create_witness(string owner_account, string url, bool broadcast /* = false */) @@ -4303,6 +4329,11 @@ map wallet_api::list_sons(const string& lowerbound, uint32_ return my->_remote_db->lookup_son_accounts(lowerbound, limit); } +map wallet_api::list_active_sons() +{ + return my->list_active_sons(); +} + signed_transaction wallet_api::create_witness(string owner_account, string url, bool broadcast /* = false */) From 8c4eb579a724ec52265b3e0a8553637c5964e006 Mon Sep 17 00:00:00 2001 From: gladcow Date: Thu, 7 Nov 2019 13:03:32 +0300 Subject: [PATCH 2/4] unit test for list_active_sons --- tests/cli/son.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/cli/son.cpp b/tests/cli/son.cpp index b6c7b887..b72bf567 100644 --- a/tests/cli/son.cpp +++ b/tests/cli/son.cpp @@ -530,6 +530,62 @@ BOOST_AUTO_TEST_CASE( related_functions ) BOOST_TEST_MESSAGE("SON-related functions cli wallet tests end"); } +BOOST_FIXTURE_TEST_CASE( cli_list_active_sons, cli_fixture ) +{ + BOOST_TEST_MESSAGE("SON cli wallet tests for list_active_sons begin"); + try + { + son_test_helper sth(*this); + + signed_transaction vote_tx; + global_property_object gpo; + + gpo = con.wallet_api_ptr->get_global_properties(); + unsigned int son_number = gpo.parameters.maximum_son_count; + + // create son accounts + for(unsigned int i = 0; i < son_number + 1; i++) + { + sth.create_son("sonaccount" + fc::to_pretty_string(i), + "http://son" + fc::to_pretty_string(i), false); + } + BOOST_CHECK(generate_maintenance_block()); + + BOOST_TEST_MESSAGE("Voting for SONs"); + for(unsigned int i = 1; i < son_number + 1; i++) + { + std::string name = "sonaccount" + fc::to_pretty_string(i); + vote_tx = con.wallet_api_ptr->vote_for_son(name, name, true, true); + } + BOOST_CHECK(generate_maintenance_block()); + + for(unsigned int i = 1; i < son_number; i++) + { + std::string name1 = "sonaccount" + fc::to_pretty_string(i); + std::string name2 = "sonaccount" + fc::to_pretty_string(i + 1); + vote_tx = con.wallet_api_ptr->vote_for_son(name1, name2, true, true); + } + BOOST_CHECK(generate_maintenance_block()); + gpo = con.wallet_api_ptr->get_global_properties(); + BOOST_TEST_MESSAGE("gpo: " << gpo.active_sons.size()); + + BOOST_CHECK(gpo.active_sons.size() == son_number); + + map active_sons = con.wallet_api_ptr->list_active_sons(); + BOOST_CHECK(active_sons.size() == son_number); + for(unsigned int i = 1; i < son_number + 1; i++) + { + std::string name = "sonaccount" + fc::to_pretty_string(i); + BOOST_CHECK(active_sons.find(name) != active_sons.end()); + } + + } catch( fc::exception& e ) { + BOOST_TEST_MESSAGE("SON cli wallet tests exception"); + edump((e.to_detail_string())); + throw; + } + BOOST_TEST_MESSAGE("SON cli wallet tests for list_active_sons end"); +} BOOST_AUTO_TEST_SUITE_END() From 62f973ca80ad3a2b8359300f6196a488b9eb20ed Mon Sep 17 00:00:00 2001 From: gladcow Date: Mon, 11 Nov 2019 12:54:54 +0300 Subject: [PATCH 3/4] fix code style --- libraries/wallet/wallet.cpp | 42 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index c427f373..f769fd12 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1934,28 +1934,26 @@ public: map list_active_sons() { try { - global_property_object gpo = get_global_properties(); - std::vector> son_objects = - _remote_db->get_sons(gpo.active_sons); - vector owners; - owners.resize(son_objects.size()); - std::transform(son_objects.begin(), son_objects.end(), owners.begin(), - [](const fc::optional& obj) { - if(!obj) - FC_THROW("Invalid active SONs list in global properties."); - return obj->son_account; - }); - vector> accs = _remote_db->get_accounts(owners); - map result; - std::transform(accs.begin(), accs.end(), gpo.active_sons.begin(), - std::inserter(result, result.end()), - [](fc::optional& acct, - son_id_type& sid) { - if(!acct) - FC_THROW("Invalid active SONs list in global properties."); - return std::make_pair(string(acct->name), std::move(sid)); - }); - return result; + global_property_object gpo = get_global_properties(); + std::vector> son_objects = _remote_db->get_sons(gpo.active_sons); + vector owners; + owners.resize(son_objects.size()); + std::transform(son_objects.begin(), son_objects.end(), owners.begin(), + [](const fc::optional& obj) { + if(!obj) + FC_THROW("Invalid active SONs list in global properties."); + return obj->son_account; + }); + vector> accs = _remote_db->get_accounts(owners); + map result; + std::transform(accs.begin(), accs.end(), gpo.active_sons.begin(), + std::inserter(result, result.end()), + [](fc::optional& acct, son_id_type& sid) { + if(!acct) + FC_THROW("Invalid active SONs list in global properties."); + return std::make_pair(string(acct->name), std::move(sid)); + }); + return result; } FC_CAPTURE_AND_RETHROW() } signed_transaction create_witness(string owner_account, From dbf6be02c5a4def3d4c2e4405bc9ec517b9a8386 Mon Sep 17 00:00:00 2001 From: gladcow Date: Mon, 11 Nov 2019 13:13:57 +0300 Subject: [PATCH 4/4] use assert instead of checking condition with low possibility --- libraries/wallet/wallet.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index f769fd12..f7cc2a51 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1940,8 +1940,7 @@ public: owners.resize(son_objects.size()); std::transform(son_objects.begin(), son_objects.end(), owners.begin(), [](const fc::optional& obj) { - if(!obj) - FC_THROW("Invalid active SONs list in global properties."); + FC_ASSERT(obj, "Invalid active SONs list in global properties."); return obj->son_account; }); vector> accs = _remote_db->get_accounts(owners); @@ -1949,8 +1948,7 @@ public: std::transform(accs.begin(), accs.end(), gpo.active_sons.begin(), std::inserter(result, result.end()), [](fc::optional& acct, son_id_type& sid) { - if(!acct) - FC_THROW("Invalid active SONs list in global properties."); + FC_ASSERT(acct, "Invalid active SONs list in global properties."); return std::make_pair(string(acct->name), std::move(sid)); }); return result;