Skip to content

Commit 1f3d5eb

Browse files
committed
test(mempool): test max num inputs on insertion
1 parent 61501a2 commit 1f3d5eb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/models/state/mempool.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,4 +1969,54 @@ mod tests {
19691969
));
19701970
}
19711971
}
1972+
1973+
#[cfg(test)]
1974+
mod restrict_num_inputs {
1975+
use super::*;
1976+
use crate::models::blockchain::transaction::transaction_kernel::transaction_kernel_tests::pseudorandom_transaction_kernel;
1977+
1978+
/// Return an invalid SingleProof-backed transaction with `n` inputs.
1979+
fn tx_with_n_inputs(n: usize) -> Transaction {
1980+
let mut rng = rand::rng();
1981+
let num_outputs = 10;
1982+
let num_public_announcements = 10;
1983+
let kernel = pseudorandom_transaction_kernel(
1984+
rng.random(),
1985+
n,
1986+
num_outputs,
1987+
num_public_announcements,
1988+
);
1989+
Transaction {
1990+
kernel,
1991+
proof: TransactionProof::invalid(),
1992+
}
1993+
}
1994+
1995+
#[test]
1996+
fn cannot_insert_tx_with_too_many_inputs() {
1997+
let network = Network::Main;
1998+
let genesis_block = Block::genesis(network);
1999+
let max_inputs_per_tx = 5;
2000+
let mut mempool = Mempool::new(
2001+
ByteSize::gb(1),
2002+
None,
2003+
Some(max_inputs_per_tx),
2004+
genesis_block.hash(),
2005+
);
2006+
2007+
for i in 0..=max_inputs_per_tx {
2008+
let tx = tx_with_n_inputs(i);
2009+
assert!(mempool.insert(tx, TransactionOrigin::Foreign).is_ok());
2010+
}
2011+
2012+
assert_eq!(max_inputs_per_tx + 1, mempool.len());
2013+
assert!(mempool
2014+
.insert(
2015+
tx_with_n_inputs(max_inputs_per_tx + 1),
2016+
TransactionOrigin::Foreign
2017+
)
2018+
.is_err());
2019+
assert_eq!(max_inputs_per_tx + 1, mempool.len());
2020+
}
2021+
}
19722022
}

0 commit comments

Comments
 (0)