Support multiple batches per block by the coordinator

Also corrects an assumption block hash == batch ID.
This commit is contained in:
Luke Parker
2023-08-24 19:06:22 -04:00
parent dc2656a538
commit b91bd44476
4 changed files with 77 additions and 26 deletions

View File

@@ -1,3 +1,6 @@
use scale::Encode;
use serai_client::primitives::{NetworkId, BlockHash};
pub use serai_db::*;
use crate::tributary::TributarySpec;
@@ -42,6 +45,42 @@ impl<'a, D: Db> MainDb<'a, D> {
txn.commit();
}
fn batches_in_block_key(network: NetworkId, block: [u8; 32]) -> Vec<u8> {
Self::main_key(b"batches_in_block", (network, block).encode())
}
pub fn batches_in_block<G: Get>(
getter: &G,
network: NetworkId,
block: [u8; 32],
) -> Vec<[u8; 32]> {
getter
.get(Self::batches_in_block_key(network, block))
.expect("asking for batches in block for block without batches")
.chunks(32)
.map(|id| id.try_into().unwrap())
.collect()
}
pub fn add_batch_to_block(
txn: &mut D::Transaction<'_>,
network: NetworkId,
block: BlockHash,
id: [u8; 32],
) {
let key = Self::batches_in_block_key(network, block.0);
let Some(mut existing) = txn.get(&key) else {
txn.put(&key, block.0);
return;
};
if existing.chunks(32).any(|existing_id| existing_id == id) {
// TODO: Is this an invariant?
return;
}
existing.extend(block.0);
txn.put(&key, existing);
}
fn first_preprocess_key(id: [u8; 32]) -> Vec<u8> {
Self::main_key(b"first_preprocess", id)
}