mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
implement db macro for processor/substrate_signer (#439)
* implement db macro for processor/substrate_signer * Use () * Correct AttemptDb usage of () * () -> &() --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
@@ -19,49 +19,25 @@ use log::{info, debug, warn};
|
|||||||
|
|
||||||
use scale::Encode;
|
use scale::Encode;
|
||||||
use serai_client::{
|
use serai_client::{
|
||||||
primitives::NetworkId,
|
primitives::{NetworkId, BlockHash},
|
||||||
in_instructions::primitives::{Batch, SignedBatch, batch_message},
|
in_instructions::primitives::{Batch, SignedBatch, batch_message},
|
||||||
};
|
};
|
||||||
|
|
||||||
use messages::coordinator::*;
|
use messages::coordinator::*;
|
||||||
use crate::{Get, DbTxn, Db};
|
use crate::{Get, DbTxn, Db, create_db};
|
||||||
|
|
||||||
// Generate an ID unique to a Batch
|
// Generate an ID unique to a Batch
|
||||||
fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] {
|
fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] {
|
||||||
(network, id).encode().try_into().unwrap()
|
(network, id).encode().try_into().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
create_db!(
|
||||||
struct SubstrateSignerDb<D: Db>(D);
|
SubstrateSignerDb {
|
||||||
impl<D: Db> SubstrateSignerDb<D> {
|
CompletedDb: (id: [u8; 5]) -> (),
|
||||||
fn sign_key(dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec<u8> {
|
AttemptDb: (id: [u8; 5], attempt: u32) -> (),
|
||||||
D::key(b"SUBSTRATE_SIGNER", dst, key)
|
BatchDb: (block: BlockHash) -> SignedBatch
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fn completed_key(id: [u8; 5]) -> Vec<u8> {
|
|
||||||
Self::sign_key(b"completed", id)
|
|
||||||
}
|
|
||||||
fn complete(txn: &mut D::Transaction<'_>, id: [u8; 5]) {
|
|
||||||
txn.put(Self::completed_key(id), []);
|
|
||||||
}
|
|
||||||
fn completed<G: Get>(getter: &G, id: [u8; 5]) -> bool {
|
|
||||||
getter.get(Self::completed_key(id)).is_some()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn attempt_key(id: [u8; 5], attempt: u32) -> Vec<u8> {
|
|
||||||
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<G: Get>(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 = <AlgorithmMachine<Ristretto, Schnorrkel> as PreprocessMachine>::Preprocess;
|
type Preprocess = <AlgorithmMachine<Ristretto, Schnorrkel> as PreprocessMachine>::Preprocess;
|
||||||
type SignatureShare = <AlgorithmSignMachine<Ristretto, Schnorrkel> as SignMachine<
|
type SignatureShare = <AlgorithmSignMachine<Ristretto, Schnorrkel> as SignMachine<
|
||||||
@@ -150,7 +126,7 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
attempt: u32,
|
attempt: u32,
|
||||||
) -> Option<ProcessorMessage> {
|
) -> Option<ProcessorMessage> {
|
||||||
// See above commentary for why this doesn't emit SignedBatch
|
// See above commentary for why this doesn't emit SignedBatch
|
||||||
if SubstrateSignerDb::<D>::completed(txn, id) {
|
if CompletedDb::get(txn, id).is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +173,7 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
//
|
//
|
||||||
// Only run if this hasn't already been attempted
|
// 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
|
// TODO: This isn't complete as this txn may not be committed with the expected timing
|
||||||
if SubstrateSignerDb::<D>::has_attempt(txn, id, attempt) {
|
if AttemptDb::get(txn, id, attempt).is_some() {
|
||||||
warn!(
|
warn!(
|
||||||
"already attempted batch {}, attempt #{}. this is an error if we didn't reboot",
|
"already attempted batch {}, attempt #{}. this is an error if we didn't reboot",
|
||||||
hex::encode(id),
|
hex::encode(id),
|
||||||
@@ -205,7 +181,7 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
);
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
SubstrateSignerDb::<D>::attempt(txn, id, attempt);
|
AttemptDb::set(txn, id, attempt, &());
|
||||||
|
|
||||||
let mut machines = vec![];
|
let mut machines = vec![];
|
||||||
let mut preprocesses = vec![];
|
let mut preprocesses = vec![];
|
||||||
@@ -239,7 +215,7 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
) -> Option<ProcessorMessage> {
|
) -> Option<ProcessorMessage> {
|
||||||
debug_assert_eq!(self.network, batch.network);
|
debug_assert_eq!(self.network, batch.network);
|
||||||
let id = batch_sign_id(batch.network, batch.id);
|
let id = batch_sign_id(batch.network, batch.id);
|
||||||
if SubstrateSignerDb::<D>::completed(txn, id) {
|
if CompletedDb::get(txn, id).is_some() {
|
||||||
debug!("Sign batch order for ID we've already completed signing");
|
debug!("Sign batch order for ID we've already completed signing");
|
||||||
// See batch_signed for commentary on why this simply returns
|
// See batch_signed for commentary on why this simply returns
|
||||||
return None;
|
return None;
|
||||||
@@ -428,8 +404,8 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
SignedBatch { batch: self.signable.remove(&id).unwrap(), signature: sig.into() };
|
SignedBatch { batch: self.signable.remove(&id).unwrap(), signature: sig.into() };
|
||||||
|
|
||||||
// Save the batch in case it's needed for recovery
|
// Save the batch in case it's needed for recovery
|
||||||
SubstrateSignerDb::<D>::save_batch(txn, &batch);
|
BatchDb::set(txn, batch.batch.block, &batch);
|
||||||
SubstrateSignerDb::<D>::complete(txn, id);
|
CompletedDb::set(txn, id, &());
|
||||||
|
|
||||||
// Stop trying to sign for this batch
|
// Stop trying to sign for this batch
|
||||||
assert!(self.attempt.remove(&id).is_some());
|
assert!(self.attempt.remove(&id).is_some());
|
||||||
@@ -452,7 +428,7 @@ impl<D: Db> SubstrateSigner<D> {
|
|||||||
let sign_id = batch_sign_id(self.network, id);
|
let sign_id = batch_sign_id(self.network, id);
|
||||||
|
|
||||||
// Stop trying to sign for this batch
|
// Stop trying to sign for this batch
|
||||||
SubstrateSignerDb::<D>::complete(txn, sign_id);
|
CompletedDb::set(txn, sign_id, &());
|
||||||
|
|
||||||
self.signable.remove(&sign_id);
|
self.signable.remove(&sign_id);
|
||||||
self.attempt.remove(&sign_id);
|
self.attempt.remove(&sign_id);
|
||||||
|
|||||||
Reference in New Issue
Block a user