mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
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:
25
processor/scheduler/primitives/Cargo.toml
Normal file
25
processor/scheduler/primitives/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "serai-processor-scheduler-primitives"
|
||||
version = "0.1.0"
|
||||
description = "Primitives for schedulers for the Serai processor"
|
||||
license = "AGPL-3.0-only"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/processor/scheduler/primitives"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
keywords = []
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
group = { version = "0.13", 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-db = { path = "../../../common/db" }
|
||||
15
processor/scheduler/primitives/LICENSE
Normal file
15
processor/scheduler/primitives/LICENSE
Normal file
@@ -0,0 +1,15 @@
|
||||
AGPL-3.0-only license
|
||||
|
||||
Copyright (c) 2024 Luke Parker
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License Version 3 as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
3
processor/scheduler/primitives/README.md
Normal file
3
processor/scheduler/primitives/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Scheduler Primitives
|
||||
|
||||
Primitives for schedulers.
|
||||
48
processor/scheduler/primitives/src/lib.rs
Normal file
48
processor/scheduler/primitives/src/lib.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user