2023-04-14 00:35:55 +00:00
|
|
|
use std::collections::HashMap;
|
2023-04-12 16:15:38 +00:00
|
|
|
|
|
|
|
use ciphersuite::{Ciphersuite, Ristretto};
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
use crate::{ACCOUNT_MEMPOOL_LIMIT, Signed, TransactionKind, Transaction, verify_transaction};
|
2023-04-12 16:15:38 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) struct Mempool<T: Transaction> {
|
2023-04-12 16:15:38 +00:00
|
|
|
genesis: [u8; 32],
|
|
|
|
txs: HashMap<[u8; 32], T>,
|
|
|
|
next_nonces: HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Transaction> Mempool<T> {
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn new(genesis: [u8; 32]) -> Self {
|
2023-04-12 16:15:38 +00:00
|
|
|
Mempool { genesis, txs: HashMap::new(), next_nonces: HashMap::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if this is a valid, new transaction.
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn add(
|
2023-04-12 16:15:38 +00:00
|
|
|
&mut self,
|
2023-04-12 16:42:23 +00:00
|
|
|
blockchain_next_nonces: &HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
2023-04-14 00:35:55 +00:00
|
|
|
internal: bool,
|
2023-04-12 16:15:38 +00:00
|
|
|
tx: T,
|
|
|
|
) -> bool {
|
|
|
|
match tx.kind() {
|
|
|
|
TransactionKind::Signed(Signed { signer, nonce, .. }) => {
|
2023-04-13 13:47:14 +00:00
|
|
|
// 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);
|
2023-04-12 16:15:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
// 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(&tx, self.genesis, &mut self.next_nonces).is_err() {
|
2023-04-12 16:15:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assert_eq!(self.next_nonces[signer], nonce + 1);
|
|
|
|
|
|
|
|
self.txs.insert(tx.hash(), tx);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:42:23 +00:00
|
|
|
// Returns None if the mempool doesn't have a nonce tracked.
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn next_nonce(&self, signer: &<Ristretto as Ciphersuite>::G) -> Option<u32> {
|
2023-04-12 16:15:38 +00:00
|
|
|
self.next_nonces.get(signer).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get transactions to include in a block.
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn block(
|
2023-04-12 16:15:38 +00:00
|
|
|
&mut self,
|
2023-04-12 16:42:23 +00:00
|
|
|
blockchain_next_nonces: &HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
2023-04-14 00:35:55 +00:00
|
|
|
) -> Vec<T> {
|
|
|
|
let mut res = vec![];
|
2023-04-12 16:15:38 +00:00
|
|
|
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, .. }) => {
|
2023-04-12 16:42:23 +00:00
|
|
|
if blockchain_next_nonces[signer] > *nonce {
|
2023-04-12 16:15:38 +00:00
|
|
|
self.txs.remove(&hash);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("non-signed transaction entered mempool"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since this TX isn't stale, include it
|
2023-04-14 00:35:55 +00:00
|
|
|
res.push(tx.clone());
|
2023-04-12 16:15:38 +00:00
|
|
|
}
|
2023-04-14 00:35:55 +00:00
|
|
|
|
|
|
|
// Sort res by nonce.
|
|
|
|
let nonce = |tx: &T| {
|
|
|
|
if let TransactionKind::Signed(Signed { nonce, .. }) = tx.kind() {
|
|
|
|
*nonce
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
res.sort_by(|a, b| nonce(a).partial_cmp(&nonce(b)).unwrap());
|
|
|
|
|
2023-04-12 16:15:38 +00:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a transaction from the mempool.
|
2023-04-14 00:35:55 +00:00
|
|
|
pub(crate) fn remove(&mut self, tx: &[u8; 32]) {
|
2023-04-12 16:15:38 +00:00
|
|
|
self.txs.remove(tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn txs(&self) -> &HashMap<[u8; 32], T> {
|
|
|
|
&self.txs
|
|
|
|
}
|
|
|
|
}
|