Handle batch n+1 being signed before batch n is

This commit is contained in:
Luke Parker
2023-08-31 22:09:29 -04:00
parent 9bf24480f4
commit 2dc35193c9
2 changed files with 78 additions and 31 deletions

View File

@@ -1,5 +1,8 @@
use scale::Encode;
use serai_client::primitives::{NetworkId, BlockHash};
use scale::{Encode, Decode};
use serai_client::{
primitives::{NetworkId, BlockHash},
in_instructions::primitives::SignedBatch,
};
pub use serai_db::*;
@@ -95,4 +98,19 @@ impl<'a, D: Db> MainDb<'a, D> {
pub fn first_preprocess<G: Get>(getter: &G, id: [u8; 32]) -> Option<Vec<u8>> {
getter.get(Self::first_preprocess_key(id))
}
fn batch_key(network: NetworkId, id: u32) -> Vec<u8> {
Self::main_key(b"batch", (network, id).encode())
}
pub fn save_batch(&mut self, batch: SignedBatch) {
let mut txn = self.0.txn();
txn.put(Self::batch_key(batch.batch.network, batch.batch.id), batch.encode());
txn.commit();
}
pub fn batch(&self, network: NetworkId, id: u32) -> Option<SignedBatch> {
self
.0
.get(Self::batch_key(network, id))
.map(|batch| SignedBatch::decode(&mut batch.as_ref()).unwrap())
}
}