Skip to content

Commit

Permalink
Fix input-output-hk#1762: CardanoTransactionSnapshot block number is …
Browse files Browse the repository at this point in the history
…not certified

This is due to the request to retrieve the precomputed block range roots
in database using a exclusive bound comparaison instead of an inclusive,
leading to the exclusion of the last block range root from the retrieved
data.

This means that not only the prover was impacted but the protocol
message generation for the single & multi signatures too.

Co-authored-by: Sébastien Fauvel <sfauvel@users.noreply.github.com>
  • Loading branch information
2 people authored and locallycompact committed Sep 27, 2024
1 parent 4f8a916 commit 6658314
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ impl GetBlockRangeRootQuery {
}
}

pub fn up_to_block_number(block_number: BlockNumber) -> Self {
pub fn up_to_block_number(up_to_or_equal_end_block_number: BlockNumber) -> Self {
Self {
condition: WhereCondition::new("end < ?*", vec![Value::Integer(block_number as i64)]),
condition: WhereCondition::new(
"end <= ?*",
vec![Value::Integer(up_to_or_equal_end_block_number as i64)],
),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ impl CardanoTransactionRepository {
)
}

/// Retrieve all the Block Range Roots in database up to the given end block number excluded.
/// Retrieve all the Block Range Roots in database up to the given end block number included.
pub async fn retrieve_block_range_roots_up_to(
&self,
end_block_number: BlockNumber,
up_to_or_equal_end_block_number: BlockNumber,
) -> StdResult<Box<dyn Iterator<Item = (BlockRange, MKTreeNode)> + '_>> {
let block_range_roots = self
.connection_pool
.connection()?
.fetch(GetBlockRangeRootQuery::up_to_block_number(end_block_number))?
.fetch(GetBlockRangeRootQuery::up_to_block_number(
up_to_or_equal_end_block_number,
))?
.map(|record| -> (BlockRange, MKTreeNode) { record.into() })
.collect::<Vec<_>>(); // TODO: remove this collect to return the iterator directly

Expand Down Expand Up @@ -312,10 +314,10 @@ impl CardanoTransactionRepository {
impl BlockRangeRootRetriever for CardanoTransactionRepository {
async fn retrieve_block_range_roots(
&self,
up_to_beacon: BlockNumber,
up_to_or_equal_beacon: BlockNumber,
) -> StdResult<Box<dyn Iterator<Item = (BlockRange, MKTreeNode)>>> {
let iterator = self
.retrieve_block_range_roots_up_to(up_to_beacon)
.retrieve_block_range_roots_up_to(up_to_or_equal_beacon)
.await?
.collect::<Vec<_>>() // TODO: remove this collect to return the iterator directly
.into_iter();
Expand Down Expand Up @@ -958,21 +960,21 @@ mod tests {
retrieved_block_ranges.collect::<Vec<_>>()
);
}
// The given block is matched to the end (excluded) - should return the first of the three
// Right below the end of the second block range - should return first of the three
{
let retrieved_block_ranges = repository
.retrieve_block_range_roots_up_to(45)
.retrieve_block_range_roots_up_to(44)
.await
.unwrap();
assert_eq!(
vec![block_range_roots[0].clone()],
retrieved_block_ranges.collect::<Vec<_>>()
);
}
// Right after the end of the second block range - should return first two of the three
// The given block is matched to the end (included) - should return the two of the three
{
let retrieved_block_ranges = repository
.retrieve_block_range_roots_up_to(46)
.retrieve_block_range_roots_up_to(45)
.await
.unwrap();
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait BlockRangeRootRetriever: Send + Sync {
/// Returns a Merkle map of the block ranges roots up to a given beacon
async fn retrieve_block_range_roots(
&self,
up_to_beacon: BlockNumber,
up_to_or_equal_beacon: BlockNumber,
) -> StdResult<Box<dyn Iterator<Item = (BlockRange, MKTreeNode)>>>;

/// Returns a Merkle map of the block ranges roots up to a given beacon
Expand Down

0 comments on commit 6658314

Please sign in to comment.