-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(blockifier): cap max_validate_gas and max_execute_gas #2397
Conversation
2aca2fa
to
347c242
Compare
064540d
to
ab923d0
Compare
49e97f3
to
7508d01
Compare
d65e49d
to
14b5650
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2397 +/- ##
===========================================
+ Coverage 40.10% 75.85% +35.74%
===========================================
Files 26 109 +83
Lines 1895 14440 +12545
Branches 1895 14440 +12545
===========================================
+ Hits 760 10953 +10193
- Misses 1100 3004 +1904
- Partials 35 483 +448 ☔ View full report in Codecov by Sentry. |
14b5650
to
a8e25e3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 4 files at r3, 4 of 4 files at r4, 1 of 3 files at r6, 6 of 6 files at r7, all commit messages.
Reviewable status: all files reviewed, 19 unresolved discussions (waiting on @aner-starkware)
a discussion (no related file):
please add some unit tests, where you parametrize
- for different tx types
- for different resource bounds
- for different account / deployed contract cairo versions
and make sure the initial gas is as expected:
- if user bound is huge, both entry points should be the max allowed for the execution mode (validate/execute). note that
__execute__
can be run in validate execution mode (in deploy account txs) - if user bound is over the first entry point max but not over the second, first entry point should have the max initial gas and second should not
- if user bound is below both entry point maxes, neither entry point should have max bounds
crates/blockifier/src/context.rs
line 69 at r4 (raw file):
impl GasCounter { pub(crate) fn new(initial_gas: u64) -> Self {
for all new types/functions - be strict with the type you use.
we want to eventually migrate all representations of an amount of gas to GasAmount
Suggestion:
initial_gas: GasAmount
crates/blockifier/src/context.rs
line 73 at r4 (raw file):
} pub(crate) fn spend(&mut self, amount: GasAmount) {
is this used anywhere other than in subtract_used_gas?
if not, please inline the logic and remove this function
Code quote:
pub(crate) fn spend
crates/blockifier/src/context.rs
line 79 at r4 (raw file):
.checked_sub(amount) .expect("Overuse of gas; should have been caught earlier"); }
consider forcing the caller to decide whether or not to panic; non-blocking
Suggestion:
pub(crate) fn spend(&mut self, amount: GasAmount) -> Result<()> {
self.spent_gas = self.spent_gas.checked_add(amount).expect("Gas overflow");
self.remaining_gas = self
.remaining_gas
.checked_sub(amount)?;
}
crates/blockifier/src/context.rs
line 81 at r4 (raw file):
} pub(crate) fn cap_usage(&self, amount: GasAmount) -> u64 {
- docstring (unclear to me what exactly this does, just from the function name)
- use stricter type
Suggestion:
pub(crate) fn cap_usage(&self, amount: GasAmount) -> GasAmount {
crates/blockifier/src/transaction/account_transactions_test.rs
line 1700 at r7 (raw file):
.min( block_context.versioned_constants.default_initial_gas_amount() - GasAmount(validate_gas_consumed),
this should depend on the user bound, not the default initial gas.
Suggestion:
user_gas_bound
- GasAmount(validate_gas_consumed),
crates/blockifier/src/versioned_constants.rs
line 262 at r4 (raw file):
pub fn max_execution_sierra_gas(&self) -> GasAmount { self.execute_max_sierra_gas }
are these getters necessary? aren't the fields public?
Code quote:
/// Returns the maximum gas amount for validation.
pub fn max_validation_sierra_gas(&self) -> GasAmount {
self.validate_max_sierra_gas
}
/// Returns the maximum gas amount for execute.
pub fn max_execution_sierra_gas(&self) -> GasAmount {
self.execute_max_sierra_gas
}
crates/blockifier/src/transaction/transactions_test.rs
line 243 at r7 (raw file):
TrackedResource::CairoSteps => { default_initial_gas_amount().min(VERSIONED_CONSTANTS.max_validation_sierra_gas()).0 }
not sure there should be a difference between cairo0 initial gas and cairo1-step-mode initial gas; discussing offline
Code quote:
CairoVersion::Cairo0 => default_initial_gas_cost(),
CairoVersion::Cairo1 => match tracked_resource {
TrackedResource::CairoSteps => {
default_initial_gas_amount().min(VERSIONED_CONSTANTS.max_validation_sierra_gas()).0
}
crates/blockifier/src/transaction/transactions_test.rs
line 529 at r7 (raw file):
if account_cairo_version == CairoVersion::Cairo0 { expected_arguments.inner_call_initial_gas = VERSIONED_CONSTANTS.default_initial_gas_cost(); }
how is this working?
currently, inner calls of cairo0 (step-mode -> step-mode) should have default_initial_gas - gas_consumed
, why do inner calls also have the default initial gas?
maybe because, if the account version is cairo0, then tracked_resource
is steps, so you enter the if
below and subtract the execute gas consumed; please combine the two if
s for clarity (put this if
inside the below if
)
Code quote:
if account_cairo_version == CairoVersion::Cairo0 {
expected_arguments.inner_call_initial_gas = VERSIONED_CONSTANTS.default_initial_gas_cost();
}
crates/blockifier/src/transaction/transactions_test.rs
line 536 at r7 (raw file):
} else { expected_initial_execution_gas }, // expected_validated_call.initial_gas - expected_arguments.validate_gas_consumed,
delete
Code quote:
// expected_validated_call.initial_gas - expected_arguments.validate_gas_consumed,
crates/blockifier/src/transaction/transactions_test.rs
line 547 at r7 (raw file):
); expected_arguments.inner_call_initial_gas -= expected_arguments.execute_gas_consumed; // }
delete dead code
Suggestion:
expected_arguments.inner_call_initial_gas -= expected_arguments.validate_gas_consumed;
expected_arguments.inner_call_initial_gas = min(
expected_arguments.inner_call_initial_gas,
VERSIONED_CONSTANTS.max_execution_sierra_gas().0,
);
expected_arguments.inner_call_initial_gas -= expected_arguments.execute_gas_consumed;
crates/blockifier/src/transaction/transactions_test.rs
line 560 at r7 (raw file):
initial_gas: if account_cairo_version == CairoVersion::Cairo0 { VERSIONED_CONSTANTS.default_initial_gas_cost() } else {
similar to the Q above: why would the inner call not have some gas deducted? if the parent call is step mode, the inner call initial gas currently subtracts something (unless someone already changed this...?)
Code quote:
initial_gas: if account_cairo_version == CairoVersion::Cairo0 {
VERSIONED_CONSTANTS.default_initial_gas_cost()
} else {
crates/blockifier/src/transaction/transactions_test.rs
line 1813 at r7 (raw file):
let expected_execute_initial_gas = user_initial_gas .unwrap_or(default_initial_gas_amount()) .min(VERSIONED_CONSTANTS.max_execution_sierra_gas());
this is wrong, I think: in DEPLOY_ACCOUNT txs, the execute entry point is run in VALIDATE mode (i.e. initial gas should be capped by validate limit).
Code quote:
.min(VERSIONED_CONSTANTS.max_execution_sierra_gas());
crates/blockifier/src/transaction/transactions_test.rs
line 167 at r4 (raw file):
fn default_initial_gas_amount() -> GasAmount { VERSIONED_CONSTANTS.default_initial_gas_amount() }
this fixture should depend on the VC ==> depend on the block context.
I see other tests here use the static VC but if you have access to a block context object, you should use it
Code quote:
#[fixture]
fn default_initial_gas_amount() -> GasAmount {
VERSIONED_CONSTANTS.default_initial_gas_amount()
}
crates/blockifier/src/transaction/transactions_test.rs
line 191 at r4 (raw file):
ValidResourceBounds::AllResources(bounds) => Some(bounds.l2_gas.max_amount), } }
Suggestion:
fn user_initial_gas_from_bounds(bounds: ValidResourceBounds) -> GasAmount {
match bounds {
ValidResourceBounds::L1Gas(_) => default_initial_gas_amount(),
ValidResourceBounds::AllResources(bounds) => bounds.l2_gas.max_amount,
}
}
crates/blockifier/src/transaction/transactions_test.rs
line 504 at r4 (raw file):
account_cairo_version, tracked_resource, initial_gas.min(Some(VERSIONED_CONSTANTS.max_validation_sierra_gas())),
use the versioned constants from the block context
Code quote:
VERSIONED_CONSTANTS
crates/blockifier/src/transaction/transactions_test.rs
line 511 at r4 (raw file):
let expected_validated_call = expected_validate_call_info.as_ref().unwrap().call.clone(); let expected_initial_execution_gas = VERSIONED_CONSTANTS
use the versioned constants from the block context
Code quote:
VERSIONED_CONSTANTS
crates/blockifier/src/transaction/transactions_test.rs
line 528 at r4 (raw file):
expected_arguments.inner_call_initial_gas = min( expected_arguments.inner_call_initial_gas, VERSIONED_CONSTANTS.max_execution_sierra_gas().0,
use the versioned constants from the block context
Code quote:
VERSIONED_CONSTANTS.default_initial_gas_cost();
} else {
expected_arguments.inner_call_initial_gas -= expected_arguments.validate_gas_consumed;
expected_arguments.inner_call_initial_gas = min(
expected_arguments.inner_call_initial_gas,
VERSIONED_CONSTANTS.max_execution_sierra_gas().0,
crates/blockifier/src/transaction/transactions_test.rs
line 531 at r4 (raw file):
); expected_arguments.inner_call_initial_gas -= expected_arguments.execute_gas_consumed; }
if we start with cairo steps, then the outer and all inner calls should all have the default initial gas.
you may need to wait for yoni's work to be merged before this is true...
Code quote:
expected_arguments.inner_call_initial_gas -= expected_arguments.validate_gas_consumed;
expected_arguments.inner_call_initial_gas = min(
expected_arguments.inner_call_initial_gas,
VERSIONED_CONSTANTS.max_execution_sierra_gas().0,
);
expected_arguments.inner_call_initial_gas -= expected_arguments.execute_gas_consumed;
}
crates/blockifier/src/transaction/account_transaction.rs
line 371 at r4 (raw file):
} None => Ok(None), }
does this work?
Suggestion:
)?.map(|call_info| {
// TODO(Aner): Update the gas counter.
remaining_gas.subtract_used_gas(&call_info);
call_info
})
crates/blockifier/src/transaction/account_transaction.rs
line 511 at r4 (raw file):
Transaction::Invoke(tx) => tx.run_execute(state, context, remaining_execution_gas), }?; match call_info {
see above (does map
work?)
Code quote:
}?;
match call_info {
Previously, dorimedini-starkware wrote…
Let's talk offline about this point - I think it contradicts what you previously said. |
a8e25e3
to
424a056
Compare
7363746
to
ab670f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 1 of 9 files reviewed, 19 unresolved discussions (waiting on @dorimedini-starkware)
crates/blockifier/src/context.rs
line 69 at r4 (raw file):
Previously, dorimedini-starkware wrote…
for all new types/functions - be strict with the type you use.
we want to eventually migrate all representations of an amount of gas to GasAmount
Done.
crates/blockifier/src/context.rs
line 73 at r4 (raw file):
Previously, dorimedini-starkware wrote…
is this used anywhere other than in subtract_used_gas?
if not, please inline the logic and remove this function
I removed the pub(crate)
- I disagree here; it's better to separate this logic.
crates/blockifier/src/context.rs
line 79 at r4 (raw file):
Previously, dorimedini-starkware wrote…
consider forcing the caller to decide whether or not to panic; non-blocking
There's an issue here—IIUC, we should never panic!
, so panicking makes sense IMO.
crates/blockifier/src/context.rs
line 81 at r4 (raw file):
Previously, dorimedini-starkware wrote…
- docstring (unclear to me what exactly this does, just from the function name)
- use stricter type
Done.
crates/blockifier/src/transaction/account_transaction.rs
line 371 at r4 (raw file):
Previously, dorimedini-starkware wrote…
does this work?
Done.
crates/blockifier/src/transaction/account_transaction.rs
line 511 at r4 (raw file):
Previously, dorimedini-starkware wrote…
see above (does
map
work?)
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 7 of 8 files at r9, 1 of 1 files at r10, all commit messages.
Reviewable status: all files reviewed, 15 unresolved discussions (waiting on @aner-starkware)
crates/blockifier/src/transaction/account_transaction.rs
line 360 at r10 (raw file):
Ok(self .validate_tx(state, tx_context, remaining_validation_gas, limit_steps_by_resources)? .inspect(|call_info| {
cool!
Code quote:
inspect(
crates/blockifier/src/transaction/account_transaction.rs
line 498 at r10 (raw file):
.block_context .versioned_constants .sierra_gas_limit(&context.execution_mode),
can you add a global_sierra_gas_limit
method on EntryPointExecutionContext
? that way you won't need to write context
twice (non-blocking)
Suggestion:
context.global_sierra_gas_limit(),
ab670f8
to
3547859
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r11, all commit messages.
Reviewable status: all files reviewed, 14 unresolved discussions (waiting on @aner-starkware)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @aner-starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
/// Default initial gas amount when L2 gas is not provided. pub fn default_initial_gas_amount(&self) -> u64 {
Hmmm, how about making this a function of the resource bounds?
I don't want to get confused with the other default
Code quote:
pub fn default_initial_gas_amount(&self) -> u64 {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @aner-starkware and @Yoni-Starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
Previously, Yoni-Starkware (Yoni) wrote…
Hmmm, how about making this a function of the resource bounds?
I don't want to get confused with the other default
WDYM?
this function is a function of the versioned constants, resource bounds (a) are irrelevant and (b) don't have access to VC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @aner-starkware and @dorimedini-starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
Previously, dorimedini-starkware wrote…
WDYM?
this function is a function of the versioned constants, resource bounds (a) are irrelevant and (b) don't have access to VC
They are relevant. The suggestion is only for readability. E.g.,
fn get_initial_sierra_gas(self, vc){
match &self {
ValidResourceBounds::L1Gas(_) => vs.validate_gas() + vs.execute_gas(),
ValidResourceBounds::AllResources(l2_gas, ...) => l2_gas
}
}
Alternatively, you can rename default_initial_gas_amount
to something more specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @aner-starkware and @dorimedini-starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
Previously, Yoni-Starkware (Yoni) wrote…
They are relevant. The suggestion is only for readability. E.g.,
fn get_initial_sierra_gas(self, vc){ match &self { ValidResourceBounds::L1Gas(_) => vs.validate_gas() + vs.execute_gas(), ValidResourceBounds::AllResources(l2_gas, ...) => l2_gas } }Alternatively, you can rename
default_initial_gas_amount
to something more specific.
Or make it a function of the tx context
Previously, Yoni-Starkware (Yoni) wrote…
There's already the function |
8655fd3
to
e575f59
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 9 of 9 files at r21, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @aner-starkware and @Yoni-Starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
Previously, aner-starkware wrote…
There's already the function
initial_sierra_gas
in the tx context; maybe that is what you're referring to?
maybe rename to initial_gas_without_user_l2_bound
or something
Previously, dorimedini-starkware wrote…
Which one? The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @aner-starkware and @dorimedini-starkware)
crates/blockifier/src/versioned_constants.rs
line 252 at r17 (raw file):
Previously, aner-starkware wrote…
Which one? The
initial_sierra_gas
deals with both cases. This function is indeed for the case with no userl2_bound
, but I actually think the other way around - the functiondefault_initial_gas_cost
should probably be renamed toinifite_gas_for_vm_mode
or something.
Either way is fine by me :)
Previously, dorimedini-starkware wrote…
tests in this PR: https://reviewable.io/reviews/starkware-libs/sequencer/2596 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @aner-starkware)
e575f59
to
bbd9bea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 10 files at r22, 9 of 9 files at r23, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @aner-starkware)
Benchmark movements: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @aner-starkware)
bbd9bea
to
825b072
Compare
Previously, Yoni-Starkware (Yoni) wrote…
Done. |
37ff6e0
to
bda947b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 12 of 12 files at r25, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Yoni-Starkware)
bda947b
to
ac4ed34
Compare
ac4ed34
to
444ca94
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dismissed @Yoni-Starkware from a discussion.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @aner-starkware)
No description provided.