From d2fef12817725db967c698ed1c16fd9ed5a55ca7 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Fri, 12 Apr 2024 16:08:30 -0300 Subject: [PATCH] feat: Wire AVM gas used to public kernel --- .../src/core/libraries/ConstantsGen.sol | 6 +- .../context/inputs/public_context_inputs.nr | 4 +- .../aztec/src/context/private_context.nr | 11 +-- .../aztec/src/context/public_context.nr | 3 +- .../contracts/gas_token_contract/src/lib.nr | 1 + .../src/private_kernel_init.nr | 2 +- .../crates/public-kernel-lib/src/common.nr | 12 ++++ .../src/public_kernel_app_logic.nr | 2 + .../src/public_kernel_setup.nr | 2 + .../crates/types/src/abis.nr | 2 +- .../combined_accumulated_data.nr | 6 +- .../private_accumulated_data.nr | 4 +- .../private_accumulated_data_builder.nr | 6 +- .../public_accumulated_data.nr | 6 +- .../public_accumulated_data_builder.nr | 6 +- .../crates/types/src/abis/call_context.nr | 7 +- .../crates/types/src/abis/gas.nr | 58 +++++++++++++++ .../crates/types/src/abis/gas_settings.nr | 25 ++++++- .../crates/types/src/abis/gas_used.nr | 49 ------------- .../types/src/abis/public_call_stack_item.nr | 4 +- .../src/abis/public_circuit_public_inputs.nr | 10 ++- .../crates/types/src/constants.nr | 14 ++-- .../crates/types/src/tests/fixture_builder.nr | 8 +-- .../src/tests/private_call_data_builder.nr | 12 ++-- .../private_circuit_public_inputs_builder.nr | 14 ++-- .../src/tests/public_call_data_builder.nr | 8 ++- .../public_circuit_public_inputs_builder.nr | 11 +-- .../types/src/transaction/tx_request.nr | 29 ++++++-- .../contract/contract_function_interaction.ts | 9 +-- .../src/entrypoint/default_entrypoint.ts | 3 +- .../default_multi_call_entrypoint.ts | 3 +- .../aztec.js/src/entrypoint/payload.ts | 8 +-- .../aztec.js/src/fee/fee_payment_method.ts | 7 +- .../src/fee/native_fee_payment_method.ts | 11 ++- .../src/fee/private_fee_payment_method.ts | 7 +- .../src/fee/public_fee_payment_method.ts | 8 +-- .../circuit-types/src/tx_execution_request.ts | 10 ++- yarn-project/circuits.js/src/constants.gen.ts | 6 +- .../public_call_stack_item.test.ts.snap | 6 +- .../public_circuit_public_inputs.test.ts.snap | 4 +- .../__snapshots__/tx_request.test.ts.snap | 2 +- .../circuits.js/src/structs/call_context.ts | 9 +++ yarn-project/circuits.js/src/structs/gas.ts | 63 ++++++++++++++++ .../circuits.js/src/structs/gas_fees.ts | 5 ++ .../circuits.js/src/structs/gas_settings.ts | 66 ++++++++++++++++- .../circuits.js/src/structs/gas_used.ts | 36 ---------- yarn-project/circuits.js/src/structs/index.ts | 2 +- .../kernel/combined_accumulated_data.ts | 8 +-- .../kernel/private_accumulated_data.ts | 8 +-- .../structs/kernel/public_accumulated_data.ts | 8 +-- .../structs/public_circuit_public_inputs.ts | 11 ++- .../src/structs/tx_request.test.ts | 7 ++ .../circuits.js/src/structs/tx_request.ts | 32 +++++---- .../circuits.js/src/tests/factories.ts | 71 ++++++++----------- .../src/benchmarks/bench_tx_size_fees.test.ts | 10 +-- .../src/e2e_account_init_fees.test.ts | 15 ++-- .../src/e2e_dapp_subscription.test.ts | 21 +++--- yarn-project/end-to-end/src/e2e_fees.test.ts | 49 +++++++------ .../entrypoints/src/account_entrypoint.ts | 3 +- .../entrypoints/src/dapp_entrypoint.ts | 3 +- yarn-project/foundation/src/fields/fields.ts | 1 + .../src/__snapshots__/index.test.ts.snap | 4 +- .../src/type_conversion.ts | 27 +++---- .../src/pxe_service/test/pxe_test_suite.ts | 2 + .../global_variable_builder/global_builder.ts | 2 +- .../src/sequencer/abstract_phase_manager.ts | 7 ++ .../src/sequencer/public_processor.test.ts | 3 + .../src/avm/avm_execution_environment.ts | 10 ++- .../simulator/src/avm/avm_machine_state.ts | 18 +++-- .../simulator/src/avm/fixtures/index.ts | 4 +- .../src/client/client_execution_context.ts | 7 +- .../src/client/private_execution.test.ts | 4 ++ .../simulator/src/client/simulator.ts | 7 +- .../simulator/src/public/avm_executor.test.ts | 2 + .../simulator/src/public/execution.ts | 4 ++ yarn-project/simulator/src/public/executor.ts | 12 ++-- .../simulator/src/public/index.test.ts | 22 +++--- .../src/public/public_execution_context.ts | 7 +- .../src/public/transitional_adaptors.ts | 18 +++-- 79 files changed, 626 insertions(+), 368 deletions(-) create mode 100644 noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr delete mode 100644 noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_used.nr create mode 100644 yarn-project/circuits.js/src/structs/gas.ts delete mode 100644 yarn-project/circuits.js/src/structs/gas_used.ts diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index ecc56722a511..9faf3e841804 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -94,7 +94,7 @@ library Constants { uint256 internal constant GAS_SETTINGS_LENGTH = 10; uint256 internal constant DIMENSION_GAS_SETTINGS_LENGTH = 3; uint256 internal constant GAS_FEES_LENGTH = 3; - uint256 internal constant GAS_USED_LENGTH = 3; + uint256 internal constant GAS_LENGTH = 3; uint256 internal constant CONTENT_COMMITMENT_LENGTH = 4; uint256 internal constant CONTRACT_INSTANCE_LENGTH = 6; uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2; @@ -112,10 +112,10 @@ library Constants { uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 6; uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 221; uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 218; - uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 209; + uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 212; uint256 internal constant STATE_REFERENCE_LENGTH = 8; uint256 internal constant TX_CONTEXT_DATA_LENGTH = 4; - uint256 internal constant TX_REQUEST_LENGTH = 8; + uint256 internal constant TX_REQUEST_LENGTH = 18; uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 22; uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674; uint256 internal constant NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048; diff --git a/noir-projects/aztec-nr/aztec/src/context/inputs/public_context_inputs.nr b/noir-projects/aztec-nr/aztec/src/context/inputs/public_context_inputs.nr index 856d57a8085e..fb2f1d27f42c 100644 --- a/noir-projects/aztec-nr/aztec/src/context/inputs/public_context_inputs.nr +++ b/noir-projects/aztec-nr/aztec/src/context/inputs/public_context_inputs.nr @@ -1,6 +1,6 @@ use crate::context::globals::public_global_variables::PublicGlobalVariables; -use dep::protocol_types::{abis::call_context::CallContext, header::Header, traits::Empty}; +use dep::protocol_types::{abis::call_context::CallContext, abis::gas::Gas, header::Header, traits::Empty}; // PublicContextInputs are expected to be provided to each public function // docs:start:public-context-inputs @@ -23,4 +23,4 @@ impl Empty for PublicContextInputs { start_side_effect_counter: 0 as u32, } } -} \ No newline at end of file +} diff --git a/noir-projects/aztec-nr/aztec/src/context/private_context.nr b/noir-projects/aztec-nr/aztec/src/context/private_context.nr index 3c8398ffb614..c8222645c352 100644 --- a/noir-projects/aztec-nr/aztec/src/context/private_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/private_context.nr @@ -10,8 +10,9 @@ use crate::{ }; use dep::protocol_types::{ abis::{ - call_context::CallContext, function_data::FunctionData, function_selector::FunctionSelector, - max_block_number::MaxBlockNumber, nullifier_key_validation_request::NullifierKeyValidationRequest, + gas::Gas, call_context::CallContext, function_data::FunctionData, + function_selector::FunctionSelector, max_block_number::MaxBlockNumber, + nullifier_key_validation_request::NullifierKeyValidationRequest, private_call_stack_item::PrivateCallStackItem, private_circuit_public_inputs::PrivateCircuitPublicInputs, public_call_stack_item::PublicCallStackItem, @@ -28,7 +29,8 @@ use dep::protocol_types::{ }, contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, grumpkin_private_key::GrumpkinPrivateKey, header::Header, - messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{is_empty, Deserialize, Empty} + messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, + traits::{is_empty, Deserialize, Empty} }; // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) @@ -494,7 +496,8 @@ impl PrivateContext { unencrypted_log_preimages_length: 0, historical_header: Header::empty(), prover_address: AztecAddress::zero(), - revert_code: 0 + revert_code: 0, + gas_left: Gas::empty() }, is_execution_request: true }; diff --git a/noir-projects/aztec-nr/aztec/src/context/public_context.nr b/noir-projects/aztec-nr/aztec/src/context/public_context.nr index af98dc9d169a..a12572238504 100644 --- a/noir-projects/aztec-nr/aztec/src/context/public_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/public_context.nr @@ -154,7 +154,8 @@ impl PublicContext { unencrypted_log_preimages_length, historical_header: self.inputs.historical_header, prover_address: self.prover_address, - revert_code: 0 + revert_code: 0, + gas_left: self.inputs.call_context.gas_left }; pub_circuit_pub_inputs } diff --git a/noir-projects/noir-contracts/contracts/gas_token_contract/src/lib.nr b/noir-projects/noir-contracts/contracts/gas_token_contract/src/lib.nr index 9e9786bd2ed3..dc2a42daf392 100644 --- a/noir-projects/noir-contracts/contracts/gas_token_contract/src/lib.nr +++ b/noir-projects/noir-contracts/contracts/gas_token_contract/src/lib.nr @@ -3,6 +3,7 @@ use dep::aztec::context::PublicContext; use dep::aztec::protocol_types::hash::sha256_to_field; pub fn calculate_fee(_context: PublicContext) -> U128 { + // TODO(palla/gas-in-circuits): Use the transaction_fee injected into the context U128::from_integer(1) } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr index 84e68d19d10a..0c225152de13 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr @@ -22,7 +22,7 @@ impl PrivateKernelInitCircuitPrivateInputs { public_inputs.constants = CombinedConstantData { historical_header: self.private_call.call_stack_item.public_inputs.historical_header, tx_context: self.tx_request.tx_context, - gas_settings: GasSettings::empty(), // TODO(palla/gas-in-circuits) + gas_settings: self.tx_request.gas_settings, }; public_inputs.min_revertible_side_effect_counter = self.private_call.call_stack_item.public_inputs.min_revertible_side_effect_counter; } diff --git a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/common.nr b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/common.nr index 1008e0c0f49a..61e96e824efb 100644 --- a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/common.nr +++ b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/common.nr @@ -169,6 +169,18 @@ pub fn update_validation_requests(public_call: PublicCallData, circuit_outputs: propagate_valid_public_data_reads(public_call, circuit_outputs); } +pub fn update_revertible_gas_used(public_call: PublicCallData, circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder) { + circuit_outputs.end.gas_used = circuit_outputs.constants.gas_settings.get_gas_limits() + .sub(public_call.call_stack_item.public_inputs.gas_left) + .sub(circuit_outputs.end_non_revertible.gas_used); +} + +pub fn update_non_revertible_gas_used(public_call: PublicCallData, circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder) { + circuit_outputs.end_non_revertible.gas_used = circuit_outputs.constants.gas_settings.get_gas_limits() + .sub(public_call.call_stack_item.public_inputs.gas_left) + .sub(circuit_outputs.end.gas_used); +} + pub fn update_public_end_non_revertible_values( public_call: PublicCallData, circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder diff --git a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_app_logic.nr b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_app_logic.nr index 05dea2c0e60a..840cda7e921c 100644 --- a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_app_logic.nr +++ b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_app_logic.nr @@ -35,6 +35,8 @@ impl PublicKernelAppLogicCircuitPrivateInputs { common::update_validation_requests(self.public_call, &mut public_inputs); + common::update_revertible_gas_used(self.public_call, &mut public_inputs); + if public_inputs.revert_code == 0 { // Pops the item from the call stack and validates it against the current execution. let call_request = public_inputs.end.public_call_stack.pop(); diff --git a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_setup.nr b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_setup.nr index 761bc370cf49..b3734d21fc8b 100644 --- a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_setup.nr +++ b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_setup.nr @@ -39,6 +39,8 @@ impl PublicKernelSetupCircuitPrivateInputs { // validate the inputs unique to having a previous private kernel self.validate_inputs(); + common::update_non_revertible_gas_used(self.public_call, &mut public_inputs); + // Pops the item from the call stack and validates it against the current execution. let call_request = public_inputs.end_non_revertible.public_call_stack.pop(); common::validate_call_against_request(self.public_call, call_request); diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr index d4576c369db4..23762fd5bf31 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr @@ -41,4 +41,4 @@ mod private_circuit_public_inputs; mod gas_fees; mod gas_settings; -mod gas_used; +mod gas; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr index da9ad715af60..93f148a675a6 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr @@ -2,7 +2,7 @@ use crate::{ abis::{ accumulated_data::public_accumulated_data::PublicAccumulatedData, public_data_update_request::PublicDataUpdateRequest, - side_effect::{SideEffect, SideEffectLinkedToNoteHash}, gas_used::GasUsed + side_effect::{SideEffect, SideEffectLinkedToNoteHash}, gas::Gas }, constants::{ MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX, @@ -26,7 +26,7 @@ struct CombinedAccumulatedData { public_data_update_requests: [PublicDataUpdateRequest; MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], - gas_used: GasUsed, + gas_used: Gas, } impl CombinedAccumulatedData { @@ -59,7 +59,7 @@ impl Empty for CombinedAccumulatedData { encrypted_log_preimages_length: 0, unencrypted_log_preimages_length: 0, public_data_update_requests: [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], - gas_used: GasUsed::empty() + gas_used: Gas::empty() } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data.nr index 2d3304491ddb..708f52a5785e 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data.nr @@ -1,4 +1,4 @@ -use crate::{abis::{call_request::CallRequest, gas_used::GasUsed, side_effect::{SideEffect, SideEffectLinkedToNoteHash}}}; +use crate::{abis::{call_request::CallRequest, gas::Gas, side_effect::{SideEffect, SideEffectLinkedToNoteHash}}}; use crate::constants::{ MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX @@ -20,5 +20,5 @@ struct PrivateAccumulatedData { private_call_stack: [CallRequest; MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX], public_call_stack: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX], - gas_used: GasUsed, + gas_used: Gas, } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data_builder.nr index 078888372fc8..f49529271117 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/private_accumulated_data_builder.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - gas_used::GasUsed, + gas::Gas, accumulated_data::{ combined_accumulated_data::CombinedAccumulatedData, private_accumulated_data::PrivateAccumulatedData, public_accumulated_data::PublicAccumulatedData, @@ -33,7 +33,7 @@ struct PrivateAccumulatedDataBuilder { private_call_stack: BoundedVec, public_call_stack: BoundedVec, - gas_used: GasUsed + gas_used: Gas } impl PrivateAccumulatedDataBuilder { @@ -204,7 +204,7 @@ impl Empty for PrivateAccumulatedDataBuilder { unencrypted_log_preimages_length: 0, private_call_stack: BoundedVec::new(), public_call_stack: BoundedVec::new(), - gas_used: GasUsed::empty(), + gas_used: Gas::empty(), } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr index e1e472a0e2f1..813d86ec94b4 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest, gas_used::GasUsed, + call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest, gas::Gas, side_effect::{SideEffect, SideEffectLinkedToNoteHash} }, constants::{ @@ -27,7 +27,7 @@ struct PublicAccumulatedData { public_call_stack: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX], - gas_used: GasUsed, + gas_used: Gas, } impl Empty for PublicAccumulatedData { @@ -42,7 +42,7 @@ impl Empty for PublicAccumulatedData { unencrypted_log_preimages_length: 0, public_data_update_requests: [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], public_call_stack: [CallRequest::empty(); MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX], - gas_used: GasUsed::empty(), + gas_used: Gas::empty(), } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr index 66cc145413f7..02bb488ff27a 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - gas_used::GasUsed, accumulated_data::public_accumulated_data::PublicAccumulatedData, + gas::Gas, accumulated_data::public_accumulated_data::PublicAccumulatedData, call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest, side_effect::{SideEffect, SideEffectLinkedToNoteHash} }, @@ -28,7 +28,7 @@ struct PublicAccumulatedDataBuilder { public_call_stack: BoundedVec, - gas_used: GasUsed, + gas_used: Gas, } impl PublicAccumulatedDataBuilder { @@ -60,7 +60,7 @@ impl Empty for PublicAccumulatedDataBuilder { unencrypted_log_preimages_length: 0, public_data_update_requests: BoundedVec::new(), public_call_stack: BoundedVec::new(), - gas_used: GasUsed::empty(), + gas_used: Gas::empty(), } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/call_context.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/call_context.nr index ab2c27b2b707..870e4b80df5d 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/call_context.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/call_context.nr @@ -2,7 +2,7 @@ use crate::{ abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress}, constants::{CALL_CONTEXT_LENGTH, GENERATOR_INDEX__CALL_CONTEXT}, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered, - abis::gas_settings::GasSettings, utils::reader::Reader + abis::{gas_settings::GasSettings, gas::Gas}, utils::reader::Reader }; // docs:start:call-context @@ -10,8 +10,8 @@ struct CallContext { msg_sender : AztecAddress, storage_contract_address : AztecAddress, portal_contract_address : EthAddress, - function_selector : FunctionSelector, + gas_left: Gas, is_delegate_call : bool, is_static_call : bool, @@ -53,6 +53,7 @@ impl Serialize for CallContext { serialized.push(self.storage_contract_address.to_field()); serialized.push(self.portal_contract_address.to_field()); serialized.push(self.function_selector.to_field()); + serialized.extend_from_array(self.gas_left.serialize()); serialized.push(self.is_delegate_call as Field); serialized.push(self.is_static_call as Field); serialized.push(self.side_effect_counter as Field); @@ -71,6 +72,7 @@ impl Deserialize for CallContext { storage_contract_address: AztecAddress::from_field(reader.read()), portal_contract_address: EthAddress::from_field(reader.read()), function_selector: FunctionSelector::from_field(reader.read()), + gas_left: reader.read_struct(Gas::deserialize), is_delegate_call: reader.read() as bool, is_static_call: reader.read() as bool, side_effect_counter: reader.read() as u32, @@ -87,6 +89,7 @@ impl Empty for CallContext { storage_contract_address: AztecAddress::empty(), portal_contract_address: EthAddress::empty(), function_selector: FunctionSelector::empty(), + gas_left: Gas::empty(), is_delegate_call: false, is_static_call: false, side_effect_counter: 0, 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 new file mode 100644 index 000000000000..c04b7eea4cde --- /dev/null +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr @@ -0,0 +1,58 @@ +use crate::{ + abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress}, + constants::GAS_LENGTH, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty}, + abis::side_effect::Ordered, utils::reader::Reader +}; + +struct Gas { + da_gas: Field, + l1_gas: Field, + l2_gas: Field, +} + +impl Gas { + fn new(da_gas: Field, l1_gas: Field, l2_gas: Field) -> Self { + Self { da_gas, l1_gas, l2_gas } + } + + fn add(self, other: Gas) -> Self { + Gas::new( + self.da_gas + other.da_gas, + self.l1_gas + other.l1_gas, + self.l2_gas + other.l2_gas + ) + } + + fn sub(self, other: Gas) -> Self { + Gas::new( + self.da_gas - other.da_gas, + self.l1_gas - other.l1_gas, + self.l2_gas - other.l2_gas + ) + } +} + +impl Serialize for Gas { + fn serialize(self) -> [Field; GAS_LENGTH] { + [self.da_gas, self.l1_gas, self.l2_gas] + } +} + +impl Deserialize for Gas { + fn deserialize(serialized: [Field; GAS_LENGTH]) -> Gas { + Gas::new(serialized[0], serialized[1], serialized[2]) + } +} + +impl Eq for Gas { + fn eq(self, other : Gas) -> bool { + (self.da_gas == other.da_gas) & (self.l1_gas == other.l1_gas) & (self.l2_gas == other.l2_gas) + } +} + +impl Empty for Gas { + fn empty() -> Self { + Gas::new(0, 0, 0) + } +} + diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_settings.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_settings.nr index d2fcb6c451ce..17d69cc829d6 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_settings.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_settings.nr @@ -1,5 +1,5 @@ use crate::{ - abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress}, + abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress}, abis::gas::Gas, constants::{GAS_SETTINGS_LENGTH, DIMENSION_GAS_SETTINGS_LENGTH}, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered, utils::reader::Reader }; @@ -20,6 +20,20 @@ impl GasSettings { ) -> Self { Self { da, l1, l2, inclusion_fee } } + + fn get_gas_limits(self) -> Gas { + Gas { + da_gas: self.da.gas_limit as Field, + l1_gas: self.l1.gas_limit as Field, + l2_gas: self.l2.gas_limit as Field + } + } +} + +impl Eq for GasSettings { + fn eq(self, other: Self) -> bool { + (self.da == other.da) & (self.l1 == other.l1) & (self.l2 == other.l2) & (self.inclusion_fee == other.inclusion_fee) + } } impl Empty for GasSettings { @@ -65,11 +79,18 @@ struct DimensionGasSettings { } impl DimensionGasSettings { - fn new(gas_limit: u32, teardown_gas_limit: u32, max_fee_per_gas: Field) -> Self { + pub fn new(gas_limit: u32, teardown_gas_limit: u32, max_fee_per_gas: Field) -> Self { Self { gas_limit, teardown_gas_limit, max_fee_per_gas } } } +impl Eq for DimensionGasSettings { + fn eq(self, other: Self) -> bool { + (self.gas_limit == other.gas_limit) & (self.teardown_gas_limit == other.teardown_gas_limit) & (self.max_fee_per_gas == other.max_fee_per_gas) + } + +} + impl Serialize for DimensionGasSettings { fn serialize(self) -> [Field; DIMENSION_GAS_SETTINGS_LENGTH] { [ diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_used.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_used.nr deleted file mode 100644 index 9ac8a02520f6..000000000000 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/gas_used.nr +++ /dev/null @@ -1,49 +0,0 @@ -use crate::{ - abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress}, - constants::GAS_USED_LENGTH, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty}, - abis::side_effect::Ordered, utils::reader::Reader -}; - -struct GasUsed { - da_gas: Field, - l1_gas: Field, - l2_gas: Field, -} - -impl GasUsed { - fn new(da_gas: Field, l1_gas: Field, l2_gas: Field) -> Self { - Self { da_gas, l1_gas, l2_gas } - } - - fn add(self, other: GasUsed) -> Self { - GasUsed::new( - self.da_gas + other.da_gas, - self.l1_gas + other.l1_gas, - self.l2_gas + other.l2_gas - ) - } -} - -impl Serialize for GasUsed { - fn serialize(self) -> [Field; GAS_USED_LENGTH] { - [self.da_gas, self.l1_gas, self.l2_gas] - } -} - -impl Deserialize for GasUsed { - fn deserialize(serialized: [Field; GAS_USED_LENGTH]) -> GasUsed { - GasUsed::new(serialized[0], serialized[1], serialized[2]) - } -} - -impl Eq for GasUsed { - fn eq(self, other : GasUsed) -> bool { - (self.da_gas == other.da_gas) & (self.l1_gas == other.l1_gas) & (self.l2_gas == other.l2_gas) - } -} - -impl Empty for GasUsed { - fn empty() -> Self { - GasUsed::new(0, 0, 0) - } -} diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr index 76f1c28c4952..1f5b5f9fa52c 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item.nr @@ -69,7 +69,7 @@ mod tests { let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: true, function_data }; // Value from public_call_stack_item.test.ts "Computes a callstack item request hash" test - let test_data_call_stack_item_request_hash = 0x151bc9ee42eb63112fb2a350dcaa33c4c4b81cc37ded8773e785f47029f35983; + let test_data_call_stack_item_request_hash = 0x017e4a2d67fcd5e2d3f1e42302b2c8450ebb4ebc94add68807dc36a2d679e05a; assert_eq(call_stack_item.hash(), test_data_call_stack_item_request_hash); } @@ -87,7 +87,7 @@ mod tests { let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: false, function_data }; // Value from public_call_stack_item.test.ts "Computes a callstack item hash" test - let test_data_call_stack_item_hash = 0x1a7b9d0cd965f512a3b3ed70333198a2a69bd4f9e70be68379c54e68a7b07a4c; + let test_data_call_stack_item_hash = 0x2db2c0292bb5eb65ba450eb69cf56735b40e484e8ab607459bb897aa31427ebb; assert_eq(call_stack_item.hash(), test_data_call_stack_item_hash); } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_circuit_public_inputs.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_circuit_public_inputs.nr index 3a6182880db8..b4b6e9acffbe 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_circuit_public_inputs.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_circuit_public_inputs.nr @@ -1,7 +1,7 @@ use crate::{ abis::{ call_context::CallContext, read_request::ReadRequest, - side_effect::{SideEffect, SideEffectLinkedToNoteHash} + side_effect::{SideEffect, SideEffectLinkedToNoteHash}, gas::Gas }, address::AztecAddress, constants::{ @@ -49,6 +49,9 @@ struct PublicCircuitPublicInputs{ prover_address: AztecAddress, revert_code: u8, + + // gas left after execution is completed + gas_left: Gas, } impl Eq for PublicCircuitPublicInputs { @@ -95,6 +98,7 @@ impl Serialize for PublicCircuitPublicInput fields.extend_from_array(self.historical_header.serialize()); fields.push(self.prover_address.to_field()); fields.push(self.revert_code as Field); + fields.extend_from_array(self.gas_left.serialize()); fields.storage } } @@ -122,6 +126,7 @@ impl Deserialize for PublicCircuitPublicInp historical_header: reader.read_struct(Header::deserialize), prover_address: reader.read_struct(AztecAddress::deserialize), revert_code: reader.read() as u8, + gas_left: reader.read_struct(Gas::deserialize), }; reader.finish(); @@ -156,6 +161,7 @@ impl Empty for PublicCircuitPublicInputs { historical_header: Header::empty(), prover_address: AztecAddress::zero(), revert_code: 0 as u8, + gas_left: Gas::empty(), } } } @@ -174,6 +180,6 @@ fn empty_hash() { let hash = inputs.hash(); // Value from public_circuit_public_inputs.test.ts "computes empty item hash" test - let test_data_empty_hash = 0x2745ec62624afeb19b86af3d440db1f8c3432e1d17a074c75cb8f44999fd3fae; + let test_data_empty_hash = 0x23c9b1f89ddf144a2751a74a9179ca41bb8426654fdbb8a69b8f3fad94dbeac6; assert_eq(hash, test_data_empty_hash); } 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 a25b45b2226c..1210dc1f2727 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -136,11 +136,11 @@ global VIEW_NOTE_ORACLE_RETURN_LENGTH: u64 = 212; // LENGTH OF STRUCTS SERIALIZED TO FIELDS global AZTEC_ADDRESS_LENGTH = 1; -global CALL_CONTEXT_LENGTH: u64 = 18; // 8 + GAS_SETTINGS_LENGTH +global CALL_CONTEXT_LENGTH: u64 = 21; // 8 + GAS_SETTINGS_LENGTH + GAS_LENGTH global GAS_SETTINGS_LENGTH: u64 = 10; // 1 + 3 * DIMENSION_GAS_SETTINGS_LENGTH global DIMENSION_GAS_SETTINGS_LENGTH: u64 = 3; global GAS_FEES_LENGTH: u64 = 3; -global GAS_USED_LENGTH: u64 = 3; +global GAS_LENGTH: u64 = 3; global CONTENT_COMMITMENT_LENGTH: u64 = 4; global CONTRACT_INSTANCE_LENGTH: u64 = 6; global CONTRACT_STORAGE_READ_LENGTH: u64 = 2; @@ -156,18 +156,18 @@ global MAX_BLOCK_NUMBER_LENGTH: u64 = 2; // 1 for the option flag, 1 for the val global NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4; global NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5; global PARTIAL_STATE_REFERENCE_LENGTH: u64 = 6; -global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 221; +global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 224; // Change this ONLY if you have changed the PrivateCircuitPublicInputs structure. // In other words, if the structure/size of the public inputs of a function call changes then we should change this // constant as well PRIVATE_CALL_STACK_ITEM_LENGTH -global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 218; +global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 221; // Change this ONLY if you have changed the PublicCircuitPublicInputs structure. -global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 209; +global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: u64 = 215; global STATE_REFERENCE_LENGTH: u64 = 8; // 2 for snap + 8 for partial global TX_CONTEXT_DATA_LENGTH: u64 = 4; -global TX_REQUEST_LENGTH: u64 = 8; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH +global TX_REQUEST_LENGTH: u64 = 18; // 2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH + GAS_SETTINGS_LENGTH -global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 22; // 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH +global ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH: Field = 25; // 2 + FUNCTION_DATA_LENGTH + CALL_CONTEXT_LENGTH global GET_NOTES_ORACLE_RETURN_LENGTH: u64 = 674; global NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr index bf56794334de..9c6a861eddea 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - gas_used::GasUsed, gas_settings::GasSettings, call_context::CallContext, + gas::Gas, gas_settings::GasSettings, call_context::CallContext, call_request::{CallerContext, CallRequest}, accumulated_data::{ CombinedAccumulatedData, PrivateAccumulatedData, PrivateAccumulatedDataBuilder, @@ -47,7 +47,7 @@ struct FixtureBuilder { public_data_update_requests: BoundedVec, private_call_stack: BoundedVec, public_call_stack: BoundedVec, - gas_used: GasUsed, + gas_used: Gas, // Validation requests. max_block_number: MaxBlockNumber, @@ -101,7 +101,7 @@ impl FixtureBuilder { revert_code: 0, min_revertible_side_effect_counter: 0, counter: 0, - gas_used: GasUsed::empty(), + gas_used: Gas::empty(), gas_settings: GasSettings::empty() } } @@ -420,7 +420,7 @@ impl Empty for FixtureBuilder { min_revertible_side_effect_counter: 0, counter: 0, gas_settings: GasSettings::empty(), - gas_used: GasUsed::empty(), + gas_used: Gas::empty(), } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_call_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_call_data_builder.nr index 7ed7b0de8187..6063bef7866c 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_call_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_call_data_builder.nr @@ -1,7 +1,8 @@ use crate::{ abis::{ - call_request::{CallerContext, CallRequest}, private_call_stack_item::PrivateCallStackItem, - function_data::FunctionData, max_block_number::MaxBlockNumber, + gas_settings::GasSettings, call_request::{CallerContext, CallRequest}, + private_call_stack_item::PrivateCallStackItem, function_data::FunctionData, + max_block_number::MaxBlockNumber, membership_witness::{FunctionLeafMembershipWitness, NoteHashReadRequestMembershipWitness}, private_circuit_public_inputs::PrivateCircuitPublicInputs, private_kernel::private_call_data::PrivateCallData @@ -38,6 +39,7 @@ struct PrivateCallDataBuilder { note_hash_read_request_membership_witnesses: BoundedVec, portal_contract_address: EthAddress, acir_hash: Field, + gas_settings: GasSettings, } impl PrivateCallDataBuilder { @@ -64,7 +66,8 @@ impl PrivateCallDataBuilder { contract_class_public_bytecode_commitment: contract_data.public_bytecode_commitment, note_hash_read_request_membership_witnesses: BoundedVec::new(), portal_contract_address: public_inputs.call_context.portal_contract_address, - acir_hash: contract_function.acir_hash + acir_hash: contract_function.acir_hash, + gas_settings: public_inputs.call_context.gas_settings } } @@ -87,7 +90,8 @@ impl PrivateCallDataBuilder { origin: self.contract_address, args_hash: self.public_inputs.args_hash, tx_context, - function_data: self.function_data + function_data: self.function_data, + gas_settings: self.gas_settings } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr index 56d4175f62cf..5858a61e8a2f 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - call_context::CallContext, gas_settings::{GasSettings, DimensionGasSettings}, + call_context::CallContext, gas_settings::{GasSettings, DimensionGasSettings}, gas::Gas, max_block_number::MaxBlockNumber, nullifier_key_validation_request::NullifierKeyValidationRequest, private_circuit_public_inputs::PrivateCircuitPublicInputs, read_request::ReadRequest, side_effect::{SideEffect, SideEffectLinkedToNoteHash} @@ -14,7 +14,7 @@ use crate::{ MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, MAX_NEW_NOTE_HASHES_PER_CALL, MAX_NEW_NULLIFIERS_PER_CALL, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL - }, +}, traits::Empty }; @@ -73,12 +73,8 @@ impl PrivateCircuitPublicInputsBuilder { is_delegate_call: false, is_static_call: false, side_effect_counter: 0, - gas_settings: GasSettings { - da: DimensionGasSettings::new(0, 0, 0), - l1: DimensionGasSettings::new(0, 0, 0), - l2: DimensionGasSettings::new(0, 0, 0), - inclusion_fee: 0 - }, + gas_left: Gas::empty(), + gas_settings: GasSettings::empty(), transaction_fee: 0 }; public_inputs.call_context = call_context; @@ -143,4 +139,4 @@ impl Empty for PrivateCircuitPublicInputsBuilder { version: 0, } } -} \ No newline at end of file +} diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr index 7338a7263635..5efc84e71dfb 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr @@ -1,8 +1,9 @@ use crate::{ abis::{ - gas_settings::GasSettings, call_context::CallContext, call_request::{CallerContext, CallRequest}, - function_data::FunctionData, public_call_data::PublicCallData, - public_call_stack_item::PublicCallStackItem, public_circuit_public_inputs::PublicCircuitPublicInputs + gas_settings::GasSettings, gas::Gas, call_context::CallContext, + call_request::{CallerContext, CallRequest}, function_data::FunctionData, + public_call_data::PublicCallData, public_call_stack_item::PublicCallStackItem, + public_circuit_public_inputs::PublicCircuitPublicInputs }, address::{AztecAddress, EthAddress}, contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, mocked::Proof, @@ -45,6 +46,7 @@ impl PublicCallDataBuilder { side_effect_counter: 0, // needed? gas_settings: GasSettings::empty(), transaction_fee: 0, + gas_left: Gas::empty(), }; PublicCallDataBuilder { diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_circuit_public_inputs_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_circuit_public_inputs_builder.nr index 2898875fce68..c719774093e0 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_circuit_public_inputs_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_circuit_public_inputs_builder.nr @@ -1,6 +1,6 @@ use crate::{ abis::{ - call_context::CallContext, public_circuit_public_inputs::PublicCircuitPublicInputs, + gas::Gas, call_context::CallContext, public_circuit_public_inputs::PublicCircuitPublicInputs, read_request::ReadRequest, side_effect::{SideEffect, SideEffectLinkedToNoteHash} }, address::AztecAddress, @@ -14,7 +14,7 @@ use crate::{ MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_DATA_READS_PER_CALL, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL }, -traits::Empty, + traits::Empty }; struct PublicCircuitPublicInputsBuilder { @@ -36,6 +36,7 @@ struct PublicCircuitPublicInputsBuilder { historical_header: Header, prover_address: AztecAddress, revert_code: u8, + gas_left: Gas, } impl PublicCircuitPublicInputsBuilder { @@ -65,7 +66,8 @@ impl PublicCircuitPublicInputsBuilder { unencrypted_log_preimages_length: self.unencrypted_log_preimages_length, historical_header: self.historical_header, prover_address: self.prover_address, - revert_code: self.revert_code + revert_code: self.revert_code, + gas_left: self.gas_left } } } @@ -91,6 +93,7 @@ impl Empty for PublicCircuitPublicInputsBuilder { historical_header: Header::empty(), prover_address: AztecAddress::zero(), revert_code: 0 as u8, + gas_left: Gas::empty(), } } -} \ No newline at end of file +} diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr b/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr index 74be0805af57..a1a9a8649b11 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_request.nr @@ -1,7 +1,8 @@ use crate::{ - address::AztecAddress, abis::function_data::FunctionData, + address::AztecAddress, abis::function_data::FunctionData, abis::gas_settings::GasSettings, constants::{GENERATOR_INDEX__TX_REQUEST, TX_REQUEST_LENGTH}, hash::pedersen_hash, - traits::{Hash, Serialize, Deserialize, Empty}, transaction::tx_context::TxContext, utils::reader::Reader + traits::{Hash, Serialize, Deserialize, Empty}, transaction::tx_context::TxContext, + utils::reader::Reader }; struct TxRequest { @@ -9,6 +10,7 @@ struct TxRequest { args_hash: Field, tx_context: TxContext, function_data: FunctionData, + gas_settings: GasSettings, } impl Empty for TxRequest { @@ -17,7 +19,8 @@ impl Empty for TxRequest { origin: AztecAddress::empty(), args_hash: 0, tx_context: TxContext::empty(), - function_data: FunctionData::empty() + function_data: FunctionData::empty(), + gas_settings: GasSettings::empty(), } } } @@ -27,7 +30,8 @@ impl Eq for TxRequest { (self.origin == other.origin) & (self.args_hash == other.args_hash) & (self.tx_context == other.tx_context) & - (self.function_data == other.function_data) + (self.function_data == other.function_data) & + (self.gas_settings == other.gas_settings) } } @@ -46,6 +50,7 @@ impl Serialize for TxRequest { fields.extend_from_array(self.function_data.serialize()); fields.push(self.args_hash); fields.extend_from_array(self.tx_context.serialize()); + fields.extend_from_array(self.gas_settings.serialize()); assert_eq(fields.len(), TX_REQUEST_LENGTH); @@ -62,6 +67,7 @@ impl Deserialize for TxRequest { args_hash: reader.read(), tx_context: reader.read_struct(TxContext::deserialize), function_data: reader.read_struct(FunctionData::deserialize), + gas_settings: reader.read_struct(GasSettings::deserialize), }; reader.finish(); @@ -71,7 +77,10 @@ impl Deserialize for TxRequest { mod tests { use crate::{ - abis::{function_selector::FunctionSelector, function_data::FunctionData}, + abis::{ + function_selector::FunctionSelector, function_data::FunctionData, + gas_settings::{GasSettings, DimensionGasSettings} + }, address::{AztecAddress, EthAddress}, contract_class_id::ContractClassId, grumpkin_point::GrumpkinPoint, transaction::{tx_request::TxRequest, tx_context::TxContext} }; @@ -90,10 +99,16 @@ mod tests { origin: AztecAddress::from_field(1), args_hash: 3, tx_context: TxContext { is_fee_payment_tx: false, is_rebate_payment_tx: false, chain_id: 0, version: 0 }, - function_data: FunctionData { selector: FunctionSelector::from_u32(2), is_private: true } + function_data: FunctionData { selector: FunctionSelector::from_u32(2), is_private: true }, + gas_settings: GasSettings { + da: DimensionGasSettings { gas_limit: 1, teardown_gas_limit: 2, max_fee_per_gas: 3 }, + l1: DimensionGasSettings { gas_limit: 1, teardown_gas_limit: 2, max_fee_per_gas: 3 }, + l2: DimensionGasSettings { gas_limit: 1, teardown_gas_limit: 2, max_fee_per_gas: 3 }, + inclusion_fee: 10 + } }; // Value from tx_request.test.ts "compute hash" test - let test_data_tx_request_hash = 0x20af6f595c396494f1177fa196d17e98d55a2416b28c262b76e78a36d6c01daa; + let test_data_tx_request_hash = 0x1acb7ccc17b87f966229cb5afc3c539c65bf235cb464a38ab6224f04594e2fa6; assert(tx_request.hash() == test_data_tx_request_hash); } } diff --git a/yarn-project/aztec.js/src/contract/contract_function_interaction.ts b/yarn-project/aztec.js/src/contract/contract_function_interaction.ts index 897bd6ba1b90..5f8033124dba 100644 --- a/yarn-project/aztec.js/src/contract/contract_function_interaction.ts +++ b/yarn-project/aztec.js/src/contract/contract_function_interaction.ts @@ -1,5 +1,5 @@ import { type FunctionCall, PackedValues, TxExecutionRequest } from '@aztec/circuit-types'; -import { type AztecAddress, FunctionData, TxContext } from '@aztec/circuits.js'; +import { type AztecAddress, FunctionData, GasSettings, TxContext } from '@aztec/circuits.js'; import { type FunctionAbi, FunctionType, encodeArguments } from '@aztec/foundation/abi'; import { type Wallet } from '../account/wallet.js'; @@ -13,10 +13,10 @@ export { SendMethodOptions }; * Disregarded for simulation of public functions */ export type SimulateMethodOptions = { - /** - * The sender's Aztec address. - */ + /** The sender's Aztec address. */ from?: AztecAddress; + /** Gas settings for the simulation. */ + gasSettings?: GasSettings; }; /** @@ -101,6 +101,7 @@ export class ContractFunctionInteraction extends BaseContractInteraction { txContext: TxContext.empty(nodeInfo.chainId, nodeInfo.protocolVersion), packedArguments: [packedArgs], authWitnesses: [], + gasSettings: options.gasSettings ?? GasSettings.simulation(), }); const simulatedTx = await this.pxe.simulateTx(txRequest, false, options.from ?? this.wallet.getAddress()); return simulatedTx.privateReturnValues?.[0]; diff --git a/yarn-project/aztec.js/src/entrypoint/default_entrypoint.ts b/yarn-project/aztec.js/src/entrypoint/default_entrypoint.ts index 18bc21b369cd..1596a1c6c02e 100644 --- a/yarn-project/aztec.js/src/entrypoint/default_entrypoint.ts +++ b/yarn-project/aztec.js/src/entrypoint/default_entrypoint.ts @@ -1,5 +1,5 @@ import { PackedValues, TxExecutionRequest } from '@aztec/circuit-types'; -import { TxContext } from '@aztec/circuits.js'; +import { GasSettings, TxContext } from '@aztec/circuits.js'; import { type EntrypointInterface, type ExecutionRequestInit } from './entrypoint.js'; @@ -27,6 +27,7 @@ export class DefaultEntrypoint implements EntrypointInterface { txContext, [...packedArguments, entrypointPackedValues], authWitnesses, + exec.fee?.gasSettings ?? GasSettings.default(), ), ); } diff --git a/yarn-project/aztec.js/src/entrypoint/default_multi_call_entrypoint.ts b/yarn-project/aztec.js/src/entrypoint/default_multi_call_entrypoint.ts index 39501247b81e..968960c4671b 100644 --- a/yarn-project/aztec.js/src/entrypoint/default_multi_call_entrypoint.ts +++ b/yarn-project/aztec.js/src/entrypoint/default_multi_call_entrypoint.ts @@ -1,6 +1,6 @@ import { type EntrypointInterface, EntrypointPayload, type ExecutionRequestInit } from '@aztec/aztec.js/entrypoint'; import { PackedValues, TxExecutionRequest } from '@aztec/circuit-types'; -import { type AztecAddress, FunctionData, TxContext } from '@aztec/circuits.js'; +import { type AztecAddress, FunctionData, GasSettings, TxContext } from '@aztec/circuits.js'; import { type FunctionAbi, encodeArguments } from '@aztec/foundation/abi'; import { getCanonicalMultiCallEntrypointAddress } from '@aztec/protocol-contracts/multi-call-entrypoint'; @@ -27,6 +27,7 @@ export class DefaultMultiCallEntrypoint implements EntrypointInterface { txContext: TxContext.empty(this.chainId, this.version), packedArguments: [...payload.packedArguments, ...packedArguments, entrypointPackedArgs], authWitnesses, + gasSettings: executions.fee?.gasSettings ?? GasSettings.default(), }); return Promise.resolve(txRequest); diff --git a/yarn-project/aztec.js/src/entrypoint/payload.ts b/yarn-project/aztec.js/src/entrypoint/payload.ts index acebdc4544f5..c4bfb965aaab 100644 --- a/yarn-project/aztec.js/src/entrypoint/payload.ts +++ b/yarn-project/aztec.js/src/entrypoint/payload.ts @@ -1,5 +1,5 @@ import { type FunctionCall, PackedValues, emptyFunctionCall } from '@aztec/circuit-types'; -import { Fr, GeneratorIndex } from '@aztec/circuits.js'; +import { Fr, type GasSettings, GeneratorIndex } from '@aztec/circuits.js'; import { padArrayEnd } from '@aztec/foundation/collection'; import { pedersenHash } from '@aztec/foundation/crypto'; import { type Tuple } from '@aztec/foundation/serialize'; @@ -12,8 +12,8 @@ import { type FeePaymentMethod } from '../fee/fee_payment_method.js'; export type FeeOptions = { /** The fee payment method to use */ paymentMethod: FeePaymentMethod; - /** The fee limit to pay */ - maxFee: bigint | number | Fr; + /** The gas settings */ + gasSettings: GasSettings; }; // These must match the values defined in: @@ -137,7 +137,7 @@ export class EntrypointPayload { * @returns The execution payload */ static async fromFeeOptions(feeOpts?: FeeOptions) { - const calls = feeOpts ? await feeOpts.paymentMethod.getFunctionCalls(new Fr(feeOpts.maxFee)) : []; + const calls = feeOpts ? await feeOpts.paymentMethod.getFunctionCalls(feeOpts?.gasSettings) : []; const paddedCalls = padArrayEnd(calls, emptyFunctionCall(), FEE_MAX_CALLS); return new EntrypointPayload(paddedCalls, GeneratorIndex.FEE_PAYLOAD); } diff --git a/yarn-project/aztec.js/src/fee/fee_payment_method.ts b/yarn-project/aztec.js/src/fee/fee_payment_method.ts index cc67345b2c07..62c4f0a353e1 100644 --- a/yarn-project/aztec.js/src/fee/fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/fee_payment_method.ts @@ -1,6 +1,6 @@ import { type FunctionCall } from '@aztec/circuit-types'; +import { type GasSettings } from '@aztec/circuits.js'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; -import { type Fr } from '@aztec/foundation/fields'; /** * Holds information about how the fee for a transaction is to be paid. @@ -17,9 +17,8 @@ export interface FeePaymentMethod { /** * Creates a function call to pay the fee in the given asset. - * TODO(fees) replace maxFee with gas limits - * @param maxFee - The maximum fee to be paid in the given asset. + * @param gasSettings - The gas limits and max fees. * @returns The function call to pay the fee. */ - getFunctionCalls(maxFee: Fr): Promise; + getFunctionCalls(gasSettings: GasSettings): Promise; } diff --git a/yarn-project/aztec.js/src/fee/native_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/native_fee_payment_method.ts index f1b77c2037c8..af5a9ac3b312 100644 --- a/yarn-project/aztec.js/src/fee/native_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/native_fee_payment_method.ts @@ -1,7 +1,6 @@ import { type FunctionCall } from '@aztec/circuit-types'; -import { type AztecAddress, FunctionData } from '@aztec/circuits.js'; +import { type AztecAddress, FunctionData, type GasSettings } from '@aztec/circuits.js'; import { FunctionSelector } from '@aztec/foundation/abi'; -import { type Fr } from '@aztec/foundation/fields'; import { getCanonicalGasTokenAddress } from '@aztec/protocol-contracts/gas-token'; import { type Wallet } from '../account/wallet.js'; @@ -47,16 +46,16 @@ export class NativeFeePaymentMethod implements FeePaymentMethod { } /** - * Creates a function call to pay the fee in gas token.. - * @param feeLimit - The maximum fee to be paid in gas token. + * Creates a function call to pay the fee in gas token. + * @param gasSettings - The gas settings. * @returns A function call */ - getFunctionCalls(feeLimit: Fr): Promise { + getFunctionCalls(gasSettings: GasSettings): Promise { return Promise.resolve([ { to: this.#gasTokenAddress, functionData: new FunctionData(FunctionSelector.fromSignature('pay_fee(Field)'), false), - args: [feeLimit], + args: [gasSettings.getFeeLimit()], }, ]); } diff --git a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts index ce4e3ae12424..e6abb694172f 100644 --- a/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/private_fee_payment_method.ts @@ -1,5 +1,5 @@ import { type FunctionCall } from '@aztec/circuit-types'; -import { FunctionData } from '@aztec/circuits.js'; +import { FunctionData, type GasSettings } from '@aztec/circuits.js'; import { computeMessageSecretHash } from '@aztec/circuits.js/hash'; import { FunctionSelector } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; @@ -53,11 +53,12 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod { /** * Creates a function call to pay the fee in the given asset. - * @param maxFee - The maximum fee to be paid in the given asset. + * @param gasSettings - The gas settings. * @returns The function call to pay the fee. */ - async getFunctionCalls(maxFee: Fr): Promise { + async getFunctionCalls(gasSettings: GasSettings): Promise { const nonce = Fr.random(); + const maxFee = gasSettings.getFeeLimit(); const messageHash = computeAuthWitMessageHash( this.paymentContract, this.wallet.getChainId(), diff --git a/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts b/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts index 9dbd4b416d6f..33b2430ebeeb 100644 --- a/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts +++ b/yarn-project/aztec.js/src/fee/public_fee_payment_method.ts @@ -1,5 +1,5 @@ import { type FunctionCall } from '@aztec/circuit-types'; -import { FunctionData } from '@aztec/circuits.js'; +import { FunctionData, type GasSettings } from '@aztec/circuits.js'; import { FunctionSelector } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr } from '@aztec/foundation/fields'; @@ -46,12 +46,12 @@ export class PublicFeePaymentMethod implements FeePaymentMethod { /** * Creates a function call to pay the fee in the given asset. - * @param maxFee - The maximum fee to be paid in the given asset. + * @param gasSettings - The gas settings. * @returns The function call to pay the fee. */ - getFunctionCalls(maxFee: Fr): Promise { + getFunctionCalls(gasSettings: GasSettings): Promise { const nonce = Fr.random(); - + const maxFee = gasSettings.getFeeLimit(); const messageHash = computeAuthWitMessageHash( this.paymentContract, this.wallet.getChainId(), diff --git a/yarn-project/circuit-types/src/tx_execution_request.ts b/yarn-project/circuit-types/src/tx_execution_request.ts index eba0eda27d87..9169dfe0ceea 100644 --- a/yarn-project/circuit-types/src/tx_execution_request.ts +++ b/yarn-project/circuit-types/src/tx_execution_request.ts @@ -1,4 +1,4 @@ -import { AztecAddress, Fr, FunctionData, TxContext, TxRequest, Vector } from '@aztec/circuits.js'; +import { AztecAddress, Fr, FunctionData, GasSettings, TxContext, TxRequest, Vector } from '@aztec/circuits.js'; import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; import { type FieldsOf } from '@aztec/foundation/types'; @@ -38,10 +38,13 @@ export class TxExecutionRequest { * These witnesses are not expected to be stored in the local witnesses database of the PXE. */ public authWitnesses: AuthWitness[], + + /** Gas choices for this transaction. */ + public gasSettings: GasSettings, ) {} toTxRequest(): TxRequest { - return new TxRequest(this.origin, this.functionData, this.argsHash, this.txContext); + return new TxRequest(this.origin, this.functionData, this.argsHash, this.txContext, this.gasSettings); } static getFields(fields: FieldsOf) { @@ -52,6 +55,7 @@ export class TxExecutionRequest { fields.txContext, fields.packedArguments, fields.authWitnesses, + fields.gasSettings, ] as const; } @@ -71,6 +75,7 @@ export class TxExecutionRequest { this.txContext, new Vector(this.packedArguments), new Vector(this.authWitnesses), + this.gasSettings, ); } @@ -96,6 +101,7 @@ export class TxExecutionRequest { reader.readObject(TxContext), reader.readVector(PackedValues), reader.readVector(AuthWitness), + reader.readObject(GasSettings), ); } diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index 5a8cb29928c9..baa757643877 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -79,7 +79,7 @@ export const CALL_CONTEXT_LENGTH = 18; export const GAS_SETTINGS_LENGTH = 10; export const DIMENSION_GAS_SETTINGS_LENGTH = 3; export const GAS_FEES_LENGTH = 3; -export const GAS_USED_LENGTH = 3; +export const GAS_LENGTH = 3; export const CONTENT_COMMITMENT_LENGTH = 4; export const CONTRACT_INSTANCE_LENGTH = 6; export const CONTRACT_STORAGE_READ_LENGTH = 2; @@ -97,10 +97,10 @@ export const NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5; export const PARTIAL_STATE_REFERENCE_LENGTH = 6; export const PRIVATE_CALL_STACK_ITEM_LENGTH = 221; export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 218; -export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 209; +export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 212; export const STATE_REFERENCE_LENGTH = 8; export const TX_CONTEXT_DATA_LENGTH = 4; -export const TX_REQUEST_LENGTH = 8; +export const TX_REQUEST_LENGTH = 18; export const ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 22; export const GET_NOTES_ORACLE_RETURN_LENGTH = 674; export const NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048; diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/public_call_stack_item.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/public_call_stack_item.test.ts.snap index 589fe9bb59e9..86dd1bd0f4c6 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/public_call_stack_item.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/public_call_stack_item.test.ts.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x1a7b9d0cd965f512a3b3ed70333198a2a69bd4f9e70be68379c54e68a7b07a4c"`; +exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x2db2c0292bb5eb65ba450eb69cf56735b40e484e8ab607459bb897aa31427ebb"`; -exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x151bc9ee42eb63112fb2a350dcaa33c4c4b81cc37ded8773e785f47029f35983"`; +exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x017e4a2d67fcd5e2d3f1e42302b2c8450ebb4ebc94add68807dc36a2d679e05a"`; -exports[`PublicCallStackItem computes hash 1`] = `Fr<0x107c825cf4cf15d2618c5828eced84edc7dd29c277ff1f6171c6354237174b7a>`; +exports[`PublicCallStackItem computes hash 1`] = `Fr<0x07a6d26dfcbda230468659b24a08340c99c526965b1a97768158724ea84a5605>`; diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/public_circuit_public_inputs.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/public_circuit_public_inputs.test.ts.snap index 962240da2684..345e505dfdca 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/public_circuit_public_inputs.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/public_circuit_public_inputs.test.ts.snap @@ -1,5 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`PublicCircuitPublicInputs computes empty item hash 1`] = `Fr<0x2745ec62624afeb19b86af3d440db1f8c3432e1d17a074c75cb8f44999fd3fae>`; +exports[`PublicCircuitPublicInputs computes empty item hash 1`] = `Fr<0x23c9b1f89ddf144a2751a74a9179ca41bb8426654fdbb8a69b8f3fad94dbeac6>`; -exports[`PublicCircuitPublicInputs hash matches snapshot 1`] = `Fr<0x0082fbb87371d9f98bb2d7c0b6e8f5e12f21f67085b5bd42931596f3d2e5eb9b>`; +exports[`PublicCircuitPublicInputs hash matches snapshot 1`] = `Fr<0x1843df93d439f01bc709da7710b2d85e64fa4d2f4ffbfb405b87a92accf6c553>`; diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/tx_request.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/tx_request.test.ts.snap index 06f830fd9cb6..9edf2d226eb4 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/tx_request.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/tx_request.test.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`TxRequest compute hash 1`] = `"0x20af6f595c396494f1177fa196d17e98d55a2416b28c262b76e78a36d6c01daa"`; +exports[`TxRequest compute hash 1`] = `"0x1acb7ccc17b87f966229cb5afc3c539c65bf235cb464a38ab6224f04594e2fa6"`; diff --git a/yarn-project/circuits.js/src/structs/call_context.ts b/yarn-project/circuits.js/src/structs/call_context.ts index 8a3326759883..b542a46b1e06 100644 --- a/yarn-project/circuits.js/src/structs/call_context.ts +++ b/yarn-project/circuits.js/src/structs/call_context.ts @@ -6,6 +6,7 @@ import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from import { type FieldsOf } from '@aztec/foundation/types'; import { CALL_CONTEXT_LENGTH } from '../constants.gen.js'; +import { Gas } from './gas.js'; import { GasSettings } from './gas_settings.js'; /** @@ -31,6 +32,8 @@ export class CallContext { * Function selector of the function being called. */ public functionSelector: FunctionSelector, + /** How much gas is available for execution of this function. */ + public gasLeft: Gas, /** * Determines whether the call is a delegate call (see Ethereum's delegate call opcode for more information). */ @@ -61,6 +64,7 @@ export class CallContext { AztecAddress.ZERO, EthAddress.ZERO, FunctionSelector.empty(), + Gas.empty(), false, false, 0, @@ -75,6 +79,7 @@ export class CallContext { this.storageContractAddress.isZero() && this.portalContractAddress.isZero() && this.functionSelector.isEmpty() && + this.gasLeft.isEmpty() && Fr.ZERO && this.gasSettings.isEmpty() && this.transactionFee.isZero() @@ -91,6 +96,7 @@ export class CallContext { fields.storageContractAddress, fields.portalContractAddress, fields.functionSelector, + fields.gasLeft, fields.isDelegateCall, fields.isStaticCall, fields.sideEffectCounter, @@ -129,6 +135,7 @@ export class CallContext { reader.readObject(AztecAddress), reader.readObject(EthAddress), reader.readObject(FunctionSelector), + reader.readObject(Gas), reader.readBoolean(), reader.readBoolean(), reader.readNumber(), @@ -144,6 +151,7 @@ export class CallContext { reader.readObject(AztecAddress), reader.readObject(EthAddress), reader.readObject(FunctionSelector), + reader.readObject(Gas), reader.readBoolean(), reader.readBoolean(), reader.readU32(), @@ -158,6 +166,7 @@ export class CallContext { callContext.storageContractAddress.equals(this.storageContractAddress) && callContext.portalContractAddress.equals(this.portalContractAddress) && callContext.functionSelector.equals(this.functionSelector) && + callContext.gasLeft.equals(this.gasLeft) && callContext.isDelegateCall === this.isDelegateCall && callContext.isStaticCall === this.isStaticCall && callContext.sideEffectCounter === this.sideEffectCounter && diff --git a/yarn-project/circuits.js/src/structs/gas.ts b/yarn-project/circuits.js/src/structs/gas.ts new file mode 100644 index 000000000000..54795c86a52a --- /dev/null +++ b/yarn-project/circuits.js/src/structs/gas.ts @@ -0,0 +1,63 @@ +import { type Fr } from '@aztec/foundation/fields'; +import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from '@aztec/foundation/serialize'; +import { type FieldsOf } from '@aztec/foundation/types'; + +import { inspect } from 'util'; + +import { type UInt32 } from './shared.js'; + +/** Gas amounts in each dimension. */ +export class Gas { + constructor(public readonly daGas: UInt32, public readonly l1Gas: UInt32, public readonly l2Gas: UInt32) {} + + equals(other: Gas) { + return this.daGas === other.daGas && this.l1Gas === other.l1Gas && this.l2Gas === other.l2Gas; + } + + static from(fields: FieldsOf) { + return new Gas(fields.daGas, fields.l1Gas, fields.l2Gas); + } + + static empty() { + return new Gas(0, 0, 0); + } + + /** Returns large enough gas amounts for testing purposes. */ + static test() { + return new Gas(1e12, 1e12, 1e12); + } + + isEmpty() { + return this.daGas === 0 && this.l1Gas === 0 && this.l2Gas === 0; + } + + static fromBuffer(buffer: Buffer | BufferReader): Gas { + const reader = BufferReader.asReader(buffer); + return new Gas(reader.readNumber(), reader.readNumber(), reader.readNumber()); + } + + toBuffer() { + return serializeToBuffer(this.daGas, this.l1Gas, this.l2Gas); + } + + [inspect.custom]() { + return `Gas { daGas=${this.daGas} l1Gas=${this.l1Gas} l2Gas=${this.l2Gas} }`; + } + + add(other: Gas) { + return new Gas(this.daGas + other.daGas, this.l1Gas + other.l1Gas, this.l2Gas + other.l2Gas); + } + + sub(other: Gas) { + return new Gas(this.daGas - other.daGas, this.l1Gas - other.l1Gas, this.l2Gas - other.l2Gas); + } + + toFields() { + return serializeToFields(this.daGas, this.l1Gas, this.l2Gas); + } + + static fromFields(fields: Fr[] | FieldReader) { + const reader = FieldReader.asReader(fields); + return new Gas(reader.readU32(), reader.readU32(), reader.readU32()); + } +} diff --git a/yarn-project/circuits.js/src/structs/gas_fees.ts b/yarn-project/circuits.js/src/structs/gas_fees.ts index 89a7f9ff7b2d..ee5489d47b28 100644 --- a/yarn-project/circuits.js/src/structs/gas_fees.ts +++ b/yarn-project/circuits.js/src/structs/gas_fees.ts @@ -20,6 +20,11 @@ export class GasFees { return new GasFees(Fr.ZERO, Fr.ZERO, Fr.ZERO); } + /** Fixed gas fee values used until we define how gas fees in the protocol are computed. */ + static default() { + return new GasFees(Fr.ONE, Fr.ONE, Fr.ONE); + } + isEmpty() { return this.feePerDaGas.isZero() && this.feePerL1Gas.isZero() && this.feePerL2Gas.isZero(); } diff --git a/yarn-project/circuits.js/src/structs/gas_settings.ts b/yarn-project/circuits.js/src/structs/gas_settings.ts index 68cbc3fad420..36bc7363ed2b 100644 --- a/yarn-project/circuits.js/src/structs/gas_settings.ts +++ b/yarn-project/circuits.js/src/structs/gas_settings.ts @@ -3,6 +3,7 @@ import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from import { type FieldsOf } from '@aztec/foundation/types'; import { GAS_SETTINGS_LENGTH } from '../constants.gen.js'; +import { Gas } from './gas.js'; import { type UInt32 } from './shared.js'; /** Gas usage and fees limits set by the transaction sender for different dimensions and phases. */ @@ -14,6 +15,28 @@ export class GasSettings { public readonly inclusionFee: Fr, ) {} + static new( + da: FieldsOf, + l1: FieldsOf, + l2: FieldsOf, + inclusionFee: Fr, + ) { + return new GasSettings( + DimensionGasSettings.from(da), + DimensionGasSettings.from(l1), + DimensionGasSettings.from(l2), + inclusionFee, + ); + } + + /** Returns the maximum fee to be paid according to gas limits and max fees set. */ + getFeeLimit() { + return [this.da, this.l1, this.l2] + .reduce((acc, dimension) => acc.add(dimension.getFeeLimit()), Fr.ZERO) + .add(this.inclusionFee); + } + + /** Zero-value gas settings. */ static empty() { return new GasSettings( DimensionGasSettings.empty(), @@ -23,6 +46,16 @@ export class GasSettings { ); } + /** Default gas settings to use when user has not provided them. */ + static default() { + return GasSettings.empty(); + } + + /** Gas settings to use for simulating a contract call. */ + static simulation() { + return GasSettings.empty(); + } + isEmpty() { return this.da.isEmpty() && this.l1.isEmpty() && this.l2.isEmpty() && this.inclusionFee.isZero(); } @@ -73,6 +106,25 @@ export class GasSettings { static getFields(fields: FieldsOf) { return [fields.da, fields.l1, fields.l2, fields.inclusionFee] as const; } + + /** Returns total gas limits. */ + getLimits(): Gas { + return new Gas(this.da.gasLimit, this.l1.gasLimit, this.l2.gasLimit); + } + + /** Returns how much gas is available for execution of setup and app phases (ie total limit minus teardown). */ + getInitialAvailable(): Gas { + return new Gas( + this.da.gasLimit - this.da.teardownGasLimit, + this.l1.gasLimit - this.l1.teardownGasLimit, + this.l2.gasLimit - this.l2.teardownGasLimit, + ); + } + + /** Returns how much gas is available for execution of teardown phase. */ + getTeardownLimits(): Gas { + return new Gas(this.da.teardownGasLimit, this.l1.teardownGasLimit, this.l2.teardownGasLimit); + } } /** Gas usage and fees limits set by the transaction sender for different phases on a specific dimension. */ @@ -81,7 +133,15 @@ export class DimensionGasSettings { public readonly gasLimit: UInt32, public readonly teardownGasLimit: UInt32, public readonly maxFeePerGas: Fr, - ) {} + ) { + if (teardownGasLimit > gasLimit) { + throw new Error(`Teardown gas limit ${teardownGasLimit} is greater than gas limit ${gasLimit}`); + } + } + + getFeeLimit() { + return this.maxFeePerGas.mul(new Fr(this.gasLimit + this.teardownGasLimit)); + } static empty() { return new DimensionGasSettings(0, 0, Fr.ZERO); @@ -120,4 +180,8 @@ export class DimensionGasSettings { static getFields(fields: FieldsOf) { return [fields.gasLimit, fields.teardownGasLimit, fields.maxFeePerGas] as const; } + + static from(fields: FieldsOf) { + return new DimensionGasSettings(fields.gasLimit, fields.teardownGasLimit, fields.maxFeePerGas); + } } diff --git a/yarn-project/circuits.js/src/structs/gas_used.ts b/yarn-project/circuits.js/src/structs/gas_used.ts deleted file mode 100644 index 46100b229150..000000000000 --- a/yarn-project/circuits.js/src/structs/gas_used.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; -import { type FieldsOf } from '@aztec/foundation/types'; - -import { inspect } from 'util'; - -import { type UInt32 } from './shared.js'; - -/** Gas in each dimension used so far in the context of the current transaction or phase. */ -export class GasUsed { - constructor(public readonly daGas: UInt32, public readonly l1Gas: UInt32, public readonly l2Gas: UInt32) {} - - static from(fields: FieldsOf) { - return new GasUsed(fields.daGas, fields.l1Gas, fields.l2Gas); - } - - static empty() { - return new GasUsed(0, 0, 0); - } - - isEmpty() { - return this.daGas === 0 && this.l1Gas === 0 && this.l2Gas === 0; - } - - static fromBuffer(buffer: Buffer | BufferReader): GasUsed { - const reader = BufferReader.asReader(buffer); - return new GasUsed(reader.readNumber(), reader.readNumber(), reader.readNumber()); - } - - toBuffer() { - return serializeToBuffer(this.daGas, this.l1Gas, this.l2Gas); - } - - [inspect.custom]() { - return `GasUsed { daGas=${this.daGas} l1Gas=${this.l1Gas} l2Gas=${this.l2Gas} }`; - } -} diff --git a/yarn-project/circuits.js/src/structs/index.ts b/yarn-project/circuits.js/src/structs/index.ts index e2c688921c54..da3ec5a2c961 100644 --- a/yarn-project/circuits.js/src/structs/index.ts +++ b/yarn-project/circuits.js/src/structs/index.ts @@ -9,7 +9,7 @@ export * from './contract_storage_update_request.js'; export * from './function_data.js'; export * from './gas_fees.js'; export * from './gas_settings.js'; -export * from './gas_used.js'; +export * from './gas.js'; export * from './global_variables.js'; export * from './header.js'; export * from './kernel/combined_accumulated_data.js'; diff --git a/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts b/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts index 8599e3e0de84..3c4d58cf3ed8 100644 --- a/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts @@ -9,7 +9,7 @@ import { MAX_NEW_NULLIFIERS_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, } from '../../constants.gen.js'; -import { GasUsed } from '../gas_used.js'; +import { Gas } from '../gas.js'; import { PublicDataUpdateRequest } from '../public_data_update_request.js'; /** @@ -53,7 +53,7 @@ export class CombinedAccumulatedData { public publicDataUpdateRequests: Tuple, /** Gas used during this transaction */ - public gasUsed: GasUsed, + public gasUsed: Gas, ) {} toBuffer() { @@ -90,7 +90,7 @@ export class CombinedAccumulatedData { Fr.fromBuffer(reader), Fr.fromBuffer(reader), reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest), - reader.readObject(GasUsed), + reader.readObject(Gas), ); } @@ -113,7 +113,7 @@ export class CombinedAccumulatedData { Fr.zero(), Fr.zero(), makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest.empty), - GasUsed.empty(), + Gas.empty(), ); } } diff --git a/yarn-project/circuits.js/src/structs/kernel/private_accumulated_data.ts b/yarn-project/circuits.js/src/structs/kernel/private_accumulated_data.ts index 688aa74558cf..909f1afe7967 100644 --- a/yarn-project/circuits.js/src/structs/kernel/private_accumulated_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/private_accumulated_data.ts @@ -11,7 +11,7 @@ import { MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, } from '../../constants.gen.js'; import { CallRequest } from '../call_request.js'; -import { GasUsed } from '../gas_used.js'; +import { Gas } from '../gas.js'; import { SideEffect, SideEffectLinkedToNoteHash } from '../side_effects.js'; /** @@ -61,7 +61,7 @@ export class PrivateAccumulatedData { public publicCallStack: Tuple, /** Gas used so far by this transaction. */ - public gasUsed: GasUsed, + public gasUsed: Gas, ) {} toBuffer() { @@ -100,7 +100,7 @@ export class PrivateAccumulatedData { Fr.fromBuffer(reader), reader.readArray(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, CallRequest), reader.readArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, CallRequest), - reader.readObject(GasUsed), + reader.readObject(Gas), ); } @@ -124,7 +124,7 @@ export class PrivateAccumulatedData { Fr.zero(), makeTuple(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, CallRequest.empty), makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, CallRequest.empty), - GasUsed.empty(), + Gas.empty(), ); } } diff --git a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts index 82c924c2d587..21d94842d5af 100644 --- a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts @@ -13,7 +13,7 @@ import { MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, } from '../../constants.gen.js'; import { CallRequest } from '../call_request.js'; -import { GasUsed } from '../gas_used.js'; +import { Gas } from '../gas.js'; import { PublicDataUpdateRequest } from '../public_data_update_request.js'; import { SideEffect, SideEffectLinkedToNoteHash } from '../side_effects.js'; @@ -59,7 +59,7 @@ export class PublicAccumulatedData { public publicCallStack: Tuple, /** Gas used so far by the transaction. */ - public gasUsed: GasUsed, + public gasUsed: Gas, ) {} toBuffer() { @@ -129,7 +129,7 @@ export class PublicAccumulatedData { Fr.fromBuffer(reader), reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest), reader.readArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, CallRequest), - reader.readObject(GasUsed), + reader.readObject(Gas), ); } @@ -153,7 +153,7 @@ export class PublicAccumulatedData { Fr.zero(), makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest.empty), makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, CallRequest.empty), - GasUsed.empty(), + Gas.empty(), ); } } diff --git a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts index c294816a6eb1..9c2c5c65a4fe 100644 --- a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts @@ -27,6 +27,7 @@ import { import { CallContext } from './call_context.js'; import { ContractStorageRead } from './contract_storage_read.js'; import { ContractStorageUpdateRequest } from './contract_storage_update_request.js'; +import { Gas } from './gas.js'; import { Header } from './header.js'; import { L2ToL1Message } from './l2_to_l1_message.js'; import { ReadRequest } from './read_request.js'; @@ -119,6 +120,9 @@ export class PublicCircuitPublicInputs { * Flag indicating if the call was reverted. */ public revertCode: RevertCode, + + /** How much gas was left after execution. */ + public gasLeft: Gas, ) {} /** @@ -154,6 +158,7 @@ export class PublicCircuitPublicInputs { Header.empty(), AztecAddress.ZERO, RevertCode.OK, + Gas.empty(), ); } @@ -180,7 +185,8 @@ export class PublicCircuitPublicInputs { this.unencryptedLogPreimagesLength.isZero() && this.historicalHeader.isEmpty() && this.proverAddress.isZero() && - this.revertCode.isOK() + this.revertCode.isOK() && + this.gasLeft.isEmpty() ); } @@ -209,6 +215,7 @@ export class PublicCircuitPublicInputs { fields.historicalHeader, fields.proverAddress, fields.revertCode, + fields.gasLeft, ] as const; } @@ -256,6 +263,7 @@ export class PublicCircuitPublicInputs { reader.readObject(Header), reader.readObject(AztecAddress), reader.readObject(RevertCode), + reader.readObject(Gas), ); } @@ -281,6 +289,7 @@ export class PublicCircuitPublicInputs { Header.fromFields(reader), AztecAddress.fromFields(reader), RevertCode.fromFields(reader), + Gas.fromFields(reader), ); } diff --git a/yarn-project/circuits.js/src/structs/tx_request.test.ts b/yarn-project/circuits.js/src/structs/tx_request.test.ts index d353bb36b248..839e04e6577b 100644 --- a/yarn-project/circuits.js/src/structs/tx_request.test.ts +++ b/yarn-project/circuits.js/src/structs/tx_request.test.ts @@ -7,6 +7,7 @@ import { setupCustomSnapshotSerializers, updateInlineTestData } from '@aztec/fou import { TX_REQUEST_LENGTH } from '../constants.gen.js'; import { makeTxRequest } from '../tests/factories.js'; import { FunctionData } from './function_data.js'; +import { GasSettings } from './gas_settings.js'; import { TxContext } from './tx_context.js'; import { TxRequest } from './tx_request.js'; @@ -36,6 +37,12 @@ describe('TxRequest', () => { functionData: new FunctionData(FunctionSelector.fromField(new Fr(2n)), true), argsHash: new Fr(3), txContext: new TxContext(false, false, Fr.ZERO, Fr.ZERO), + gasSettings: GasSettings.new( + { gasLimit: 1, teardownGasLimit: 2, maxFeePerGas: new Fr(3) }, + { gasLimit: 1, teardownGasLimit: 2, maxFeePerGas: new Fr(3) }, + { gasLimit: 1, teardownGasLimit: 2, maxFeePerGas: new Fr(3) }, + new Fr(10), + ), }); const hash = txRequest.hash().toString(); diff --git a/yarn-project/circuits.js/src/structs/tx_request.ts b/yarn-project/circuits.js/src/structs/tx_request.ts index 6a3812692a74..8b2337e665d3 100644 --- a/yarn-project/circuits.js/src/structs/tx_request.ts +++ b/yarn-project/circuits.js/src/structs/tx_request.ts @@ -6,6 +6,7 @@ import { type FieldsOf } from '@aztec/foundation/types'; import { GeneratorIndex, TX_REQUEST_LENGTH } from '../constants.gen.js'; import { FunctionData } from './function_data.js'; +import { GasSettings } from './gas_settings.js'; import { TxContext } from './tx_context.js'; /** @@ -13,26 +14,20 @@ import { TxContext } from './tx_context.js'; */ export class TxRequest { constructor( - /** - * Sender. - */ + /** Sender. */ public origin: AztecAddress, - /** - * Function data representing the function to call. - */ + /** Function data representing the function to call. */ public functionData: FunctionData, - /** - * Pedersen hash of function arguments. - */ + /** Pedersen hash of function arguments. */ public argsHash: Fr, - /** - * Transaction context. - */ + /** Transaction context. */ public txContext: TxContext, + /** Gas limits and max fees per dimension. */ + public gasSettings: GasSettings, ) {} static getFields(fields: FieldsOf) { - return [fields.origin, fields.functionData, fields.argsHash, fields.txContext] as const; + return [fields.origin, fields.functionData, fields.argsHash, fields.txContext, fields.gasSettings] as const; } static from(fields: FieldsOf): TxRequest { @@ -67,6 +62,7 @@ export class TxRequest { reader.readObject(FunctionData), Fr.fromBuffer(reader), reader.readObject(TxContext), + reader.readObject(GasSettings), ); } @@ -75,10 +71,16 @@ export class TxRequest { } static empty() { - return new TxRequest(AztecAddress.ZERO, FunctionData.empty(), Fr.zero(), TxContext.empty()); + return new TxRequest(AztecAddress.ZERO, FunctionData.empty(), Fr.zero(), TxContext.empty(), GasSettings.empty()); } isEmpty() { - return this.origin.isZero() && this.functionData.isEmpty() && this.argsHash.isZero() && this.txContext.isEmpty(); + return ( + this.origin.isZero() && + this.functionData.isEmpty() && + this.argsHash.isZero() && + this.txContext.isEmpty() && + this.gasSettings.isEmpty() + ); } } diff --git a/yarn-project/circuits.js/src/tests/factories.ts b/yarn-project/circuits.js/src/tests/factories.ts index 753c7ca0db42..440f4bac9ab7 100644 --- a/yarn-project/circuits.js/src/tests/factories.ts +++ b/yarn-project/circuits.js/src/tests/factories.ts @@ -1,4 +1,4 @@ -import { makeHalfFullTuple, makeTuple, range } from '@aztec/foundation/array'; +import { FieldsOf, makeHalfFullTuple, makeTuple, range } from '@aztec/foundation/array'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { toBufferBE } from '@aztec/foundation/bigint-buffer'; import { EthAddress } from '@aztec/foundation/eth-address'; @@ -125,9 +125,9 @@ import { packBytecode, } from '../index.js'; import { ContentCommitment, NUM_BYTES_PER_SHA256 } from '../structs/content_commitment.js'; +import { Gas } from '../structs/gas.js'; import { GasFees } from '../structs/gas_fees.js'; import { DimensionGasSettings, GasSettings } from '../structs/gas_settings.js'; -import { GasUsed } from '../structs/gas_used.js'; import { GlobalVariables } from '../structs/global_variables.js'; import { Header } from '../structs/header.js'; import { KernelCircuitPublicInputs } from '../structs/kernel/kernel_circuit_public_inputs.js'; @@ -315,12 +315,12 @@ export function makeCombinedAccumulatedData(seed = 1, full = false): CombinedAcc seed + 0xd00, PublicDataUpdateRequest.empty, ), - makeGasUsed(seed + 0xe00), + makeGas(seed + 0xe00), ); } -export function makeGasUsed(seed = 1) { - return new GasUsed(seed, seed + 1, seed + 2); +export function makeGas(seed = 1) { + return new Gas(seed, seed + 1, seed + 2); } /** @@ -351,7 +351,7 @@ export function makePublicAccumulatedData(seed = 1, full = false): PublicAccumul PublicDataUpdateRequest.empty, ), tupleGenerator(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, makeCallRequest, seed + 0x500, CallRequest.empty), - makeGasUsed(seed + 0x600), + makeGas(seed + 0x600), ); } @@ -378,7 +378,7 @@ export function makePrivateAccumulatedData(seed = 1, full = false) { fr(seed + 0xa00), // unencrypted_log_preimages_length tupleGenerator(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, makeCallRequest, seed + 0x400, CallRequest.empty), tupleGenerator(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, makeCallRequest, seed + 0x500, CallRequest.empty), - makeGasUsed(seed + 0x600), + makeGas(seed + 0x600), ); } @@ -402,18 +402,20 @@ export function makeAggregationObject(seed = 1): AggregationObject { * @param storageContractAddress - The storage contract address set on the call context. * @returns A call context. */ -export function makeCallContext(seed = 0, storageContractAddress = makeAztecAddress(seed + 1)): CallContext { - return new CallContext( - makeAztecAddress(seed), - storageContractAddress, - makeEthAddress(seed + 2), - makeSelector(seed + 3), - false, - false, - 0, - makeGasSettings(seed + 4), - fr(seed + 5), - ); +export function makeCallContext(seed = 0, overrides: Partial> = {}): CallContext { + return CallContext.from({ + msgSender: makeAztecAddress(seed), + storageContractAddress: makeAztecAddress(seed + 1), + portalContractAddress: makeEthAddress(seed + 2), + functionSelector: makeSelector(seed + 3), + gasLeft: makeGas(seed + 4), + isStaticCall: false, + isDelegateCall: false, + sideEffectCounter: 0, + gasSettings: makeGasSettings(seed + 5), + transactionFee: fr(seed + 6), + ...overrides, + }); } /** @@ -430,7 +432,7 @@ export function makePublicCircuitPublicInputs( const tupleGenerator = full ? makeTuple : makeHalfFullTuple; return new PublicCircuitPublicInputs( - makeCallContext(seed, storageContractAddress), + makeCallContext(seed, { storageContractAddress }), fr(seed + 0x100), fr(seed + 0x200), tupleGenerator(MAX_NULLIFIER_READ_REQUESTS_PER_CALL, makeReadRequest, seed + 0x400, ReadRequest.empty), @@ -458,6 +460,7 @@ export function makePublicCircuitPublicInputs( makeHeader(seed + 0xa00, undefined), makeAztecAddress(seed + 0xb01), RevertCode.OK, + makeGas(seed + 0xc00), ); } @@ -547,18 +550,9 @@ export function makeKernelCircuitPublicInputs(seed = 1, fullAccumulatedData = tr * @returns Public call request. */ export function makePublicCallRequest(seed = 1): PublicCallRequest { - const childCallContext = makeCallContext(seed + 0x2, makeAztecAddress(seed)); - const parentCallContext = CallContext.from({ - msgSender: makeAztecAddress(seed + 0x3), - storageContractAddress: childCallContext.msgSender, - portalContractAddress: makeEthAddress(seed + 2), - functionSelector: makeSelector(seed + 3), - isStaticCall: false, - isDelegateCall: false, - sideEffectCounter: 0, - gasSettings: makeGasSettings(seed + 4), - transactionFee: fr(seed + 5), - }); + const childCallContext = makeCallContext(seed + 0x2, { storageContractAddress: makeAztecAddress(seed) }); + const parentCallContext = makeCallContext(seed + 0x3, { storageContractAddress: childCallContext.msgSender }); + return new PublicCallRequest( makeAztecAddress(seed), new FunctionData(makeSelector(seed + 0x1), false), @@ -830,6 +824,7 @@ export function makeTxRequest(seed = 1): TxRequest { functionData: new FunctionData(makeSelector(seed + 0x100), true), argsHash: fr(seed + 0x200), txContext: makeTxContext(seed + 0x400), + gasSettings: makeGasSettings(seed + 0x500), }); } @@ -881,17 +876,7 @@ export function makePrivateCallStackItem(seed = 1): PrivateCallStackItem { export function makePrivateCircuitPublicInputs(seed = 0): PrivateCircuitPublicInputs { return PrivateCircuitPublicInputs.from({ maxBlockNumber: new MaxBlockNumber(true, new Fr(seed + 0x31415)), - callContext: new CallContext( - makeAztecAddress(seed + 1), - makeAztecAddress(seed + 2), - new EthAddress(numToUInt32BE(seed + 3, /* eth address is 20 bytes */ 20)), - makeSelector(seed + 4), - true, - true, - 0, - makeGasSettings(seed + 4), - fr(seed + 5), - ), + callContext: makeCallContext(seed, { isDelegateCall: true, isStaticCall: true }), argsHash: fr(seed + 0x100), returnsHash: fr(seed + 0x200), minRevertibleSideEffectCounter: fr(0), diff --git a/yarn-project/end-to-end/src/benchmarks/bench_tx_size_fees.test.ts b/yarn-project/end-to-end/src/benchmarks/bench_tx_size_fees.test.ts index 727c1125f70a..b894125954dc 100644 --- a/yarn-project/end-to-end/src/benchmarks/bench_tx_size_fees.test.ts +++ b/yarn-project/end-to-end/src/benchmarks/bench_tx_size_fees.test.ts @@ -8,6 +8,7 @@ import { PublicFeePaymentMethod, TxStatus, } from '@aztec/aztec.js'; +import { GasSettings } from '@aztec/circuits.js'; import { FPCContract, GasTokenContract, TokenContract } from '@aztec/noir-contracts.js'; import { getCanonicalGasTokenAddress } from '@aztec/protocol-contracts/gas-token'; @@ -69,14 +70,7 @@ describe('benchmarks/tx_size_fees', () => { const paymentMethod = await createPaymentMethod(); const tx = await token.methods .transfer(aliceWallet.getAddress(), bobAddress, 1n, 0) - .send({ - fee: paymentMethod - ? { - maxFee: 3n, - paymentMethod, - } - : undefined, - }) + .send({ fee: paymentMethod ? { gasSettings: GasSettings.empty(), paymentMethod } : undefined }) .wait(); expect(tx.status).toEqual(TxStatus.MINED); diff --git a/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts b/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts index a6c74dbbf1c4..62211718fbb8 100644 --- a/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts +++ b/yarn-project/end-to-end/src/e2e_account_init_fees.test.ts @@ -15,7 +15,7 @@ import { computeMessageSecretHash, generatePublicKey, } from '@aztec/aztec.js'; -import { type AztecAddress, CompleteAddress, Fq } from '@aztec/circuits.js'; +import { type AztecAddress, CompleteAddress, DimensionGasSettings, Fq, GasSettings } from '@aztec/circuits.js'; import { TokenContract as BananaCoin, FPCContract, @@ -70,6 +70,7 @@ describe('e2e_fees_account_init', () => { let fpcsInitialGas: bigint; let fpcsInitialPublicBananas: bigint; + let gasSettings: GasSettings; let maxFee: bigint; let actualFee: bigint; @@ -123,7 +124,9 @@ describe('e2e_fees_account_init', () => { afterAll(() => ctx.teardown()); beforeEach(() => { - maxFee = 3n; + const individualGasSettings = new DimensionGasSettings(2, 1, Fr.ONE); + gasSettings = new GasSettings(individualGasSettings, individualGasSettings, individualGasSettings, new Fr(5)); + maxFee = 3n * 3n + 5n; actualFee = 1n; bobsPrivateEncryptionKey = Fq.random(); bobsPrivateSigningKey = Fq.random(); @@ -143,7 +146,7 @@ describe('e2e_fees_account_init', () => { await bobsAccountManager .deploy({ fee: { - maxFee, + gasSettings, paymentMethod: await NativeFeePaymentMethod.create(await bobsAccountManager.getWallet()), }, }) @@ -188,7 +191,7 @@ describe('e2e_fees_account_init', () => { const tx = await bobsAccountManager .deploy({ fee: { - maxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -246,7 +249,7 @@ describe('e2e_fees_account_init', () => { .deploy({ skipPublicDeployment: false, fee: { - maxFee, + gasSettings, paymentMethod: new PublicFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -301,7 +304,7 @@ describe('e2e_fees_account_init', () => { skipInitialization: false, universalDeploy: true, fee: { - maxFee, + gasSettings, paymentMethod: await NativeFeePaymentMethod.create(alice), }, }) diff --git a/yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts b/yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts index df3cf484e453..498d31975cd2 100644 --- a/yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts +++ b/yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts @@ -11,6 +11,7 @@ import { PublicFeePaymentMethod, SentTx, } from '@aztec/aztec.js'; +import { GasSettings } from '@aztec/circuits.js'; import { DefaultDappEntrypoint } from '@aztec/entrypoints/dapp'; import { AppSubscriptionContract, @@ -55,13 +56,22 @@ describe('e2e_dapp_subscription', () => { const PUBLICLY_MINTED_BANANAS = 500n; const PRIVATELY_MINTED_BANANAS = 600n; - const FEE_AMOUNT = 1n; - const REFUND = 2n; // intentionally overpay the gas fee. This is the expected refund. + const FEE_AMOUNT = 10n; + const REFUND = 20n; // intentionally overpay the gas fee. This is the expected refund. const MAX_FEE = FEE_AMOUNT + REFUND; + const GAS_SETTINGS = GasSettings.new( + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + new Fr(6), + ); + beforeAll(async () => { process.env.PXE_URL = ''; + expect(GAS_SETTINGS.getFeeLimit()).toEqual(MAX_FEE); + let wallets: AccountWalletWithPrivateKey[]; let aztecNode: AztecNode; let deployL1ContractsValues: DeployL1Contracts; @@ -246,12 +256,7 @@ describe('e2e_dapp_subscription', () => { return subscriptionContract .withWallet(aliceWallet) .methods.subscribe(aliceAddress, nonce, (await pxe.getBlockNumber()) + blockDelta, txCount) - .send({ - fee: { - maxFee, - paymentMethod, - }, - }) + .send({ fee: { gasSettings: GAS_SETTINGS, paymentMethod } }) .wait(); } diff --git a/yarn-project/end-to-end/src/e2e_fees.test.ts b/yarn-project/end-to-end/src/e2e_fees.test.ts index 9e0248b16f10..9a4eab365b80 100644 --- a/yarn-project/end-to-end/src/e2e_fees.test.ts +++ b/yarn-project/end-to-end/src/e2e_fees.test.ts @@ -16,7 +16,7 @@ import { computeAuthWitMessageHash, computeMessageSecretHash, } from '@aztec/aztec.js'; -import { FunctionData } from '@aztec/circuits.js'; +import { FunctionData, GasSettings } from '@aztec/circuits.js'; import { type ContractArtifact, decodeFunctionSignature } from '@aztec/foundation/abi'; import { TokenContract as BananaCoin, @@ -53,6 +53,13 @@ describe('e2e_fees', () => { let bananaPublicBalances: BalancesFn; let bananaPrivateBalances: BalancesFn; + const gasSettings = GasSettings.new( + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + { gasLimit: 5, teardownGasLimit: 3, maxFeePerGas: Fr.ONE }, + new Fr(6), + ); + beforeAll(async () => { const { wallets: _wallets, aztecNode, deployL1ContractsValues, logger, pxe } = await setup(3); wallets = _wallets; @@ -105,9 +112,7 @@ describe('e2e_fees', () => { it('reverts transactions but still pays fees using PublicFeePaymentMethod', async () => { const OutrageousPublicAmountAliceDoesNotHave = 10000n; const PublicMintedAlicePublicBananas = 1000n; - const FeeAmount = 1n; - const RefundAmount = 2n; - const MaxFee = FeeAmount + RefundAmount; + const FeeAmount = 10n; const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await bananaPrivateBalances( aliceAddress, @@ -130,7 +135,7 @@ describe('e2e_fees', () => { .transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -160,7 +165,7 @@ describe('e2e_fees', () => { .send({ skipPublicSimulation: true, fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -261,7 +266,7 @@ describe('e2e_fees', () => { .transfer(aliceAddress, bobAddress, transferAmount, 0n) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -324,7 +329,7 @@ describe('e2e_fees', () => { .privately_mint_private_note(newlyMintedBananas) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -390,7 +395,7 @@ describe('e2e_fees', () => { .shield(aliceAddress, shieldedBananas, shieldSecretHash, 0n) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -462,7 +467,7 @@ describe('e2e_fees', () => { ]) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bananaFPC.address, @@ -516,7 +521,7 @@ describe('e2e_fees', () => { // we need to skip public simulation otherwise the PXE refuses to accept the TX skipPublicSimulation: true, fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new PrivateFeePaymentMethod( bananaCoin.address, bankruptFPC.address, @@ -532,10 +537,6 @@ describe('e2e_fees', () => { it('fails transaction that error in setup', async () => { const OutrageousPublicAmountAliceDoesNotHave = 10000n; - // const PublicMintedAlicePublicBananas = 1000n; - const FeeAmount = 1n; - const RefundAmount = 2n; - const MaxFee = FeeAmount + RefundAmount; // simulation throws an error when setup fails await expect( @@ -543,7 +544,7 @@ describe('e2e_fees', () => { .transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0) .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new BuggedSetupFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -557,7 +558,7 @@ describe('e2e_fees', () => { .send({ skipPublicSimulation: true, fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new BuggedSetupFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -570,11 +571,7 @@ describe('e2e_fees', () => { * We trigger an error in teardown by having the FPC authorize a transfer of its entire balance to Alice * as part of app logic. This will cause the FPC to not have enough funds to pay the refund back to Alice. */ - const PublicMintedAlicePublicBananas = 1000n; - const FeeAmount = 1n; - const RefundAmount = 2n; - const MaxFee = FeeAmount + RefundAmount; const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await bananaPrivateBalances( aliceAddress, @@ -597,7 +594,7 @@ describe('e2e_fees', () => { .mint_public(aliceAddress, 1n) // random operation .send({ fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new BuggedTeardownFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -611,7 +608,7 @@ describe('e2e_fees', () => { .send({ skipPublicSimulation: true, fee: { - maxFee: MaxFee, + gasSettings, paymentMethod: new BuggedTeardownFeePaymentMethod(bananaCoin.address, bananaFPC.address, wallets[0]), }, }) @@ -672,7 +669,8 @@ describe('e2e_fees', () => { }); class BuggedSetupFeePaymentMethod extends PublicFeePaymentMethod { - getFunctionCalls(maxFee: Fr): Promise { + getFunctionCalls(gasSettings: GasSettings): Promise { + const maxFee = gasSettings.getFeeLimit(); const nonce = Fr.random(); const messageHash = computeAuthWitMessageHash( this.paymentContract, @@ -705,9 +703,10 @@ class BuggedSetupFeePaymentMethod extends PublicFeePaymentMethod { } class BuggedTeardownFeePaymentMethod extends PublicFeePaymentMethod { - async getFunctionCalls(maxFee: Fr): Promise { + async getFunctionCalls(gasSettings: GasSettings): Promise { // authorize the FPC to take the max fee from Alice const nonce = Fr.random(); + const maxFee = gasSettings.getFeeLimit(); const messageHash1 = computeAuthWitMessageHash( this.paymentContract, this.wallet.getChainId(), diff --git a/yarn-project/entrypoints/src/account_entrypoint.ts b/yarn-project/entrypoints/src/account_entrypoint.ts index 84a78f8b1a96..fccd48001777 100644 --- a/yarn-project/entrypoints/src/account_entrypoint.ts +++ b/yarn-project/entrypoints/src/account_entrypoint.ts @@ -1,7 +1,7 @@ import { type AuthWitnessProvider } from '@aztec/aztec.js/account'; import { type EntrypointInterface, EntrypointPayload, type ExecutionRequestInit } from '@aztec/aztec.js/entrypoint'; import { PackedValues, TxExecutionRequest } from '@aztec/circuit-types'; -import { type AztecAddress, FunctionData, TxContext } from '@aztec/circuits.js'; +import { type AztecAddress, FunctionData, GasSettings, TxContext } from '@aztec/circuits.js'; import { type FunctionAbi, encodeArguments } from '@aztec/foundation/abi'; import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js'; @@ -36,6 +36,7 @@ export class DefaultAccountEntrypoint implements EntrypointInterface { txContext: TxContext.empty(this.chainId, this.version), packedArguments: [...appPayload.packedArguments, ...feePayload.packedArguments, entrypointPackedArgs], authWitnesses: [appAuthWitness, feeAuthWitness], + gasSettings: exec.fee?.gasSettings ?? GasSettings.default(), }); return txRequest; diff --git a/yarn-project/entrypoints/src/dapp_entrypoint.ts b/yarn-project/entrypoints/src/dapp_entrypoint.ts index a65e501d1aba..55381e04cc01 100644 --- a/yarn-project/entrypoints/src/dapp_entrypoint.ts +++ b/yarn-project/entrypoints/src/dapp_entrypoint.ts @@ -2,7 +2,7 @@ import { computeInnerAuthWitHash, computeOuterAuthWitHash } from '@aztec/aztec.j import { type AuthWitnessProvider } from '@aztec/aztec.js/account'; import { type EntrypointInterface, EntrypointPayload, type ExecutionRequestInit } from '@aztec/aztec.js/entrypoint'; import { PackedValues, TxExecutionRequest } from '@aztec/circuit-types'; -import { type AztecAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; +import { type AztecAddress, Fr, FunctionData, GasSettings, TxContext } from '@aztec/circuits.js'; import { type FunctionAbi, encodeArguments } from '@aztec/foundation/abi'; import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js'; @@ -50,6 +50,7 @@ export class DefaultDappEntrypoint implements EntrypointInterface { txContext: TxContext.empty(this.chainId, this.version), packedArguments: [...payload.packedArguments, entrypointPackedArgs], authWitnesses: [authWitness], + gasSettings: exec.fee?.gasSettings ?? GasSettings.default(), }); return txRequest; diff --git a/yarn-project/foundation/src/fields/fields.ts b/yarn-project/foundation/src/fields/fields.ts index bc180fa9ecb6..737a6807b5c5 100644 --- a/yarn-project/foundation/src/fields/fields.ts +++ b/yarn-project/foundation/src/fields/fields.ts @@ -188,6 +188,7 @@ export interface Fr { */ export class Fr extends BaseField { static ZERO = new Fr(0n); + static ONE = new Fr(1n); static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n; constructor(value: number | bigint | boolean | Fr | Buffer) { diff --git a/yarn-project/noir-protocol-circuits-types/src/__snapshots__/index.test.ts.snap b/yarn-project/noir-protocol-circuits-types/src/__snapshots__/index.test.ts.snap index 67fdd01c8720..f3329120ce77 100644 --- a/yarn-project/noir-protocol-circuits-types/src/__snapshots__/index.test.ts.snap +++ b/yarn-project/noir-protocol-circuits-types/src/__snapshots__/index.test.ts.snap @@ -106,7 +106,7 @@ PrivateKernelCircuitPublicInputs { "end": PrivateAccumulatedData { "encryptedLogPreimagesLength": Fr<0x000000000000000000000000000000000000000000000000000000000000000c>, "encryptedLogsHash": Fr<0x00f33ae280239814c4dfaaafc16fc138a8d3eae52bb962af6576cbb61c2af246>, - "gasUsed": GasUsed { + "gasUsed": Gas { "daGas": 0, "l1Gas": 0, "l2Gas": 0, @@ -1780,7 +1780,7 @@ PrivateKernelTailCircuitPublicInputs { "end": CombinedAccumulatedData { "encryptedLogPreimagesLength": Fr<0x000000000000000000000000000000000000000000000000000000000000013c>, "encryptedLogsHash": Fr<0x00d9390f71f6e9a79785314dbe71198e95418c404086825f7b33a1cecd7e0a16>, - "gasUsed": GasUsed { + "gasUsed": Gas { "daGas": 0, "l1Gas": 0, "l2Gas": 0, diff --git a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts index 67ed202f4364..0dbf841e51ea 100644 --- a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts +++ b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts @@ -21,9 +21,9 @@ import { Fr, FunctionData, FunctionSelector, + Gas, GasFees, GasSettings, - GasUsed, GlobalVariables, type GrumpkinPrivateKey, GrumpkinScalar, @@ -117,8 +117,8 @@ import { type FunctionLeafMembershipWitness as FunctionLeafMembershipWitnessNoir, type FunctionSelector as FunctionSelectorNoir, type GasFees as GasFeesNoir, + type Gas as GasNoir, type GasSettings as GasSettingsNoir, - type GasUsed as GasUsedNoir, type GrumpkinPrivateKey as GrumpkinPrivateKeyNoir, type L2ToL1Message as L2ToL1MessageNoir, type MaxBlockNumber as MaxBlockNumberNoir, @@ -410,6 +410,7 @@ export function mapTxRequestToNoir(txRequest: TxRequest): TxRequestNoir { args_hash: mapFieldToNoir(txRequest.argsHash), tx_context: mapTxContextToNoir(txRequest.txContext), function_data: mapFunctionDataToNoir(txRequest.functionData), + gas_settings: mapGasSettingsToNoir(txRequest.gasSettings), }; } @@ -424,6 +425,7 @@ export function mapCallContextFromNoir(callContext: CallContextNoir): CallContex mapAztecAddressFromNoir(callContext.storage_contract_address), mapEthAddressFromNoir(callContext.portal_contract_address), mapFunctionSelectorFromNoir(callContext.function_selector), + mapGasFromNoir(callContext.gas_left), callContext.is_delegate_call, callContext.is_static_call, mapNumberFromNoir(callContext.side_effect_counter), @@ -443,6 +445,7 @@ export function mapCallContextToNoir(callContext: CallContext): CallContextNoir storage_contract_address: mapAztecAddressToNoir(callContext.storageContractAddress), portal_contract_address: mapEthAddressToNoir(callContext.portalContractAddress), function_selector: mapFunctionSelectorToNoir(callContext.functionSelector), + gas_left: mapGasToNoir(callContext.gasLeft), is_delegate_call: callContext.isDelegateCall, is_static_call: callContext.isStaticCall, side_effect_counter: mapNumberToNoir(callContext.sideEffectCounter), @@ -1022,7 +1025,7 @@ export function mapPrivateAccumulatedDataFromNoir( MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, mapCallRequestFromNoir, ), - mapGasUsedFromNoir(privateAccumulatedData.gas_used), + mapGasFromNoir(privateAccumulatedData.gas_used), ); } @@ -1037,7 +1040,7 @@ export function mapPrivateAccumulatedDataToNoir(data: PrivateAccumulatedData): P unencrypted_log_preimages_length: mapFieldToNoir(data.unencryptedLogPreimagesLength), private_call_stack: mapTuple(data.privateCallStack, mapCallRequestToNoir), public_call_stack: mapTuple(data.publicCallStack, mapCallRequestToNoir), - gas_used: mapGasUsedToNoir(data.gasUsed), + gas_used: mapGasToNoir(data.gasUsed), }; } @@ -1062,7 +1065,7 @@ export function mapPublicAccumulatedDataFromNoir( MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, mapCallRequestFromNoir, ), - mapGasUsedFromNoir(publicAccumulatedData.gas_used), + mapGasFromNoir(publicAccumulatedData.gas_used), ); } @@ -1082,19 +1085,19 @@ export function mapPublicAccumulatedDataToNoir( mapPublicDataUpdateRequestToNoir, ), public_call_stack: mapTuple(publicAccumulatedData.publicCallStack, mapCallRequestToNoir), - gas_used: mapGasUsedToNoir(publicAccumulatedData.gasUsed), + gas_used: mapGasToNoir(publicAccumulatedData.gasUsed), }; } -export function mapGasUsedFromNoir(gasUsed: GasUsedNoir): GasUsed { - return GasUsed.from({ +export function mapGasFromNoir(gasUsed: GasNoir): Gas { + return Gas.from({ daGas: mapNumberFromNoir(gasUsed.da_gas), l1Gas: mapNumberFromNoir(gasUsed.l1_gas), l2Gas: mapNumberFromNoir(gasUsed.l2_gas), }); } -export function mapGasUsedToNoir(gasUsed: GasUsed): GasUsedNoir { +export function mapGasToNoir(gasUsed: Gas): GasNoir { return { da_gas: mapNumberToNoir(gasUsed.daGas), l1_gas: mapNumberToNoir(gasUsed.l1Gas), @@ -1150,7 +1153,7 @@ export function mapCombinedAccumulatedDataFromNoir( MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, mapPublicDataUpdateRequestFromNoir, ), - mapGasUsedFromNoir(combinedAccumulatedData.gas_used), + mapGasFromNoir(combinedAccumulatedData.gas_used), ); } @@ -1169,7 +1172,7 @@ export function mapCombinedAccumulatedDataToNoir( combinedAccumulatedData.publicDataUpdateRequests, mapPublicDataUpdateRequestToNoir, ), - gas_used: mapGasUsedToNoir(combinedAccumulatedData.gasUsed), + gas_used: mapGasToNoir(combinedAccumulatedData.gasUsed), }; } @@ -1530,9 +1533,9 @@ export function mapPublicCircuitPublicInputsToNoir( unencrypted_logs_hash: mapFieldToNoir(publicInputs.unencryptedLogsHash), unencrypted_log_preimages_length: mapFieldToNoir(publicInputs.unencryptedLogPreimagesLength), historical_header: mapHeaderToNoir(publicInputs.historicalHeader), - prover_address: mapAztecAddressToNoir(publicInputs.proverAddress), revert_code: mapRevertCodeToNoir(publicInputs.revertCode), + gas_left: mapGasToNoir(publicInputs.gasLeft), }; } /** diff --git a/yarn-project/pxe/src/pxe_service/test/pxe_test_suite.ts b/yarn-project/pxe/src/pxe_service/test/pxe_test_suite.ts index 314995a4fd5a..39c3e395ca48 100644 --- a/yarn-project/pxe/src/pxe_service/test/pxe_test_suite.ts +++ b/yarn-project/pxe/src/pxe_service/test/pxe_test_suite.ts @@ -10,6 +10,7 @@ import { CompleteAddress, Fr, FunctionData, + GasSettings, INITIAL_L2_BLOCK_NUM, Point, TxContext, @@ -133,6 +134,7 @@ export const pxeTestSuite = (testName: string, pxeSetup: () => Promise) => txContext: TxContext.empty(), packedArguments: [], authWitnesses: [], + gasSettings: GasSettings.default(), }); await expect(async () => await pxe.proveTx(txExecutionRequest, false)).rejects.toThrow( diff --git a/yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts b/yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts index 33766cfa2c18..3ec8a8eb64cd 100644 --- a/yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts +++ b/yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts @@ -90,7 +90,7 @@ export class SimpleTestGlobalVariableBuilder implements GlobalVariableBuilder { `Built global variables for block ${blockNumber}: (${chainId}, ${version}, ${blockNumber}, ${lastTimestamp}, ${coinbase}, ${feeRecipient})`, ); - const gasFees = GasFees.empty(); // TODO(palla/gas-in-circuits) + const gasFees = GasFees.default(); return new GlobalVariables(chainId, version, blockNumber, lastTimestamp, coinbase, feeRecipient, gasFees); } } diff --git a/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts b/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts index ca12297b5faa..26544061eea2 100644 --- a/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts +++ b/yarn-project/sequencer-client/src/sequencer/abstract_phase_manager.ts @@ -5,6 +5,7 @@ import { ContractStorageRead, ContractStorageUpdateRequest, Fr, + Gas, type GlobalVariables, type Header, type KernelCircuitPublicInputs, @@ -238,6 +239,11 @@ export abstract class AbstractPhaseManager { const isExecutionRequest = !isPublicExecutionResult(current); const sideEffectCounter = lastSideEffectCounter(tx) + 1; + // const gasLeft = kernelOutput.constants.gasSettings + // .getLimits() + // .sub(kernelOutput.end.gasUsed) + // .sub(kernelOutput.endNonRevertibleData.gasUsed); + const result = isExecutionRequest ? await this.publicExecutor.simulate(current, this.globalVariables, sideEffectCounter) : current; @@ -398,6 +404,7 @@ export abstract class AbstractPhaseManager { historicalHeader: this.historicalHeader, // TODO(@just-mitch): need better mapping from simulator to revert code. revertCode: result.reverted ? RevertCode.REVERTED : RevertCode.OK, + gasLeft: Gas.from(result.gasLeft), }); } diff --git a/yarn-project/sequencer-client/src/sequencer/public_processor.test.ts b/yarn-project/sequencer-client/src/sequencer/public_processor.test.ts index 567ec659e087..d293d981cc95 100644 --- a/yarn-project/sequencer-client/src/sequencer/public_processor.test.ts +++ b/yarn-project/sequencer-client/src/sequencer/public_processor.test.ts @@ -21,6 +21,7 @@ import { EthAddress, Fr, FunctionData, + Gas, GasSettings, GlobalVariables, Header, @@ -799,6 +800,7 @@ class PublicExecutionResultBuilder { tx.to, EthAddress.ZERO, tx.functionData.selector, + Gas.test(), false, false, 0, @@ -858,6 +860,7 @@ class PublicExecutionResultBuilder { endSideEffectCounter: Fr.ZERO, reverted: this._reverted, revertReason: this._revertReason, + gasLeft: Gas.empty(), }; } } diff --git a/yarn-project/simulator/src/avm/avm_execution_environment.ts b/yarn-project/simulator/src/avm/avm_execution_environment.ts index c21fb948a980..a3dbb81f6606 100644 --- a/yarn-project/simulator/src/avm/avm_execution_environment.ts +++ b/yarn-project/simulator/src/avm/avm_execution_environment.ts @@ -1,4 +1,4 @@ -import { FunctionSelector, type GlobalVariables, type Header } from '@aztec/circuits.js'; +import { FunctionSelector, type GasSettings, type GlobalVariables, type Header } from '@aztec/circuits.js'; import { computeVarArgsHash } from '@aztec/circuits.js/hash'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; import { type EthAddress } from '@aztec/foundation/eth-address'; @@ -35,6 +35,8 @@ export class AvmExecutionEnvironment { public readonly isStaticCall: boolean, public readonly isDelegateCall: boolean, public readonly calldata: Fr[], + public readonly gasSettings: GasSettings, + public readonly transactionFee: Fr, // Function selector is temporary since eventually public contract bytecode will be one blob // containing all functions, and function selector will become an application-level mechanism @@ -67,6 +69,8 @@ export class AvmExecutionEnvironment { this.isStaticCall, this.isDelegateCall, calldata, + this.gasSettings, + this.transactionFee, temporaryFunctionSelector, ); } @@ -91,6 +95,8 @@ export class AvmExecutionEnvironment { /*isStaticCall=*/ true, this.isDelegateCall, calldata, + this.gasSettings, + this.transactionFee, temporaryFunctionSelector, ); } @@ -115,6 +121,8 @@ export class AvmExecutionEnvironment { this.isStaticCall, /*isDelegateCall=*/ true, calldata, + this.gasSettings, + this.transactionFee, temporaryFunctionSelector, ); } diff --git a/yarn-project/simulator/src/avm/avm_machine_state.ts b/yarn-project/simulator/src/avm/avm_machine_state.ts index 178ca1adcf25..4c5c58fcc6ad 100644 --- a/yarn-project/simulator/src/avm/avm_machine_state.ts +++ b/yarn-project/simulator/src/avm/avm_machine_state.ts @@ -44,10 +44,20 @@ export class AvmMachineState { /** Output data must NOT be modified once it is set */ private output: Fr[] = []; - constructor(l1GasLeft: number, l2GasLeft: number, daGasLeft: number) { - this.l1GasLeft = l1GasLeft; - this.l2GasLeft = l2GasLeft; - this.daGasLeft = daGasLeft; + constructor(gasLeft: Gas); + constructor(l1GasLeft: number, l2GasLeft: number, daGasLeft: number); + constructor(gasLeftOrL1GasLeft: Gas | number, l2GasLeft?: number, daGasLeft?: number) { + if (typeof gasLeftOrL1GasLeft === 'object') { + ({ l1Gas: this.l1GasLeft, l2Gas: this.l2GasLeft, daGas: this.daGasLeft } = gasLeftOrL1GasLeft); + } else { + this.l1GasLeft = gasLeftOrL1GasLeft; + this.l2GasLeft = l2GasLeft!; + this.daGasLeft = daGasLeft!; + } + } + + public get gasLeft(): Gas { + return { l1Gas: this.l1GasLeft, l2Gas: this.l2GasLeft, daGas: this.daGasLeft }; } public static fromState(state: InitialAvmMachineState): AvmMachineState { diff --git a/yarn-project/simulator/src/avm/fixtures/index.ts b/yarn-project/simulator/src/avm/fixtures/index.ts index 05a711550315..ba5cbb7eae74 100644 --- a/yarn-project/simulator/src/avm/fixtures/index.ts +++ b/yarn-project/simulator/src/avm/fixtures/index.ts @@ -1,5 +1,5 @@ import { SiblingPath } from '@aztec/circuit-types'; -import { GasFees, GlobalVariables, Header, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js'; +import { GasFees, GasSettings, GlobalVariables, Header, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js'; import { FunctionSelector } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { EthAddress } from '@aztec/foundation/eth-address'; @@ -72,6 +72,8 @@ export function initExecutionEnvironment(overrides?: Partial { txContext: TxContext.from({ ...txContextFields, ...txContext }), packedArguments: [packedArguments], authWitnesses: [], + gasSettings: GasSettings.default(), }); return acirSimulator.run(txRequest, artifact, contractAddress, portalContractAddress, msgSender); @@ -797,6 +798,7 @@ describe('Private Execution test suite', () => { const transactionFee = new Fr(0); const gasSettings = GasSettings.empty(); + const gasLeft = gasSettings.getInitialAvailable(); const publicCallRequest = PublicCallRequest.from({ contractAddress: childAddress, @@ -807,6 +809,7 @@ describe('Private Execution test suite', () => { storageContractAddress: childAddress, portalContractAddress: childPortalContractAddress, functionSelector: childSelector, + gasLeft, isDelegateCall: false, isStaticCall: false, sideEffectCounter: 1, @@ -818,6 +821,7 @@ describe('Private Execution test suite', () => { storageContractAddress: parentAddress, portalContractAddress: EthAddress.ZERO, functionSelector: FunctionSelector.fromNameAndParameters(parentArtifact.name, parentArtifact.parameters), + gasLeft, isDelegateCall: false, isStaticCall: false, sideEffectCounter: 1, diff --git a/yarn-project/simulator/src/client/simulator.ts b/yarn-project/simulator/src/client/simulator.ts index 64c963cff81f..25bd77e8688e 100644 --- a/yarn-project/simulator/src/client/simulator.ts +++ b/yarn-project/simulator/src/client/simulator.ts @@ -1,5 +1,5 @@ import { type AztecNode, type FunctionCall, type Note, type TxExecutionRequest } from '@aztec/circuit-types'; -import { CallContext, FunctionData, GasSettings } from '@aztec/circuits.js'; +import { CallContext, FunctionData } from '@aztec/circuits.js'; import { Grumpkin } from '@aztec/circuits.js/barretenberg'; import { type ArrayType, @@ -89,16 +89,17 @@ export class AcirSimulator { // reserve the first side effect for the tx hash (inserted by the private kernel) const startSideEffectCounter = 1; - const transactionFee = Fr.ZERO; // TODO(palla/gas-in-circuits) + const transactionFee = Fr.ZERO; const callContext = new CallContext( msgSender, contractAddress, portalContractAddress, FunctionSelector.fromNameAndParameters(entryPointArtifact.name, entryPointArtifact.parameters), + request.gasSettings.getInitialAvailable(), false, false, startSideEffectCounter, - GasSettings.empty(), // TODO(palla/gas-in-circuits) + request.gasSettings, transactionFee, ); const context = new ClientExecutionContext( diff --git a/yarn-project/simulator/src/public/avm_executor.test.ts b/yarn-project/simulator/src/public/avm_executor.test.ts index cf2c254e3ac6..8ef04ddd1324 100644 --- a/yarn-project/simulator/src/public/avm_executor.test.ts +++ b/yarn-project/simulator/src/public/avm_executor.test.ts @@ -4,6 +4,7 @@ import { EthAddress, FunctionData, FunctionSelector, + Gas, GasSettings, type Header, } from '@aztec/circuits.js'; @@ -30,6 +31,7 @@ describe('AVM WitGen and Proof Generation', () => { storageContractAddress: AztecAddress.random(), portalContractAddress: EthAddress.random(), functionSelector: FunctionSelector.empty(), + gasLeft: Gas.test(), isDelegateCall: false, isStaticCall: false, sideEffectCounter: 0, diff --git a/yarn-project/simulator/src/public/execution.ts b/yarn-project/simulator/src/public/execution.ts index 96ac9e84e97c..74d33ec15c6b 100644 --- a/yarn-project/simulator/src/public/execution.ts +++ b/yarn-project/simulator/src/public/execution.ts @@ -14,6 +14,8 @@ import { } from '@aztec/circuits.js'; import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/hash'; +import { type Gas } from '../avm/avm_gas.js'; + /** * The public function execution result. */ @@ -55,6 +57,8 @@ export interface PublicExecutionResult { * The revert reason if the execution reverted. */ revertReason: SimulationError | undefined; + /** How much gas was left after this public execution. */ + gasLeft: Gas; } /** diff --git a/yarn-project/simulator/src/public/executor.ts b/yarn-project/simulator/src/public/executor.ts index c805b4496a86..0acbc9d8be06 100644 --- a/yarn-project/simulator/src/public/executor.ts +++ b/yarn-project/simulator/src/public/executor.ts @@ -1,5 +1,5 @@ import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types'; -import { Fr, type GlobalVariables, type Header, PublicCircuitPublicInputs } from '@aztec/circuits.js'; +import { Fr, Gas, type GlobalVariables, type Header, PublicCircuitPublicInputs } from '@aztec/circuits.js'; import { createDebugLogger } from '@aztec/foundation/log'; import { spawn } from 'child_process'; @@ -66,9 +66,7 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext executionContext.globalVariables, ); - // TODO(@spalladino) Load initial gas from the public execution request - const machineState = new AvmMachineState(1e7, 1e7, 1e7); - + const machineState = new AvmMachineState(executionContext.execution.callContext.gasLeft); const context = new AvmContext(worldStateJournal, executionEnv, machineState); const simulator = new AvmSimulator(context); @@ -79,8 +77,7 @@ async function executePublicFunctionAvm(executionContext: PublicExecutionContext `[AVM] ${address.toString()}:${selector} returned, reverted: ${result.reverted}, reason: ${result.revertReason}.`, ); - // TODO(@spalladino) Read gas left from machineState and return it - return await convertAvmResults(executionContext, newWorldState, result); + return await convertAvmResults(executionContext, newWorldState, result, machineState); } async function executePublicFunctionAcvm( @@ -154,6 +151,7 @@ async function executePublicFunctionAcvm( unencryptedLogs: UnencryptedFunctionL2Logs.empty(), reverted, revertReason, + gasLeft: Gas.empty(), }; } @@ -195,6 +193,7 @@ async function executePublicFunctionAcvm( const nestedExecutions = context.getNestedExecutions(); const unencryptedLogs = context.getUnencryptedLogs(); + const gasLeft = context.execution.callContext.gasLeft; // No gas metering for ACVM return { execution, @@ -212,6 +211,7 @@ async function executePublicFunctionAcvm( unencryptedLogs, reverted: false, revertReason: undefined, + gasLeft, }; } diff --git a/yarn-project/simulator/src/public/index.test.ts b/yarn-project/simulator/src/public/index.test.ts index d309a73f5e67..5f97336dcbfd 100644 --- a/yarn-project/simulator/src/public/index.test.ts +++ b/yarn-project/simulator/src/public/index.test.ts @@ -3,6 +3,7 @@ import { AppendOnlyTreeSnapshot, CallContext, FunctionData, + Gas, GasFees, GasSettings, GlobalVariables, @@ -47,6 +48,9 @@ describe('ACIR public execution simulator', () => { let executor: PublicExecutor; let header: Header; + const gasLeft = new Gas(1e9, 1e9, 1e9); + const globalVariables = GlobalVariables.empty(); + beforeEach(() => { publicState = mock(); publicContracts = mock(); @@ -89,6 +93,7 @@ describe('ACIR public execution simulator', () => { CallContext.from({ storageContractAddress, msgSender: AztecAddress.random(), + gasLeft, portalContractAddress: EthAddress.random(), functionSelector: FunctionSelector.empty(), isDelegateCall: false, @@ -132,7 +137,8 @@ describe('ACIR public execution simulator', () => { .mockResolvedValueOnce(previousTotalSupply); // reading total supply const execution: PublicExecution = { contractAddress, functionData, args, callContext }; - const result = await executor.simulate(execution, GlobalVariables.empty()); + const result = await executor.simulate(execution, globalVariables); + expect(result.revertReason).toBeUndefined(); const recipientBalanceStorageSlot = computeSlotForMapping(new Fr(6n), recipient); const totalSupplyStorageSlot = new Fr(4n); @@ -210,7 +216,7 @@ describe('ACIR public execution simulator', () => { const recipientBalance = new Fr(20n); mockStore(senderBalance, recipientBalance); - const result = await executor.simulate(execution, GlobalVariables.empty()); + const result = await executor.simulate(execution, globalVariables); const expectedRecipientBalance = new Fr(160n); const expectedSenderBalance = new Fr(60n); @@ -236,7 +242,7 @@ describe('ACIR public execution simulator', () => { const recipientBalance = new Fr(20n); mockStore(senderBalance, recipientBalance); - const { reverted, revertReason } = await executor.simulate(execution, GlobalVariables.empty()); + const { reverted, revertReason } = await executor.simulate(execution, globalVariables); expect(reverted).toBe(true); expect(revertReason?.message).toMatch('Assertion failed: attempt to subtract with underflow'); }); @@ -325,7 +331,7 @@ describe('ACIR public execution simulator', () => { publicState.storageRead.mockResolvedValue(amount); const execution: PublicExecution = { contractAddress, functionData, args, callContext }; - const result = await executor.simulate(execution, GlobalVariables.empty()); + const result = await executor.simulate(execution, globalVariables); // Assert the note hash was created expect(result.newNoteHashes.length).toEqual(1); @@ -349,7 +355,7 @@ describe('ACIR public execution simulator', () => { publicContracts.getBytecode.mockResolvedValue(createL2ToL1MessagePublicArtifact.bytecode); const execution: PublicExecution = { contractAddress, functionData, args, callContext }; - const result = await executor.simulate(execution, GlobalVariables.empty()); + const result = await executor.simulate(execution, globalVariables); // Assert the l2 to l1 message was created expect(result.newL2ToL1Messages.length).toEqual(1); @@ -371,7 +377,7 @@ describe('ACIR public execution simulator', () => { publicContracts.getBytecode.mockResolvedValue(createNullifierPublicArtifact.bytecode); const execution: PublicExecution = { contractAddress, functionData, args, callContext }; - const result = await executor.simulate(execution, GlobalVariables.empty()); + const result = await executor.simulate(execution, globalVariables); // Assert the l2 to l1 message was created expect(result.newNullifiers.length).toEqual(1); @@ -702,7 +708,7 @@ describe('ACIR public execution simulator', () => { const execution: PublicExecution = { contractAddress, functionData, args, callContext }; executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header); - expect(() => executor.simulate(execution, GlobalVariables.empty())).not.toThrow(); + expect(() => executor.simulate(execution, globalVariables)).not.toThrow(); }); it('Throws when header is not as expected', async () => { @@ -712,7 +718,7 @@ describe('ACIR public execution simulator', () => { const execution: PublicExecution = { contractAddress, functionData, args, callContext }; executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header); - const { revertReason, reverted } = await executor.simulate(execution, GlobalVariables.empty()); + const { revertReason, reverted } = await executor.simulate(execution, globalVariables); expect(reverted).toBe(true); expect(revertReason?.message).toMatch(`Invalid header hash`); }); diff --git a/yarn-project/simulator/src/public/public_execution_context.ts b/yarn-project/simulator/src/public/public_execution_context.ts index b681df3f95a4..297df66fb3b6 100644 --- a/yarn-project/simulator/src/public/public_execution_context.ts +++ b/yarn-project/simulator/src/public/public_execution_context.ts @@ -7,7 +7,6 @@ import { CallContext, FunctionData, type FunctionSelector, - GasSettings, type GlobalVariables, type Header, } from '@aztec/circuits.js'; @@ -210,16 +209,18 @@ export class PublicExecutionContext extends TypedOracle { const portalAddress = (await this.contractsDb.getPortalContractAddress(targetContractAddress)) ?? EthAddress.ZERO; const functionData = new FunctionData(functionSelector, /*isPrivate=*/ false); + const { transactionFee, gasSettings, gasLeft } = this.execution.callContext; const callContext = CallContext.from({ msgSender: isDelegateCall ? this.execution.callContext.msgSender : this.execution.contractAddress, storageContractAddress: isDelegateCall ? this.execution.contractAddress : targetContractAddress, portalContractAddress: portalAddress, functionSelector, + gasLeft, // Propagate the same gas left as when we started since ACVM public functions don't have any metering isDelegateCall, isStaticCall, sideEffectCounter, - transactionFee: Fr.ZERO, // TODO(palla/gas-in-circuits) - gasSettings: GasSettings.empty(), // TODO(palla/gas-in-circuits) + gasSettings, + transactionFee, }); const nestedExecution: PublicExecution = { diff --git a/yarn-project/simulator/src/public/transitional_adaptors.ts b/yarn-project/simulator/src/public/transitional_adaptors.ts index 567852b124ef..00519f933a6c 100644 --- a/yarn-project/simulator/src/public/transitional_adaptors.ts +++ b/yarn-project/simulator/src/public/transitional_adaptors.ts @@ -5,7 +5,7 @@ import { ContractStorageRead, ContractStorageUpdateRequest, FunctionData, - GasSettings, + Gas, type GlobalVariables, type Header, L2ToL1Message, @@ -17,6 +17,7 @@ import { Fr } from '@aztec/foundation/fields'; import { type AvmContext } from '../avm/avm_context.js'; import { AvmExecutionEnvironment } from '../avm/avm_execution_environment.js'; +import { type AvmMachineState } from '../avm/avm_machine_state.js'; import { AvmContractCallResults } from '../avm/avm_message_call_result.js'; import { type JournalData } from '../avm/journal/journal.js'; import { Mov } from '../avm/opcodes/memory.js'; @@ -43,15 +44,17 @@ export function createAvmExecutionEnvironment( current.callContext.msgSender, // TODO: origin is not available current.callContext.msgSender, current.callContext.portalContractAddress, - /*feePerL1Gas=*/ Fr.zero(), - /*feePerL2Gas=*/ Fr.zero(), - /*feePerDaGas=*/ Fr.zero(), + globalVariables.gasFees.feePerL1Gas, + globalVariables.gasFees.feePerL2Gas, + globalVariables.gasFees.feePerDaGas, /*contractCallDepth=*/ Fr.zero(), header, globalVariables, current.callContext.isStaticCall, current.callContext.isDelegateCall, current.args, + current.callContext.gasSettings, + current.callContext.transactionFee, current.functionData.selector, ); } @@ -63,11 +66,12 @@ export function createPublicExecutionContext(avmContext: AvmContext, calldata: F storageContractAddress: avmContext.environment.storageAddress, portalContractAddress: avmContext.environment.portal, functionSelector: avmContext.environment.temporaryFunctionSelector, + gasLeft: Gas.from(avmContext.machineState.gasLeft), isDelegateCall: avmContext.environment.isDelegateCall, isStaticCall: avmContext.environment.isStaticCall, sideEffectCounter: sideEffectCounter, - gasSettings: GasSettings.empty(), // TODO(palla/gas-in-circuits) - transactionFee: Fr.ZERO, // TODO(palla/gas-in-circuits) + gasSettings: avmContext.environment.gasSettings, + transactionFee: avmContext.environment.transactionFee, }); const functionData = new FunctionData(avmContext.environment.temporaryFunctionSelector, /*isPrivate=*/ false); const execution: PublicExecution = { @@ -104,6 +108,7 @@ export async function convertAvmResults( executionContext: PublicExecutionContext, newWorldState: JournalData, result: AvmContractCallResults, + endMachineState: AvmMachineState, ): Promise { const execution = executionContext.execution; @@ -165,6 +170,7 @@ export async function convertAvmResults( unencryptedLogs, reverted: result.reverted, revertReason: result.revertReason ? createSimulationError(result.revertReason) : undefined, + gasLeft: endMachineState.gasLeft, }; }