Skip to content

Commit

Permalink
fix(node): use correct epochs while synchronizing node
Browse files Browse the repository at this point in the history
  • Loading branch information
drcpu-github committed Nov 25, 2024
1 parent 13a9a90 commit e8ab133
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
17 changes: 7 additions & 10 deletions node/src/actors/chain_manager/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,6 @@ impl ChainManager {
if consensus_constants == &chain_info_from_storage.consensus_constants {
log::debug!("ChainInfo successfully obtained from storage");

// Load protocol info into global state (overwrites whatever was set
// through configuration). If possible, derives the current protocol
// version from that info and the current epoch. This essentially
// allows a node to catch up with a new protocol version if the
// transition happened while it was down.
load_protocol_info(chain_info_from_storage.protocol.clone());
if let Some(ChainInfo { highest_block_checkpoint, .. }) = chain_state_from_storage.chain_info {
refresh_protocol_version(highest_block_checkpoint.checkpoint);
}

chain_state_from_storage
} else {
// Mismatching consensus constants between config and storage
Expand Down Expand Up @@ -370,6 +360,13 @@ impl ChainManager {
chain_info.highest_block_checkpoint.hash_prev_block
);

// Load protocol info into global state (overwrites whatever was set through
// configuration). If possible, derives the current protocol version from that
// info and the current epoch. This essentially allows a node to catch up with
// a new protocol version if the transition happened while it was down.
load_protocol_info(chain_info.protocol.clone());
refresh_protocol_version(chain_info.highest_block_checkpoint.checkpoint);

// If hash_prev_block is the bootstrap hash, create and consolidate genesis block.
// Consolidating the genesis block is not needed if the chain state has been reset
// because of a rewind: the genesis block will be processed with the other blocks.
Expand Down
47 changes: 17 additions & 30 deletions node/src/actors/chain_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,23 +950,6 @@ impl ChainManager {
}
};

let current_epoch = if let Some(epoch) = self.current_epoch {
epoch
} else {
// If there is no epoch set, it's because the chain is yet to be bootstrapped, or because of a data race
match self.chain_state.chain_info.as_ref() {
// If the chain is yet to be bootstrapped (the block we are processing is the genesis block), set the epoch to zero
Some(chain_info) if chain_info.consensus_constants.genesis_hash == block.hash() => {
0
}
// In case of data race, shortcut the function
_ => {
log::error!("Current epoch not loaded in ChainManager");
return;
}
}
};

match self.chain_state {
ChainState {
chain_info: Some(ref mut chain_info),
Expand Down Expand Up @@ -1203,7 +1186,7 @@ impl ChainManager {
transaction_fees += vt_transaction_fee(
vt_tx,
&utxo_diff_wit2,
current_epoch,
block_epoch,
epoch_constants,
)
.unwrap_or_default();
Expand All @@ -1212,7 +1195,7 @@ impl ChainManager {
transaction_fees += dr_transaction_fee(
dr_tx,
&utxo_diff_wit2,
current_epoch,
block_epoch,
epoch_constants,
)
.unwrap_or_default();
Expand All @@ -1221,7 +1204,7 @@ impl ChainManager {
transaction_fees += st_transaction_fee(
st_tx,
&utxo_diff_wit2,
current_epoch,
block_epoch,
epoch_constants,
)
.unwrap_or_default();
Expand Down Expand Up @@ -1252,20 +1235,24 @@ impl ChainManager {
log::debug!(
"Resetting mining age for {} to {}",
miner_pkh,
current_epoch,
block_epoch + 1,
);
let _ = stakes.reset_age(miner_pkh, Capability::Mining, current_epoch, 1);
let _ = stakes.reset_age(miner_pkh, Capability::Mining, block_epoch + 1, 1);

// Reset witnessing power
for co_tx in &block.txns.commit_txns {
let commit_pkh = co_tx.body.proof.proof.pkh();
log::debug!(
"Resetting witnessing age for {} to {}",
commit_pkh,
current_epoch,
block_epoch + 1,
);
let _ = stakes.reset_age(
commit_pkh,
Capability::Witnessing,
block_epoch + 1,
1,
);
let _ =
stakes.reset_age(commit_pkh, Capability::Witnessing, current_epoch, 1);
}

// Slash lieing validators
Expand Down Expand Up @@ -1325,7 +1312,7 @@ impl ChainManager {

let minimum_stakeable = self
.consensus_constants_wit2
.get_validator_min_stake_nanowits(current_epoch);
.get_validator_min_stake_nanowits(block_epoch);

let _ = process_stake_transactions(
stakes,
Expand All @@ -1341,7 +1328,7 @@ impl ChainManager {

let minimum_stakeable = self
.consensus_constants_wit2
.get_validator_min_stake_nanowits(current_epoch);
.get_validator_min_stake_nanowits(block_epoch);

let _ = process_unstake_transactions(
stakes,
Expand Down Expand Up @@ -1381,7 +1368,7 @@ impl ChainManager {
let reveals = self
.chain_state
.data_request_pool
.update_data_request_stages(Some(validator_count), Some(current_epoch));
.update_data_request_stages(Some(validator_count), Some(block_epoch));

for reveal in reveals {
// Send AddTransaction message to self
Expand All @@ -1407,7 +1394,7 @@ impl ChainManager {
let reveals = self
.chain_state
.data_request_pool
.update_data_request_stages(Some(validator_count), Some(current_epoch));
.update_data_request_stages(Some(validator_count), Some(block_epoch));

for reveal in reveals {
// Send AddTransaction message to self
Expand All @@ -1433,7 +1420,7 @@ impl ChainManager {
let reveals = self
.chain_state
.data_request_pool
.update_data_request_stages(Some(validator_count), Some(current_epoch));
.update_data_request_stages(Some(validator_count), Some(block_epoch));

show_info_dr(&self.chain_state.data_request_pool, &block);

Expand Down

0 comments on commit e8ab133

Please sign in to comment.