diff --git a/finalizer/src/actor.rs b/finalizer/src/actor.rs index 9991cf8..67ced5d 100644 --- a/finalizer/src/actor.rs +++ b/finalizer/src/actor.rs @@ -444,7 +444,7 @@ impl< #[cfg(feature = "prom")] let header_start = Instant::now(); self.db - .store_finalized_header(new_height, &finalized_header) + .store_finalized_header(self.canonical_state.epoch, &finalized_header) .await; #[cfg(feature = "prom")] { @@ -522,7 +522,7 @@ impl< #[cfg(feature = "prom")] let consensus_state_start = Instant::now(); self.db - .store_consensus_state(new_height, &self.canonical_state) + .store_consensus_state(self.canonical_state.epoch, &self.canonical_state) .await; #[cfg(feature = "prom")] { @@ -893,8 +893,8 @@ impl< .cloned(); let _ = sender.send(ConsensusStateResponse::ValidatorAccount(account)); } - ConsensusStateRequest::GetFinalizedHeader(height) => { - let header = self.db.get_finalized_header(height).await; + ConsensusStateRequest::GetFinalizedHeader(epoch) => { + let header = self.db.get_finalized_header(epoch).await; let _ = sender.send(ConsensusStateResponse::FinalizedHeader(header)); } ConsensusStateRequest::GetMinimumStake => { diff --git a/finalizer/src/db.rs b/finalizer/src/db.rs index f3a93e3..c00d8bd 100644 --- a/finalizer/src/db.rs +++ b/finalizer/src/db.rs @@ -21,8 +21,8 @@ const CHECKPOINT_PREFIX: u8 = 0x06; const FINALIZED_HEADER_PREFIX: u8 = 0x07; // State variable keys -const LATEST_CONSENSUS_STATE_HEIGHT_KEY: [u8; 2] = [STATE_PREFIX, 0]; -const LATEST_FINALIZED_HEADER_HEIGHT_KEY: [u8; 2] = [STATE_PREFIX, 1]; +const LATEST_CONSENSUS_STATE_EPOCH_KEY: [u8; 2] = [STATE_PREFIX, 0]; +const LATEST_FINALIZED_HEADER_EPOCH_KEY: [u8; 2] = [STATE_PREFIX, 1]; const LATEST_CHECKPOINT_EPOCH_KEY: [u8; 2] = [STATE_PREFIX, 2]; pub struct FinalizerState { @@ -54,19 +54,19 @@ impl FinalizerState { FixedBytes::new(padded) } - fn make_consensus_state_key(height: u64) -> FixedBytes<64> { + fn make_consensus_state_key(epoch: u64) -> FixedBytes<64> { let mut key = [0u8; 64]; key[0] = CONSENSUS_STATE_PREFIX; // Use little-endian so varying bytes come first (for EightCap translator) - key[1..9].copy_from_slice(&height.to_le_bytes()); + key[1..9].copy_from_slice(&epoch.to_le_bytes()); FixedBytes::new(key) } - fn make_finalized_header_key(height: u64) -> FixedBytes<64> { + fn make_finalized_header_key(epoch: u64) -> FixedBytes<64> { let mut key = [0u8; 64]; key[0] = FINALIZED_HEADER_PREFIX; // Use little-endian so varying bytes come first (for EightCap translator) - key[1..9].copy_from_slice(&height.to_le_bytes()); + key[1..9].copy_from_slice(&epoch.to_le_bytes()); FixedBytes::new(key) } @@ -79,49 +79,49 @@ impl FinalizerState { } // State variable operations - async fn get_latest_consensus_state_height(&self) -> u64 { - let key = Self::pad_key(&LATEST_CONSENSUS_STATE_HEIGHT_KEY); - if let Some(Value::U64(height)) = self + async fn get_latest_consensus_state_epoch(&self) -> u64 { + let key = Self::pad_key(&LATEST_CONSENSUS_STATE_EPOCH_KEY); + if let Some(Value::U64(epoch)) = self .store() .get(&key) .await - .expect("failed to get latest consensus state height") + .expect("failed to get latest consensus state epoch") { - height + epoch } else { 0 } } - async fn set_latest_consensus_state_height(&mut self, height: u64) { - let key = Self::pad_key(&LATEST_CONSENSUS_STATE_HEIGHT_KEY); + async fn set_latest_consensus_state_epoch(&mut self, epoch: u64) { + let key = Self::pad_key(&LATEST_CONSENSUS_STATE_EPOCH_KEY); self.store_mut() - .update(key, Value::U64(height)) + .update(key, Value::U64(epoch)) .await - .expect("failed to set latest consensus state height"); + .expect("failed to set latest consensus state epoch"); } - // FinalizedHeader height tracking operations - async fn get_latest_finalized_header_height(&self) -> u64 { - let key = Self::pad_key(&LATEST_FINALIZED_HEADER_HEIGHT_KEY); - if let Some(Value::U64(height)) = self + // FinalizedHeader epoch tracking operations + async fn get_latest_finalized_header_epoch(&self) -> u64 { + let key = Self::pad_key(&LATEST_FINALIZED_HEADER_EPOCH_KEY); + if let Some(Value::U64(epoch)) = self .store() .get(&key) .await - .expect("failed to get latest finalized header height") + .expect("failed to get latest finalized header epoch") { - height + epoch } else { 0 } } - async fn set_latest_finalized_header_height(&mut self, height: u64) { - let key = Self::pad_key(&LATEST_FINALIZED_HEADER_HEIGHT_KEY); + async fn set_latest_finalized_header_epoch(&mut self, epoch: u64) { + let key = Self::pad_key(&LATEST_FINALIZED_HEADER_EPOCH_KEY); self.store_mut() - .update(key, Value::U64(height)) + .update(key, Value::U64(epoch)) .await - .expect("failed to set latest finalized header height"); + .expect("failed to set latest finalized header epoch"); } // Checkpoint epoch tracking operations @@ -148,22 +148,22 @@ impl FinalizerState { } // ConsensusState blob operations - pub async fn store_consensus_state(&mut self, height: u64, state: &ConsensusState) { - let key = Self::make_consensus_state_key(height); + pub async fn store_consensus_state(&mut self, epoch: u64, state: &ConsensusState) { + let key = Self::make_consensus_state_key(epoch); self.store_mut() .update(key, Value::ConsensusState(Box::new(state.clone()))) .await .expect("failed to store consensus state"); - // Update the latest height tracker - let current_latest = self.get_latest_consensus_state_height().await; - if height > current_latest { - self.set_latest_consensus_state_height(height).await; + // Update the latest epoch tracker + let current_latest = self.get_latest_consensus_state_epoch().await; + if epoch >= current_latest { + self.set_latest_consensus_state_epoch(epoch).await; } } - pub async fn get_consensus_state(&self, height: u64) -> Option { - let key = Self::make_consensus_state_key(height); + pub async fn get_consensus_state(&self, epoch: u64) -> Option { + let key = Self::make_consensus_state_key(epoch); if let Some(Value::ConsensusState(state)) = self .store() .get(&key) @@ -177,22 +177,21 @@ impl FinalizerState { } pub async fn get_latest_consensus_state(&self) -> Option { - // Check if we have a latest height tracker - let key = Self::pad_key(&LATEST_CONSENSUS_STATE_HEIGHT_KEY); - if let Some(Value::U64(latest_height)) = self + let key = Self::pad_key(&LATEST_CONSENSUS_STATE_EPOCH_KEY); + if let Some(Value::U64(latest_epoch)) = self .store() .get(&key) .await - .expect("failed to get latest consensus state height") + .expect("failed to get latest consensus state epoch") { - self.get_consensus_state(latest_height).await + self.get_consensus_state(latest_epoch).await } else { None } } - pub async fn delete_consensus_state(&mut self, height: u64) -> bool { - let key = Self::make_consensus_state_key(height); + pub async fn delete_consensus_state(&mut self, epoch: u64) -> bool { + let key = Self::make_consensus_state_key(epoch); self.store_mut() .delete(key) .await @@ -250,28 +249,28 @@ impl FinalizerState { // FinalizedHeader operations pub async fn store_finalized_header( &mut self, - height: u64, + epoch: u64, header: &FinalizedHeader>, ) { - let key = Self::make_finalized_header_key(height); + let key = Self::make_finalized_header_key(epoch); self.store_mut() .update(key, Value::FinalizedHeader(Box::new(header.clone()))) .await .expect("failed to store finalized header"); - // Update the latest finalized header height tracker - let current_latest = self.get_latest_finalized_header_height().await; - if height > current_latest { - self.set_latest_finalized_header_height(height).await; + // Update the latest finalized header epoch tracker + let current_latest = self.get_latest_finalized_header_epoch().await; + if epoch >= current_latest { + self.set_latest_finalized_header_epoch(epoch).await; } } #[allow(unused)] pub async fn get_finalized_header( &self, - height: u64, + epoch: u64, ) -> Option>> { - let key = Self::make_finalized_header_key(height); + let key = Self::make_finalized_header_key(epoch); if let Some(Value::FinalizedHeader(header)) = self .store() .get(&key) @@ -287,12 +286,8 @@ impl FinalizerState { pub async fn get_most_recent_finalized_header( &self, ) -> Option>> { - let latest_height = self.get_latest_finalized_header_height().await; - if latest_height > 0 { - self.get_finalized_header(latest_height).await - } else { - None - } + let latest_epoch = self.get_latest_finalized_header_epoch().await; + self.get_finalized_header(latest_epoch).await } // Commit all pending changes to the database diff --git a/node/src/tests/checkpointing.rs b/node/src/tests/checkpointing.rs index dabe670..d60d684 100644 --- a/node/src/tests/checkpointing.rs +++ b/node/src/tests/checkpointing.rs @@ -190,9 +190,8 @@ fn test_checkpoint_created() { ConsensusState::try_from(&checkpoint).expect("failed to parse consensus state"); // Verify the finalized header's checkpoint_hash matches the checkpoint digest - let last_block_epoch_0 = utils::last_block_in_epoch(BLOCKS_PER_EPOCH, 0); let finalized_header = consensus_state_query - .get_finalized_header(last_block_epoch_0) + .get_finalized_header(0) .await .expect("failed to get finalized header"); assert_eq!( diff --git a/node/src/tests/execution_requests/validator_set.rs b/node/src/tests/execution_requests/validator_set.rs index 6907d20..aa28cf3 100644 --- a/node/src/tests/execution_requests/validator_set.rs +++ b/node/src/tests/execution_requests/validator_set.rs @@ -171,10 +171,10 @@ fn test_added_validators_at_epoch_boundary() { context.sleep(Duration::from_secs(1)).await; } - // Get the finalized header for the last block of epoch 1 (block 19) + // Get the finalized header for epoch 1 let mut mailbox = finalizer_mailboxes.get(&0).unwrap().clone(); let finalized_header = mailbox - .get_finalized_header(last_block_epoch_1) + .get_finalized_header(1) .await .expect("Failed to get finalized header for last block of epoch 1"); @@ -393,10 +393,10 @@ fn test_removed_validators_at_epoch_boundary() { context.sleep(Duration::from_secs(1)).await; } - // Get the finalized header for the last block of epoch 0 (block 9) + // Get the finalized header for epoch 0 let mut mailbox = finalizer_mailboxes.get(&0).unwrap().clone(); let finalized_header = mailbox - .get_finalized_header(last_block_epoch_0) + .get_finalized_header(0) .await .expect("Failed to get finalized header for last block of epoch 0"); diff --git a/types/src/engine_client.rs b/types/src/engine_client.rs index fec9eb2..e4d0d53 100644 --- a/types/src/engine_client.rs +++ b/types/src/engine_client.rs @@ -153,7 +153,7 @@ impl EngineClient for RethEngineClient { .new_payload_v4( block.payload.clone(), Vec::new(), - [1; 32].into(), + block.parent().0.into(), block.execution_requests.clone(), ) .await