From 35ff2524092e15b5e021a4595b55a46f66e54e2d Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 16 Mar 2017 20:54:02 -0500 Subject: [PATCH] Adding database fix for zero successor blocks. --- rai/core_test/block_store.cpp | 32 ++++++++++++++++++++++++++++++++ rai/core_test/rpc.cpp | 2 +- rai/secure.cpp | 28 ++++++++++++++++++++++++++++ rai/secure.hpp | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/rai/core_test/block_store.cpp b/rai/core_test/block_store.cpp index dfedb88e02..6f5a297be0 100644 --- a/rai/core_test/block_store.cpp +++ b/rai/core_test/block_store.cpp @@ -663,6 +663,38 @@ TEST (block_store, upgrade_v3_v4) ASSERT_EQ (rai::amount (100), info.amount); } +TEST (block_store, upgrade_v4_v5) +{ + rai::block_hash genesis_hash (0); + rai::block_hash hash (0); + auto path (rai::unique_path ()); + { + bool init (false); + rai::block_store store (init, path); + ASSERT_FALSE (init); + rai::transaction transaction (store.environment, nullptr, true); + rai::genesis genesis;; + genesis.initialize (transaction, store); + rai::ledger ledger (store); + store.version_put (transaction, 4); + rai::account_info info; + store.account_get (transaction, rai::test_genesis_key.pub, info); + rai::keypair key0; + rai::send_block block0 (info.head, key0.pub, rai::genesis_amount - rai::Grai_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0); + ASSERT_EQ (rai::process_result::progress, ledger.process (transaction, block0).code); + hash = block0.hash (); + auto original (store.block_get (transaction, info.head)); + genesis_hash = info.head; + store.block_successor_clear (transaction, info.head); + ASSERT_TRUE (store.block_successor (transaction, genesis_hash).is_zero ()); + } + bool init (false); + rai::block_store store (init, path); + ASSERT_FALSE (init); + rai::transaction transaction (store.environment, nullptr, false); + ASSERT_EQ (hash, store.block_successor (transaction, genesis_hash)); +} + TEST (block_store, block_random) { bool init (false); diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 30892c1ef3..7458b53a56 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1313,7 +1313,7 @@ TEST (rpc, version) ASSERT_EQ (200, response1.status); ASSERT_EQ ("1", response1.json.get ("rpc_version")); ASSERT_EQ (200, response1.status); - ASSERT_EQ ("4", response1.json.get ("store_version")); + ASSERT_EQ ("5", response1.json.get ("store_version")); ASSERT_EQ (boost::str (boost::format ("RaiBlocks %1%.%2%.%3%") % RAIBLOCKS_VERSION_MAJOR % RAIBLOCKS_VERSION_MINOR % RAIBLOCKS_VERSION_PATCH), response1.json.get ("node_vendor")); auto & headers (response1.resp.fields); auto access_control (std::find_if (headers.begin (), headers.end (), [] (decltype (*headers.begin ()) & header_a) { return boost::iequals (header_a.first, "Access-Control-Allow-Origin"); })); diff --git a/rai/secure.cpp b/rai/secure.cpp index 1543e2dd86..8a8613b517 100644 --- a/rai/secure.cpp +++ b/rai/secure.cpp @@ -1577,6 +1577,8 @@ void rai::block_store::do_upgrades (MDB_txn * transaction_a) case 3: upgrade_v3_to_v4 (transaction_a); case 4: + upgrade_v4_to_v5 (transaction_a); + case 5: break; default: assert (false); @@ -1694,6 +1696,32 @@ void rai::block_store::upgrade_v3_to_v4 (MDB_txn * transaction_a) } } +void rai::block_store::upgrade_v4_to_v5 (MDB_txn * transaction_a) +{ + unsigned fixes (0); + version_put (transaction_a, 5); + for (auto i (latest_begin (transaction_a)), n (latest_end ()); i != n; ++i) + { + rai::account account (i->first); + rai::account_info info (i->second); + rai::block_hash successor (0); + auto block (block_get (transaction_a, info.head)); + while (block != nullptr) + { + auto hash (block->hash ()); + if (block_successor (transaction_a, hash).is_zero () && !successor.is_zero ()) + { + //std::cerr << boost::str (boost::format ("Adding successor for account %1%, block %2%, successor %3%\n") % account.to_account () % hash.to_string () % successor.to_string ()); + ++fixes; + block_put (transaction_a, hash, *block, successor); + } + successor = hash; + block = block_get (transaction_a, block->previous ()); + } + } + //std::cerr << boost::str (boost::format ("Fixed up %1% blocks\n") % fixes); +} + void rai::block_store::clear (MDB_dbi db_a) { rai::transaction transaction (environment, nullptr, true); diff --git a/rai/secure.hpp b/rai/secure.hpp index e710c4423f..a04e2966ae 100644 --- a/rai/secure.hpp +++ b/rai/secure.hpp @@ -391,6 +391,7 @@ class block_store void upgrade_v1_to_v2 (MDB_txn *); void upgrade_v2_to_v3 (MDB_txn *); void upgrade_v3_to_v4 (MDB_txn *); + void upgrade_v4_to_v5 (MDB_txn *); void clear (MDB_dbi);