Add scheduler-primitives

The main benefit is whatever scheduler is in use, we now have a single API to
receive TXs to sign (which is of value to the TX signer crate we'll inevitably
build).
This commit is contained in:
Luke Parker
2024-09-02 16:09:52 -04:00
parent c88ebe985e
commit fadc88d2ad
12 changed files with 173 additions and 21 deletions

View File

@@ -0,0 +1,48 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
use core::marker::PhantomData;
use std::io;
use group::GroupEncoding;
use serai_db::DbTxn;
/// A signable transaction.
pub trait SignableTransaction: 'static + Sized + Send + Sync {
/// Read a `SignableTransaction`.
fn read(reader: &mut impl io::Read) -> io::Result<Self>;
/// Write a `SignableTransaction`.
fn write(&self, writer: &mut impl io::Write) -> io::Result<()>;
}
mod db {
use serai_db::{Get, DbTxn, create_db, db_channel};
db_channel! {
SchedulerPrimitives {
TransactionsToSign: (key: &[u8]) -> Vec<u8>,
}
}
}
/// The transactions to sign, as scheduled by a Scheduler.
pub struct TransactionsToSign<T>(PhantomData<T>);
impl<T: SignableTransaction> TransactionsToSign<T> {
/// Send a transaction to sign.
pub fn send(txn: &mut impl DbTxn, key: &impl GroupEncoding, tx: &T) {
let mut buf = Vec::with_capacity(128);
tx.write(&mut buf).unwrap();
db::TransactionsToSign::send(txn, key.to_bytes().as_ref(), &buf);
}
/// Try to receive a transaction to sign.
pub fn try_recv(txn: &mut impl DbTxn, key: &impl GroupEncoding) -> Option<T> {
let tx = db::TransactionsToSign::try_recv(txn, key.to_bytes().as_ref())?;
let mut tx = tx.as_slice();
let res = T::read(&mut tx).unwrap();
assert!(tx.is_empty());
Some(res)
}
}