Skip to content

Commit

Permalink
Adding database fix for zero successor blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Mar 17, 2017
1 parent f6163b1 commit 35ff252
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
32 changes: 32 additions & 0 deletions rai/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ TEST (rpc, version)
ASSERT_EQ (200, response1.status);
ASSERT_EQ ("1", response1.json.get <std::string> ("rpc_version"));
ASSERT_EQ (200, response1.status);
ASSERT_EQ ("4", response1.json.get <std::string> ("store_version"));
ASSERT_EQ ("5", response1.json.get <std::string> ("store_version"));
ASSERT_EQ (boost::str (boost::format ("RaiBlocks %1%.%2%.%3%") % RAIBLOCKS_VERSION_MAJOR % RAIBLOCKS_VERSION_MINOR % RAIBLOCKS_VERSION_PATCH), response1.json.get <std::string> ("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"); }));
Expand Down
28 changes: 28 additions & 0 deletions rai/secure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions rai/secure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 35ff252

Please sign in to comment.