mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Further work on transaction signing
This commit is contained in:
@@ -20,13 +20,23 @@ ignored = ["borsh", "scale"]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
group = { version = "0.13", default-features = false }
|
||||
async-trait = { version = "0.1", default-features = false }
|
||||
|
||||
ciphersuite = { path = "../../crypto/ciphersuite", default-features = false, features = ["std"] }
|
||||
frost = { package = "modular-frost", path = "../../crypto/frost", default-features = false }
|
||||
|
||||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["std"] }
|
||||
borsh = { version = "1", default-features = false, features = ["std", "derive", "de_strict_order"] }
|
||||
|
||||
serai-validator-sets-primitives = { path = "../../substrate/validator-sets/primitives", default-features = false, features = ["std"] }
|
||||
|
||||
serai-db = { path = "../../common/db" }
|
||||
log = { version = "0.4", default-features = false, features = ["std"] }
|
||||
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "sync", "time", "macros"] }
|
||||
|
||||
messages = { package = "serai-processor-messages", path = "../messages" }
|
||||
primitives = { package = "serai-processor-primitives", path = "../primitives" }
|
||||
scanner = { package = "serai-processor-scanner", path = "../scanner" }
|
||||
scheduler = { package = "serai-scheduler-primitives", path = "../scheduler/primitives" }
|
||||
scheduler = { package = "serai-processor-scheduler-primitives", path = "../scheduler/primitives" }
|
||||
|
||||
frost-attempt-manager = { package = "serai-processor-frost-attempt-manager", path = "../frost-attempt-manager" }
|
||||
|
||||
27
processor/signers/src/db.rs
Normal file
27
processor/signers/src/db.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use serai_validator_sets_primitives::Session;
|
||||
|
||||
use serai_db::{Get, DbTxn, create_db, db_channel};
|
||||
|
||||
use messages::sign::{ProcessorMessage, CoordinatorMessage};
|
||||
|
||||
db_channel! {
|
||||
SignersGlobal {
|
||||
// CompletedEventualities needs to be handled by each signer, meaning we need to turn its
|
||||
// effective spsc into a spmc. We do this by duplicating its message for all keys we're
|
||||
// signing for.
|
||||
// TODO: Populate from CompletedEventualities
|
||||
CompletedEventualitiesForEachKey: (session: Session) -> [u8; 32],
|
||||
|
||||
CoordinatorToTransactionSignerMessages: (session: Session) -> CoordinatorMessage,
|
||||
TransactionSignerToCoordinatorMessages: (session: Session) -> ProcessorMessage,
|
||||
|
||||
CoordinatorToBatchSignerMessages: (session: Session) -> CoordinatorMessage,
|
||||
BatchSignerToCoordinatorMessages: (session: Session) -> ProcessorMessage,
|
||||
|
||||
CoordinatorToSlashReportSignerMessages: (session: Session) -> CoordinatorMessage,
|
||||
SlashReportSignerToCoordinatorMessages: (session: Session) -> ProcessorMessage,
|
||||
|
||||
CoordinatorToCosignerMessages: (session: Session) -> CoordinatorMessage,
|
||||
CosignerToCoordinatorMessages: (session: Session) -> ProcessorMessage,
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,38 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
use core::fmt::Debug;
|
||||
|
||||
use frost::sign::PreprocessMachine;
|
||||
|
||||
use scheduler::SignableTransaction;
|
||||
|
||||
pub(crate) mod db;
|
||||
|
||||
mod transaction;
|
||||
|
||||
/// An object capable of publishing a transaction.
|
||||
#[async_trait::async_trait]
|
||||
pub trait TransactionPublisher<S: SignableTransaction>: 'static + Send + Sync {
|
||||
/// An error encountered when publishing a transaction.
|
||||
///
|
||||
/// This MUST be an ephemeral error. Retrying publication MUST eventually resolve without manual
|
||||
/// intervention/changing the arguments.
|
||||
///
|
||||
/// The transaction already being present in the mempool/on-chain SHOULD NOT be considered an
|
||||
/// error.
|
||||
type EphemeralError: Debug;
|
||||
|
||||
/// Publish a transaction.
|
||||
///
|
||||
/// This will be called multiple times, with the same transaction, until the transaction is
|
||||
/// confirmed on-chain.
|
||||
async fn publish(
|
||||
&self,
|
||||
tx: <S::PreprocessMachine as PreprocessMachine>::Signature,
|
||||
) -> Result<(), Self::EphemeralError>;
|
||||
}
|
||||
|
||||
/*
|
||||
// The signers used by a Processor, key-scoped.
|
||||
struct KeySigners<D: Db, T: Clone + PreprocessMachine> {
|
||||
|
||||
@@ -1,68 +1,127 @@
|
||||
use serai_db::{Get, DbTxn, Db};
|
||||
use frost::dkg::ThresholdKeys;
|
||||
|
||||
use serai_validator_sets_primitives::Session;
|
||||
|
||||
use serai_db::{DbTxn, Db};
|
||||
|
||||
use primitives::task::ContinuallyRan;
|
||||
use scanner::ScannerFeed;
|
||||
use scheduler::TransactionsToSign;
|
||||
use scheduler::{SignableTransaction, TransactionsToSign};
|
||||
use scanner::{ScannerFeed, Scheduler};
|
||||
|
||||
use frost_attempt_manager::*;
|
||||
|
||||
use crate::{
|
||||
db::{
|
||||
CoordinatorToTransactionSignerMessages, TransactionSignerToCoordinatorMessages,
|
||||
CompletedEventualitiesForEachKey,
|
||||
},
|
||||
TransactionPublisher,
|
||||
};
|
||||
|
||||
mod db;
|
||||
use db::IndexDb;
|
||||
|
||||
// Fetches transactions to sign and signs them.
|
||||
pub(crate) struct TransactionTask<D: Db, S: ScannerFeed, Sch: Scheduler> {
|
||||
pub(crate) struct TransactionTask<
|
||||
D: Db,
|
||||
S: ScannerFeed,
|
||||
Sch: Scheduler<S>,
|
||||
P: TransactionPublisher<Sch::SignableTransaction>,
|
||||
> {
|
||||
db: D,
|
||||
keys: ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>,
|
||||
session: Session,
|
||||
keys: Vec<ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>>,
|
||||
attempt_manager:
|
||||
AttemptManager<D, <Sch::SignableTransaction as SignableTransaction>::PreprocessMachine>,
|
||||
publisher: P,
|
||||
}
|
||||
|
||||
impl<D: Db, S: ScannerFeed> TransactionTask<D, S> {
|
||||
pub(crate) async fn new(
|
||||
impl<D: Db, S: ScannerFeed, Sch: Scheduler<S>, P: TransactionPublisher<Sch::SignableTransaction>>
|
||||
TransactionTask<D, S, Sch, P>
|
||||
{
|
||||
pub(crate) fn new(
|
||||
db: D,
|
||||
keys: ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>,
|
||||
session: Session,
|
||||
keys: Vec<ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>>,
|
||||
publisher: P,
|
||||
) -> Self {
|
||||
Self { db, keys, attempt_manager: AttemptManager::new() }
|
||||
let attempt_manager = AttemptManager::new(
|
||||
db.clone(),
|
||||
session,
|
||||
keys.first().expect("creating a transaction signer with 0 keys").params().i(),
|
||||
);
|
||||
Self { db, session, keys, attempt_manager, publisher }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<D: Db, S: ScannerFeed> ContinuallyRan for TransactionTask<D, S> {
|
||||
impl<D: Db, S: ScannerFeed, Sch: Scheduler<S>, P: TransactionPublisher<Sch::SignableTransaction>>
|
||||
ContinuallyRan for TransactionTask<D, S, Sch, P>
|
||||
{
|
||||
async fn run_iteration(&mut self) -> Result<bool, String> {
|
||||
let mut iterated = false;
|
||||
|
||||
// Check for new transactions to sign
|
||||
loop {
|
||||
let mut txn = self.db.txn();
|
||||
let Some(tx) = TransactionsToSign::try_recv(&mut txn, self.key) else { break };
|
||||
let Some(tx) = TransactionsToSign::<Sch::SignableTransaction>::try_recv(
|
||||
&mut txn,
|
||||
&self.keys[0].group_key(),
|
||||
) else {
|
||||
break;
|
||||
};
|
||||
iterated = true;
|
||||
|
||||
let mut machines = Vec::with_capacity(self.keys.len());
|
||||
for keys in &self.keys {
|
||||
machines.push(tx.clone().sign(keys.clone()));
|
||||
}
|
||||
let messages = self.attempt_manager.register(tx.id(), machines);
|
||||
todo!("TODO");
|
||||
for msg in self.attempt_manager.register(tx.id(), machines) {
|
||||
TransactionSignerToCoordinatorMessages::send(&mut txn, self.session, &msg);
|
||||
}
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
// Check for completed Eventualities (meaning we should no longer sign for these transactions)
|
||||
loop {
|
||||
let mut txn = self.db.txn();
|
||||
let Some(tx) = CompletedEventualities::try_recv(&mut txn, self.key) else { break };
|
||||
let Some(id) = CompletedEventualitiesForEachKey::try_recv(&mut txn, self.session) else {
|
||||
break;
|
||||
};
|
||||
iterated = true;
|
||||
|
||||
self.attempt_manager.retire(tx);
|
||||
self.attempt_manager.retire(id);
|
||||
// TODO: Stop rebroadcasting this transaction
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
// Handle any messages sent to us
|
||||
loop {
|
||||
let mut txn = self.db.txn();
|
||||
let Some(msg) = TransactionSignMessages::try_recv(&mut txn, self.key) else { break };
|
||||
let Some(msg) = CoordinatorToTransactionSignerMessages::try_recv(&mut txn, self.session)
|
||||
else {
|
||||
break;
|
||||
};
|
||||
iterated = true;
|
||||
|
||||
match self.attempt_manager.handle(msg) {
|
||||
Response::Messages(messages) => todo!("TODO"),
|
||||
Response::Signature(signature) => todo!("TODO"),
|
||||
Response::Messages(msgs) => {
|
||||
for msg in msgs {
|
||||
TransactionSignerToCoordinatorMessages::send(&mut txn, self.session, &msg);
|
||||
}
|
||||
}
|
||||
Response::Signature(signed_tx) => {
|
||||
// TODO: Save this TX to the DB
|
||||
// TODO: Attempt publication every minute
|
||||
// TODO: On boot, reload all TXs to rebroadcast
|
||||
self
|
||||
.publisher
|
||||
.publish(signed_tx)
|
||||
.await
|
||||
.map_err(|e| format!("couldn't publish transaction: {e:?}"))?;
|
||||
}
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
Ok(iterated)
|
||||
|
||||
Reference in New Issue
Block a user