Implement Tributary syncing

Also adds a forwards-lookup to the Tributary blockchain.
This commit is contained in:
Luke Parker
2023-04-24 00:53:15 -04:00
parent 215155f84b
commit 14388e746c
6 changed files with 119 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
use core::ops::Deref;
use std::collections::{VecDeque, HashMap};
use std::collections::HashMap;
use zeroize::Zeroizing;
@@ -297,43 +297,11 @@ pub async fn handle_new_blocks<D: Db, Pro: Processor, P: P2p>(
spec: &TributarySpec,
tributary: &Tributary<D, Transaction, P>,
) {
let last_block = db.last_block(tributary.genesis());
// Check if there's been a new Tributary block
let latest = tributary.tip().await;
if latest == last_block {
return;
}
let mut blocks = VecDeque::new();
// This is a new block, as per the prior if check
blocks.push_back(tributary.block(&latest).unwrap());
let mut block = None;
while {
let parent = blocks.back().unwrap().parent();
// If the parent is the genesis, we've reached the end
if parent == tributary.genesis() {
false
} else {
// Get this block
block = Some(tributary.block(&parent).unwrap());
// If it's the last block we've scanned, it's the end. Else, push it
block.as_ref().unwrap().hash() != last_block
}
} {
blocks.push_back(block.take().unwrap());
// Prevent this from loading the entire chain into RAM by setting a limit of 1000 blocks at a
// time (roughly 350 MB under the current block size limit)
if blocks.len() > 1000 {
blocks.pop_front();
}
}
while let Some(block) = blocks.pop_back() {
let hash = block.hash();
let mut last_block = db.last_block(tributary.genesis());
while let Some(next) = tributary.block_after(&last_block) {
let block = tributary.block(&next).unwrap();
handle_block(db, key, processor, spec, tributary, block).await;
db.set_last_block(tributary.genesis(), hash);
last_block = next;
db.set_last_block(tributary.genesis(), next);
}
}