diff --git a/processor/src/substrate_signer.rs b/processor/src/substrate_signer.rs index e9a40bb7..645603af 100644 --- a/processor/src/substrate_signer.rs +++ b/processor/src/substrate_signer.rs @@ -19,49 +19,25 @@ use log::{info, debug, warn}; use scale::Encode; use serai_client::{ - primitives::NetworkId, + primitives::{NetworkId, BlockHash}, in_instructions::primitives::{Batch, SignedBatch, batch_message}, }; use messages::coordinator::*; -use crate::{Get, DbTxn, Db}; +use crate::{Get, DbTxn, Db, create_db}; // Generate an ID unique to a Batch fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] { (network, id).encode().try_into().unwrap() } -#[derive(Debug)] -struct SubstrateSignerDb(D); -impl SubstrateSignerDb { - fn sign_key(dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec { - D::key(b"SUBSTRATE_SIGNER", dst, key) +create_db!( + SubstrateSignerDb { + CompletedDb: (id: [u8; 5]) -> (), + AttemptDb: (id: [u8; 5], attempt: u32) -> (), + BatchDb: (block: BlockHash) -> SignedBatch } - - fn completed_key(id: [u8; 5]) -> Vec { - Self::sign_key(b"completed", id) - } - fn complete(txn: &mut D::Transaction<'_>, id: [u8; 5]) { - txn.put(Self::completed_key(id), []); - } - fn completed(getter: &G, id: [u8; 5]) -> bool { - getter.get(Self::completed_key(id)).is_some() - } - - fn attempt_key(id: [u8; 5], attempt: u32) -> Vec { - Self::sign_key(b"attempt", (id, attempt).encode()) - } - fn attempt(txn: &mut D::Transaction<'_>, id: [u8; 5], attempt: u32) { - txn.put(Self::attempt_key(id, attempt), []); - } - fn has_attempt(getter: &G, id: [u8; 5], attempt: u32) -> bool { - getter.get(Self::attempt_key(id, attempt)).is_some() - } - - fn save_batch(txn: &mut D::Transaction<'_>, batch: &SignedBatch) { - txn.put(Self::sign_key(b"batch", batch.batch.block), batch.encode()); - } -} +); type Preprocess = as PreprocessMachine>::Preprocess; type SignatureShare = as SignMachine< @@ -150,7 +126,7 @@ impl SubstrateSigner { attempt: u32, ) -> Option { // See above commentary for why this doesn't emit SignedBatch - if SubstrateSignerDb::::completed(txn, id) { + if CompletedDb::get(txn, id).is_some() { return None; } @@ -197,7 +173,7 @@ impl SubstrateSigner { // // Only run if this hasn't already been attempted // TODO: This isn't complete as this txn may not be committed with the expected timing - if SubstrateSignerDb::::has_attempt(txn, id, attempt) { + if AttemptDb::get(txn, id, attempt).is_some() { warn!( "already attempted batch {}, attempt #{}. this is an error if we didn't reboot", hex::encode(id), @@ -205,7 +181,7 @@ impl SubstrateSigner { ); return None; } - SubstrateSignerDb::::attempt(txn, id, attempt); + AttemptDb::set(txn, id, attempt, &()); let mut machines = vec![]; let mut preprocesses = vec![]; @@ -239,7 +215,7 @@ impl SubstrateSigner { ) -> Option { debug_assert_eq!(self.network, batch.network); let id = batch_sign_id(batch.network, batch.id); - if SubstrateSignerDb::::completed(txn, id) { + if CompletedDb::get(txn, id).is_some() { debug!("Sign batch order for ID we've already completed signing"); // See batch_signed for commentary on why this simply returns return None; @@ -428,8 +404,8 @@ impl SubstrateSigner { SignedBatch { batch: self.signable.remove(&id).unwrap(), signature: sig.into() }; // Save the batch in case it's needed for recovery - SubstrateSignerDb::::save_batch(txn, &batch); - SubstrateSignerDb::::complete(txn, id); + BatchDb::set(txn, batch.batch.block, &batch); + CompletedDb::set(txn, id, &()); // Stop trying to sign for this batch assert!(self.attempt.remove(&id).is_some()); @@ -452,7 +428,7 @@ impl SubstrateSigner { let sign_id = batch_sign_id(self.network, id); // Stop trying to sign for this batch - SubstrateSignerDb::::complete(txn, sign_id); + CompletedDb::set(txn, sign_id, &()); self.signable.remove(&sign_id); self.attempt.remove(&sign_id);