mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 13:39:25 +00:00
49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
|
|
#![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)
|
||
|
|
}
|
||
|
|
}
|