diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 62585794b0..97879f6fd3 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -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 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 ()); + 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 ("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); diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 24f865939c..5808de5de4 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -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 ());