From 43550586c7acdd0fcbb50f21983a3a7968b33100 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Fri, 5 Feb 2016 23:32:49 -0600 Subject: [PATCH] Upgrading node and wallet config to new account numbers and adding version number. --- rai/core_test/node.cpp | 36 ++++++++++++++++++++++ rai/node/node.cpp | 66 +++++++++++++++++++++++++++++++--------- rai/node/node.hpp | 1 + rai/rai_wallet/entry.cpp | 30 ++++++++++++++++++ 4 files changed, 118 insertions(+), 15 deletions(-) diff --git a/rai/core_test/node.cpp b/rai/core_test/node.cpp index 2c4c3742..bd9b9aae 100644 --- a/rai/core_test/node.cpp +++ b/rai/core_test/node.cpp @@ -449,6 +449,42 @@ TEST (node_config, v1_v2_upgrade) ASSERT_TRUE (!!tree.get_child_optional ("work_peers")); } +TEST (node_config, unversioned_v2_upgrade) +{ + rai::logging logging1; + boost::property_tree::ptree tree; + tree.put ("peering_port", std::to_string (0)); + tree.put ("packet_delay_microseconds", std::to_string (0)); + tree.put ("bootstrap_fraction_numerator", std::to_string (0)); + tree.put ("creation_rebroadcast", std::to_string (0)); + tree.put ("rebroadcast_delay", std::to_string (0)); + tree.put ("receive_minimum", rai::amount (0).to_string_dec ()); + boost::property_tree::ptree logging_l; + logging1.serialize_json (logging_l); + tree.add_child ("logging", logging_l); + boost::property_tree::ptree preconfigured_peers_l; + tree.add_child ("preconfigured_peers", preconfigured_peers_l); + boost::property_tree::ptree preconfigured_representatives_l; + boost::property_tree::ptree entry; + entry.put ("", "TR6ZJ4pdp6HC76xMRpVDny5x2s8AEbrhFue3NKVxYYdmKuTEib"); + preconfigured_representatives_l.push_back (std::make_pair ("", entry)); + tree.add_child ("preconfigured_representatives", preconfigured_representatives_l); + boost::property_tree::ptree work_peers_l; + tree.add_child ("work_peers", work_peers_l); + bool upgraded (false); + rai::node_config config1; + ASSERT_FALSE (tree.get_optional ("version")); + config1.deserialize_json (upgraded, tree); + ASSERT_TRUE (upgraded); + ASSERT_EQ (1, config1.preconfigured_representatives.size ()); + ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", config1.preconfigured_representatives [0].to_account ()); + auto reps (tree.get_child ("preconfigured_representatives")); + ASSERT_EQ (1, reps.size ()); + ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", reps.begin ()->second.get ("")); + auto version (tree.get ("version")); + ASSERT_GT (std::stoull (version), 1); +} + TEST (node, confirm_locked) { rai::system system (24000, 1); diff --git a/rai/node/node.cpp b/rai/node/node.cpp index 88c7b9a9..d39b28f7 100644 --- a/rai/node/node.cpp +++ b/rai/node/node.cpp @@ -726,11 +726,55 @@ void rai::node_config::serialize_json (boost::property_tree::ptree & tree_a) con tree_a.add_child ("preconfigured_representatives", preconfigured_representatives_l); } +bool rai::node_config::upgrade_json (unsigned version, boost::property_tree::ptree & tree_a) +{ + auto result (false); + switch (version) + { + case 1: + { + auto reps_l (tree_a.get_child ("preconfigured_representatives")); + boost::property_tree::ptree reps; + for (auto i (reps_l.begin ()), n (reps_l.end ()); i != n; ++i) + { + rai::uint256_union account; + account.decode_account (i->second.get ("")); + boost::property_tree::ptree entry; + entry.put ("", account.to_account ()); + reps.push_back (std::make_pair ("", entry)); + } + tree_a.erase ("preconfigured_representatives"); + tree_a.add_child ("preconfigured_representatives", reps); + tree_a.erase ("version"); + tree_a.put ("version", "2"); + result = true; + } + case 2: + break; + default: + throw std::runtime_error ("Unknown node_config version"); + } + return result; +} + bool rai::node_config::deserialize_json (bool & upgraded_a, boost::property_tree::ptree & tree_a) { auto result (false); try { + auto version_l (tree_a.get_optional ("version")); + if (!version_l) + { + tree_a.put ("version", "1"); + version_l = "1"; + auto work_peers_l (tree_a.get_child_optional ("work_peers")); + if (!work_peers_l) + { + tree_a.add_child ("work_peers", boost::property_tree::ptree ()); + } + upgraded_a = true; + } + upgraded_a |= upgrade_json (std::stoull (version_l.get ()), tree_a); auto peering_port_l (tree_a.get ("peering_port")); auto packet_delay_microseconds_l (tree_a.get ("packet_delay_microseconds")); auto bootstrap_fraction_numerator_l (tree_a.get ("bootstrap_fraction_numerator")); @@ -738,23 +782,15 @@ bool rai::node_config::deserialize_json (bool & upgraded_a, boost::property_tree auto rebroadcast_delay_l (tree_a.get ("rebroadcast_delay")); auto receive_minimum_l (tree_a.get ("receive_minimum")); auto logging_l (tree_a.get_child ("logging")); - auto work_peers_l (tree_a.get_child_optional ("work_peers")); work_peers.clear (); - if (work_peers_l) + auto work_peers_l (tree_a.get_child ("work_peers")); + for (auto i (work_peers_l.begin ()), n (work_peers_l.end ()); i != n; ++i) { - for (auto i (work_peers_l.get ().begin ()), n (work_peers_l.get ().end ()); i != n; ++i) - { - auto work_peer (i->second.get ("")); - boost::asio::ip::address address; - uint16_t port; - result |= rai::parse_address_port (work_peer, address, port); - work_peers.push_back (std::make_pair (address, port)); - } - } - else - { - tree_a.add_child ("work_peers", boost::property_tree::ptree ()); - upgraded_a = true; + auto work_peer (i->second.get ("")); + boost::asio::ip::address address; + uint16_t port; + result |= rai::parse_address_port (work_peer, address, port); + work_peers.push_back (std::make_pair (address, port)); } auto preconfigured_peers_l (tree_a.get_child ("preconfigured_peers")); preconfigured_peers.clear (); diff --git a/rai/node/node.hpp b/rai/node/node.hpp index 9f9aef6a..e42584b6 100644 --- a/rai/node/node.hpp +++ b/rai/node/node.hpp @@ -282,6 +282,7 @@ public: node_config (uint16_t, rai::logging const &); void serialize_json (boost::property_tree::ptree &) const; bool deserialize_json (bool &, boost::property_tree::ptree &); + bool upgrade_json (unsigned, boost::property_tree::ptree &); rai::account random_representative (); uint16_t peering_port; rai::logging logging; diff --git a/rai/rai_wallet/entry.cpp b/rai/rai_wallet/entry.cpp index dfa48b29..db27e0a0 100644 --- a/rai/rai_wallet/entry.cpp +++ b/rai/rai_wallet/entry.cpp @@ -18,11 +18,41 @@ public: rai::random_pool.GenerateBlock (wallet.bytes.data (), wallet.bytes.size ()); assert (!wallet.is_zero ()); } + bool upgrade_json (unsigned version_a, boost::property_tree::ptree & tree_a) + { + auto result (false); + switch (version_a) + { + case 1: + { + rai::account account; + account.decode_account (tree_a.get ("account")); + tree_a.erase ("account"); + tree_a.put ("account", account.to_account ()); + tree_a.erase ("version"); + tree_a.put ("version", "2"); + result = true; + } + case 2: + break; + default: + throw std::runtime_error ("Unknown qt_wallet_config version"); + } + return result; + } bool deserialize_json (bool & upgraded_a, boost::property_tree::ptree & tree_a) { auto error (false); if (!tree_a.empty ()) { + auto version_l (tree_a.get_optional ("version")); + if (!version_l) + { + tree_a.put ("version", "1"); + version_l = "1"; + upgraded_a = true; + } + upgraded_a |= upgrade_json (std::stoull (version_l.get ()), tree_a); auto wallet_l (tree_a.get ("wallet")); auto account_l (tree_a.get ("account")); auto & node_l (tree_a.get_child ("node"));