Make a dedicated IndexDb

This commit is contained in:
Luke Parker
2024-08-26 23:24:49 -04:00
parent 66f3428051
commit 1e8f4e6156
4 changed files with 47 additions and 34 deletions

View File

@@ -0,0 +1,34 @@
use serai_db::{Get, DbTxn, create_db};
create_db!(
ScannerIndex {
// A lookup of a block's number to its ID
BlockId: (number: u64) -> [u8; 32],
// A lookup of a block's ID to its number
BlockNumber: (id: [u8; 32]) -> u64,
// The latest finalized block to appear on the blockchain
LatestFinalizedBlock: () -> u64,
}
);
pub(crate) struct IndexDb;
impl IndexDb {
pub(crate) fn set_block(txn: &mut impl DbTxn, number: u64, id: [u8; 32]) {
BlockId::set(txn, number, &id);
BlockNumber::set(txn, id, &number);
}
pub(crate) fn block_id(getter: &impl Get, number: u64) -> Option<[u8; 32]> {
BlockId::get(getter, number)
}
pub(crate) fn block_number(getter: &impl Get, id: [u8; 32]) -> Option<u64> {
BlockNumber::get(getter, id)
}
pub(crate) fn set_latest_finalized_block(txn: &mut impl DbTxn, latest_finalized_block: u64) {
LatestFinalizedBlock::set(txn, &latest_finalized_block);
}
pub(crate) fn latest_finalized_block(getter: &impl Get) -> Option<u64> {
LatestFinalizedBlock::get(getter)
}
}