From 07eeef09f5556cf4bfb61a8dd88fb31ec204ef8e Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 4 Dec 2014 18:30:21 -0600 Subject: [PATCH] Adding ledger observers and converted receivable to an observer. --- rai/core/core.cpp | 37 +++++++------------------------------ rai/core_test/client.cpp | 3 ++- rai/core_test/ledger.cpp | 12 ++++++++---- rai/secure.cpp | 16 ++++++++-------- rai/secure.hpp | 8 ++++---- 5 files changed, 29 insertions(+), 47 deletions(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index 67017469..11c4a66a 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -858,6 +858,13 @@ transactions (*this), peers (network.endpoint ()), service (processor_a) { + ledger.send_observer = [this] (rai::send_block const & block_a, rai::account const & account_a, rai::amount const & balance_a) + { + if (wallet.find (block_a.hashables.destination) != wallet.end ()) + { + conflicts.start (block_a, true); + } + }; if (!init_a.error ()) { if (client_lifetime_tracing ()) @@ -1130,34 +1137,6 @@ void rai::processor::process_receive_republish (std::unique_ptr inc namespace { -class receivable_visitor : public rai::block_visitor -{ -public: - receivable_visitor (rai::client & client_a, rai::block const & incoming_a) : - client (client_a), - incoming (incoming_a) - { - } - void send_block (rai::send_block const & block_a) override - { - if (client.wallet.find (block_a.hashables.destination) != client.wallet.end ()) - { - client.conflicts.start (block_a, true); - } - } - void receive_block (rai::receive_block const &) override - { - } - void open_block (rai::open_block const &) override - { - } - void change_block (rai::change_block const &) override - { - } - rai::client & client; - rai::block const & incoming; -}; - class successor_visitor : public rai::block_visitor { public: @@ -1189,8 +1168,6 @@ rai::process_result rai::processor::process_receive (rai::block const & block_a) { case rai::process_result::progress: { - receivable_visitor visitor (client, block_a); - block_a.visit (visitor); break; } case rai::process_result::gap_previous: diff --git a/rai/core_test/client.cpp b/rai/core_test/client.cpp index f7155ec3..99b6ff63 100644 --- a/rai/core_test/client.cpp +++ b/rai/core_test/client.cpp @@ -274,9 +274,9 @@ TEST (client, search_pending) rai::system system (24000, 1); rai::keypair key2; system.clients [0]->wallet.insert (rai::test_genesis_key.prv); + auto balance (system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub)); ASSERT_FALSE (system.clients [0]->transactions.send (key2.pub, 1000)); auto iterations1 (0); - auto balance (system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub)); while (system.clients [0]->ledger.account_balance (rai::test_genesis_key.pub) == balance) { system.service->poll_one (); @@ -320,4 +320,5 @@ TEST (client, connect_after_junk) ++iterations2; ASSERT_LT (iterations2, 200); } + client1->stop (); } \ No newline at end of file diff --git a/rai/core_test/ledger.cpp b/rai/core_test/ledger.cpp index 2059a3de..0a578179 100644 --- a/rai/core_test/ledger.cpp +++ b/rai/core_test/ledger.cpp @@ -103,10 +103,11 @@ TEST (ledger, process_send) rai::sign_message (rai::test_genesis_key.prv, rai::test_genesis_key.pub, hash1, send.signature); rai::account account1; rai::amount amount1; - ledger.send_observer = [&account1, &amount1] (rai::account const & account_a, rai::amount const & amount_a) + ledger.send_observer = [&account1, &amount1, &send] (rai::send_block const & block_a, rai::account const & account_a, rai::amount const & amount_a) { account1 = account_a; amount1 = amount_a; + ASSERT_EQ (send, block_a); }; ASSERT_EQ (rai::process_result::progress, ledger.process (send)); ASSERT_EQ (rai::test_genesis_key.pub, account1); @@ -195,11 +196,12 @@ TEST (ledger, process_receive) rai::account account2; rai::amount amount2; rai::account account3; - ledger.open_observer = [&account2, &amount2, &account3] (rai::account const & account_a, rai::amount const & amount_a, rai::account const & representative_a) + ledger.open_observer = [&account2, &amount2, &account3, &open] (rai::open_block const & block_a, rai::account const & account_a, rai::amount const & amount_a, rai::account const & representative_a) { account2 = account_a; amount2 = amount_a; account3 = representative_a; + ASSERT_EQ (open, block_a); }; ASSERT_EQ (rai::process_result::progress, ledger.process (open)); ASSERT_EQ (key2.pub, account2); @@ -220,10 +222,11 @@ TEST (ledger, process_receive) rai::sign_message (key2.prv, key2.pub, hash4, receive.signature); rai::account account1; rai::amount amount1; - ledger.receive_observer = [&account1, &amount1] (rai::account const & account_a, rai::amount const & amount_a) + ledger.receive_observer = [&account1, &amount1, &receive] (rai::receive_block const & block_a, rai::account const & account_a, rai::amount const & amount_a) { account1 = account_a; amount1 = amount_a; + ASSERT_EQ (receive, block_a); }; ASSERT_EQ (rai::process_result::progress, ledger.process (receive)); ASSERT_EQ (rai::uint128_union (std::numeric_limits ::max () - 25), amount1); @@ -417,10 +420,11 @@ TEST (ledger, representative_change) rai::change_block block (key2.pub, frontier1.hash, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub); rai::account account1; rai::account account2; - ledger.change_observer = [&account1, &account2] (rai::account const & account_a, rai::account const & representative_a) + ledger.change_observer = [&account1, &account2, &block] (rai::change_block const & block_a, rai::account const & account_a, rai::account const & representative_a) { account1 = account_a; account2 = representative_a; + ASSERT_EQ (block, block_a); }; ASSERT_EQ (rai::process_result::progress, ledger.process (block)); ASSERT_EQ (rai::test_genesis_key.pub, account1); diff --git a/rai/secure.cpp b/rai/secure.cpp index f67afedd..503ed709 100644 --- a/rai/secure.cpp +++ b/rai/secure.cpp @@ -148,10 +148,10 @@ rai::keypair::keypair (std::string const & prv_a) rai::ledger::ledger (bool & init_a, leveldb::Status const & store_init_a, rai::block_store & store_a) : store (store_a), -send_observer ([] (rai::account const &, rai::amount const &) {}), -receive_observer ([] (rai::account const &, rai::amount const &) {}), -open_observer ([] (rai::account const &, rai::amount const &, rai::account const &) {}), -change_observer ([] (rai::account const &, rai::account const &) {}) +send_observer ([] (rai::send_block const &, rai::account const &, rai::amount const &) {}), +receive_observer ([] (rai::receive_block const &, rai::account const &, rai::amount const &) {}), +open_observer ([] (rai::open_block const &, rai::account const &, rai::amount const &, rai::account const &) {}), +change_observer ([] (rai::change_block const &, rai::account const &, rai::account const &) {}) { if (store_init_a.ok ()) { @@ -2522,7 +2522,7 @@ void ledger_processor::change_block (rai::change_block const & block_a) ledger.move_representation (frontier.representative, block_a.hashables.representative, ledger.balance (block_a.hashables.previous)); ledger.store.block_put (message, block_a); ledger.change_latest (account, message, block_a.hashables.representative, frontier.balance); - ledger.change_observer (account, block_a.hashables.representative); + ledger.change_observer (block_a, account, block_a.hashables.representative); } } } @@ -2556,7 +2556,7 @@ void ledger_processor::send_block (rai::send_block const & block_a) ledger.store.block_put (message, block_a); ledger.change_latest (account, message, frontier.representative, block_a.hashables.balance); ledger.store.pending_put (message, {account, frontier.balance.number () - block_a.hashables.balance.number (), block_a.hashables.destination}); - ledger.send_observer (account, block_a.hashables.balance); + ledger.send_observer (block_a, account, block_a.hashables.balance); } } } @@ -2597,7 +2597,7 @@ void ledger_processor::receive_block (rai::receive_block const & block_a) ledger.store.block_put (hash, block_a); ledger.change_latest (receivable.destination, hash, frontier.representative, new_balance); ledger.move_representation (source_frontier.representative, frontier.representative, receivable.amount.number ()); - ledger.receive_observer (receivable.destination, new_balance); + ledger.receive_observer (block_a, receivable.destination, new_balance); } else { @@ -2639,7 +2639,7 @@ void ledger_processor::open_block (rai::open_block const & block_a) ledger.store.block_put (hash, block_a); ledger.change_latest (receivable.destination, hash, block_a.hashables.representative, receivable.amount.number ()); ledger.move_representation (source_frontier.representative, block_a.hashables.representative, receivable.amount.number ()); - ledger.open_observer (receivable.destination, receivable.amount, block_a.hashables.representative); + ledger.open_observer (block_a, receivable.destination, receivable.amount, block_a.hashables.representative); } } } diff --git a/rai/secure.hpp b/rai/secure.hpp index d9b078e5..5239a09e 100644 --- a/rai/secure.hpp +++ b/rai/secure.hpp @@ -509,10 +509,10 @@ namespace rai void checksum_update (rai::block_hash const &); rai::checksum checksum (rai::account const &, rai::account const &); rai::block_store & store; - std::function send_observer; - std::function receive_observer; - std::function open_observer; - std::function change_observer; + std::function send_observer; + std::function receive_observer; + std::function open_observer; + std::function change_observer; }; class vote {