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

Move GetEpoch, IsEpochStart, and IsCheckpoint to finalization::Params #1087

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
24 changes: 6 additions & 18 deletions src/esperanza/finalizationstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool FinalizationState::operator!=(const FinalizationState &other) const {
Result FinalizationState::InitializeEpoch(blockchain::Height blockHeight) {
LOCK(cs_esperanza);

assert(IsEpochStart(blockHeight) &&
assert(m_settings.IsEpochStart(blockHeight) &&
"provided blockHeight is not the first block of a new epoch");

IncrementDynasty();
Expand Down Expand Up @@ -872,11 +872,7 @@ uint32_t FinalizationState::GetEpoch(const CBlockIndex &blockIndex) const {
}

uint32_t FinalizationState::GetEpoch(const blockchain::Height block_height) const {
uint32_t epoch = block_height / m_settings.epoch_length;
if (block_height % m_settings.epoch_length != 0) {
++epoch;
}
return epoch;
return m_settings.GetEpoch(block_height);
}

blockchain::Height FinalizationState::GetEpochStartHeight(const uint32_t epoch) const {
Expand Down Expand Up @@ -1005,15 +1001,15 @@ void FinalizationState::ProcessNewCommits(const CBlockIndex &block_index,
assert(m_status == NEW);
uint256 block_hash = block_index.GetBlockHash();

if (IsEpochStart(block_index.nHeight)) {
if (m_settings.IsEpochStart(block_index.nHeight)) {
InitializeEpoch(block_index.nHeight);
}

for (const auto &tx : txes) {
ProcessNewCommit(tx);
}

if (IsCheckpoint(block_index.nHeight)) {
if (m_settings.IsCheckpoint(block_index.nHeight)) {
LogPrint(BCLog::FINALIZATION, /* Continued */
"%s: Last block of the epoch, new m_recommended_target_hash=%s\n",
__func__, block_hash.GetHex());
Expand Down Expand Up @@ -1067,24 +1063,16 @@ uint256 FinalizationState::GetLastTxHash(const uint160 &validatorAddress) const
return validator.m_last_transaction_hash;
}

bool FinalizationState::IsEpochStart(blockchain::Height block_height) const {
return block_height % m_settings.epoch_length == 1;
}

bool FinalizationState::IsCheckpoint(blockchain::Height blockHeight) const {
return blockHeight % m_settings.epoch_length == 0;
}

bool FinalizationState::IsJustifiedCheckpoint(blockchain::Height blockHeight) const {
if (!IsCheckpoint(blockHeight)) {
if (!m_settings.IsCheckpoint(blockHeight)) {
return false;
}
auto const it = m_checkpoints.find(GetEpoch(blockHeight));
return it != m_checkpoints.end() && it->second.m_is_justified;
}

bool FinalizationState::IsFinalizedCheckpoint(blockchain::Height blockHeight) const {
if (!IsCheckpoint(blockHeight)) {
if (!m_settings.IsCheckpoint(blockHeight)) {
return false;
}
auto const it = m_checkpoints.find(GetEpoch(blockHeight));
Expand Down
6 changes: 0 additions & 6 deletions src/esperanza/finalizationstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ class FinalizationState : public FinalizationStateData {
//! \brief Retrives the hash of the last finalization transaction performed by the validator.
uint256 GetLastTxHash(const uint160 &validatorAddress) const;

//! \brief Returns whether block on block_height is the first block of the epoch
bool IsEpochStart(blockchain::Height block_height) const;

//! \brief Returns whether block on blockHeight is the last block of the epoch
bool IsCheckpoint(blockchain::Height blockHeight) const;

//! \brief Returns whether block on height blockHeight is justified checkpoint
bool IsJustifiedCheckpoint(blockchain::Height blockHeight) const;

Expand Down
21 changes: 21 additions & 0 deletions src/finalization/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ struct Params {

esperanza::AdminParams admin_params;

//! \brief Returns the epoch which includes block_height.
inline uint32_t GetEpoch(const blockchain::Height block_height) const {
uint32_t epoch = block_height / epoch_length;
if (block_height % epoch_length != 0) {
++epoch;
}
return epoch;
}

//! \brief Returns the height of the first block of the epoch.
inline blockchain::Height GetEpochStartHeight(const uint32_t epoch) const {
// epoch=0 contains only genesis
if (epoch == 0) {
Expand All @@ -39,10 +49,21 @@ struct Params {
return GetEpochCheckpointHeight(epoch - 1) + 1;
}

//! \brief Returns the height of the last block of the epoch.
inline blockchain::Height GetEpochCheckpointHeight(const uint32_t epoch) const {
return epoch * epoch_length;
}

//! \brief Returns whether block at block_height is the first block of the epoch.
inline bool IsEpochStart(blockchain::Height block_height) const {
return block_height % epoch_length == 1;
}

//! \brief Returns whether block at block_height is the last block of the epoch.
inline bool IsCheckpoint(blockchain::Height block_height) const {
return block_height % epoch_length == 0;
}

static Params RegTest(bool gen_admin_keys = false);
static Params TestNet(bool gen_admin_keys = false);

Expand Down
27 changes: 0 additions & 27 deletions src/test/esperanza/finalizationstate_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,6 @@ BOOST_AUTO_TEST_CASE(constructor) {
BOOST_CHECK_EQUAL(0, state.GetLastJustifiedEpoch());
}

BOOST_AUTO_TEST_CASE(get_epoch) {
std::map<uint32_t, uint32_t> height_to_epoch{
{0, 0},
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 2},
{9, 2},
{10, 2},
{11, 3},
{15, 3},
{16, 4},
{20, 4},
{25, 5},
};

finalization::Params params;
FinalizationState state(params);
BOOST_REQUIRE_EQUAL(state.GetEpochLength(), 5);

for (const auto &it : height_to_epoch) {
BOOST_CHECK_EQUAL(state.GetEpoch(it.first), it.second);
}
}

// InitializeEpoch tests

BOOST_AUTO_TEST_CASE(initialize_epoch_wrong_height_passed) {
Expand Down
70 changes: 70 additions & 0 deletions src/test/finalization/params_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@

BOOST_AUTO_TEST_SUITE(finalization_params_tests)

BOOST_AUTO_TEST_CASE(get_epoch) {
std::map<uint32_t, uint32_t> height_to_epoch{
{0, 0},
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 2},
{9, 2},
{10, 2},
{11, 3},
{15, 3},
{16, 4},
{20, 4},
{25, 5},
};

finalization::Params params;
params.epoch_length = 5;

for (const auto &it : height_to_epoch) {
BOOST_CHECK_EQUAL(params.GetEpoch(it.first), it.second);
}
}

BOOST_AUTO_TEST_CASE(get_epoch_start_height) {
finalization::Params params;
params.epoch_length = 5;
Expand Down Expand Up @@ -41,6 +67,50 @@ BOOST_AUTO_TEST_CASE(get_epoch_checkpoint_height) {
BOOST_CHECK_EQUAL(params.GetEpochCheckpointHeight(3), 150);
}

BOOST_AUTO_TEST_CASE(is_epoch_start) {
finalization::Params params;
params.epoch_length = 5;

BOOST_CHECK(!params.IsEpochStart(0));
BOOST_CHECK(params.IsEpochStart(1));
BOOST_CHECK(!params.IsEpochStart(2));
BOOST_CHECK(!params.IsEpochStart(3));
BOOST_CHECK(!params.IsEpochStart(4));
BOOST_CHECK(!params.IsEpochStart(5));
BOOST_CHECK(params.IsEpochStart(6));
BOOST_CHECK(params.IsEpochStart(11));

params.epoch_length = 42;
BOOST_CHECK(!params.IsEpochStart(0));
BOOST_CHECK(params.IsEpochStart(1));
BOOST_CHECK(!params.IsEpochStart(2));
BOOST_CHECK(!params.IsEpochStart(6));
BOOST_CHECK(params.IsEpochStart(43));
BOOST_CHECK(params.IsEpochStart(85));
}

BOOST_AUTO_TEST_CASE(is_checkpoint) {
finalization::Params params;
params.epoch_length = 5;

BOOST_CHECK(params.IsCheckpoint(0));
BOOST_CHECK(!params.IsCheckpoint(1));
BOOST_CHECK(!params.IsCheckpoint(2));
BOOST_CHECK(!params.IsCheckpoint(3));
BOOST_CHECK(!params.IsCheckpoint(4));
BOOST_CHECK(params.IsCheckpoint(5));
BOOST_CHECK(!params.IsCheckpoint(6));
BOOST_CHECK(params.IsCheckpoint(10));

params.epoch_length = 11;
BOOST_CHECK(params.IsCheckpoint(0));
BOOST_CHECK(!params.IsCheckpoint(1));
BOOST_CHECK(!params.IsCheckpoint(2));
BOOST_CHECK(!params.IsCheckpoint(5));
BOOST_CHECK(params.IsCheckpoint(11));
BOOST_CHECK(params.IsCheckpoint(22));
}

BOOST_AUTO_TEST_CASE(parse_params_invalid_json) {
const std::string json = R"(
this is not json {[]}
Expand Down