mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 12:49:23 +00:00
Slash malevolent validators (#294)
* add slash tx * ignore unsigned tx replays * verify that provided evidence is valid * fix clippy + fmt * move application tx handling to another module * partially handle the tendermint txs * fix pr comments * support unsigned app txs * add slash target to the votes * enforce provided, unsigned, signed tx ordering within a block * bug fixes * add unit test for tendermint txs * bug fixes * update tests for tendermint txs * add tx ordering test * tidy up tx ordering test * cargo +nightly fmt * Misc fixes from rebasing * Finish resolving clippy * Remove sha3 from tendermint-machine * Resolve a DoS in SlashEvidence's read Also moves Evidence from Vec<Message> to (Message, Option<Message>). That should meet all requirements while being a bit safer. * Make lazy_static a dev-depend for tributary * Various small tweaks One use of sort was inefficient, sorting unsigned || signed when unsigned was already properly sorted. Given how the unsigned TXs were given a nonce of 0, an unstable sort may swap places with an unsigned TX and a signed TX with a nonce of 0 (leading to a faulty block). The extra protection added here sorts signed, then concats. * Fix Tributary tests I broke, start review on tendermint/tx.rs * Finish reviewing everything outside tests and empty_signature * Remove empty_signature empty_signature led to corrupted local state histories. Unfortunately, the API is only sane with a signature. We now use the actual signature, which risks creating a signature over a malicious message if we have ever have an invariant producing malicious messages. Prior, we only signed the message after the local machine confirmed it was okay per the local view of consensus. This is tolerated/preferred over a corrupt state history since production of such messages is already an invariant. TODOs are added to make handling of this theoretical invariant further robust. * Remove async_sequential for tokio::test There was no competition for resources forcing them to be run sequentially. * Modify block order test to be statistically significant without multiple runs * Clean tests --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
@@ -4,18 +4,25 @@ use ciphersuite::{Ciphersuite, Ristretto};
|
||||
|
||||
use serai_db::{DbTxn, Db};
|
||||
|
||||
use crate::{ACCOUNT_MEMPOOL_LIMIT, Signed, TransactionKind, Transaction, verify_transaction};
|
||||
use tendermint::ext::{Network, Commit};
|
||||
|
||||
use crate::{
|
||||
ACCOUNT_MEMPOOL_LIMIT, ReadWrite,
|
||||
transaction::{Signed, TransactionKind, Transaction as TransactionTrait, verify_transaction},
|
||||
tendermint::tx::verify_tendermint_tx,
|
||||
Transaction,
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub(crate) struct Mempool<D: Db, T: Transaction> {
|
||||
pub(crate) struct Mempool<D: Db, T: TransactionTrait> {
|
||||
db: D,
|
||||
genesis: [u8; 32],
|
||||
|
||||
txs: HashMap<[u8; 32], T>,
|
||||
txs: HashMap<[u8; 32], Transaction<T>>,
|
||||
next_nonces: HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
||||
}
|
||||
|
||||
impl<D: Db, T: Transaction> Mempool<D, T> {
|
||||
impl<D: Db, T: TransactionTrait> Mempool<D, T> {
|
||||
fn transaction_key(&self, hash: &[u8]) -> Vec<u8> {
|
||||
D::key(b"tributary_mempool", b"transaction", [self.genesis.as_ref(), hash].concat())
|
||||
}
|
||||
@@ -23,90 +30,141 @@ impl<D: Db, T: Transaction> Mempool<D, T> {
|
||||
D::key(b"tributary_mempool", b"current", self.genesis)
|
||||
}
|
||||
|
||||
// save given tx to the mempool db
|
||||
fn save_tx(&mut self, tx: Transaction<T>) {
|
||||
let tx_hash = tx.hash();
|
||||
let transaction_key = self.transaction_key(&tx_hash);
|
||||
let current_mempool_key = self.current_mempool_key();
|
||||
let mut current_mempool = self.db.get(¤t_mempool_key).unwrap_or(vec![]);
|
||||
|
||||
let mut txn = self.db.txn();
|
||||
txn.put(transaction_key, tx.serialize());
|
||||
current_mempool.extend(tx_hash);
|
||||
txn.put(current_mempool_key, current_mempool);
|
||||
txn.commit();
|
||||
|
||||
self.txs.insert(tx_hash, tx);
|
||||
}
|
||||
|
||||
fn unsigned_already_exist(
|
||||
&self,
|
||||
hash: [u8; 32],
|
||||
unsigned_in_chain: impl Fn([u8; 32]) -> bool,
|
||||
) -> bool {
|
||||
unsigned_in_chain(hash) || self.txs.contains_key(&hash)
|
||||
}
|
||||
|
||||
pub(crate) fn new(db: D, genesis: [u8; 32]) -> Self {
|
||||
let mut res = Mempool { db, genesis, txs: HashMap::new(), next_nonces: HashMap::new() };
|
||||
|
||||
let current_mempool = res.db.get(res.current_mempool_key()).unwrap_or(vec![]);
|
||||
let mut hash = [0; 32];
|
||||
let mut i = 0;
|
||||
while i < current_mempool.len() {
|
||||
hash.copy_from_slice(¤t_mempool[i .. (i + 32)]);
|
||||
let tx =
|
||||
T::read::<&[u8]>(&mut res.db.get(res.transaction_key(&hash)).unwrap().as_ref()).unwrap();
|
||||
|
||||
match tx.kind() {
|
||||
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
||||
if let Some(prev) = res.next_nonces.insert(*signer, nonce + 1) {
|
||||
// These mempool additions should've been ordered
|
||||
assert!(prev < *nonce);
|
||||
for hash in current_mempool.chunks(32) {
|
||||
let hash: [u8; 32] = hash.try_into().unwrap();
|
||||
let tx: Transaction<T> =
|
||||
Transaction::read::<&[u8]>(&mut res.db.get(res.transaction_key(&hash)).unwrap().as_ref())
|
||||
.unwrap();
|
||||
debug_assert_eq!(tx.hash(), hash);
|
||||
|
||||
match tx {
|
||||
Transaction::Tendermint(tx) => {
|
||||
res.txs.insert(hash, Transaction::Tendermint(tx));
|
||||
}
|
||||
Transaction::Application(tx) => {
|
||||
match tx.kind() {
|
||||
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
||||
if let Some(prev) = res.next_nonces.insert(*signer, nonce + 1) {
|
||||
// These mempool additions should've been ordered
|
||||
debug_assert!(prev < *nonce);
|
||||
}
|
||||
res.txs.insert(hash, Transaction::Application(tx));
|
||||
}
|
||||
TransactionKind::Unsigned => {
|
||||
res.txs.insert(hash, Transaction::Application(tx));
|
||||
}
|
||||
_ => panic!("mempool database had a provided transaction"),
|
||||
}
|
||||
}
|
||||
_ => panic!("mempool database had a non-signed transaction"),
|
||||
}
|
||||
|
||||
debug_assert_eq!(tx.hash(), hash);
|
||||
res.txs.insert(hash, tx);
|
||||
i += 32;
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
/// Returns true if this is a valid, new transaction.
|
||||
pub(crate) fn add(
|
||||
pub(crate) fn add<N: Network>(
|
||||
&mut self,
|
||||
blockchain_next_nonces: &HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
||||
internal: bool,
|
||||
tx: T,
|
||||
tx: Transaction<T>,
|
||||
schema: N::SignatureScheme,
|
||||
unsigned_in_chain: impl Fn([u8; 32]) -> bool,
|
||||
commit: impl Fn(u32) -> Option<Commit<N::SignatureScheme>>,
|
||||
) -> bool {
|
||||
match tx.kind() {
|
||||
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
||||
// Get the nonce from the blockchain
|
||||
let Some(blockchain_next_nonce) = blockchain_next_nonces.get(signer).cloned() else {
|
||||
// Not a participant
|
||||
return false;
|
||||
};
|
||||
match &tx {
|
||||
Transaction::Tendermint(tendermint_tx) => {
|
||||
// All Tendermint transactions should be unsigned
|
||||
assert_eq!(TransactionKind::Unsigned, tendermint_tx.kind());
|
||||
|
||||
// If the blockchain's nonce is greater than the mempool's, use it
|
||||
// Default to true so if the mempool hasn't tracked this nonce yet, it'll be inserted
|
||||
let mut blockchain_is_greater = true;
|
||||
if let Some(mempool_next_nonce) = self.next_nonces.get(signer) {
|
||||
blockchain_is_greater = blockchain_next_nonce > *mempool_next_nonce;
|
||||
}
|
||||
|
||||
if blockchain_is_greater {
|
||||
self.next_nonces.insert(*signer, blockchain_next_nonce);
|
||||
}
|
||||
|
||||
// If we have too many transactions from this sender, don't add this yet UNLESS we are
|
||||
// this sender
|
||||
if !internal && (nonce >= &(blockchain_next_nonce + ACCOUNT_MEMPOOL_LIMIT)) {
|
||||
// check we have the tx in the pool/chain
|
||||
if self.unsigned_already_exist(tx.hash(), unsigned_in_chain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if verify_transaction(&tx, self.genesis, &mut self.next_nonces).is_err() {
|
||||
// verify the tx
|
||||
if verify_tendermint_tx::<N>(tendermint_tx, self.genesis, schema, commit).is_err() {
|
||||
return false;
|
||||
}
|
||||
assert_eq!(self.next_nonces[signer], nonce + 1);
|
||||
|
||||
let tx_hash = tx.hash();
|
||||
|
||||
let transaction_key = self.transaction_key(&tx_hash);
|
||||
let current_mempool_key = self.current_mempool_key();
|
||||
let mut current_mempool = self.db.get(¤t_mempool_key).unwrap_or(vec![]);
|
||||
|
||||
let mut txn = self.db.txn();
|
||||
txn.put(transaction_key, tx.serialize());
|
||||
current_mempool.extend(tx_hash);
|
||||
txn.put(current_mempool_key, current_mempool);
|
||||
txn.commit();
|
||||
|
||||
self.txs.insert(tx_hash, tx);
|
||||
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
Transaction::Application(app_tx) => {
|
||||
match app_tx.kind() {
|
||||
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
||||
// Get the nonce from the blockchain
|
||||
let Some(blockchain_next_nonce) = blockchain_next_nonces.get(signer).cloned() else {
|
||||
// Not a participant
|
||||
return false;
|
||||
};
|
||||
|
||||
// If the blockchain's nonce is greater than the mempool's, use it
|
||||
// Default to true so if the mempool hasn't tracked this nonce yet, it'll be inserted
|
||||
let mut blockchain_is_greater = true;
|
||||
if let Some(mempool_next_nonce) = self.next_nonces.get(signer) {
|
||||
blockchain_is_greater = blockchain_next_nonce > *mempool_next_nonce;
|
||||
}
|
||||
|
||||
if blockchain_is_greater {
|
||||
self.next_nonces.insert(*signer, blockchain_next_nonce);
|
||||
}
|
||||
|
||||
// If we have too many transactions from this sender, don't add this yet UNLESS we are
|
||||
// this sender
|
||||
if !internal && (nonce >= &(blockchain_next_nonce + ACCOUNT_MEMPOOL_LIMIT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if verify_transaction(app_tx, self.genesis, &mut self.next_nonces).is_err() {
|
||||
return false;
|
||||
}
|
||||
debug_assert_eq!(self.next_nonces[signer], nonce + 1);
|
||||
}
|
||||
TransactionKind::Unsigned => {
|
||||
// check we have the tx in the pool/chain
|
||||
if self.unsigned_already_exist(tx.hash(), unsigned_in_chain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if app_tx.verify().is_err() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
TransactionKind::Provided(_) => return false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save the TX to the pool
|
||||
self.save_tx(tx);
|
||||
true
|
||||
}
|
||||
|
||||
// Returns None if the mempool doesn't have a nonce tracked.
|
||||
@@ -118,10 +176,13 @@ impl<D: Db, T: Transaction> Mempool<D, T> {
|
||||
pub(crate) fn block(
|
||||
&mut self,
|
||||
blockchain_next_nonces: &HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
||||
) -> Vec<T> {
|
||||
let mut res = vec![];
|
||||
unsigned_in_chain: impl Fn([u8; 32]) -> bool,
|
||||
) -> Vec<Transaction<T>> {
|
||||
let mut unsigned = vec![];
|
||||
let mut signed = vec![];
|
||||
for hash in self.txs.keys().cloned().collect::<Vec<_>>() {
|
||||
let tx = &self.txs[&hash];
|
||||
|
||||
// Verify this hasn't gone stale
|
||||
match tx.kind() {
|
||||
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
||||
@@ -129,25 +190,35 @@ impl<D: Db, T: Transaction> Mempool<D, T> {
|
||||
self.remove(&hash);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => panic!("non-signed transaction entered mempool"),
|
||||
}
|
||||
|
||||
// Since this TX isn't stale, include it
|
||||
res.push(tx.clone());
|
||||
// Since this TX isn't stale, include it
|
||||
signed.push(tx.clone());
|
||||
}
|
||||
TransactionKind::Unsigned => {
|
||||
if unsigned_in_chain(hash) {
|
||||
self.remove(&hash);
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned.push(tx.clone());
|
||||
}
|
||||
_ => panic!("provided transaction entered mempool"),
|
||||
}
|
||||
}
|
||||
|
||||
// Sort res by nonce.
|
||||
let nonce = |tx: &T| {
|
||||
// Sort signed by nonce
|
||||
let nonce = |tx: &Transaction<T>| {
|
||||
if let TransactionKind::Signed(Signed { nonce, .. }) = tx.kind() {
|
||||
*nonce
|
||||
} else {
|
||||
0
|
||||
unreachable!()
|
||||
}
|
||||
};
|
||||
res.sort_by(|a, b| nonce(a).partial_cmp(&nonce(b)).unwrap());
|
||||
signed.sort_by(|a, b| nonce(a).partial_cmp(&nonce(b)).unwrap());
|
||||
|
||||
res
|
||||
// unsigned first, then signed.
|
||||
unsigned.append(&mut signed);
|
||||
unsigned
|
||||
}
|
||||
|
||||
/// Remove a transaction from the mempool.
|
||||
@@ -177,7 +248,7 @@ impl<D: Db, T: Transaction> Mempool<D, T> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn txs(&self) -> &HashMap<[u8; 32], T> {
|
||||
pub(crate) fn txs(&self) -> &HashMap<[u8; 32], Transaction<T>> {
|
||||
&self.txs
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user