Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace account_info::rep_block with representative account #2283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ TEST (mdb_block_store, upgrade_v2_v3)
ASSERT_EQ (6, ledger.weight (transaction, key2.pub));
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction, nano::test_genesis_key.pub, info));
info.rep_block = 42;
nano::account_info_v5 info_old (info.head, info.rep_block, info.open_block, info.balance, info.modified);
auto rep_block = ledger.representative (transaction, ledger.latest (transaction, nano::test_genesis_key.pub));
nano::account_info_v5 info_old (info.head, rep_block, info.open_block, info.balance, info.modified);
auto status (mdb_put (store.env.tx (transaction), store.accounts_v0, nano::mdb_val (nano::test_genesis_key.pub), nano::mdb_val (sizeof (info_old), &info_old), 0));
(void)status;
assert (status == 0);
Expand All @@ -829,7 +829,7 @@ TEST (mdb_block_store, upgrade_v2_v3)
ASSERT_EQ (0, ledger.weight (transaction, key2.pub));
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction, nano::test_genesis_key.pub, info));
ASSERT_EQ (change_hash, info.rep_block);
ASSERT_EQ (change_hash, ledger.representative (transaction, ledger.latest (transaction, nano::test_genesis_key.pub)));
}

TEST (mdb_block_store, upgrade_v3_v4)
Expand Down Expand Up @@ -1767,7 +1767,9 @@ TEST (block_store, upgrade_confirmation_height_many)
for (auto i = 0; i < total_num_accounts - 1; ++i)
{
nano::account account (i);
nano::open_block open (1, 2, 3, nullptr);
nano::open_block open (1, nano::genesis_account, 3, nullptr);
nano::block_sideband sideband (nano::block_type::open, 0, 0, 0, 0, 0);
store.block_put (transaction, open.hash (), open, sideband);
nano::account_info_v13 account_info_v13 (open.hash (), open.hash (), open.hash (), 3, 4, 1, nano::epoch::epoch_1);
auto status (mdb_put (store.env.tx (transaction), store.accounts_v1, nano::mdb_val (account), nano::mdb_val (account_info_v13), 0));
ASSERT_EQ (status, 0);
Expand Down Expand Up @@ -1875,32 +1877,38 @@ TEST (block_store, rocksdb_force_test_env_variable)
namespace
{
// These functions take the latest account_info and create a legacy one so that upgrade tests can be emulated more easily.
void modify_account_info_to_v13 (nano::mdb_store & store, nano::transaction const & transaction_a, nano::account const & account)
void modify_account_info_to_v13 (nano::mdb_store & store, nano::transaction const & transaction, nano::account const & account)
{
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction_a, account, info));
nano::account_info_v13 account_info_v13 (info.head, info.rep_block, info.open_block, info.balance, info.modified, info.block_count, info.epoch);
auto status (mdb_put (store.env.tx (transaction_a), store.get_account_db (info.epoch) == nano::tables::accounts_v0 ? store.accounts_v0 : store.accounts_v1, nano::mdb_val (account), nano::mdb_val (account_info_v13), 0));
ASSERT_FALSE (store.account_get (transaction, account, info));
nano::representative_visitor visitor (transaction, store);
visitor.compute (info.head);
nano::account_info_v13 account_info_v13 (info.head, visitor.result, info.open_block, info.balance, info.modified, info.block_count, info.epoch);
auto status (mdb_put (store.env.tx (transaction), store.get_account_db (info.epoch) == nano::tables::accounts_v0 ? store.accounts_v0 : store.accounts_v1, nano::mdb_val (account), nano::mdb_val (account_info_v13), 0));
(void)status;
assert (status == 0);
}

void modify_account_info_to_v14 (nano::mdb_store & store, nano::transaction const & transaction_a, nano::account const & account, uint64_t confirmation_height)
void modify_account_info_to_v14 (nano::mdb_store & store, nano::transaction const & transaction, nano::account const & account, uint64_t confirmation_height)
{
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction_a, account, info));
nano::account_info_v14 account_info_v14 (info.head, info.rep_block, info.open_block, info.balance, info.modified, info.block_count, confirmation_height, info.epoch);
auto status (mdb_put (store.env.tx (transaction_a), store.get_account_db (info.epoch) == nano::tables::accounts_v0 ? store.accounts_v0 : store.accounts_v1, nano::mdb_val (account), nano::mdb_val (account_info_v14), 0));
ASSERT_FALSE (store.account_get (transaction, account, info));
nano::representative_visitor visitor (transaction, store);
visitor.compute (info.head);
nano::account_info_v14 account_info_v14 (info.head, visitor.result, info.open_block, info.balance, info.modified, info.block_count, confirmation_height, info.epoch);
auto status (mdb_put (store.env.tx (transaction), store.get_account_db (info.epoch) == nano::tables::accounts_v0 ? store.accounts_v0 : store.accounts_v1, nano::mdb_val (account), nano::mdb_val (account_info_v14), 0));
(void)status;
assert (status == 0);
}

void modify_genesis_account_info_to_v5 (nano::mdb_store & store, nano::transaction const & transaction_a)
void modify_genesis_account_info_to_v5 (nano::mdb_store & store, nano::transaction const & transaction)
{
nano::account_info info;
store.account_get (transaction_a, nano::test_genesis_key.pub, info);
nano::account_info_v5 info_old (info.head, info.rep_block, info.open_block, info.balance, info.modified);
auto status (mdb_put (store.env.tx (transaction_a), store.accounts_v0, nano::mdb_val (nano::test_genesis_key.pub), nano::mdb_val (sizeof (info_old), &info_old), 0));
store.account_get (transaction, nano::test_genesis_key.pub, info);
nano::representative_visitor visitor (transaction, store);
visitor.compute (info.head);
nano::account_info_v5 info_old (info.head, visitor.result, info.open_block, info.balance, info.modified);
auto status (mdb_put (store.env.tx (transaction), store.accounts_v0, nano::mdb_val (nano::test_genesis_key.pub), nano::mdb_val (sizeof (info_old), &info_old), 0));
(void)status;
assert (status == 0);
}
Expand Down
8 changes: 4 additions & 4 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ TEST (ledger, rollback_representation)
ASSERT_EQ (nano::genesis_amount - 1, ledger.weight (transaction, key4.pub));
nano::account_info info1;
ASSERT_FALSE (store->account_get (transaction, key2.pub, info1));
ASSERT_EQ (open.hash (), info1.rep_block);
ASSERT_EQ (key4.pub, info1.representative);
ASSERT_FALSE (ledger.rollback (transaction, receive1.hash ()));
nano::account_info info2;
ASSERT_FALSE (store->account_get (transaction, key2.pub, info2));
ASSERT_EQ (open.hash (), info2.rep_block);
ASSERT_EQ (key4.pub, info2.representative);
ASSERT_EQ (0, ledger.weight (transaction, key2.pub));
ASSERT_EQ (nano::genesis_amount - 50, ledger.weight (transaction, key4.pub));
ASSERT_FALSE (ledger.rollback (transaction, open.hash ()));
Expand All @@ -308,11 +308,11 @@ TEST (ledger, rollback_representation)
ASSERT_EQ (nano::genesis_amount, ledger.weight (transaction, key3.pub));
nano::account_info info3;
ASSERT_FALSE (store->account_get (transaction, nano::test_genesis_key.pub, info3));
ASSERT_EQ (change2.hash (), info3.rep_block);
ASSERT_EQ (key3.pub, info3.representative);
ASSERT_FALSE (ledger.rollback (transaction, change2.hash ()));
nano::account_info info4;
ASSERT_FALSE (store->account_get (transaction, nano::test_genesis_key.pub, info4));
ASSERT_EQ (change1.hash (), info4.rep_block);
ASSERT_EQ (key5.pub, info4.representative);
ASSERT_EQ (nano::genesis_amount, ledger.weight (transaction, key5.pub));
ASSERT_EQ (0, ledger.weight (transaction, key3.pub));
}
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ TEST (node, balance)
TEST (node, representative)
{
nano::system system (24000, 1);
auto block1 (system.nodes[0]->representative (nano::test_genesis_key.pub));
auto block1 (system.nodes[0]->rep_block (nano::test_genesis_key.pub));
{
auto transaction (system.nodes[0]->store.tx_begin_read ());
ASSERT_TRUE (system.nodes[0]->ledger.store.block_exists (transaction, block1));
}
nano::keypair key;
ASSERT_TRUE (system.nodes[0]->representative (key.pub).is_zero ());
ASSERT_TRUE (system.nodes[0]->rep_block (key.pub).is_zero ());
}

TEST (node, send_unkeyed)
Expand Down
6 changes: 3 additions & 3 deletions nano/core_test/versioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST (versioning, account_info_v1)
ASSERT_EQ (v1.balance, v_latest.balance);
ASSERT_EQ (v1.head, v_latest.head);
ASSERT_EQ (v1.modified, v_latest.modified);
ASSERT_EQ (v1.rep_block, v_latest.rep_block);
ASSERT_EQ (v1.rep_block, open.hash ());
ASSERT_EQ (1, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
Expand Down Expand Up @@ -69,7 +69,7 @@ TEST (versioning, account_info_v5)
ASSERT_EQ (v5.balance, v_latest.balance);
ASSERT_EQ (v5.head, v_latest.head);
ASSERT_EQ (v5.modified, v_latest.modified);
ASSERT_EQ (v5.rep_block, v_latest.rep_block);
ASSERT_EQ (v5.rep_block, open.hash ());
ASSERT_EQ (1, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
Expand Down Expand Up @@ -105,7 +105,7 @@ TEST (versioning, account_info_v13)
ASSERT_EQ (v13.balance, v_latest.balance);
ASSERT_EQ (v13.head, v_latest.head);
ASSERT_EQ (v13.modified, v_latest.modified);
ASSERT_EQ (v13.rep_block, v_latest.rep_block);
ASSERT_EQ (v13.rep_block, open.hash ());
ASSERT_EQ (v13.block_count, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ TEST (wallet, change)
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
nano::keypair key2;
auto block1 (system.nodes[0]->representative (nano::test_genesis_key.pub));
auto block1 (system.nodes[0]->rep_block (nano::test_genesis_key.pub));
ASSERT_FALSE (block1.is_zero ());
ASSERT_NE (nullptr, system.wallet (0)->change_action (nano::test_genesis_key.pub, key2.pub));
auto block2 (system.nodes[0]->representative (nano::test_genesis_key.pub));
auto block2 (system.nodes[0]->rep_block (nano::test_genesis_key.pub));
ASSERT_FALSE (block2.is_zero ());
ASSERT_NE (block1, block2);
}
Expand Down
3 changes: 2 additions & 1 deletion nano/core_test/wallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ TEST (wallets, upgrade)

nano::account_info info;
ASSERT_FALSE (mdb_store.account_get (transaction_destination, nano::genesis_account, info));
nano::account_info_v13 account_info_v13 (info.head, info.rep_block, info.open_block, info.balance, info.modified, info.block_count, info.epoch);
auto rep_block = node1->rep_block (nano::genesis_account);
nano::account_info_v13 account_info_v13 (info.head, rep_block, info.open_block, info.balance, info.modified, info.block_count, info.epoch);
auto status (mdb_put (mdb_store.env.tx (transaction_destination), mdb_store.get_account_db (info.epoch) == nano::tables::accounts_v0 ? mdb_store.accounts_v0 : mdb_store.accounts_v1, nano::mdb_val (nano::test_genesis_key.pub), nano::mdb_val (account_info_v13), 0));
(void)status;
assert (status == 0);
Expand Down
8 changes: 4 additions & 4 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ int main (int argc, char * const * argv)
auto block (node.node->store.block_get (transaction, hash, &sideband)); // Block data
uint64_t height (0);
uint64_t previous_timestamp (0);
nano::block_hash calculated_representative_block (0);
nano::account calculated_representative (0);
while (!hash.is_zero () && block != nullptr)
{
++block_count;
Expand Down Expand Up @@ -911,7 +911,7 @@ int main (int argc, char * const * argv)
// Calculate representative block
if (block->type () == nano::block_type::open || block->type () == nano::block_type::change || block->type () == nano::block_type::state)
{
calculated_representative_block = hash;
calculated_representative = block->representative ();
}
// Retrieving successor block hash
hash = node.node->store.block_successor (transaction, hash);
Expand All @@ -937,9 +937,9 @@ int main (int argc, char * const * argv)
std::cerr << boost::str (boost::format ("Incorrect frontier for account %1%. Actual: %2%. Expected: %3%\n") % account.to_account () % calculated_hash.to_string () % info.head.to_string ());
}
// Check account representative block
if (info.rep_block != calculated_representative_block)
if (info.representative != calculated_representative)
{
std::cerr << boost::str (boost::format ("Incorrect representative block for account %1%. Actual: %2%. Expected: %3%\n") % account.to_account () % calculated_representative_block.to_string () % info.rep_block.to_string ());
std::cerr << boost::str (boost::format ("Incorrect representative for account %1%. Actual: %2%. Expected: %3%\n") % account.to_account () % calculated_representative.to_string () % info.representative.to_string ());
}
}
std::cout << boost::str (boost::format ("%1% accounts validated\n") % count);
Expand Down
40 changes: 12 additions & 28 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ void nano::json_handler::account_info ()
{
response_l.put ("frontier", info.head.to_string ());
response_l.put ("open_block", info.open_block.to_string ());
response_l.put ("representative_block", info.rep_block.to_string ());
response_l.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
response_l.put ("balance", balance);
Expand All @@ -538,9 +538,7 @@ void nano::json_handler::account_info ()
response_l.put ("confirmation_height", std::to_string (confirmation_height));
if (representative)
{
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
response_l.put ("representative", block->representative ().to_account ());
response_l.put ("representative", info.representative.to_account ());
}
if (weight)
{
Expand Down Expand Up @@ -659,9 +657,7 @@ void nano::json_handler::account_representative ()
nano::account_info info;
if (!node.store.account_get (transaction, account, info))
{
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
response_l.put ("representative", block->representative ().to_account ());
response_l.put ("representative", info.representative.to_account ());
}
else
{
Expand Down Expand Up @@ -1955,9 +1951,7 @@ void nano::json_handler::delegators ()
for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i)
{
nano::account_info const & info (i->second);
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
if (block->representative () == account)
if (info.representative == account)
{
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
Expand All @@ -1980,9 +1974,7 @@ void nano::json_handler::delegators_count ()
for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i)
{
nano::account_info const & info (i->second);
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
if (block->representative () == account)
if (info.representative == account)
{
++count;
}
Expand Down Expand Up @@ -2429,17 +2421,15 @@ void nano::json_handler::ledger ()
}
response_a.put ("frontier", info.head.to_string ());
response_a.put ("open_block", info.open_block.to_string ());
response_a.put ("representative_block", info.rep_block.to_string ());
response_a.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
response_a.put ("balance", balance);
response_a.put ("modified_timestamp", std::to_string (info.modified));
response_a.put ("block_count", std::to_string (info.block_count));
if (representative)
{
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
response_a.put ("representative", block->representative ().to_account ());
response_a.put ("representative", info.representative.to_account ());
}
if (weight)
{
Expand Down Expand Up @@ -2483,17 +2473,15 @@ void nano::json_handler::ledger ()
}
response_a.put ("frontier", info.head.to_string ());
response_a.put ("open_block", info.open_block.to_string ());
response_a.put ("representative_block", info.rep_block.to_string ());
response_a.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
std::string balance;
(i->first).encode_dec (balance);
response_a.put ("balance", balance);
response_a.put ("modified_timestamp", std::to_string (info.modified));
response_a.put ("block_count", std::to_string (info.block_count));
if (representative)
{
auto block (node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
response_a.put ("representative", block->representative ().to_account ());
response_a.put ("representative", info.representative.to_account ());
}
if (weight)
{
Expand Down Expand Up @@ -4235,17 +4223,15 @@ void nano::json_handler::wallet_ledger ()
boost::property_tree::ptree entry;
entry.put ("frontier", info.head.to_string ());
entry.put ("open_block", info.open_block.to_string ());
entry.put ("representative_block", info.rep_block.to_string ());
entry.put ("representative_block", node.ledger.representative (block_transaction, info.head).to_string ());
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
entry.put ("balance", balance);
entry.put ("modified_timestamp", std::to_string (info.modified));
entry.put ("block_count", std::to_string (info.block_count));
if (representative)
{
auto block (node.store.block_get (block_transaction, info.rep_block));
assert (block != nullptr);
entry.put ("representative", block->representative ().to_account ());
entry.put ("representative", info.representative.to_account ());
}
if (weight)
{
Expand Down Expand Up @@ -4392,9 +4378,7 @@ void nano::json_handler::wallet_representative_set ()
nano::account_info info;
if (!rpc_l->node.store.account_get (block_transaction, account, info))
{
auto block (rpc_l->node.store.block_get (block_transaction, info.rep_block));
assert (block != nullptr);
if (block->representative () != representative)
if (info.representative != representative)
{
accounts.push_back (account);
}
Expand Down
6 changes: 5 additions & 1 deletion nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ void nano::mdb_store::upgrade_v14_to_v15 (nano::write_transaction const & transa
for (; i != n; ++i)
{
auto const & account_info_v14 (i->second);
account_infos.emplace_back (i->first, nano::account_info{ account_info_v14.head, account_info_v14.rep_block, account_info_v14.open_block, account_info_v14.balance, account_info_v14.modified, account_info_v14.block_count, account_info_v14.epoch });

// Upgrade rep block to representative account
auto rep_block = block_get (transaction_a, account_info_v14.rep_block);
release_assert (rep_block != nullptr);
account_infos.emplace_back (i->first, nano::account_info{ account_info_v14.head, rep_block->representative (), account_info_v14.open_block, account_info_v14.balance, account_info_v14.modified, account_info_v14.block_count, account_info_v14.epoch });
confirmation_height_put (transaction_a, i->first, i->second.confirmation_height);
}

Expand Down
4 changes: 2 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,14 @@ nano::uint128_t nano::node::weight (nano::account const & account_a)
return ledger.weight (transaction, account_a);
}

nano::account nano::node::representative (nano::account const & account_a)
nano::block_hash nano::node::rep_block (nano::account const & account_a)
{
auto transaction (store.tx_begin_read ());
nano::account_info info;
nano::account result (0);
if (!store.account_get (transaction, account_a, info))
{
result = info.rep_block;
result = ledger.representative (transaction, info.head);
}
return result;
}
Expand Down
Loading