diff --git a/barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp b/barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp index 8988b8aaede0..d5587f417097 100644 --- a/barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp +++ b/barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp @@ -32,7 +32,7 @@ const std::vector REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESS const std::vector SETUP_ENQUEUED_CALLS = {}; const FF MSG_SENDER = 100; const std::optional TEARDOWN_ENQUEUED_CALLS = std::nullopt; -const Gas GAS_USED_BY_PRIVATE = Gas{ .l2_gas = 0, .da_gas = 0 }; +const Gas GAS_USED_BY_PRIVATE = Gas{ .l2_gas = PUBLIC_TX_L2_GAS_OVERHEAD, .da_gas = TX_DA_GAS_OVERHEAD }; const AztecAddress FEE_PAYER = AztecAddress{ 0 }; const FF CONTRACT_ADDRESS = 42; const FF TRANSACTION_FEE = 0; diff --git a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_data.cpp b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_data.cpp index a78fe06b9f34..64f41ef467ca 100644 --- a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_data.cpp +++ b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_data.cpp @@ -112,7 +112,7 @@ void mutate_tx(Tx& tx, std::vector& contract_addresses, std::mt199 case TxMutationOptions::GasUsedByPrivate: // Mutate gas_used_by_private fuzz_info("Mutating gas used by private"); - mutate_gas(tx.gas_used_by_private, rng, tx.gas_settings.gas_limits); + mutate_gas(tx.gas_used_by_private, rng, GAS_USED_BY_PRIVATE, tx.gas_settings.gas_limits); break; case TxMutationOptions::FeePayer: // Mutate fee_payer diff --git a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp index 4d12a4f3fd29..a5a61f3d2e79 100644 --- a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp +++ b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp @@ -28,26 +28,26 @@ uint128_t generate_u128(std::mt19937_64& rng, uint128_t min = 0, uint128_t max = namespace bb::avm2::fuzzer { -Gas generate_gas(std::mt19937_64& rng) +Gas generate_gas(std::mt19937_64& rng, const Gas& min, const Gas& max) { - uint32_t l2_gas = std::uniform_int_distribution(MIN_GAS, AVM_MAX_PROCESSABLE_L2_GAS)(rng); - uint32_t da_gas = std::uniform_int_distribution(MIN_GAS, AVM_MAX_PROCESSABLE_DA_GAS)(rng); + uint32_t l2_gas = std::uniform_int_distribution(min.l2_gas, max.l2_gas)(rng); + uint32_t da_gas = std::uniform_int_distribution(min.da_gas, max.da_gas)(rng); return Gas{ l2_gas, da_gas }; } -void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& max) +void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& min, const Gas& max) { auto choice = std::uniform_int_distribution(0, 1)(rng); switch (choice) { case 0: // Mutate l2_gas - gas.l2_gas = std::uniform_int_distribution(MIN_GAS, max.l2_gas)(rng); + gas.l2_gas = std::uniform_int_distribution(min.l2_gas, max.l2_gas)(rng); break; case 1: // Mutate da_gas - gas.da_gas = std::uniform_int_distribution(MIN_GAS, max.da_gas)(rng); + gas.da_gas = std::uniform_int_distribution(min.da_gas, max.da_gas)(rng); break; } } diff --git a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp index f8715e30d1ee..e2f013637f32 100644 --- a/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp +++ b/barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp @@ -15,12 +15,13 @@ constexpr uint128_t MIN_FEE = 1; constexpr uint128_t MAX_FEE = 1000; // // Gas bounds for mutation -constexpr uint32_t MIN_GAS = 0; -constexpr uint32_t AVM_MAX_PROCESSABLE_DA_GAS = (MAX_NOTE_HASHES_PER_TX * AVM_EMITNOTEHASH_BASE_DA_GAS) + - (MAX_NULLIFIERS_PER_TX * AVM_EMITNULLIFIER_BASE_DA_GAS) + - (MAX_L2_TO_L1_MSGS_PER_TX * AVM_SENDL2TOL1MSG_BASE_DA_GAS) + - (MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * AVM_SSTORE_DYN_DA_GAS) + - (PUBLIC_LOGS_LENGTH * AVM_EMITPUBLICLOG_BASE_DA_GAS); +constexpr uint32_t MAX_PROCESSABLE_DA_GAS = TX_DA_GAS_OVERHEAD + + (MAX_NOTE_HASHES_PER_TX * AVM_EMITNOTEHASH_BASE_DA_GAS) + + (MAX_NULLIFIERS_PER_TX * AVM_EMITNULLIFIER_BASE_DA_GAS) + + (MAX_L2_TO_L1_MSGS_PER_TX * AVM_SENDL2TOL1MSG_BASE_DA_GAS) + + (MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * AVM_SSTORE_DYN_DA_GAS) + + (PUBLIC_LOGS_LENGTH * AVM_EMITPUBLICLOG_BASE_DA_GAS); +constexpr Gas MAX_GAS_LIMIT = Gas{ .l2_gas = MAX_PROCESSABLE_L2_GAS, .da_gas = MAX_PROCESSABLE_DA_GAS }; enum class GasSettingsMutationOptions : uint8_t { GasLimits, @@ -38,10 +39,8 @@ constexpr GasSettingsMutationConfig GAS_SETTINGS_MUTATION_CONFIGURATION = GasSet { GasSettingsMutationOptions::MaxPriorityFeesPerGas, 5 }, }); -Gas generate_gas(std::mt19937_64& rng); -void mutate_gas(Gas& gas, - std::mt19937_64& rng, - const Gas& max = Gas{ AVM_MAX_PROCESSABLE_L2_GAS, AVM_MAX_PROCESSABLE_DA_GAS }); +Gas generate_gas(std::mt19937_64& rng, const Gas& min = {}, const Gas& max = MAX_GAS_LIMIT); +void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& min = {}, const Gas& max = MAX_GAS_LIMIT); GasSettings generate_gas_settings(std::mt19937_64& rng); void mutate_gas_settings(GasSettings& data, std::mt19937_64& rng); diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/aztec_constants.hpp b/barretenberg/cpp/src/barretenberg/vm2/common/aztec_constants.hpp index 3890636f80b3..1939294f3582 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/common/aztec_constants.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/common/aztec_constants.hpp @@ -179,7 +179,10 @@ #define AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH 18740 #define AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED 16400 #define AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS_PADDED 1000 +#define TX_DA_GAS_OVERHEAD 96 +#define PUBLIC_TX_L2_GAS_OVERHEAD 540000 #define AVM_MAX_PROCESSABLE_L2_GAS 6000000 +#define MAX_PROCESSABLE_L2_GAS 6540000 #define AVM_PC_SIZE_IN_BITS 32 #define AVM_MAX_OPERANDS 7 #define AVM_MAX_REGISTERS 6 @@ -221,7 +224,7 @@ #define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 540 #define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 6108 #define AVM_EMITPUBLICLOG_BASE_L2_GAS 15 -#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 478 +#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 5239 #define AVM_CALL_BASE_L2_GAS 9936 #define AVM_STATICCALL_BASE_L2_GAS 9936 #define AVM_RETURN_BASE_L2_GAS 9 diff --git a/barretenberg/cpp/src/barretenberg/vm2/constraining/avm_fixed_vk.hpp b/barretenberg/cpp/src/barretenberg/vm2/constraining/avm_fixed_vk.hpp index 93e925fff4c8..3e725f93b8c7 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/constraining/avm_fixed_vk.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/constraining/avm_fixed_vk.hpp @@ -17,7 +17,7 @@ class AvmHardCodedVKAndHash { using FF = bb::curve::BN254::ScalarField; // Precomputed VK hash (hash of all commitments below). - static FF vk_hash() { return FF(uint256_t("0x02296b934ced1a5cdacae120d2032d88a119bdb0738d4c4f3ada4f5a831a5153")); } + static FF vk_hash() { return FF(uint256_t("0x18952f5711d5f7a6e29c980f1077eecd2ec45e80ba0ae78d5a4ae08e50428cab")); } static constexpr std::array get_all() { @@ -71,9 +71,9 @@ class AvmHardCodedVKAndHash { uint256_t( "0x090dda25e7d64ab5cabe09fd80fbb731af2a98de7a608157dc10394b4fc022a4")), // precomputed_exec_opcode_dynamic_l2_gas Commitment( - uint256_t("0x2216a1693dcb1cc83f57ea8058f681d71bdf0e6cfc839502cf16fb0a88a5f673"), + uint256_t("0x26086b5fb31a24f236f0441d5b922b94ca141e861b9cc640184681c518cd68d3"), uint256_t( - "0x255e6760ed9adda61aca7d0b7d4bb28bb62e3cca6e860009461a9a1708184be2")), // precomputed_exec_opcode_opcode_gas + "0x0bab134bb4e25ff33584c1094847e762ce6573054bae27715d0e4eb2b7278d80")), // precomputed_exec_opcode_opcode_gas Commitment( uint256_t("0x296def9415d1c96b4d8ab91df5f59ad8522a726f98461b1ab5c4d4c5b22471a4"), uint256_t( diff --git a/barretenberg/cpp/src/barretenberg/vm2/testing/avm_inputs.testdata.bin b/barretenberg/cpp/src/barretenberg/vm2/testing/avm_inputs.testdata.bin index 44bfd7a9a088..13cf29d653c4 100644 Binary files a/barretenberg/cpp/src/barretenberg/vm2/testing/avm_inputs.testdata.bin and b/barretenberg/cpp/src/barretenberg/vm2/testing/avm_inputs.testdata.bin differ diff --git a/barretenberg/cpp/src/barretenberg/vm2/testing/minimal_tx.testdata.bin b/barretenberg/cpp/src/barretenberg/vm2/testing/minimal_tx.testdata.bin index 57ac06aafc7d..10e8459aa257 100644 Binary files a/barretenberg/cpp/src/barretenberg/vm2/testing/minimal_tx.testdata.bin and b/barretenberg/cpp/src/barretenberg/vm2/testing/minimal_tx.testdata.bin differ diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/gas_meter.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/gas_meter.nr index d598d9c99a5b..306af3f510a2 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/gas_meter.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/gas_meter.nr @@ -89,5 +89,11 @@ pub fn meter_gas_used(public_inputs: PrivateKernelCircuitPublicInputs, is_for_pu let metered_da_gas = metered_da_fields * DA_GAS_PER_FIELD; - Gas::tx_overhead() + Gas::new(metered_da_gas, metered_l2_gas) + teardown_gas + let overhead = if is_for_public { + Gas::public_tx_overhead() + } else { + Gas::private_tx_overhead() + }; + + overhead + Gas::new(metered_da_gas, metered_l2_gas) + teardown_gas } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail/meter_gas_used_tests.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail/meter_gas_used_tests.nr index 619b3e5af792..b88e87604e8e 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail/meter_gas_used_tests.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail/meter_gas_used_tests.nr @@ -12,7 +12,7 @@ use types::{ /// A minimum (private) tx initialized in the TestBuilder contains a protocol nullifier, which must exist in every tx. fn get_minimum_private_tx_gas_used() -> Gas { let nullifier_gas_used = Gas { da_gas: DA_GAS_PER_FIELD, l2_gas: L2_GAS_PER_NULLIFIER }; - Gas::tx_overhead() + nullifier_gas_used + Gas::private_tx_overhead() + nullifier_gas_used } #[test] @@ -100,7 +100,7 @@ fn full_side_effects() { let mut builder = TestBuilder::new(); // Fill the tx with side effects and compute the expected gas used. - let mut expected_gas_used = Gas::tx_overhead(); + let mut expected_gas_used = Gas::private_tx_overhead(); // Note hashes. builder.previous_kernel.append_siloed_note_hashes(MAX_NOTE_HASHES_PER_TX); expected_gas_used += Gas { diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail_to_public/meter_gas_used_tests.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail_to_public/meter_gas_used_tests.nr index fda045e1726b..a3ae3df2822a 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail_to_public/meter_gas_used_tests.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail_to_public/meter_gas_used_tests.nr @@ -17,7 +17,7 @@ fn get_minimum_public_tx_gas_used() -> Gas { let nullifier_gas_used = Gas { da_gas: DA_GAS_PER_FIELD, l2_gas: AVM_EMITNULLIFIER_BASE_L2_GAS }; let public_call_gas_used = Gas { da_gas: 0, l2_gas: FIXED_AVM_STARTUP_L2_GAS }; - Gas::tx_overhead() + nullifier_gas_used + public_call_gas_used + Gas::public_tx_overhead() + nullifier_gas_used + public_call_gas_used } #[test] @@ -236,7 +236,8 @@ fn full_side_effects() { + MAX_PRIVATE_LOGS_PER_TX * L2_GAS_PER_PRIVATE_LOG + MAX_CONTRACT_CLASS_LOGS_PER_TX * L2_GAS_PER_CONTRACT_CLASS_LOG + MAX_ENQUEUED_CALLS_PER_TX * FIXED_AVM_STARTUP_L2_GAS; - let expected_gas_used = Gas::tx_overhead() + Gas::new(da_gas, l2_gas) + teardown_gas_limits; + let expected_gas_used = + Gas::public_tx_overhead() + Gas::new(da_gas, l2_gas) + teardown_gas_limits; let public_inputs = builder.execute(); assert_eq(public_inputs.gas_used, expected_gas_used); diff --git a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/private_tail_validator.nr b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/private_tail_validator.nr index f696642ceff4..a34fcc865938 100644 --- a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/private_tail_validator.nr +++ b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/private_tail_validator.nr @@ -8,8 +8,8 @@ use types::{ log_hash::LogHash, tx_constant_data::TxConstantData, }, constants::{ - ARCHIVE_HEIGHT, AVM_MAX_PROCESSABLE_L2_GAS, CONTRACT_CLASS_LOG_SIZE_IN_FIELDS, - MAX_CONTRACT_CLASS_LOGS_PER_TX, + ARCHIVE_HEIGHT, CONTRACT_CLASS_LOG_SIZE_IN_FIELDS, MAX_CONTRACT_CLASS_LOGS_PER_TX, + MAX_PROCESSABLE_L2_GAS, }, hash::compute_contract_class_log_hash, merkle_tree::{check_membership, MembershipWitness}, @@ -101,10 +101,9 @@ pub fn validate_tx_constant_data( ); // Ensure that the l2 gas limit is within the max processable l2 gas. - // The constant is prefixed with `AVM_` but it applies to both private-only and public-inclusive txs. // TODO: This should be moved to the private kernels once they are not used for gas estimation anymore. assert( - tx_gas_settings.gas_limits.l2_gas <= AVM_MAX_PROCESSABLE_L2_GAS, + tx_gas_settings.gas_limits.l2_gas <= MAX_PROCESSABLE_L2_GAS, "l2 gas limit exceeds max processable l2 gas", ); } diff --git a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/private_tx_base/validate_private_tail_tests.nr b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/private_tx_base/validate_private_tail_tests.nr index 8c478c970435..33c0831bdb81 100644 --- a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/private_tx_base/validate_private_tail_tests.nr +++ b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/private_tx_base/validate_private_tail_tests.nr @@ -1,5 +1,5 @@ use super::TestBuilder; -use types::{constants::AVM_MAX_PROCESSABLE_L2_GAS, tests::fixture_builder::FixtureBuilder}; +use types::{constants::MAX_PROCESSABLE_L2_GAS, tests::fixture_builder::FixtureBuilder}; #[test(should_fail_with = "Membership check failed: anchor block header hash not found in archive tree")] unconstrained fn anchor_block_header_not_in_archive() { @@ -62,7 +62,7 @@ unconstrained fn gas_settings_l2_gas_limit_exceeds_max_processable_l2_gas() { let mut builder = TestBuilder::new(); builder.private_tail.constants.tx_context.gas_settings.gas_limits.l2_gas = - AVM_MAX_PROCESSABLE_L2_GAS + 1; + MAX_PROCESSABLE_L2_GAS + 1; builder.execute_and_fail(); } diff --git a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/public_tx_base/validate_private_tail_to_public_tests.nr b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/public_tx_base/validate_private_tail_to_public_tests.nr index cf5c99f91b3d..e3b8791540cf 100644 --- a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/public_tx_base/validate_private_tail_to_public_tests.nr +++ b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/tx_base/tests/public_tx_base/validate_private_tail_to_public_tests.nr @@ -1,5 +1,5 @@ use super::TestBuilder; -use types::{constants::AVM_MAX_PROCESSABLE_L2_GAS, tests::fixture_builder::FixtureBuilder}; +use types::{constants::MAX_PROCESSABLE_L2_GAS, tests::fixture_builder::FixtureBuilder}; #[test(should_fail_with = "Membership check failed: anchor block header hash not found in archive tree")] unconstrained fn anchor_block_header_not_in_archive() { @@ -53,7 +53,7 @@ unconstrained fn gas_settings_l2_gas_limit_exceeds_max_processable_l2_gas() { let mut builder = TestBuilder::new(); builder.private_tail.constants.tx_context.gas_settings.gas_limits.l2_gas = - AVM_MAX_PROCESSABLE_L2_GAS + 1; + MAX_PROCESSABLE_L2_GAS + 1; builder.execute_and_fail(); } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr index 8b85a81a6e39..aa878684297d 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr @@ -1,6 +1,6 @@ use crate::{ abis::gas_fees::GasFees, - constants::{FIXED_DA_GAS, FIXED_L2_GAS}, + constants::{PRIVATE_TX_L2_GAS_OVERHEAD, PUBLIC_TX_L2_GAS_OVERHEAD, TX_DA_GAS_OVERHEAD}, traits::{Deserialize, Empty, Serialize}, }; use std::{meta::derive, ops::{Add, Sub}}; @@ -16,8 +16,12 @@ impl Gas { Self { da_gas, l2_gas } } - pub fn tx_overhead() -> Self { - Self { da_gas: FIXED_DA_GAS, l2_gas: FIXED_L2_GAS } + pub fn private_tx_overhead() -> Self { + Self { da_gas: TX_DA_GAS_OVERHEAD, l2_gas: PRIVATE_TX_L2_GAS_OVERHEAD } + } + + pub fn public_tx_overhead() -> Self { + Self { da_gas: TX_DA_GAS_OVERHEAD, l2_gas: PUBLIC_TX_L2_GAS_OVERHEAD } } pub fn compute_fee(self, fees: GasFees) -> Field { diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index c0ba749b920a..429afe14aec6 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -1032,21 +1032,22 @@ pub global AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED: u32 = 16400; pub global AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS_PADDED: u32 = 1000; // GAS DEFAULTS -// The maximum amount of l2 gas that the AVM can process safely. -pub global AVM_MAX_PROCESSABLE_L2_GAS: u32 = 6_000_000; // Arbitrary. - pub global DA_BYTES_PER_FIELD: u32 = 32; pub global DA_GAS_PER_BYTE: u32 = 1; // Arbitrary. pub global DA_GAS_PER_FIELD: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; // A tx always emits 3 fields: tx_start_marker, tx_hash, and tx_fee. -pub global FIXED_DA_GAS: u32 = 3 * DA_GAS_PER_FIELD; -// TODO: take into account the cost of executing and proving the rollup circuits (if they can be attributed to this tx... eg the tx base rollup, half a tx merge). -// We need a test suite to demonstrate these measurements -// pays for fixed tx costs like validation, and updating state roots -pub global FIXED_L2_GAS: u32 = 512; +pub global TX_DA_GAS_OVERHEAD: u32 = 3 * DA_GAS_PER_FIELD; +// Computed taking into account simulation time metrics. +pub global PUBLIC_TX_L2_GAS_OVERHEAD: u32 = 540000; +pub global PRIVATE_TX_L2_GAS_OVERHEAD: u32 = 440000; // base cost for a single public call pub global FIXED_AVM_STARTUP_L2_GAS: u32 = 20_000; +// The maximum amount of l2 gas that the AVM can process safely. +pub global AVM_MAX_PROCESSABLE_L2_GAS: u32 = 6_000_000; // Arbitrary. +// The following limit assumes that a private only tx will always be cheaper than a tx with a full public component. +pub global MAX_PROCESSABLE_L2_GAS: u32 = PUBLIC_TX_L2_GAS_OVERHEAD + AVM_MAX_PROCESSABLE_L2_GAS; + pub global MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT: u32 = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB * DA_GAS_PER_FIELD; @@ -1054,9 +1055,9 @@ pub global MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT: u32 = // Since we split in teardown and total, teardown has the theoretical maximum and total is that times 2. // After gas estimation, we tune it down to the actual amount necessary. // TODO: this is a wallet concern; it should not be part of the protocol -pub global GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT: u32 = AVM_MAX_PROCESSABLE_L2_GAS; +pub global GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT: u32 = MAX_PROCESSABLE_L2_GAS; pub global GAS_ESTIMATION_L2_GAS_LIMIT: u32 = - GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT + AVM_MAX_PROCESSABLE_L2_GAS; + GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT + MAX_PROCESSABLE_L2_GAS; pub global GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT: u32 = MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT; pub global GAS_ESTIMATION_DA_GAS_LIMIT: u32 = @@ -1065,7 +1066,7 @@ pub global GAS_ESTIMATION_DA_GAS_LIMIT: u32 = // Default gas limits. Users should use gas estimation, or they will overpay gas fees. // TODO: consider moving to typescript pub global DEFAULT_TEARDOWN_L2_GAS_LIMIT: u32 = 1_000_000; // Arbitrary default number. -pub global DEFAULT_L2_GAS_LIMIT: u32 = AVM_MAX_PROCESSABLE_L2_GAS; // Arbitrary default number. +pub global DEFAULT_L2_GAS_LIMIT: u32 = MAX_PROCESSABLE_L2_GAS; // Arbitrary default number. pub global DEFAULT_TEARDOWN_DA_GAS_LIMIT: u32 = MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT / 2; // Arbitrary default number. pub global DEFAULT_DA_GAS_LIMIT: u32 = MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT; // Arbitrary default number. @@ -1089,14 +1090,12 @@ pub global AVM_ADDRESSING_INDIRECT_L2_GAS: u32 = 3; // One mem access pub global AVM_ADDRESSING_RELATIVE_L2_GAS: u32 = 3; // One range check // Base L2 GAS -// TODO: Decide what the following constants should be. -pub global L2_GAS_PER_NOTE_HASH: u32 = 0; -pub global L2_GAS_PER_NULLIFIER: u32 = 0; -// Gas for writing message to L1 portal -pub global L2_GAS_PER_L2_TO_L1_MSG: u32 = 200; // TODO: Update and explain this. -// Zero gas because we don't have to hash and validate the private logs -pub global L2_GAS_PER_PRIVATE_LOG: u32 = 0; -pub global L2_GAS_PER_CONTRACT_CLASS_LOG: u32 = 0; // TODO: this should be nonzero, because the sequencer is doing work to hash this, as part of tx validation +// Based on simulation time metrics +pub global L2_GAS_PER_NOTE_HASH: u32 = 2700; +pub global L2_GAS_PER_NULLIFIER: u32 = 16000; +pub global L2_GAS_PER_L2_TO_L1_MSG: u32 = 5200; +pub global L2_GAS_PER_PRIVATE_LOG: u32 = 2500; +pub global L2_GAS_PER_CONTRACT_CLASS_LOG: u32 = 73000; // Note: magic numbers here are derived from each op's AVM circuit trace area // https://docs.google.com/spreadsheets/d/1FPyLfJFPOfZTmZC-T6b_4yB5dSrOAofz3dufte23_TA/edit?usp=sharing // Some have a "SLOW_SIM_MUL" multiplier because they are slower to simulate and their trace-area-derived gas cost @@ -1136,7 +1135,7 @@ pub global AVM_EMITNULLIFIER_BASE_L2_GAS: u32 = (516 + L2_GAS_DISTRIBUTED_STORAG pub global AVM_L1TOL2MSGEXISTS_BASE_L2_GAS: u32 = 108 * 5; // SLOW_SIM_MUL = 4 (+1 for slow proving) pub global AVM_GETCONTRACTINSTANCE_BASE_L2_GAS: u32 = 1527 * 4; // SLOW_SIM_MUL = 4 pub global AVM_EMITPUBLICLOG_BASE_L2_GAS: u32 = 15; -pub global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u32 = (39 + L2_GAS_PER_L2_TO_L1_MSG) * 2; // SLOW_SIM_MUL = 2 +pub global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u32 = 39 + L2_GAS_PER_L2_TO_L1_MSG; // See PR https://github.com/AztecProtocol/aztec-packages/pull/15495 on why we need this buffer. pub global AVM_CALL_BASE_L2_GAS: u32 = 3312 * 3; // SLOW_SIM_MUL = 3 pub global AVM_STATICCALL_BASE_L2_GAS: u32 = 3312 * 3; // SLOW_SIM_MUL = 3 diff --git a/yarn-project/aztec.js/src/contract/get_gas_limits.test.ts b/yarn-project/aztec.js/src/contract/get_gas_limits.test.ts index 1fcdc12d2c9c..bf4edcc7c9ab 100644 --- a/yarn-project/aztec.js/src/contract/get_gas_limits.test.ts +++ b/yarn-project/aztec.js/src/contract/get_gas_limits.test.ts @@ -1,4 +1,4 @@ -import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; +import { MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; import { Gas } from '@aztec/stdlib/gas'; import { mockSimulatedTx, mockTxForRollup } from '@aztec/stdlib/testing'; import type { TxSimulationResult } from '@aztec/stdlib/tx'; @@ -50,19 +50,19 @@ describe('getGasLimits', () => { it('should fail if gas exceeds max processable gas', () => { // Consumes all of processable gas txSimulationResult.publicOutput!.gasUsed = { - totalGas: Gas.from({ daGas: 140, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS }), - billedGas: Gas.from({ daGas: 150, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS }), - teardownGas: Gas.from({ daGas: 10, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS * 0.2 }), - publicGas: Gas.from({ daGas: 50, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS }), + totalGas: Gas.from({ daGas: 140, l2Gas: MAX_PROCESSABLE_L2_GAS }), + billedGas: Gas.from({ daGas: 150, l2Gas: MAX_PROCESSABLE_L2_GAS }), + teardownGas: Gas.from({ daGas: 10, l2Gas: MAX_PROCESSABLE_L2_GAS * 0.2 }), + publicGas: Gas.from({ daGas: 50, l2Gas: MAX_PROCESSABLE_L2_GAS }), }; // Does not fail without padding since it's at the limit expect(getGasLimits(txSimulationResult, 0)).toEqual({ - gasLimits: Gas.from({ daGas: 140, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS }), - teardownGasLimits: Gas.from({ daGas: 10, l2Gas: AVM_MAX_PROCESSABLE_L2_GAS * 0.2 }), + gasLimits: Gas.from({ daGas: 140, l2Gas: MAX_PROCESSABLE_L2_GAS }), + teardownGasLimits: Gas.from({ daGas: 10, l2Gas: Math.ceil(MAX_PROCESSABLE_L2_GAS * 0.2) }), }); // Fails with padding since it's over the limit expect(() => getGasLimits(txSimulationResult, 0.1)).toThrow( - 'Transaction consumes more gas than the AVM maximum processable gas', + 'Transaction consumes more l2 gas than the maximum processable gas', ); }); }); diff --git a/yarn-project/aztec.js/src/contract/get_gas_limits.ts b/yarn-project/aztec.js/src/contract/get_gas_limits.ts index d840011cce9f..d54a786c113b 100644 --- a/yarn-project/aztec.js/src/contract/get_gas_limits.ts +++ b/yarn-project/aztec.js/src/contract/get_gas_limits.ts @@ -1,4 +1,4 @@ -import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; +import { MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; import { Gas } from '@aztec/stdlib/gas'; import type { TxSimulationResult } from '@aztec/stdlib/tx'; @@ -23,8 +23,8 @@ export function getGasLimits( const gasLimits = simulationResult.gasUsed.totalGas.mul(1 + pad); const teardownGasLimits = simulationResult.gasUsed.teardownGas.mul(1 + pad); - if (gasLimits.l2Gas > AVM_MAX_PROCESSABLE_L2_GAS) { - throw new Error('Transaction consumes more gas than the AVM maximum processable gas'); + if (gasLimits.l2Gas > MAX_PROCESSABLE_L2_GAS) { + throw new Error('Transaction consumes more l2 gas than the maximum processable gas'); } return { gasLimits, diff --git a/yarn-project/constants/src/constants.gen.ts b/yarn-project/constants/src/constants.gen.ts index ddc691f25053..99ea3d02afff 100644 --- a/yarn-project/constants/src/constants.gen.ts +++ b/yarn-project/constants/src/constants.gen.ts @@ -385,20 +385,22 @@ export const AVM_NUM_PUBLIC_INPUT_COLUMNS = 4; export const AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH = 18740; export const AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED = 16400; export const AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS_PADDED = 1000; -export const AVM_MAX_PROCESSABLE_L2_GAS = 6000000; export const DA_BYTES_PER_FIELD = 32; export const DA_GAS_PER_BYTE = 1; export const DA_GAS_PER_FIELD = 32; -export const FIXED_DA_GAS = 96; -export const FIXED_L2_GAS = 512; +export const TX_DA_GAS_OVERHEAD = 96; +export const PUBLIC_TX_L2_GAS_OVERHEAD = 540000; +export const PRIVATE_TX_L2_GAS_OVERHEAD = 440000; export const FIXED_AVM_STARTUP_L2_GAS = 20000; +export const AVM_MAX_PROCESSABLE_L2_GAS = 6000000; +export const MAX_PROCESSABLE_L2_GAS = 6540000; export const MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT = 786432; -export const GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT = 6000000; -export const GAS_ESTIMATION_L2_GAS_LIMIT = 12000000; +export const GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT = 6540000; +export const GAS_ESTIMATION_L2_GAS_LIMIT = 13080000; export const GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT = 786432; export const GAS_ESTIMATION_DA_GAS_LIMIT = 1572864; export const DEFAULT_TEARDOWN_L2_GAS_LIMIT = 1000000; -export const DEFAULT_L2_GAS_LIMIT = 6000000; +export const DEFAULT_L2_GAS_LIMIT = 6540000; export const DEFAULT_TEARDOWN_DA_GAS_LIMIT = 393216; export const DEFAULT_DA_GAS_LIMIT = 786432; export const L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024; @@ -408,11 +410,11 @@ export const AVM_MAX_REGISTERS = 6; export const AVM_ADDRESSING_BASE_RESOLUTION_L2_GAS = 3; export const AVM_ADDRESSING_INDIRECT_L2_GAS = 3; export const AVM_ADDRESSING_RELATIVE_L2_GAS = 3; -export const L2_GAS_PER_NOTE_HASH = 0; -export const L2_GAS_PER_NULLIFIER = 0; -export const L2_GAS_PER_L2_TO_L1_MSG = 200; -export const L2_GAS_PER_PRIVATE_LOG = 0; -export const L2_GAS_PER_CONTRACT_CLASS_LOG = 0; +export const L2_GAS_PER_NOTE_HASH = 2700; +export const L2_GAS_PER_NULLIFIER = 16000; +export const L2_GAS_PER_L2_TO_L1_MSG = 5200; +export const L2_GAS_PER_PRIVATE_LOG = 2500; +export const L2_GAS_PER_CONTRACT_CLASS_LOG = 73000; export const AVM_ADD_BASE_L2_GAS = 12; export const AVM_SUB_BASE_L2_GAS = 12; export const AVM_MUL_BASE_L2_GAS = 27; @@ -448,7 +450,7 @@ export const AVM_EMITNULLIFIER_BASE_L2_GAS = 30800; export const AVM_L1TOL2MSGEXISTS_BASE_L2_GAS = 540; export const AVM_GETCONTRACTINSTANCE_BASE_L2_GAS = 6108; export const AVM_EMITPUBLICLOG_BASE_L2_GAS = 15; -export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 478; +export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 5239; export const AVM_CALL_BASE_L2_GAS = 9936; export const AVM_STATICCALL_BASE_L2_GAS = 9936; export const AVM_RETURN_BASE_L2_GAS = 9; diff --git a/yarn-project/constants/src/scripts/constants.in.ts b/yarn-project/constants/src/scripts/constants.in.ts index c43076c728a8..f1893cd88c32 100644 --- a/yarn-project/constants/src/scripts/constants.in.ts +++ b/yarn-project/constants/src/scripts/constants.in.ts @@ -28,6 +28,7 @@ const CPP_CONSTANTS = [ 'CONTRACT_CLASS_REGISTRY_CONTRACT_ADDRESS', 'MULTI_CALL_ENTRYPOINT_ADDRESS', 'FEE_JUICE_ADDRESS', + 'TX_DA_GAS_OVERHEAD', 'PUBLIC_CHECKS_ADDRESS', 'FEE_JUICE_BALANCES_SLOT', 'UPDATED_CLASS_IDS_SLOT', @@ -44,6 +45,7 @@ const CPP_CONSTANTS = [ 'MAX_NOTE_HASHES_PER_TX', 'MAX_NULLIFIERS_PER_TX', 'MAX_L2_TO_L1_MSGS_PER_TX', + 'MAX_PROCESSABLE_L2_GAS', 'MAX_PUBLIC_LOGS_PER_TX', 'MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX', 'MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS', @@ -111,6 +113,7 @@ const CPP_CONSTANTS = [ 'FLAT_PUBLIC_LOGS_PAYLOAD_LENGTH', 'PUBLIC_LOGS_LENGTH', 'PUBLIC_LOG_HEADER_LENGTH', + 'PUBLIC_TX_L2_GAS_OVERHEAD', 'MAX_PROTOCOL_CONTRACTS', 'DEFAULT_MAX_DEBUG_LOG_MEMORY_READS', ]; @@ -621,7 +624,11 @@ function evaluateExpressions(expressions: [string, string][]): { [key: string]: // We split the expression into terms... .split(/\s+/) // ...and then we convert each term to a BigInt if it is a number. - .map(term => (isNaN(+term) ? term : `BigInt('${term}')`)) + .map(term => { + // Remove underscores from numeric literals (e.g., 6_000_000 -> 6000000) + const termWithoutUnderscores = term.replace(/_/g, ''); + return isNaN(+termWithoutUnderscores) ? term : `BigInt('${termWithoutUnderscores}')`; + }) // .. also, we convert the known bigints to BigInts. .map(term => (knownBigInts.includes(term) ? `BigInt(${term})` : term)) // We join the terms back together. diff --git a/yarn-project/end-to-end/src/e2e_bot.test.ts b/yarn-project/end-to-end/src/e2e_bot.test.ts index 233293f72314..9ed66d5b27f6 100644 --- a/yarn-project/end-to-end/src/e2e_bot.test.ts +++ b/yarn-project/end-to-end/src/e2e_bot.test.ts @@ -13,7 +13,7 @@ import { SupportedTokenContracts, getBotDefaultConfig, } from '@aztec/bot'; -import { AVM_MAX_PROCESSABLE_L2_GAS, MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT } from '@aztec/constants'; +import { MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; import { SecretValue } from '@aztec/foundation/config'; import { bufferToHex } from '@aztec/foundation/string'; import { openTmpStore } from '@aztec/kv-store/lmdb-v2'; @@ -73,7 +73,7 @@ describe('e2e_bot', () => { }); it('sends token transfers with hardcoded gas and no simulation', async () => { - bot.updateConfig({ daGasLimit: MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, l2GasLimit: AVM_MAX_PROCESSABLE_L2_GAS }); + bot.updateConfig({ daGasLimit: MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, l2GasLimit: MAX_PROCESSABLE_L2_GAS }); const { recipient: recipientBefore } = await bot.getBalances(); await bot.run(); diff --git a/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.test.ts b/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.test.ts index 0330c20c496e..e014c085f6bf 100644 --- a/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.test.ts +++ b/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.test.ts @@ -1,9 +1,9 @@ import { - AVM_MAX_PROCESSABLE_L2_GAS, DEFAULT_DA_GAS_LIMIT, DEFAULT_TEARDOWN_DA_GAS_LIMIT, - FIXED_DA_GAS, - FIXED_L2_GAS, + MAX_PROCESSABLE_L2_GAS, + PUBLIC_TX_L2_GAS_OVERHEAD, + TX_DA_GAS_OVERHEAD, } from '@aztec/constants'; import { Fr } from '@aztec/foundation/curves/bn254'; import type { Writeable } from '@aztec/foundation/types'; @@ -22,6 +22,7 @@ import { type Tx, } from '@aztec/stdlib/tx'; +import assert from 'assert'; import { type MockProxy, mock, mockFn } from 'jest-mock-extended'; import { GasTxValidator } from './gas_validator.js'; @@ -112,16 +113,32 @@ describe('GasTxValidator', () => { }); it('rejects txs if the DA gas limit is not above the minimum amount', async () => { + assert(!!tx.data.forPublic); tx.data.constants.txContext.gasSettings = GasSettings.default({ - gasLimits: new Gas(1, FIXED_L2_GAS), + gasLimits: new Gas(1, PUBLIC_TX_L2_GAS_OVERHEAD), maxFeesPerGas: gasFees.clone(), }); await expectInvalid(tx, TX_ERROR_INSUFFICIENT_GAS_LIMIT); }); - it('rejects txs if the L2 gas limit is not above the minimum amount', async () => { + it('rejects txs if the L2 gas limit is not above the minimum amount in a public tx', async () => { + assert(!!tx.data.forPublic); tx.data.constants.txContext.gasSettings = GasSettings.default({ - gasLimits: new Gas(FIXED_DA_GAS, 1), + gasLimits: new Gas(TX_DA_GAS_OVERHEAD, 1), + maxFeesPerGas: gasFees.clone(), + }); + await expectInvalid(tx, TX_ERROR_INSUFFICIENT_GAS_LIMIT); + }); + + it('rejects txs if the L2 gas limit is not above the minimum amount in a private tx', async () => { + tx = await mockTx(1, { + numberOfNonRevertiblePublicCallRequests: 0, + numberOfRevertiblePublicCallRequests: 0, + hasPublicTeardownCallRequest: false, + }); + assert(!tx.data.forPublic); + tx.data.constants.txContext.gasSettings = GasSettings.default({ + gasLimits: new Gas(TX_DA_GAS_OVERHEAD, 1), maxFeesPerGas: gasFees.clone(), }); await expectInvalid(tx, TX_ERROR_INSUFFICIENT_GAS_LIMIT); @@ -147,7 +164,7 @@ describe('GasTxValidator', () => { it('rejects txs if the l2 gas limit is too high', async () => { tx.data.constants.txContext.gasSettings = GasSettings.default({ - gasLimits: new Gas(DEFAULT_DA_GAS_LIMIT, AVM_MAX_PROCESSABLE_L2_GAS + 1), + gasLimits: new Gas(DEFAULT_DA_GAS_LIMIT, MAX_PROCESSABLE_L2_GAS + 1), maxFeesPerGas: gasFees.clone(), teardownGasLimits: new Gas(DEFAULT_TEARDOWN_DA_GAS_LIMIT, 1), }); diff --git a/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.ts b/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.ts index 2b07bd66cd0b..a404cb0287bd 100644 --- a/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.ts +++ b/yarn-project/p2p/src/msg_validators/tx_validator/gas_validator.ts @@ -1,4 +1,9 @@ -import { AVM_MAX_PROCESSABLE_L2_GAS, FIXED_DA_GAS, FIXED_L2_GAS } from '@aztec/constants'; +import { + MAX_PROCESSABLE_L2_GAS, + PRIVATE_TX_L2_GAS_OVERHEAD, + PUBLIC_TX_L2_GAS_OVERHEAD, + TX_DA_GAS_OVERHEAD, +} from '@aztec/constants'; import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log'; import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice'; import type { AztecAddress } from '@aztec/stdlib/aztec-address'; @@ -73,7 +78,10 @@ export class GasTxValidator implements TxValidator { */ #validateGasLimit(tx: Tx): TxValidationResult { const gasLimits = tx.data.constants.txContext.gasSettings.gasLimits; - const minGasLimits = new Gas(FIXED_DA_GAS, FIXED_L2_GAS); + const minGasLimits = new Gas( + TX_DA_GAS_OVERHEAD, + tx.data.forPublic ? PUBLIC_TX_L2_GAS_OVERHEAD : PRIVATE_TX_L2_GAS_OVERHEAD, + ); if (minGasLimits.gtAny(gasLimits)) { this.#log.verbose(`Rejecting transaction due to the gas limit(s) not being above the minimum gas limit`, { @@ -83,7 +91,7 @@ export class GasTxValidator implements TxValidator { return { result: 'invalid', reason: [TX_ERROR_INSUFFICIENT_GAS_LIMIT] }; } - if (gasLimits.l2Gas > AVM_MAX_PROCESSABLE_L2_GAS) { + if (gasLimits.l2Gas > MAX_PROCESSABLE_L2_GAS) { this.#log.verbose(`Rejecting transaction due to the gas limit(s) being higher than the maximum processable gas`, { gasLimits, minGasLimits, diff --git a/yarn-project/pxe/src/contract_function_simulator/contract_function_simulator.ts b/yarn-project/pxe/src/contract_function_simulator/contract_function_simulator.ts index 96cda506683c..e4e783f26b02 100644 --- a/yarn-project/pxe/src/contract_function_simulator/contract_function_simulator.ts +++ b/yarn-project/pxe/src/contract_function_simulator/contract_function_simulator.ts @@ -4,8 +4,6 @@ import { AVM_SENDL2TOL1MSG_BASE_L2_GAS, DA_GAS_PER_FIELD, FIXED_AVM_STARTUP_L2_GAS, - FIXED_DA_GAS, - FIXED_L2_GAS, L2_GAS_PER_CONTRACT_CLASS_LOG, L2_GAS_PER_L2_TO_L1_MSG, L2_GAS_PER_NOTE_HASH, @@ -19,6 +17,9 @@ import { MAX_NULLIFIERS_PER_TX, MAX_NULLIFIER_READ_REQUESTS_PER_TX, MAX_PRIVATE_LOGS_PER_TX, + PRIVATE_TX_L2_GAS_OVERHEAD, + PUBLIC_TX_L2_GAS_OVERHEAD, + TX_DA_GAS_OVERHEAD, } from '@aztec/constants'; import { arrayNonEmptyLength, padArrayEnd } from '@aztec/foundation/collection'; import { Fr } from '@aztec/foundation/curves/bn254'; @@ -653,7 +654,12 @@ export async function generateSimulatedProvingResult( const publicInputs = new PrivateKernelTailCircuitPublicInputs( constantData, - /*gasUsed=*/ gasUsed.add(Gas.from({ l2Gas: FIXED_L2_GAS, daGas: FIXED_DA_GAS })), + /*gasUsed=*/ gasUsed.add( + Gas.from({ + l2Gas: isPrivateOnlyTx ? PRIVATE_TX_L2_GAS_OVERHEAD : PUBLIC_TX_L2_GAS_OVERHEAD, + daGas: TX_DA_GAS_OVERHEAD, + }), + ), /*feePayer=*/ AztecAddress.zero(), /*expirationTimestamp=*/ 0n, hasPublicCalls ? inputsForPublic : undefined, diff --git a/yarn-project/simulator/src/public/fixtures/public_tx_simulation_tester.ts b/yarn-project/simulator/src/public/fixtures/public_tx_simulation_tester.ts index ae636ab8fb9e..895f6cc76ec0 100644 --- a/yarn-project/simulator/src/public/fixtures/public_tx_simulation_tester.ts +++ b/yarn-project/simulator/src/public/fixtures/public_tx_simulation_tester.ts @@ -1,4 +1,9 @@ -import { DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT } from '@aztec/constants'; +import { + DEFAULT_TEARDOWN_DA_GAS_LIMIT, + DEFAULT_TEARDOWN_L2_GAS_LIMIT, + PUBLIC_TX_L2_GAS_OVERHEAD, + TX_DA_GAS_OVERHEAD, +} from '@aztec/constants'; import { asyncMap } from '@aztec/foundation/async-map'; import { BlockNumber } from '@aztec/foundation/branded-types'; import { Fr } from '@aztec/foundation/curves/bn254'; @@ -131,8 +136,11 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester { teardownCallRequest, feePayer, /*gasUsedByPrivate*/ teardownCall - ? new Gas(DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT) - : Gas.empty(), + ? new Gas( + DEFAULT_TEARDOWN_DA_GAS_LIMIT + TX_DA_GAS_OVERHEAD, + DEFAULT_TEARDOWN_L2_GAS_LIMIT + PUBLIC_TX_L2_GAS_OVERHEAD, + ) + : new Gas(TX_DA_GAS_OVERHEAD, PUBLIC_TX_L2_GAS_OVERHEAD), defaultGlobals(), ); } diff --git a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.test.ts b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.test.ts index 38771de3382a..a0a86e68d5c0 100644 --- a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.test.ts +++ b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.test.ts @@ -1,9 +1,9 @@ import { - AVM_MAX_PROCESSABLE_L2_GAS, GAS_ESTIMATION_DA_GAS_LIMIT, GAS_ESTIMATION_L2_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT, + MAX_PROCESSABLE_L2_GAS, NULLIFIER_SUBTREE_HEIGHT, } from '@aztec/constants'; import { Fr } from '@aztec/foundation/curves/bn254'; @@ -498,14 +498,14 @@ describe('public_tx_simulator', () => { it('fails a tx that consumes more than the AVM maximum processable gas', async () => { gasLimits = new Gas(GAS_ESTIMATION_DA_GAS_LIMIT, GAS_ESTIMATION_L2_GAS_LIMIT); teardownGasLimits = new Gas(GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT, GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT); - enqueuedCallGasUsed = new Gas(GAS_ESTIMATION_L2_GAS_LIMIT, AVM_MAX_PROCESSABLE_L2_GAS); + enqueuedCallGasUsed = new Gas(GAS_ESTIMATION_L2_GAS_LIMIT, MAX_PROCESSABLE_L2_GAS); const tx = await mockTxWithPublicCalls({ numberOfAppLogicCalls: 1, }); await expect(simulator.simulate(tx)).rejects.toThrow( - `exceeds the AVM maximum processable gas of ${AVM_MAX_PROCESSABLE_L2_GAS}`, + `exceeds the maximum processable gas of ${MAX_PROCESSABLE_L2_GAS}`, ); expect(simulateInternal).toHaveBeenCalledTimes(1); diff --git a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.ts b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.ts index 6e3af53f7c2f..c07f0d04945c 100644 --- a/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.ts +++ b/yarn-project/simulator/src/public/public_tx_simulator/public_tx_simulator.ts @@ -1,4 +1,4 @@ -import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; +import { MAX_PROCESSABLE_L2_GAS } from '@aztec/constants'; import { Fr } from '@aztec/foundation/curves/bn254'; import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log'; import { ProtocolContractAddress, ProtocolContractsList } from '@aztec/protocol-contracts'; @@ -199,8 +199,8 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface { // Such transactions should be filtered by GasTxValidator. assert( - context.getActualGasUsed().l2Gas <= AVM_MAX_PROCESSABLE_L2_GAS, - `Transaction consumes ${context.getActualGasUsed().l2Gas} L2 gas, which exceeds the AVM maximum processable gas of ${AVM_MAX_PROCESSABLE_L2_GAS}`, + context.getActualGasUsed().l2Gas <= MAX_PROCESSABLE_L2_GAS, + `Transaction consumes ${context.getActualGasUsed().l2Gas} L2 gas, which exceeds the maximum processable gas of ${MAX_PROCESSABLE_L2_GAS}`, ); await this.payFee(context); diff --git a/yarn-project/stdlib/src/tests/mocks.ts b/yarn-project/stdlib/src/tests/mocks.ts index 6ce72b3563de..ceffb21c01a8 100644 --- a/yarn-project/stdlib/src/tests/mocks.ts +++ b/yarn-project/stdlib/src/tests/mocks.ts @@ -1,10 +1,11 @@ import { - FIXED_DA_GAS, - FIXED_L2_GAS, MAX_ENQUEUED_CALLS_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_TX_LIFETIME, + PRIVATE_TX_L2_GAS_OVERHEAD, + PUBLIC_TX_L2_GAS_OVERHEAD, + TX_DA_GAS_OVERHEAD, } from '@aztec/constants'; import { type FieldsOf, makeTuple } from '@aztec/foundation/array'; import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types'; @@ -205,8 +206,11 @@ export async function mockProcessedTx({ feePayer, feePaymentPublicDataWrite, // The default gasUsed is the tx overhead. - gasUsed = Gas.from({ daGas: FIXED_DA_GAS, l2Gas: FIXED_L2_GAS }), privateOnly = false, + gasUsed = Gas.from({ + daGas: TX_DA_GAS_OVERHEAD, + l2Gas: privateOnly ? PRIVATE_TX_L2_GAS_OVERHEAD : PUBLIC_TX_L2_GAS_OVERHEAD, + }), avmAccumulatedData, ...mockTxOpts }: {