From e8ab1335f8def9e7c3b216ca0cd8074cabc69d10 Mon Sep 17 00:00:00 2001 From: drcpu Date: Sun, 24 Nov 2024 19:52:40 +0000 Subject: [PATCH] fix(node): use correct epochs while synchronizing node --- node/src/actors/chain_manager/actor.rs | 17 ++++------ node/src/actors/chain_manager/mod.rs | 47 ++++++++++---------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/node/src/actors/chain_manager/actor.rs b/node/src/actors/chain_manager/actor.rs index 633e94c01..5c7f0a343 100644 --- a/node/src/actors/chain_manager/actor.rs +++ b/node/src/actors/chain_manager/actor.rs @@ -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 @@ -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. diff --git a/node/src/actors/chain_manager/mod.rs b/node/src/actors/chain_manager/mod.rs index 2f02fe34d..712371b12 100644 --- a/node/src/actors/chain_manager/mod.rs +++ b/node/src/actors/chain_manager/mod.rs @@ -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), @@ -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(); @@ -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(); @@ -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(); @@ -1252,9 +1235,9 @@ 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 { @@ -1262,10 +1245,14 @@ impl ChainManager { 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 @@ -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, @@ -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, @@ -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 @@ -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 @@ -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);