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);
std::string account_text1 (response.json.get<std::string> ("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<std::string> ("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)

View file

@ -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::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, 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 (transaction, key.pub, prv));
ASSERT_EQ (key.prv, prv);
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)

View file

@ -620,8 +620,6 @@ void rai::rpc_handler::account_representative_set ()
auto wallet (wallet_impl ());
auto account (account_impl ());
if (!ec)
{
if (wallet->valid_password ())
{
std::string representative_text (request.get<std::string> ("representative"));
rai::account representative;
@ -631,6 +629,8 @@ void rai::rpc_handler::account_representative_set ()
if (!ec && work)
{
rai::transaction transaction (node.store.environment, true);
if (wallet->store.valid_password (transaction))
{
rai::account_info info;
if (!node.store.account_get (transaction, account, info))
{
@ -648,6 +648,11 @@ void rai::rpc_handler::account_representative_set ()
ec = nano::error_common::account_not_found;
}
}
else
{
ec = nano::error_common::wallet_locked;
}
}
if (!ec)
{
auto response_a (response);
@ -669,11 +674,6 @@ void rai::rpc_handler::account_representative_set ()
ec = nano::error_rpc::bad_representative_number;
}
}
else
{
ec = nano::error_common::wallet_locked;
}
}
// Because of change_async
if (ec)
{
@ -2565,8 +2565,6 @@ void rai::rpc_handler::send ()
auto wallet (wallet_impl ());
auto amount (amount_impl ());
if (!ec)
{
if (wallet->valid_password ())
{
std::string source_text (request.get<std::string> ("source"));
rai::account source;
@ -2581,6 +2579,8 @@ void rai::rpc_handler::send ()
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::account_info info;
if (!node.store.account_get (transaction, source, info))
{
@ -2602,12 +2602,19 @@ void rai::rpc_handler::send ()
}
}
}
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 ())
{
auto rpc_l (shared_from_this ());
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)
{
rai::uint256_union hash (block_a->hash ());
@ -2616,20 +2623,17 @@ void rai::rpc_handler::send ()
response_a (response_l);
}
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 ());
}
}
},
work == 0, send_id);
}
else
{
ec = nano::error_common::insufficient_balance;
}
}
}
else
{
@ -2641,11 +2645,6 @@ void rai::rpc_handler::send ()
ec = nano::error_rpc::bad_source;
}
}
else
{
ec = nano::error_common::wallet_locked;
}
}
// Because of send_async
if (ec)
{

View file

@ -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);

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 &, 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);