From a99d2006ba4bf6fb884a171af08520c15f9734d3 Mon Sep 17 00:00:00 2001 From: j75689 Date: Mon, 5 Aug 2024 18:16:56 +0800 Subject: [PATCH 1/2] fix: snapshot incompatibility issue --- crates/bsc/consensus/src/lib.rs | 3 ++- crates/bsc/evm/src/execute.rs | 4 ++-- crates/primitives/src/parlia/snapshot.rs | 16 ++++++++++------ crates/storage/db-api/src/models/mod.rs | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/bsc/consensus/src/lib.rs b/crates/bsc/consensus/src/lib.rs index 2513b8730c..9ff16702a8 100644 --- a/crates/bsc/consensus/src/lib.rs +++ b/crates/bsc/consensus/src/lib.rs @@ -323,7 +323,8 @@ impl Parlia { } let mut rng = if self.chain_spec.is_bohr_active_at_timestamp(header.timestamp) { - RngSource::new(header.number as i64 / snap.turn_length as i64) + let turn_length = snap.turn_length.unwrap_or(DEFAULT_TURN_LENGTH); + RngSource::new(header.number as i64 / turn_length as i64) } else { RngSource::new(snap.block_number as i64) }; diff --git a/crates/bsc/evm/src/execute.rs b/crates/bsc/evm/src/execute.rs index 024c1d9290..b81c83b452 100644 --- a/crates/bsc/evm/src/execute.rs +++ b/crates/bsc/evm/src/execute.rs @@ -869,8 +869,8 @@ where // the old snapshots don't have turn length, make sure we initialize it with default // before accessing it - if snap.turn_length == 0 { - snap.turn_length = DEFAULT_TURN_LENGTH; + if snap.turn_length.is_none() { + snap.turn_length = Some(DEFAULT_TURN_LENGTH); } // apply skip headers diff --git a/crates/primitives/src/parlia/snapshot.rs b/crates/primitives/src/parlia/snapshot.rs index 08ecd5b7a8..46d5b24ca6 100644 --- a/crates/primitives/src/parlia/snapshot.rs +++ b/crates/primitives/src/parlia/snapshot.rs @@ -40,7 +40,8 @@ pub struct Snapshot { /// record the block attestation's vote data pub vote_data: VoteData, /// record length of `turn` - pub turn_length: u8, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub turn_length: Option, } impl Snapshot { @@ -81,7 +82,7 @@ impl Snapshot { validators_map, recent_proposers: Default::default(), vote_data: Default::default(), - turn_length: DEFAULT_TURN_LENGTH, + turn_length: Some(DEFAULT_TURN_LENGTH), } } @@ -124,7 +125,7 @@ impl Snapshot { { new_validators.sort(); if let Some(turn_length) = turn_length { - snap.turn_length = turn_length; + snap.turn_length = Some(turn_length); } if is_bohr { @@ -174,13 +175,15 @@ impl Snapshot { /// Returns the number of blocks after which the miner history should be checked pub fn miner_history_check_len(&self) -> u64 { - (self.validators.len() / 2 + 1) as u64 * self.turn_length as u64 - 1 + let turn_length = u64::from(self.turn_length.unwrap_or(DEFAULT_TURN_LENGTH)); + (self.validators.len() / 2 + 1) as u64 * turn_length - 1 } /// Returns the validator who should propose the block pub fn inturn_validator(&self) -> Address { + let turn_length = u64::from(self.turn_length.unwrap_or(DEFAULT_TURN_LENGTH)); self.validators - [((self.block_number + 1) as usize) / self.turn_length as usize % self.validators.len()] + [((self.block_number + 1) as usize) / turn_length as usize % self.validators.len()] } /// Return index of the validator's index in validators list @@ -225,7 +228,8 @@ impl Snapshot { counts: &HashMap, ) -> bool { if let Some(&seen_times) = counts.get(&validator) { - if seen_times >= self.turn_length { + let turn_length = self.turn_length.unwrap_or(DEFAULT_TURN_LENGTH); + if seen_times >= turn_length { return true; } } diff --git a/crates/storage/db-api/src/models/mod.rs b/crates/storage/db-api/src/models/mod.rs index eeae101d4b..b92128a3fe 100644 --- a/crates/storage/db-api/src/models/mod.rs +++ b/crates/storage/db-api/src/models/mod.rs @@ -416,7 +416,7 @@ mod tests { validators_map: HashMap::new(), recent_proposers: BTreeMap::new(), vote_data: VoteData::default(), - turn_length: DEFAULT_TURN_LENGTH, + turn_length: Some(DEFAULT_TURN_LENGTH), }; snap.validators_map.insert( snap.validators[0], From 4b9aa53747e4edec1a97850b7a87edc14700eb4f Mon Sep 17 00:00:00 2001 From: j75689 Date: Mon, 5 Aug 2024 18:39:46 +0800 Subject: [PATCH 2/2] fix: check snap.turn_length is none or zero --- crates/bsc/evm/src/execute.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bsc/evm/src/execute.rs b/crates/bsc/evm/src/execute.rs index b81c83b452..41e5c363a5 100644 --- a/crates/bsc/evm/src/execute.rs +++ b/crates/bsc/evm/src/execute.rs @@ -869,7 +869,7 @@ where // the old snapshots don't have turn length, make sure we initialize it with default // before accessing it - if snap.turn_length.is_none() { + if snap.turn_length.is_none() || snap.turn_length == Some(0) { snap.turn_length = Some(DEFAULT_TURN_LENGTH); }