Skip to content

Commit e9dd7aa

Browse files
committed
Add a query to retrieve the highest block_range_root.start block number
1 parent 19493db commit e9dd7aa

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

internal/mithril-persistence/src/database/repository/cardano_transaction_repository.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ impl CardanoTransactionRepository {
161161
)
162162
}
163163

164+
/// Get the highest start [BlockNumber] of the block range roots stored in the database.
165+
pub async fn get_highest_start_block_number_for_block_range_roots(
166+
&self,
167+
) -> StdResult<Option<BlockNumber>> {
168+
let highest: Option<i64> = self
169+
.connection
170+
.query_single_cell("select max(start) as highest from block_range_root;", &[])?;
171+
highest
172+
.map(u64::try_from)
173+
.transpose()
174+
.with_context(||
175+
format!("Integer field max(start) (value={highest:?}) is incompatible with u64 representation.")
176+
)
177+
}
178+
164179
/// Retrieve all the Block Range Roots in database up to the given end block number excluded.
165180
pub async fn retrieve_block_range_roots_up_to(
166181
&self,
@@ -874,4 +889,37 @@ mod tests {
874889
.unwrap();
875890
assert_eq!(Vec::<CardanoTransactionRecord>::new(), transaction_result);
876891
}
892+
893+
#[tokio::test]
894+
async fn get_highest_start_block_number_for_block_range_roots() {
895+
let connection = Arc::new(cardano_tx_db_connection().unwrap());
896+
let repository = CardanoTransactionRepository::new(connection);
897+
898+
let highest = repository
899+
.get_highest_start_block_number_for_block_range_roots()
900+
.await
901+
.unwrap();
902+
assert_eq!(None, highest);
903+
904+
let block_range_roots = vec![
905+
(
906+
BlockRange::from_block_number(15),
907+
MKTreeNode::from_hex("AAAA").unwrap(),
908+
),
909+
(
910+
BlockRange::from_block_number(30),
911+
MKTreeNode::from_hex("BBBB").unwrap(),
912+
),
913+
];
914+
repository
915+
.create_block_range_roots(block_range_roots.clone())
916+
.await
917+
.unwrap();
918+
919+
let highest = repository
920+
.get_highest_start_block_number_for_block_range_roots()
921+
.await
922+
.unwrap();
923+
assert_eq!(Some(30), highest);
924+
}
877925
}

0 commit comments

Comments
 (0)