Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,26 +1732,6 @@ TEST_F(AvmExecutionTests, daGasLeft)
validate_trace(std::move(trace), public_inputs);
}

TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated)
{
GTEST_SKIP();
std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender)
"00" // Indirect flag
+ "0007" + to_hex(static_cast<uint8_t>(EnvironmentVariable::SENDER)); // addr 7

std::vector<FF> calldata = {};
std::vector<FF> returndata = {};
public_inputs.gas_settings.gas_limits.l2_gas = MAX_L2_GAS_PER_ENQUEUED_CALL;

auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse_bytecode_statically(bytecode);

ExecutionHints execution_hints;
EXPECT_THROW_WITH_MESSAGE(gen_trace(bytecode, calldata, public_inputs, returndata, execution_hints),
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for "
"execution of an enqueued call");
}

// Should throw whenever the wrong number of public inputs are provided
// TEST_F(AvmExecutionTests, ExecutorThrowsWithIncorrectNumberOfPublicInputs)
// {
Expand Down
15 changes: 0 additions & 15 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ template <typename FF_> VmPublicInputs_<FF_> convert_public_inputs(std::vector<F
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 constexpr (IsAnyOf<FF_, bb::fr>) {
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");
}
} else {
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET].get_value() > 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
7 changes: 5 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,12 @@ AvmTraceBuilder::AvmTraceBuilder(AvmPublicInputs public_inputs,
, intermediate_tree_snapshots(public_inputs.start_tree_snapshots)
, bytecode_trace_builder(execution_hints.all_contract_bytecode)
{
// Only allocate up to the maximum L2 gas for execution
// TODO: constrain this!
auto const allocated_l2_gas = std::min(new_public_inputs.gas_settings.gas_limits.l2_gas,
static_cast<uint32_t>(MAX_L2_GAS_PER_TX_PUBLIC_PORTION));
// TODO: think about cast
gas_trace_builder.set_initial_gas(static_cast<uint32_t>(new_public_inputs.gas_settings.gas_limits.l2_gas -
new_public_inputs.start_gas_used.l2_gas),
gas_trace_builder.set_initial_gas(static_cast<uint32_t>(allocated_l2_gas - new_public_inputs.start_gas_used.l2_gas),
static_cast<uint32_t>(new_public_inputs.gas_settings.gas_limits.da_gas -
new_public_inputs.start_gas_used.da_gas));
}
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define MAX_L2_TO_L1_MSGS_PER_TX 8
#define MAX_UNENCRYPTED_LOGS_PER_TX 8
#define MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS 3000
#define MAX_L2_GAS_PER_ENQUEUED_CALL 12000000
#define MAX_L2_GAS_PER_TX_PUBLIC_PORTION 12000000
#define AZTEC_ADDRESS_LENGTH 1
#define GAS_FEES_LENGTH 2
#define GAS_LENGTH 2
Expand Down
4 changes: 2 additions & 2 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ library Constants {
14061769416655647708490531650437236735160113654556896985372298487345;
uint256 internal constant DEFAULT_GAS_LIMIT = 1000000000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
uint256 internal constant MAX_L2_GAS_PER_TX_PUBLIC_PORTION = 12000000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DA_BYTES_PER_FIELD = 32;
uint256 internal constant DA_GAS_PER_BYTE = 16;
uint256 internal constant FIXED_DA_GAS = 512;
uint256 internal constant FIXED_L2_GAS = 512;
uint256 internal constant FIXED_AVM_STARTUP_L2_GAS = 1024;
uint256 internal constant FIXED_AVM_STARTUP_L2_GAS = 50000;
uint256 internal constant L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024;
uint256 internal constant L2_GAS_PER_READ_MERKLE_HASH = 30;
uint256 internal constant L2_GAS_PER_WRITE_MERKLE_HASH = 40;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
// GAS DEFAULTS
pub global DEFAULT_GAS_LIMIT: u32 = 1_000_000_000;
pub global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 12_000_000;
pub global MAX_L2_GAS_PER_ENQUEUED_CALL: u32 = 12_000_000;
pub global MAX_L2_GAS_PER_TX_PUBLIC_PORTION: u32 = 12_000_000;
Comment thread
dbanks12 marked this conversation as resolved.
pub global DEFAULT_MAX_FEE_PER_GAS: Field = 10;
pub global DA_BYTES_PER_FIELD: u32 = 32;
pub global DA_GAS_PER_BYTE: u32 = 16;
Expand All @@ -182,7 +182,7 @@ pub global FIXED_DA_GAS: u32 = 512;
// pays for fixed tx costs like validation, and updating state roots
pub global FIXED_L2_GAS: u32 = 512;
// base cost for a single public call
pub global FIXED_AVM_STARTUP_L2_GAS: u32 = 1024;
pub global FIXED_AVM_STARTUP_L2_GAS: u32 = 50_000;

// Some tree insertions incur an additional cost associated with
// the new database entry to be stored by all network participants.
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
14061769416655647708490531650437236735160113654556896985372298487345n;
export const DEFAULT_GAS_LIMIT = 1000000000;
export const DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
export const MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
export const MAX_L2_GAS_PER_TX_PUBLIC_PORTION = 12000000;
export const DEFAULT_MAX_FEE_PER_GAS = 10;
export const DA_BYTES_PER_FIELD = 32;
export const DA_GAS_PER_BYTE = 16;
export const FIXED_DA_GAS = 512;
export const FIXED_L2_GAS = 512;
export const FIXED_AVM_STARTUP_L2_GAS = 1024;
export const FIXED_AVM_STARTUP_L2_GAS = 50000;
export const L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024;
export const L2_GAS_PER_READ_MERKLE_HASH = 30;
export const L2_GAS_PER_WRITE_MERKLE_HASH = 40;
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/scripts/constants.in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const CPP_CONSTANTS = [
'MEM_TAG_U64',
'MEM_TAG_U128',
'MEM_TAG_FF',
'MAX_L2_GAS_PER_ENQUEUED_CALL',
'MAX_L2_GAS_PER_TX_PUBLIC_PORTION',
'MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS',
];

Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/avm/avm_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Fr,
type FunctionSelector,
type GlobalVariables,
MAX_L2_GAS_PER_ENQUEUED_CALL,
MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
} from '@aztec/circuits.js';
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';

Expand Down Expand Up @@ -44,8 +44,8 @@ export class AvmSimulator {

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`,
context.machineState.gasLeft.l2Gas <= MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
`Cannot allocate more than ${MAX_L2_GAS_PER_TX_PUBLIC_PORTION} to the AVM for execution.`,
);
this.log = createDebugLogger(`aztec:avm_simulator:core(f:${context.environment.functionSelector.toString()})`);
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/simulator/src/avm/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNoirCallStackUnresolved } from '@aztec/circuit-types';
import { GasFees, GlobalVariables, MAX_L2_GAS_PER_ENQUEUED_CALL } from '@aztec/circuits.js';
import { GasFees, GlobalVariables, MAX_L2_GAS_PER_TX_PUBLIC_PORTION } from '@aztec/circuits.js';
import { type FunctionArtifact, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -94,7 +94,7 @@ export function initGlobalVariables(overrides?: Partial<GlobalVariables>): Globa
*/
export function initMachineState(overrides?: Partial<AvmMachineState>): AvmMachineState {
return AvmMachineState.fromState({
l2GasLeft: overrides?.l2GasLeft ?? MAX_L2_GAS_PER_ENQUEUED_CALL,
l2GasLeft: overrides?.l2GasLeft ?? MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
daGasLeft: overrides?.daGasLeft ?? 1e8,
});
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/simulator/src/public/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
GasSettings,
GlobalVariables,
Header,
MAX_L2_GAS_PER_ENQUEUED_CALL,
MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
PartialPrivateTailPublicInputsForPublic,
PrivateKernelTailCircuitPublicInputs,
type PublicFunction,
Expand Down Expand Up @@ -127,7 +127,7 @@ export function createTxForPublicCall(
): Tx {
const callRequest = executionRequest.toCallRequest();
// use max limits
const gasLimits = new Gas(DEFAULT_GAS_LIMIT, MAX_L2_GAS_PER_ENQUEUED_CALL);
const gasLimits = new Gas(DEFAULT_GAS_LIMIT, MAX_L2_GAS_PER_TX_PUBLIC_PORTION);

const forPublic = PartialPrivateTailPublicInputsForPublic.empty();
// TODO(#9269): Remove this fake nullifier method as we move away from 1st nullifier as hash.
Expand Down
19 changes: 15 additions & 4 deletions yarn-project/simulator/src/public/public_tx_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type GasSettings,
type GlobalVariables,
type Header,
MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
type PrivateToPublicAccumulatedData,
type PublicCallRequest,
RevertCode,
Expand Down Expand Up @@ -243,13 +244,13 @@ export class PublicTxContext {
}

/**
* How much gas is left for the specified phase?
* How much gas is left as of the specified phase?
*/
getGasLeftForPhase(phase: TxExecutionPhase): Gas {
getGasLeftAtPhase(phase: TxExecutionPhase): Gas {
if (phase === TxExecutionPhase.TEARDOWN) {
return this.gasSettings.teardownGasLimits;
return applyMaxToAvailableGas(this.gasSettings.teardownGasLimits);
} else {
return this.gasSettings.gasLimits.sub(this.gasUsed);
return applyMaxToAvailableGas(this.gasSettings.gasLimits).sub(this.gasUsed);
}
}

Expand Down Expand Up @@ -414,3 +415,13 @@ class PhaseStateManager {
this.currentlyActiveStateManager = undefined;
}
}

/**
* Apply L2 gas maximum.
*/
function applyMaxToAvailableGas(availableGas: Gas) {
return new Gas(
/*daGas=*/ availableGas.daGas,
/*l2Gas=*/ Math.min(availableGas.l2Gas, MAX_L2_GAS_PER_TX_PUBLIC_PORTION),
);
}
21 changes: 4 additions & 17 deletions yarn-project/simulator/src/public/public_tx_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ import {
UnencryptedFunctionL2Logs,
} from '@aztec/circuit-types';
import { type AvmSimulationStats } from '@aztec/circuit-types/stats';
import {
type Fr,
Gas,
type GlobalVariables,
MAX_L2_GAS_PER_ENQUEUED_CALL,
type PublicCallRequest,
type RevertCode,
} from '@aztec/circuits.js';
import { type Fr, type Gas, type GlobalVariables, type PublicCallRequest, type RevertCode } from '@aztec/circuits.js';
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
import { type TelemetryClient } from '@aztec/telemetry-client';
Expand Down Expand Up @@ -255,23 +248,17 @@ export class PublicTxSimulator {
const selector = executionRequest.callContext.functionSelector;
const fnName = await getPublicFunctionDebugName(this.worldStateDB, address, selector, executionRequest.args);

const availableGas = context.getGasLeftForPhase(phase);
// Gas allocated to an enqueued call can be different from the available gas
// if there is more gas available than the max allocation per enqueued call.
const allocatedGas = new Gas(
/*daGas=*/ availableGas.daGas,
/*l2Gas=*/ Math.min(availableGas.l2Gas, MAX_L2_GAS_PER_ENQUEUED_CALL),
);
const allocatedGas = context.getGasLeftAtPhase(phase);

const result = await this.simulateEnqueuedCallInternal(
context.state.getActiveStateManager(),
executionRequest,
allocatedGas,
context.getTransactionFee(phase),
/*transactionFee=*/ context.getTransactionFee(phase),
fnName,
);

const gasUsed = allocatedGas.sub(result.gasLeft);
const gasUsed = allocatedGas.sub(result.gasLeft); // by enqueued call
context.consumeGas(phase, gasUsed);
this.log.verbose(
`[AVM] Enqueued public call consumed ${gasUsed.l2Gas} L2 gas ending with ${result.gasLeft.l2Gas} L2 gas left.`,
Expand Down