Add DoS limits to tributary and require provided transactions be ordered

This commit is contained in:
Luke Parker
2023-04-13 20:35:55 -04:00
parent 8b1bce6abd
commit 72dd665ebf
13 changed files with 262 additions and 294 deletions

View File

@@ -1,33 +1,32 @@
use std::collections::HashMap;
use std::collections::VecDeque;
use crate::{TransactionKind, Transaction};
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ProvidedTransactions<T: Transaction> {
pub(crate) transactions: HashMap<[u8; 32], T>,
pub(crate) transactions: VecDeque<T>,
}
impl<T: Transaction> Default for ProvidedTransactions<T> {
fn default() -> Self {
ProvidedTransactions { transactions: HashMap::new() }
ProvidedTransactions { transactions: VecDeque::new() }
}
}
impl<T: Transaction> ProvidedTransactions<T> {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
ProvidedTransactions::default()
}
/// Provide a transaction for inclusion in a block.
pub fn provide(&mut self, tx: T) {
pub(crate) fn provide(&mut self, tx: T) {
// TODO: Make an error out of this
assert_eq!(tx.kind(), TransactionKind::Provided, "provided a non-provided transaction");
self.transactions.insert(tx.hash(), tx);
self.transactions.push_back(tx);
}
/// Withdraw a transaction, no longer proposing it or voting for its validity.
///
/// Returns true if the transaction was withdrawn and false otherwise.
pub fn withdraw(&mut self, tx: [u8; 32]) -> bool {
self.transactions.remove(&tx).is_some()
/// Complete a provided transaction, no longer proposing it nor voting for its validity.
pub(crate) fn complete(&mut self, tx: [u8; 32]) {
assert_eq!(self.transactions.pop_front().unwrap().hash(), tx);
}
}