Attach epoch to mdb_val since the block store needs to encode this information anyway.

This cleans up a lot of call-site logic to figure out epoch of account_info and pending_info.
This commit is contained in:
clemahieu 2018-08-07 14:35:20 +01:00
commit c6ac2e2445
14 changed files with 95 additions and 80 deletions

View file

@ -134,7 +134,7 @@ TEST (block_store, pending_iterator)
rai::pending_key key1 (current->first);
ASSERT_EQ (rai::account (1), key1.account);
ASSERT_EQ (rai::block_hash (2), key1.hash);
rai::pending_info pending (current->second, current->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info pending (current->second);
ASSERT_EQ (rai::account (2), pending.source);
ASSERT_EQ (rai::amount (3), pending.amount);
ASSERT_EQ (rai::epoch::epoch_1, pending.epoch);
@ -335,7 +335,7 @@ TEST (block_store, one_account)
auto end (store.latest_end ());
ASSERT_NE (end, begin);
ASSERT_EQ (account, begin->first.uint256 ());
rai::account_info info (begin->second, begin->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (begin->second);
ASSERT_EQ (hash, info.head);
ASSERT_EQ (42, info.balance.number ());
ASSERT_EQ (100, info.modified);
@ -381,7 +381,7 @@ TEST (block_store, two_account)
auto end (store.latest_end ());
ASSERT_NE (end, begin);
ASSERT_EQ (account1, begin->first.uint256 ());
rai::account_info info1 (begin->second, begin->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info1 (begin->second);
ASSERT_EQ (hash1, info1.head);
ASSERT_EQ (42, info1.balance.number ());
ASSERT_EQ (100, info1.modified);
@ -389,7 +389,7 @@ TEST (block_store, two_account)
++begin;
ASSERT_NE (end, begin);
ASSERT_EQ (account2, begin->first.uint256 ());
rai::account_info info2 (begin->second, begin->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info2 (begin->second);
ASSERT_EQ (hash2, info2.head);
ASSERT_EQ (84, info2.balance.number ());
ASSERT_EQ (200, info2.modified);

View file

@ -358,7 +358,7 @@ void rai::frontier_req_client::next (MDB_txn * transaction_a)
if (iterator != connection->node->store.latest_end ())
{
current = rai::account (iterator->first.uint256 ());
info = rai::account_info (iterator->second, iterator->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
info = rai::account_info (iterator->second);
}
else
{
@ -2137,7 +2137,7 @@ void rai::frontier_req_server::next ()
if (iterator != connection->node->store.latest_end ())
{
current = rai::uint256_union (iterator->first.uint256 ());
info = rai::account_info (iterator->second, iterator->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
info = rai::account_info (iterator->second);
}
else
{

View file

@ -782,7 +782,7 @@ void rai::rpc_handler::accounts_pending ()
}
else
{
rai::pending_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info info (i->second);
if (info.amount.number () >= threshold.number ())
{
if (source)
@ -1392,7 +1392,7 @@ void rai::rpc_handler::delegators ()
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i)
{
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
if (block->representative () == account)
@ -1416,7 +1416,7 @@ void rai::rpc_handler::delegators_count ()
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i)
{
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
if (block->representative () == account)
@ -1468,7 +1468,7 @@ void rai::rpc_handler::frontiers ()
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && frontiers.size () < count; ++i)
{
frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0).head.to_string ());
frontiers.put (rai::account (i->first.uint256 ()).to_account (), rai::account_info (i->second).head.to_string ());
}
response_l.add_child ("frontiers", frontiers);
}
@ -1778,7 +1778,7 @@ void rai::rpc_handler::ledger ()
{
for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n && accounts.size () < count; ++i)
{
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
if (info.modified >= modified_since)
{
rai::account account (i->first.uint256 ());
@ -1816,7 +1816,7 @@ void rai::rpc_handler::ledger ()
std::vector<std::pair<rai::uint128_union, rai::account>> ledger_l;
for (auto i (node.store.latest_begin (transaction, start)), n (node.store.latest_end ()); i != n; ++i)
{
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
rai::uint128_union balance (info.balance);
if (info.modified >= modified_since)
{
@ -1974,7 +1974,7 @@ void rai::rpc_handler::pending ()
}
else
{
rai::pending_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info info (i->second);
if (info.amount.number () >= threshold.number ())
{
if (source || min_version)
@ -3142,7 +3142,7 @@ void rai::rpc_handler::wallet_pending ()
}
else
{
rai::pending_info info (ii->second, ii->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info info (ii->second);
if (info.amount.number () >= threshold.number ())
{
if (source || min_version)

View file

@ -166,7 +166,7 @@ void rai::system::generate_receive (rai::node & node_a)
if (i != node_a.store.pending_end ())
{
rai::pending_key send_hash (i->first);
rai::pending_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info info (i->second);
send_block = node_a.store.block_get (transaction, send_hash.hash);
}
}

View file

@ -1132,7 +1132,7 @@ bool rai::wallet::search_pending ()
{
rai::pending_key key (j->first);
auto hash (key.hash);
rai::pending_info pending (j->second, j->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::pending_info pending (j->second);
auto amount (pending.amount.number ());
if (node.config.receive_minimum.number () <= amount)
{

View file

@ -1857,7 +1857,7 @@ void rai_qt::advanced_actions::refresh_ledger ()
{
QList<QStandardItem *> items;
items.push_back (new QStandardItem (QString (rai::block_hash (i->first.uint256 ()).to_account ().c_str ())));
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
std::string balance;
rai::amount (info.balance.number () / wallet.rendering_ratio).encode_dec (balance);
items.push_back (new QStandardItem (QString (balance.c_str ())));

View file

@ -131,7 +131,7 @@ int main (int argc, char * const * argv)
std::map<rai::account, rai::uint128_t> calculated;
for (auto i (node.node->store.latest_begin (transaction)), n (node.node->store.latest_end ()); i != n; ++i)
{
rai::account_info info (i->second, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
rai::account_info info (i->second);
rai::block_hash rep_block (node.node->ledger.representative_calculated (transaction, info.head));
std::unique_ptr<rai::block> block (node.node->store.block_get (transaction, rep_block));
calculated[block->representative ()] += info.balance.number ();

View file

@ -60,9 +60,11 @@ std::pair<rai::mdb_val, rai::mdb_val> * rai::store_iterator::operator-> ()
return &current;
}
rai::store_iterator::store_iterator (MDB_txn * transaction_a, MDB_dbi db_a) :
rai::store_iterator::store_iterator (MDB_txn * transaction_a, MDB_dbi db_a, rai::epoch epoch_a) :
cursor (nullptr)
{
current.first.epoch = epoch_a;
current.second.epoch = epoch_a;
auto status (mdb_cursor_open (transaction_a, db_a, &cursor));
assert (status == 0);
auto status2 (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_FIRST));
@ -83,9 +85,11 @@ cursor (nullptr)
{
}
rai::store_iterator::store_iterator (MDB_txn * transaction_a, MDB_dbi db_a, MDB_val const & val_a) :
rai::store_iterator::store_iterator (MDB_txn * transaction_a, MDB_dbi db_a, MDB_val const & val_a, rai::epoch epoch_a) :
cursor (nullptr)
{
current.first.epoch = epoch_a;
current.second.epoch = epoch_a;
auto status (mdb_cursor_open (transaction_a, db_a, &cursor));
assert (status == 0);
current.first.value = val_a;
@ -167,8 +171,8 @@ bool rai::store_iterator::operator!= (rai::store_iterator const & other_a) const
void rai::store_iterator::clear ()
{
current.first = rai::mdb_val ();
current.second = rai::mdb_val ();
current.first = rai::mdb_val (current.first.epoch);
current.second = rai::mdb_val (current.second.epoch);
}
std::pair<MDB_cursor **, rai::merged_store_kv *> rai::store_merge_iterator::cursor_current ()
@ -217,12 +221,18 @@ rai::merged_store_kv * rai::store_merge_iterator::operator-> ()
return cursor_current ().second;
}
rai::merged_store_kv::merged_store_kv (rai::epoch epoch_a) :
first (epoch_a),
second (epoch_a)
{
}
rai::store_merge_iterator::store_merge_iterator (MDB_txn * transaction_a, MDB_dbi db1_a, MDB_dbi db2_a) :
cursor1 (nullptr),
cursor2 (nullptr)
cursor2 (nullptr),
current1 (rai::epoch::epoch_0),
current2 (rai::epoch::epoch_1)
{
current1.from_secondary_store = false;
current2.from_secondary_store = true;
auto status (mdb_cursor_open (transaction_a, db1_a, &cursor1));
assert (status == 0);
status = mdb_cursor_get (cursor1, &current1.first.value, &current1.second.value, MDB_FIRST);
@ -234,8 +244,8 @@ cursor2 (nullptr)
}
else
{
current1.first = rai::mdb_val ();
current1.second = rai::mdb_val ();
current1.first = rai::mdb_val (rai::epoch::epoch_0);
current1.second = rai::mdb_val (rai::epoch::epoch_1);
}
status = mdb_cursor_open (transaction_a, db2_a, &cursor2);
assert (status == 0);
@ -248,25 +258,25 @@ cursor2 (nullptr)
}
else
{
current2.first = rai::mdb_val ();
current2.second = rai::mdb_val ();
current2.first = rai::mdb_val (rai::epoch::epoch_0);
current2.second = rai::mdb_val (rai::epoch::epoch_1);
}
}
rai::store_merge_iterator::store_merge_iterator (std::nullptr_t) :
cursor1 (nullptr),
cursor2 (nullptr)
cursor2 (nullptr),
current1 (rai::epoch::epoch_0),
current2 (rai::epoch::epoch_1)
{
current1.from_secondary_store = false;
current2.from_secondary_store = true;
}
rai::store_merge_iterator::store_merge_iterator (MDB_txn * transaction_a, MDB_dbi db1_a, MDB_dbi db2_a, MDB_val const & val_a) :
cursor1 (nullptr),
cursor2 (nullptr)
cursor2 (nullptr),
current1 (rai::epoch::epoch_0),
current2 (rai::epoch::epoch_1)
{
current1.from_secondary_store = false;
current2.from_secondary_store = true;
auto status (mdb_cursor_open (transaction_a, db1_a, &cursor1));
assert (status == 0);
current1.first.value = val_a;
@ -279,8 +289,8 @@ cursor2 (nullptr)
}
else
{
current1.first = rai::mdb_val ();
current1.second = rai::mdb_val ();
current1.first = rai::mdb_val (rai::epoch::epoch_0);
current1.second = rai::mdb_val (rai::epoch::epoch_1);
}
status = mdb_cursor_open (transaction_a, db2_a, &cursor2);
assert (status == 0);
@ -294,8 +304,8 @@ cursor2 (nullptr)
}
else
{
current2.first = rai::mdb_val ();
current2.second = rai::mdb_val ();
current2.first = rai::mdb_val (rai::epoch::epoch_0);
current2.second = rai::mdb_val (rai::epoch::epoch_1);
}
}
@ -742,7 +752,7 @@ void rai::block_store::upgrade_v9_to_v10 (MDB_txn * transaction_a)
version_put (transaction_a, 10);
for (auto i (latest_v0_begin (transaction_a)), n (latest_v0_end ()); i != n; ++i)
{
rai::account_info info (i->second, rai::epoch::epoch_0);
rai::account_info info (i->second);
if (info.block_count >= block_info_max)
{
rai::account account (i->first.uint256 ());
@ -1341,13 +1351,13 @@ bool rai::block_store::pending_get (MDB_txn * transaction_a, rai::pending_key co
rai::store_iterator rai::block_store::pending_v0_begin (MDB_txn * transaction_a, rai::pending_key const & key_a)
{
rai::store_iterator result (transaction_a, pending_v0, key_a.val ());
rai::store_iterator result (transaction_a, pending_v0, key_a.val (), rai::epoch::epoch_0);
return result;
}
rai::store_iterator rai::block_store::pending_v0_begin (MDB_txn * transaction_a)
{
rai::store_iterator result (transaction_a, pending_v0);
rai::store_iterator result (transaction_a, pending_v0, rai::epoch::epoch_0);
return result;
}
@ -1359,13 +1369,13 @@ rai::store_iterator rai::block_store::pending_v0_end ()
rai::store_iterator rai::block_store::pending_v1_begin (MDB_txn * transaction_a, rai::pending_key const & key_a)
{
rai::store_iterator result (transaction_a, pending_v1, key_a.val ());
rai::store_iterator result (transaction_a, pending_v1, key_a.val (), rai::epoch::epoch_1);
return result;
}
rai::store_iterator rai::block_store::pending_v1_begin (MDB_txn * transaction_a)
{
rai::store_iterator result (transaction_a, pending_v1);
rai::store_iterator result (transaction_a, pending_v1, rai::epoch::epoch_1);
return result;
}
@ -1667,13 +1677,13 @@ std::shared_ptr<rai::vote> rai::block_store::vote_max (MDB_txn * transaction_a,
rai::store_iterator rai::block_store::latest_v0_begin (MDB_txn * transaction_a, rai::account const & account_a)
{
rai::store_iterator result (transaction_a, accounts_v0, rai::mdb_val (account_a));
rai::store_iterator result (transaction_a, accounts_v0, rai::mdb_val (account_a), rai::epoch::epoch_0);
return result;
}
rai::store_iterator rai::block_store::latest_v0_begin (MDB_txn * transaction_a)
{
rai::store_iterator result (transaction_a, accounts_v0);
rai::store_iterator result (transaction_a, accounts_v0, rai::epoch::epoch_0);
return result;
}
@ -1685,13 +1695,13 @@ rai::store_iterator rai::block_store::latest_v0_end ()
rai::store_iterator rai::block_store::latest_v1_begin (MDB_txn * transaction_a, rai::account const & account_a)
{
rai::store_iterator result (transaction_a, accounts_v1, rai::mdb_val (account_a));
rai::store_iterator result (transaction_a, accounts_v1, rai::mdb_val (account_a), rai::epoch::epoch_1);
return result;
}
rai::store_iterator rai::block_store::latest_v1_begin (MDB_txn * transaction_a)
{
rai::store_iterator result (transaction_a, accounts_v1);
rai::store_iterator result (transaction_a, accounts_v1, rai::epoch::epoch_1);
return result;
}

View file

@ -10,9 +10,9 @@ namespace rai
class store_iterator
{
public:
store_iterator (MDB_txn *, MDB_dbi);
store_iterator (MDB_txn *, MDB_dbi, rai::epoch = rai::epoch::unspecified);
store_iterator (std::nullptr_t);
store_iterator (MDB_txn *, MDB_dbi, MDB_val const &);
store_iterator (MDB_txn *, MDB_dbi, MDB_val const &, rai::epoch = rai::epoch::unspecified);
store_iterator (rai::store_iterator &&);
store_iterator (rai::store_iterator const &) = delete;
~store_iterator ();
@ -34,9 +34,9 @@ public:
class merged_store_kv
{
public:
merged_store_kv (rai::epoch = rai::epoch::unspecified);
rai::mdb_val first;
rai::mdb_val second;
bool from_secondary_store;
};
/**

View file

@ -200,12 +200,13 @@ epoch (rai::epoch::epoch_0)
{
}
rai::account_info::account_info (MDB_val const & val_a, rai::epoch epoch_a) :
epoch (epoch_a)
rai::account_info::account_info (rai::mdb_val const & val_a) :
epoch (val_a.epoch)
{
assert (val_a.epoch == rai::epoch::epoch_0 || val_a.epoch == rai::epoch::epoch_1);
auto size (db_size ());
assert (val_a.mv_size == size);
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + size, reinterpret_cast<uint8_t *> (this));
assert (val_a.value.mv_size == size);
std::copy (reinterpret_cast<uint8_t const *> (val_a.value.mv_data), reinterpret_cast<uint8_t const *> (val_a.value.mv_data) + size, reinterpret_cast<uint8_t *> (this));
}
rai::account_info::account_info (rai::block_hash const & head_a, rai::block_hash const & rep_block_a, rai::block_hash const & open_block_a, rai::amount const & balance_a, uint64_t modified_a, uint64_t block_count_a, rai::epoch epoch_a) :
@ -303,14 +304,15 @@ epoch (rai::epoch::epoch_0)
{
}
rai::pending_info::pending_info (MDB_val const & val_a, rai::epoch epoch_a) :
epoch (epoch_a)
rai::pending_info::pending_info (rai::mdb_val const & val_a) :
epoch (val_a.epoch)
{
assert (val_a.epoch == rai::epoch::epoch_0 || val_a.epoch == rai::epoch::epoch_1);
auto db_size (sizeof (source) + sizeof (amount));
assert (val_a.mv_size == db_size);
assert (val_a.value.mv_size == db_size);
assert (reinterpret_cast<const uint8_t *> (this) == reinterpret_cast<const uint8_t *> (&source));
assert (reinterpret_cast<const uint8_t *> (&source) + sizeof (source) == reinterpret_cast<const uint8_t *> (&amount));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + db_size, reinterpret_cast<uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.value.mv_data), reinterpret_cast<uint8_t const *> (val_a.value.mv_data) + db_size, reinterpret_cast<uint8_t *> (this));
}
rai::pending_info::pending_info (rai::account const & source_a, rai::amount const & amount_a, rai::epoch epoch_a) :

View file

@ -107,17 +107,6 @@ public:
std::unique_ptr<rai::block> deserialize_block (MDB_val const &);
/**
* Tag for which epoch an entry belongs to
*/
enum class epoch : uint8_t
{
invalid = 0,
unspecified = 1,
epoch_0 = 2,
epoch_1 = 3
};
/**
* Latest information about an account
*/
@ -125,7 +114,7 @@ class account_info
{
public:
account_info ();
account_info (MDB_val const &, epoch);
account_info (rai::mdb_val const &);
account_info (rai::account_info const &) = default;
account_info (rai::block_hash const &, rai::block_hash const &, rai::block_hash const &, rai::amount const &, uint64_t, uint64_t, epoch);
void serialize (rai::stream &) const;
@ -151,7 +140,7 @@ class pending_info
{
public:
pending_info ();
pending_info (MDB_val const &, epoch);
pending_info (rai::mdb_val const &);
pending_info (rai::account const &, rai::amount const &, epoch);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);

View file

@ -666,12 +666,12 @@ rai::uint128_t rai::ledger::account_pending (MDB_txn * transaction_a, rai::accou
rai::account end (account_a.number () + 1);
for (auto i (store.pending_v0_begin (transaction_a, rai::pending_key (account_a, 0))), n (store.pending_v0_begin (transaction_a, rai::pending_key (end, 0))); i != n; ++i)
{
rai::pending_info info (i->second, rai::epoch::epoch_0);
rai::pending_info info (i->second);
result += info.amount.number ();
}
for (auto i (store.pending_v1_begin (transaction_a, rai::pending_key (account_a, 0))), n (store.pending_v1_begin (transaction_a, rai::pending_key (end, 0))); i != n; ++i)
{
rai::pending_info info (i->second, rai::epoch::epoch_1);
rai::pending_info info (i->second);
result += info.amount.number ();
}
return result;

View file

@ -102,13 +102,15 @@ rai::mdb_env::operator MDB_env * () const
return environment;
}
rai::mdb_val::mdb_val () :
value ({ 0, nullptr })
rai::mdb_val::mdb_val (rai::epoch epoch_a) :
value ({ 0, nullptr }),
epoch (epoch_a)
{
}
rai::mdb_val::mdb_val (MDB_val const & value_a) :
value (value_a)
rai::mdb_val::mdb_val (MDB_val const & value_a, rai::epoch epoch_a) :
value (value_a),
epoch (epoch_a)
{
}

View file

@ -113,14 +113,25 @@ public:
MDB_env * environment;
};
/**
* Tag for which epoch an entry belongs to
*/
enum class epoch : uint8_t
{
invalid = 0,
unspecified = 1,
epoch_0 = 2,
epoch_1 = 3
};
/**
* Encapsulates MDB_val and provides uint256_union conversion of the data.
*/
class mdb_val
{
public:
mdb_val ();
mdb_val (MDB_val const &);
mdb_val (rai::epoch = rai::epoch::unspecified);
mdb_val (MDB_val const &, rai::epoch = rai::epoch::unspecified);
mdb_val (size_t, void *);
mdb_val (rai::uint128_union const &);
mdb_val (rai::uint256_union const &);
@ -130,6 +141,7 @@ public:
operator MDB_val * () const;
operator MDB_val const & () const;
MDB_val value;
rai::epoch epoch;
};
/**