From 2a6030ccd34536680ce2a26a3d07dfba6102167f Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 23 Aug 2018 23:06:48 +0100 Subject: [PATCH] Removing non-transactional version of valid_password. --- rai/core_test/rpc.cpp | 21 ++++++-- rai/core_test/wallet.cpp | 80 ++++++++++++++++++------------ rai/node/rpc.cpp | 103 +++++++++++++++++++-------------------- rai/node/wallet.cpp | 11 +---- rai/node/wallet.hpp | 1 - 5 files changed, 117 insertions(+), 99 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 64c6e090..f5358663 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -467,11 +467,18 @@ TEST (rpc, wallet_password_change) ASSERT_EQ (200, response.status); std::string account_text1 (response.json.get ("changed")); ASSERT_EQ (account_text1, "1"); - ASSERT_TRUE (system.wallet (0)->valid_password ()); + { + rai::transaction transaction (system.wallet (0)->store.environment, false); + ASSERT_TRUE (system.wallet (0)->store.valid_password (transaction)); + } ASSERT_TRUE (system.wallet (0)->enter_password ("")); - ASSERT_FALSE (system.wallet (0)->valid_password ()); + { + rai::transaction transaction (system.wallet (0)->store.environment, false); + ASSERT_FALSE (system.wallet (0)->store.valid_password (transaction)); + } ASSERT_FALSE (system.wallet (0)->enter_password ("test")); - ASSERT_TRUE (system.wallet (0)->valid_password ()); + rai::transaction transaction (system.wallet (0)->store.environment, false); + ASSERT_TRUE (system.wallet (0)->store.valid_password (transaction)); } TEST (rpc, wallet_password_enter) @@ -3436,7 +3443,10 @@ TEST (rpc, wallet_lock) boost::property_tree::ptree request; std::string wallet; system.nodes[0]->wallets.items.begin ()->first.encode_hex (wallet); - ASSERT_TRUE (system.wallet (0)->valid_password ()); + { + rai::transaction transaction (system.wallet (0)->store.environment, false); + ASSERT_TRUE (system.wallet (0)->store.valid_password (transaction)); + } request.put ("wallet", wallet); request.put ("action", "wallet_lock"); test_response response (request, rpc, system.service); @@ -3447,7 +3457,8 @@ TEST (rpc, wallet_lock) ASSERT_EQ (200, response.status); std::string account_text1 (response.json.get ("locked")); ASSERT_EQ (account_text1, "1"); - ASSERT_FALSE (system.wallet (0)->valid_password ()); + rai::transaction transaction (system.wallet (0)->store.environment, false); + ASSERT_FALSE (system.wallet (0)->store.valid_password (transaction)); } TEST (rpc, wallet_locked) diff --git a/rai/core_test/wallet.cpp b/rai/core_test/wallet.cpp index a79070ee..bec26e18 100644 --- a/rai/core_test/wallet.cpp +++ b/rai/core_test/wallet.cpp @@ -651,10 +651,14 @@ TEST (wallet, insert_locked) { rai::system system (24000, 1); auto wallet (system.wallet (0)); - wallet->store.rekey (rai::transaction (wallet->store.environment, true), "1"); - ASSERT_TRUE (wallet->valid_password ()); + { + rai::transaction transaction (wallet->store.environment, true); + wallet->store.rekey (transaction, "1"); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + } wallet->enter_password (""); - ASSERT_FALSE (wallet->valid_password ()); + rai::transaction transaction (wallet->store.environment, false); + ASSERT_FALSE (wallet->store.valid_password (transaction)); ASSERT_TRUE (wallet->insert_adhoc (rai::keypair ().prv).is_zero ()); } @@ -663,13 +667,16 @@ TEST (wallet, version_1_upgrade) rai::system system (24000, 1); auto wallet (system.wallet (0)); wallet->enter_initial_password (); - ASSERT_TRUE (wallet->valid_password ()); rai::keypair key; - wallet->store.rekey (rai::transaction (wallet->store.environment, true), "1"); - wallet->enter_password (""); - ASSERT_FALSE (wallet->valid_password ()); { rai::transaction transaction (wallet->store.environment, true); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + wallet->store.rekey (transaction, "1"); + } + wallet->enter_password (""); + { + rai::transaction transaction (wallet->store.environment, true); + ASSERT_FALSE (wallet->store.valid_password (transaction)); rai::raw_key password_l; rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special)); rai::raw_key kdf; @@ -682,14 +689,14 @@ TEST (wallet, version_1_upgrade) } wallet->enter_password ("1"); - ASSERT_TRUE (wallet->valid_password ()); - ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::transaction (wallet->store.environment, false))); - rai::raw_key prv; - ASSERT_FALSE (wallet->store.fetch (rai::transaction (wallet->store.environment, false), key.pub, prv)); - ASSERT_EQ (key.prv, prv); { rai::transaction transaction (wallet->store.environment, true); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + ASSERT_EQ (wallet->store.version_current, wallet->store.version (transaction)); + rai::raw_key prv; + ASSERT_FALSE (wallet->store.fetch (transaction, key.pub, prv)); + ASSERT_EQ (key.prv, prv); rai::raw_key password_l; rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special)); rai::raw_key kdf; @@ -701,10 +708,11 @@ TEST (wallet, version_1_upgrade) wallet->store.version_put (transaction, 1); } wallet->enter_password ("1"); - ASSERT_TRUE (wallet->valid_password ()); - ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::transaction (wallet->store.environment, false))); + rai::transaction transaction (wallet->store.environment, true); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + ASSERT_EQ (wallet->store.version_current, wallet->store.version (transaction)); rai::raw_key prv2; - ASSERT_FALSE (wallet->store.fetch (rai::transaction (wallet->store.environment, false), key.pub, prv2)); + ASSERT_FALSE (wallet->store.fetch (transaction, key.pub, prv2)); ASSERT_EQ (key.prv, prv2); } @@ -791,11 +799,15 @@ TEST (wallet, insert_deterministic_locked) { rai::system system (24000, 1); auto wallet (system.wallet (0)); - wallet->store.rekey (rai::transaction (wallet->store.environment, true), "1"); - ASSERT_TRUE (wallet->valid_password ()); + { + rai::transaction transaction (wallet->store.environment, true); + wallet->store.rekey (transaction, "1"); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + } wallet->enter_password (""); - ASSERT_FALSE (wallet->valid_password ()); - ASSERT_TRUE (wallet->deterministic_insert ().is_zero ()); + rai::transaction transaction (wallet->store.environment, true); + ASSERT_FALSE (wallet->store.valid_password (transaction)); + ASSERT_TRUE (wallet->deterministic_insert (transaction).is_zero ()); } TEST (wallet, version_2_upgrade) @@ -828,10 +840,16 @@ TEST (wallet, version_3_upgrade) { rai::system system (24000, 1); auto wallet (system.wallet (0)); - wallet->store.rekey (rai::transaction (wallet->store.environment, true), "1"); + { + rai::transaction transaction (wallet->store.environment, true); + wallet->store.rekey (transaction, "1"); + } wallet->enter_password ("1"); - ASSERT_TRUE (wallet->valid_password ()); - ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::transaction (wallet->store.environment, false))); + { + rai::transaction transaction (wallet->store.environment, false); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + ASSERT_EQ (wallet->store.version_current, wallet->store.version (transaction)); + } rai::keypair key; rai::raw_key seed; rai::uint256_union seed_ciphertext; @@ -851,18 +869,16 @@ TEST (wallet, version_3_upgrade) wallet->store.version_put (transaction, 3); } wallet->enter_password ("1"); - ASSERT_TRUE (wallet->valid_password ()); - ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::transaction (wallet->store.environment, false))); + rai::transaction transaction (wallet->store.environment, false); + ASSERT_TRUE (wallet->store.valid_password (transaction)); + ASSERT_EQ (wallet->store.version_current, wallet->store.version (transaction)); rai::raw_key prv; - ASSERT_FALSE (wallet->store.fetch (rai::transaction (wallet->store.environment, false), key.pub, prv)); + ASSERT_FALSE (wallet->store.fetch (transaction, key.pub, prv)); ASSERT_EQ (key.prv, prv); - { - rai::transaction transaction (wallet->store.environment, false); - rai::raw_key seed_compare; - wallet->store.seed (seed_compare, transaction); - ASSERT_EQ (seed, seed_compare); - ASSERT_NE (seed_ciphertext, wallet->store.entry_get_raw (transaction, rai::wallet_store::seed_special).key); - } + rai::raw_key seed_compare; + wallet->store.seed (seed_compare, transaction); + ASSERT_EQ (seed, seed_compare); + ASSERT_NE (seed_ciphertext, wallet->store.entry_get_raw (transaction, rai::wallet_store::seed_special).key); } TEST (wallet, no_work) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 6c24b742..09f0a2d0 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -621,16 +621,16 @@ void rai::rpc_handler::account_representative_set () auto account (account_impl ()); if (!ec) { - if (wallet->valid_password ()) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) + auto work (work_optional_impl ()); + if (!ec && work) { - auto work (work_optional_impl ()); - if (!ec && work) + rai::transaction transaction (node.store.environment, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, true); rai::account_info info; if (!node.store.account_get (transaction, account, info)) { @@ -648,30 +648,30 @@ void rai::rpc_handler::account_representative_set () ec = nano::error_common::account_not_found; } } - if (!ec) + else { - auto response_a (response); - wallet->change_async (account, representative, [response_a](std::shared_ptr block) { - rai::block_hash hash (0); - if (block != nullptr) - { - hash = block->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - }, - work == 0); + ec = nano::error_common::wallet_locked; } } - else + if (!ec) { - ec = nano::error_rpc::bad_representative_number; + auto response_a (response); + wallet->change_async (account, representative, [response_a](std::shared_ptr block) { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + }, + work == 0); } } else { - ec = nano::error_common::wallet_locked; + ec = nano::error_rpc::bad_representative_number; } } // Because of change_async @@ -2566,21 +2566,21 @@ void rai::rpc_handler::send () auto amount (amount_impl ()); if (!ec) { - if (wallet->valid_password ()) + std::string source_text (request.get ("source")); + rai::account source; + if (!source.decode_account (source_text)) { - std::string source_text (request.get ("source")); - rai::account source; - if (!source.decode_account (source_text)) + std::string destination_text (request.get ("destination")); + rai::account destination; + if (!destination.decode_account (destination_text)) { - std::string destination_text (request.get ("destination")); - rai::account destination; - if (!destination.decode_account (destination_text)) + auto work (work_optional_impl ()); + rai::uint128_t balance (0); + if (!ec) { - auto work (work_optional_impl ()); - rai::uint128_t balance (0); - if (!ec) + rai::transaction transaction (node.store.environment, work != 0); // false if no "work" in request, true if work > 0 + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, work != 0); // false if no "work" in request, true if work > 0 rai::account_info info; if (!node.store.account_get (transaction, source, info)) { @@ -2602,12 +2602,19 @@ void rai::rpc_handler::send () } } } - if (!ec) + else + { + ec = nano::error_common::wallet_locked; + } + } + if (!ec) + { + boost::optional send_id (request.get_optional ("id")); + if (balance >= amount.number ()) { - boost::optional send_id (request.get_optional ("id")); auto rpc_l (shared_from_this ()); auto response_a (response); - wallet->send_async (source, destination, amount.number (), [balance, amount, response_a](std::shared_ptr block_a) { + wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { if (block_a != nullptr) { rai::uint256_union hash (block_a->hash ()); @@ -2617,33 +2624,25 @@ void rai::rpc_handler::send () } else { - if (balance >= amount.number ()) - { - error_response (response_a, "Error generating block"); - } - else - { - std::error_code ec (nano::error_common::insufficient_balance); - error_response (response_a, ec.message ()); - } + error_response (response_a, "Error generating block"); } }, work == 0, send_id); } - } - else - { - ec = nano::error_rpc::bad_destination; + else + { + ec = nano::error_common::insufficient_balance; + } } } else { - ec = nano::error_rpc::bad_source; + ec = nano::error_rpc::bad_destination; } } else { - ec = nano::error_common::wallet_locked; + ec = nano::error_rpc::bad_source; } } // Because of send_async diff --git a/rai/node/wallet.cpp b/rai/node/wallet.cpp index c141cb3b..ce19b47b 100644 --- a/rai/node/wallet.cpp +++ b/rai/node/wallet.cpp @@ -764,23 +764,16 @@ void rai::wallet::enter_initial_password () store.password.value (password_l); if (password_l.data.is_zero ()) { - if (valid_password ()) + rai::transaction transaction (store.environment, true); + if (store.valid_password (transaction)) { // Newly created wallets have a zero key - rai::transaction transaction (store.environment, true); store.rekey (transaction, ""); } enter_password (""); } } -bool rai::wallet::valid_password () -{ - rai::transaction transaction (store.environment, false); - auto result (store.valid_password (transaction)); - return result; -} - bool rai::wallet::enter_password (std::string const & password_a) { rai::transaction transaction (store.environment, false); diff --git a/rai/node/wallet.hpp b/rai/node/wallet.hpp index 113812a1..9c4c365f 100644 --- a/rai/node/wallet.hpp +++ b/rai/node/wallet.hpp @@ -133,7 +133,6 @@ public: wallet (bool &, rai::mdb_env &, rai::transaction &, rai::wallets &, std::string const &); wallet (bool &, rai::mdb_env &, rai::transaction &, rai::wallets &, std::string const &, std::string const &); void enter_initial_password (); - bool valid_password (); bool enter_password (std::string const &); rai::public_key insert_adhoc (rai::raw_key const &, bool = true); rai::public_key insert_adhoc (MDB_txn *, rai::raw_key const &, bool = true);