From 655fa66609f82d415f4a7e1424323bc1c49abc18 Mon Sep 17 00:00:00 2001 From: cryptocode <34946442+cryptocode@users.noreply.github.com> Date: Sun, 21 Jan 2018 19:02:39 +0100 Subject: [PATCH] Simplify code; mostly reduced nesting --- rai/lib/numbers.cpp | 25 +++++++++-------------- rai/node/node.cpp | 28 ++++++++------------------ rai/node/wallet.cpp | 48 +++++++++++++++++++-------------------------- 3 files changed, 37 insertions(+), 64 deletions(-) diff --git a/rai/lib/numbers.cpp b/rai/lib/numbers.cpp index ae0f766d..eefb6d61 100644 --- a/rai/lib/numbers.cpp +++ b/rai/lib/numbers.cpp @@ -257,28 +257,21 @@ void rai::uint256_union::encode_hex (std::string & text) const bool rai::uint256_union::decode_hex (std::string const & text) { auto result (false); - if (!text.empty ()) + if (!text.empty () && text.size () <= 64) { - if (text.size () <= 64) + std::stringstream stream (text); + stream << std::hex << std::noshowbase; + rai::uint256_t number_l; + try { - std::stringstream stream (text); - stream << std::hex << std::noshowbase; - rai::uint256_t number_l; - try - { - stream >> number_l; - *this = number_l; - if (!stream.eof ()) - { - result = true; - } - } - catch (std::runtime_error &) + stream >> number_l; + *this = number_l; + if (!stream.eof ()) { result = true; } } - else + catch (std::runtime_error &) { result = true; } diff --git a/rai/node/node.cpp b/rai/node/node.cpp index fc7a3fd8..10d7a3d9 100644 --- a/rai/node/node.cpp +++ b/rai/node/node.cpp @@ -89,12 +89,9 @@ void rai::network::send_keepalive (rai::endpoint const & endpoint_a) send_buffer (bytes->data (), bytes->size (), endpoint_a, [bytes, node_w, endpoint_a](boost::system::error_code const & ec, size_t) { if (auto node_l = node_w.lock ()) { - if (node_l->config.logging.network_keepalive_logging ()) + if (ec && node_l->config.logging.network_keepalive_logging ()) { - if (ec) - { - BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending keepalive to %1% %2%") % endpoint_a % ec.message ()); - } + BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending keepalive to %1% %2%") % endpoint_a % ec.message ()); } } }); @@ -134,12 +131,9 @@ void rai::network::republish (rai::block_hash const & hash_a, std::shared_ptrdata (), buffer_a->size (), endpoint_a, [buffer_a, node_w, endpoint_a](boost::system::error_code const & ec, size_t size) { if (auto node_l = node_w.lock ()) { - if (node_l->config.logging.network_logging ()) + if (ec && node_l->config.logging.network_logging ()) { - if (ec) - { - BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending publish: %1% to %2%") % ec.message () % endpoint_a); - } + BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending publish: %1% to %2%") % ec.message () % endpoint_a); } } }); @@ -282,12 +276,9 @@ void rai::network::send_confirm_req (rai::endpoint const & endpoint_a, std::shar send_buffer (bytes->data (), bytes->size (), endpoint_a, [bytes, node_w](boost::system::error_code const & ec, size_t size) { if (auto node_l = node_w.lock ()) { - if (node_l->config.logging.network_logging ()) + if (ec && node_l->config.logging.network_logging ()) { - if (ec) - { - BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending confirm request: %1%") % ec.message ()); - } + BOOST_LOG (node_l->log) << boost::str (boost::format ("Error sending confirm request: %1%") % ec.message ()); } } }); @@ -1625,12 +1616,9 @@ void rai::network::confirm_send (rai::confirm_ack const & confirm_a, std::shared node.network.send_buffer (bytes_a->data (), bytes_a->size (), endpoint_a, [bytes_a, node_w, endpoint_a](boost::system::error_code const & ec, size_t size_a) { if (auto node_l = node_w.lock ()) { - if (node_l->config.logging.network_logging ()) + if (ec && node_l->config.logging.network_logging ()) { - if (ec) - { - BOOST_LOG (node_l->log) << boost::str (boost::format ("Error broadcasting confirm_ack to %1%: %2%") % endpoint_a % ec.message ()); - } + BOOST_LOG (node_l->log) << boost::str (boost::format ("Error broadcasting confirm_ack to %1%: %2%") % endpoint_a % ec.message ()); } } }); diff --git a/rai/node/wallet.cpp b/rai/node/wallet.cpp index fdd98e07..86ea8f07 100644 --- a/rai/node/wallet.cpp +++ b/rai/node/wallet.cpp @@ -896,18 +896,15 @@ std::shared_ptr rai::wallet::change_action (rai::account const & sou if (store.valid_password (transaction)) { auto existing (store.find (transaction, source_a)); - if (existing != store.end ()) + if (existing != store.end () && !node.ledger.latest (transaction, source_a).is_zero ()) { - if (!node.ledger.latest (transaction, source_a).is_zero ()) - { - rai::account_info info; - auto error1 (node.ledger.store.account_get (transaction, source_a, info)); - assert (!error1); - rai::raw_key prv; - auto error2 (store.fetch (transaction, source_a, prv)); - assert (!error2); - block.reset (new rai::change_block (info.head, representative_a, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); - } + rai::account_info info; + auto error1 (node.ledger.store.account_get (transaction, source_a, info)); + assert (!error1); + rai::raw_key prv; + auto error2 (store.fetch (transaction, source_a, prv)); + assert (!error2); + block.reset (new rai::change_block (info.head, representative_a, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); } } } @@ -939,18 +936,15 @@ std::shared_ptr rai::wallet::send_action (rai::account const & sourc if (existing != store.end ()) { auto balance (node.ledger.account_balance (transaction, source_a)); - if (!balance.is_zero ()) + if (!balance.is_zero () && balance >= amount_a) { - if (balance >= amount_a) - { - rai::account_info info; - auto error1 (node.ledger.store.account_get (transaction, source_a, info)); - assert (!error1); - rai::raw_key prv; - auto error2 (store.fetch (transaction, source_a, prv)); - assert (!error2); - block.reset (new rai::send_block (info.head, account_a, balance - amount_a, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); - } + rai::account_info info; + auto error1 (node.ledger.store.account_get (transaction, source_a, info)); + assert (!error1); + rai::raw_key prv; + auto error2 (store.fetch (transaction, source_a, prv)); + assert (!error2); + block.reset (new rai::send_block (info.head, account_a, balance - amount_a, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); } } } @@ -1054,14 +1048,12 @@ uint64_t rai::wallet::work_fetch (MDB_txn * transaction_a, rai::account const & { result = node.generate_work (root_a); } - else + else if (rai::work_validate (root_a, result)) { - if (rai::work_validate (root_a, result)) - { - BOOST_LOG (node.log) << "Cached work invalid, regenerating"; - result = node.generate_work (root_a); - } + BOOST_LOG (node.log) << "Cached work invalid, regenerating"; + result = node.generate_work (root_a); } + return result; }