Skip to content

Commit

Permalink
Creating blocks from wallet now uses cached work. Added tests for cre…
Browse files Browse the repository at this point in the history
…ating blocks.
  • Loading branch information
clemahieu committed Jan 17, 2015
1 parent 7b67611 commit d62d641
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
13 changes: 9 additions & 4 deletions rai/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ void rai_qt::block_creation::create_send ()
rai::private_key key;
if (!wallet.wallet_m->store.fetch (account_l, key))
{
std::lock_guard <std::mutex> lock (wallet.wallet_m->mutex);
auto balance (wallet.node.ledger.account_balance (account_l));
if (amount_l.number () <= balance)
{
Expand All @@ -871,7 +872,7 @@ void rai_qt::block_creation::create_send ()
send.hashables.balance = rai::amount (balance - amount_l.number ());
rai::sign_message (key, account_l, send.hash (), send.signature);
key.clear ();
wallet.node.work_create (send);
send.block_work_set (wallet.wallet_m->work_fetch (account_l, send.root ()));
std::string block_l;
send.serialize_json (block_l);
block->setPlainText (QString (block_l.c_str ()));
Expand Down Expand Up @@ -926,12 +927,13 @@ void rai_qt::block_creation::create_receive ()
auto error (wallet.wallet_m->store.fetch (receivable.destination, key));
if (!error)
{
std::lock_guard <std::mutex> lock (wallet.wallet_m->mutex);
rai::receive_block receive;
receive.hashables.previous = frontier.hash;
receive.hashables.source = source_l;
rai::sign_message (key, receivable.destination, receive.hash (), receive.signature);
key.clear ();
wallet.node.work_create (receive);
receive.block_work_set (wallet.wallet_m->work_fetch (receivable.destination, receive.root ()));
std::string block_l;
receive.serialize_json (block_l);
block->setPlainText (QString (block_l.c_str ()));
Expand Down Expand Up @@ -981,9 +983,10 @@ void rai_qt::block_creation::create_change ()
auto error (wallet.wallet_m->store.fetch (account_l, key));
if (!error)
{
std::lock_guard <std::mutex> lock (wallet.wallet_m->mutex);
rai::change_block change (representative_l, frontier.hash, key, account_l);
key.clear ();
wallet.node.work_create (change);
change.block_work_set (wallet.wallet_m->work_fetch (account_l, change.root ()));
std::string block_l;
change.serialize_json (block_l);
block->setPlainText (QString (block_l.c_str ()));
Expand Down Expand Up @@ -1036,12 +1039,14 @@ void rai_qt::block_creation::create_open ()
auto error (wallet.wallet_m->store.fetch (receivable.destination, key));
if (!error)
{
std::lock_guard <std::mutex> lock (wallet.wallet_m->mutex);
rai::open_block open;
open.hashables.account = receivable.destination;
open.hashables.source = source_l;
open.hashables.representative = representative_l;
rai::sign_message (key, receivable.destination, open.hash (), open.signature);
key.clear ();
wallet.node.work_create (open);
open.block_work_set (wallet.wallet_m->work_fetch (receivable.destination, open.root ()));
std::string block_l;
open.serialize_json (block_l);
block->setPlainText (QString (block_l.c_str ()));
Expand Down
74 changes: 74 additions & 0 deletions rai/qt_test/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ TEST (wallet, create_send)
int argc (0);
QApplication application (argc, nullptr);
rai_qt::wallet wallet (application, *system.nodes [0], system.wallet (0), rai::test_genesis_key.pub);
wallet.client_window->show ();
QTest::mouseClick (wallet.show_advanced, Qt::LeftButton);
QTest::mouseClick (wallet.advanced.create_block, Qt::LeftButton);
QTest::mouseClick (wallet.block_creation.send, Qt::LeftButton);
Expand All @@ -208,6 +209,79 @@ TEST (wallet, create_send)
ASSERT_EQ (rai::process_result::old, system.nodes [0]->ledger.process (send));
}

TEST (wallet, create_open_receive)
{
rai::keypair key;
rai::system system (24000, 1);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
system.wallet (0)->send (key.pub, 100);
auto latest1 (system.nodes [0]->ledger.latest (rai::test_genesis_key.pub));
system.wallet (0)->send (key.pub, 100);
auto latest2 (system.nodes [0]->ledger.latest (rai::test_genesis_key.pub));
ASSERT_NE (latest1, latest2);
system.wallet (0)->store.insert (key.prv);
int argc (0);
QApplication application (argc, nullptr);
rai_qt::wallet wallet (application, *system.nodes [0], system.wallet (0), rai::test_genesis_key.pub);
wallet.client_window->show ();
QTest::mouseClick (wallet.show_advanced, Qt::LeftButton);
QTest::mouseClick (wallet.advanced.create_block, Qt::LeftButton);
QTest::mouseClick (wallet.block_creation.open, Qt::LeftButton);
QTest::keyClicks (wallet.block_creation.source, latest1.to_string ().c_str ());
QTest::keyClicks (wallet.block_creation.representative, rai::test_genesis_key.pub.to_base58check ().c_str ());
QTest::mouseClick (wallet.block_creation.create, Qt::LeftButton);
std::string json1 (wallet.block_creation.block->toPlainText ().toStdString ());
ASSERT_FALSE (json1.empty ());
rai::open_block open;
boost::property_tree::ptree tree1;
std::stringstream istream1 (json1);
boost::property_tree::read_json (istream1, tree1);
ASSERT_FALSE (open.deserialize_json (tree1));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (open));
ASSERT_EQ (rai::process_result::old, system.nodes [0]->ledger.process (open));
wallet.block_creation.block->clear ();
wallet.block_creation.source->clear ();
QTest::mouseClick (wallet.block_creation.receive, Qt::LeftButton);
QTest::keyClicks (wallet.block_creation.source, latest2.to_string ().c_str ());
QTest::mouseClick (wallet.block_creation.create, Qt::LeftButton);
std::string json2 (wallet.block_creation.block->toPlainText ().toStdString ());
ASSERT_FALSE (json2.empty ());
rai::receive_block receive;
boost::property_tree::ptree tree2;
std::stringstream istream2 (json2);
boost::property_tree::read_json (istream2, tree2);
ASSERT_FALSE (receive.deserialize_json (tree2));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (receive));
ASSERT_EQ (rai::process_result::old, system.nodes [0]->ledger.process (receive));
}

TEST (wallet, create_change)
{
rai::keypair key;
rai::system system (24000, 1);
system.wallet (0)->store.insert (rai::test_genesis_key.prv);
int argc (0);
QApplication application (argc, nullptr);
rai_qt::wallet wallet (application, *system.nodes [0], system.wallet (0), rai::test_genesis_key.pub);
wallet.client_window->show ();
QTest::mouseClick (wallet.show_advanced, Qt::LeftButton);
QTest::mouseClick (wallet.advanced.create_block, Qt::LeftButton);
QTest::mouseClick (wallet.block_creation.change, Qt::LeftButton);
QTest::keyClicks (wallet.block_creation.account, rai::test_genesis_key.pub.to_base58check ().c_str ());
QTest::keyClicks (wallet.block_creation.representative, key.pub.to_base58check ().c_str ());
QTest::mouseClick (wallet.block_creation.create, Qt::LeftButton);
std::string json (wallet.block_creation.block->toPlainText ().toStdString ());
ASSERT_FALSE (json.empty ());
boost::property_tree::ptree tree1;
std::stringstream istream (json);
boost::property_tree::read_json (istream, tree1);
bool error (false);
rai::change_block change (error, tree1);
ASSERT_FALSE (error);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (change));
ASSERT_EQ (rai::process_result::old, system.nodes [0]->ledger.process (change));
}

TEST (history, short_text)
{
leveldb::Status init;
Expand Down

0 comments on commit d62d641

Please sign in to comment.