Skip to content

Commit a91e53e

Browse files
committed
Fix get_commitment_by_index on fullnode
1 parent d6715c2 commit a91e53e

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

crates/fullnode/src/da_block_handler.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,8 @@ where
453453
) -> Result<ProcessingResult, ProcessingError> {
454454
// Skip if this commitment index was already processed
455455
// This prevents double-processing and handles conflicting commitments
456-
if let Some(existing_commitment) = self
457-
.ledger_db
458-
.get_commitment_by_index(sequencer_commitment.index)?
456+
if let Some(existing_commitment) =
457+
self.get_commitment_by_index(sequencer_commitment.index, schema_batch)?
459458
{
460459
// Check if the new commitment has a different merkle root but keep the first processed one as canonical
461460
if existing_commitment.merkle_root != sequencer_commitment.merkle_root {
@@ -508,10 +507,7 @@ where
508507
let start_l2_height = if sequencer_commitment.index == 1 {
509508
get_tangerine_activation_height_non_zero()
510509
} else {
511-
match self
512-
.ledger_db
513-
.get_commitment_by_index(sequencer_commitment.index - 1)?
514-
{
510+
match self.get_commitment_by_index(sequencer_commitment.index - 1, schema_batch)? {
515511
Some(previous_commitment) => previous_commitment.l2_end_block_number + 1,
516512
None => {
517513
// If previous commitment is missing, store this one as pending
@@ -899,7 +895,8 @@ where
899895
let end_l2_height = commitment.l2_end_block_number;
900896
end_l2_height <= head_l2_height
901897
} else {
902-
self.ledger_db.get_commitment_by_index(index - 1)?.is_some()
898+
self.get_commitment_by_index(index - 1, schema_batch)?
899+
.is_some()
903900
};
904901

905902
if processable {
@@ -1053,4 +1050,20 @@ where
10531050
}
10541051
Ok(sequencer_commitment.l2_end_block_number)
10551052
}
1053+
1054+
/// Retrieves a sequencer commitment by its index
1055+
/// First checks the schema batch, then falls back to the ledger DB
1056+
fn get_commitment_by_index(
1057+
&self,
1058+
index: u32,
1059+
schema_batch: &SchemaBatch,
1060+
) -> Result<Option<SequencerCommitment>, ProcessingError> {
1061+
if let Some(Some(commitment)) =
1062+
schema_batch.read_latest::<SequencerCommitmentByIndex>(&index)?
1063+
{
1064+
Ok(Some(commitment))
1065+
} else {
1066+
Ok(self.ledger_db.get_commitment_by_index(index)?)
1067+
}
1068+
}
10561069
}

crates/sovereign-sdk/full-node/db/sov-schema-db/src/schema_batch.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ impl SchemaBatch {
6666
Ok(None)
6767
}
6868

69+
/// Reads the last write for a given key in the batch.
70+
/// Parses the value if the operation is a Put.
71+
pub fn read_latest<S: Schema>(
72+
&self,
73+
key: &impl KeyCodec<S>,
74+
) -> anyhow::Result<Option<Option<S::Value>>> {
75+
if let Some(operation) = self.read::<S>(key)? {
76+
match operation {
77+
Operation::Put { value } => {
78+
let parsed_value = S::Value::decode_value(value)?;
79+
Ok(Some(Some(parsed_value)))
80+
}
81+
Operation::Delete => Ok(Some(None)),
82+
}
83+
} else {
84+
Ok(None)
85+
}
86+
}
87+
6988
/// Iterate over all the writes in the batch for a given column family in reversed lexicographic order
7089
/// Returns None column family name does not have any writes
7190
pub fn iter<S: Schema>(

0 commit comments

Comments
 (0)