Skip to content

Commit 5444758

Browse files
authored
Merge pull request #2167 from AleoHQ/fix/from-bytes-bounds
[TOB] Adds check for possible panics in FromBytes impls
2 parents 6417f78 + ebe96d2 commit 5444758

File tree

18 files changed

+111
-18
lines changed

18 files changed

+111
-18
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console/network/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub trait Network:
109109
const BLOCK_TIME: u16 = 10;
110110
/// The coinbase puzzle degree.
111111
const COINBASE_PUZZLE_DEGREE: u32 = (1 << 13) - 1; // 8,191
112-
/// The maximum number of prover solutions that can be included per block.
113-
const MAX_PROVER_SOLUTIONS: usize = 1 << 8; // 256 prover solutions
112+
/// The maximum number of solutions that can be included per block.
113+
const MAX_SOLUTIONS: usize = 1 << 8; // 256 solutions
114114
/// The number of blocks per epoch.
115115
const NUM_BLOCKS_PER_EPOCH: u32 = 3600 / Self::BLOCK_TIME as u32; // 360 blocks == ~1 hour
116116

ledger/block/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ package = "snarkvm-ledger-committee"
100100
path = "../../ledger/committee"
101101
features = [ "test-helpers" ]
102102

103+
[dev-dependencies.ledger-narwhal-batch-header]
104+
package = "snarkvm-ledger-narwhal-batch-header"
105+
path = "../narwhal/batch-header"
106+
103107
[dev-dependencies.ledger-query]
104108
package = "snarkvm-ledger-query"
105109
path = "../query"

ledger/block/src/transactions/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,18 @@ pub mod test_helpers {
339339
crate::test_helpers::sample_genesis_block(rng).transactions().clone()
340340
}
341341
}
342+
343+
#[cfg(test)]
344+
mod tests {
345+
use super::*;
346+
347+
type CurrentNetwork = console::network::Testnet3;
348+
349+
#[test]
350+
fn test_max_transactions() {
351+
assert_eq!(
352+
Transactions::<CurrentNetwork>::MAX_TRANSACTIONS,
353+
ledger_narwhal_batch_header::BatchHeader::<CurrentNetwork>::MAX_TRANSACTIONS
354+
);
355+
}
356+
}

ledger/block/src/verify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ impl<N: Network> Block<N> {
266266
Some(coinbase) => {
267267
// Ensure the number of solutions is within the allowed range.
268268
ensure!(
269-
coinbase.len() <= N::MAX_PROVER_SOLUTIONS,
269+
coinbase.len() <= N::MAX_SOLUTIONS,
270270
"Block {height} contains too many prover solutions (found '{}', expected '{}')",
271271
coinbase.len(),
272-
N::MAX_PROVER_SOLUTIONS
272+
N::MAX_SOLUTIONS
273273
);
274274
// Ensure the solutions are not accepted after the block height at year 10.
275275
if height > block_height_at_year(N::BLOCK_TIME, 10) {

ledger/coinbase/benches/coinbase_puzzle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn coinbase_puzzle_verify(c: &mut Criterion) {
9595
let puzzle = CoinbasePuzzleInst::trim(&universal_srs, config).unwrap();
9696
let epoch_challenge = sample_epoch_challenge(degree, rng);
9797

98-
for batch_size in [10, 100, <Testnet3 as Network>::MAX_PROVER_SOLUTIONS] {
98+
for batch_size in [10, 100, <Testnet3 as Network>::MAX_SOLUTIONS] {
9999
let solutions = (0..batch_size)
100100
.map(|_| {
101101
let (address, nonce) = sample_address_and_nonce(rng);

ledger/coinbase/src/helpers/coinbase_solution/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ impl<N: Network> CoinbaseSolution<N> {
3333
// Ensure the solutions are not empty.
3434
ensure!(!solutions.is_empty(), "There are no solutions to verify for the coinbase puzzle");
3535
// Ensure the number of partial solutions does not exceed `MAX_PROVER_SOLUTIONS`.
36-
if solutions.len() > N::MAX_PROVER_SOLUTIONS {
36+
if solutions.len() > N::MAX_SOLUTIONS {
3737
bail!(
3838
"The solutions exceed the allowed number of partial solutions. ({} > {})",
3939
solutions.len(),
40-
N::MAX_PROVER_SOLUTIONS
40+
N::MAX_SOLUTIONS
4141
);
4242
}
4343
// Ensure the puzzle commitments are unique.

ledger/coinbase/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ impl<N: Network> CoinbasePuzzle<N> {
171171
ensure!(!solutions.is_empty(), "There are no solutions to verify for the coinbase puzzle");
172172

173173
// Ensure the number of partial solutions does not exceed `MAX_PROVER_SOLUTIONS`.
174-
if solutions.len() > N::MAX_PROVER_SOLUTIONS {
174+
if solutions.len() > N::MAX_SOLUTIONS {
175175
bail!(
176176
"The solutions exceed the allowed number of partial solutions. ({} > {})",
177177
solutions.len(),
178-
N::MAX_PROVER_SOLUTIONS
178+
N::MAX_SOLUTIONS
179179
);
180180
}
181181

ledger/coinbase/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn test_profiler() -> Result<()> {
133133
// Generate proof inputs
134134
let epoch_challenge = EpochChallenge::new(rng.next_u32(), Default::default(), degree).unwrap();
135135

136-
for batch_size in [10, 100, <Testnet3 as Network>::MAX_PROVER_SOLUTIONS] {
136+
for batch_size in [10, 100, <Testnet3 as Network>::MAX_SOLUTIONS] {
137137
// Generate the solutions.
138138
let solutions = (0..batch_size)
139139
.map(|_| {

ledger/committee/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ version = "1"
8282
[dev-dependencies.snarkvm-ledger-committee]
8383
path = "."
8484
features = [ "prop-tests" ]
85+
86+
[dev-dependencies.ledger-narwhal-batch-header]
87+
package = "snarkvm-ledger-narwhal-batch-header"
88+
path = "../narwhal/batch-header"

0 commit comments

Comments
 (0)