From c2a8857a71c86a832763ce605217f88ec5db9c38 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sun, 28 Dec 2014 18:20:26 -0600 Subject: [PATCH] Renaming wallet_remove to wallet_destroy. Changing wallet_merge to wallet_move. --- rai/core/core.cpp | 85 +++++++++++++++++++++++++++++++++++---- rai/core/core.hpp | 5 ++- rai/core_test/rpc.cpp | 42 ++++++++++++++++++- rai/core_test/wallet.cpp | 8 +++- rai/core_test/wallets.cpp | 2 +- 5 files changed, 127 insertions(+), 15 deletions(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index 14e24b89..a6999433 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -692,6 +692,12 @@ void rai::wallet_store::insert (rai::private_key const & prv) assert (status.ok ()); } +void rai::wallet_store::erase (rai::public_key const & pub) +{ + auto status (handle->Delete (leveldb::WriteOptions (), leveldb::Slice (pub.chars.data (), pub.chars.size ()))); + assert (status.ok ()); +} + bool rai::wallet_store::fetch (rai::public_key const & pub, rai::private_key & prv) { auto result (false); @@ -745,19 +751,20 @@ void rai::wallet_store::serialize_json (std::string & string_a) string_a = ostream.str (); } -bool rai::wallet_store::merge (rai::wallet_store & other_a) +bool rai::wallet_store::move (rai::wallet_store & other_a, std::vector const & keys) { assert (valid_password ()); assert (other_a.valid_password ()); auto result (false); - for (auto i (other_a.begin ()), n (other_a.end ()); i != n; ++i) + for (auto i (keys.begin ()), n (keys.end ()); i != n; ++i) { rai::private_key prv; - auto error (other_a.fetch (i->first, prv)); + auto error (other_a.fetch (*i, prv)); result = result | error; if (!result) { insert (prv); + other_a.erase (*i); } } return result; @@ -880,7 +887,12 @@ bool rai::wallet::import (std::string const & json_a, std::string const & passwo result = !store_l.valid_password (); if (!result) { - result = store.merge (store_l); + std::vector accounts; + for (auto i (store_l.begin ()), n (store_l.end ()); i != n; ++i) + { + accounts.push_back (i->first); + } + result = store.move (store_l, accounts); } } return result; @@ -951,7 +963,7 @@ std::shared_ptr rai::wallets::create (rai::uint256_union const & i return result; } -void rai::wallets::remove (rai::uint256_union const & id_a) +void rai::wallets::destroy (rai::uint256_union const & id_a) { auto existing (items.find (id_a)); assert (existing != items.end ()); @@ -2453,7 +2465,7 @@ void rai::rpc::operator () (boost::network::http::server ::request con response.content = "Bad account number"; } } - else if (action == "wallet_remove") + else if (action == "wallet_destroy") { std::string wallet_text (request_l.get ("wallet")); rai::uint256_union wallet; @@ -2463,7 +2475,7 @@ void rai::rpc::operator () (boost::network::http::server ::request con auto existing (client.wallets.items.find (wallet)); if (existing != client.wallets.items.end ()) { - client.wallets.remove (wallet); + client.wallets.destroy (wallet); boost::property_tree::ptree response_l; set_response (response, response_l); } @@ -2476,7 +2488,64 @@ void rai::rpc::operator () (boost::network::http::server ::request con else { response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); - response.content = "Bad account number"; + response.content = "Bad wallet number"; + } + } + else if (action == "account_move") + { + std::string wallet_text (request_l.get ("wallet")); + std::string source_text (request_l.get ("source")); + auto accounts_text (request_l.get_child ("accounts")); + rai::uint256_union wallet; + auto error (wallet.decode_hex (wallet_text)); + if (!error) + { + auto existing (client.wallets.items.find (wallet)); + if (existing != client.wallets.items.end ()) + { + auto wallet (existing->second); + rai::uint256_union source; + auto error (source.decode_hex (source_text)); + if (!error) + { + auto existing (client.wallets.items.find (source)); + if (existing != client.wallets.items.end ()) + { + auto source (existing->second); + std::vector accounts; + for (auto i (accounts_text.begin ()), n (accounts_text.end ()); i != n; ++i) + { + rai::public_key account; + account.decode_hex (i->second.get ("")); + accounts.push_back (account); + } + auto error (wallet->store.move (source->store, accounts)); + boost::property_tree::ptree response_l; + response_l.put ("moved", error ? "false" : "true"); + set_response (response, response_l); + } + else + { + response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); + response.content = "Source not found"; + } + } + else + { + response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); + response.content = "Bad source number"; + } + } + else + { + response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); + response.content = "Wallet not found"; + } + } + else + { + response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); + response.content = "Bad wallet number"; } } else diff --git a/rai/core/core.hpp b/rai/core/core.hpp index 73af3447..0268e4ef 100644 --- a/rai/core/core.hpp +++ b/rai/core/core.hpp @@ -291,6 +291,7 @@ public: rai::account representative (); void representative_set (rai::account const &); void insert (rai::private_key const &); + void erase (rai::public_key const &); bool fetch (rai::public_key const &, rai::private_key &); bool exists (rai::public_key const &); key_iterator find (rai::uint256_union const &); @@ -299,7 +300,7 @@ public: rai::uint256_union derive_key (std::string const &); rai::uint128_t balance (rai::ledger &); void serialize_json (std::string &); - bool merge (rai::wallet_store &); + bool move (rai::wallet_store &, std::vector const &); rai::fan password; static rai::uint256_union const version_1; static rai::uint256_union const version_current; @@ -332,7 +333,7 @@ public: wallets (rai::client &, boost::filesystem::path const &); std::shared_ptr open (rai::uint256_union const &); std::shared_ptr create (rai::uint256_union const &); - void remove (rai::uint256_union const &); + void destroy (rai::uint256_union const &); std::unordered_map > items; boost::filesystem::path const path; rai::client & client; diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 9a1f0b65..1e059a37 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -637,7 +637,7 @@ TEST (rpc, wallet_export) ASSERT_TRUE (store.exists (rai::test_genesis_key.pub)); } -TEST (rpc, wallet_remove) +TEST (rpc, wallet_destroy) { rai::system system (24000, 1); auto wallet_id (system.clients [0]->wallets.items.begin ()->first); @@ -648,7 +648,7 @@ TEST (rpc, wallet_remove) boost::network::http::server ::response response; request.method = "POST"; boost::property_tree::ptree request_tree; - request_tree.put ("action", "wallet_remove"); + request_tree.put ("action", "wallet_destroy"); request_tree.put ("wallet", wallet_id.to_string ()); std::stringstream ostream; boost::property_tree::write_json (ostream, request_tree); @@ -659,4 +659,42 @@ TEST (rpc, wallet_remove) std::stringstream istream (response.content); boost::property_tree::read_json (istream, response_tree); ASSERT_EQ (system.clients [0]->wallets.items.end (), system.clients [0]->wallets.items.find (wallet_id)); +} + +TEST (rpc, account_move) +{ + rai::system system (24000, 1); + auto wallet_id (system.clients [0]->wallets.items.begin ()->first); + auto pool (boost::make_shared ()); + rai::rpc rpc (system.service, pool, boost::asio::ip::address_v6::loopback (), 25000, *system.clients [0], true); + auto destination (system.wallet (0)); + destination->store.insert (rai::test_genesis_key.prv); + rai::keypair source_id; + rai::keypair key; + auto source (system.clients [0]->wallets.create (source_id.prv)); + source->store.insert (key.prv); + boost::network::http::server ::request request; + boost::network::http::server ::response response; + request.method = "POST"; + boost::property_tree::ptree request_tree; + request_tree.put ("action", "account_move"); + request_tree.put ("wallet", wallet_id.to_string ()); + request_tree.put ("source", source_id.prv.to_string ()); + boost::property_tree::ptree keys; + boost::property_tree::ptree entry; + entry.put ("", key.pub.to_string ()); + keys.push_back (std::make_pair ("", entry)); + request_tree.add_child ("accounts", keys); + std::stringstream ostream; + boost::property_tree::write_json (ostream, request_tree); + request.body = ostream.str (); + rpc (request, response); + ASSERT_EQ (boost::network::http::server ::response::ok, response.status); + boost::property_tree::ptree response_tree; + std::stringstream istream (response.content); + boost::property_tree::read_json (istream, response_tree); + ASSERT_EQ ("true", response_tree.get ("moved")); + ASSERT_NE (destination->store.end (), destination->store.find (key.pub)); + ASSERT_NE (destination->store.end (), destination->store.find (rai::test_genesis_key.pub)); + ASSERT_EQ (source->store.end (), source->store.begin ()); } \ No newline at end of file diff --git a/rai/core_test/wallet.cpp b/rai/core_test/wallet.cpp index 64088506..5283485d 100644 --- a/rai/core_test/wallet.cpp +++ b/rai/core_test/wallet.cpp @@ -396,7 +396,7 @@ TEST (wallet, serialize_json_password) ASSERT_EQ (key.prv, prv); } -TEST (wallet_store, merge) +TEST (wallet_store, move) { auto error (false); rai::wallet_store wallet1 (error, boost::filesystem::unique_path ()); @@ -408,6 +408,10 @@ TEST (wallet_store, merge) rai::keypair key2; wallet2.insert (key2.prv); ASSERT_FALSE (wallet1.exists (key2.pub)); - ASSERT_FALSE (wallet1.merge (wallet2)); + ASSERT_TRUE (wallet2.exists (key2.pub)); + std::vector keys; + keys.push_back (key2.pub); + ASSERT_FALSE (wallet1.move (wallet2, keys)); ASSERT_TRUE (wallet1.exists (key2.pub)); + ASSERT_FALSE (wallet2.exists (key2.pub)); } \ No newline at end of file diff --git a/rai/core_test/wallets.cpp b/rai/core_test/wallets.cpp index d486df23..a65e501c 100644 --- a/rai/core_test/wallets.cpp +++ b/rai/core_test/wallets.cpp @@ -41,7 +41,7 @@ TEST (wallets, remove) ASSERT_EQ (0, wallets.items.size ()); auto wallet (wallets.create (one)); ASSERT_EQ (1, wallets.items.size ()); - wallets.remove (one); + wallets.destroy (one); ASSERT_EQ (0, wallets.items.size ()); } {