Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
refactor: add BlockWeights for the future bouncer (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Feb 21, 2024
1 parent 696d0b9 commit e2de5ec
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
49 changes: 49 additions & 0 deletions crates/blockifier/src/bouncer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use serde::Deserialize;

#[cfg(test)]
#[path = "bouncer_test.rs"]
mod test;

macro_rules! impl_checked_sub {
($($field:ident),+) => {
pub fn checked_sub(self: Self, other: Self) -> Option<Self> {
Some(
Self {
$(
$field: self.$field.checked_sub(other.$field)?,
)+
}
)
}
};
}

#[derive(Clone, Copy, Debug, Default, derive_more::Sub, Deserialize, PartialEq)]
/// Represents the execution resources counted throughout block creation.
pub struct BouncerWeights {
gas: u64,
n_steps: u64,
message_segment_length: u64,
state_diff_size: u64,
builtin_count: BuiltinCount,
}

impl BouncerWeights {
impl_checked_sub!(gas, n_steps, message_segment_length, state_diff_size, builtin_count);
}

#[derive(Clone, Copy, Debug, Default, derive_more::Sub, Deserialize, PartialEq)]
pub struct BuiltinCount {
bitwise: u64,
ecdsa: u64,
ec_op: u64,
keccak: u64,
output: u64,
pedersen: u64,
poseidon: u64,
range_check: u64,
}

impl BuiltinCount {
impl_checked_sub!(bitwise, ecdsa, ec_op, keccak, output, pedersen, poseidon, range_check);
}
64 changes: 64 additions & 0 deletions crates/blockifier/src/bouncer_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::ops::Sub;

use crate::bouncer::{BouncerWeights, BuiltinCount};

#[test]
fn test_block_weights_sub_checked() {
let max_bouncer_weights = BouncerWeights {
gas: 10,
n_steps: 10,
message_segment_length: 10,
state_diff_size: 10,
builtin_count: BuiltinCount {
bitwise: 10,
ecdsa: 10,
ec_op: 10,
keccak: 10,
output: 10,
pedersen: 10,
poseidon: 10,
range_check: 10,
},
};

let bouncer_weights = BouncerWeights {
gas: 7,
n_steps: 0,
message_segment_length: 10,
state_diff_size: 7,
builtin_count: BuiltinCount {
bitwise: 6,
ecdsa: 7,
ec_op: 7,
keccak: 8,
output: 7,
pedersen: 7,
poseidon: 9,
range_check: 10,
},
};

let result = max_bouncer_weights.checked_sub(bouncer_weights).unwrap();
let difference_bouncer_weights = max_bouncer_weights.sub(bouncer_weights);
assert_eq!(result, difference_bouncer_weights);

let bouncer_weights_exceeds_max = BouncerWeights {
gas: 5,
n_steps: 5,
message_segment_length: 5,
state_diff_size: 5,
builtin_count: BuiltinCount {
bitwise: 11,
ecdsa: 5,
ec_op: 5,
keccak: 5,
output: 5,
pedersen: 5,
poseidon: 5,
range_check: 5,
},
};

let result = max_bouncer_weights.checked_sub(bouncer_weights_exceeds_max);
assert!(result.is_none());
}
1 change: 1 addition & 0 deletions crates/blockifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

pub mod abi;
pub mod blockifier;
pub mod bouncer;
pub mod context;
pub mod execution;
pub mod fee;
Expand Down

0 comments on commit e2de5ec

Please sign in to comment.