Skip to content

Commit

Permalink
feat: new per-enqueued-call gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Oct 4, 2024
1 parent 4873c7b commit d647ff2
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 2 deletions.
9 changes: 9 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "barretenberg/vm/avm/trace/common.hpp"
#include "barretenberg/vm/avm/trace/trace.hpp"
#include "barretenberg/vm/constants.hpp"

namespace bb::avm_trace {

Expand Down Expand Up @@ -37,6 +38,14 @@ template <typename FF_> VmPublicInputs<FF_> convert_public_inputs(std::vector<FF
throw_or_abort("Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
}

// WARNING: this must be constrained by the kernel!
// Here this is just a sanity check to prevent generation of proofs that
// will be thrown out by the kernel anyway.
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET] > MAX_L2_GAS_PER_ENQUEUED_CALL) {
throw_or_abort(
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
}

std::array<FF_, KERNEL_INPUTS_LENGTH>& kernel_inputs = std::get<KERNEL_INPUTS>(public_inputs);

// Copy items from PublicCircuitPublicInputs vector to public input columns
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL 16
#define MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL 16
#define MAX_UNENCRYPTED_LOGS_PER_CALL 4
#define MAX_L2_GAS_PER_ENQUEUED_CALL 1500000
#define AZTEC_ADDRESS_LENGTH 1
#define GAS_FEES_LENGTH 2
#define GAS_LENGTH 2
Expand Down
1 change: 1 addition & 0 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ library Constants {
14061769416655647708490531650437236735160113654556896985372298487345;
uint256 internal constant DEFAULT_GAS_LIMIT = 1000000000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 1500000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
uint256 internal constant DA_BYTES_PER_FIELD = 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use dep::types::{
kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs, enqueued_call_data::EnqueuedCallData,
public_call_request::PublicCallRequest, validation_requests::PublicValidationRequestArrayLengths
},
constants::MAX_L2_GAS_PER_ENQUEUED_CALL,
utils::arrays::array_length
};

Expand Down Expand Up @@ -89,15 +90,19 @@ impl EnqueuedCallDataValidator {
// Validates that the start gas injected into the vm circuit matches the remaining gas.
fn validate_start_gas(self, previous_kernel: PublicKernelCircuitPublicInputs) {
let enqueued_call_start_gas = self.enqueued_call.data.start_gas_left;
// NOTE: the AVM circuit will fail to generate a proof if its "start gas" is > MAX_L2_GAS_PER_ENQUEUED_CALL,
// so the kernel never allocates more than that maximum to one enqueued call.
if self.phase != PublicKernelPhase.TEARDOWN {
// An enqueued call's start gas is the remaining gas left in the transaction after the previous kernel.
let tx_gas_limits = previous_kernel.constants.tx_context.gas_settings.gas_limits;
let computed_start_gas = tx_gas_limits.sub(previous_kernel.end.gas_used).sub(previous_kernel.end_non_revertible.gas_used);
let mut computed_start_gas = tx_gas_limits.sub(previous_kernel.end.gas_used).sub(previous_kernel.end_non_revertible.gas_used);
computed_start_gas.l2_gas = std::cmp::min(computed_start_gas.l2_gas, MAX_L2_GAS_PER_ENQUEUED_CALL);
assert_eq(
enqueued_call_start_gas, computed_start_gas, "Start gas for enqueued call does not match transaction gas left"
);
} else {
let teardown_gas_limit = previous_kernel.constants.tx_context.gas_settings.teardown_gas_limits;
let mut teardown_gas_limit = previous_kernel.constants.tx_context.gas_settings.teardown_gas_limits;
teardown_gas_limit.l2_gas = std::cmp::min(teardown_gas_limit.l2_gas, MAX_L2_GAS_PER_ENQUEUED_CALL);
assert_eq(
enqueued_call_start_gas, teardown_gas_limit, "Start gas for enqueued call does not match teardown gas allocation"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ impl PublicKernelOutputComposer {
let gas_used = self.output_builder.end.gas_used;
self.output_builder.end = PublicAccumulatedDataBuilder::empty();
self.output_builder.end.gas_used = gas_used;
// TODO: consume all remaining gas on revert?
//self.output_builder.end.gas_used = self.output_builder.constants.tx_context.gas_settings.gas_limits;
}
}

Expand All @@ -143,6 +145,10 @@ impl PublicKernelOutputComposer {

fn update_gas_used(&mut self, enqueued_call: VMCircuitPublicInputs, phase: u8) {
let call_gas_used = enqueued_call.accumulated_data.gas_used;
// TODO: since we're using gas used here, this should be fine
// but if vm circuit public inputs change to use expose gas_left instead,
// then we need to compute the call's gas used as start_gas_left - end_gas_left
//let call_gas_used = enqueued_call.start_gas_left - enqueued_call.accumulated_data.gas_left;
if phase == PublicKernelPhase.SETUP {
self.output_builder.end_non_revertible.gas_used += call_gas_used;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE = 0x85864497636cf755ae7bd
// GAS DEFAULTS
global DEFAULT_GAS_LIMIT: u32 = 1_000_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
global MAX_L2_GAS_PER_ENQUEUED_CALL: u32 = 1_500_000;
global DEFAULT_MAX_FEE_PER_GAS: Field = 10;
global DEFAULT_INCLUSION_FEE: Field = 0;
global DA_BYTES_PER_FIELD: u32 = 32;
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
14061769416655647708490531650437236735160113654556896985372298487345n;
export const DEFAULT_GAS_LIMIT = 1000000000;
export const DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
export const MAX_L2_GAS_PER_ENQUEUED_CALL = 1500000;
export const DEFAULT_MAX_FEE_PER_GAS = 10;
export const DEFAULT_INCLUSION_FEE = 0;
export const DA_BYTES_PER_FIELD = 32;
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/scripts/constants.in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const CPP_CONSTANTS = [
'MEM_TAG_U64',
'MEM_TAG_U128',
'MEM_TAG_FF',
'MAX_L2_GAS_PER_ENQUEUED_CALL',
];

const PIL_CONSTANTS = [
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/simulator/src/avm/avm_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
} from './errors.js';
import type { Instruction } from './opcodes/index.js';
import { decodeFromBytecode } from './serialization/bytecode_serialization.js';
import { MAX_L2_GAS_PER_ENQUEUED_CALL } from '@aztec/circuits.js';

export class AvmSimulator {
private log: DebugLogger;
private bytecode: Buffer | undefined;

constructor(private context: AvmContext) {
assert(context.machineState.gasLeft.l2Gas > MAX_L2_GAS_PER_ENQUEUED_CALL, `Cannot allocate more than ${MAX_L2_GAS_PER_ENQUEUED_CALL} to the AVM for execution of an enqueued call`);
this.log = createDebugLogger(`aztec:avm_simulator:core(f:${context.environment.functionSelector.toString()})`);
}

Expand Down

0 comments on commit d647ff2

Please sign in to comment.