Removing non-transactional version of valid_password.

This commit is contained in:
clemahieu 2018-08-23 23:06:48 +01:00
commit 2a6030ccd3
5 changed files with 117 additions and 99 deletions

View file

@ -467,11 +467,18 @@ TEST (rpc, wallet_password_change)
ASSERT_EQ (200, response.status); ASSERT_EQ (200, response.status);
std::string account_text1 (response.json.get<std::string> ("changed")); std::string account_text1 (response.json.get<std::string> ("changed"));
ASSERT_EQ (account_text1, "1"); 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_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_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) TEST (rpc, wallet_password_enter)
@ -3436,7 +3443,10 @@ TEST (rpc, wallet_lock)
boost::property_tree::ptree request; boost::property_tree::ptree request;
std::string wallet; std::string wallet;
system.nodes[0]->wallets.items.begin ()->first.encode_hex (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 ("wallet", wallet);
request.put ("action", "wallet_lock"); request.put ("action", "wallet_lock");
test_response response (request, rpc, system.service); test_response response (request, rpc, system.service);
@ -3447,7 +3457,8 @@ TEST (rpc, wallet_lock)
ASSERT_EQ (200, response.status); ASSERT_EQ (200, response.status);
std::string account_text1 (response.json.get<std::string> ("locked")); std::string account_text1 (response.json.get<std::string> ("locked"));
ASSERT_EQ (account_text1, "1"); 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) TEST (rpc, wallet_locked)

View file

@ -651,10 +651,14 @@ TEST (wallet, insert_locked)
{ {
rai::system system (24000, 1); rai::system system (24000, 1);
auto wallet (system.wallet (0)); 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 (""); 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 ()); ASSERT_TRUE (wallet->insert_adhoc (rai::keypair ().prv).is_zero ());
} }
@ -663,13 +667,16 @@ TEST (wallet, version_1_upgrade)
rai::system system (24000, 1); rai::system system (24000, 1);
auto wallet (system.wallet (0)); auto wallet (system.wallet (0));
wallet->enter_initial_password (); wallet->enter_initial_password ();
ASSERT_TRUE (wallet->valid_password ());
rai::keypair key; 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); 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::raw_key password_l;
rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special)); rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
rai::raw_key kdf; rai::raw_key kdf;
@ -682,14 +689,14 @@ TEST (wallet, version_1_upgrade)
} }
wallet->enter_password ("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::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); 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::raw_key password_l;
rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special)); rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
rai::raw_key kdf; rai::raw_key kdf;
@ -701,10 +708,11 @@ TEST (wallet, version_1_upgrade)
wallet->store.version_put (transaction, 1); wallet->store.version_put (transaction, 1);
} }
wallet->enter_password ("1"); wallet->enter_password ("1");
ASSERT_TRUE (wallet->valid_password ()); rai::transaction transaction (wallet->store.environment, true);
ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::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 prv2; 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); ASSERT_EQ (key.prv, prv2);
} }
@ -791,11 +799,15 @@ TEST (wallet, insert_deterministic_locked)
{ {
rai::system system (24000, 1); rai::system system (24000, 1);
auto wallet (system.wallet (0)); 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 (""); wallet->enter_password ("");
ASSERT_FALSE (wallet->valid_password ()); rai::transaction transaction (wallet->store.environment, true);
ASSERT_TRUE (wallet->deterministic_insert ().is_zero ()); ASSERT_FALSE (wallet->store.valid_password (transaction));
ASSERT_TRUE (wallet->deterministic_insert (transaction).is_zero ());
} }
TEST (wallet, version_2_upgrade) TEST (wallet, version_2_upgrade)
@ -828,10 +840,16 @@ TEST (wallet, version_3_upgrade)
{ {
rai::system system (24000, 1); rai::system system (24000, 1);
auto wallet (system.wallet (0)); 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"); 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::keypair key;
rai::raw_key seed; rai::raw_key seed;
rai::uint256_union seed_ciphertext; rai::uint256_union seed_ciphertext;
@ -851,18 +869,16 @@ TEST (wallet, version_3_upgrade)
wallet->store.version_put (transaction, 3); wallet->store.version_put (transaction, 3);
} }
wallet->enter_password ("1"); wallet->enter_password ("1");
ASSERT_TRUE (wallet->valid_password ()); rai::transaction transaction (wallet->store.environment, false);
ASSERT_EQ (wallet->store.version_current, wallet->store.version (rai::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; 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); ASSERT_EQ (key.prv, prv);
{ rai::raw_key seed_compare;
rai::transaction transaction (wallet->store.environment, false); wallet->store.seed (seed_compare, transaction);
rai::raw_key seed_compare; ASSERT_EQ (seed, seed_compare);
wallet->store.seed (seed_compare, transaction); ASSERT_NE (seed_ciphertext, wallet->store.entry_get_raw (transaction, rai::wallet_store::seed_special).key);
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) TEST (wallet, no_work)

View file

@ -621,16 +621,16 @@ void rai::rpc_handler::account_representative_set ()
auto account (account_impl ()); auto account (account_impl ());
if (!ec) if (!ec)
{ {
if (wallet->valid_password ()) std::string representative_text (request.get<std::string> ("representative"));
rai::account representative;
if (!representative.decode_account (representative_text))
{ {
std::string representative_text (request.get<std::string> ("representative")); auto work (work_optional_impl ());
rai::account representative; if (!ec && work)
if (!representative.decode_account (representative_text))
{ {
auto work (work_optional_impl ()); rai::transaction transaction (node.store.environment, true);
if (!ec && work) if (wallet->store.valid_password (transaction))
{ {
rai::transaction transaction (node.store.environment, true);
rai::account_info info; rai::account_info info;
if (!node.store.account_get (transaction, account, 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; ec = nano::error_common::account_not_found;
} }
} }
if (!ec) else
{ {
auto response_a (response); ec = nano::error_common::wallet_locked;
wallet->change_async (account, representative, [response_a](std::shared_ptr<rai::block> 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 if (!ec)
{ {
ec = nano::error_rpc::bad_representative_number; auto response_a (response);
wallet->change_async (account, representative, [response_a](std::shared_ptr<rai::block> 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 else
{ {
ec = nano::error_common::wallet_locked; ec = nano::error_rpc::bad_representative_number;
} }
} }
// Because of change_async // Because of change_async
@ -2566,21 +2566,21 @@ void rai::rpc_handler::send ()
auto amount (amount_impl ()); auto amount (amount_impl ());
if (!ec) if (!ec)
{ {
if (wallet->valid_password ()) std::string source_text (request.get<std::string> ("source"));
rai::account source;
if (!source.decode_account (source_text))
{ {
std::string source_text (request.get<std::string> ("source")); std::string destination_text (request.get<std::string> ("destination"));
rai::account source; rai::account destination;
if (!source.decode_account (source_text)) if (!destination.decode_account (destination_text))
{ {
std::string destination_text (request.get<std::string> ("destination")); auto work (work_optional_impl ());
rai::account destination; rai::uint128_t balance (0);
if (!destination.decode_account (destination_text)) if (!ec)
{ {
auto work (work_optional_impl ()); rai::transaction transaction (node.store.environment, work != 0); // false if no "work" in request, true if work > 0
rai::uint128_t balance (0); if (wallet->store.valid_password (transaction))
if (!ec)
{ {
rai::transaction transaction (node.store.environment, work != 0); // false if no "work" in request, true if work > 0
rai::account_info info; rai::account_info info;
if (!node.store.account_get (transaction, source, 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<std::string> send_id (request.get_optional<std::string> ("id"));
if (balance >= amount.number ())
{ {
boost::optional<std::string> send_id (request.get_optional<std::string> ("id"));
auto rpc_l (shared_from_this ()); auto rpc_l (shared_from_this ());
auto response_a (response); auto response_a (response);
wallet->send_async (source, destination, amount.number (), [balance, amount, response_a](std::shared_ptr<rai::block> block_a) { wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr<rai::block> block_a) {
if (block_a != nullptr) if (block_a != nullptr)
{ {
rai::uint256_union hash (block_a->hash ()); rai::uint256_union hash (block_a->hash ());
@ -2617,33 +2624,25 @@ void rai::rpc_handler::send ()
} }
else else
{ {
if (balance >= amount.number ()) error_response (response_a, "Error generating block");
{
error_response (response_a, "Error generating block");
}
else
{
std::error_code ec (nano::error_common::insufficient_balance);
error_response (response_a, ec.message ());
}
} }
}, },
work == 0, send_id); work == 0, send_id);
} }
} else
else {
{ ec = nano::error_common::insufficient_balance;
ec = nano::error_rpc::bad_destination; }
} }
} }
else else
{ {
ec = nano::error_rpc::bad_source; ec = nano::error_rpc::bad_destination;
} }
} }
else else
{ {
ec = nano::error_common::wallet_locked; ec = nano::error_rpc::bad_source;
} }
} }
// Because of send_async // Because of send_async

View file

@ -764,23 +764,16 @@ void rai::wallet::enter_initial_password ()
store.password.value (password_l); store.password.value (password_l);
if (password_l.data.is_zero ()) 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 // Newly created wallets have a zero key
rai::transaction transaction (store.environment, true);
store.rekey (transaction, ""); store.rekey (transaction, "");
} }
enter_password (""); 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) bool rai::wallet::enter_password (std::string const & password_a)
{ {
rai::transaction transaction (store.environment, false); rai::transaction transaction (store.environment, false);

View file

@ -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 &);
wallet (bool &, rai::mdb_env &, rai::transaction &, rai::wallets &, std::string const &, std::string const &); wallet (bool &, rai::mdb_env &, rai::transaction &, rai::wallets &, std::string const &, std::string const &);
void enter_initial_password (); void enter_initial_password ();
bool valid_password ();
bool enter_password (std::string const &); bool enter_password (std::string const &);
rai::public_key insert_adhoc (rai::raw_key const &, bool = true); rai::public_key insert_adhoc (rai::raw_key const &, bool = true);
rai::public_key insert_adhoc (MDB_txn *, rai::raw_key const &, bool = true); rai::public_key insert_adhoc (MDB_txn *, rai::raw_key const &, bool = true);