diff --git a/sequencer/src/block/payload.rs b/sequencer/src/block/payload.rs index 4197971ab..73ad3ecc9 100644 --- a/sequencer/src/block/payload.rs +++ b/sequencer/src/block/payload.rs @@ -351,6 +351,31 @@ mod test { const NUM_STORAGE_NODES: usize = 10; + #[test] + fn enforce_max_block_size() { + let mut rng = jf_utils::test_rng(); + let max_block_size = 1000u64; + let payload_size = 10; + + let len = max_block_size; + // let mut txs: Vec = vec![]; + let mut txs = (0..max_block_size / payload_size) + .map(|_| Transaction::of_size(&mut rng, payload_size)) + .collect::>(); + + // should panic b/c txs size > max_block_size + txns.push(Transaction::of_size(&mut rng, payload_size)); + Payload::::from_txs(txs, max_block_size).unwrap(); + + // should panic b/c txs size = max_block_size + txns.pop(Transaction::of_size(&mut rng, payload_size)); + Payload::::from_txs(txs, max_block_size).unwrap(); + + // should succeed b/c txs size < max_block_size + txns.pop(Transaction::of_size(&mut rng, payload_size)); + Payload::::from_txs(txs, max_block_size).unwrap(); + } + #[test] fn basic_correctness() { check_basic_correctness::() diff --git a/sequencer/src/transaction.rs b/sequencer/src/transaction.rs index 801e74884..e9f0b5735 100644 --- a/sequencer/src/transaction.rs +++ b/sequencer/src/transaction.rs @@ -75,6 +75,15 @@ impl Transaction { (0..len).map(|_| rand::random::()).collect::>(), ) } + #[cfg(any(test, feature = "testing"))] + /// Useful for when we want to test size of transaction(s) + pub fn of_size(rng: &mut dyn rand::RngCore, len: usize) -> Self { + use rand::Rng; + Self::new( + NamespaceId(rng.gen_range(0..10)), + (0..len).map(|_| rand::random::()).collect::>(), + ) + } } impl HotShotTransaction for Transaction {}