From 0eaf75c25f5e77a0831a7bd41ff901efe1f4ebc2 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sat, 17 Jan 2015 21:46:23 -0600 Subject: [PATCH] Fixing issue with generating incorrect work. --- rai/core_test/wallet.cpp | 12 +++++++++++- rai/node.cpp | 16 ++++++++++++---- rai/secure.cpp | 3 +++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/rai/core_test/wallet.cpp b/rai/core_test/wallet.cpp index ec3795f4f6..86cb500d03 100644 --- a/rai/core_test/wallet.cpp +++ b/rai/core_test/wallet.cpp @@ -465,7 +465,7 @@ TEST (wallet, work_generate) ASSERT_LT (iterations1, 200); } auto iterations2 (0); - while (wallet->work.get (account1, work1)) + while (wallet->work.get (account1, work1) || rai::work_validate (system.nodes [0]->ledger.latest_root (account1), work1)) { system.service->poll_one (); system.processor.poll_one (); @@ -490,4 +490,14 @@ TEST (wallet, startup_work) ++iterations2; ASSERT_LT (iterations2, 200); } +} + +TEST (wallet, unsynced_work) +{ + rai::system system (24000, 1); + auto wallet (system.wallet (0)); + wallet->work.put (0, 0); + std::lock_guard lock (wallet->mutex); + auto work1 (wallet->work_fetch (0, 0)); + ASSERT_FALSE (rai::work_validate (0, work1)); } \ No newline at end of file diff --git a/rai/node.cpp b/rai/node.cpp index 09b2e955a0..9a81f32ce3 100644 --- a/rai/node.cpp +++ b/rai/node.cpp @@ -984,6 +984,14 @@ uint64_t rai::wallet::work_fetch (rai::account const & account_a, rai::block_has { result = rai::work_generate (root_a); } + else + { + if (rai::work_validate (root_a, result)) + { + result = rai::work_generate (root_a); + BOOST_LOG (node.log) << "Cached work invalid, regenerating"; + } + } return result; } @@ -1412,7 +1420,7 @@ service (processor_a) auto wallet (i->second); if (wallet->store.exists (account_a)) { - auto root (block_a.root ()); + auto root (ledger.latest_root (account_a)); service.add (std::chrono::system_clock::now (), [wallet, account_a, root] () { wallet->work_generate (account_a, root); @@ -1427,7 +1435,7 @@ service (processor_a) auto wallet (i->second); if (wallet->store.exists (account_a)) { - auto root (block_a.root ()); + auto root (ledger.latest_root (account_a)); service.add (std::chrono::system_clock::now (), [wallet, account_a, root] () { wallet->work_generate (account_a, root); @@ -1442,7 +1450,7 @@ service (processor_a) auto wallet (i->second); if (wallet->store.exists (account_a)) { - auto root (block_a.root ()); + auto root (ledger.latest_root (account_a)); service.add (std::chrono::system_clock::now (), [wallet, account_a, root] () { wallet->work_generate (account_a, root); @@ -1457,7 +1465,7 @@ service (processor_a) auto wallet (i->second); if (wallet->store.exists (account_a)) { - auto root (block_a.root ()); + auto root (ledger.latest_root (account_a)); service.add (std::chrono::system_clock::now (), [wallet, account_a, root] () { wallet->work_generate (account_a, root); diff --git a/rai/secure.cpp b/rai/secure.cpp index 13db223907..6b7a470738 100644 --- a/rai/secure.cpp +++ b/rai/secure.cpp @@ -639,6 +639,7 @@ uint64_t rai::receive_block::block_work () const void rai::receive_block::block_work_set (uint64_t work_a) { + assert (!rai::work_validate (root (), work_a)); work = work_a; } @@ -1261,6 +1262,7 @@ uint64_t rai::open_block::block_work () const void rai::open_block::block_work_set (uint64_t work_a) { + assert (!rai::work_validate (root (), work_a)); work = work_a; } @@ -1493,6 +1495,7 @@ uint64_t rai::change_block::block_work () const void rai::change_block::block_work_set (uint64_t work_a) { + assert (!rai::work_validate (root (), work_a)); work = work_a; }