Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions finalizer/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
{
Expand Down Expand Up @@ -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")]
{
Expand Down Expand Up @@ -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 => {
Expand Down
103 changes: 49 additions & 54 deletions finalizer/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E: Clock + Storage + Metrics, V: Variant> {
Expand Down Expand Up @@ -54,19 +54,19 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
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)
}

Expand All @@ -79,49 +79,49 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
}

// 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
Expand All @@ -148,22 +148,22 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
}

// 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<ConsensusState> {
let key = Self::make_consensus_state_key(height);
pub async fn get_consensus_state(&self, epoch: u64) -> Option<ConsensusState> {
let key = Self::make_consensus_state_key(epoch);
if let Some(Value::ConsensusState(state)) = self
.store()
.get(&key)
Expand All @@ -177,22 +177,21 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
}

pub async fn get_latest_consensus_state(&self) -> Option<ConsensusState> {
// 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
Expand Down Expand Up @@ -250,28 +249,28 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
// FinalizedHeader operations
pub async fn store_finalized_header(
&mut self,
height: u64,
epoch: u64,
header: &FinalizedHeader<bls12381_multisig::Scheme<PublicKey, V>>,
) {
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<FinalizedHeader<bls12381_multisig::Scheme<PublicKey, V>>> {
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)
Expand All @@ -287,12 +286,8 @@ impl<E: Clock + Storage + Metrics, V: Variant> FinalizerState<E, V> {
pub async fn get_most_recent_finalized_header(
&self,
) -> Option<FinalizedHeader<bls12381_multisig::Scheme<PublicKey, V>>> {
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
Expand Down
3 changes: 1 addition & 2 deletions node/src/tests/checkpointing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
8 changes: 4 additions & 4 deletions node/src/tests/execution_requests/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion types/src/engine_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down