mirror of
https://github.com/serai-dex/serai.git
synced 2024-12-25 04:59:31 +00:00
Minor work on the transaction signing task
This commit is contained in:
parent
8380653855
commit
b62fc3a1fa
14 changed files with 169 additions and 2 deletions
|
@ -32,6 +32,8 @@ pub struct AttemptManager<D: Db, M: Clone + PreprocessMachine> {
|
||||||
|
|
||||||
impl<D: Db, M: Clone + PreprocessMachine> AttemptManager<D, M> {
|
impl<D: Db, M: Clone + PreprocessMachine> AttemptManager<D, M> {
|
||||||
/// Create a new attempt manager.
|
/// Create a new attempt manager.
|
||||||
|
///
|
||||||
|
/// This will not restore any signing sessions from the database. Those must be re-registered.
|
||||||
pub fn new(db: D, session: Session, start_i: Participant) -> Self {
|
pub fn new(db: D, session: Session, start_i: Participant) -> Self {
|
||||||
AttemptManager { db, session, start_i, active: HashMap::new() }
|
AttemptManager { db, session, start_i, active: HashMap::new() }
|
||||||
}
|
}
|
||||||
|
@ -52,7 +54,7 @@ impl<D: Db, M: Clone + PreprocessMachine> AttemptManager<D, M> {
|
||||||
/// This frees all memory used for it and means no further messages will be handled for it.
|
/// This frees all memory used for it and means no further messages will be handled for it.
|
||||||
/// This does not stop the protocol from being re-registered and further worked on (with
|
/// This does not stop the protocol from being re-registered and further worked on (with
|
||||||
/// undefined behavior) then. The higher-level context must never call `register` again with this
|
/// undefined behavior) then. The higher-level context must never call `register` again with this
|
||||||
/// ID.
|
/// ID accordingly.
|
||||||
pub fn retire(&mut self, id: [u8; 32]) {
|
pub fn retire(&mut self, id: [u8; 32]) {
|
||||||
if self.active.remove(&id).is_none() {
|
if self.active.remove(&id).is_none() {
|
||||||
log::info!("retiring protocol {}, which we didn't register/already retired", hex::encode(id));
|
log::info!("retiring protocol {}, which we didn't register/already retired", hex::encode(id));
|
||||||
|
|
|
@ -7,6 +7,11 @@ pub trait Eventuality: Sized + Send + Sync {
|
||||||
/// The type used to identify a received output.
|
/// The type used to identify a received output.
|
||||||
type OutputId: Id;
|
type OutputId: Id;
|
||||||
|
|
||||||
|
/// The ID of the transaction this Eventuality is for.
|
||||||
|
///
|
||||||
|
/// This is an internal ID arbitrarily definable so long as it's unique.
|
||||||
|
fn id(&self) -> [u8; 32];
|
||||||
|
|
||||||
/// A unique byte sequence which can be used to identify potentially resolving transactions.
|
/// A unique byte sequence which can be used to identify potentially resolving transactions.
|
||||||
///
|
///
|
||||||
/// Both a transaction and an Eventuality are expected to be able to yield lookup sequences.
|
/// Both a transaction and an Eventuality are expected to be able to yield lookup sequences.
|
||||||
|
|
|
@ -39,3 +39,4 @@ serai-in-instructions-primitives = { path = "../../substrate/in-instructions/pri
|
||||||
serai-coins-primitives = { path = "../../substrate/coins/primitives", default-features = false, features = ["std", "borsh"] }
|
serai-coins-primitives = { path = "../../substrate/coins/primitives", default-features = false, features = ["std", "borsh"] }
|
||||||
|
|
||||||
primitives = { package = "serai-processor-primitives", path = "../primitives" }
|
primitives = { package = "serai-processor-primitives", path = "../primitives" }
|
||||||
|
scheduler-primitives = { package = "serai-processor-scheduler-primitives", path = "../scheduler/primitives" }
|
||||||
|
|
|
@ -247,6 +247,9 @@ impl<S: ScannerFeed> SchedulerUpdate<S> {
|
||||||
|
|
||||||
/// The object responsible for accumulating outputs and planning new transactions.
|
/// The object responsible for accumulating outputs and planning new transactions.
|
||||||
pub trait Scheduler<S: ScannerFeed>: 'static + Send {
|
pub trait Scheduler<S: ScannerFeed>: 'static + Send {
|
||||||
|
/// The type for a signable transaction.
|
||||||
|
type SignableTransaction: scheduler_primitives::SignableTransaction;
|
||||||
|
|
||||||
/// Activate a key.
|
/// Activate a key.
|
||||||
///
|
///
|
||||||
/// This SHOULD setup any necessary database structures. This SHOULD NOT cause the new key to
|
/// This SHOULD setup any necessary database structures. This SHOULD NOT cause the new key to
|
||||||
|
|
|
@ -10,11 +10,26 @@ use group::GroupEncoding;
|
||||||
use serai_db::DbTxn;
|
use serai_db::DbTxn;
|
||||||
|
|
||||||
/// A signable transaction.
|
/// A signable transaction.
|
||||||
pub trait SignableTransaction: 'static + Sized + Send + Sync {
|
pub trait SignableTransaction: 'static + Sized + Send + Sync + Clone {
|
||||||
|
/// The ciphersuite used to sign this transaction.
|
||||||
|
type Ciphersuite: Cuphersuite;
|
||||||
|
/// The preprocess machine for the signing protocol for this transaction.
|
||||||
|
type PreprocessMachine: PreprocessMachine;
|
||||||
|
|
||||||
/// Read a `SignableTransaction`.
|
/// Read a `SignableTransaction`.
|
||||||
fn read(reader: &mut impl io::Read) -> io::Result<Self>;
|
fn read(reader: &mut impl io::Read) -> io::Result<Self>;
|
||||||
/// Write a `SignableTransaction`.
|
/// Write a `SignableTransaction`.
|
||||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()>;
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()>;
|
||||||
|
|
||||||
|
/// The ID for this transaction.
|
||||||
|
///
|
||||||
|
/// This is an internal ID arbitrarily definable so long as it's unique.
|
||||||
|
///
|
||||||
|
/// This same ID MUST be returned by the Eventuality for this transaction.
|
||||||
|
fn id(&self) -> [u8; 32];
|
||||||
|
|
||||||
|
/// Sign this transaction.
|
||||||
|
fn sign(self, keys: ThresholdKeys<Self::Ciphersuite>) -> Self::PreprocessMachine;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod db {
|
mod db {
|
||||||
|
|
|
@ -309,6 +309,8 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Scheduler<S, P> {
|
impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Scheduler<S, P> {
|
||||||
|
type SignableTransaction = P::SignableTransaction;
|
||||||
|
|
||||||
fn activate_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
fn activate_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
||||||
for coin in S::NETWORK.coins() {
|
for coin in S::NETWORK.coins() {
|
||||||
assert!(Db::<S>::outputs(txn, key, *coin).is_none());
|
assert!(Db::<S>::outputs(txn, key, *coin).is_none());
|
||||||
|
|
|
@ -368,6 +368,8 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> Sched
|
||||||
impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> SchedulerTrait<S>
|
impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> SchedulerTrait<S>
|
||||||
for Scheduler<S, P>
|
for Scheduler<S, P>
|
||||||
{
|
{
|
||||||
|
type SignableTransaction = P::SignableTransaction;
|
||||||
|
|
||||||
fn activate_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
fn activate_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
||||||
for coin in S::NETWORK.coins() {
|
for coin in S::NETWORK.coins() {
|
||||||
assert!(Db::<S>::outputs(txn, key, *coin).is_none());
|
assert!(Db::<S>::outputs(txn, key, *coin).is_none());
|
||||||
|
|
|
@ -20,3 +20,13 @@ ignored = ["borsh", "scale"]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
group = { version = "0.13", default-features = false }
|
||||||
|
|
||||||
|
log = { version = "0.4", default-features = false, features = ["std"] }
|
||||||
|
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "sync", "time", "macros"] }
|
||||||
|
|
||||||
|
primitives = { package = "serai-processor-primitives", path = "../primitives" }
|
||||||
|
scanner = { package = "serai-processor-scanner", path = "../scanner" }
|
||||||
|
scheduler = { package = "serai-scheduler-primitives", path = "../scheduler/primitives" }
|
||||||
|
|
||||||
|
frost-attempt-manager = { package = "serai-processor-frost-attempt-manager", path = "../frost-attempt-manager" }
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
|
#![doc = include_str!("../README.md")]
|
||||||
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
|
mod transaction;
|
||||||
|
|
||||||
|
/*
|
||||||
|
// The signers used by a Processor, key-scoped.
|
||||||
|
struct KeySigners<D: Db, T: Clone + PreprocessMachine> {
|
||||||
|
transaction: AttemptManager<D, T>,
|
||||||
|
substrate: AttemptManager<D, AlgorithmMachine<Ristretto, Schnorrkel>>,
|
||||||
|
cosigner: AttemptManager<D, AlgorithmMachine<Ristretto, Schnorrkel>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The signers used by a protocol.
|
||||||
|
pub struct Signers<D: Db, T: Clone + PreprocessMachine>(HashMap<Vec<u8>, KeySigners<D, T>>);
|
||||||
|
|
||||||
|
impl<D: Db, T: Clone + PreprocessMachine> Signers<D, T> {
|
||||||
|
/// Create a new set of signers.
|
||||||
|
pub fn new(db: D) -> Self {
|
||||||
|
// TODO: Load the registered keys
|
||||||
|
// TODO: Load the transactions being signed
|
||||||
|
// TODO: Load the batches being signed
|
||||||
|
todo!("TODO")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register a transaction to sign.
|
||||||
|
pub fn sign_transaction(&mut self) -> Vec<ProcessorMessage> {
|
||||||
|
todo!("TODO")
|
||||||
|
}
|
||||||
|
/// Mark a transaction as signed.
|
||||||
|
pub fn signed_transaction(&mut self) { todo!("TODO") }
|
||||||
|
|
||||||
|
/// Register a batch to sign.
|
||||||
|
pub fn sign_batch(&mut self, key: KeyFor<S>, batch: Batch) -> Vec<ProcessorMessage> {
|
||||||
|
todo!("TODO")
|
||||||
|
}
|
||||||
|
/// Mark a batch as signed.
|
||||||
|
pub fn signed_batch(&mut self, batch: u32) { todo!("TODO") }
|
||||||
|
|
||||||
|
/// Register a slash report to sign.
|
||||||
|
pub fn sign_slash_report(&mut self) -> Vec<ProcessorMessage> {
|
||||||
|
todo!("TODO")
|
||||||
|
}
|
||||||
|
/// Mark a slash report as signed.
|
||||||
|
pub fn signed_slash_report(&mut self) { todo!("TODO") }
|
||||||
|
|
||||||
|
/// Start a cosigning protocol.
|
||||||
|
pub fn cosign(&mut self) { todo!("TODO") }
|
||||||
|
|
||||||
|
/// Handle a message for a signing protocol.
|
||||||
|
pub fn handle(&mut self, msg: CoordinatorMessage) -> Vec<ProcessorMessage> {
|
||||||
|
todo!("TODO")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
1
processor/signers/src/transaction/db.rs
Normal file
1
processor/signers/src/transaction/db.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
70
processor/signers/src/transaction/mod.rs
Normal file
70
processor/signers/src/transaction/mod.rs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
use serai_db::{Get, DbTxn, Db};
|
||||||
|
|
||||||
|
use primitives::task::ContinuallyRan;
|
||||||
|
use scanner::ScannerFeed;
|
||||||
|
use scheduler::TransactionsToSign;
|
||||||
|
|
||||||
|
mod db;
|
||||||
|
use db::IndexDb;
|
||||||
|
|
||||||
|
// Fetches transactions to sign and signs them.
|
||||||
|
pub(crate) struct TransactionTask<D: Db, S: ScannerFeed, Sch: Scheduler> {
|
||||||
|
db: D,
|
||||||
|
keys: ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>,
|
||||||
|
attempt_manager:
|
||||||
|
AttemptManager<D, <Sch::SignableTransaction as SignableTransaction>::PreprocessMachine>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: Db, S: ScannerFeed> TransactionTask<D, S> {
|
||||||
|
pub(crate) async fn new(
|
||||||
|
db: D,
|
||||||
|
keys: ThresholdKeys<<Sch::SignableTransaction as SignableTransaction>::Ciphersuite>,
|
||||||
|
) -> Self {
|
||||||
|
Self { db, keys, attempt_manager: AttemptManager::new() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl<D: Db, S: ScannerFeed> ContinuallyRan for TransactionTask<D, S> {
|
||||||
|
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 };
|
||||||
|
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");
|
||||||
|
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 };
|
||||||
|
iterated = true;
|
||||||
|
|
||||||
|
self.attempt_manager.retire(tx);
|
||||||
|
txn.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut txn = self.db.txn();
|
||||||
|
let Some(msg) = TransactionSignMessages::try_recv(&mut txn, self.key) else { break };
|
||||||
|
iterated = true;
|
||||||
|
|
||||||
|
match self.attempt_manager.handle(msg) {
|
||||||
|
Response::Messages(messages) => todo!("TODO"),
|
||||||
|
Response::Signature(signature) => todo!("TODO"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(iterated)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue