Skip to content

Commit

Permalink
Refactoring epochs (#2268)
Browse files Browse the repository at this point in the history
* Splitting epoch concepts in to their own class.

* Holding reference to ledger for is_epoch_link check.

* Removing parameterization of epoch link/signer.

* Encapsulating epoch_signer

* Encapsulating epoch_link.

* Encapsulating epoch data in its own class.

* Moving epochs in to ledger constants.

* Reworking epochs::signer function to use nano::epoch.

* Allow there to be multiple epochs.

* Removing unused parameter.

* Eliminate extra account_info_get and confirmation_height_exists check for each change_latest call.

* Encapsulating account_info::epoch

* Formatting.

* Fix compile error within #define.

* Fixing epoch call sites in other targets.

* Fixing some unused variable warnings.

* Removing epoch_2 enum which isn't yet used.

* Adding epoch hash specialization.
  • Loading branch information
clemahieu authored Sep 7, 2019
1 parent 72c7317 commit c20c265
Show file tree
Hide file tree
Showing 28 changed files with 269 additions and 195 deletions.
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_executable (core_test
difficulty.cpp
distributed_work.cpp
entry.cpp
epochs.cpp
gap_cache.cpp
ipc.cpp
ledger.cpp
Expand Down
20 changes: 9 additions & 11 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,11 +1322,11 @@ TEST (mdb_block_store, upgrade_sideband_epoch)
nano::mdb_store store (logger, path);
ASSERT_FALSE (error);
nano::stat stat;
nano::ledger ledger (store, stat, 42, nano::test_genesis_key.pub);
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.version_put (transaction, 11);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
nano::state_block block1 (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount, 42, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
nano::state_block block1 (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount, ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
hash2 = block1.hash ();
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, block1).code);
ASSERT_EQ (nano::epoch::epoch_1, store.block_version (transaction, hash2));
Expand All @@ -1337,7 +1337,7 @@ TEST (mdb_block_store, upgrade_sideband_epoch)
nano::logger_mt logger;
nano::mdb_store store (logger, path);
nano::stat stat;
nano::ledger ledger (store, stat, 42, nano::test_genesis_key.pub);
nano::ledger ledger (store, stat);
ASSERT_FALSE (error);
auto transaction (store.tx_begin_write ());
ASSERT_TRUE (store.full_sideband (transaction));
Expand All @@ -1354,15 +1354,13 @@ TEST (mdb_block_store, sideband_height)
{
nano::logger_mt logger;
nano::genesis genesis;
nano::keypair epoch_key;
nano::keypair key1;
nano::keypair key2;
nano::keypair key3;
nano::mdb_store store (logger, nano::unique_path ());
ASSERT_FALSE (store.init_error ());
nano::stat stat;
nano::ledger ledger (store, stat);
ledger.epoch_signer = epoch_key.pub;
auto transaction (store.tx_begin_write ());
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
Expand All @@ -1380,10 +1378,10 @@ TEST (mdb_block_store, sideband_height)
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, state_send3).code);
nano::state_block state_open (key1.pub, 0, 0, nano::Gxrb_ratio, state_send1.hash (), key1.prv, key1.pub, pool.generate (key1.pub));
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, state_open).code);
nano::state_block epoch (key1.pub, state_open.hash (), 0, nano::Gxrb_ratio, ledger.epoch_link, epoch_key.prv, epoch_key.pub, pool.generate (state_open.hash ()));
nano::state_block epoch (key1.pub, state_open.hash (), 0, nano::Gxrb_ratio, ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (state_open.hash ()));
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, epoch).code);
ASSERT_EQ (nano::epoch::epoch_1, store.block_version (transaction, epoch.hash ()));
nano::state_block epoch_open (key2.pub, 0, 0, 0, ledger.epoch_link, epoch_key.prv, epoch_key.pub, pool.generate (key2.pub));
nano::state_block epoch_open (key2.pub, 0, 0, 0, ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (key2.pub));
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, epoch_open).code);
ASSERT_EQ (nano::epoch::epoch_1, store.block_version (transaction, epoch_open.hash ()));
nano::state_block state_receive (key2.pub, epoch_open.hash (), 0, nano::Gxrb_ratio, state_send2.hash (), key2.prv, key2.pub, pool.generate (epoch_open.hash ()));
Expand Down Expand Up @@ -1880,8 +1878,8 @@ void modify_account_info_to_v13 (nano::mdb_store & store, nano::transaction cons
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));
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);
}
Expand All @@ -1892,8 +1890,8 @@ void modify_account_info_to_v14 (nano::mdb_store & store, nano::transaction cons
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));
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);
}
Expand Down
6 changes: 3 additions & 3 deletions nano/core_test/conflicts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ TEST (conflicts, adjusted_difficulty)
node1.process_active (send3);
auto send4 (std::make_shared<nano::state_block> (key1.pub, send3->hash (), key1.pub, 0, key3.pub, key1.prv, key1.pub, system.work.generate (send3->hash ())));
node1.process_active (send4);
ASSERT_EQ (node1.ledger.epoch_signer, nano::test_genesis_key.pub);
auto open_epoch1 (std::make_shared<nano::state_block> (key2.pub, 0, 0, 0, node1.ledger.epoch_link, nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (key2.pub)));
ASSERT_EQ (node1.ledger.signer (node1.ledger.link (nano::epoch::epoch_1)), nano::test_genesis_key.pub);
auto open_epoch1 (std::make_shared<nano::state_block> (key2.pub, 0, 0, 0, node1.ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (key2.pub)));
node1.process_active (open_epoch1);
auto receive2 (std::make_shared<nano::state_block> (key2.pub, open_epoch1->hash (), 0, nano::xrb_ratio, send3->hash (), key2.prv, key2.pub, system.work.generate (open_epoch1->hash ())));
node1.process_active (receive2);
Expand Down Expand Up @@ -280,7 +280,7 @@ TEST (conflicts, adjusted_difficulty)
ASSERT_GT (adjusted_difficulties.find (open2->hash ())->second, adjusted_difficulties.find (change1->hash ())->second);
// Independent elections can have higher difficulty than adjusted tree
nano::keypair key4;
auto open_epoch2 (std::make_shared<nano::state_block> (key4.pub, 0, 0, 0, node1.ledger.epoch_link, nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (key4.pub, adjusted_difficulties.find (send1->hash ())->second)));
auto open_epoch2 (std::make_shared<nano::state_block> (key4.pub, 0, 0, 0, node1.ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (key4.pub, adjusted_difficulties.find (send1->hash ())->second)));
uint64_t difficulty;
ASSERT_FALSE (nano::work_validate (*open_epoch2, &difficulty));
ASSERT_GT (difficulty, adjusted_difficulties.find (send1->hash ())->second);
Expand Down
22 changes: 22 additions & 0 deletions nano/core_test/epochs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <gtest/gtest.h>

#include <nano/secure/common.hpp>
#include <nano/secure/epoch.hpp>

TEST (epochs, is_epoch_link)
{
nano::epochs epochs;
nano::keypair key1;
nano::keypair key2;
ASSERT_FALSE (epochs.is_epoch_link (42));
ASSERT_FALSE (epochs.is_epoch_link (43));
epochs.add (nano::epoch::epoch_1, key1.pub, 42);
ASSERT_TRUE (epochs.is_epoch_link (42));
ASSERT_FALSE (epochs.is_epoch_link (43));
/*epochs.add (nano::epoch::epoch_2, key2.pub, 43);
ASSERT_TRUE (epochs.is_epoch_link (43));
ASSERT_EQ (key1.pub, epochs.signer (nano::epoch::epoch_1));
ASSERT_EQ (key2.pub, epochs.signer (nano::epoch::epoch_2));
ASSERT_EQ (nano::uint256_union (42), epochs.link (nano::epoch::epoch_1));
ASSERT_EQ (nano::uint256_union (43), epochs.link (nano::epoch::epoch_2));*/
}
Loading

0 comments on commit c20c265

Please sign in to comment.