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

Work generation for state blocks in block_create RPC #785

Merged
merged 1 commit into from
Apr 11, 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
41 changes: 41 additions & 0 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3249,6 +3249,47 @@ TEST (rpc, block_create_state_open)
ASSERT_FALSE (system.nodes[0]->latest (key.pub).is_zero ());
}

// Missing "work" parameter should cause work to be generated for us.
TEST (rpc, block_create_state_request_work)
{
rai::genesis genesis;

// Test work generation for state blocks both with and without previous (in the latter
// case, the account will be used for work generation)
std::vector<std::string> previous_test_input{ genesis.hash ().to_string (), std::string ("0") };
for (auto previous : previous_test_input)
{
rai::system system (24000, 1);
rai::keypair key;
rai::genesis genesis;
system.nodes[0]->ledger.state_block_parse_canary = genesis.hash ();
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
boost::property_tree::ptree request;
request.put ("action", "block_create");
request.put ("type", "state");
request.put ("wallet", system.nodes[0]->wallets.items.begin ()->first.to_string ());
request.put ("account", rai::test_genesis_key.pub.to_account ());
request.put ("representative", rai::test_genesis_key.pub.to_account ());
request.put ("balance", (rai::genesis_amount - rai::Gxrb_ratio).convert_to<std::string> ());
request.put ("link", key.pub.to_account ());
request.put ("previous", previous);
rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true));
rpc.start ();
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
boost::property_tree::ptree block_l;
std::stringstream block_stream (response.json.get<std::string> ("block"));
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
ASSERT_NE (nullptr, block);
ASSERT_FALSE (rai::work_validate (*block));
}
}

TEST (rpc, wallet_lock)
{
rai::system system (24000, 1);
Expand Down
5 changes: 5 additions & 0 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,11 @@ void rai::rpc_handler::block_create ()
{
if (!account.is_zero () && previous_text.is_initialized () && !representative.is_zero () && !balance.is_zero () && link_text.is_initialized ())
{
if (work == 0)
{
work = node.generate_work (previous.is_zero () ? pub : previous);
}

rai::state_block state (account, previous, representative, balance, link, prv, pub, work);
boost::property_tree::ptree response_l;
response_l.put ("hash", state.hash ().to_string ());
Expand Down