Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added start of passing around a transaction inside the wallet #1083

Merged
merged 7 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions rai/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ bool rai::wallet_store::valid_password (MDB_txn * transaction_a)
return ok;
}

bool rai::wallet_store::attempt_password (MDB_txn * transaction_a, std::string const & password_a)
bool rai::wallet_store::attempt_password (rai::transaction & transaction, std::string const & password_a)
{
std::shared_ptr<rai::transaction> write_transaction;
MDB_txn * transaction_a;

if (!transaction.open_for_write)
{
write_transaction = std::make_shared<rai::transaction> (environment, nullptr, true);
transaction_a = *write_transaction;
}
else
{
transaction_a = transaction;
}


bool result = false;
{
std::lock_guard<std::recursive_mutex> lock (mutex);
Expand All @@ -148,11 +162,11 @@ bool rai::wallet_store::attempt_password (MDB_txn * transaction_a, std::string c
switch (version (transaction_a))
{
case version_1:
upgrade_v1_v2 ();
upgrade_v1_v2 (transaction_a);
case version_2:
upgrade_v2_v3 ();
upgrade_v2_v3 (transaction_a);
case version_3:
upgrade_v3_v4 ();
upgrade_v3_v4 (transaction_a);
case version_4:
break;
default:
Expand Down Expand Up @@ -643,9 +657,8 @@ void rai::wallet_store::version_put (MDB_txn * transaction_a, unsigned version_a
entry_put_raw (transaction_a, rai::wallet_store::version_special, rai::wallet_value (entry, 0));
}

void rai::wallet_store::upgrade_v1_v2 ()
void rai::wallet_store::upgrade_v1_v2 (MDB_txn * transaction)
{
rai::transaction transaction (environment, nullptr, true);
assert (version (transaction) == 1);
rai::raw_key zero_password;
rai::wallet_value value (entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
Expand Down Expand Up @@ -689,9 +702,8 @@ void rai::wallet_store::upgrade_v1_v2 ()
version_put (transaction, 2);
}

void rai::wallet_store::upgrade_v2_v3 ()
void rai::wallet_store::upgrade_v2_v3 (MDB_txn * transaction)
{
rai::transaction transaction (environment, nullptr, true);
assert (version (transaction) == 2);
rai::raw_key seed;
random_pool.GenerateBlock (seed.data.bytes.data (), seed.data.bytes.size ());
Expand All @@ -700,9 +712,8 @@ void rai::wallet_store::upgrade_v2_v3 ()
version_put (transaction, 3);
}

void rai::wallet_store::upgrade_v3_v4 ()
void rai::wallet_store::upgrade_v3_v4 (MDB_txn * transaction)
{
rai::transaction transaction (environment, nullptr, true);
assert (version (transaction) == 3);
version_put (transaction, 4);
assert (valid_password (transaction));
Expand Down Expand Up @@ -767,6 +778,7 @@ node (node_a)

void rai::wallet::enter_initial_password ()
{
rai::transaction transaction (store.environment, nullptr, true);
std::lock_guard<std::recursive_mutex> lock (store.mutex);
rai::raw_key password_l;
store.password.value (password_l);
Expand All @@ -775,10 +787,9 @@ void rai::wallet::enter_initial_password ()
if (valid_password ())
{
// Newly created wallets have a zero key
rai::transaction transaction (store.environment, nullptr, true);
store.rekey (transaction, "");
}
enter_password ("");
enter_password (transaction, "");
}
}

Expand All @@ -789,9 +800,8 @@ bool rai::wallet::valid_password ()
return result;
}

bool rai::wallet::enter_password (std::string const & password_a)
bool rai::wallet::enter_password (rai::transaction & transaction, std::string const & password_a)
{
rai::transaction transaction (store.environment, nullptr, false);
auto result (store.attempt_password (transaction, password_a));
if (!result)
{
Expand All @@ -804,6 +814,13 @@ bool rai::wallet::enter_password (std::string const & password_a)
return result;
}

bool rai::wallet::enter_password (std::string const & password_a)
{
rai::transaction transaction (store.environment, nullptr, true);
std::lock_guard<std::recursive_mutex> lock (store.mutex);
return enter_password (transaction, password_a);
}

rai::public_key rai::wallet::deterministic_insert (MDB_txn * transaction_a, bool generate_work_a)
{
rai::public_key key (0);
Expand Down
9 changes: 5 additions & 4 deletions rai/node/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class wallet_store
rai::uint256_union check (MDB_txn *);
bool rekey (MDB_txn *, std::string const &);
bool valid_password (MDB_txn *);
bool attempt_password (MDB_txn *, std::string const &);
bool attempt_password (rai::transaction &, std::string const &);
void wallet_key (rai::raw_key &, MDB_txn *);
void seed (rai::raw_key &, MDB_txn *);
void seed_set (MDB_txn *, rai::raw_key const &);
Expand Down Expand Up @@ -94,9 +94,9 @@ class wallet_store
void work_put (MDB_txn *, rai::public_key const &, uint64_t);
unsigned version (MDB_txn *);
void version_put (MDB_txn *, unsigned);
void upgrade_v1_v2 ();
void upgrade_v2_v3 ();
void upgrade_v3_v4 ();
void upgrade_v1_v2 (MDB_txn *);
void upgrade_v2_v3 (MDB_txn *);
void upgrade_v3_v4 (MDB_txn *);
rai::fan password;
rai::fan wallet_key_mem;
static unsigned const version_1 = 1;
Expand Down Expand Up @@ -135,6 +135,7 @@ class wallet : public std::enable_shared_from_this<rai::wallet>
void enter_initial_password ();
bool valid_password ();
bool enter_password (std::string const &);
bool enter_password (rai::transaction &, 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);
void insert_watch (MDB_txn *, rai::public_key const &);
Expand Down
2 changes: 1 addition & 1 deletion rai/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ wallet (wallet_a)
else
{
// try to unlock wallet
if (!this->wallet.wallet_m->enter_password (std::string (password->text ().toLocal8Bit ())))
if (!this->wallet.wallet_m->enter_password (transaction, std::string (password->text ().toLocal8Bit ())))
{
password->clear ();
lock_toggle->setText ("Lock");
Expand Down
1 change: 1 addition & 0 deletions rai/secure/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ environment (environment_a)
{
auto status (mdb_txn_begin (environment_a, parent_a, write ? 0 : MDB_RDONLY, &handle));
assert (status == 0);
open_for_write = write;
}

rai::transaction::~transaction ()
Expand Down
1 change: 1 addition & 0 deletions rai/secure/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ class transaction
operator MDB_txn * () const;
MDB_txn * handle;
rai::mdb_env & environment;
bool open_for_write;
};
}