Skip to content

Commit

Permalink
Rejoin for recovery in vacuous epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
ckyang committed Nov 20, 2018
1 parent 0fff687 commit f99a4e5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 81 deletions.
138 changes: 75 additions & 63 deletions src/libNode/Node.cpp
Expand Up @@ -89,83 +89,82 @@ bool Node::Install(SyncType syncType, bool toRetrieveHistory) {
bool wakeupForUpgrade = false;
bool retrieveSuccessButTooLate = false;

if (StartRetrieveHistory(wakeupForUpgrade, retrieveSuccessButTooLate)) {
m_mediator.m_currentEpochNum =
m_mediator.m_txBlockChain.GetLastBlock().GetHeader().GetBlockNum() +
1;
if (!StartRetrieveHistory(wakeupForUpgrade, retrieveSuccessButTooLate)) {
return retrieveSuccessButTooLate;
}

if (wakeupForUpgrade) {
m_mediator.m_consensusID = m_mediator.m_currentEpochNum == 1 ? 1 : 0;
}
m_mediator.m_currentEpochNum =
m_mediator.m_txBlockChain.GetLastBlock().GetHeader().GetBlockNum() + 1;

m_consensusLeaderID = 0;
runInitializeGenesisBlocks = false;
m_mediator.UpdateDSBlockRand();
m_mediator.UpdateTxBlockRand();
m_mediator.m_ds->m_mode = DirectoryService::IDLE;

for (const auto& ds : *m_mediator.m_DSCommittee) {
if (ds.first == m_mediator.m_selfKey.second) {
m_mediator.m_ds->m_consensusMyID = 0;

for (auto const& i : *m_mediator.m_DSCommittee) {
if (i.first == m_mediator.m_selfKey.second) {
LOG_EPOCH(INFO, to_string(m_mediator.m_currentEpochNum).c_str(),
"My node ID for this PoW consensus is "
<< m_mediator.m_ds->m_consensusMyID);
break;
}

++m_mediator.m_ds->m_consensusMyID;
if (wakeupForUpgrade) {
m_mediator.m_consensusID = m_mediator.m_currentEpochNum == 1 ? 1 : 0;
}

m_consensusLeaderID = 0;
runInitializeGenesisBlocks = false;
m_mediator.UpdateDSBlockRand();
m_mediator.UpdateTxBlockRand();
m_mediator.m_ds->m_mode = DirectoryService::IDLE;

for (const auto& ds : *m_mediator.m_DSCommittee) {
if (ds.first == m_mediator.m_selfKey.second) {
m_mediator.m_ds->m_consensusMyID = 0;

for (auto const& i : *m_mediator.m_DSCommittee) {
if (i.first == m_mediator.m_selfKey.second) {
LOG_EPOCH(INFO, to_string(m_mediator.m_currentEpochNum).c_str(),
"My node ID for this PoW consensus is "
<< m_mediator.m_ds->m_consensusMyID);
break;
}

m_mediator.m_node->m_consensusMyID = m_mediator.m_ds->m_consensusMyID;
++m_mediator.m_ds->m_consensusMyID;
}

if (m_mediator.m_DSCommittee->at(m_mediator.m_ds->m_consensusLeaderID)
.first == m_mediator.m_selfKey.second) {
m_mediator.m_ds->m_mode = DirectoryService::PRIMARY_DS;
m_mediator.m_node->m_consensusMyID = m_mediator.m_ds->m_consensusMyID;

if (!wakeupForUpgrade) {
LOG_GENERAL(INFO,
"Node recovery cannot be applied on DS leader, apply "
"re-join process instead");
return false;
}
if (m_mediator.m_DSCommittee->at(m_mediator.m_ds->m_consensusLeaderID)
.first == m_mediator.m_selfKey.second) {
m_mediator.m_ds->m_mode = DirectoryService::PRIMARY_DS;

if (!wakeupForUpgrade) {
LOG_GENERAL(INFO,
"Set as DS leader: "
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< ":" << m_mediator.m_selfPeer.m_listenPortHost);
LOG_STATE("[IDENT]["
<< std::setw(15) << std::left
<< m_mediator.m_selfPeer.GetPrintableIPAddress() << "]["
<< std::setw(6) << std::left
<< m_mediator.m_ds->m_consensusMyID << "] DSLD");
} else {
m_mediator.m_ds->m_mode = DirectoryService::BACKUP_DS;
LOG_GENERAL(INFO,
"Set as DS backup: "
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< ":" << m_mediator.m_selfPeer.m_listenPortHost);
LOG_STATE("[IDENT]["
<< std::setw(15) << std::left
<< m_mediator.m_selfPeer.GetPrintableIPAddress() << "]["
<< std::setw(6) << std::left
<< m_mediator.m_ds->m_consensusMyID << "] DSBK");
"Node recovery cannot be applied on DS leader, apply "
"re-join process instead");
return false;
}

break;
LOG_GENERAL(INFO, "Set as DS leader: "
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< ":"
<< m_mediator.m_selfPeer.m_listenPortHost);
LOG_STATE("[IDENT][" << std::setw(15) << std::left
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< "][" << std::setw(6) << std::left
<< m_mediator.m_ds->m_consensusMyID << "] DSLD");
} else {
m_mediator.m_ds->m_mode = DirectoryService::BACKUP_DS;
LOG_GENERAL(INFO, "Set as DS backup: "
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< ":"
<< m_mediator.m_selfPeer.m_listenPortHost);
LOG_STATE("[IDENT][" << std::setw(15) << std::left
<< m_mediator.m_selfPeer.GetPrintableIPAddress()
<< "][" << std::setw(6) << std::left
<< m_mediator.m_ds->m_consensusMyID << "] DSBK");
}
}

if (wakeupForUpgrade) {
WakeupForUpgrade();
} else {
WakeupForRecovery();
return true;
break;
}
}

if (wakeupForUpgrade) {
WakeupForUpgrade();
} else {
WakeupForRecovery();
return true;
}

// When do node recovery, if retrieve the history success, then shouldn't
// continue to run add genesis account, but let the node to synchronize with
// lookup node.
Expand Down Expand Up @@ -344,6 +343,20 @@ bool Node::StartRetrieveHistory(bool& wakeupForUpgrade,
}
}

/// If recovery mode with vacuous epoch or less than 1 DS epoch, apply re-join
/// process instead of node recovery
if (!wakeupForUpgrade && !LOOKUP_NODE_MODE &&
(m_mediator.m_txBlockChain.GetLastBlock().GetHeader().GetBlockNum() <
NUM_FINAL_BLOCK_PER_POW ||
m_mediator.GetIsVacuousEpoch(
m_mediator.m_txBlockChain.GetLastBlock().GetHeader().GetBlockNum() +
1))) {
LOG_GENERAL(INFO,
"Node recovery with vacuous epoch or too early, apply "
"re-join process instead");
return false;
}

/// Save coin base for final block, from last DS epoch to current TX epoch
if (!LOOKUP_NODE_MODE) {
for (uint64_t blockNum =
Expand Down Expand Up @@ -486,7 +499,6 @@ void Node::WakeupForRecovery() {
LOG_MARKER();

if (LOOKUP_NODE_MODE) {
LOG_GENERAL(INFO, "Lookup node, do nothing temporarily");
return;
}

Expand Down
20 changes: 2 additions & 18 deletions src/libPersistence/Retriever.cpp
Expand Up @@ -118,10 +118,8 @@ void Retriever::RetrieveTxBlocks(bool& result, const bool& wakeupForUpgrade) {
unsigned int totalSize = blocks.size();
unsigned int extra_txblocks = totalSize % NUM_FINAL_BLOCK_PER_POW;

if (wakeupForUpgrade ||
(blocks.back()->GetHeader().GetBlockNum() + NUM_VACUOUS_EPOCHS) %
NUM_FINAL_BLOCK_PER_POW ==
0) {
if (wakeupForUpgrade || m_mediator.GetIsVacuousEpoch(
(blocks.back()->GetHeader().GetBlockNum()))) {
// truncate the extra final blocks at last
for (unsigned int i = 0; i < extra_txblocks; ++i) {
BlockStorage::GetBlockStorage().DeleteTxBlock(totalSize - 1 - i);
Expand All @@ -135,20 +133,6 @@ void Retriever::RetrieveTxBlocks(bool& result, const bool& wakeupForUpgrade) {

result = true;

/// If recovery mode with vacuous epoch or less than 1 DS epoch, apply re-join
/// process instead of node recovery
if (!wakeupForUpgrade &&
(blocks.back()->GetHeader().GetBlockNum() < NUM_FINAL_BLOCK_PER_POW ||
(blocks.back()->GetHeader().GetBlockNum() + NUM_VACUOUS_EPOCHS) %
NUM_FINAL_BLOCK_PER_POW ==
0)) {
result = false;
LOG_GENERAL(INFO,
"Node recovery with vacuous epoch or too early, apply "
"re-join process instead");
return;
}

/// Retrieve final block state delta from last DS epoch to
/// current TX epoch
for (const auto& block : blocks) {
Expand Down

0 comments on commit f99a4e5

Please sign in to comment.