mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Make a dedicated IndexDb
This commit is contained in:
34
processor/scanner/src/index/db.rs
Normal file
34
processor/scanner/src/index/db.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
use serai_db::{DbTxn, Db};
|
||||
|
||||
use primitives::BlockHeader;
|
||||
use primitives::{task::ContinuallyRan, BlockHeader};
|
||||
|
||||
// TODO: Localize to IndexDb?
|
||||
use crate::{db::ScannerDb, ScannerFeed, ContinuallyRan};
|
||||
use crate::ScannerFeed;
|
||||
|
||||
mod db;
|
||||
pub(crate) use db::IndexDb;
|
||||
|
||||
/*
|
||||
This processor should build its own index of the blockchain, yet only for finalized blocks which
|
||||
@@ -22,7 +24,7 @@ struct IndexFinalizedTask<D: Db, S: ScannerFeed> {
|
||||
impl<D: Db, S: ScannerFeed> ContinuallyRan for IndexFinalizedTask<D, S> {
|
||||
async fn run_iteration(&mut self) -> Result<bool, String> {
|
||||
// Fetch the latest finalized block
|
||||
let our_latest_finalized = ScannerDb::<S>::latest_finalized_block(&self.db)
|
||||
let our_latest_finalized = IndexDb::latest_finalized_block(&self.db)
|
||||
.expect("IndexTask run before writing the start block");
|
||||
let latest_finalized = match self.feed.latest_finalized_block_number().await {
|
||||
Ok(latest_finalized) => latest_finalized,
|
||||
@@ -51,7 +53,7 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for IndexFinalizedTask<D, S> {
|
||||
// Check this descends from our indexed chain
|
||||
{
|
||||
let expected_parent =
|
||||
ScannerDb::<S>::block_id(&self.db, b - 1).expect("didn't have the ID of the prior block");
|
||||
IndexDb::block_id(&self.db, b - 1).expect("didn't have the ID of the prior block");
|
||||
if block.parent() != expected_parent {
|
||||
panic!(
|
||||
"current finalized block (#{b}, {}) doesn't build off finalized block (#{}, {})",
|
||||
@@ -64,8 +66,8 @@ impl<D: Db, S: ScannerFeed> ContinuallyRan for IndexFinalizedTask<D, S> {
|
||||
|
||||
// Update the latest finalized block
|
||||
let mut txn = self.db.txn();
|
||||
ScannerDb::<S>::set_block(&mut txn, b, block.id());
|
||||
ScannerDb::<S>::set_latest_finalized_block(&mut txn, b);
|
||||
IndexDb::set_block(&mut txn, b, block.id());
|
||||
IndexDb::set_latest_finalized_block(&mut txn, b);
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user