From 36bf07eccc57b3556320790a9b99112a56e50d0e Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 16:59:07 +0300 Subject: [PATCH 01/38] Rewriting RPC with error codes --- rai/core_test/rpc.cpp | 2 +- rai/lib/errors.cpp | 122 ++- rai/lib/errors.hpp | 67 +- rai/node/rpc.cpp | 1821 +++++++++++++++++++---------------------- rai/node/rpc.hpp | 3 + 5 files changed, 1041 insertions(+), 974 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 8f550e1a..7ebe12fb 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -3470,7 +3470,7 @@ TEST (rpc, wallet_create_fail) { system.poll (); } - ASSERT_EQ ("Failed to create wallet. Increase lmdb_max_dbs in node config.", response.json.get ("error")); + ASSERT_EQ ("Failed to create wallet. Increase lmdb_max_dbs in node config", response.json.get ("error")); } TEST (rpc, wallet_ledger) diff --git a/rai/lib/errors.cpp b/rai/lib/errors.cpp index 0a14f040..d686e3a3 100644 --- a/rai/lib/errors.cpp +++ b/rai/lib/errors.cpp @@ -10,22 +10,42 @@ std::string nano::error_common_messages::message (int ev) const return "Account already exists"; case nano::error_common::account_not_found: return "Account not found"; + case nano::error_common::account_not_found_wallet: + return "Account not found in wallet"; case nano::error_common::bad_account_number: return "Bad account number"; + case nano::error_common::bad_private_key: + return "Bad private key"; case nano::error_common::bad_public_key: return "Bad public key"; case nano::error_common::bad_seed: return "Bad seed"; + case nano::error_common::bad_threshold: + return "Bad threshold number"; case nano::error_common::bad_wallet_number: return "Bad wallet number"; case nano::error_common::bad_work_format: return "Bad work"; - case nano::error_common::invalid_work: - return "Invalid work"; + case nano::error_common::insufficient_balance: + return "Insufficient balance"; + case nano::error_common::invalid_amount: + return "Invalid amount number"; + case nano::error_common::invalid_amount_big: + return "Amount too big"; + case nano::error_common::invalid_count: + return "Invalid count"; + case nano::error_common::invalid_ip_address: + return "Invalid IP address"; + case nano::error_common::invalid_port: + return "Invalid port"; case nano::error_common::invalid_index: return "Invalid index"; + case nano::error_common::invalid_work: + return "Invalid work"; case nano::error_common::numeric_conversion: return "Numeric conversion error"; + case nano::error_common::wallet_lmdb_max_dbs: + return "Failed to create wallet. Increase lmdb_max_dbs in node config"; case nano::error_common::wallet_locked: return "Wallet is locked"; case nano::error_common::wallet_not_found: @@ -43,10 +63,108 @@ std::string nano::error_blocks_messages::message (int ev) const return "Unknown error"; case nano::error_blocks::bad_hash_number: return "Bad hash number"; + case nano::error_blocks::invalid_block: + return "Block is invalid"; case nano::error_blocks::invalid_block_hash: return "Invalid block hash"; + case nano::error_blocks::invalid_type: + return "Invalid block type"; case nano::error_blocks::not_found: return "Block not found"; + case nano::error_blocks::work_low: + return "Block work is less than threshold"; + } + + return "Invalid error code"; +} + +std::string nano::error_rpc_messages::message (int ev) const +{ + switch (static_cast (ev)) + { + case nano::error_rpc::generic: + return "Unknown error"; + case nano::error_rpc::bad_destination: + return "Bad destination account"; + case nano::error_rpc::bad_key: + return "Bad key"; + case nano::error_rpc::bad_link: + return "Bad link number"; + case nano::error_rpc::bad_previous: + return "Bad previous"; + case nano::error_rpc::bad_representative_number: + return "Bad representative number"; + case nano::error_rpc::bad_source: + return "Bad source"; + case nano::error_rpc::bad_timeout: + return "Bad timeout number"; + case nano::error_rpc::block_create_balance_mismatch: + return "Balance mismatch for previous block"; + case nano::error_rpc::block_create_key_required: + return "Private key or local wallet and account required"; + case nano::error_rpc::block_create_public_key_mismatch: + return "Incorrect key for given account"; + case nano::error_rpc::block_create_requirements_state: + return "Previous, representative, final balance and link (source or destination) are required"; + case nano::error_rpc::block_create_requirements_open: + return "Representative account and source hash required"; + case nano::error_rpc::block_create_requirements_receive: + return "Previous hash and source hash required"; + case nano::error_rpc::block_create_requirements_change: + return "Representative account and previous hash required"; + case nano::error_rpc::block_create_requirements_send: + return "Destination account, previous hash, current balance and amount required"; + case nano::error_rpc::invalid_balance: + return "Invalid balance number"; + case nano::error_rpc::invalid_destinations: + return "Invalid destinations number"; + case nano::error_rpc::invalid_offset: + return "Invalid offset"; + case nano::error_rpc::invalid_missing_type: + return "Invalid or missing type argument"; + case nano::error_rpc::invalid_sources: + return "Invalid sources number"; + case nano::error_rpc::payment_account_balance: + return "Account has non-zero balance"; + case nano::error_rpc::payment_unable_create_account: + return "Unable to create transaction account"; + case nano::error_rpc::rpc_control_disabled: + return "RPC control is disabled"; + case nano::error_rpc::source_not_found: + return "Source not found"; + } + + return "Invalid error code"; +} + +std::string nano::error_process_messages::message (int ev) const +{ + switch (static_cast (ev)) + { + case nano::error_process::generic: + return "Unknown error"; + case nano::error_process::bad_signature: + return "Bad signature"; + case nano::error_process::old: + return "Old block"; + case nano::error_process::negative_spend: + return "Negative spend"; + case nano::error_process::fork: + return "Fork"; + case nano::error_process::unreceivable: + return "Unreceivable"; + case nano::error_process::gap_previous: + return "Gap previous block"; + case nano::error_process::gap_source: + return "Gap source block"; + case nano::error_process::opened_burn_account: + return "Burning account"; + case nano::error_process::balance_mismatch: + return "Balance and amount delta do not match"; + case nano::error_process::block_position: + return "This block cannot follow the previous block"; + case nano::error_process::other: + "Error processing block"; } return "Invalid error code"; diff --git a/rai/lib/errors.hpp b/rai/lib/errors.hpp index 5878fac0..c764f679 100644 --- a/rai/lib/errors.hpp +++ b/rai/lib/errors.hpp @@ -29,15 +29,25 @@ enum class error_common { generic = 1, account_not_found, + account_not_found_wallet, account_exists, bad_account_number, + bad_private_key, bad_public_key, bad_seed, + bad_threshold, bad_wallet_number, bad_work_format, - invalid_work, + invalid_amount, + invalid_amount_big, + invalid_count, invalid_index, + invalid_ip_address, + invalid_port, + invalid_work, + insufficient_balance, numeric_conversion, + wallet_lmdb_max_dbs, wallet_locked, wallet_not_found }; @@ -47,8 +57,59 @@ enum class error_blocks { generic = 1, bad_hash_number, + invalid_block, invalid_block_hash, - not_found + invalid_type, + not_found, + work_low +}; + +/** RPC related errors */ +enum class error_rpc +{ + generic = 1, + bad_destination, + bad_key, + bad_link, + bad_previous, + bad_representative_number, + bad_source, + bad_timeout, + block_create_balance_mismatch, + block_create_key_required, + block_create_public_key_mismatch, + block_create_requirements_state, + block_create_requirements_open, + block_create_requirements_receive, + block_create_requirements_change, + block_create_requirements_send, + invalid_balance, + invalid_destinations, + invalid_offset, + invalid_missing_type, + invalid_sources, + payment_account_balance, + payment_unable_create_account, + rpc_control_disabled, + source_not_found +}; + + +/** process_result related errors */ +enum class error_process +{ + generic = 1, + bad_signature, // Signature was bad, forged or transmission error + old, // Already seen and was valid + negative_spend, // Malicious attempt to spend a negative amount + fork, // Malicious fork based on previous + unreceivable, // Source block doesn't exist or has already been received + gap_previous, // Block marked as previous is unknown + gap_source, // Block marked as source is unknown + opened_burn_account, // The impossible happened, someone found the private key associated with the public key '0'. + balance_mismatch, // Balance and amount delta don't match + block_position, // This block cannot follow the previous block + other }; } @@ -95,3 +156,5 @@ enum class error_blocks REGISTER_ERROR_CODES (nano, error_common); REGISTER_ERROR_CODES (nano, error_blocks); +REGISTER_ERROR_CODES (nano, error_rpc); +REGISTER_ERROR_CODES (nano, error_process); diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index a3306e4f..cb203e57 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -11,6 +11,8 @@ #include #endif +#include + rai::rpc_secure_config::rpc_secure_config () : enable (false), verbose_logging (false) @@ -202,6 +204,20 @@ void rai::error_response (std::function ("account")); rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { auto balance (node.balance_pending (account)); - boost::property_tree::ptree response_l; response_l.put ("balance", balance.first.convert_to ()); response_l.put ("pending", balance.second.convert_to ()); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_block_count () { std::string account_text (request.get ("account")); rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, false); rai::account_info info; if (!node.store.account_get (transaction, account, info)) { - boost::property_tree::ptree response_l; response_l.put ("block_count", std::to_string (info.block_count)); - response (response_l); } else { - error_response (response, "Account not found"); + ec = nano::error_common::account_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_create () @@ -277,8 +289,7 @@ void rai::rpc_handler::account_create () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -287,54 +298,50 @@ void rai::rpc_handler::account_create () rai::account new_key (existing->second->deterministic_insert (generate_work)); if (!new_key.is_zero ()) { - boost::property_tree::ptree response_l; response_l.put ("account", new_key.to_account ()); - response (response_l); } else { - error_response (response, "Wallet is locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::account_get () { std::string key_text (request.get ("key")); rai::uint256_union pub; - auto error (pub.decode_hex (key_text)); - if (!error) + if (!pub.decode_hex (key_text)) { - boost::property_tree::ptree response_l; response_l.put ("account", pub.to_account ()); - response (response_l); } else { - error_response (response, "Bad public key"); + ec = nano::error_common::bad_public_key; } + response_errors (); } void rai::rpc_handler::account_info () { std::string account_text (request.get ("account")); rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { const bool representative = request.get ("representative", false); const bool weight = request.get ("weight", false); @@ -343,7 +350,6 @@ void rai::rpc_handler::account_info () rai::account_info info; if (!node.store.account_get (transaction, account, info)) { - boost::property_tree::ptree response_l; response_l.put ("frontier", info.head.to_string ()); response_l.put ("open_block", info.open_block.to_string ()); response_l.put ("representative_block", info.rep_block.to_string ()); @@ -368,47 +374,43 @@ void rai::rpc_handler::account_info () auto account_pending (node.ledger.account_pending (transaction, account)); response_l.put ("pending", account_pending.convert_to ()); } - response (response_l); } else { - error_response (response, "Account not found"); + ec = nano::error_common::account_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_key () { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { - boost::property_tree::ptree response_l; response_l.put ("key", account.to_string ()); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_list () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; boost::property_tree::ptree accounts; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (existing->second->store.begin (transaction)), j (existing->second->store.end ()); i != j; ++i) @@ -418,17 +420,17 @@ void rai::rpc_handler::account_list () accounts.push_back (std::make_pair ("", entry)); } response_l.add_child ("accounts", accounts); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::account_move () @@ -439,16 +441,14 @@ void rai::rpc_handler::account_move () std::string source_text (request.get ("source")); auto accounts_text (request.get_child ("accounts")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { auto wallet (existing->second); rai::uint256_union source; - auto error (source.decode_hex (source_text)); - if (!error) + if (!source.decode_hex (source_text)) { auto existing (node.wallets.items.find (source)); if (existing != node.wallets.items.end ()) @@ -463,34 +463,33 @@ void rai::rpc_handler::account_move () } rai::transaction transaction (node.store.environment, nullptr, true); auto error (wallet->store.move (transaction, source->store, accounts)); - boost::property_tree::ptree response_l; response_l.put ("moved", error ? "0" : "1"); - response (response_l); } else { - error_response (response, "Source not found"); + ec = nano::error_rpc::source_not_found; } } else { - error_response (response, "Bad source number"); + ec = nano::error_rpc::bad_source; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::account_remove () @@ -500,8 +499,7 @@ void rai::rpc_handler::account_remove () std::string wallet_text (request.get ("wallet")); std::string account_text (request.get ("account")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -511,75 +509,70 @@ void rai::rpc_handler::account_remove () if (existing->second->store.valid_password (transaction)) { rai::account account_id; - auto error (account_id.decode_account (account_text)); - if (!error) + if (!account_id.decode_account (account_text)) { auto account (wallet->store.find (transaction, account_id)); if (account != wallet->store.end ()) { wallet->store.erase (transaction, account_id); - boost::property_tree::ptree response_l; response_l.put ("removed", "1"); - response (response_l); } else { - error_response (response, "Account not found in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "Wallet locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::account_representative () { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, false); rai::account_info info; - auto error (node.store.account_get (transaction, account, info)); - if (!error) + if (!node.store.account_get (transaction, account, info)) { auto block (node.store.block_get (transaction, info.rep_block)); assert (block != nullptr); - boost::property_tree::ptree response_l; response_l.put ("representative", block->representative ().to_account ()); - response (response_l); } else { - error_response (response, "Account not found"); + ec = nano::error_common::account_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_representative_set () @@ -588,8 +581,7 @@ void rai::rpc_handler::account_representative_set () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -597,13 +589,11 @@ void rai::rpc_handler::account_representative_set () auto wallet (existing->second); std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { std::string representative_text (request.get ("representative")); rai::account representative; - auto error (representative.decode_account (representative_text)); - if (!error) + if (!representative.decode_account (representative_text)) { uint64_t work (0); boost::optional work_text (request.get_optional ("work")); @@ -612,10 +602,10 @@ void rai::rpc_handler::account_representative_set () auto work_error (rai::from_string_hex (work_text.get (), work)); if (work_error) { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } - if (work) + if (!ec && work) { rai::transaction transaction (node.store.environment, nullptr, true); rai::account_info info; @@ -627,38 +617,54 @@ void rai::rpc_handler::account_representative_set () } else { - error_response (response, "Invalid work"); + ec = nano::error_common::invalid_work; } } else { - error_response (response, "Account not found"); + ec = nano::error_common::account_not_found; } } - auto response_a (response); - wallet->change_async (account, representative, [response_a](std::shared_ptr block) { - rai::block_hash hash (0); - if (block != nullptr) - { - hash = block->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - }, - work == 0); + if (!ec) + { + auto response_a (response); + wallet->change_async (account, representative, [response_a](std::shared_ptr block) { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + }, + work == 0); + } + else + { + response_errors (); + } + } + else + { + ec = nano::error_rpc::bad_representative_number; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; + } + // Because of change_async + if (ec) + { + response_errors (); } } @@ -666,44 +672,43 @@ void rai::rpc_handler::account_weight () { std::string account_text (request.get ("account")); rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { auto balance (node.weight (account)); - boost::property_tree::ptree response_l; response_l.put ("weight", balance.convert_to ()); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::accounts_balances () { - boost::property_tree::ptree response_l; boost::property_tree::ptree balances; for (auto & accounts : request.get_child ("accounts")) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!ec) { - boost::property_tree::ptree entry; - auto balance (node.balance_pending (account)); - entry.put ("balance", balance.first.convert_to ()); - entry.put ("pending", balance.second.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } - else - { - error_response (response, "Bad account number"); + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) + { + boost::property_tree::ptree entry; + auto balance (node.balance_pending (account)); + entry.put ("balance", balance.first.convert_to ()); + entry.put ("pending", balance.second.convert_to ()); + balances.push_back (std::make_pair (account.to_account (), entry)); + } + else + { + ec = nano::error_common::bad_account_number; + } } } response_l.add_child ("balances", balances); - response (response_l); + response_errors (); } void rai::rpc_handler::accounts_create () @@ -712,19 +717,16 @@ void rai::rpc_handler::accounts_create () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { uint64_t count; std::string count_text (request.get ("count")); - auto count_error (decode_unsigned (count_text, count)); - if (!count_error && count != 0) + if (!decode_unsigned (count_text, count) && count != 0) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { const bool generate_work = request.get ("work", false); - boost::property_tree::ptree response_l; boost::property_tree::ptree accounts; for (auto i (0); accounts.size () < count; ++i) { @@ -737,54 +739,55 @@ void rai::rpc_handler::accounts_create () } } response_l.add_child ("accounts", accounts); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::accounts_frontiers () { - boost::property_tree::ptree response_l; boost::property_tree::ptree frontiers; rai::transaction transaction (node.store.environment, nullptr, false); for (auto & accounts : request.get_child ("accounts")) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!ec) { - auto latest (node.ledger.latest (transaction, account)); - if (!latest.is_zero ()) + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) { - frontiers.put (account.to_account (), latest.to_string ()); + auto latest (node.ledger.latest (transaction, account)); + if (!latest.is_zero ()) + { + frontiers.put (account.to_account (), latest.to_string ()); + } + } + else + { + ec = nano::error_common::bad_account_number; } - } - else - { - error_response (response, "Bad account number"); } } response_l.add_child ("frontiers", frontiers); - response (response_l); + response_errors (); } void rai::rpc_handler::accounts_pending () @@ -794,70 +797,70 @@ void rai::rpc_handler::accounts_pending () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } boost::optional threshold_text (request.get_optional ("threshold")); - if (threshold_text.is_initialized ()) + if (!ec && threshold_text.is_initialized ()) { - auto error_threshold (threshold.decode_dec (threshold_text.get ())); - if (error_threshold) + if (threshold.decode_dec (threshold_text.get ())) { - error_response (response, "Bad threshold number"); + ec = nano::error_common::bad_threshold; } } const bool source = request.get ("source", false); - boost::property_tree::ptree response_l; boost::property_tree::ptree pending; rai::transaction transaction (node.store.environment, nullptr, false); for (auto & accounts : request.get_child ("accounts")) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + if (!ec) { - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) { - rai::pending_key key (i->first); - if (threshold.is_zero () && !source) + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (i->second); - if (info.amount.number () >= threshold.number ()) + rai::pending_key key (i->first); + if (threshold.is_zero () && !source) { - if (source) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (i->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - pending_tree.put ("source", info.source.to_account ()); - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + if (source) + { + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + pending_tree.put ("source", info.source.to_account ()); + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + } } } } + pending.add_child (account.to_account (), peers_l); + } + else + { + ec = nano::error_common::bad_account_number; } - pending.add_child (account.to_account (), peers_l); - } - else - { - error_response (response, "Bad account number"); } } response_l.add_child ("blocks", pending); - response (response_l); + response_errors (); } void rai::rpc_handler::available_supply () @@ -867,37 +870,34 @@ void rai::rpc_handler::available_supply () auto faucet_balance (node.balance (rai::account ("8E319CE6F3025E5B2DF66DA7AB1467FE48F1679C13DD43BFDB29FA2E9FC40D3B"))); // Faucet account auto burned_balance ((node.balance_pending (rai::account (0))).second); // Burning 0 account auto available (rai::genesis_amount - genesis_balance - landing_balance - faucet_balance - burned_balance); - boost::property_tree::ptree response_l; response_l.put ("available", available.convert_to ()); - response (response_l); + response_errors (); } void rai::rpc_handler::block () { std::string hash_text (request.get ("hash")); rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { rai::transaction transaction (node.store.environment, nullptr, false); auto block (node.store.block_get (transaction, hash)); if (block != nullptr) { - boost::property_tree::ptree response_l; std::string contents; block->serialize_json (contents); response_l.put ("contents", contents); - response (response_l); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Bad hash number"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::block_confirm () @@ -911,53 +911,53 @@ void rai::rpc_handler::block_confirm () if (block_l != nullptr) { node.block_confirm (std::move (block_l)); - boost::property_tree::ptree response_l; response_l.put ("started", "1"); - response (response_l); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Invalid block hash"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::blocks () { std::vector hashes; - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); for (boost::property_tree::ptree::value_type & hashes : request.get_child ("hashes")) { - std::string hash_text = hashes.second.data (); - rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!ec) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + std::string hash_text = hashes.second.data (); + rai::uint256_union hash; + if (!hash.decode_hex (hash_text)) { - std::string contents; - block->serialize_json (contents); - blocks.put (hash_text, contents); + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) + { + std::string contents; + block->serialize_json (contents); + blocks.put (hash_text, contents); + } + else + { + ec = nano::error_blocks::not_found; + } } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::bad_hash_number; } } - else - { - error_response (response, "Bad hash number"); - } } response_l.add_child ("blocks", blocks); - response (response_l); + response_errors (); } void rai::rpc_handler::blocks_info () @@ -966,70 +966,71 @@ void rai::rpc_handler::blocks_info () const bool source = request.get ("source", false); const bool balance = request.get ("balance", false); std::vector hashes; - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); for (boost::property_tree::ptree::value_type & hashes : request.get_child ("hashes")) { - std::string hash_text = hashes.second.data (); - rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!ec) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + std::string hash_text = hashes.second.data (); + rai::uint256_union hash; + if (!hash.decode_hex (hash_text)) { - boost::property_tree::ptree entry; - auto account (node.ledger.account (transaction, hash)); - entry.put ("block_account", account.to_account ()); - auto amount (node.ledger.amount (transaction, hash)); - entry.put ("amount", amount.convert_to ()); - std::string contents; - block->serialize_json (contents); - entry.put ("contents", contents); - if (pending) + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) { - bool exists (false); - auto destination (node.ledger.block_destination (transaction, *block)); - if (!destination.is_zero ()) + boost::property_tree::ptree entry; + auto account (node.ledger.account (transaction, hash)); + entry.put ("block_account", account.to_account ()); + auto amount (node.ledger.amount (transaction, hash)); + entry.put ("amount", amount.convert_to ()); + std::string contents; + block->serialize_json (contents); + entry.put ("contents", contents); + if (pending) { - exists = node.store.pending_exists (transaction, rai::pending_key (destination, hash)); + bool exists (false); + auto destination (node.ledger.block_destination (transaction, *block)); + if (!destination.is_zero ()) + { + exists = node.store.pending_exists (transaction, rai::pending_key (destination, hash)); + } + entry.put ("pending", exists ? "1" : "0"); } - entry.put ("pending", exists ? "1" : "0"); + if (source) + { + rai::block_hash source_hash (node.ledger.block_source (transaction, *block)); + std::unique_ptr block_a (node.store.block_get (transaction, source_hash)); + if (block_a != nullptr) + { + auto source_account (node.ledger.account (transaction, source_hash)); + entry.put ("source_account", source_account.to_account ()); + } + else + { + entry.put ("source_account", "0"); + } + } + if (balance) + { + auto balance (node.ledger.balance (transaction, hash)); + entry.put ("balance", balance.convert_to ()); + } + blocks.push_back (std::make_pair (hash_text, entry)); } - if (source) + else { - rai::block_hash source_hash (node.ledger.block_source (transaction, *block)); - std::unique_ptr block_a (node.store.block_get (transaction, source_hash)); - if (block_a != nullptr) - { - auto source_account (node.ledger.account (transaction, source_hash)); - entry.put ("source_account", source_account.to_account ()); - } - else - { - entry.put ("source_account", "0"); - } + ec = nano::error_blocks::not_found; } - if (balance) - { - auto balance (node.ledger.balance (transaction, hash)); - entry.put ("balance", balance.convert_to ()); - } - blocks.push_back (std::make_pair (hash_text, entry)); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::bad_hash_number; } } - else - { - error_response (response, "Bad hash number"); - } } response_l.add_child ("blocks", blocks); - response (response_l); + response_errors (); } void rai::rpc_handler::block_account () @@ -1041,42 +1042,39 @@ void rai::rpc_handler::block_account () rai::transaction transaction (node.store.environment, nullptr, false); if (node.store.block_exists (transaction, hash)) { - boost::property_tree::ptree response_l; auto account (node.ledger.account (transaction, hash)); response_l.put ("account", account.to_account ()); - response (response_l); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Invalid block hash"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::block_count () { rai::transaction transaction (node.store.environment, nullptr, false); - boost::property_tree::ptree response_l; response_l.put ("count", std::to_string (node.store.block_count (transaction).sum ())); response_l.put ("unchecked", std::to_string (node.store.unchecked_count (transaction))); - response (response_l); + response_errors (); } void rai::rpc_handler::block_count_type () { rai::transaction transaction (node.store.environment, nullptr, false); rai::block_counts count (node.store.block_count (transaction)); - boost::property_tree::ptree response_l; response_l.put ("send", std::to_string (count.send)); response_l.put ("receive", std::to_string (count.receive)); response_l.put ("open", std::to_string (count.open)); response_l.put ("change", std::to_string (count.change)); response_l.put ("state", std::to_string (count.state)); - response (response_l); + response_errors (); } void rai::rpc_handler::block_create () @@ -1088,77 +1086,71 @@ void rai::rpc_handler::block_create () boost::optional wallet_text (request.get_optional ("wallet")); if (wallet_text.is_initialized ()) { - auto error (wallet.decode_hex (wallet_text.get ())); - if (error) + if (wallet.decode_hex (wallet_text.get ())) { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } rai::uint256_union account (0); boost::optional account_text (request.get_optional ("account")); - if (account_text.is_initialized ()) + if (!ec && account_text.is_initialized ()) { - auto error_account (account.decode_account (account_text.get ())); - if (error_account) + if (account.decode_account (account_text.get ())) { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } rai::uint256_union representative (0); boost::optional representative_text (request.get_optional ("representative")); - if (representative_text.is_initialized ()) + if (!ec && representative_text.is_initialized ()) { - auto error_representative (representative.decode_account (representative_text.get ())); - if (error_representative) + if (representative.decode_account (representative_text.get ())) { - error_response (response, "Bad representative account"); + ec = nano::error_rpc::bad_representative_number; } } rai::uint256_union destination (0); boost::optional destination_text (request.get_optional ("destination")); - if (destination_text.is_initialized ()) + if (!ec && destination_text.is_initialized ()) { - auto error_destination (destination.decode_account (destination_text.get ())); - if (error_destination) + if (destination.decode_account (destination_text.get ())) { - error_response (response, "Bad destination account"); + ec = nano::error_rpc::bad_destination; } } rai::block_hash source (0); boost::optional source_text (request.get_optional ("source")); - if (source_text.is_initialized ()) + if (!ec && source_text.is_initialized ()) { - auto error_source (source.decode_hex (source_text.get ())); - if (error_source) + if (source.decode_hex (source_text.get ())) { - error_response (response, "Invalid source hash"); + ec = nano::error_rpc::bad_source; } } rai::uint128_union amount (0); boost::optional amount_text (request.get_optional ("amount")); - if (amount_text.is_initialized ()) + if (!ec && amount_text.is_initialized ()) { - auto error_amount (amount.decode_dec (amount_text.get ())); - if (error_amount) + if (amount.decode_dec (amount_text.get ())) { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } } uint64_t work (0); boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (!ec && work_text.is_initialized ()) { auto work_error (rai::from_string_hex (work_text.get (), work)); if (work_error) { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } rai::raw_key prv; prv.data.clear (); rai::uint256_union previous (0); rai::uint128_union balance (0); - if (wallet != 0 && account != 0) + if (!ec && wallet != 0 && account != 0) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -1176,57 +1168,52 @@ void rai::rpc_handler::block_create () } else { - error_response (response, "Account not found in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Wallet is locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } boost::optional key_text (request.get_optional ("key")); - if (key_text.is_initialized ()) + if (!ec && key_text.is_initialized ()) { - auto error_key (prv.data.decode_hex (key_text.get ())); - if (error_key) + if (prv.data.decode_hex (key_text.get ())) { - error_response (response, "Bad private key"); + ec = nano::error_common::bad_private_key; } } boost::optional previous_text (request.get_optional ("previous")); - if (previous_text.is_initialized ()) + if (!ec && previous_text.is_initialized ()) { - auto error_previous (previous.decode_hex (previous_text.get ())); - if (error_previous) + if (previous.decode_hex (previous_text.get ())) { - error_response (response, "Invalid previous hash"); + ec = nano::error_rpc::bad_previous; } } boost::optional balance_text (request.get_optional ("balance")); - if (balance_text.is_initialized ()) + if (!ec && balance_text.is_initialized ()) { - auto error_balance (balance.decode_dec (balance_text.get ())); - if (error_balance) + if (balance.decode_dec (balance_text.get ())) { - error_response (response, "Bad balance number"); + ec = nano::error_rpc::invalid_balance; } } rai::uint256_union link (0); boost::optional link_text (request.get_optional ("link")); - if (link_text.is_initialized ()) + if (!ec && link_text.is_initialized ()) { - auto error_link (link.decode_account (link_text.get ())); - if (error_link) + if (link.decode_account (link_text.get ())) { - auto error_link (link.decode_hex (link_text.get ())); - if (error_link) + if (link.decode_hex (link_text.get ())) { - error_response (response, "Bad link number"); + ec = nano::error_rpc::bad_link; } } } @@ -1252,15 +1239,15 @@ void rai::rpc_handler::block_create () rai::transaction transaction (node.store.environment, nullptr, false); if (node.store.block_exists (transaction, previous) && node.store.block_balance (transaction, previous) != balance.number ()) { - error_response (response, "Balance mismatch for previous block"); + ec = nano::error_rpc::block_create_balance_mismatch; } } // Check for incorrect account key - if (account_text.is_initialized ()) + if (!ec && account_text.is_initialized ()) { if (account != pub) { - error_response (response, "Incorrect key for given account"); + ec = nano::error_rpc::block_create_public_key_mismatch; } } if (type == "state") @@ -1272,16 +1259,14 @@ void rai::rpc_handler::block_create () work = node.work_generate_blocking (previous.is_zero () ? pub : previous); } rai::state_block state (pub, previous, representative, balance, link, prv, pub, work); - boost::property_tree::ptree response_l; response_l.put ("hash", state.hash ().to_string ()); std::string contents; state.serialize_json (contents); response_l.put ("block", contents); - response (response_l); } else { - error_response (response, "Previous, representative, final balance and link (source or destination) are required"); + ec = nano::error_rpc::block_create_requirements_state; } } else if (type == "open") @@ -1293,16 +1278,14 @@ void rai::rpc_handler::block_create () work = node.work_generate_blocking (pub); } rai::open_block open (source, representative, pub, prv, pub, work); - boost::property_tree::ptree response_l; response_l.put ("hash", open.hash ().to_string ()); std::string contents; open.serialize_json (contents); response_l.put ("block", contents); - response (response_l); } else { - error_response (response, "Representative account and source hash required"); + ec = nano::error_rpc::block_create_requirements_open; } } else if (type == "receive") @@ -1314,16 +1297,14 @@ void rai::rpc_handler::block_create () work = node.work_generate_blocking (previous); } rai::receive_block receive (previous, source, prv, pub, work); - boost::property_tree::ptree response_l; response_l.put ("hash", receive.hash ().to_string ()); std::string contents; receive.serialize_json (contents); response_l.put ("block", contents); - response (response_l); } else { - error_response (response, "Previous hash and source hash required"); + ec = nano::error_rpc::block_create_requirements_receive; } } else if (type == "change") @@ -1335,16 +1316,14 @@ void rai::rpc_handler::block_create () work = node.work_generate_blocking (previous); } rai::change_block change (previous, representative, prv, pub, work); - boost::property_tree::ptree response_l; response_l.put ("hash", change.hash ().to_string ()); std::string contents; change.serialize_json (contents); response_l.put ("block", contents); - response (response_l); } else { - error_response (response, "Representative account and previous hash required"); + ec = nano::error_rpc::block_create_requirements_change; } } else if (type == "send") @@ -1358,37 +1337,36 @@ void rai::rpc_handler::block_create () work = node.work_generate_blocking (previous); } rai::send_block send (previous, destination, balance.number () - amount.number (), prv, pub, work); - boost::property_tree::ptree response_l; response_l.put ("hash", send.hash ().to_string ()); std::string contents; send.serialize_json (contents); response_l.put ("block", contents); - response (response_l); } else { - error_response (response, "Insufficient balance"); + ec = nano::error_common::insufficient_balance; } } else { - error_response (response, "Destination account, previous hash, current balance and amount required"); + ec = nano::error_rpc::block_create_requirements_send; } } else { - error_response (response, "Invalid block type"); + ec = nano::error_blocks::invalid_type; } } else { - error_response (response, "Private key or local wallet and account required"); + ec = nano::error_rpc::block_create_key_required; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::block_hash () @@ -1402,14 +1380,13 @@ void rai::rpc_handler::block_hash () auto block (rai::deserialize_block_json (block_l)); if (block != nullptr) { - boost::property_tree::ptree response_l; response_l.put ("hash", block->hash ().to_string ()); - response (response_l); } else { - error_response (response, "Block is invalid"); + ec = nano::error_blocks::invalid_block; } + response_errors (); } void rai::rpc_handler::successors () @@ -1422,7 +1399,6 @@ void rai::rpc_handler::successors () uint64_t count; if (!decode_unsigned (count_text, count)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); while (!block.is_zero () && blocks.size () < count) @@ -1441,52 +1417,50 @@ void rai::rpc_handler::successors () } } response_l.add_child ("blocks", blocks); - response (response_l); } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } else { - error_response (response, "Invalid block hash"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::bootstrap () { std::string address_text = request.get ("address"); std::string port_text = request.get ("port"); - boost::system::error_code ec; - auto address (boost::asio::ip::address_v6::from_string (address_text, ec)); - if (!ec) + boost::system::error_code address_ec; + auto address (boost::asio::ip::address_v6::from_string (address_text, address_ec)); + if (!address_ec) { uint16_t port; if (!rai::parse_port (port_text, port)) { node.bootstrap_initiator.bootstrap (rai::endpoint (address, port)); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Invalid port"); + ec = nano::error_common::invalid_port; } } else { - error_response (response, "Invalid address"); + ec = nano::error_common::invalid_ip_address; } + response_errors (); } void rai::rpc_handler::bootstrap_any () { node.bootstrap_initiator.bootstrap (); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); + response_errors (); } void rai::rpc_handler::chain () @@ -1499,7 +1473,6 @@ void rai::rpc_handler::chain () uint64_t count; if (!decode_unsigned (count_text, count)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); while (!block.is_zero () && blocks.size () < count) @@ -1518,22 +1491,21 @@ void rai::rpc_handler::chain () } } response_l.add_child ("blocks", blocks); - response (response_l); } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } else { - error_response (response, "Invalid block hash"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::confirmation_history () { - boost::property_tree::ptree response_l; boost::property_tree::ptree elections; { std::lock_guard lock (node.active.mutex); @@ -1546,17 +1518,15 @@ void rai::rpc_handler::confirmation_history () } } response_l.add_child ("confirmations", elections); - response (response_l); + response_errors (); } void rai::rpc_handler::delegators () { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree delegators; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i) @@ -1572,20 +1542,19 @@ void rai::rpc_handler::delegators () } } response_l.add_child ("delegators", delegators); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::delegators_count () { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { uint64_t count (0); rai::transaction transaction (node.store.environment, nullptr, false); @@ -1599,14 +1568,13 @@ void rai::rpc_handler::delegators_count () ++count; } } - boost::property_tree::ptree response_l; response_l.put ("count", std::to_string (count)); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::deterministic_key () @@ -1627,23 +1595,22 @@ void rai::rpc_handler::deterministic_key () blake2b_update (&hash, seed.data.bytes.data (), seed.data.bytes.size ()); blake2b_update (&hash, reinterpret_cast (&index.dwords[7]), sizeof (uint32_t)); blake2b_final (&hash, prv.bytes.data (), prv.bytes.size ()); - boost::property_tree::ptree response_l; rai::uint256_union pub; ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); response_l.put ("private", prv.to_string ()); response_l.put ("public", pub.to_string ()); response_l.put ("account", pub.to_account ()); - response (response_l); } else { - error_response (response, "Invalid index"); + ec = nano::error_common::invalid_index; } } else { - error_response (response, "Bad seed"); + ec = nano::error_common::bad_seed; } + response_errors (); } void rai::rpc_handler::frontiers () @@ -1656,7 +1623,6 @@ void rai::rpc_handler::frontiers () uint64_t count; if (!decode_unsigned (count_text, count)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree frontiers; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && frontiers.size () < count; ++i) @@ -1664,26 +1630,25 @@ void rai::rpc_handler::frontiers () frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second).head.to_string ()); } response_l.add_child ("frontiers", frontiers); - response (response_l); } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } else { - error_response (response, "Invalid starting account"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::account_count () { rai::transaction transaction (node.store.environment, nullptr, false); auto size (node.store.account_count (transaction)); - boost::property_tree::ptree response_l; response_l.put ("count", std::to_string (size)); - response (response_l); + response_errors (); } namespace @@ -1823,37 +1788,34 @@ void rai::rpc_handler::account_history () std::string account_text; std::string count_text (request.get ("count")); bool output_raw (request.get_optional ("raw") == true); - auto error (false); rai::block_hash hash; auto head_str (request.get_optional ("head")); rai::transaction transaction (node.store.environment, nullptr, false); if (head_str) { - error = hash.decode_hex (*head_str); - if (!error) + if (!hash.decode_hex (*head_str)) { account_text = node.ledger.account (transaction, hash).to_account (); } else { - error_response (response, "Invalid block hash"); + ec = nano::error_blocks::bad_hash_number; } } else { account_text = request.get ("account"); rai::uint256_union account; - error = account.decode_account (account_text); - if (!error) + if (!account.decode_account (account_text)) { hash = node.ledger.latest (transaction, account); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } - if (!error) + if (!ec) { uint64_t count; if (!decode_unsigned (count_text, count)) @@ -1862,60 +1824,52 @@ void rai::rpc_handler::account_history () auto offset_text (request.get_optional ("offset")); if (!offset_text || !decode_unsigned (*offset_text, offset)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree history; - if (!error) + response_l.put ("account", account_text); + auto block (node.store.block_get (transaction, hash)); + while (block != nullptr && count > 0) { - response_l.put ("account", account_text); - auto block (node.store.block_get (transaction, hash)); - while (block != nullptr && count > 0) + if (offset > 0) { - if (offset > 0) + --offset; + } + else + { + boost::property_tree::ptree entry; + history_visitor visitor (*this, output_raw, transaction, entry, hash); + block->visit (visitor); + if (!entry.empty ()) { - --offset; - } - else - { - boost::property_tree::ptree entry; - history_visitor visitor (*this, output_raw, transaction, entry, hash); - block->visit (visitor); - if (!entry.empty ()) + entry.put ("hash", hash.to_string ()); + if (output_raw) { - entry.put ("hash", hash.to_string ()); - if (output_raw) - { - entry.put ("work", rai::to_string_hex (block->block_work ())); - entry.put ("signature", block->block_signature ().to_string ()); - } - history.push_back (std::make_pair ("", entry)); + entry.put ("work", rai::to_string_hex (block->block_work ())); + entry.put ("signature", block->block_signature ().to_string ()); } - --count; + history.push_back (std::make_pair ("", entry)); } - hash = block->previous (); - block = node.store.block_get (transaction, hash); + --count; } - response_l.add_child ("history", history); - if (!hash.is_zero ()) - { - response_l.put ("previous", hash.to_string ()); - } - response (response_l); + hash = block->previous (); + block = node.store.block_get (transaction, hash); } - else + response_l.add_child ("history", history); + if (!hash.is_zero ()) { - error_response (response, "Failed to decode head block hash"); + response_l.put ("previous", hash.to_string ()); } } else { - error_response (response, "Invalid offset"); + ec = nano::error_rpc::invalid_offset; } } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } + response_errors (); } void rai::rpc_handler::keepalive () @@ -1928,49 +1882,46 @@ void rai::rpc_handler::keepalive () if (!rai::parse_port (port_text, port)) { node.keepalive (address_text, port); - boost::property_tree::ptree response_l; - response (response_l); + response_l.put ("started", "1"); } else { - error_response (response, "Invalid port"); + ec = nano::error_common::invalid_port; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::key_create () { - boost::property_tree::ptree response_l; rai::keypair pair; response_l.put ("private", pair.prv.data.to_string ()); response_l.put ("public", pair.pub.to_string ()); response_l.put ("account", pair.pub.to_account ()); - response (response_l); + response_errors (); } void rai::rpc_handler::key_expand () { std::string key_text (request.get ("key")); rai::uint256_union prv; - auto error (prv.decode_hex (key_text)); - if (!error) + if (!prv.decode_hex (key_text)) { - boost::property_tree::ptree response_l; rai::uint256_union pub; ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); response_l.put ("private", prv.to_string ()); response_l.put ("public", pub.to_string ()); response_l.put ("account", pub.to_account ()); - response (response_l); } else { - error_response (response, "Bad private key"); + ec = nano::error_common::bad_private_key; } + response_errors (); } void rai::rpc_handler::ledger () @@ -1994,7 +1945,7 @@ void rai::rpc_handler::ledger () auto error_count (decode_unsigned (count_text.get (), count)); if (error_count) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } uint64_t modified_since (0); @@ -2007,8 +1958,6 @@ void rai::rpc_handler::ledger () const bool representative = request.get ("representative", false); const bool weight = request.get ("weight", false); const bool pending = request.get ("pending", false); - boost::property_tree::ptree response_a; - boost::property_tree::ptree response_l; boost::property_tree::ptree accounts; rai::transaction transaction (node.store.environment, nullptr, false); if (!sorting) // Simple @@ -2019,32 +1968,32 @@ void rai::rpc_handler::ledger () if (info.modified >= modified_since) { rai::account account (i->first.uint256 ()); - boost::property_tree::ptree response_l; - response_l.put ("frontier", info.head.to_string ()); - response_l.put ("open_block", info.open_block.to_string ()); - response_l.put ("representative_block", info.rep_block.to_string ()); + boost::property_tree::ptree response_a; + response_a.put ("frontier", info.head.to_string ()); + response_a.put ("open_block", info.open_block.to_string ()); + response_a.put ("representative_block", info.rep_block.to_string ()); std::string balance; rai::uint128_union (info.balance).encode_dec (balance); - response_l.put ("balance", balance); - response_l.put ("modified_timestamp", std::to_string (info.modified)); - response_l.put ("block_count", std::to_string (info.block_count)); + response_a.put ("balance", balance); + response_a.put ("modified_timestamp", std::to_string (info.modified)); + response_a.put ("block_count", std::to_string (info.block_count)); if (representative) { auto block (node.store.block_get (transaction, info.rep_block)); assert (block != nullptr); - response_l.put ("representative", block->representative ().to_account ()); + response_a.put ("representative", block->representative ().to_account ()); } if (weight) { auto account_weight (node.ledger.weight (transaction, account)); - response_l.put ("weight", account_weight.convert_to ()); + response_a.put ("weight", account_weight.convert_to ()); } if (pending) { auto account_pending (node.ledger.account_pending (transaction, account)); - response_l.put ("pending", account_pending.convert_to ()); + response_a.put ("pending", account_pending.convert_to ()); } - accounts.push_back (std::make_pair (account.to_account (), response_l)); + accounts.push_back (std::make_pair (account.to_account (), response_a)); } } } @@ -2067,40 +2016,41 @@ void rai::rpc_handler::ledger () { node.store.account_get (transaction, i->second, info); rai::account account (i->second); - response_l.put ("frontier", info.head.to_string ()); - response_l.put ("open_block", info.open_block.to_string ()); - response_l.put ("representative_block", info.rep_block.to_string ()); + boost::property_tree::ptree response_a; + response_a.put ("frontier", info.head.to_string ()); + response_a.put ("open_block", info.open_block.to_string ()); + response_a.put ("representative_block", info.rep_block.to_string ()); std::string balance; (i->first).encode_dec (balance); - response_l.put ("balance", balance); - response_l.put ("modified_timestamp", std::to_string (info.modified)); - response_l.put ("block_count", std::to_string (info.block_count)); + response_a.put ("balance", balance); + response_a.put ("modified_timestamp", std::to_string (info.modified)); + response_a.put ("block_count", std::to_string (info.block_count)); if (representative) { auto block (node.store.block_get (transaction, info.rep_block)); assert (block != nullptr); - response_l.put ("representative", block->representative ().to_account ()); + response_a.put ("representative", block->representative ().to_account ()); } if (weight) { auto account_weight (node.ledger.weight (transaction, account)); - response_l.put ("weight", account_weight.convert_to ()); + response_a.put ("weight", account_weight.convert_to ()); } if (pending) { auto account_pending (node.ledger.account_pending (transaction, account)); - response_l.put ("pending", account_pending.convert_to ()); + response_a.put ("pending", account_pending.convert_to ()); } - accounts.push_back (std::make_pair (account.to_account (), response_l)); + accounts.push_back (std::make_pair (account.to_account (), response_a)); } } - response_a.add_child ("accounts", accounts); - response (response_a); + response_l.add_child ("accounts", accounts); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::mrai_from_raw () @@ -2110,14 +2060,13 @@ void rai::rpc_handler::mrai_from_raw () if (!amount.decode_dec (amount_text)) { auto result (amount.number () / rai::Mxrb_ratio); - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::mrai_to_raw () @@ -2129,19 +2078,18 @@ void rai::rpc_handler::mrai_to_raw () auto result (amount.number () * rai::Mxrb_ratio); if (result > amount.number ()) { - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Amount too big"); + ec = nano::error_common::invalid_amount_big; } } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::krai_from_raw () @@ -2151,14 +2099,13 @@ void rai::rpc_handler::krai_from_raw () if (!amount.decode_dec (amount_text)) { auto result (amount.number () / rai::kxrb_ratio); - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::krai_to_raw () @@ -2170,19 +2117,18 @@ void rai::rpc_handler::krai_to_raw () auto result (amount.number () * rai::kxrb_ratio); if (result > amount.number ()) { - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Amount too big"); + ec = nano::error_common::invalid_amount_big; } } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::password_change () @@ -2191,74 +2137,68 @@ void rai::rpc_handler::password_change () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, true); - boost::property_tree::ptree response_l; std::string password_text (request.get ("password")); auto error (existing->second->store.rekey (transaction, password_text)); response_l.put ("changed", error ? "0" : "1"); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::password_enter () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; std::string password_text (request.get ("password")); auto error (existing->second->enter_password (password_text)); response_l.put ("valid", error ? "0" : "1"); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::password_valid (bool wallet_locked = false) { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, false); - boost::property_tree::ptree response_l; auto valid (existing->second->store.valid_password (transaction)); if (!wallet_locked) { @@ -2268,22 +2208,21 @@ void rai::rpc_handler::password_valid (bool wallet_locked = false) { response_l.put ("locked", valid ? "0" : "1"); } - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::peers () { - boost::property_tree::ptree response_l; boost::property_tree::ptree peers_l; auto peers_list (node.peers.list_version ()); for (auto i (peers_list.begin ()), n (peers_list.end ()); i != n; ++i) @@ -2293,7 +2232,7 @@ void rai::rpc_handler::peers () peers_l.push_back (boost::property_tree::ptree::value_type (text.str (), boost::property_tree::ptree (std::to_string (i->second)))); } response_l.add_child ("peers", peers_l); - response (response_l); + response_errors (); } void rai::rpc_handler::pending () @@ -2307,25 +2246,23 @@ void rai::rpc_handler::pending () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } boost::optional threshold_text (request.get_optional ("threshold")); - if (threshold_text.is_initialized ()) + if (!ec && threshold_text.is_initialized ()) { - auto error_threshold (threshold.decode_dec (threshold_text.get ())); - if (error_threshold) + if (threshold.decode_dec (threshold_text.get ())) { - error_response (response, "Bad threshold number"); + ec = nano::error_common::bad_threshold; } } const bool source = request.get ("source", false); - boost::property_tree::ptree response_l; - boost::property_tree::ptree peers_l; + if (!ec) { + boost::property_tree::ptree peers_l; rai::transaction transaction (node.store.environment, nullptr, false); rai::account end (account.number () + 1); for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) @@ -2356,22 +2293,21 @@ void rai::rpc_handler::pending () } } } + response_l.add_child ("blocks", peers_l); } - response_l.add_child ("blocks", peers_l); - response (response_l); } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::pending_exists () { std::string hash_text (request.get ("hash")); rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { rai::transaction transaction (node.store.environment, nullptr, false); auto block (node.store.block_get (transaction, hash)); @@ -2383,19 +2319,18 @@ void rai::rpc_handler::pending_exists () { exists = node.store.pending_exists (transaction, rai::pending_key (destination, hash)); } - boost::property_tree::ptree response_l; response_l.put ("exists", exists ? "1" : "0"); - response (response_l); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Bad hash number"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::payment_begin () @@ -2441,29 +2376,28 @@ void rai::rpc_handler::payment_begin () } while (account.is_zero ()); if (!account.is_zero ()) { - boost::property_tree::ptree response_l; response_l.put ("account", account.to_account ()); - response (response_l); } else { - error_response (response, "Unable to create transaction account"); + ec = nano::error_rpc::payment_unable_create_account; } } else { - error_response (response, "Wallet locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Unable to find wallets"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::payment_init () @@ -2480,28 +2414,23 @@ void rai::rpc_handler::payment_init () if (wallet->store.valid_password (transaction)) { wallet->init_free_accounts (transaction); - boost::property_tree::ptree response_l; response_l.put ("status", "Ready"); - response (response_l); } else { - boost::property_tree::ptree response_l; - response_l.put ("status", "Transaction wallet locked"); - response (response_l); + ec = nano::error_common::wallet_locked; } } else { - boost::property_tree::ptree response_l; - response_l.put ("status", "Unable to find transaction wallet"); - response (response_l); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad transaction wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::payment_end () @@ -2525,33 +2454,33 @@ void rai::rpc_handler::payment_end () if (node.ledger.account_balance (transaction, account).is_zero ()) { wallet->free_accounts.insert (account); - boost::property_tree::ptree response_l; - response (response_l); + response_l.put ("ended", "1"); } else { - error_response (response, "Account has non-zero balance"); + ec = nano::error_rpc::payment_account_balance; } } else { - error_response (response, "Account not in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Invalid account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "Unable to find wallet"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::payment_wait () @@ -2579,17 +2508,21 @@ void rai::rpc_handler::payment_wait () } else { - error_response (response, "Bad timeout number"); + ec = nano::error_rpc::bad_timeout; } } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; + } + if (ec) + { + response_errors (); } } @@ -2615,40 +2548,48 @@ void rai::rpc_handler::process () { case rai::process_result::progress: { - boost::property_tree::ptree response_l; response_l.put ("hash", hash.to_string ()); - response (response_l); break; } case rai::process_result::gap_previous: { - error_response (response, "Gap previous block"); + ec = nano::error_process::gap_previous; break; } case rai::process_result::gap_source: { - error_response (response, "Gap source block"); + ec = nano::error_process::gap_source; break; } case rai::process_result::old: { - error_response (response, "Old block"); + ec = nano::error_process::old; break; } case rai::process_result::bad_signature: { - error_response (response, "Bad signature"); + ec = nano::error_process::bad_signature; break; } case rai::process_result::negative_spend: { // TODO once we get RPC versioning, this should be changed to "negative spend" - error_response (response, "Overspend"); + ec = nano::error_process::negative_spend; + break; + } + case rai::process_result::balance_mismatch: + { + ec = nano::error_process::balance_mismatch; break; } case rai::process_result::unreceivable: { - error_response (response, "Unreceivable"); + ec = nano::error_process::unreceivable; + break; + } + case rai::process_result::block_position: + { + ec = nano::error_process::block_position; break; } case rai::process_result::fork: @@ -2658,32 +2599,31 @@ void rai::rpc_handler::process () { node.active.erase (*block); node.block_processor.force (block); - boost::property_tree::ptree response_l; response_l.put ("hash", hash.to_string ()); - response (response_l); } else { - error_response (response, "Fork"); + ec = nano::error_process::fork; } break; } default: { - error_response (response, "Error processing block"); + ec = nano::error_process::other; break; } } } else { - error_response (response, "Block work is invalid"); + ec = nano::error_blocks::work_low; } } else { - error_response (response, "Block is invalid"); + ec = nano::error_blocks::invalid_block; } + response_errors (); } void rai::rpc_handler::rai_from_raw () @@ -2693,14 +2633,13 @@ void rai::rpc_handler::rai_from_raw () if (!amount.decode_dec (amount_text)) { auto result (amount.number () / rai::xrb_ratio); - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::rai_to_raw () @@ -2712,19 +2651,18 @@ void rai::rpc_handler::rai_to_raw () auto result (amount.number () * rai::xrb_ratio); if (result > amount.number ()) { - boost::property_tree::ptree response_l; response_l.put ("amount", result.convert_to ()); - response (response_l); } else { - error_response (response, "Amount too big"); + ec = nano::error_common::invalid_amount_big; } } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } + response_errors (); } void rai::rpc_handler::receive () @@ -2733,16 +2671,14 @@ void rai::rpc_handler::receive () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, false); auto account_check (existing->second->store.find (transaction, account)); @@ -2750,8 +2686,7 @@ void rai::rpc_handler::receive () { std::string hash_text (request.get ("block")); rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { auto block (node.store.block_get (transaction, hash)); if (block != nullptr) @@ -2760,16 +2695,14 @@ void rai::rpc_handler::receive () { uint64_t work (0); boost::optional work_text (request.get_optional ("work")); - auto error (false); if (work_text.is_initialized ()) { - error = rai::from_string_hex (work_text.get (), work); - if (error) + if (rai::from_string_hex (work_text.get (), work)) { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } - if (work) + if (!ec && work) { rai::account_info info; rai::uint256_union head; @@ -2788,11 +2721,10 @@ void rai::rpc_handler::receive () } else { - error = true; - error_response (response, "Invalid work"); + ec = nano::error_common::invalid_work; } } - if (!error) + if (!ec) { auto response_a (response); existing->second->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { @@ -2810,42 +2742,47 @@ void rai::rpc_handler::receive () } else { - error_response (response, "Block is not available to receive"); + ec = nano::error_process::unreceivable; } } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Bad block number"); + ec = nano::error_blocks::bad_hash_number; } } else { - error_response (response, "Account not found in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; + } + // Because of receive_async + if (ec) + { + response_errors (); } } @@ -2853,14 +2790,13 @@ void rai::rpc_handler::receive_minimum () { if (rpc.config.enable_control) { - boost::property_tree::ptree response_l; response_l.put ("amount", node.config.receive_minimum.to_string_dec ()); - response (response_l); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::receive_minimum_set () @@ -2872,19 +2808,18 @@ void rai::rpc_handler::receive_minimum_set () if (!amount.decode_dec (amount_text)) { node.config.receive_minimum = amount; - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Bad amount number"); + ec = nano::error_common::invalid_amount; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::representatives () @@ -2893,48 +2828,48 @@ void rai::rpc_handler::representatives () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } - const bool sorting = request.get ("sorting", false); - boost::property_tree::ptree response_l; - boost::property_tree::ptree representatives; - rai::transaction transaction (node.store.environment, nullptr, false); - if (!sorting) // Simple + if (!ec) { - for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n && representatives.size () < count; ++i) + const bool sorting = request.get ("sorting", false); + boost::property_tree::ptree representatives; + rai::transaction transaction (node.store.environment, nullptr, false); + if (!sorting) // Simple { - rai::account account (i->first.uint256 ()); - auto amount (node.store.representation_get (transaction, account)); - representatives.put (account.to_account (), amount.convert_to ()); + for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n && representatives.size () < count; ++i) + { + rai::account account (i->first.uint256 ()); + auto amount (node.store.representation_get (transaction, account)); + representatives.put (account.to_account (), amount.convert_to ()); + } } + else // Sorting + { + std::vector> representation; + for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n; ++i) + { + rai::account account (i->first.uint256 ()); + auto amount (node.store.representation_get (transaction, account)); + representation.push_back (std::make_pair (amount, account.to_account ())); + } + std::sort (representation.begin (), representation.end ()); + std::reverse (representation.begin (), representation.end ()); + for (auto i (representation.begin ()), n (representation.end ()); i != n && representatives.size () < count; ++i) + { + representatives.put (i->second, (i->first).number ().convert_to ()); + } + } + response_l.add_child ("representatives", representatives); } - else // Sorting - { - std::vector> representation; - for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n; ++i) - { - rai::account account (i->first.uint256 ()); - auto amount (node.store.representation_get (transaction, account)); - representation.push_back (std::make_pair (amount, account.to_account ())); - } - std::sort (representation.begin (), representation.end ()); - std::reverse (representation.begin (), representation.end ()); - for (auto i (representation.begin ()), n (representation.end ()); i != n && representatives.size () < count; ++i) - { - representatives.put (i->second, (i->first).number ().convert_to ()); - } - } - response_l.add_child ("representatives", representatives); - response (response_l); + response_errors (); } void rai::rpc_handler::representatives_online () { - boost::property_tree::ptree response_l; boost::property_tree::ptree representatives; auto reps (node.online_reps.list ()); for (auto & i : reps) @@ -2942,7 +2877,7 @@ void rai::rpc_handler::representatives_online () representatives.put (i.to_account (), ""); } response_l.add_child ("representatives", representatives); - response (response_l); + response_errors (); } void rai::rpc_handler::republish () @@ -2953,36 +2888,31 @@ void rai::rpc_handler::republish () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } boost::optional sources_text (request.get_optional ("sources")); - if (sources_text.is_initialized ()) + if (!ec && sources_text.is_initialized ()) { - auto sources_error (decode_unsigned (sources_text.get (), sources)); - if (sources_error) + if (decode_unsigned (sources_text.get (), sources)) { - error_response (response, "Invalid sources number"); + ec = nano::error_rpc::invalid_sources; } } boost::optional destinations_text (request.get_optional ("destinations")); - if (destinations_text.is_initialized ()) + if (!ec && destinations_text.is_initialized ()) { - auto destinations_error (decode_unsigned (destinations_text.get (), destinations)); - if (destinations_error) + if (decode_unsigned (destinations_text.get (), destinations)) { - error_response (response, "Invalid destinations number"); + ec = nano::error_rpc::invalid_destinations; } } std::string hash_text (request.get ("hash")); rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!ec && !hash.decode_hex (hash_text)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); auto block (node.store.block_get (transaction, hash)); @@ -3056,17 +2986,17 @@ void rai::rpc_handler::republish () } response_l.put ("success", ""); // obsolete response_l.add_child ("blocks", blocks); - response (response_l); } else { - error_response (response, "Block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Bad hash number"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::search_pending () @@ -3075,27 +3005,25 @@ void rai::rpc_handler::search_pending () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { auto error (existing->second->search_pending ()); - boost::property_tree::ptree response_l; response_l.put ("started", !error); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::search_pending_all () @@ -3103,14 +3031,13 @@ void rai::rpc_handler::search_pending_all () if (rpc.config.enable_control) { node.wallets.search_pending_all (); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::send () @@ -3119,8 +3046,7 @@ void rai::rpc_handler::send () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3137,21 +3063,19 @@ void rai::rpc_handler::send () { std::string amount_text (request.get ("amount")); rai::amount amount; - auto error (amount.decode_dec (amount_text)); - if (!error) + if (!amount.decode_dec (amount_text)) { uint64_t work (0); boost::optional work_text (request.get_optional ("work")); if (work_text.is_initialized ()) { - error = rai::from_string_hex (work_text.get (), work); - if (error) + if (rai::from_string_hex (work_text.get (), work)) { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } rai::uint128_t balance (0); - if (!error) + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 rai::account_info info; @@ -3161,10 +3085,9 @@ void rai::rpc_handler::send () } else { - error = true; - error_response (response, "Account not found"); + ec = nano::error_common::account_not_found; } - if (!error && work) + if (!ec && work) { if (!rai::work_validate (info.head, work)) { @@ -3172,12 +3095,11 @@ void rai::rpc_handler::send () } else { - error = true; - error_response (response, "Invalid work"); + ec = nano::error_common::invalid_work; } } } - if (!error) + if (!ec) { boost::optional send_id (request.get_optional ("id")); if (balance >= amount.number ()) @@ -3201,44 +3123,48 @@ void rai::rpc_handler::send () } else { - error_response (response, "Insufficient balance"); + ec = nano::error_common::insufficient_balance; } } } else { - error_response (response, "Bad amount format"); + ec = nano::error_common::invalid_amount; } } else { - error_response (response, "Bad destination account"); + ec = nano::error_rpc::bad_destination; } } else { - error_response (response, "Bad source account"); + ec = nano::error_rpc::bad_source; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; + } + // Because of send_async + if (ec) + { + response_errors (); } } void rai::rpc_handler::stats () { - bool error = false; auto sink = node.stats.log_sink_json (); std::string type (request.get ("type", "")); if (type == "counters") @@ -3251,29 +3177,33 @@ void rai::rpc_handler::stats () } else { - error = true; - error_response (response, "Invalid or missing type argument"); + ec = nano::error_rpc::invalid_missing_type; } - - if (!error) + if (!ec) { response (*static_cast (sink->to_object ())); } + else + { + response_errors (); + } } void rai::rpc_handler::stop () { if (rpc.config.enable_control) { - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); - rpc.stop (); - node.stop (); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; + } + response_errors (); + if (!ec) + { + rpc.stop (); + node.stop (); } } @@ -3283,25 +3213,26 @@ void rai::rpc_handler::unchecked () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } - boost::property_tree::ptree response_l; - boost::property_tree::ptree unchecked; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (node.store.unchecked_begin (transaction)), n (node.store.unchecked_end ()); i != n && unchecked.size () < count; ++i) + if (!ec) { - rai::bufferstream stream (reinterpret_cast (i->second.data ()), i->second.size ()); - auto block (rai::deserialize_block (stream)); - std::string contents; - block->serialize_json (contents); - unchecked.put (block->hash ().to_string (), contents); + boost::property_tree::ptree unchecked; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (node.store.unchecked_begin (transaction)), n (node.store.unchecked_end ()); i != n && unchecked.size () < count; ++i) + { + rai::bufferstream stream (reinterpret_cast (i->second.data ()), i->second.size ()); + auto block (rai::deserialize_block (stream)); + std::string contents; + block->serialize_json (contents); + unchecked.put (block->hash ().to_string (), contents); + } + response_l.add_child ("blocks", unchecked); } - response_l.add_child ("blocks", unchecked); - response (response_l); + response_errors (); } void rai::rpc_handler::unchecked_clear () @@ -3310,14 +3241,13 @@ void rai::rpc_handler::unchecked_clear () { rai::transaction transaction (node.store.environment, nullptr, true); node.store.unchecked_clear (transaction); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::unchecked_get () @@ -3341,19 +3271,16 @@ void rai::rpc_handler::unchecked_get () break; } } - if (!response_l.empty ()) + if (response_l.empty ()) { - response (response_l); - } - else - { - error_response (response, "Unchecked block not found"); + ec = nano::error_blocks::not_found; } } else { - error_response (response, "Bad hash number"); + ec = nano::error_blocks::bad_hash_number; } + response_errors (); } void rai::rpc_handler::unchecked_keys () @@ -3363,47 +3290,46 @@ void rai::rpc_handler::unchecked_keys () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } boost::optional hash_text (request.get_optional ("key")); - if (hash_text.is_initialized ()) + if (!ec && hash_text.is_initialized ()) { - auto error_hash (key.decode_hex (hash_text.get ())); - if (error_hash) + if (key.decode_hex (hash_text.get ())) { - error_response (response, "Bad key hash number"); + ec = nano::error_rpc::bad_key; } } - boost::property_tree::ptree response_l; - boost::property_tree::ptree unchecked; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (node.store.unchecked_begin (transaction, key)), n (node.store.unchecked_end ()); i != n && unchecked.size () < count; ++i) + if (!ec) { - boost::property_tree::ptree entry; - rai::bufferstream stream (reinterpret_cast (i->second.data ()), i->second.size ()); - auto block (rai::deserialize_block (stream)); - std::string contents; - block->serialize_json (contents); - entry.put ("key", rai::block_hash (i->first.uint256 ()).to_string ()); - entry.put ("hash", block->hash ().to_string ()); - entry.put ("contents", contents); - unchecked.push_back (std::make_pair ("", entry)); + boost::property_tree::ptree unchecked; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (node.store.unchecked_begin (transaction, key)), n (node.store.unchecked_end ()); i != n && unchecked.size () < count; ++i) + { + boost::property_tree::ptree entry; + rai::bufferstream stream (reinterpret_cast (i->second.data ()), i->second.size ()); + auto block (rai::deserialize_block (stream)); + std::string contents; + block->serialize_json (contents); + entry.put ("key", rai::block_hash (i->first.uint256 ()).to_string ()); + entry.put ("hash", block->hash ().to_string ()); + entry.put ("contents", contents); + unchecked.push_back (std::make_pair ("", entry)); + } + response_l.add_child ("unchecked", unchecked); } - response_l.add_child ("unchecked", unchecked); - response (response_l); + response_errors (); } void rai::rpc_handler::version () { - boost::property_tree::ptree response_l; response_l.put ("rpc_version", "1"); response_l.put ("store_version", std::to_string (node.store_version ())); response_l.put ("node_vendor", boost::str (boost::format ("RaiBlocks %1%.%2%") % RAIBLOCKS_VERSION_MAJOR % RAIBLOCKS_VERSION_MINOR)); - response (response_l); + response_errors (); } void rai::rpc_handler::validate_account_number () @@ -3411,9 +3337,8 @@ void rai::rpc_handler::validate_account_number () std::string account_text (request.get ("account")); rai::uint256_union account; auto error (account.decode_account (account_text)); - boost::property_tree::ptree response_l; response_l.put ("valid", error ? "0" : "1"); - response (response_l); + response_errors (); } void rai::rpc_handler::wallet_add () @@ -3423,12 +3348,10 @@ void rai::rpc_handler::wallet_add () std::string key_text (request.get ("key")); std::string wallet_text (request.get ("wallet")); rai::raw_key key; - auto error (key.data.decode_hex (key_text)); - if (!error) + if (!key.data.decode_hex (key_text)) { rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3437,34 +3360,33 @@ void rai::rpc_handler::wallet_add () auto pub (existing->second->insert_adhoc (key, generate_work)); if (!pub.is_zero ()) { - boost::property_tree::ptree response_l; response_l.put ("account", pub.to_account ()); - response (response_l); } else { - error_response (response, "Wallet locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "Bad private key"); + ec = nano::error_common::bad_private_key; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_add_watch () @@ -3473,8 +3395,7 @@ void rai::rpc_handler::wallet_add_watch () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3484,49 +3405,49 @@ void rai::rpc_handler::wallet_add_watch () { for (auto & accounts : request.get_child ("accounts")) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!ec) { - existing->second->insert_watch (transaction, account); - } - else - { - error_response (response, "Bad account number"); + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) + { + existing->second->insert_watch (transaction, account); + } + else + { + ec = nano::error_common::bad_account_number; + } } } - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Wallet locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_balance_total () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3540,60 +3461,48 @@ void rai::rpc_handler::wallet_balance_total () balance = balance + node.ledger.account_balance (transaction, account); pending = pending + node.ledger.account_pending (transaction, account); } - boost::property_tree::ptree response_l; response_l.put ("balance", balance.convert_to ()); response_l.put ("pending", pending.convert_to ()); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_balances () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { rai::uint128_union threshold (0); boost::optional threshold_text (request.get_optional ("threshold")); if (threshold_text.is_initialized ()) { - auto error_threshold (threshold.decode_dec (threshold_text.get ())); - if (error_threshold) + if (threshold.decode_dec (threshold_text.get ())) { - error_response (response, "Bad threshold number"); + ec = nano::error_common::bad_threshold; } } - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + if (!ec) { - boost::property_tree::ptree response_l; - boost::property_tree::ptree balances; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + auto existing (node.wallets.items.find (wallet)); + if (existing != node.wallets.items.end ()) { - rai::account account (i->first.uint256 ()); - rai::uint128_t balance = node.ledger.account_balance (transaction, account); - if (threshold.is_zero ()) + boost::property_tree::ptree balances; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) { - boost::property_tree::ptree entry; - rai::uint128_t pending = node.ledger.account_pending (transaction, account); - entry.put ("balance", balance.convert_to ()); - entry.put ("pending", pending.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } - else - { - if (balance >= threshold.number ()) + rai::account account (i->first.uint256 ()); + rai::uint128_t balance = node.ledger.account_balance (transaction, account); + if (threshold.is_zero ()) { boost::property_tree::ptree entry; rai::uint128_t pending = node.ledger.account_pending (transaction, account); @@ -3601,20 +3510,31 @@ void rai::rpc_handler::wallet_balances () entry.put ("pending", pending.convert_to ()); balances.push_back (std::make_pair (account.to_account (), entry)); } + else + { + if (balance >= threshold.number ()) + { + boost::property_tree::ptree entry; + rai::uint128_t pending = node.ledger.account_pending (transaction, account); + entry.put ("balance", balance.convert_to ()); + entry.put ("pending", pending.convert_to ()); + balances.push_back (std::make_pair (account.to_account (), entry)); + } + } } + response_l.add_child ("balances", balances); + } + else + { + ec = nano::error_common::wallet_not_found; } - response_l.add_child ("balances", balances); - response (response_l); - } - else - { - error_response (response, "Wallet not found"); } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_change_seed () @@ -3624,12 +3544,10 @@ void rai::rpc_handler::wallet_change_seed () std::string seed_text (request.get ("seed")); std::string wallet_text (request.get ("wallet")); rai::raw_key seed; - auto error (seed.data.decode_hex (seed_text)); - if (!error) + if (!seed.data.decode_hex (seed_text)) { rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3638,34 +3556,33 @@ void rai::rpc_handler::wallet_change_seed () if (existing->second->store.valid_password (transaction)) { existing->second->change_seed (transaction, seed); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Wallet locked"); + ec = nano::error_common::wallet_locked; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "Bad seed"); + ec = nano::error_common::bad_seed; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_contains () @@ -3673,36 +3590,33 @@ void rai::rpc_handler::wallet_contains () std::string account_text (request.get ("account")); std::string wallet_text (request.get ("wallet")); rai::uint256_union account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, false); auto exists (existing->second->store.find (transaction, account) != existing->second->store.end ()); - boost::property_tree::ptree response_l; response_l.put ("exists", exists ? "1" : "0"); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::wallet_create () @@ -3715,19 +3629,18 @@ void rai::rpc_handler::wallet_create () auto existing (node.wallets.items.find (wallet_id.pub)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; response_l.put ("wallet", wallet_id.pub.to_string ()); - response (response_l); } else { - error_response (response, "Failed to create wallet. Increase lmdb_max_dbs in node config."); + ec = nano::error_common::wallet_lmdb_max_dbs; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_destroy () @@ -3736,38 +3649,37 @@ void rai::rpc_handler::wallet_destroy () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { node.wallets.destroy (wallet); - boost::property_tree::ptree response_l; - response (response_l); + bool destroyed (node.wallets.items.find (wallet) == node.wallets.items.end ()); + response_l.put ("destroyed", destroyed ? "1" : "0"); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_export () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3775,32 +3687,29 @@ void rai::rpc_handler::wallet_export () rai::transaction transaction (node.store.environment, nullptr, false); std::string json; existing->second->store.serialize_json (transaction, json); - boost::property_tree::ptree response_l; response_l.put ("json", json); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::wallet_frontiers () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; boost::property_tree::ptree frontiers; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) @@ -3813,44 +3722,42 @@ void rai::rpc_handler::wallet_frontiers () } } response_l.add_child ("frontiers", frontiers); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_key_valid () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, false); auto valid (existing->second->store.valid_password (transaction)); - boost::property_tree::ptree response_l; response_l.put ("valid", valid ? "1" : "0"); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_ledger () @@ -3866,13 +3773,11 @@ void rai::rpc_handler::wallet_ledger () } std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; boost::property_tree::ptree accounts; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) @@ -3913,17 +3818,17 @@ void rai::rpc_handler::wallet_ledger () } } response_l.add_child ("accounts", accounts); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_lock () @@ -3932,41 +3837,38 @@ void rai::rpc_handler::wallet_lock () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; rai::raw_key empty; empty.data.clear (); existing->second->store.password.value_set (empty); response_l.put ("locked", "1"); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_pending () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) @@ -3976,101 +3878,99 @@ void rai::rpc_handler::wallet_pending () boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { - auto error (decode_unsigned (count_text.get (), count)); - if (error) + if (decode_unsigned (count_text.get (), count)) { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } boost::optional threshold_text (request.get_optional ("threshold")); - if (threshold_text.is_initialized ()) + if (!ec && threshold_text.is_initialized ()) { - auto error_threshold (threshold.decode_dec (threshold_text.get ())); - if (error_threshold) + if (threshold.decode_dec (threshold_text.get ())) { - error_response (response, "Bad threshold number"); + ec = nano::error_common::bad_threshold; } } const bool source = request.get ("source", false); - boost::property_tree::ptree response_l; - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + if (!ec) { - rai::account account (i->first.uint256 ()); - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) + boost::property_tree::ptree pending; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) { - rai::pending_key key (ii->first); - if (threshold.is_zero () && !source) + rai::account account (i->first.uint256 ()); + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (ii->second); - if (info.amount.number () >= threshold.number ()) + rai::pending_key key (ii->first); + if (threshold.is_zero () && !source) { - if (source) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (ii->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - pending_tree.put ("source", info.source.to_account ()); - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + if (source) + { + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + pending_tree.put ("source", info.source.to_account ()); + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + } } } } + if (!peers_l.empty ()) + { + pending.add_child (account.to_account (), peers_l); + } } - if (!peers_l.empty ()) - { - pending.add_child (account.to_account (), peers_l); - } + response_l.add_child ("blocks", pending); } - response_l.add_child ("blocks", pending); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } + response_errors (); } void rai::rpc_handler::wallet_representative () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, false); - boost::property_tree::ptree response_l; response_l.put ("representative", existing->second->store.representative (transaction).to_account ()); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } + response_errors (); } void rai::rpc_handler::wallet_representative_set () @@ -4079,42 +3979,39 @@ void rai::rpc_handler::wallet_representative_set () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { std::string representative_text (request.get ("representative")); rai::account representative; - auto error (representative.decode_account (representative_text)); - if (!error) + if (!representative.decode_account (representative_text)) { rai::transaction transaction (node.store.environment, nullptr, true); existing->second->store.representative_set (transaction, representative); - boost::property_tree::ptree response_l; response_l.put ("set", "1"); - response (response_l); } else { - error_response (response, "Invalid account number"); + ec = nano::error_rpc::bad_representative_number; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_republish () @@ -4123,18 +4020,15 @@ void rai::rpc_handler::wallet_republish () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { uint64_t count; std::string count_text (request.get ("count")); - auto error (decode_unsigned (count_text, count)); - if (!error) + if (!decode_unsigned (count_text, count)) { - boost::property_tree::ptree response_l; boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) @@ -4161,27 +4055,27 @@ void rai::rpc_handler::wallet_republish () } } response_l.add_child ("blocks", blocks); - response (response_l); } else { - error_response (response, "Invalid count limit"); + ec = nano::error_common::invalid_count; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::wallet_work_get () @@ -4190,13 +4084,11 @@ void rai::rpc_handler::wallet_work_get () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - boost::property_tree::ptree response_l; boost::property_tree::ptree works; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) @@ -4207,22 +4099,22 @@ void rai::rpc_handler::wallet_work_get () works.put (account.to_account (), rai::to_string_hex (work)); } response_l.add_child ("works", works); - response (response_l); } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_generate () @@ -4259,12 +4151,17 @@ void rai::rpc_handler::work_generate () } else { - error_response (response, "Bad block hash"); + ec = nano::error_blocks::invalid_block_hash; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; + } + // Because of callback + if (ec) + { + response_errors (); } } @@ -4274,22 +4171,20 @@ void rai::rpc_handler::work_cancel () { std::string hash_text (request.get ("hash")); rai::block_hash hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { node.work.cancel (hash); - boost::property_tree::ptree response_l; - response (response_l); } else { - error_response (response, "Bad block hash"); + ec = nano::error_blocks::invalid_block_hash; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_get () @@ -4298,16 +4193,14 @@ void rai::rpc_handler::work_get () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, false); auto account_check (existing->second->store.find (transaction, account)); @@ -4315,34 +4208,33 @@ void rai::rpc_handler::work_get () { uint64_t work (0); auto error_work (existing->second->store.work_get (transaction, account, work)); - boost::property_tree::ptree response_l; response_l.put ("work", rai::to_string_hex (work)); - response (response_l); } else { - error_response (response, "Account not found in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_set () @@ -4351,16 +4243,14 @@ void rai::rpc_handler::work_set () { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) + if (!wallet.decode_hex (wallet_text)) { auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { std::string account_text (request.get ("account")); rai::account account; - auto error (account.decode_account (account_text)); - if (!error) + if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, true); auto account_check (existing->second->store.find (transaction, account)); @@ -4368,51 +4258,48 @@ void rai::rpc_handler::work_set () { std::string work_text (request.get ("work")); uint64_t work; - auto work_error (rai::from_string_hex (work_text, work)); - if (!work_error) + if (!rai::from_string_hex (work_text, work)) { existing->second->store.work_put (transaction, account, work); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } else { - error_response (response, "Account not found in wallet"); + ec = nano::error_common::account_not_found_wallet; } } else { - error_response (response, "Bad account number"); + ec = nano::error_common::bad_account_number; } } else { - error_response (response, "Wallet not found"); + ec = nano::error_common::wallet_not_found; } } else { - error_response (response, "Bad wallet number"); + ec = nano::error_common::bad_wallet_number; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_validate () { std::string hash_text (request.get ("hash")); rai::block_hash hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { std::string work_text (request.get ("work")); uint64_t work; @@ -4420,19 +4307,18 @@ void rai::rpc_handler::work_validate () if (!work_error) { auto validate (rai::work_validate (hash, work)); - boost::property_tree::ptree response_l; response_l.put ("valid", validate ? "0" : "1"); - response (response_l); } else { - error_response (response, "Bad work"); + ec = nano::error_common::bad_work_format; } } else { - error_response (response, "Bad block hash"); + ec = nano::error_blocks::invalid_block_hash; } + response_errors (); } void rai::rpc_handler::work_peer_add () @@ -4445,19 +4331,18 @@ void rai::rpc_handler::work_peer_add () if (!rai::parse_port (port_text, port)) { node.config.work_peers.push_back (std::make_pair (address_text, port)); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "Invalid port"); + ec = nano::error_common::invalid_port; } } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_peers () @@ -4471,14 +4356,13 @@ void rai::rpc_handler::work_peers () entry.put ("", boost::str (boost::format ("%1%:%2%") % i->first % i->second)); work_peers_l.push_back (std::make_pair ("", entry)); } - boost::property_tree::ptree response_l; response_l.add_child ("work_peers", work_peers_l); - response (response_l); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } void rai::rpc_handler::work_peers_clear () @@ -4486,14 +4370,13 @@ void rai::rpc_handler::work_peers_clear () if (rpc.config.enable_control) { node.config.work_peers.clear (); - boost::property_tree::ptree response_l; response_l.put ("success", ""); - response (response_l); } else { - error_response (response, "RPC control is disabled"); + ec = nano::error_rpc::rpc_control_disabled; } + response_errors (); } rai::rpc_connection::rpc_connection (rai::node & node_a, rai::rpc & rpc_a) : diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index b77769dc..cba20edb 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,6 +222,9 @@ public: rai::rpc & rpc; boost::property_tree::ptree request; std::function response; + void response_errors (); + std::error_code ec; + boost::property_tree::ptree response_l; }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From db1b0f6a33e1ba4a3ffb8e78d8e6177b2ae49991 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 17:28:41 +0300 Subject: [PATCH 02/38] Clang formatting --- rai/node/rpc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index cb203e57..3946dbee 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1886,7 +1886,7 @@ void rai::rpc_handler::keepalive () } else { - ec = nano::error_common::invalid_port; + ec = nano::error_common::invalid_port; } } else @@ -3994,7 +3994,7 @@ void rai::rpc_handler::wallet_representative_set () } else { - ec = nano::error_rpc::bad_representative_number; + ec = nano::error_rpc::bad_representative_number; } } else From 12e33e5b76ed6dc659d9328b4dcddda7cd4f7433 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 18:53:19 +0300 Subject: [PATCH 03/38] Clang formatting --- rai/lib/errors.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/rai/lib/errors.hpp b/rai/lib/errors.hpp index c764f679..22a2c2c7 100644 --- a/rai/lib/errors.hpp +++ b/rai/lib/errors.hpp @@ -94,7 +94,6 @@ enum class error_rpc source_not_found }; - /** process_result related errors */ enum class error_process { From 9ed06410438c960733f323f560cf789a924ed410 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 18:54:17 +0300 Subject: [PATCH 04/38] Merge RPC chain & RPC successors --- rai/node/rpc.cpp | 49 ++++-------------------------------------------- rai/node/rpc.hpp | 5 ++--- 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 3946dbee..4c92ab9c 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1389,47 +1389,6 @@ void rai::rpc_handler::block_hash () response_errors (); } -void rai::rpc_handler::successors () -{ - std::string block_text (request.get ("block")); - std::string count_text (request.get ("count")); - rai::block_hash block; - if (!block.decode_hex (block_text)) - { - uint64_t count; - if (!decode_unsigned (count_text, count)) - { - boost::property_tree::ptree blocks; - rai::transaction transaction (node.store.environment, nullptr, false); - while (!block.is_zero () && blocks.size () < count) - { - auto block_l (node.store.block_get (transaction, block)); - if (block_l != nullptr) - { - boost::property_tree::ptree entry; - entry.put ("", block.to_string ()); - blocks.push_back (std::make_pair ("", entry)); - block = node.store.block_successor (transaction, block); - } - else - { - block.clear (); - } - } - response_l.add_child ("blocks", blocks); - } - else - { - ec = nano::error_common::invalid_count; - } - } - else - { - ec = nano::error_blocks::bad_hash_number; - } - response_errors (); -} - void rai::rpc_handler::bootstrap () { std::string address_text = request.get ("address"); @@ -1463,7 +1422,7 @@ void rai::rpc_handler::bootstrap_any () response_errors (); } -void rai::rpc_handler::chain () +void rai::rpc_handler::chain (bool successors) { std::string block_text (request.get ("block")); std::string count_text (request.get ("count")); @@ -1483,7 +1442,7 @@ void rai::rpc_handler::chain () boost::property_tree::ptree entry; entry.put ("", block.to_string ()); blocks.push_back (std::make_pair ("", entry)); - block = block_l->previous (); + block = successors ? node.store.block_successor (transaction, block) : block_l->previous (); } else { @@ -2189,7 +2148,7 @@ void rai::rpc_handler::password_enter () response_errors (); } -void rai::rpc_handler::password_valid (bool wallet_locked = false) +void rai::rpc_handler::password_valid (bool wallet_locked) { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; @@ -4608,7 +4567,7 @@ void rai::rpc_handler::process_request () } else if (action == "successors") { - successors (); + chain (true); } else if (action == "bootstrap") { diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index cba20edb..9ed3978b 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -144,7 +144,7 @@ public: void block_hash (); void bootstrap (); void bootstrap_any (); - void chain (); + void chain (bool = false); void confirmation_history (); void delegators (); void delegators_count (); @@ -161,7 +161,7 @@ public: void mrai_from_raw (); void password_change (); void password_enter (); - void password_valid (bool wallet_locked); + void password_valid (bool = false); void payment_begin (); void payment_init (); void payment_end (); @@ -183,7 +183,6 @@ public: void send (); void stats (); void stop (); - void successors (); void unchecked (); void unchecked_clear (); void unchecked_get (); From 516976795e03b758f479179ad614364bea3cbd2e Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 19:04:13 +0300 Subject: [PATCH 05/38] Merge mrai_to_raw+krai_to_raw+rai_to_raw & mrai_from_raw+krai_from_raw+rai_from_raw --- rai/node/rpc.cpp | 94 +++++------------------------------------------- rai/node/rpc.hpp | 8 ++--- 2 files changed, 10 insertions(+), 92 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 4c92ab9c..ac6a0dd7 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -2012,13 +2012,13 @@ void rai::rpc_handler::ledger () response_errors (); } -void rai::rpc_handler::mrai_from_raw () +void rai::rpc_handler::mrai_from_raw (rai::uint128_t ratio) { std::string amount_text (request.get ("amount")); rai::uint128_union amount; if (!amount.decode_dec (amount_text)) { - auto result (amount.number () / rai::Mxrb_ratio); + auto result (amount.number () / ratio); response_l.put ("amount", result.convert_to ()); } else @@ -2028,52 +2028,13 @@ void rai::rpc_handler::mrai_from_raw () response_errors (); } -void rai::rpc_handler::mrai_to_raw () +void rai::rpc_handler::mrai_to_raw (rai::uint128_t ratio) { std::string amount_text (request.get ("amount")); rai::uint128_union amount; if (!amount.decode_dec (amount_text)) { - auto result (amount.number () * rai::Mxrb_ratio); - if (result > amount.number ()) - { - response_l.put ("amount", result.convert_to ()); - } - else - { - ec = nano::error_common::invalid_amount_big; - } - } - else - { - ec = nano::error_common::invalid_amount; - } - response_errors (); -} - -void rai::rpc_handler::krai_from_raw () -{ - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) - { - auto result (amount.number () / rai::kxrb_ratio); - response_l.put ("amount", result.convert_to ()); - } - else - { - ec = nano::error_common::invalid_amount; - } - response_errors (); -} - -void rai::rpc_handler::krai_to_raw () -{ - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) - { - auto result (amount.number () * rai::kxrb_ratio); + auto result (amount.number () * ratio); if (result > amount.number ()) { response_l.put ("amount", result.convert_to ()); @@ -2585,45 +2546,6 @@ void rai::rpc_handler::process () response_errors (); } -void rai::rpc_handler::rai_from_raw () -{ - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) - { - auto result (amount.number () / rai::xrb_ratio); - response_l.put ("amount", result.convert_to ()); - } - else - { - ec = nano::error_common::invalid_amount; - } - response_errors (); -} - -void rai::rpc_handler::rai_to_raw () -{ - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) - { - auto result (amount.number () * rai::xrb_ratio); - if (result > amount.number ()) - { - response_l.put ("amount", result.convert_to ()); - } - else - { - ec = nano::error_common::invalid_amount_big; - } - } - else - { - ec = nano::error_common::invalid_amount; - } - response_errors (); -} - void rai::rpc_handler::receive () { if (rpc.config.enable_control) @@ -4624,11 +4546,11 @@ void rai::rpc_handler::process_request () } else if (action == "krai_from_raw") { - krai_from_raw (); + mrai_from_raw (rai::kxrb_ratio); } else if (action == "krai_to_raw") { - krai_to_raw (); + mrai_to_raw (rai::kxrb_ratio); } else if (action == "ledger") { @@ -4688,11 +4610,11 @@ void rai::rpc_handler::process_request () } else if (action == "rai_from_raw") { - rai_from_raw (); + mrai_from_raw (rai::xrb_ratio); } else if (action == "rai_to_raw") { - rai_to_raw (); + mrai_to_raw (rai::xrb_ratio); } else if (action == "receive") { diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 9ed3978b..739f9f5a 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -154,11 +154,9 @@ public: void keepalive (); void key_create (); void key_expand (); - void krai_to_raw (); - void krai_from_raw (); void ledger (); - void mrai_to_raw (); - void mrai_from_raw (); + void mrai_to_raw (rai::uint128_t = rai::Mxrb_ratio); + void mrai_from_raw (rai::uint128_t = rai::Mxrb_ratio); void password_change (); void password_enter (); void password_valid (bool = false); @@ -170,8 +168,6 @@ public: void pending (); void pending_exists (); void process (); - void rai_to_raw (); - void rai_from_raw (); void receive (); void receive_minimum (); void receive_minimum_set (); From b83666794cbd48395dd40f1356252e119b980403 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 19:15:12 +0300 Subject: [PATCH 06/38] Small changes --- rai/node/rpc.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index ac6a0dd7..60d7ecce 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1892,17 +1892,15 @@ void rai::rpc_handler::ledger () boost::optional account_text (request.get_optional ("account")); if (account_text.is_initialized ()) { - auto error (start.decode_account (account_text.get ())); - if (error) + if (start.decode_account (account_text.get ())) { - error_response (response, "Invalid starting account"); + ec = nano::error_common::bad_account_number; } } boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) + if (!ec && count_text.is_initialized ()) { - auto error_count (decode_unsigned (count_text.get (), count)); - if (error_count) + if (decode_unsigned (count_text.get (), count)) { ec = nano::error_common::invalid_count; } @@ -1919,7 +1917,7 @@ void rai::rpc_handler::ledger () const bool pending = request.get ("pending", false); boost::property_tree::ptree accounts; rai::transaction transaction (node.store.environment, nullptr, false); - if (!sorting) // Simple + if (!ec && !sorting) // Simple { for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && accounts.size () < count; ++i) { @@ -1956,7 +1954,7 @@ void rai::rpc_handler::ledger () } } } - else // Sorting + else if (!ec) // Sorting { std::vector> ledger_l; for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n; ++i) @@ -2562,8 +2560,7 @@ void rai::rpc_handler::receive () if (!account.decode_account (account_text)) { rai::transaction transaction (node.store.environment, nullptr, false); - auto account_check (existing->second->store.find (transaction, account)); - if (account_check != existing->second->store.end ()) + if (existing->second->store.find (transaction, account) != existing->second->store.end ()) { std::string hash_text (request.get ("block")); rai::uint256_union hash; @@ -2833,8 +2830,7 @@ void rai::rpc_handler::republish () auto destination (node.ledger.block_destination (transaction, *block_b)); if (!destination.is_zero ()) { - auto exists (node.store.pending_exists (transaction, rai::pending_key (destination, hash))); - if (!exists) + if (!node.store.pending_exists (transaction, rai::pending_key (destination, hash))) { rai::block_hash previous (node.ledger.latest (transaction, destination)); std::unique_ptr block_d (node.store.block_get (transaction, previous)); @@ -2934,13 +2930,11 @@ void rai::rpc_handler::send () { std::string source_text (request.get ("source")); rai::account source; - auto error (source.decode_account (source_text)); - if (!error) + if (!source.decode_account (source_text)) { std::string destination_text (request.get ("destination")); rai::account destination; - auto error (destination.decode_account (destination_text)); - if (!error) + if (!destination.decode_account (destination_text)) { std::string amount_text (request.get ("amount")); rai::amount amount; From 1530722af2ce5567078a5174551fb59af75b5b7f Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 20:13:06 +0300 Subject: [PATCH 07/38] Reduce code with wallet_impl --- rai/node/rpc.cpp | 1382 +++++++++++++++++----------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 513 insertions(+), 870 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 60d7ecce..bd949855 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -218,6 +218,29 @@ void rai::rpc_handler::response_errors () } } +std::shared_ptr rai::rpc_handler::wallet_impl () +{ + std::string wallet_text (request.get ("wallet")); + rai::uint256_union wallet; + if (!wallet.decode_hex (wallet_text)) + { + auto existing (node.wallets.items.find (wallet)); + if (existing != node.wallets.items.end ()) + { + return existing->second; + } + else + { + ec = nano::error_common::wallet_not_found; + } + } + else + { + ec = nano::error_common::bad_wallet_number; + } + return nullptr; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -287,33 +310,20 @@ void rai::rpc_handler::account_create () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + const bool generate_work = request.get ("work", true); + rai::account new_key (wallet->deterministic_insert (generate_work)); + if (!new_key.is_zero ()) { - const bool generate_work = request.get ("work", true); - rai::account new_key (existing->second->deterministic_insert (generate_work)); - if (!new_key.is_zero ()) - { - response_l.put ("account", new_key.to_account ()); - } - else - { - ec = nano::error_common::wallet_locked; - } + response_l.put ("account", new_key.to_account ()); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -404,31 +414,18 @@ void rai::rpc_handler::account_key () void rai::rpc_handler::account_list () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + boost::property_tree::ptree accounts; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), j (wallet->store.end ()); i != j; ++i) { - boost::property_tree::ptree accounts; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), j (existing->second->store.end ()); i != j; ++i) - { - boost::property_tree::ptree entry; - entry.put ("", rai::uint256_union (i->first.uint256 ()).to_account ()); - accounts.push_back (std::make_pair ("", entry)); - } - response_l.add_child ("accounts", accounts); + boost::property_tree::ptree entry; + entry.put ("", rai::uint256_union (i->first.uint256 ()).to_account ()); + accounts.push_back (std::make_pair ("", entry)); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.add_child ("accounts", accounts); } response_errors (); } @@ -437,53 +434,39 @@ void rai::rpc_handler::account_move () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - std::string source_text (request.get ("source")); - auto accounts_text (request.get_child ("accounts")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string source_text (request.get ("source")); + auto accounts_text (request.get_child ("accounts")); + rai::uint256_union source; + if (!source.decode_hex (source_text)) { - auto wallet (existing->second); - rai::uint256_union source; - if (!source.decode_hex (source_text)) + auto existing (node.wallets.items.find (source)); + if (existing != node.wallets.items.end ()) { - auto existing (node.wallets.items.find (source)); - if (existing != node.wallets.items.end ()) + auto source (existing->second); + std::vector accounts; + for (auto i (accounts_text.begin ()), n (accounts_text.end ()); i != n; ++i) { - auto source (existing->second); - std::vector accounts; - for (auto i (accounts_text.begin ()), n (accounts_text.end ()); i != n; ++i) - { - rai::public_key account; - account.decode_hex (i->second.get ("")); - accounts.push_back (account); - } - rai::transaction transaction (node.store.environment, nullptr, true); - auto error (wallet->store.move (transaction, source->store, accounts)); - response_l.put ("moved", error ? "0" : "1"); - } - else - { - ec = nano::error_rpc::source_not_found; + rai::public_key account; + account.decode_hex (i->second.get ("")); + accounts.push_back (account); } + rai::transaction transaction (node.store.environment, nullptr, true); + auto error (wallet->store.move (transaction, source->store, accounts)); + response_l.put ("moved", error ? "0" : "1"); } else { - ec = nano::error_rpc::bad_source; + ec = nano::error_rpc::source_not_found; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_rpc::bad_source; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -496,51 +479,37 @@ void rai::rpc_handler::account_remove () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - std::string account_text (request.get ("account")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string account_text (request.get ("account")); + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - auto wallet (existing->second); - rai::transaction transaction (node.store.environment, nullptr, true); - if (existing->second->store.valid_password (transaction)) + rai::account account_id; + if (!account_id.decode_account (account_text)) { - rai::account account_id; - if (!account_id.decode_account (account_text)) + auto account (wallet->store.find (transaction, account_id)); + if (account != wallet->store.end ()) { - auto account (wallet->store.find (transaction, account_id)); - if (account != wallet->store.end ()) - { - wallet->store.erase (transaction, account_id); - response_l.put ("removed", "1"); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + wallet->store.erase (transaction, account_id); + response_l.put ("removed", "1"); } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } else { - ec = nano::error_common::wallet_locked; + ec = nano::error_common::bad_account_number; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -579,82 +548,75 @@ void rai::rpc_handler::account_representative_set () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string account_text (request.get ("account")); + rai::account account; + if (!account.decode_account (account_text)) { - auto wallet (existing->second); - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - auto work_error (rai::from_string_hex (work_text.get (), work)); - if (work_error) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - if (!ec && work) + } + if (!ec && work) + { + rai::transaction transaction (node.store.environment, nullptr, true); + rai::account_info info; + if (!node.store.account_get (transaction, account, info)) { - rai::transaction transaction (node.store.environment, nullptr, true); - rai::account_info info; - if (!node.store.account_get (transaction, account, info)) + if (!rai::work_validate (info.head, work)) { - if (!rai::work_validate (info.head, work)) - { - existing->second->store.work_put (transaction, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } + wallet->store.work_put (transaction, account, work); } else { - ec = nano::error_common::account_not_found; + ec = nano::error_common::invalid_work; } } - if (!ec) - { - auto response_a (response); - wallet->change_async (account, representative, [response_a](std::shared_ptr block) { - rai::block_hash hash (0); - if (block != nullptr) - { - hash = block->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - }, - work == 0); - } else { - response_errors (); + ec = nano::error_common::account_not_found; } } + if (!ec) + { + auto response_a (response); + wallet->change_async (account, representative, [response_a](std::shared_ptr block) { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + }, + work == 0); + } else { - ec = nano::error_rpc::bad_representative_number; + response_errors (); } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_rpc::bad_representative_number; } } + else + { + ec = nano::error_common::bad_account_number; + } } } else @@ -715,22 +677,18 @@ void rai::rpc_handler::accounts_create () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { uint64_t count; std::string count_text (request.get ("count")); if (!decode_unsigned (count_text, count) && count != 0) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { const bool generate_work = request.get ("work", false); boost::property_tree::ptree accounts; for (auto i (0); accounts.size () < count; ++i) { - rai::account new_key (existing->second->deterministic_insert (generate_work)); + rai::account new_key (wallet->deterministic_insert (generate_work)); if (!new_key.is_zero ()) { boost::property_tree::ptree entry; @@ -739,21 +697,12 @@ void rai::rpc_handler::accounts_create () } } response_l.add_child ("accounts", accounts); - } - else - { - ec = nano::error_common::wallet_not_found; - } } else { ec = nano::error_common::invalid_count; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -2053,26 +2002,13 @@ void rai::rpc_handler::password_change () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::transaction transaction (node.store.environment, nullptr, true); - std::string password_text (request.get ("password")); - auto error (existing->second->store.rekey (transaction, password_text)); - response_l.put ("changed", error ? "0" : "1"); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + rai::transaction transaction (node.store.environment, nullptr, true); + std::string password_text (request.get ("password")); + auto error (wallet->store.rekey (transaction, password_text)); + response_l.put ("changed", error ? "0" : "1"); } } else @@ -2084,58 +2020,32 @@ void rai::rpc_handler::password_change () void rai::rpc_handler::password_enter () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - std::string password_text (request.get ("password")); - auto error (existing->second->enter_password (password_text)); - response_l.put ("valid", error ? "0" : "1"); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + std::string password_text (request.get ("password")); + auto error (wallet->enter_password (password_text)); + response_l.put ("valid", error ? "0" : "1"); } response_errors (); } void rai::rpc_handler::password_valid (bool wallet_locked) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + rai::transaction transaction (node.store.environment, nullptr, false); + auto valid (wallet->store.valid_password (transaction)); + if (!wallet_locked) { - rai::transaction transaction (node.store.environment, nullptr, false); - auto valid (existing->second->store.valid_password (transaction)); - if (!wallet_locked) - { - response_l.put ("valid", valid ? "1" : "0"); - } - else - { - response_l.put ("locked", valid ? "0" : "1"); - } + response_l.put ("valid", valid ? "1" : "0"); } else { - ec = nano::error_common::wallet_not_found; + response_l.put ("locked", valid ? "0" : "1"); } } - else - { - ec = nano::error_common::bad_wallet_number; - } response_errors (); } @@ -2548,110 +2458,97 @@ void rai::rpc_handler::receive () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string account_text (request.get ("account")); + rai::account account; + if (!account.decode_account (account_text)) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - if (existing->second->store.find (transaction, account) != existing->second->store.end ()) + std::string hash_text (request.get ("block")); + rai::uint256_union hash; + if (!hash.decode_hex (hash_text)) { - std::string hash_text (request.get ("block")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) { - if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } - if (!ec && work) - { - rai::account_info info; - rai::uint256_union head; - if (!node.store.account_get (transaction, account, info)) - { - head = info.head; - } - else - { - head = account; - } - if (!rai::work_validate (head, work)) - { - rai::transaction transaction_a (node.store.environment, nullptr, true); - existing->second->store.work_put (transaction_a, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - if (!ec) - { - auto response_a (response); - existing->second->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { - rai::uint256_union hash_a (0); - if (block_a != nullptr) - { - hash_a = block_a->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash_a.to_string ()); - response_a (response_l); - }, - work == 0); + ec = nano::error_common::bad_work_format; } } - else + if (!ec && work) { - ec = nano::error_process::unreceivable; + rai::account_info info; + rai::uint256_union head; + if (!node.store.account_get (transaction, account, info)) + { + head = info.head; + } + else + { + head = account; + } + if (!rai::work_validate (head, work)) + { + rai::transaction transaction_a (node.store.environment, nullptr, true); + wallet->store.work_put (transaction_a, account, work); + } + else + { + ec = nano::error_common::invalid_work; + } + } + if (!ec) + { + auto response_a (response); + wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { + rai::uint256_union hash_a (0); + if (block_a != nullptr) + { + hash_a = block_a->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash_a.to_string ()); + response_a (response_l); + }, + work == 0); } } else { - ec = nano::error_blocks::not_found; + ec = nano::error_process::unreceivable; } } else { - ec = nano::error_blocks::bad_hash_number; + ec = nano::error_blocks::not_found; } } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_blocks::bad_hash_number; } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::bad_account_number; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -2880,20 +2777,11 @@ void rai::rpc_handler::search_pending () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - auto error (existing->second->search_pending ()); - response_l.put ("started", !error); - } - else - { - ec = nano::error_common::wallet_not_found; - } + auto error (wallet->search_pending ()); + response_l.put ("started", !error); } } else @@ -2921,111 +2809,98 @@ void rai::rpc_handler::send () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string source_text (request.get ("source")); + rai::account source; + if (!source.decode_account (source_text)) { - std::string source_text (request.get ("source")); - rai::account source; - if (!source.decode_account (source_text)) + std::string destination_text (request.get ("destination")); + rai::account destination; + if (!destination.decode_account (destination_text)) { - std::string destination_text (request.get ("destination")); - rai::account destination; - if (!destination.decode_account (destination_text)) + std::string amount_text (request.get ("amount")); + rai::amount amount; + if (!amount.decode_dec (amount_text)) { - std::string amount_text (request.get ("amount")); - rai::amount amount; - if (!amount.decode_dec (amount_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - rai::uint128_t balance (0); - if (!ec) + } + rai::uint128_t balance (0); + if (!ec) + { + rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 + rai::account_info info; + if (!node.store.account_get (transaction, source, info)) { - rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 - rai::account_info info; - if (!node.store.account_get (transaction, source, info)) + balance = (info.balance).number (); + } + else + { + ec = nano::error_common::account_not_found; + } + if (!ec && work) + { + if (!rai::work_validate (info.head, work)) { - balance = (info.balance).number (); + wallet->store.work_put (transaction, source, work); } else { - ec = nano::error_common::account_not_found; - } - if (!ec && work) - { - if (!rai::work_validate (info.head, work)) - { - existing->second->store.work_put (transaction, source, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - } - if (!ec) - { - boost::optional send_id (request.get_optional ("id")); - if (balance >= amount.number ()) - { - auto rpc_l (shared_from_this ()); - auto response_a (response); - existing->second->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { - if (block_a != nullptr) - { - rai::uint256_union hash (block_a->hash ()); - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - } - else - { - error_response (response_a, "Error generating block"); - } - }, - work == 0, send_id); - } - else - { - ec = nano::error_common::insufficient_balance; + ec = nano::error_common::invalid_work; } } } - else + if (!ec) { - ec = nano::error_common::invalid_amount; + boost::optional send_id (request.get_optional ("id")); + if (balance >= amount.number ()) + { + auto rpc_l (shared_from_this ()); + auto response_a (response); + wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { + if (block_a != nullptr) + { + rai::uint256_union hash (block_a->hash ()); + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + } + else + { + error_response (response_a, "Error generating block"); + } + }, + work == 0, send_id); + } + else + { + ec = nano::error_common::insufficient_balance; + } } } else { - ec = nano::error_rpc::bad_destination; + ec = nano::error_common::invalid_amount; } } else { - ec = nano::error_rpc::bad_source; + ec = nano::error_rpc::bad_destination; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_rpc::bad_source; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -3221,36 +3096,23 @@ void rai::rpc_handler::wallet_add () if (rpc.config.enable_control) { std::string key_text (request.get ("key")); - std::string wallet_text (request.get ("wallet")); rai::raw_key key; if (!key.data.decode_hex (key_text)) { - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + const bool generate_work = request.get ("work", true); + auto pub (wallet->insert_adhoc (key, generate_work)); + if (!pub.is_zero ()) { - const bool generate_work = request.get ("work", true); - auto pub (existing->second->insert_adhoc (key, generate_work)); - if (!pub.is_zero ()) - { - response_l.put ("account", pub.to_account ()); - } - else - { - ec = nano::error_common::wallet_locked; - } + response_l.put ("account", pub.to_account ()); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -3268,48 +3130,35 @@ void rai::rpc_handler::wallet_add_watch () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (existing->second->store.valid_password (transaction)) + for (auto & accounts : request.get_child ("accounts")) { - for (auto & accounts : request.get_child ("accounts")) + if (!ec) { - if (!ec) + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) - { - existing->second->insert_watch (transaction, account); - } - else - { - ec = nano::error_common::bad_account_number; - } + wallet->insert_watch (transaction, account); + } + else + { + ec = nano::error_common::bad_account_number; } } - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::wallet_locked; } + response_l.put ("success", ""); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -3320,42 +3169,28 @@ void rai::rpc_handler::wallet_add_watch () void rai::rpc_handler::wallet_balance_total () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + rai::uint128_t balance (0); + rai::uint128_t pending (0); + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - rai::uint128_t balance (0); - rai::uint128_t pending (0); - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) - { - rai::account account (i->first.uint256 ()); - balance = balance + node.ledger.account_balance (transaction, account); - pending = pending + node.ledger.account_pending (transaction, account); - } - response_l.put ("balance", balance.convert_to ()); - response_l.put ("pending", pending.convert_to ()); + rai::account account (i->first.uint256 ()); + balance = balance + node.ledger.account_balance (transaction, account); + pending = pending + node.ledger.account_pending (transaction, account); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.put ("balance", balance.convert_to ()); + response_l.put ("pending", pending.convert_to ()); } response_errors (); } void rai::rpc_handler::wallet_balances () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { rai::uint128_union threshold (0); boost::optional threshold_text (request.get_optional ("threshold")); @@ -3368,16 +3203,23 @@ void rai::rpc_handler::wallet_balances () } if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + boost::property_tree::ptree balances; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree balances; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + rai::uint128_t balance = node.ledger.account_balance (transaction, account); + if (threshold.is_zero ()) { - rai::account account (i->first.uint256 ()); - rai::uint128_t balance = node.ledger.account_balance (transaction, account); - if (threshold.is_zero ()) + boost::property_tree::ptree entry; + rai::uint128_t pending = node.ledger.account_pending (transaction, account); + entry.put ("balance", balance.convert_to ()); + entry.put ("pending", pending.convert_to ()); + balances.push_back (std::make_pair (account.to_account (), entry)); + } + else + { + if (balance >= threshold.number ()) { boost::property_tree::ptree entry; rai::uint128_t pending = node.ledger.account_pending (transaction, account); @@ -3385,24 +3227,9 @@ void rai::rpc_handler::wallet_balances () entry.put ("pending", pending.convert_to ()); balances.push_back (std::make_pair (account.to_account (), entry)); } - else - { - if (balance >= threshold.number ()) - { - boost::property_tree::ptree entry; - rai::uint128_t pending = node.ledger.account_pending (transaction, account); - entry.put ("balance", balance.convert_to ()); - entry.put ("pending", pending.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } - } } - response_l.add_child ("balances", balances); - } - else - { - ec = nano::error_common::wallet_not_found; } + response_l.add_child ("balances", balances); } } else @@ -3417,36 +3244,23 @@ void rai::rpc_handler::wallet_change_seed () if (rpc.config.enable_control) { std::string seed_text (request.get ("seed")); - std::string wallet_text (request.get ("wallet")); rai::raw_key seed; if (!seed.data.decode_hex (seed_text)) { - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (existing->second->store.valid_password (transaction)) - { - existing->second->change_seed (transaction, seed); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::wallet_locked; - } + wallet->change_seed (transaction, seed); + response_l.put ("success", ""); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -3463,28 +3277,15 @@ void rai::rpc_handler::wallet_change_seed () void rai::rpc_handler::wallet_contains () { std::string account_text (request.get ("account")); - std::string wallet_text (request.get ("wallet")); rai::uint256_union account; if (!account.decode_account (account_text)) { - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::transaction transaction (node.store.environment, nullptr, false); - auto exists (existing->second->store.find (transaction, account) != existing->second->store.end ()); - response_l.put ("exists", exists ? "1" : "0"); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + rai::transaction transaction (node.store.environment, nullptr, false); + auto exists (wallet->store.find (transaction, account) != wallet->store.end ()); + response_l.put ("exists", exists ? "1" : "0"); } } else @@ -3552,85 +3353,46 @@ void rai::rpc_handler::wallet_destroy () void rai::rpc_handler::wallet_export () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::transaction transaction (node.store.environment, nullptr, false); - std::string json; - existing->second->store.serialize_json (transaction, json); - response_l.put ("json", json); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_account_number; + rai::transaction transaction (node.store.environment, nullptr, false); + std::string json; + wallet->store.serialize_json (transaction, json); + response_l.put ("json", json); } response_errors (); } void rai::rpc_handler::wallet_frontiers () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + boost::property_tree::ptree frontiers; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree frontiers; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + auto latest (node.ledger.latest (transaction, account)); + if (!latest.is_zero ()) { - rai::account account (i->first.uint256 ()); - auto latest (node.ledger.latest (transaction, account)); - if (!latest.is_zero ()) - { - frontiers.put (account.to_account (), latest.to_string ()); - } + frontiers.put (account.to_account (), latest.to_string ()); } - response_l.add_child ("frontiers", frontiers); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.add_child ("frontiers", frontiers); } response_errors (); } void rai::rpc_handler::wallet_key_valid () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::transaction transaction (node.store.environment, nullptr, false); - auto valid (existing->second->store.valid_password (transaction)); - response_l.put ("valid", valid ? "1" : "0"); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + rai::transaction transaction (node.store.environment, nullptr, false); + auto valid (wallet->store.valid_password (transaction)); + response_l.put ("valid", valid ? "1" : "0"); } response_errors (); } @@ -3646,62 +3408,49 @@ void rai::rpc_handler::wallet_ledger () { modified_since = strtoul (modified_since_text.get ().c_str (), NULL, 10); } - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + boost::property_tree::ptree accounts; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree accounts; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + rai::account_info info; + if (!node.store.account_get (transaction, account, info)) { - rai::account account (i->first.uint256 ()); - rai::account_info info; - if (!node.store.account_get (transaction, account, info)) + if (info.modified >= modified_since) { - if (info.modified >= modified_since) + boost::property_tree::ptree entry; + entry.put ("frontier", info.head.to_string ()); + entry.put ("open_block", info.open_block.to_string ()); + entry.put ("representative_block", info.rep_block.to_string ()); + std::string balance; + rai::uint128_union (info.balance).encode_dec (balance); + entry.put ("balance", balance); + entry.put ("modified_timestamp", std::to_string (info.modified)); + entry.put ("block_count", std::to_string (info.block_count)); + if (representative) { - boost::property_tree::ptree entry; - entry.put ("frontier", info.head.to_string ()); - entry.put ("open_block", info.open_block.to_string ()); - entry.put ("representative_block", info.rep_block.to_string ()); - std::string balance; - rai::uint128_union (info.balance).encode_dec (balance); - entry.put ("balance", balance); - entry.put ("modified_timestamp", std::to_string (info.modified)); - entry.put ("block_count", std::to_string (info.block_count)); - if (representative) - { - auto block (node.store.block_get (transaction, info.rep_block)); - assert (block != nullptr); - entry.put ("representative", block->representative ().to_account ()); - } - if (weight) - { - auto account_weight (node.ledger.weight (transaction, account)); - entry.put ("weight", account_weight.convert_to ()); - } - if (pending) - { - auto account_pending (node.ledger.account_pending (transaction, account)); - entry.put ("pending", account_pending.convert_to ()); - } - accounts.push_back (std::make_pair (account.to_account (), entry)); + auto block (node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + entry.put ("representative", block->representative ().to_account ()); } + if (weight) + { + auto account_weight (node.ledger.weight (transaction, account)); + entry.put ("weight", account_weight.convert_to ()); + } + if (pending) + { + auto account_pending (node.ledger.account_pending (transaction, account)); + entry.put ("pending", account_pending.convert_to ()); + } + accounts.push_back (std::make_pair (account.to_account (), entry)); } } - response_l.add_child ("accounts", accounts); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.add_child ("accounts", accounts); } response_errors (); } @@ -3710,26 +3459,13 @@ void rai::rpc_handler::wallet_lock () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::raw_key empty; - empty.data.clear (); - existing->second->store.password.value_set (empty); - response_l.put ("locked", "1"); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + rai::raw_key empty; + empty.data.clear (); + wallet->store.password.value_set (empty); + response_l.put ("locked", "1"); } } else @@ -3741,109 +3477,83 @@ void rai::rpc_handler::wallet_lock () void rai::rpc_handler::wallet_pending () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + uint64_t count (std::numeric_limits::max ()); + rai::uint128_union threshold (0); + boost::optional count_text (request.get_optional ("count")); + if (count_text.is_initialized ()) { - uint64_t count (std::numeric_limits::max ()); - rai::uint128_union threshold (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) + if (decode_unsigned (count_text.get (), count)) { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } + ec = nano::error_common::invalid_count; } - boost::optional threshold_text (request.get_optional ("threshold")); - if (!ec && threshold_text.is_initialized ()) + } + boost::optional threshold_text (request.get_optional ("threshold")); + if (!ec && threshold_text.is_initialized ()) + { + if (threshold.decode_dec (threshold_text.get ())) { - if (threshold.decode_dec (threshold_text.get ())) - { - ec = nano::error_common::bad_threshold; - } + ec = nano::error_common::bad_threshold; } - const bool source = request.get ("source", false); - if (!ec) + } + const bool source = request.get ("source", false); + if (!ec) + { + boost::property_tree::ptree pending; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) { - rai::account account (i->first.uint256 ()); - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) + rai::pending_key key (ii->first); + if (threshold.is_zero () && !source) { - rai::pending_key key (ii->first); - if (threshold.is_zero () && !source) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (ii->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (ii->second); - if (info.amount.number () >= threshold.number ()) + if (source) { - if (source) - { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - pending_tree.put ("source", info.source.to_account ()); - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); - } + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + pending_tree.put ("source", info.source.to_account ()); + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } } } - if (!peers_l.empty ()) - { - pending.add_child (account.to_account (), peers_l); - } } - response_l.add_child ("blocks", pending); + if (!peers_l.empty ()) + { + pending.add_child (account.to_account (), peers_l); + } } + response_l.add_child ("blocks", pending); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; } response_errors (); } void rai::rpc_handler::wallet_representative () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - rai::transaction transaction (node.store.environment, nullptr, false); - response_l.put ("representative", existing->second->store.representative (transaction).to_account ()); - } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_account_number; + rai::transaction transaction (node.store.environment, nullptr, false); + response_l.put ("representative", wallet->store.representative (transaction).to_account ()); } response_errors (); } @@ -3852,35 +3562,22 @@ void rai::rpc_handler::wallet_representative_set () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) - { - rai::transaction transaction (node.store.environment, nullptr, true); - existing->second->store.representative_set (transaction, representative); - response_l.put ("set", "1"); - } - else - { - ec = nano::error_rpc::bad_representative_number; - } + rai::transaction transaction (node.store.environment, nullptr, true); + wallet->store.representative_set (transaction, representative); + response_l.put ("set", "1"); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_rpc::bad_representative_number; } } - else - { - ec = nano::error_common::bad_account_number; - } } else { @@ -3893,58 +3590,44 @@ void rai::rpc_handler::wallet_republish () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + uint64_t count; + std::string count_text (request.get ("count")); + if (!decode_unsigned (count_text, count)) { - uint64_t count; - std::string count_text (request.get ("count")); - if (!decode_unsigned (count_text, count)) + boost::property_tree::ptree blocks; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree blocks; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + auto latest (node.ledger.latest (transaction, account)); + std::unique_ptr block; + std::vector hashes; + while (!latest.is_zero () && hashes.size () < count) { - rai::account account (i->first.uint256 ()); - auto latest (node.ledger.latest (transaction, account)); - std::unique_ptr block; - std::vector hashes; - while (!latest.is_zero () && hashes.size () < count) - { - hashes.push_back (latest); - block = node.store.block_get (transaction, latest); - latest = block->previous (); - } - std::reverse (hashes.begin (), hashes.end ()); - for (auto & hash : hashes) - { - block = node.store.block_get (transaction, hash); - node.network.republish_block (transaction, std::move (block)); - ; - boost::property_tree::ptree entry; - entry.put ("", hash.to_string ()); - blocks.push_back (std::make_pair ("", entry)); - } + hashes.push_back (latest); + block = node.store.block_get (transaction, latest); + latest = block->previous (); + } + std::reverse (hashes.begin (), hashes.end ()); + for (auto & hash : hashes) + { + block = node.store.block_get (transaction, hash); + node.network.republish_block (transaction, std::move (block)); + boost::property_tree::ptree entry; + entry.put ("", hash.to_string ()); + blocks.push_back (std::make_pair ("", entry)); } - response_l.add_child ("blocks", blocks); - } - else - { - ec = nano::error_common::invalid_count; } + response_l.add_child ("blocks", blocks); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::invalid_count; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -3957,32 +3640,19 @@ void rai::rpc_handler::wallet_work_get () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + boost::property_tree::ptree works; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree works; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) - { - rai::account account (i->first.uint256 ()); - uint64_t work (0); - auto error_work (existing->second->store.work_get (transaction, account, work)); - works.put (account.to_account (), rai::to_string_hex (work)); - } - response_l.add_child ("works", works); + rai::account account (i->first.uint256 ()); + uint64_t work (0); + auto error_work (wallet->store.work_get (transaction, account, work)); + works.put (account.to_account (), rai::to_string_hex (work)); } - else - { - ec = nano::error_common::wallet_not_found; - } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.add_child ("works", works); } } else @@ -4066,44 +3736,30 @@ void rai::rpc_handler::work_get () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string account_text (request.get ("account")); + rai::account account; + if (!account.decode_account (account_text)) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - auto account_check (existing->second->store.find (transaction, account)); - if (account_check != existing->second->store.end ()) - { - uint64_t work (0); - auto error_work (existing->second->store.work_get (transaction, account, work)); - response_l.put ("work", rai::to_string_hex (work)); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + uint64_t work (0); + auto error_work (wallet->store.work_get (transaction, account, work)); + response_l.put ("work", rai::to_string_hex (work)); } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::bad_account_number; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { @@ -4116,52 +3772,38 @@ void rai::rpc_handler::work_set () { if (rpc.config.enable_control) { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + auto wallet (wallet_impl ()); + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string account_text (request.get ("account")); + rai::account account; + if (!account.decode_account (account_text)) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, true); - auto account_check (existing->second->store.find (transaction, account)); - if (account_check != existing->second->store.end ()) + std::string work_text (request.get ("work")); + uint64_t work; + if (!rai::from_string_hex (work_text, work)) { - std::string work_text (request.get ("work")); - uint64_t work; - if (!rai::from_string_hex (work_text, work)) - { - existing->second->store.work_put (transaction, account, work); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::bad_work_format; - } + wallet->store.work_put (transaction, account, work); + response_l.put ("success", ""); } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_common::bad_work_format; } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::bad_account_number; } } - else - { - ec = nano::error_common::bad_wallet_number; - } } else { diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 739f9f5a..eafc6918 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -220,6 +220,7 @@ public: void response_errors (); std::error_code ec; boost::property_tree::ptree response_l; + std::shared_ptr wallet_impl (); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From da82b1c2b71c1de625786046c978eb005eb48856 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 20:34:55 +0300 Subject: [PATCH 08/38] Reduce code with account_impl --- rai/node/rpc.cpp | 403 +++++++++++++++++++---------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 164 insertions(+), 240 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index bd949855..43803782 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -220,27 +220,44 @@ void rai::rpc_handler::response_errors () std::shared_ptr rai::rpc_handler::wallet_impl () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - if (!wallet.decode_hex (wallet_text)) + if (!ec) { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) + std::string wallet_text (request.get ("wallet")); + rai::uint256_union wallet; + if (!wallet.decode_hex (wallet_text)) { - return existing->second; + auto existing (node.wallets.items.find (wallet)); + if (existing != node.wallets.items.end ()) + { + return existing->second; + } + else + { + ec = nano::error_common::wallet_not_found; + } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::bad_wallet_number; } } - else - { - ec = nano::error_common::bad_wallet_number; - } return nullptr; } +rai::account rai::rpc_handler::account_impl () +{ + rai::account result (0); + if (!ec) + { + std::string account_text (request.get ("account")); + if (result.decode_account (account_text)) + { + ec = nano::error_common::bad_account_number; + } + } + return result; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -267,26 +284,20 @@ bool decode_unsigned (std::string const & text, uint64_t & number) void rai::rpc_handler::account_balance () { - std::string account_text (request.get ("account")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { auto balance (node.balance_pending (account)); response_l.put ("balance", balance.first.convert_to ()); response_l.put ("pending", balance.second.convert_to ()); } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } void rai::rpc_handler::account_block_count () { - std::string account_text (request.get ("account")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); rai::account_info info; @@ -299,10 +310,6 @@ void rai::rpc_handler::account_block_count () ec = nano::error_common::account_not_found; } } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -349,9 +356,8 @@ void rai::rpc_handler::account_get () void rai::rpc_handler::account_info () { - std::string account_text (request.get ("account")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { const bool representative = request.get ("representative", false); const bool weight = request.get ("weight", false); @@ -390,25 +396,16 @@ void rai::rpc_handler::account_info () ec = nano::error_common::account_not_found; } } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } void rai::rpc_handler::account_key () { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { response_l.put ("key", account.to_string ()); } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -480,29 +477,20 @@ void rai::rpc_handler::account_remove () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto account (account_impl ()); if (!ec) { - std::string account_text (request.get ("account")); rai::transaction transaction (node.store.environment, nullptr, true); if (wallet->store.valid_password (transaction)) { - rai::account account_id; - if (!account_id.decode_account (account_text)) + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - auto account (wallet->store.find (transaction, account_id)); - if (account != wallet->store.end ()) - { - wallet->store.erase (transaction, account_id); - response_l.put ("removed", "1"); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + wallet->store.erase (transaction, account); + response_l.put ("removed", "1"); } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } else @@ -520,9 +508,8 @@ void rai::rpc_handler::account_remove () void rai::rpc_handler::account_representative () { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); rai::account_info info; @@ -537,10 +524,6 @@ void rai::rpc_handler::account_representative () ec = nano::error_common::account_not_found; } } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -549,73 +532,65 @@ void rai::rpc_handler::account_representative_set () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto account (account_impl ()); if (!ec) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - if (!ec && work) + } + if (!ec && work) + { + rai::transaction transaction (node.store.environment, nullptr, true); + rai::account_info info; + if (!node.store.account_get (transaction, account, info)) { - rai::transaction transaction (node.store.environment, nullptr, true); - rai::account_info info; - if (!node.store.account_get (transaction, account, info)) + if (!rai::work_validate (info.head, work)) { - if (!rai::work_validate (info.head, work)) - { - wallet->store.work_put (transaction, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } + wallet->store.work_put (transaction, account, work); } else { - ec = nano::error_common::account_not_found; + ec = nano::error_common::invalid_work; } } - if (!ec) - { - auto response_a (response); - wallet->change_async (account, representative, [response_a](std::shared_ptr block) { - rai::block_hash hash (0); - if (block != nullptr) - { - hash = block->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - }, - work == 0); - } else { - response_errors (); + ec = nano::error_common::account_not_found; } } + if (!ec) + { + auto response_a (response); + wallet->change_async (account, representative, [response_a](std::shared_ptr block) { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + }, + work == 0); + } else { - ec = nano::error_rpc::bad_representative_number; + response_errors (); } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_rpc::bad_representative_number; } } } @@ -632,17 +607,12 @@ void rai::rpc_handler::account_representative_set () void rai::rpc_handler::account_weight () { - std::string account_text (request.get ("account")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { auto balance (node.weight (account)); response_l.put ("weight", balance.convert_to ()); } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -1431,9 +1401,8 @@ void rai::rpc_handler::confirmation_history () void rai::rpc_handler::delegators () { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { boost::property_tree::ptree delegators; rai::transaction transaction (node.store.environment, nullptr, false); @@ -1451,18 +1420,13 @@ void rai::rpc_handler::delegators () } response_l.add_child ("delegators", delegators); } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } void rai::rpc_handler::delegators_count () { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { uint64_t count (0); rai::transaction transaction (node.store.environment, nullptr, false); @@ -1478,10 +1442,6 @@ void rai::rpc_handler::delegators_count () } response_l.put ("count", std::to_string (count)); } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -2065,9 +2025,8 @@ void rai::rpc_handler::peers () void rai::rpc_handler::pending () { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { uint64_t count (std::numeric_limits::max ()); rai::uint128_union threshold (0); @@ -2124,10 +2083,6 @@ void rai::rpc_handler::pending () response_l.add_child ("blocks", peers_l); } } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -2459,94 +2414,86 @@ void rai::rpc_handler::receive () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto account (account_impl ()); if (!ec) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - if (wallet->store.find (transaction, account) != wallet->store.end ()) + std::string hash_text (request.get ("block")); + rai::uint256_union hash; + if (!hash.decode_hex (hash_text)) { - std::string hash_text (request.get ("block")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) { - if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } - if (!ec && work) - { - rai::account_info info; - rai::uint256_union head; - if (!node.store.account_get (transaction, account, info)) - { - head = info.head; - } - else - { - head = account; - } - if (!rai::work_validate (head, work)) - { - rai::transaction transaction_a (node.store.environment, nullptr, true); - wallet->store.work_put (transaction_a, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - if (!ec) - { - auto response_a (response); - wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { - rai::uint256_union hash_a (0); - if (block_a != nullptr) - { - hash_a = block_a->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash_a.to_string ()); - response_a (response_l); - }, - work == 0); + ec = nano::error_common::bad_work_format; } } - else + if (!ec && work) { - ec = nano::error_process::unreceivable; + rai::account_info info; + rai::uint256_union head; + if (!node.store.account_get (transaction, account, info)) + { + head = info.head; + } + else + { + head = account; + } + if (!rai::work_validate (head, work)) + { + rai::transaction transaction_a (node.store.environment, nullptr, true); + wallet->store.work_put (transaction_a, account, work); + } + else + { + ec = nano::error_common::invalid_work; + } + } + if (!ec) + { + auto response_a (response); + wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { + rai::uint256_union hash_a (0); + if (block_a != nullptr) + { + hash_a = block_a->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash_a.to_string ()); + response_a (response_l); + }, + work == 0); } } else { - ec = nano::error_blocks::not_found; + ec = nano::error_process::unreceivable; } } else { - ec = nano::error_blocks::bad_hash_number; + ec = nano::error_blocks::not_found; } } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_blocks::bad_hash_number; } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } } @@ -3276,21 +3223,13 @@ void rai::rpc_handler::wallet_change_seed () void rai::rpc_handler::wallet_contains () { - std::string account_text (request.get ("account")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) - { - rai::transaction transaction (node.store.environment, nullptr, false); - auto exists (wallet->store.find (transaction, account) != wallet->store.end ()); - response_l.put ("exists", exists ? "1" : "0"); - } - } - else - { - ec = nano::error_common::bad_account_number; + rai::transaction transaction (node.store.environment, nullptr, false); + auto exists (wallet->store.find (transaction, account) != wallet->store.end ()); + response_l.put ("exists", exists ? "1" : "0"); } response_errors (); } @@ -3737,27 +3676,19 @@ void rai::rpc_handler::work_get () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto account (account_impl ()); if (!ec) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - if (wallet->store.find (transaction, account) != wallet->store.end ()) - { - uint64_t work (0); - auto error_work (wallet->store.work_get (transaction, account, work)); - response_l.put ("work", rai::to_string_hex (work)); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + uint64_t work (0); + auto error_work (wallet->store.work_get (transaction, account, work)); + response_l.put ("work", rai::to_string_hex (work)); } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } } @@ -3773,35 +3704,27 @@ void rai::rpc_handler::work_set () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto account (account_impl ()); if (!ec) { - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account (account_text)) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (wallet->store.find (transaction, account) != wallet->store.end ()) + std::string work_text (request.get ("work")); + uint64_t work; + if (!rai::from_string_hex (work_text, work)) { - std::string work_text (request.get ("work")); - uint64_t work; - if (!rai::from_string_hex (work_text, work)) - { - wallet->store.work_put (transaction, account, work); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::bad_work_format; - } + wallet->store.work_put (transaction, account, work); + response_l.put ("success", ""); } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_common::bad_work_format; } } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_common::account_not_found_wallet; } } } diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index eafc6918..b040bbf0 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -221,6 +221,7 @@ public: std::error_code ec; boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); + rai::account account_impl (); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From ed91aee98d7a165d10eea07ad6747b297e33eb7c Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 20:35:59 +0300 Subject: [PATCH 09/38] Formatting --- rai/node/rpc.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 43803782..bcea88a2 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -654,19 +654,19 @@ void rai::rpc_handler::accounts_create () std::string count_text (request.get ("count")); if (!decode_unsigned (count_text, count) && count != 0) { - const bool generate_work = request.get ("work", false); - boost::property_tree::ptree accounts; - for (auto i (0); accounts.size () < count; ++i) + const bool generate_work = request.get ("work", false); + boost::property_tree::ptree accounts; + for (auto i (0); accounts.size () < count; ++i) + { + rai::account new_key (wallet->deterministic_insert (generate_work)); + if (!new_key.is_zero ()) { - rai::account new_key (wallet->deterministic_insert (generate_work)); - if (!new_key.is_zero ()) - { - boost::property_tree::ptree entry; - entry.put ("", new_key.to_account ()); - accounts.push_back (std::make_pair ("", entry)); - } + boost::property_tree::ptree entry; + entry.put ("", new_key.to_account ()); + accounts.push_back (std::make_pair ("", entry)); } - response_l.add_child ("accounts", accounts); + } + response_l.add_child ("accounts", accounts); } else { From d9b1205d85bc0821cd835ed82c0c30e088896a03 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 20:57:32 +0300 Subject: [PATCH 10/38] Several errors --- rai/node/rpc.cpp | 60 +++++++++++------------------------------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index bcea88a2..571283f5 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1483,10 +1483,9 @@ void rai::rpc_handler::deterministic_key () void rai::rpc_handler::frontiers () { - std::string account_text (request.get ("account")); std::string count_text (request.get ("count")); - rai::account start; - if (!start.decode_account (account_text)) + auto start (account_impl ()); + if (!ec) { uint64_t count; if (!decode_unsigned (count_text, count)) @@ -1504,10 +1503,6 @@ void rai::rpc_handler::frontiers () ec = nano::error_common::invalid_count; } } - else - { - ec = nano::error_common::bad_account_number; - } response_errors (); } @@ -2218,61 +2213,38 @@ void rai::rpc_handler::payment_init () void rai::rpc_handler::payment_end () { - std::string id_text (request.get ("wallet")); - std::string account_text (request.get ("account")); - rai::uint256_union id; - if (!id.decode_hex (id_text)) + auto account (account_impl ()); + auto wallet (wallet_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); - auto existing (node.wallets.items.find (id)); - if (existing != node.wallets.items.end ()) + auto existing (wallet->store.find (transaction, account)); + if (existing != wallet->store.end ()) { - auto wallet (existing->second); - rai::account account; - if (!account.decode_account (account_text)) + if (node.ledger.account_balance (transaction, account).is_zero ()) { - auto existing (wallet->store.find (transaction, account)); - if (existing != wallet->store.end ()) - { - if (node.ledger.account_balance (transaction, account).is_zero ()) - { - wallet->free_accounts.insert (account); - response_l.put ("ended", "1"); - } - else - { - ec = nano::error_rpc::payment_account_balance; - } - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + wallet->free_accounts.insert (account); + response_l.put ("ended", "1"); } else { - ec = nano::error_common::bad_account_number; + ec = nano::error_rpc::payment_account_balance; } } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::account_not_found_wallet; } } - else - { - ec = nano::error_common::bad_wallet_number; - } response_errors (); } void rai::rpc_handler::payment_wait () { - std::string account_text (request.get ("account")); std::string amount_text (request.get ("amount")); std::string timeout_text (request.get ("timeout")); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { rai::uint128_union amount; if (!amount.decode_dec (amount_text)) @@ -2299,10 +2271,6 @@ void rai::rpc_handler::payment_wait () ec = nano::error_common::invalid_amount; } } - else - { - ec = nano::error_common::bad_account_number; - } if (ec) { response_errors (); From 1909498dd954b5294a47e12a4a67bdd69936f57e Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 21:13:24 +0300 Subject: [PATCH 11/38] Reduce code size with count_impl --- rai/node/rpc.cpp | 189 +++++++++++++++++++++-------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 84 insertions(+), 106 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 571283f5..84fd16e1 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,6 +258,20 @@ rai::account rai::rpc_handler::account_impl () return result; } +uint64_t rai::rpc_handler::count_impl () +{ + uint64_t result (0); + if (!ec) + { + std::string count_text (request.get ("count")); + if (decode_unsigned (count_text, result) || result == 0) + { + ec = nano::error_common::invalid_count; + } + } + return result; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -648,30 +662,22 @@ void rai::rpc_handler::accounts_create () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto count (count_impl ()); if (!ec) { - uint64_t count; - std::string count_text (request.get ("count")); - if (!decode_unsigned (count_text, count) && count != 0) + const bool generate_work = request.get ("work", false); + boost::property_tree::ptree accounts; + for (auto i (0); accounts.size () < count; ++i) { - const bool generate_work = request.get ("work", false); - boost::property_tree::ptree accounts; - for (auto i (0); accounts.size () < count; ++i) + rai::account new_key (wallet->deterministic_insert (generate_work)); + if (!new_key.is_zero ()) { - rai::account new_key (wallet->deterministic_insert (generate_work)); - if (!new_key.is_zero ()) - { - boost::property_tree::ptree entry; - entry.put ("", new_key.to_account ()); - accounts.push_back (std::make_pair ("", entry)); - } + boost::property_tree::ptree entry; + entry.put ("", new_key.to_account ()); + accounts.push_back (std::make_pair ("", entry)); } - response_l.add_child ("accounts", accounts); - } - else - { - ec = nano::error_common::invalid_count; } + response_l.add_child ("accounts", accounts); } } else @@ -1344,12 +1350,11 @@ void rai::rpc_handler::bootstrap_any () void rai::rpc_handler::chain (bool successors) { std::string block_text (request.get ("block")); - std::string count_text (request.get ("count")); rai::block_hash block; if (!block.decode_hex (block_text)) { - uint64_t count; - if (!decode_unsigned (count_text, count)) + auto count (count_impl ()); + if (!ec) { boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); @@ -1370,10 +1375,6 @@ void rai::rpc_handler::chain (bool successors) } response_l.add_child ("blocks", blocks); } - else - { - ec = nano::error_common::invalid_count; - } } else { @@ -1483,25 +1484,17 @@ void rai::rpc_handler::deterministic_key () void rai::rpc_handler::frontiers () { - std::string count_text (request.get ("count")); auto start (account_impl ()); + auto count (count_impl ()); if (!ec) { - uint64_t count; - if (!decode_unsigned (count_text, count)) + boost::property_tree::ptree frontiers; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && frontiers.size () < count; ++i) { - boost::property_tree::ptree frontiers; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && frontiers.size () < count; ++i) - { - frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second).head.to_string ()); - } - response_l.add_child ("frontiers", frontiers); - } - else - { - ec = nano::error_common::invalid_count; + frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second).head.to_string ()); } + response_l.add_child ("frontiers", frontiers); } response_errors (); } @@ -1649,7 +1642,6 @@ public: void rai::rpc_handler::account_history () { std::string account_text; - std::string count_text (request.get ("count")); bool output_raw (request.get_optional ("raw") == true); rai::block_hash hash; auto head_str (request.get_optional ("head")); @@ -1678,58 +1670,51 @@ void rai::rpc_handler::account_history () ec = nano::error_common::bad_account_number; } } + auto count (count_impl ()); if (!ec) { - uint64_t count; - if (!decode_unsigned (count_text, count)) + uint64_t offset = 0; + auto offset_text (request.get_optional ("offset")); + if (!offset_text || !decode_unsigned (*offset_text, offset)) { - uint64_t offset = 0; - auto offset_text (request.get_optional ("offset")); - if (!offset_text || !decode_unsigned (*offset_text, offset)) + boost::property_tree::ptree history; + response_l.put ("account", account_text); + auto block (node.store.block_get (transaction, hash)); + while (block != nullptr && count > 0) { - boost::property_tree::ptree history; - response_l.put ("account", account_text); - auto block (node.store.block_get (transaction, hash)); - while (block != nullptr && count > 0) + if (offset > 0) { - if (offset > 0) + --offset; + } + else + { + boost::property_tree::ptree entry; + history_visitor visitor (*this, output_raw, transaction, entry, hash); + block->visit (visitor); + if (!entry.empty ()) { - --offset; - } - else - { - boost::property_tree::ptree entry; - history_visitor visitor (*this, output_raw, transaction, entry, hash); - block->visit (visitor); - if (!entry.empty ()) + entry.put ("hash", hash.to_string ()); + if (output_raw) { - entry.put ("hash", hash.to_string ()); - if (output_raw) - { - entry.put ("work", rai::to_string_hex (block->block_work ())); - entry.put ("signature", block->block_signature ().to_string ()); - } - history.push_back (std::make_pair ("", entry)); + entry.put ("work", rai::to_string_hex (block->block_work ())); + entry.put ("signature", block->block_signature ().to_string ()); } - --count; + history.push_back (std::make_pair ("", entry)); } - hash = block->previous (); - block = node.store.block_get (transaction, hash); - } - response_l.add_child ("history", history); - if (!hash.is_zero ()) - { - response_l.put ("previous", hash.to_string ()); + --count; } + hash = block->previous (); + block = node.store.block_get (transaction, hash); } - else + response_l.add_child ("history", history); + if (!hash.is_zero ()) { - ec = nano::error_rpc::invalid_offset; + response_l.put ("previous", hash.to_string ()); } } else { - ec = nano::error_common::invalid_count; + ec = nano::error_rpc::invalid_offset; } } response_errors (); @@ -3498,42 +3483,34 @@ void rai::rpc_handler::wallet_republish () if (rpc.config.enable_control) { auto wallet (wallet_impl ()); + auto count (count_impl ()); if (!ec) { - uint64_t count; - std::string count_text (request.get ("count")); - if (!decode_unsigned (count_text, count)) + boost::property_tree::ptree blocks; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree blocks; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + auto latest (node.ledger.latest (transaction, account)); + std::unique_ptr block; + std::vector hashes; + while (!latest.is_zero () && hashes.size () < count) { - rai::account account (i->first.uint256 ()); - auto latest (node.ledger.latest (transaction, account)); - std::unique_ptr block; - std::vector hashes; - while (!latest.is_zero () && hashes.size () < count) - { - hashes.push_back (latest); - block = node.store.block_get (transaction, latest); - latest = block->previous (); - } - std::reverse (hashes.begin (), hashes.end ()); - for (auto & hash : hashes) - { - block = node.store.block_get (transaction, hash); - node.network.republish_block (transaction, std::move (block)); - boost::property_tree::ptree entry; - entry.put ("", hash.to_string ()); - blocks.push_back (std::make_pair ("", entry)); - } + hashes.push_back (latest); + block = node.store.block_get (transaction, latest); + latest = block->previous (); + } + std::reverse (hashes.begin (), hashes.end ()); + for (auto & hash : hashes) + { + block = node.store.block_get (transaction, hash); + node.network.republish_block (transaction, std::move (block)); + boost::property_tree::ptree entry; + entry.put ("", hash.to_string ()); + blocks.push_back (std::make_pair ("", entry)); } - response_l.add_child ("blocks", blocks); - } - else - { - ec = nano::error_common::invalid_count; } + response_l.add_child ("blocks", blocks); } } else diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index b040bbf0..5fafc6b3 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,6 +222,7 @@ public: boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); rai::account account_impl (); + uint64_t count_impl (); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From 2a2913930df7348c5fbc89cb22b618b70d740b87 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 21:25:54 +0300 Subject: [PATCH 12/38] count_optional_impl --- rai/node/rpc.cpp | 135 ++++++++++++++--------------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 42 insertions(+), 94 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 84fd16e1..49741e8d 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,19 +258,6 @@ rai::account rai::rpc_handler::account_impl () return result; } -uint64_t rai::rpc_handler::count_impl () -{ - uint64_t result (0); - if (!ec) - { - std::string count_text (request.get ("count")); - if (decode_unsigned (count_text, result) || result == 0) - { - ec = nano::error_common::invalid_count; - } - } - return result; -} namespace { @@ -296,6 +283,36 @@ bool decode_unsigned (std::string const & text, uint64_t & number) } } +uint64_t rai::rpc_handler::count_impl () +{ + uint64_t result (0); + if (!ec) + { + std::string count_text (request.get ("count")); + if (decode_unsigned (count_text, result) || result == 0) + { + ec = nano::error_common::invalid_count; + } + } + return result; +} + +uint64_t rai::rpc_handler::count_optional_impl (uint64_t result) +{ + if (!ec) + { + boost::optional count_text (request.get_optional ("count")); + if (count_text.is_initialized ()) + { + if (decode_unsigned (count_text.get (), result)) + { + ec = nano::error_common::invalid_count; + } + } + } + return result; +} + void rai::rpc_handler::account_balance () { auto account (account_impl ()); @@ -717,16 +734,8 @@ void rai::rpc_handler::accounts_frontiers () void rai::rpc_handler::accounts_pending () { - uint64_t count (std::numeric_limits::max ()); + auto count (count_optional_impl ()); rai::uint128_union threshold (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } boost::optional threshold_text (request.get_optional ("threshold")); if (!ec && threshold_text.is_initialized ()) { @@ -1659,16 +1668,11 @@ void rai::rpc_handler::account_history () } else { - account_text = request.get ("account"); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl ()); + if (!ec) { hash = node.ledger.latest (transaction, account); } - else - { - ec = nano::error_common::bad_account_number; - } } auto count (count_impl ()); if (!ec) @@ -1777,7 +1781,7 @@ void rai::rpc_handler::ledger () if (rpc.config.enable_control) { rai::account start (0); - uint64_t count (std::numeric_limits::max ()); + auto count (count_optional_impl ()); boost::optional account_text (request.get_optional ("account")); if (account_text.is_initialized ()) { @@ -1786,14 +1790,6 @@ void rai::rpc_handler::ledger () ec = nano::error_common::bad_account_number; } } - boost::optional count_text (request.get_optional ("count")); - if (!ec && count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } uint64_t modified_since (0); boost::optional modified_since_text (request.get_optional ("modified_since")); if (modified_since_text.is_initialized ()) @@ -2008,16 +2004,8 @@ void rai::rpc_handler::pending () auto account (account_impl ()); if (!ec) { - uint64_t count (std::numeric_limits::max ()); + auto count (count_optional_impl ()); rai::uint128_union threshold (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } boost::optional threshold_text (request.get_optional ("threshold")); if (!ec && threshold_text.is_initialized ()) { @@ -2499,15 +2487,7 @@ void rai::rpc_handler::receive_minimum_set () void rai::rpc_handler::representatives () { - uint64_t count (std::numeric_limits::max ()); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } + auto count (count_optional_impl ()); if (!ec) { const bool sorting = request.get ("sorting", false); @@ -2557,17 +2537,9 @@ void rai::rpc_handler::representatives_online () void rai::rpc_handler::republish () { - uint64_t count (1024U); + auto count (count_optional_impl (1024U)); uint64_t sources (0); uint64_t destinations (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } boost::optional sources_text (request.get_optional ("sources")); if (!ec && sources_text.is_initialized ()) { @@ -2859,15 +2831,7 @@ void rai::rpc_handler::stop () void rai::rpc_handler::unchecked () { - uint64_t count (std::numeric_limits::max ()); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } + auto count (count_optional_impl ()); if (!ec) { boost::property_tree::ptree unchecked; @@ -2904,8 +2868,7 @@ void rai::rpc_handler::unchecked_get () { std::string hash_text (request.get ("hash")); rai::uint256_union hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + if (!hash.decode_hex (hash_text)) { boost::property_tree::ptree response_l; rai::transaction transaction (node.store.environment, nullptr, false); @@ -2935,16 +2898,8 @@ void rai::rpc_handler::unchecked_get () void rai::rpc_handler::unchecked_keys () { - uint64_t count (std::numeric_limits::max ()); + auto count (count_optional_impl ()); rai::uint256_union key (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } boost::optional hash_text (request.get_optional ("key")); if (!ec && hash_text.is_initialized ()) { @@ -3372,16 +3327,8 @@ void rai::rpc_handler::wallet_pending () auto wallet (wallet_impl ()); if (!ec) { - uint64_t count (std::numeric_limits::max ()); + auto count (count_optional_impl ()); rai::uint128_union threshold (0); - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) - { - if (decode_unsigned (count_text.get (), count)) - { - ec = nano::error_common::invalid_count; - } - } boost::optional threshold_text (request.get_optional ("threshold")); if (!ec && threshold_text.is_initialized ()) { diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 5fafc6b3..58480203 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -223,6 +223,7 @@ public: std::shared_ptr wallet_impl (); rai::account account_impl (); uint64_t count_impl (); + uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From 45a2e1bdaf208ec5dee29bbd7ae8f47ceae1777b Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 13 Jul 2018 21:46:22 +0300 Subject: [PATCH 13/38] Formatting --- rai/node/rpc.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 49741e8d..3afbc997 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,7 +258,6 @@ rai::account rai::rpc_handler::account_impl () return result; } - namespace { bool decode_unsigned (std::string const & text, uint64_t & number) From 05447ef7e0b35a4c0a098158be85b4fdbe9cf2c2 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Sun, 15 Jul 2018 15:29:03 +0300 Subject: [PATCH 14/38] Formatting --- rai/node/rpc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index dfa03d3c..e1d41fda 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -2023,7 +2023,7 @@ void rai::rpc_handler::pending () } } const bool source = request.get ("source", false); - const bool min_version = request.get ("min_version", false); + const bool min_version = request.get ("min_version", false); if (!ec) { boost::property_tree::ptree peers_l; @@ -3354,7 +3354,7 @@ void rai::rpc_handler::wallet_pending () } } const bool source = request.get ("source", false); - const bool min_version = request.get ("min_version", false); + const bool min_version = request.get ("min_version", false); if (!ec) { boost::property_tree::ptree pending; From e90112c8dbfda8a36f620168e3ac700ff4ede478 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 22:46:08 +0300 Subject: [PATCH 15/38] hash_impl --- rai/node/rpc.cpp | 99 ++++++++++++++++-------------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 34 insertions(+), 66 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index e1d41fda..73fe9862 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,6 +258,20 @@ rai::account rai::rpc_handler::account_impl () return result; } +rai::block_hash rai::rpc_handler::hash_impl () +{ + rai::block_hash result (0); + if (!ec) + { + std::string hash_text (request.get ("hash")); + if (result.decode_hex (hash_text)) + { + ec = nano::error_blocks::invalid_block_hash; + } + } + return result; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -810,9 +824,8 @@ void rai::rpc_handler::available_supply () void rai::rpc_handler::block () { - std::string hash_text (request.get ("hash")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); auto block (node.store.block_get (transaction, hash)); @@ -827,21 +840,16 @@ void rai::rpc_handler::block () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } void rai::rpc_handler::block_confirm () { - std::string hash_text (request.get ("hash")); - rai::block_hash hash_l; - if (!hash_l.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); - auto block_l (node.store.block_get (transaction, hash_l)); + auto block_l (node.store.block_get (transaction, hash)); if (block_l != nullptr) { node.block_confirm (std::move (block_l)); @@ -852,10 +860,6 @@ void rai::rpc_handler::block_confirm () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } @@ -969,9 +973,8 @@ void rai::rpc_handler::blocks_info () void rai::rpc_handler::block_account () { - std::string hash_text (request.get ("hash")); - rai::block_hash hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); if (node.store.block_exists (transaction, hash)) @@ -984,10 +987,6 @@ void rai::rpc_handler::block_account () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } @@ -2072,9 +2071,8 @@ void rai::rpc_handler::pending () void rai::rpc_handler::pending_exists () { - std::string hash_text (request.get ("hash")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); auto block (node.store.block_get (transaction, hash)); @@ -2093,10 +2091,6 @@ void rai::rpc_handler::pending_exists () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } @@ -2572,9 +2566,8 @@ void rai::rpc_handler::republish () ec = nano::error_rpc::invalid_destinations; } } - std::string hash_text (request.get ("hash")); - rai::uint256_union hash; - if (!ec && !hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { boost::property_tree::ptree blocks; rai::transaction transaction (node.store.environment, nullptr, false); @@ -2654,10 +2647,6 @@ void rai::rpc_handler::republish () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } @@ -2882,11 +2871,9 @@ void rai::rpc_handler::unchecked_clear () void rai::rpc_handler::unchecked_get () { - std::string hash_text (request.get ("hash")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { - boost::property_tree::ptree response_l; rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (node.store.unchecked_begin (transaction)), n (node.store.unchecked_end ()); i != n; ++i) { @@ -2905,10 +2892,6 @@ void rai::rpc_handler::unchecked_get () ec = nano::error_blocks::not_found; } } - else - { - ec = nano::error_blocks::bad_hash_number; - } response_errors (); } @@ -3521,11 +3504,9 @@ void rai::rpc_handler::work_generate () { if (rpc.config.enable_control) { - std::string hash_text (request.get ("hash")); bool use_peers (request.get_optional ("use_peers") == true); - rai::block_hash hash; - auto error (hash.decode_hex (hash_text)); - if (!error) + auto hash (hash_impl ()); + if (!ec) { auto rpc_l (shared_from_this ()); auto callback = [rpc_l](boost::optional const & work_a) { @@ -3549,10 +3530,6 @@ void rai::rpc_handler::work_generate () node.work_generate (hash, callback); } } - else - { - ec = nano::error_blocks::invalid_block_hash; - } } else { @@ -3569,16 +3546,11 @@ void rai::rpc_handler::work_cancel () { if (rpc.config.enable_control) { - std::string hash_text (request.get ("hash")); - rai::block_hash hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { node.work.cancel (hash); } - else - { - ec = nano::error_blocks::invalid_block_hash; - } } else { @@ -3653,9 +3625,8 @@ void rai::rpc_handler::work_set () void rai::rpc_handler::work_validate () { - std::string hash_text (request.get ("hash")); - rai::block_hash hash; - if (!hash.decode_hex (hash_text)) + auto hash (hash_impl ()); + if (!ec) { std::string work_text (request.get ("work")); uint64_t work; @@ -3670,10 +3641,6 @@ void rai::rpc_handler::work_validate () ec = nano::error_common::bad_work_format; } } - else - { - ec = nano::error_blocks::invalid_block_hash; - } response_errors (); } diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 58480203..fd3f5af8 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,6 +222,7 @@ public: boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); rai::account account_impl (); + rai::block_hash hash_impl (); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); }; From 5b9c4ed45b6d704cfca25c725c636db202bff7e5 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 22:58:51 +0300 Subject: [PATCH 16/38] Edit hash_impl to accept "block" --- rai/node/rpc.cpp | 146 +++++++++++++++++++++-------------------------- rai/node/rpc.hpp | 2 +- 2 files changed, 66 insertions(+), 82 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 73fe9862..c2c26d79 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,12 +258,12 @@ rai::account rai::rpc_handler::account_impl () return result; } -rai::block_hash rai::rpc_handler::hash_impl () +rai::block_hash rai::rpc_handler::hash_impl (std::string search_text) { rai::block_hash result (0); if (!ec) { - std::string hash_text (request.get ("hash")); + std::string hash_text (request.get (search_text)); if (result.decode_hex (hash_text)) { ec = nano::error_blocks::invalid_block_hash; @@ -1357,36 +1357,28 @@ void rai::rpc_handler::bootstrap_any () void rai::rpc_handler::chain (bool successors) { - std::string block_text (request.get ("block")); - rai::block_hash block; - if (!block.decode_hex (block_text)) + auto hash (hash_impl ("block")); + auto count (count_impl ()); + if (!ec) { - auto count (count_impl ()); - if (!ec) + boost::property_tree::ptree blocks; + rai::transaction transaction (node.store.environment, nullptr, false); + while (!hash.is_zero () && blocks.size () < count) { - boost::property_tree::ptree blocks; - rai::transaction transaction (node.store.environment, nullptr, false); - while (!block.is_zero () && blocks.size () < count) + auto block_l (node.store.block_get (transaction, hash)); + if (block_l != nullptr) { - auto block_l (node.store.block_get (transaction, block)); - if (block_l != nullptr) - { - boost::property_tree::ptree entry; - entry.put ("", block.to_string ()); - blocks.push_back (std::make_pair ("", entry)); - block = successors ? node.store.block_successor (transaction, block) : block_l->previous (); - } - else - { - block.clear (); - } + boost::property_tree::ptree entry; + entry.put ("", hash.to_string ()); + blocks.push_back (std::make_pair ("", entry)); + hash = successors ? node.store.block_successor (transaction, hash) : block_l->previous (); + } + else + { + hash.clear (); } - response_l.add_child ("blocks", blocks); } - } - else - { - ec = nano::error_blocks::bad_hash_number; + response_l.add_child ("blocks", blocks); } response_errors (); } @@ -2366,80 +2358,72 @@ void rai::rpc_handler::receive () { auto wallet (wallet_impl ()); auto account (account_impl ()); + auto hash (hash_impl ("block")); if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); if (wallet->store.find (transaction, account) != wallet->store.end ()) { - std::string hash_text (request.get ("block")); - rai::uint256_union hash; - if (!hash.decode_hex (hash_text)) + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) { - if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } - if (!ec && work) - { - rai::account_info info; - rai::uint256_union head; - if (!node.store.account_get (transaction, account, info)) - { - head = info.head; - } - else - { - head = account; - } - if (!rai::work_validate (head, work)) - { - rai::transaction transaction_a (node.store.environment, nullptr, true); - wallet->store.work_put (transaction_a, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - if (!ec) - { - auto response_a (response); - wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { - rai::uint256_union hash_a (0); - if (block_a != nullptr) - { - hash_a = block_a->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash_a.to_string ()); - response_a (response_l); - }, - work == 0); + ec = nano::error_common::bad_work_format; } } - else + if (!ec && work) { - ec = nano::error_process::unreceivable; + rai::account_info info; + rai::uint256_union head; + if (!node.store.account_get (transaction, account, info)) + { + head = info.head; + } + else + { + head = account; + } + if (!rai::work_validate (head, work)) + { + rai::transaction transaction_a (node.store.environment, nullptr, true); + wallet->store.work_put (transaction_a, account, work); + } + else + { + ec = nano::error_common::invalid_work; + } + } + if (!ec) + { + auto response_a (response); + wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { + rai::uint256_union hash_a (0); + if (block_a != nullptr) + { + hash_a = block_a->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash_a.to_string ()); + response_a (response_l); + }, + work == 0); } } else { - ec = nano::error_blocks::not_found; + ec = nano::error_process::unreceivable; } } else { - ec = nano::error_blocks::bad_hash_number; + ec = nano::error_blocks::not_found; } } else diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index fd3f5af8..b5b0c174 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,7 +222,7 @@ public: boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); rai::account account_impl (); - rai::block_hash hash_impl (); + rai::block_hash hash_impl (std::string = "hash"); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); }; From 68756bc26837358391fdcda2a734b152bc5bafff Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 23:18:09 +0300 Subject: [PATCH 17/38] rpc_control_impl --- rai/node/rpc.cpp | 1000 ++++++++++++++++++++-------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 430 insertions(+), 571 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index c2c26d79..8e23f378 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -326,6 +326,23 @@ uint64_t rai::rpc_handler::count_optional_impl (uint64_t result) return result; } +bool rai::rpc_handler::rpc_control_impl () +{ + bool result (false); + if (!ec) + { + if (!rpc.config.enable_control) + { + ec = nano::error_rpc::rpc_control_disabled; + } + else + { + result = true; + } + } + return result; +} + void rai::rpc_handler::account_balance () { auto account (account_impl ()); @@ -359,26 +376,20 @@ void rai::rpc_handler::account_block_count () void rai::rpc_handler::account_create () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + const bool generate_work = request.get ("work", true); + rai::account new_key (wallet->deterministic_insert (generate_work)); + if (!new_key.is_zero ()) { - const bool generate_work = request.get ("work", true); - rai::account new_key (wallet->deterministic_insert (generate_work)); - if (!new_key.is_zero ()) - { - response_l.put ("account", new_key.to_account ()); - } - else - { - ec = nano::error_common::wallet_locked; - } + response_l.put ("account", new_key.to_account ()); + } + else + { + ec = nano::error_common::wallet_locked; } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; } response_errors (); } @@ -474,79 +485,67 @@ void rai::rpc_handler::account_list () void rai::rpc_handler::account_move () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + std::string source_text (request.get ("source")); + auto accounts_text (request.get_child ("accounts")); + rai::uint256_union source; + if (!source.decode_hex (source_text)) { - std::string source_text (request.get ("source")); - auto accounts_text (request.get_child ("accounts")); - rai::uint256_union source; - if (!source.decode_hex (source_text)) + auto existing (node.wallets.items.find (source)); + if (existing != node.wallets.items.end ()) { - auto existing (node.wallets.items.find (source)); - if (existing != node.wallets.items.end ()) + auto source (existing->second); + std::vector accounts; + for (auto i (accounts_text.begin ()), n (accounts_text.end ()); i != n; ++i) { - auto source (existing->second); - std::vector accounts; - for (auto i (accounts_text.begin ()), n (accounts_text.end ()); i != n; ++i) - { - rai::public_key account; - account.decode_hex (i->second.get ("")); - accounts.push_back (account); - } - rai::transaction transaction (node.store.environment, nullptr, true); - auto error (wallet->store.move (transaction, source->store, accounts)); - response_l.put ("moved", error ? "0" : "1"); - } - else - { - ec = nano::error_rpc::source_not_found; + rai::public_key account; + account.decode_hex (i->second.get ("")); + accounts.push_back (account); } + rai::transaction transaction (node.store.environment, nullptr, true); + auto error (wallet->store.move (transaction, source->store, accounts)); + response_l.put ("moved", error ? "0" : "1"); } else { - ec = nano::error_rpc::bad_source; + ec = nano::error_rpc::source_not_found; } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_rpc::bad_source; + } } response_errors (); } void rai::rpc_handler::account_remove () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto account (account_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto account (account_impl ()); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (wallet->store.valid_password (transaction)) + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - if (wallet->store.find (transaction, account) != wallet->store.end ()) - { - wallet->store.erase (transaction, account); - response_l.put ("removed", "1"); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + wallet->store.erase (transaction, account); + response_l.put ("removed", "1"); } else { - ec = nano::error_common::wallet_locked; + ec = nano::error_common::account_not_found_wallet; } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_common::wallet_locked; + } } response_errors (); } @@ -574,74 +573,68 @@ void rai::rpc_handler::account_representative () void rai::rpc_handler::account_representative_set () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto account (account_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto account (account_impl ()); - if (!ec) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - if (!ec && work) + } + if (!ec && work) + { + rai::transaction transaction (node.store.environment, nullptr, true); + rai::account_info info; + if (!node.store.account_get (transaction, account, info)) { - rai::transaction transaction (node.store.environment, nullptr, true); - rai::account_info info; - if (!node.store.account_get (transaction, account, info)) + if (!rai::work_validate (info.head, work)) { - if (!rai::work_validate (info.head, work)) - { - wallet->store.work_put (transaction, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } + wallet->store.work_put (transaction, account, work); } else { - ec = nano::error_common::account_not_found; + ec = nano::error_common::invalid_work; } } - if (!ec) - { - auto response_a (response); - wallet->change_async (account, representative, [response_a](std::shared_ptr block) { - rai::block_hash hash (0); - if (block != nullptr) - { - hash = block->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - }, - work == 0); - } else { - response_errors (); + ec = nano::error_common::account_not_found; } } + if (!ec) + { + auto response_a (response); + wallet->change_async (account, representative, [response_a](std::shared_ptr block) { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + }, + work == 0); + } else { - ec = nano::error_rpc::bad_representative_number; + response_errors (); } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_rpc::bad_representative_number; + } } // Because of change_async if (ec) @@ -690,30 +683,24 @@ void rai::rpc_handler::accounts_balances () void rai::rpc_handler::accounts_create () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto count (count_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto count (count_impl ()); - if (!ec) + const bool generate_work = request.get ("work", false); + boost::property_tree::ptree accounts; + for (auto i (0); accounts.size () < count; ++i) { - const bool generate_work = request.get ("work", false); - boost::property_tree::ptree accounts; - for (auto i (0); accounts.size () < count; ++i) + rai::account new_key (wallet->deterministic_insert (generate_work)); + if (!new_key.is_zero ()) { - rai::account new_key (wallet->deterministic_insert (generate_work)); - if (!new_key.is_zero ()) - { - boost::property_tree::ptree entry; - entry.put ("", new_key.to_account ()); - accounts.push_back (std::make_pair ("", entry)); - } + boost::property_tree::ptree entry; + entry.put ("", new_key.to_account ()); + accounts.push_back (std::make_pair ("", entry)); } - response_l.add_child ("accounts", accounts); } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + response_l.add_child ("accounts", accounts); } response_errors (); } @@ -1012,7 +999,8 @@ void rai::rpc_handler::block_count_type () void rai::rpc_handler::block_create () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { std::string type (request.get ("type")); rai::uint256_union wallet (0); @@ -1295,10 +1283,6 @@ void rai::rpc_handler::block_create () ec = nano::error_rpc::block_create_key_required; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -1725,7 +1709,8 @@ void rai::rpc_handler::account_history () void rai::rpc_handler::keepalive () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { std::string address_text (request.get ("address")); std::string port_text (request.get ("port")); @@ -1740,10 +1725,6 @@ void rai::rpc_handler::keepalive () ec = nano::error_common::invalid_port; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -1777,7 +1758,8 @@ void rai::rpc_handler::key_expand () void rai::rpc_handler::ledger () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { rai::account start (0); auto count (count_optional_impl ()); @@ -1887,10 +1869,6 @@ void rai::rpc_handler::ledger () } response_l.add_child ("accounts", accounts); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -1935,20 +1913,14 @@ void rai::rpc_handler::mrai_to_raw (rai::uint128_t ratio) void rai::rpc_handler::password_change () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) - { - rai::transaction transaction (node.store.environment, nullptr, true); - std::string password_text (request.get ("password")); - auto error (wallet->store.rekey (transaction, password_text)); - response_l.put ("changed", error ? "0" : "1"); - } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + rai::transaction transaction (node.store.environment, nullptr, true); + std::string password_text (request.get ("password")); + auto error (wallet->store.rekey (transaction, password_text)); + response_l.put ("changed", error ? "0" : "1"); } response_errors (); } @@ -2354,87 +2326,81 @@ void rai::rpc_handler::process () void rai::rpc_handler::receive () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto account (account_impl ()); + auto hash (hash_impl ("block")); + if (!ec) { - auto wallet (wallet_impl ()); - auto account (account_impl ()); - auto hash (hash_impl ("block")); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - if (wallet->store.find (transaction, account) != wallet->store.end ()) + auto block (node.store.block_get (transaction, hash)); + if (block != nullptr) { - auto block (node.store.block_get (transaction, hash)); - if (block != nullptr) + if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) { - if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } - if (!ec && work) - { - rai::account_info info; - rai::uint256_union head; - if (!node.store.account_get (transaction, account, info)) - { - head = info.head; - } - else - { - head = account; - } - if (!rai::work_validate (head, work)) - { - rai::transaction transaction_a (node.store.environment, nullptr, true); - wallet->store.work_put (transaction_a, account, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - if (!ec) - { - auto response_a (response); - wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { - rai::uint256_union hash_a (0); - if (block_a != nullptr) - { - hash_a = block_a->hash (); - } - boost::property_tree::ptree response_l; - response_l.put ("block", hash_a.to_string ()); - response_a (response_l); - }, - work == 0); + ec = nano::error_common::bad_work_format; } } - else + if (!ec && work) { - ec = nano::error_process::unreceivable; + rai::account_info info; + rai::uint256_union head; + if (!node.store.account_get (transaction, account, info)) + { + head = info.head; + } + else + { + head = account; + } + if (!rai::work_validate (head, work)) + { + rai::transaction transaction_a (node.store.environment, nullptr, true); + wallet->store.work_put (transaction_a, account, work); + } + else + { + ec = nano::error_common::invalid_work; + } + } + if (!ec) + { + auto response_a (response); + wallet->receive_async (std::move (block), account, rai::genesis_amount, [response_a](std::shared_ptr block_a) { + rai::uint256_union hash_a (0); + if (block_a != nullptr) + { + hash_a = block_a->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash_a.to_string ()); + response_a (response_l); + }, + work == 0); } } else { - ec = nano::error_blocks::not_found; + ec = nano::error_process::unreceivable; } } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_blocks::not_found; } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_common::account_not_found_wallet; + } } // Because of receive_async if (ec) @@ -2445,20 +2411,18 @@ void rai::rpc_handler::receive () void rai::rpc_handler::receive_minimum () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { response_l.put ("amount", node.config.receive_minimum.to_string_dec ()); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::receive_minimum_set () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { std::string amount_text (request.get ("amount")); rai::uint128_union amount; @@ -2472,10 +2436,6 @@ void rai::rpc_handler::receive_minimum_set () ec = nano::error_common::invalid_amount; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -2636,136 +2596,121 @@ void rai::rpc_handler::republish () void rai::rpc_handler::search_pending () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) - { - auto error (wallet->search_pending ()); - response_l.put ("started", !error); - } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + auto error (wallet->search_pending ()); + response_l.put ("started", !error); } response_errors (); } void rai::rpc_handler::search_pending_all () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { node.wallets.search_pending_all (); response_l.put ("success", ""); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::send () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + std::string source_text (request.get ("source")); + rai::account source; + if (!source.decode_account (source_text)) { - std::string source_text (request.get ("source")); - rai::account source; - if (!source.decode_account (source_text)) + std::string destination_text (request.get ("destination")); + rai::account destination; + if (!destination.decode_account (destination_text)) { - std::string destination_text (request.get ("destination")); - rai::account destination; - if (!destination.decode_account (destination_text)) + std::string amount_text (request.get ("amount")); + rai::amount amount; + if (!amount.decode_dec (amount_text)) { - std::string amount_text (request.get ("amount")); - rai::amount amount; - if (!amount.decode_dec (amount_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - rai::uint128_t balance (0); - if (!ec) + } + rai::uint128_t balance (0); + if (!ec) + { + rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 + rai::account_info info; + if (!node.store.account_get (transaction, source, info)) { - rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 - rai::account_info info; - if (!node.store.account_get (transaction, source, info)) + balance = (info.balance).number (); + } + else + { + ec = nano::error_common::account_not_found; + } + if (!ec && work) + { + if (!rai::work_validate (info.head, work)) { - balance = (info.balance).number (); + wallet->store.work_put (transaction, source, work); } else { - ec = nano::error_common::account_not_found; - } - if (!ec && work) - { - if (!rai::work_validate (info.head, work)) - { - wallet->store.work_put (transaction, source, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - } - if (!ec) - { - boost::optional send_id (request.get_optional ("id")); - if (balance >= amount.number ()) - { - auto rpc_l (shared_from_this ()); - auto response_a (response); - wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { - if (block_a != nullptr) - { - rai::uint256_union hash (block_a->hash ()); - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - } - else - { - error_response (response_a, "Error generating block"); - } - }, - work == 0, send_id); - } - else - { - ec = nano::error_common::insufficient_balance; + ec = nano::error_common::invalid_work; } } } - else + if (!ec) { - ec = nano::error_common::invalid_amount; + boost::optional send_id (request.get_optional ("id")); + if (balance >= amount.number ()) + { + auto rpc_l (shared_from_this ()); + auto response_a (response); + wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { + if (block_a != nullptr) + { + rai::uint256_union hash (block_a->hash ()); + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + } + else + { + error_response (response_a, "Error generating block"); + } + }, + work == 0, send_id); + } + else + { + ec = nano::error_common::insufficient_balance; + } } } else { - ec = nano::error_rpc::bad_destination; + ec = nano::error_common::invalid_amount; } } else { - ec = nano::error_rpc::bad_source; + ec = nano::error_rpc::bad_destination; } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_rpc::bad_source; + } } // Because of send_async if (ec) @@ -2802,14 +2747,11 @@ void rai::rpc_handler::stats () void rai::rpc_handler::stop () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { response_l.put ("success", ""); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); if (!ec) { @@ -2840,16 +2782,13 @@ void rai::rpc_handler::unchecked () void rai::rpc_handler::unchecked_clear () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, true); node.store.unchecked_clear (transaction); response_l.put ("success", ""); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -2931,25 +2870,23 @@ void rai::rpc_handler::validate_account_number () void rai::rpc_handler::wallet_add () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { std::string key_text (request.get ("key")); rai::raw_key key; if (!key.data.decode_hex (key_text)) { - auto wallet (wallet_impl ()); - if (!ec) + const bool generate_work = request.get ("work", true); + auto pub (wallet->insert_adhoc (key, generate_work)); + if (!pub.is_zero ()) { - const bool generate_work = request.get ("work", true); - auto pub (wallet->insert_adhoc (key, generate_work)); - if (!pub.is_zero ()) - { - response_l.put ("account", pub.to_account ()); - } - else - { - ec = nano::error_common::wallet_locked; - } + response_l.put ("account", pub.to_account ()); + } + else + { + ec = nano::error_common::wallet_locked; } } else @@ -2957,50 +2894,40 @@ void rai::rpc_handler::wallet_add () ec = nano::error_common::bad_private_key; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::wallet_add_watch () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (wallet->store.valid_password (transaction)) + for (auto & accounts : request.get_child ("accounts")) { - for (auto & accounts : request.get_child ("accounts")) + if (!ec) { - if (!ec) + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) - { - wallet->insert_watch (transaction, account); - } - else - { - ec = nano::error_common::bad_account_number; - } + wallet->insert_watch (transaction, account); + } + else + { + ec = nano::error_common::bad_account_number; } } - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::wallet_locked; } + response_l.put ("success", ""); + } + else + { + ec = nano::error_common::wallet_locked; } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; } response_errors (); } @@ -3079,25 +3006,23 @@ void rai::rpc_handler::wallet_balances () void rai::rpc_handler::wallet_change_seed () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { std::string seed_text (request.get ("seed")); rai::raw_key seed; if (!seed.data.decode_hex (seed_text)) { - auto wallet (wallet_impl ()); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.valid_password (transaction)) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (wallet->store.valid_password (transaction)) - { - wallet->change_seed (transaction, seed); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::wallet_locked; - } + wallet->change_seed (transaction, seed); + response_l.put ("success", ""); + } + else + { + ec = nano::error_common::wallet_locked; } } else @@ -3105,10 +3030,6 @@ void rai::rpc_handler::wallet_change_seed () ec = nano::error_common::bad_seed; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -3127,7 +3048,8 @@ void rai::rpc_handler::wallet_contains () void rai::rpc_handler::wallet_create () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { rai::keypair wallet_id; node.wallets.create (wallet_id.pub); @@ -3142,16 +3064,13 @@ void rai::rpc_handler::wallet_create () ec = nano::error_common::wallet_lmdb_max_dbs; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::wallet_destroy () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { std::string wallet_text (request.get ("wallet")); rai::uint256_union wallet; @@ -3174,10 +3093,6 @@ void rai::rpc_handler::wallet_destroy () ec = nano::error_common::bad_wallet_number; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } @@ -3287,20 +3202,14 @@ void rai::rpc_handler::wallet_ledger () void rai::rpc_handler::wallet_lock () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) - { - rai::raw_key empty; - empty.data.clear (); - wallet->store.password.value_set (empty); - response_l.put ("locked", "1"); - } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + rai::raw_key empty; + empty.data.clear (); + wallet->store.password.value_set (empty); + response_l.put ("locked", "1"); } response_errors (); } @@ -3390,134 +3299,110 @@ void rai::rpc_handler::wallet_representative () void rai::rpc_handler::wallet_representative_set () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + std::string representative_text (request.get ("representative")); + rai::account representative; + if (!representative.decode_account (representative_text)) { - std::string representative_text (request.get ("representative")); - rai::account representative; - if (!representative.decode_account (representative_text)) - { - rai::transaction transaction (node.store.environment, nullptr, true); - wallet->store.representative_set (transaction, representative); - response_l.put ("set", "1"); - } - else - { - ec = nano::error_rpc::bad_representative_number; - } + rai::transaction transaction (node.store.environment, nullptr, true); + wallet->store.representative_set (transaction, representative); + response_l.put ("set", "1"); + } + else + { + ec = nano::error_rpc::bad_representative_number; } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; } response_errors (); } void rai::rpc_handler::wallet_republish () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto count (count_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto count (count_impl ()); - if (!ec) + boost::property_tree::ptree blocks; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree blocks; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) + rai::account account (i->first.uint256 ()); + auto latest (node.ledger.latest (transaction, account)); + std::unique_ptr block; + std::vector hashes; + while (!latest.is_zero () && hashes.size () < count) { - rai::account account (i->first.uint256 ()); - auto latest (node.ledger.latest (transaction, account)); - std::unique_ptr block; - std::vector hashes; - while (!latest.is_zero () && hashes.size () < count) - { - hashes.push_back (latest); - block = node.store.block_get (transaction, latest); - latest = block->previous (); - } - std::reverse (hashes.begin (), hashes.end ()); - for (auto & hash : hashes) - { - block = node.store.block_get (transaction, hash); - node.network.republish_block (transaction, std::move (block)); - boost::property_tree::ptree entry; - entry.put ("", hash.to_string ()); - blocks.push_back (std::make_pair ("", entry)); - } + hashes.push_back (latest); + block = node.store.block_get (transaction, latest); + latest = block->previous (); + } + std::reverse (hashes.begin (), hashes.end ()); + for (auto & hash : hashes) + { + block = node.store.block_get (transaction, hash); + node.network.republish_block (transaction, std::move (block)); + boost::property_tree::ptree entry; + entry.put ("", hash.to_string ()); + blocks.push_back (std::make_pair ("", entry)); } - response_l.add_child ("blocks", blocks); } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + response_l.add_child ("blocks", blocks); } response_errors (); } void rai::rpc_handler::wallet_work_get () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - if (!ec) + boost::property_tree::ptree works; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - boost::property_tree::ptree works; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) - { - rai::account account (i->first.uint256 ()); - uint64_t work (0); - auto error_work (wallet->store.work_get (transaction, account, work)); - works.put (account.to_account (), rai::to_string_hex (work)); - } - response_l.add_child ("works", works); + rai::account account (i->first.uint256 ()); + uint64_t work (0); + auto error_work (wallet->store.work_get (transaction, account, work)); + works.put (account.to_account (), rai::to_string_hex (work)); } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + response_l.add_child ("works", works); } response_errors (); } void rai::rpc_handler::work_generate () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto hash (hash_impl ()); + if (!ec) { bool use_peers (request.get_optional ("use_peers") == true); - auto hash (hash_impl ()); - if (!ec) - { - auto rpc_l (shared_from_this ()); - auto callback = [rpc_l](boost::optional const & work_a) { - if (work_a) - { - boost::property_tree::ptree response_l; - response_l.put ("work", rai::to_string_hex (work_a.value ())); - rpc_l->response (response_l); - } - else - { - error_response (rpc_l->response, "Cancelled"); - } - }; - if (!use_peers) + auto rpc_l (shared_from_this ()); + auto callback = [rpc_l](boost::optional const & work_a) { + if (work_a) { - node.work.generate (hash, callback); + boost::property_tree::ptree response_l; + response_l.put ("work", rai::to_string_hex (work_a.value ())); + rpc_l->response (response_l); } else { - node.work_generate (hash, callback); + error_response (rpc_l->response, "Cancelled"); } + }; + if (!use_peers) + { + node.work.generate (hash, callback); + } + else + { + node.work_generate (hash, callback); } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; } // Because of callback if (ec) @@ -3528,81 +3413,63 @@ void rai::rpc_handler::work_generate () void rai::rpc_handler::work_cancel () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto hash (hash_impl ()); + if (!ec) { - auto hash (hash_impl ()); - if (!ec) - { - node.work.cancel (hash); - } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + node.work.cancel (hash); } response_errors (); } void rai::rpc_handler::work_get () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto account (account_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto account (account_impl ()); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, false); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, false); - if (wallet->store.find (transaction, account) != wallet->store.end ()) - { - uint64_t work (0); - auto error_work (wallet->store.work_get (transaction, account, work)); - response_l.put ("work", rai::to_string_hex (work)); - } - else - { - ec = nano::error_common::account_not_found_wallet; - } + uint64_t work (0); + auto error_work (wallet->store.work_get (transaction, account, work)); + response_l.put ("work", rai::to_string_hex (work)); + } + else + { + ec = nano::error_common::account_not_found_wallet; } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; } response_errors (); } void rai::rpc_handler::work_set () { - if (rpc.config.enable_control) + rpc_control_impl (); + auto wallet (wallet_impl ()); + auto account (account_impl ()); + if (!ec) { - auto wallet (wallet_impl ()); - auto account (account_impl ()); - if (!ec) + rai::transaction transaction (node.store.environment, nullptr, true); + if (wallet->store.find (transaction, account) != wallet->store.end ()) { - rai::transaction transaction (node.store.environment, nullptr, true); - if (wallet->store.find (transaction, account) != wallet->store.end ()) + std::string work_text (request.get ("work")); + uint64_t work; + if (!rai::from_string_hex (work_text, work)) { - std::string work_text (request.get ("work")); - uint64_t work; - if (!rai::from_string_hex (work_text, work)) - { - wallet->store.work_put (transaction, account, work); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::bad_work_format; - } + wallet->store.work_put (transaction, account, work); + response_l.put ("success", ""); } else { - ec = nano::error_common::account_not_found_wallet; + ec = nano::error_common::bad_work_format; } } - } - else - { - ec = nano::error_rpc::rpc_control_disabled; + else + { + ec = nano::error_common::account_not_found_wallet; + } } response_errors (); } @@ -3630,7 +3497,8 @@ void rai::rpc_handler::work_validate () void rai::rpc_handler::work_peer_add () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { std::string address_text = request.get ("address"); std::string port_text = request.get ("port"); @@ -3645,16 +3513,13 @@ void rai::rpc_handler::work_peer_add () ec = nano::error_common::invalid_port; } } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::work_peers () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { boost::property_tree::ptree work_peers_l; for (auto i (node.config.work_peers.begin ()), n (node.config.work_peers.end ()); i != n; ++i) @@ -3665,24 +3530,17 @@ void rai::rpc_handler::work_peers () } response_l.add_child ("work_peers", work_peers_l); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } void rai::rpc_handler::work_peers_clear () { - if (rpc.config.enable_control) + rpc_control_impl (); + if (!ec) { node.config.work_peers.clear (); response_l.put ("success", ""); } - else - { - ec = nano::error_rpc::rpc_control_disabled; - } response_errors (); } diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index b5b0c174..7b6d418c 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -225,6 +225,7 @@ public: rai::block_hash hash_impl (std::string = "hash"); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); + bool rpc_control (); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From 2c746f4c0b0ca9c28a2b5339334f9b6c9eb3cf51 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 23:28:04 +0300 Subject: [PATCH 18/38] amount_impl --- rai/node/rpc.cpp | 180 +++++++++++++++++++++-------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 81 insertions(+), 100 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 8e23f378..b61098e2 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -258,6 +258,20 @@ rai::account rai::rpc_handler::account_impl () return result; } +rai::amount rai::rpc_handler::amount_impl () +{ + rai::amount result (0); + if (!ec) + { + std::string amount_text (request.get ("amount")); + if (result.decode_dec (amount_text)) + { + ec = nano::error_common::invalid_amount; + } + } + return result; +} + rai::block_hash rai::rpc_handler::hash_impl (std::string search_text) { rai::block_hash result (0); @@ -1874,25 +1888,19 @@ void rai::rpc_handler::ledger () void rai::rpc_handler::mrai_from_raw (rai::uint128_t ratio) { - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) + auto amount (amount_impl ()); + if (!ec) { auto result (amount.number () / ratio); response_l.put ("amount", result.convert_to ()); } - else - { - ec = nano::error_common::invalid_amount; - } response_errors (); } void rai::rpc_handler::mrai_to_raw (rai::uint128_t ratio) { - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) + auto amount (amount_impl ()); + if (!ec) { auto result (amount.number () * ratio); if (result > amount.number ()) @@ -1904,10 +1912,6 @@ void rai::rpc_handler::mrai_to_raw (rai::uint128_t ratio) ec = nano::error_common::invalid_amount_big; } } - else - { - ec = nano::error_common::invalid_amount; - } response_errors (); } @@ -2188,34 +2192,26 @@ void rai::rpc_handler::payment_end () void rai::rpc_handler::payment_wait () { - std::string amount_text (request.get ("amount")); std::string timeout_text (request.get ("timeout")); auto account (account_impl ()); + auto amount (amount_impl ()); if (!ec) { - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) + uint64_t timeout; + if (!decode_unsigned (timeout_text, timeout)) { - uint64_t timeout; - if (!decode_unsigned (timeout_text, timeout)) { - { - auto observer (std::make_shared (response, rpc, account, amount)); - observer->start (timeout); - std::lock_guard lock (rpc.mutex); - assert (rpc.payment_observers.find (account) == rpc.payment_observers.end ()); - rpc.payment_observers[account] = observer; - } - rpc.observer_action (account); - } - else - { - ec = nano::error_rpc::bad_timeout; + auto observer (std::make_shared (response, rpc, account, amount)); + observer->start (timeout); + std::lock_guard lock (rpc.mutex); + assert (rpc.payment_observers.find (account) == rpc.payment_observers.end ()); + rpc.payment_observers[account] = observer; } + rpc.observer_action (account); } else { - ec = nano::error_common::invalid_amount; + ec = nano::error_rpc::bad_timeout; } } if (ec) @@ -2422,19 +2418,11 @@ void rai::rpc_handler::receive_minimum () void rai::rpc_handler::receive_minimum_set () { rpc_control_impl (); + auto amount (amount_impl ()); if (!ec) { - std::string amount_text (request.get ("amount")); - rai::uint128_union amount; - if (!amount.decode_dec (amount_text)) - { - node.config.receive_minimum = amount; - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::invalid_amount; - } + node.config.receive_minimum = amount; + response_l.put ("success", ""); } response_errors (); } @@ -2621,6 +2609,7 @@ void rai::rpc_handler::send () { rpc_control_impl (); auto wallet (wallet_impl ()); + auto amount (amount_impl ()); if (!ec) { std::string source_text (request.get ("source")); @@ -2631,75 +2620,66 @@ void rai::rpc_handler::send () rai::account destination; if (!destination.decode_account (destination_text)) { - std::string amount_text (request.get ("amount")); - rai::amount amount; - if (!amount.decode_dec (amount_text)) + uint64_t work (0); + boost::optional work_text (request.get_optional ("work")); + if (work_text.is_initialized ()) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) + if (rai::from_string_hex (work_text.get (), work)) { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } + ec = nano::error_common::bad_work_format; } - rai::uint128_t balance (0); - if (!ec) + } + rai::uint128_t balance (0); + if (!ec) + { + rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 + rai::account_info info; + if (!node.store.account_get (transaction, source, info)) { - rai::transaction transaction (node.store.environment, nullptr, work != 0); // false if no "work" in request, true if work > 0 - rai::account_info info; - if (!node.store.account_get (transaction, source, info)) + balance = (info.balance).number (); + } + else + { + ec = nano::error_common::account_not_found; + } + if (!ec && work) + { + if (!rai::work_validate (info.head, work)) { - balance = (info.balance).number (); + wallet->store.work_put (transaction, source, work); } else { - ec = nano::error_common::account_not_found; - } - if (!ec && work) - { - if (!rai::work_validate (info.head, work)) - { - wallet->store.work_put (transaction, source, work); - } - else - { - ec = nano::error_common::invalid_work; - } - } - } - if (!ec) - { - boost::optional send_id (request.get_optional ("id")); - if (balance >= amount.number ()) - { - auto rpc_l (shared_from_this ()); - auto response_a (response); - wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { - if (block_a != nullptr) - { - rai::uint256_union hash (block_a->hash ()); - boost::property_tree::ptree response_l; - response_l.put ("block", hash.to_string ()); - response_a (response_l); - } - else - { - error_response (response_a, "Error generating block"); - } - }, - work == 0, send_id); - } - else - { - ec = nano::error_common::insufficient_balance; + ec = nano::error_common::invalid_work; } } } - else + if (!ec) { - ec = nano::error_common::invalid_amount; + boost::optional send_id (request.get_optional ("id")); + if (balance >= amount.number ()) + { + auto rpc_l (shared_from_this ()); + auto response_a (response); + wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr block_a) { + if (block_a != nullptr) + { + rai::uint256_union hash (block_a->hash ()); + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + response_a (response_l); + } + else + { + error_response (response_a, "Error generating block"); + } + }, + work == 0, send_id); + } + else + { + ec = nano::error_common::insufficient_balance; + } } } else diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 7b6d418c..34bc15dd 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,6 +222,7 @@ public: boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); rai::account account_impl (); + rai::amount amount_impl (); rai::block_hash hash_impl (std::string = "hash"); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); From 9776e0a39ccac9b7e79837ed19587002550366a5 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 23:36:27 +0300 Subject: [PATCH 19/38] Doule write issue --- rai/node/rpc.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index b61098e2..c9329d18 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -640,10 +640,6 @@ void rai::rpc_handler::account_representative_set () }, work == 0); } - else - { - response_errors (); - } } else { From 06ca5920b1c1cb4fc6c0f91428189a7105ada26d Mon Sep 17 00:00:00 2001 From: SergiySW Date: Mon, 16 Jul 2018 23:37:15 +0300 Subject: [PATCH 20/38] Typo --- rai/node/rpc.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 34bc15dd..ca4eec24 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -226,7 +226,7 @@ public: rai::block_hash hash_impl (std::string = "hash"); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); - bool rpc_control (); + bool rpc_control_impl (); }; /** Returns the correct RPC implementation based on TLS configuration */ std::unique_ptr get_rpc (boost::asio::io_service & service_a, rai::node & node_a, rai::rpc_config const & config_a); From cfdb9eb0d5f82cd20bdaaaf8c377ce0caebc25af Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 17 Jul 2018 00:00:14 +0300 Subject: [PATCH 21/38] Typo --- rai/node/rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index c9329d18..a8dcd1e6 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1351,7 +1351,7 @@ void rai::rpc_handler::bootstrap_any () void rai::rpc_handler::chain (bool successors) { - auto hash (hash_impl ("block")); + auto hash (hash_impl ("block")); auto count (count_impl ()); if (!ec) { From 371b5fed82cd10684f763466d5425033be01c871 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 17 Jul 2018 19:29:38 +0300 Subject: [PATCH 22/38] threshold_optional_impl --- rai/node/rpc.cpp | 238 ++++++++++++++++++++--------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 103 insertions(+), 136 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index a8dcd1e6..8806035c 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -286,6 +286,20 @@ rai::block_hash rai::rpc_handler::hash_impl (std::string search_text) return result; } +rai::amount threshold_optional_impl () +{ + rai::amount result (0); + boost::optional threshold_text (request.get_optional ("threshold")); + if (!ec && threshold_text.is_initialized ()) + { + if (result.decode_dec (threshold_text.get ())) + { + ec = nano::error_common::bad_threshold; + } + } + return result; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -326,15 +340,12 @@ uint64_t rai::rpc_handler::count_impl () uint64_t rai::rpc_handler::count_optional_impl (uint64_t result) { - if (!ec) + boost::optional count_text (request.get_optional ("count")); + if (!ec && count_text.is_initialized ()) { - boost::optional count_text (request.get_optional ("count")); - if (count_text.is_initialized ()) + if (decode_unsigned (count_text.get (), result)) { - if (decode_unsigned (count_text.get (), result)) - { - ec = nano::error_common::invalid_count; - } + ec = nano::error_common::invalid_count; } } return result; @@ -746,15 +757,7 @@ void rai::rpc_handler::accounts_frontiers () void rai::rpc_handler::accounts_pending () { auto count (count_optional_impl ()); - rai::uint128_union threshold (0); - boost::optional threshold_text (request.get_optional ("threshold")); - if (!ec && threshold_text.is_initialized ()) - { - if (threshold.decode_dec (threshold_text.get ())) - { - ec = nano::error_common::bad_threshold; - } - } + auto threshold (threshold_optional_impl ()); const bool source = request.get ("source", false); boost::property_tree::ptree pending; rai::transaction transaction (node.store.environment, nullptr, false); @@ -1769,10 +1772,10 @@ void rai::rpc_handler::key_expand () void rai::rpc_handler::ledger () { rpc_control_impl (); + auto count (count_optional_impl ()); if (!ec) { rai::account start (0); - auto count (count_optional_impl ()); boost::optional account_text (request.get_optional ("account")); if (account_text.is_initialized ()) { @@ -1973,62 +1976,51 @@ void rai::rpc_handler::peers () void rai::rpc_handler::pending () { auto account (account_impl ()); + auto count (count_optional_impl ()); + auto threshold (threshold_optional_impl ()); + const bool source = request.get ("source", false); + const bool min_version = request.get ("min_version", false); if (!ec) { - auto count (count_optional_impl ()); - rai::uint128_union threshold (0); - boost::optional threshold_text (request.get_optional ("threshold")); - if (!ec && threshold_text.is_initialized ()) + boost::property_tree::ptree peers_l; + rai::transaction transaction (node.store.environment, nullptr, false); + rai::account end (account.number () + 1); + for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) { - if (threshold.decode_dec (threshold_text.get ())) + rai::pending_key key (i->first); + if (threshold.is_zero () && !source && !min_version) { - ec = nano::error_common::bad_threshold; + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); } - } - const bool source = request.get ("source", false); - const bool min_version = request.get ("min_version", false); - if (!ec) - { - boost::property_tree::ptree peers_l; - rai::transaction transaction (node.store.environment, nullptr, false); - rai::account end (account.number () + 1); - for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) + else { - rai::pending_key key (i->first); - if (threshold.is_zero () && !source && !min_version) + rai::pending_info info (i->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (i->second); - if (info.amount.number () >= threshold.number ()) + if (source || min_version) { - if (source || min_version) + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + if (source) { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - if (source) - { - pending_tree.put ("source", info.source.to_account ()); - } - if (min_version) - { - pending_tree.put ("min_version", std::to_string (info.min_version)); - } - peers_l.add_child (key.hash.to_string (), pending_tree); + pending_tree.put ("source", info.source.to_account ()); } - else + if (min_version) { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + pending_tree.put ("min_version", std::to_string (info.min_version)); } + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } } } - response_l.add_child ("blocks", peers_l); } + response_l.add_child ("blocks", peers_l); } response_errors (); } @@ -2931,26 +2923,26 @@ void rai::rpc_handler::wallet_balance_total () void rai::rpc_handler::wallet_balances () { auto wallet (wallet_impl ()); + auto threshold (threshold_optional_impl ()); if (!ec) { - rai::uint128_union threshold (0); - boost::optional threshold_text (request.get_optional ("threshold")); - if (threshold_text.is_initialized ()) + boost::property_tree::ptree balances; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - if (threshold.decode_dec (threshold_text.get ())) + rai::account account (i->first.uint256 ()); + rai::uint128_t balance = node.ledger.account_balance (transaction, account); + if (threshold.is_zero ()) { - ec = nano::error_common::bad_threshold; + boost::property_tree::ptree entry; + rai::uint128_t pending = node.ledger.account_pending (transaction, account); + entry.put ("balance", balance.convert_to ()); + entry.put ("pending", pending.convert_to ()); + balances.push_back (std::make_pair (account.to_account (), entry)); } - } - if (!ec) - { - boost::property_tree::ptree balances; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) + else { - rai::account account (i->first.uint256 ()); - rai::uint128_t balance = node.ledger.account_balance (transaction, account); - if (threshold.is_zero ()) + if (balance >= threshold.number ()) { boost::property_tree::ptree entry; rai::uint128_t pending = node.ledger.account_pending (transaction, account); @@ -2958,24 +2950,9 @@ void rai::rpc_handler::wallet_balances () entry.put ("pending", pending.convert_to ()); balances.push_back (std::make_pair (account.to_account (), entry)); } - else - { - if (balance >= threshold.number ()) - { - boost::property_tree::ptree entry; - rai::uint128_t pending = node.ledger.account_pending (transaction, account); - entry.put ("balance", balance.convert_to ()); - entry.put ("pending", pending.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } - } } - response_l.add_child ("balances", balances); } - } - else - { - ec = nano::error_common::bad_wallet_number; + response_l.add_child ("balances", balances); } response_errors (); } @@ -3193,71 +3170,60 @@ void rai::rpc_handler::wallet_lock () void rai::rpc_handler::wallet_pending () { auto wallet (wallet_impl ()); + auto count (count_optional_impl ()); + auto threshold (threshold_optional_impl ()); + const bool source = request.get ("source", false); + const bool min_version = request.get ("min_version", false); if (!ec) { - auto count (count_optional_impl ()); - rai::uint128_union threshold (0); - boost::optional threshold_text (request.get_optional ("threshold")); - if (!ec && threshold_text.is_initialized ()) + boost::property_tree::ptree pending; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) { - if (threshold.decode_dec (threshold_text.get ())) + rai::account account (i->first.uint256 ()); + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) { - ec = nano::error_common::bad_threshold; - } - } - const bool source = request.get ("source", false); - const bool min_version = request.get ("min_version", false); - if (!ec) - { - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) - { - rai::account account (i->first.uint256 ()); - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii) + rai::pending_key key (ii->first); + if (threshold.is_zero () && !source) { - rai::pending_key key (ii->first); - if (threshold.is_zero () && !source) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (ii->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (ii->second); - if (info.amount.number () >= threshold.number ()) + if (source || min_version) { - if (source || min_version) + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + if (source) { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - if (source) - { - pending_tree.put ("source", info.source.to_account ()); - } - if (min_version) - { - pending_tree.put ("min_version", std::to_string (info.min_version)); - } - peers_l.add_child (key.hash.to_string (), pending_tree); + pending_tree.put ("source", info.source.to_account ()); } - else + if (min_version) { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + pending_tree.put ("min_version", std::to_string (info.min_version)); } + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } } } - if (!peers_l.empty ()) - { - pending.add_child (account.to_account (), peers_l); - } } - response_l.add_child ("blocks", pending); + if (!peers_l.empty ()) + { + pending.add_child (account.to_account (), peers_l); + } } + response_l.add_child ("blocks", pending); } response_errors (); } diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index ca4eec24..fb076c35 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -224,6 +224,7 @@ public: rai::account account_impl (); rai::amount amount_impl (); rai::block_hash hash_impl (std::string = "hash"); + rai::amount threshold_optional_impl (); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); bool rpc_control_impl (); From 73ec18529944da454f941ec08d4844ce1e7245bc Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 17 Jul 2018 19:31:07 +0300 Subject: [PATCH 23/38] Simplification --- rai/node/rpc.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 8806035c..ccca2ef1 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -3423,8 +3423,7 @@ void rai::rpc_handler::work_validate () { std::string work_text (request.get ("work")); uint64_t work; - auto work_error (rai::from_string_hex (work_text, work)); - if (!work_error) + if (!rai::from_string_hex (work_text, work)) { auto validate (rai::work_validate (hash, work)); response_l.put ("valid", validate ? "0" : "1"); From 935f98caeb0ef9f003f8cc8421fe26a4540f1ef9 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 17 Jul 2018 19:33:18 +0300 Subject: [PATCH 24/38] Simplification --- rai/node/rpc.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index ccca2ef1..e37856ae 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1090,11 +1090,9 @@ void rai::rpc_handler::block_create () if (existing != node.wallets.items.end ()) { rai::transaction transaction (node.store.environment, nullptr, false); - auto unlock_check (existing->second->store.valid_password (transaction)); - if (unlock_check) + if (existing->second->store.valid_password (transaction)) { - auto account_check (existing->second->store.find (transaction, account)); - if (account_check != existing->second->store.end ()) + if (existing->second->store.find (transaction, account) != existing->second->store.end ()) { existing->second->store.fetch (transaction, account, prv); previous = node.ledger.latest (transaction, account); From b44a1bb9ea7207d47e223f42ff25494a030e3366 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 17 Jul 2018 20:17:55 +0300 Subject: [PATCH 25/38] Fix --- rai/node/rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index e37856ae..ddf8543b 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -286,7 +286,7 @@ rai::block_hash rai::rpc_handler::hash_impl (std::string search_text) return result; } -rai::amount threshold_optional_impl () +rai::amount rai::rpc_handler::threshold_optional_impl () { rai::amount result (0); boost::optional threshold_text (request.get_optional ("threshold")); From 79a0a120298a3c21ac670b64102d12c92262a258 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Wed, 18 Jul 2018 00:18:14 +0300 Subject: [PATCH 26/38] work_optional_impl --- rai/node/rpc.cpp | 111 ++++++++++++++--------------------------------- rai/node/rpc.hpp | 1 + 2 files changed, 33 insertions(+), 79 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index ddf8543b..7a8c89ba 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -300,6 +300,20 @@ rai::amount rai::rpc_handler::threshold_optional_impl () return result; } +uint64_t rai::rpc_handler::work_optional_impl () +{ + uint64_t result (0); + boost::optional work_text (request.get_optional ("work")); + if (!ec && work_text.is_initialized ()) + { + if (rai::from_string_hex (work_text.get (), result)) + { + ec = nano::error_common::bad_work_format; + } + } + return result; +} + namespace { bool decode_unsigned (std::string const & text, uint64_t & number) @@ -601,21 +615,14 @@ void rai::rpc_handler::account_representative_set () rpc_control_impl (); auto wallet (wallet_impl ()); auto account (account_impl ()); + auto work (work_optional_impl ()); if (!ec) { std::string representative_text (request.get ("representative")); rai::account representative; if (!representative.decode_account (representative_text)) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) - { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } + auto work (work_optional_impl ()); if (!ec && work) { rai::transaction transaction (node.store.environment, nullptr, true); @@ -1070,16 +1077,7 @@ void rai::rpc_handler::block_create () ec = nano::error_common::invalid_amount; } } - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (!ec && work_text.is_initialized ()) - { - auto work_error (rai::from_string_hex (work_text.get (), work)); - if (work_error) - { - ec = nano::error_common::bad_work_format; - } - } + auto work (work_optional_impl ()); rai::raw_key prv; prv.data.clear (); rai::uint256_union previous (0); @@ -2117,34 +2115,20 @@ void rai::rpc_handler::payment_begin () void rai::rpc_handler::payment_init () { - std::string id_text (request.get ("wallet")); - rai::uint256_union id; - if (!id.decode_hex (id_text)) + auto wallet (wallet_impl ()); + if (!ec) { rai::transaction transaction (node.store.environment, nullptr, true); - auto existing (node.wallets.items.find (id)); - if (existing != node.wallets.items.end ()) + if (wallet->store.valid_password (transaction)) { - auto wallet (existing->second); - if (wallet->store.valid_password (transaction)) - { - wallet->init_free_accounts (transaction); - response_l.put ("status", "Ready"); - } - else - { - ec = nano::error_common::wallet_locked; - } + wallet->init_free_accounts (transaction); + response_l.put ("status", "Ready"); } else { - ec = nano::error_common::wallet_not_found; + ec = nano::error_common::wallet_locked; } } - else - { - ec = nano::error_common::bad_wallet_number; - } response_errors (); } @@ -2312,6 +2296,7 @@ void rai::rpc_handler::receive () auto wallet (wallet_impl ()); auto account (account_impl ()); auto hash (hash_impl ("block")); + auto work (work_optional_impl ()); if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); @@ -2322,15 +2307,7 @@ void rai::rpc_handler::receive () { if (node.store.pending_exists (transaction, rai::pending_key (account, hash))) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) - { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } + auto work (work_optional_impl ()); if (!ec && work) { rai::account_info info; @@ -2606,15 +2583,7 @@ void rai::rpc_handler::send () rai::account destination; if (!destination.decode_account (destination_text)) { - uint64_t work (0); - boost::optional work_text (request.get_optional ("work")); - if (work_text.is_initialized ()) - { - if (rai::from_string_hex (work_text.get (), work)) - { - ec = nano::error_common::bad_work_format; - } - } + auto work (work_optional_impl ()); rai::uint128_t balance (0); if (!ec) { @@ -3389,22 +3358,14 @@ void rai::rpc_handler::work_set () rpc_control_impl (); auto wallet (wallet_impl ()); auto account (account_impl ()); + auto work (work_optional_impl ()); if (!ec) { rai::transaction transaction (node.store.environment, nullptr, true); if (wallet->store.find (transaction, account) != wallet->store.end ()) { - std::string work_text (request.get ("work")); - uint64_t work; - if (!rai::from_string_hex (work_text, work)) - { - wallet->store.work_put (transaction, account, work); - response_l.put ("success", ""); - } - else - { - ec = nano::error_common::bad_work_format; - } + wallet->store.work_put (transaction, account, work); + response_l.put ("success", ""); } else { @@ -3417,19 +3378,11 @@ void rai::rpc_handler::work_set () void rai::rpc_handler::work_validate () { auto hash (hash_impl ()); + auto work (work_optional_impl ()); if (!ec) { - std::string work_text (request.get ("work")); - uint64_t work; - if (!rai::from_string_hex (work_text, work)) - { - auto validate (rai::work_validate (hash, work)); - response_l.put ("valid", validate ? "0" : "1"); - } - else - { - ec = nano::error_common::bad_work_format; - } + auto validate (rai::work_validate (hash, work)); + response_l.put ("valid", validate ? "0" : "1"); } response_errors (); } diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index fb076c35..10e49bb6 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -225,6 +225,7 @@ public: rai::amount amount_impl (); rai::block_hash hash_impl (std::string = "hash"); rai::amount threshold_optional_impl (); + uint64_t work_optional_impl (); uint64_t count_impl (); uint64_t count_optional_impl (uint64_t = std::numeric_limits::max ()); bool rpc_control_impl (); From 553a32a813eed3510edf7bfb9f5855831b57538f Mon Sep 17 00:00:00 2001 From: SergiySW Date: Wed, 18 Jul 2018 00:20:37 +0300 Subject: [PATCH 27/38] Remove double check --- rai/node/rpc.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 7a8c89ba..fcd74996 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -615,7 +615,6 @@ void rai::rpc_handler::account_representative_set () rpc_control_impl (); auto wallet (wallet_impl ()); auto account (account_impl ()); - auto work (work_optional_impl ()); if (!ec) { std::string representative_text (request.get ("representative")); @@ -2296,7 +2295,6 @@ void rai::rpc_handler::receive () auto wallet (wallet_impl ()); auto account (account_impl ()); auto hash (hash_impl ("block")); - auto work (work_optional_impl ()); if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); From fa5e6a6d469468c7e3df7d9a17491557a7d8e07d Mon Sep 17 00:00:00 2001 From: SergiySW Date: Wed, 18 Jul 2018 00:24:15 +0300 Subject: [PATCH 28/38] Simplification --- rai/node/rpc.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index fcd74996..d841d1d0 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1443,8 +1443,7 @@ void rai::rpc_handler::deterministic_key () std::string seed_text (request.get ("seed")); std::string index_text (request.get ("index")); rai::raw_key seed; - auto error (seed.data.decode_hex (seed_text)); - if (!error) + if (!seed.data.decode_hex (seed_text)) { uint64_t index_a; if (!decode_unsigned (index_text, index_a)) From 35bc0065f57b29b33d6d99dbf91f5ac4d8e218ab Mon Sep 17 00:00:00 2001 From: SergiySW Date: Wed, 18 Jul 2018 00:28:19 +0300 Subject: [PATCH 29/38] Return "account" in history --- rai/node/rpc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index d841d1d0..0f07bd4c 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1640,7 +1640,7 @@ public: void rai::rpc_handler::account_history () { - std::string account_text; + rai::account account; bool output_raw (request.get_optional ("raw") == true); rai::block_hash hash; auto head_str (request.get_optional ("head")); @@ -1649,7 +1649,7 @@ void rai::rpc_handler::account_history () { if (!hash.decode_hex (*head_str)) { - account_text = node.ledger.account (transaction, hash).to_account (); + account = node.ledger.account (transaction, hash); } else { @@ -1658,7 +1658,7 @@ void rai::rpc_handler::account_history () } else { - auto account (account_impl ()); + account = account_impl (); if (!ec) { hash = node.ledger.latest (transaction, account); @@ -1672,7 +1672,7 @@ void rai::rpc_handler::account_history () if (!offset_text || !decode_unsigned (*offset_text, offset)) { boost::property_tree::ptree history; - response_l.put ("account", account_text); + response_l.put ("account", account.to_account ()); auto block (node.store.block_get (transaction, hash)); while (block != nullptr && count > 0) { From 3c851cc7d87228a464a3e0ec71926e9f361e8ebb Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 20 Jul 2018 05:31:42 +0300 Subject: [PATCH 30/38] Optional data for amount_impl --- rai/node/rpc.cpp | 43 +++++++++++++------------------------------ rai/node/rpc.hpp | 2 +- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 0f07bd4c..760e2fc7 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -244,12 +244,15 @@ std::shared_ptr rai::rpc_handler::wallet_impl () return nullptr; } -rai::account rai::rpc_handler::account_impl () +rai::account rai::rpc_handler::account_impl (std::string account_text) { rai::account result (0); if (!ec) { - std::string account_text (request.get ("account")); + if (account_text.empty ()) + { + account_text = request.get ("account"); + } if (result.decode_account (account_text)) { ec = nano::error_common::bad_account_number; @@ -688,9 +691,8 @@ void rai::rpc_handler::accounts_balances () { if (!ec) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl (accounts.second.data ())); + if (!ec) { boost::property_tree::ptree entry; auto balance (node.balance_pending (account)); @@ -698,10 +700,6 @@ void rai::rpc_handler::accounts_balances () entry.put ("pending", balance.second.convert_to ()); balances.push_back (std::make_pair (account.to_account (), entry)); } - else - { - ec = nano::error_common::bad_account_number; - } } } response_l.add_child ("balances", balances); @@ -740,9 +738,8 @@ void rai::rpc_handler::accounts_frontiers () { if (!ec) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl (accounts.second.data ())); + if (!ec) { auto latest (node.ledger.latest (transaction, account)); if (!latest.is_zero ()) @@ -750,10 +747,6 @@ void rai::rpc_handler::accounts_frontiers () frontiers.put (account.to_account (), latest.to_string ()); } } - else - { - ec = nano::error_common::bad_account_number; - } } } response_l.add_child ("frontiers", frontiers); @@ -771,9 +764,8 @@ void rai::rpc_handler::accounts_pending () { if (!ec) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl (accounts.second.data ())); + if (!ec) { boost::property_tree::ptree peers_l; rai::account end (account.number () + 1); @@ -807,10 +799,6 @@ void rai::rpc_handler::accounts_pending () } pending.add_child (account.to_account (), peers_l); } - else - { - ec = nano::error_common::bad_account_number; - } } } response_l.add_child ("blocks", pending); @@ -2842,16 +2830,11 @@ void rai::rpc_handler::wallet_add_watch () { if (!ec) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + auto account (account_impl (accounts.second.data ())); + if (!ec) { wallet->insert_watch (transaction, account); } - else - { - ec = nano::error_common::bad_account_number; - } } } response_l.put ("success", ""); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 10e49bb6..232d6c11 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -222,7 +222,7 @@ public: boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); rai::account account_impl (); - rai::amount amount_impl (); + rai::amount amount_impl (std::string = ""); rai::block_hash hash_impl (std::string = "hash"); rai::amount threshold_optional_impl (); uint64_t work_optional_impl (); From 6d6c3878b5f473c7bd31b5cd2e0c735396177fd9 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 20 Jul 2018 05:33:56 +0300 Subject: [PATCH 31/38] Less lines --- rai/node/rpc.cpp | 84 +++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 760e2fc7..425d2910 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -689,17 +689,14 @@ void rai::rpc_handler::accounts_balances () boost::property_tree::ptree balances; for (auto & accounts : request.get_child ("accounts")) { + auto account (account_impl (accounts.second.data ())); if (!ec) { - auto account (account_impl (accounts.second.data ())); - if (!ec) - { - boost::property_tree::ptree entry; - auto balance (node.balance_pending (account)); - entry.put ("balance", balance.first.convert_to ()); - entry.put ("pending", balance.second.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } + boost::property_tree::ptree entry; + auto balance (node.balance_pending (account)); + entry.put ("balance", balance.first.convert_to ()); + entry.put ("pending", balance.second.convert_to ()); + balances.push_back (std::make_pair (account.to_account (), entry)); } } response_l.add_child ("balances", balances); @@ -736,16 +733,13 @@ void rai::rpc_handler::accounts_frontiers () rai::transaction transaction (node.store.environment, nullptr, false); for (auto & accounts : request.get_child ("accounts")) { + auto account (account_impl (accounts.second.data ())); if (!ec) { - auto account (account_impl (accounts.second.data ())); - if (!ec) + auto latest (node.ledger.latest (transaction, account)); + if (!latest.is_zero ()) { - auto latest (node.ledger.latest (transaction, account)); - if (!latest.is_zero ()) - { - frontiers.put (account.to_account (), latest.to_string ()); - } + frontiers.put (account.to_account (), latest.to_string ()); } } } @@ -762,43 +756,40 @@ void rai::rpc_handler::accounts_pending () rai::transaction transaction (node.store.environment, nullptr, false); for (auto & accounts : request.get_child ("accounts")) { + auto account (account_impl (accounts.second.data ())); if (!ec) { - auto account (account_impl (accounts.second.data ())); - if (!ec) + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) { - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) + rai::pending_key key (i->first); + if (threshold.is_zero () && !source) { - rai::pending_key key (i->first); - if (threshold.is_zero () && !source) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (i->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (i->second); - if (info.amount.number () >= threshold.number ()) + if (source) { - if (source) - { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - pending_tree.put ("source", info.source.to_account ()); - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); - } + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + pending_tree.put ("source", info.source.to_account ()); + peers_l.add_child (key.hash.to_string (), pending_tree); + } + else + { + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } } } - pending.add_child (account.to_account (), peers_l); } + pending.add_child (account.to_account (), peers_l); } } response_l.add_child ("blocks", pending); @@ -2828,13 +2819,10 @@ void rai::rpc_handler::wallet_add_watch () { for (auto & accounts : request.get_child ("accounts")) { + auto account (account_impl (accounts.second.data ())); if (!ec) { - auto account (account_impl (accounts.second.data ())); - if (!ec) - { - wallet->insert_watch (transaction, account); - } + wallet->insert_watch (transaction, account); } } response_l.put ("success", ""); From e31456736cde9351396a97ed452b1961003bcd89 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 20 Jul 2018 05:36:07 +0300 Subject: [PATCH 32/38] Simplify wallet_balances --- rai/node/rpc.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 425d2910..b4b54ed4 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -2867,7 +2867,7 @@ void rai::rpc_handler::wallet_balances () { rai::account account (i->first.uint256 ()); rai::uint128_t balance = node.ledger.account_balance (transaction, account); - if (threshold.is_zero ()) + if (balance >= threshold.number ()) { boost::property_tree::ptree entry; rai::uint128_t pending = node.ledger.account_pending (transaction, account); @@ -2875,17 +2875,6 @@ void rai::rpc_handler::wallet_balances () entry.put ("pending", pending.convert_to ()); balances.push_back (std::make_pair (account.to_account (), entry)); } - else - { - if (balance >= threshold.number ()) - { - boost::property_tree::ptree entry; - rai::uint128_t pending = node.ledger.account_pending (transaction, account); - entry.put ("balance", balance.convert_to ()); - entry.put ("pending", pending.convert_to ()); - balances.push_back (std::make_pair (account.to_account (), entry)); - } - } } response_l.add_child ("balances", balances); } From 85f9b6ac450a7df3126a5944e9f2cac7c222d869 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 20 Jul 2018 06:03:02 +0300 Subject: [PATCH 33/38] Fix --- rai/node/rpc.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 8bd3c44b..8d2a7a20 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -221,8 +221,8 @@ public: std::error_code ec; boost::property_tree::ptree response_l; std::shared_ptr wallet_impl (); - rai::account account_impl (); - rai::amount amount_impl (std::string = ""); + rai::account account_impl (std::string = ""); + rai::amount amount_impl (); rai::block_hash hash_impl (std::string = "hash"); rai::amount threshold_optional_impl (); uint64_t work_optional_impl (); From f56fc3fe7cad702f2fed1015af595a8050d325c3 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 20 Jul 2018 11:20:52 +0300 Subject: [PATCH 34/38] Fix --- rai/node/rpc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index e0632011..6469f311 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -620,7 +620,7 @@ void rai::rpc_handler::account_representative_set () auto account (account_impl ()); if (!ec) { - if (wallet->password_valid ()) + if (wallet->valid_password ()) { std::string representative_text (request.get ("representative")); rai::account representative; @@ -2283,7 +2283,7 @@ void rai::rpc_handler::receive () if (!ec) { rai::transaction transaction (node.store.environment, nullptr, false); - if (wallet->store.password_valid (transaction)) + if (wallet->store.valid_password (transaction)) { if (wallet->store.find (transaction, account) != wallet->store.end ()) { From 02cc9e893d75acbbe3887d8056345b2c8e2086d8 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 3 Aug 2018 17:32:33 +0300 Subject: [PATCH 35/38] Fix --- rai/node/rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index f5dc4b91..56d8613a 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1471,7 +1471,7 @@ void rai::rpc_handler::frontiers () rai::transaction transaction (node.store.environment, nullptr, false); for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && frontiers.size () < count; ++i) { - frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second).head.to_string ()); + frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0).head.to_string ()); } response_l.add_child ("frontiers", frontiers); } From 0d524b8debf2d6b274da0541c29ef15bdfc9a68d Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 3 Aug 2018 17:54:40 +0300 Subject: [PATCH 36/38] Use library for deterministic_key --- rai/node/rpc.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 56d8613a..4ed480d1 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1433,23 +1433,18 @@ void rai::rpc_handler::deterministic_key () rai::raw_key seed; if (!seed.data.decode_hex (seed_text)) { - uint64_t index_a; - if (!decode_unsigned (index_text, index_a)) + try { - rai::uint256_union index (index_a); + uint32_t index (std::stoul (index_text)); rai::uint256_union prv; - blake2b_state hash; - blake2b_init (&hash, prv.bytes.size ()); - blake2b_update (&hash, seed.data.bytes.data (), seed.data.bytes.size ()); - blake2b_update (&hash, reinterpret_cast (&index.dwords[7]), sizeof (uint32_t)); - blake2b_final (&hash, prv.bytes.data (), prv.bytes.size ()); + rai::deterministic_key (seed.data, index, prv); rai::uint256_union pub; ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); response_l.put ("private", prv.to_string ()); response_l.put ("public", pub.to_string ()); response_l.put ("account", pub.to_account ()); } - else + catch (std::logic_error const &) { ec = nano::error_common::invalid_index; } From 16bb25aeeefa736d8cb201b8f84094203b2a4a36 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 3 Aug 2018 18:11:51 +0300 Subject: [PATCH 37/38] rai::pub_key function from library --- rai/lib/numbers.cpp | 7 +++++++ rai/lib/numbers.hpp | 1 + rai/node/rpc.cpp | 11 +++-------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/rai/lib/numbers.cpp b/rai/lib/numbers.cpp index fcac09eb..296bdc7e 100644 --- a/rai/lib/numbers.cpp +++ b/rai/lib/numbers.cpp @@ -422,6 +422,13 @@ void rai::deterministic_key (rai::uint256_union const & seed_a, uint32_t index_a blake2b_final (&hash, prv_a.bytes.data (), prv_a.bytes.size ()); } +rai::public_key rai::pub_key (rai::private_key const & privatekey_a) +{ + rai::uint256_union result; + ed25519_publickey (privatekey_a.bytes.data (), result.bytes.data ()); + return result; +} + bool rai::validate_message (rai::public_key const & public_key, rai::uint256_union const & message, rai::uint512_union const & signature) { auto result (0 != ed25519_sign_open (message.bytes.data (), sizeof (message.bytes), public_key.bytes.data (), signature.bytes.data ())); diff --git a/rai/lib/numbers.hpp b/rai/lib/numbers.hpp index 8e7aa7bb..c97b9463 100644 --- a/rai/lib/numbers.hpp +++ b/rai/lib/numbers.hpp @@ -121,6 +121,7 @@ using signature = uint512_union; rai::uint512_union sign_message (rai::raw_key const &, rai::public_key const &, rai::uint256_union const &); bool validate_message (rai::public_key const &, rai::uint256_union const &, rai::uint512_union const &); void deterministic_key (rai::uint256_union const &, uint32_t, rai::uint256_union &); +rai::public_key pub_key (rai::private_key const &); } namespace std diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 4ed480d1..de83a9cd 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -5,8 +5,6 @@ #include #include -#include - #ifdef RAIBLOCKS_SECURE_RPC #include #endif @@ -1141,8 +1139,7 @@ void rai::rpc_handler::block_create () } if (prv.data != 0) { - rai::uint256_union pub; - ed25519_publickey (prv.data.bytes.data (), pub.bytes.data ()); + rai::uint256_union pub (rai::pub_key (prv.data)); // Fetching account balance & previous for send blocks (if aren't given directly) if (!previous_text.is_initialized () && !balance_text.is_initialized ()) { @@ -1438,8 +1435,7 @@ void rai::rpc_handler::deterministic_key () uint32_t index (std::stoul (index_text)); rai::uint256_union prv; rai::deterministic_key (seed.data, index, prv); - rai::uint256_union pub; - ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); + rai::uint256_union pub (rai::pub_key (prv)); response_l.put ("private", prv.to_string ()); response_l.put ("public", pub.to_string ()); response_l.put ("account", pub.to_account ()); @@ -1733,8 +1729,7 @@ void rai::rpc_handler::key_expand () rai::uint256_union prv; if (!prv.decode_hex (key_text)) { - rai::uint256_union pub; - ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); + rai::uint256_union pub (rai::pub_key (prv)); response_l.put ("private", prv.to_string ()); response_l.put ("public", pub.to_string ()); response_l.put ("account", pub.to_account ()); From cf24370750d1e0d48b614dc92d0df97604ab2da9 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Fri, 3 Aug 2018 18:17:44 +0300 Subject: [PATCH 38/38] Prevent possible empty responses --- rai/node/rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index de83a9cd..9143947d 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -204,7 +204,7 @@ void rai::error_response (std::function