From 361b50bc004e646457e58c68288eb8dad54b117e Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 7 Feb 2017 10:56:42 -0600 Subject: [PATCH] Adding block_store v3 to v4 upgrade function to update pending table. --- rai/core_test/block_store.cpp | 31 ++++++++++++++++++++- rai/secure.cpp | 26 ++++++++++++++++-- rai/secure.hpp | 1 + rai/versioning.cpp | 52 +++++++++++++++++++++++++++++++++++ rai/versioning.hpp | 16 ++++++++++- 5 files changed, 121 insertions(+), 5 deletions(-) diff --git a/rai/core_test/block_store.cpp b/rai/core_test/block_store.cpp index f3dae71e..806e0297 100644 --- a/rai/core_test/block_store.cpp +++ b/rai/core_test/block_store.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -593,7 +594,6 @@ TEST (block_store, sequence_increment) ASSERT_EQ (31, seq8); } - TEST (block_store, upgrade_v2_v3) { rai::keypair key1; @@ -637,3 +637,32 @@ TEST (block_store, upgrade_v2_v3) ASSERT_FALSE (store.account_get (transaction, rai::test_genesis_key.pub, info)); ASSERT_EQ (change_hash, info.rep_block); } + +TEST (block_store, upgrade_v3_v4) +{ + rai::keypair key1; + rai::keypair key2; + rai::keypair key3; + auto path (rai::unique_path ()); + { + bool init (false); + rai::block_store store (init, path); + ASSERT_FALSE (init); + rai::transaction transaction (store.environment, nullptr, true); + store.version_put (transaction, 3); + rai::pending_info_v3 info (key1.pub, 100, key2.pub); + auto status (mdb_put (transaction, store.pending, key3.pub.val (), info.val (), 0)); + ASSERT_EQ (0, status); + } + bool init (false); + rai::block_store store (init, path); + rai::ledger ledger (store); + rai::transaction transaction (store.environment, nullptr, true); + ASSERT_FALSE (init); + rai::pending_key key (key2.pub, key3.pub); + rai::pending_info info; + auto error (store.pending_get (transaction, key, info)); + ASSERT_FALSE (error); + ASSERT_EQ (key1.pub, info.source); + ASSERT_EQ (rai::amount (100), info.amount); +} diff --git a/rai/secure.cpp b/rai/secure.cpp index fe82fe31..b7773606 100644 --- a/rai/secure.cpp +++ b/rai/secure.cpp @@ -9,6 +9,8 @@ #include +#include + // Genesis keys for network variants namespace { @@ -1562,12 +1564,12 @@ void rai::block_store::do_upgrades (MDB_txn * transaction_a) { case 1: upgrade_v1_to_v2 (transaction_a); - break; case 2: upgrade_v2_to_v3 (transaction_a); - break; case 3: - break; + upgrade_v3_to_v4 (transaction_a); + case 4: + break; default: assert (false); } @@ -1666,6 +1668,24 @@ void rai::block_store::upgrade_v2_to_v3 (MDB_txn * transaction_a) } } +void rai::block_store::upgrade_v3_to_v4 (MDB_txn * transaction_a) +{ + version_put (transaction_a, 4); + std::queue > items; + for (auto i (pending_begin (transaction_a)), n (pending_end ()); i != n; ++i) + { + rai::block_hash hash (i->first); + rai::pending_info_v3 info (i->second); + items.push (std::make_pair (rai::pending_key (info.destination, hash), rai::pending_info (info.source, info.amount))); + } + mdb_drop (transaction_a, pending, 0); + while (!items.empty ()) + { + pending_put (transaction_a, items.front ().first, items.front ().second); + items.pop (); + } +} + 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 6d3e610d..5648e343 100644 --- a/rai/secure.hpp +++ b/rai/secure.hpp @@ -380,6 +380,7 @@ public: void do_upgrades (MDB_txn *); void upgrade_v1_to_v2 (MDB_txn *); void upgrade_v2_to_v3 (MDB_txn *); + void upgrade_v3_to_v4 (MDB_txn *); void clear (MDB_dbi); diff --git a/rai/versioning.cpp b/rai/versioning.cpp index b13804d7..f748c12f 100644 --- a/rai/versioning.cpp +++ b/rai/versioning.cpp @@ -53,3 +53,55 @@ rai::mdb_val rai::account_info_v1::val () const { return rai::mdb_val (sizeof (*this), const_cast (this)); } + +rai::pending_info_v3::pending_info_v3 () : +source (0), +amount (0), +destination (0) +{ +} + +rai::pending_info_v3::pending_info_v3 (MDB_val const & val_a) +{ + assert(val_a.mv_size == sizeof (*this)); + static_assert (sizeof (source) + sizeof (amount) + sizeof (destination) == sizeof (*this), "Packed class"); + std::copy (reinterpret_cast (val_a.mv_data), reinterpret_cast (val_a.mv_data) + sizeof (*this), reinterpret_cast (this)); +} + +rai::pending_info_v3::pending_info_v3 (rai::account const & source_a, rai::amount const & amount_a, rai::account const & destination_a) : +source (source_a), +amount (amount_a), +destination (destination_a) +{ +} + +void rai::pending_info_v3::serialize (rai::stream & stream_a) const +{ + rai::write (stream_a, source.bytes); + rai::write (stream_a, amount.bytes); + rai::write (stream_a, destination.bytes); +} + +bool rai::pending_info_v3::deserialize (rai::stream & stream_a) +{ + auto result (rai::read (stream_a, source.bytes)); + if (!result) + { + result = rai::read (stream_a, amount.bytes); + if (!result) + { + result = rai::read (stream_a, destination.bytes); + } + } + return result; +} + +bool rai::pending_info_v3::operator == (rai::pending_info_v3 const & other_a) const +{ + return source == other_a.source && amount == other_a.amount && destination == other_a.destination; +} + +rai::mdb_val rai::pending_info_v3::val () const +{ + return rai::mdb_val (sizeof (*this), const_cast (this)); +} diff --git a/rai/versioning.hpp b/rai/versioning.hpp index 1676afce..63f748d0 100644 --- a/rai/versioning.hpp +++ b/rai/versioning.hpp @@ -19,4 +19,18 @@ public: rai::amount balance; uint64_t modified; }; -} \ No newline at end of file +class pending_info_v3 +{ +public: + pending_info_v3 (); + pending_info_v3 (MDB_val const &); + pending_info_v3 (rai::account const &, rai::amount const &, rai::account const &); + void serialize (rai::stream &) const; + bool deserialize (rai::stream &); + bool operator == (rai::pending_info_v3 const &) const; + rai::mdb_val val () const; + rai::account source; + rai::amount amount; + rai::account destination; +}; +}