Have the Coordinator scan the Substrate genesis block

Also adds a workflow for running tests/coordinator.
This commit is contained in:
Luke Parker
2023-08-02 12:18:50 -04:00
parent d5c787fea2
commit aab8a417db
6 changed files with 56 additions and 11 deletions

View File

@@ -107,7 +107,7 @@ pub async fn scan_substrate<D: Db, Pro: Processors>(
log::info!("scanning substrate");
let mut db = substrate::SubstrateDb::new(db);
let mut last_substrate_block = db.last_block();
let mut next_substrate_block = db.next_block();
loop {
match substrate::handle_new_blocks(
@@ -126,7 +126,7 @@ pub async fn scan_substrate<D: Db, Pro: Processors>(
},
&processors,
&serai,
&mut last_substrate_block,
&mut next_substrate_block,
)
.await
{

View File

@@ -14,12 +14,12 @@ impl<D: Db> SubstrateDb<D> {
fn block_key() -> Vec<u8> {
Self::substrate_key(b"block", [])
}
pub fn set_last_block(&mut self, block: u64) {
pub fn set_next_block(&mut self, block: u64) {
let mut txn = self.0.txn();
txn.put(Self::block_key(), block.to_le_bytes());
txn.commit();
}
pub fn last_block(&self) -> u64 {
pub fn next_block(&self) -> u64 {
u64::from_le_bytes(self.0.get(Self::block_key()).unwrap_or(vec![0; 8]).try_into().unwrap())
}

View File

@@ -302,17 +302,17 @@ pub async fn handle_new_blocks<
create_new_tributary: CNT,
processors: &Pro,
serai: &Serai,
last_block: &mut u64,
next_block: &mut u64,
) -> Result<(), SeraiError> {
// Check if there's been a new Substrate block
let latest = serai.get_latest_block().await?;
let latest_number = latest.number();
if latest_number == *last_block {
if latest_number < *next_block {
return Ok(());
}
let mut latest = Some(latest);
for b in (*last_block + 1) ..= latest_number {
for b in *next_block ..= latest_number {
log::info!("found substrate block {b}");
handle_block(
db,
@@ -330,8 +330,8 @@ pub async fn handle_new_blocks<
},
)
.await?;
*last_block += 1;
db.set_last_block(*last_block);
*next_block += 1;
db.set_next_block(*next_block);
log::info!("handled substrate block {b}");
}