mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Make publish_signed_transaction safe for out of order publications
This is a possibility under the new deterministic nonce scheme. While there is a concern of us never creating a transaction with a nonce, blocking everything, we should always create transactions. We'll always publish preprocesses, and while we'll only publish shares if everyone else does, we only allocate for shares once everyone else does.
This commit is contained in:
@@ -5,7 +5,8 @@ use serai_client::{primitives::NetworkId, in_instructions::primitives::SignedBat
|
||||
|
||||
pub use serai_db::*;
|
||||
|
||||
use crate::tributary::TributarySpec;
|
||||
use ::tributary::ReadWrite;
|
||||
use crate::tributary::{TributarySpec, Transaction};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MainDb<D: Db>(PhantomData<D>);
|
||||
@@ -51,6 +52,21 @@ impl<D: Db> MainDb<D> {
|
||||
txn.put(key, existing_bytes);
|
||||
}
|
||||
|
||||
fn signed_transaction_key(nonce: u32) -> Vec<u8> {
|
||||
Self::main_key(b"signed_transaction", nonce.to_le_bytes())
|
||||
}
|
||||
pub fn save_signed_transaction(txn: &mut D::Transaction<'_>, nonce: u32, tx: Transaction) {
|
||||
txn.put(Self::signed_transaction_key(nonce), tx.serialize());
|
||||
}
|
||||
pub fn take_signed_transaction(txn: &mut D::Transaction<'_>, nonce: u32) -> Option<Transaction> {
|
||||
let key = Self::signed_transaction_key(nonce);
|
||||
let res = txn.get(&key).map(|bytes| Transaction::read(&mut bytes.as_slice()).unwrap());
|
||||
if res.is_some() {
|
||||
txn.del(&key);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn first_preprocess_key(id: [u8; 32]) -> Vec<u8> {
|
||||
Self::main_key(b"first_preprocess", id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user