-
Notifications
You must be signed in to change notification settings - Fork 579
feat(avm): gas mutations #19348
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
Open
IlyasRidhuan
wants to merge
1
commit into
merge-train/avm
Choose a base branch
from
ir/01-05-feat_avm_gas_mutations
base: merge-train/avm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−116
Open
feat(avm): gas mutations #19348
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| #include "barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp" | ||
|
|
||
| #include "barretenberg/avm_fuzzer/fuzz_lib/constants.hpp" | ||
| #include "barretenberg/avm_fuzzer/mutations/basic_types/field.hpp" | ||
| #include "barretenberg/avm_fuzzer/mutations/basic_types/vector.hpp" | ||
| #include "barretenberg/avm_fuzzer/mutations/configuration.hpp" | ||
| #include "barretenberg/common/serialize.hpp" | ||
| #include "barretenberg/numeric/uint256/uint256.hpp" | ||
| #include "barretenberg/vm2/common/avm_io.hpp" | ||
|
|
||
| using bb::avm2::AztecAddress; | ||
| using bb::avm2::FF; | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr uint128_t MAX_U128 = ~static_cast<uint128_t>(0); | ||
|
|
||
| uint128_t generate_u128(std::mt19937_64& rng, uint128_t min = 0, uint128_t max = MAX_U128) | ||
| { | ||
| uint64_t high = std::uniform_int_distribution<uint64_t>()(rng); | ||
| uint64_t low = std::uniform_int_distribution<uint64_t>()(rng); | ||
| uint128_t value = (static_cast<uint128_t>(high) << 64) | static_cast<uint128_t>(low); | ||
| // Scale to desired range | ||
| return min + (value % (max - min + 1)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace bb::avm2::fuzzer { | ||
|
|
||
| Gas generate_gas(std::mt19937_64& rng) | ||
| { | ||
| uint32_t l2_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_L2_GAS)(rng); | ||
| uint32_t da_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_DA_GAS)(rng); | ||
|
|
||
| return Gas{ l2_gas, da_gas }; | ||
| } | ||
|
|
||
| void mutate_gas(Gas& gas, std::mt19937_64& rng) | ||
| { | ||
| auto choice = std::uniform_int_distribution<uint8_t>(0, 1)(rng); | ||
|
|
||
| switch (choice) { | ||
| case 0: | ||
| // Mutate l2_gas | ||
| gas.l2_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_L2_GAS)(rng); | ||
| break; | ||
| case 1: | ||
| // Mutate da_gas | ||
| gas.da_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_DA_GAS)(rng); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| GasFees generate_gas_fees(std::mt19937_64& rng) | ||
| { | ||
| uint128_t fee_per_da_gas = generate_u128(rng, MIN_FEE, MAX_FEE); | ||
| uint128_t fee_per_l2_gas = generate_u128(rng, MIN_FEE, MAX_FEE); | ||
|
|
||
| return GasFees{ | ||
| fee_per_da_gas, | ||
| fee_per_l2_gas, | ||
| }; | ||
| } | ||
|
|
||
| void mutate_gas_fees(GasFees& fees, std::mt19937_64& rng) | ||
| { | ||
| auto choice = std::uniform_int_distribution<uint8_t>(0, 1)(rng); | ||
|
|
||
| switch (choice) { | ||
| case 0: | ||
| // Mutate fee_per_da_gas | ||
| fees.fee_per_da_gas = generate_u128(rng, MIN_FEE, MAX_FEE); | ||
| break; | ||
| case 1: | ||
| // Mutate fee_per_l2_gas | ||
| fees.fee_per_l2_gas = generate_u128(rng, MIN_FEE, MAX_FEE); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| GasSettings generate_gas_settings(std::mt19937_64& rng) | ||
| { | ||
| Gas gas_limits = generate_gas(rng); | ||
| Gas teardown_gas_limits = generate_gas(rng); | ||
| GasFees max_fees_per_gas = generate_gas_fees(rng); | ||
| GasFees max_priority_fees_per_gas = generate_gas_fees(rng); | ||
|
|
||
| return GasSettings{ | ||
| gas_limits, | ||
| teardown_gas_limits, | ||
| max_fees_per_gas, | ||
| max_priority_fees_per_gas, | ||
| }; | ||
| } | ||
|
|
||
| void mutate_gas_settings(GasSettings& gas_settings, std::mt19937_64& rng) | ||
| { | ||
| auto choice = GAS_SETTINGS_MUTATION_CONFIGURATION.select(rng); | ||
|
|
||
| switch (choice) { | ||
| case GasSettingsMutationOptions::GasLimits: | ||
| // Mutate gas_limits | ||
| mutate_gas(gas_settings.gas_limits, rng); | ||
| break; | ||
| case GasSettingsMutationOptions::TeardownGasLimits: | ||
| // Mutate teardown_gas_limits | ||
| mutate_gas(gas_settings.teardown_gas_limits, rng); | ||
| break; | ||
| case GasSettingsMutationOptions::MaxFeesPerGas: | ||
| // Mutate max_fees_per_gas | ||
| mutate_gas_fees(gas_settings.max_fees_per_gas, rng); | ||
| break; | ||
| case GasSettingsMutationOptions::MaxPriorityFeesPerGas: | ||
| // Mutate max_priority_fees_per_gas | ||
| mutate_gas_fees(gas_settings.max_priority_fees_per_gas, rng); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| GasFees compute_effective_gas_fees(const GasFees& gas_fees, const GasSettings& gas_settings) | ||
| { | ||
| // Match TS computeEffectiveGasFees from yarn-project/stdlib/src/fees/transaction_fee.ts | ||
| // priorityFees = min(maxPriorityFeesPerGas, maxFeesPerGas - gasFees) | ||
| // effectiveFees = gasFees + priorityFees | ||
| auto min_u128 = [](uint128_t a, uint128_t b) { return a < b ? a : b; }; | ||
|
|
||
| uint128_t priority_da = min_u128(gas_settings.max_priority_fees_per_gas.fee_per_da_gas, | ||
| gas_settings.max_fees_per_gas.fee_per_da_gas - gas_fees.fee_per_da_gas); | ||
| uint128_t priority_l2 = min_u128(gas_settings.max_priority_fees_per_gas.fee_per_l2_gas, | ||
| gas_settings.max_fees_per_gas.fee_per_l2_gas - gas_fees.fee_per_l2_gas); | ||
|
|
||
| return GasFees{ | ||
| .fee_per_da_gas = gas_fees.fee_per_da_gas + priority_da, | ||
| .fee_per_l2_gas = gas_fees.fee_per_l2_gas + priority_l2, | ||
| }; | ||
| } | ||
|
|
||
| } // namespace bb::avm2::fuzzer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do i need to check that this value is
< gas_limits? Or is it worthwhile trying out scenarios where this is not true?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.
As an improvement, we might want to generate taking into account the limit instead of generate + clamp, or we we'll overrepresent gas used by private == limit
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.
or we could just generate it and then do modulo the limit