Skip to content

Commit

Permalink
add test enforce_max_block_size
Browse files Browse the repository at this point in the history
This hasn't really be tested yet, but it is how I plan on testing this.
  • Loading branch information
tbro committed May 1, 2024
1 parent a831b45 commit 91a1589
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sequencer/src/block/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction> = vec![];
let mut txs = (0..max_block_size / payload_size)
.map(|_| Transaction::of_size(&mut rng, payload_size))
.collect::<Vec<Transaction>>();

// should panic b/c txs size > max_block_size
txns.push(Transaction::of_size(&mut rng, payload_size));
Payload::<TxTableEntryWord>::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::<TxTableEntryWord>::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::<TxTableEntryWord>::from_txs(txs, max_block_size).unwrap();
}

#[test]
fn basic_correctness() {
check_basic_correctness::<TxTableEntryWord>()
Expand Down
9 changes: 9 additions & 0 deletions sequencer/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ impl Transaction {
(0..len).map(|_| rand::random::<u8>()).collect::<Vec<_>>(),
)
}
#[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::<u8>()).collect::<Vec<_>>(),
)
}
}

impl HotShotTransaction for Transaction {}
Expand Down

0 comments on commit 91a1589

Please sign in to comment.