Skip to content

Commit

Permalink
fix gas
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed May 16, 2024
1 parent 7d67152 commit ce68d73
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const size_t MAX_PUBLIC_DATA_READS_PER_CALL = 16;
const size_t MAX_NOTE_HASH_READ_REQUESTS_PER_CALL = 32;
const size_t MAX_NULLIFIER_READ_REQUESTS_PER_CALL = 2;
const size_t MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL = 2;
const size_t MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL = 1;
const size_t MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL = 16;
const size_t MAX_NOTE_ENCRYPTED_LOGS_PER_CALL = 16;
const size_t MAX_ENCRYPTED_LOGS_PER_CALL = 4;
const size_t MAX_UNENCRYPTED_LOGS_PER_CALL = 4;
Expand All @@ -27,7 +27,7 @@ const size_t MAX_NEW_L2_TO_L1_MSGS_PER_TX = 2;
const size_t MAX_NOTE_HASH_READ_REQUESTS_PER_TX = 128;
const size_t MAX_NULLIFIER_READ_REQUESTS_PER_TX = 8;
const size_t MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX = 8;
const size_t MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX = 4;
const size_t MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX = 64;
const size_t MAX_NOTE_ENCRYPTED_LOGS_PER_TX = 64;
const size_t MAX_ENCRYPTED_LOGS_PER_TX = 8;
const size_t MAX_UNENCRYPTED_LOGS_PER_TX = 8;
Expand Down Expand Up @@ -63,7 +63,7 @@ const size_t ARGS_HASH_CHUNK_COUNT = 64;
const size_t MAX_ARGS_LENGTH = ARGS_HASH_CHUNK_COUNT * ARGS_HASH_CHUNK_LENGTH;
const size_t INITIAL_L2_BLOCK_NUM = 1;
const size_t BLOB_SIZE_IN_BYTES = 31 * 4096;
const size_t NESTED_CALL_L2_GAS_BUFFER = 20000;
const size_t NESTED_CALL_L2_GAS_BUFFER = 40000;
const size_t MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 32000;
const size_t MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS = 3000;
const size_t MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS = 3000;
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ library Constants {
uint256 internal constant INITIALIZATION_SLOT_SEPARATOR = 1000_000_000;
uint256 internal constant INITIAL_L2_BLOCK_NUM = 1;
uint256 internal constant BLOB_SIZE_IN_BYTES = 31 * 4096;
uint256 internal constant NESTED_CALL_L2_GAS_BUFFER = 20000;
uint256 internal constant NESTED_CALL_L2_GAS_BUFFER = 40000;
uint256 internal constant MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 32000;
uint256 internal constant MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS = 3000;
uint256 internal constant MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS = 3000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ global INITIALIZATION_SLOT_SEPARATOR: Field = 1000_000_000;
global INITIAL_L2_BLOCK_NUM: Field = 1;
global BLOB_SIZE_IN_BYTES: Field = 31 * 4096;
// How much gas is subtracted from L2GASLEFT when making a nested public call by default in the AVM
global NESTED_CALL_L2_GAS_BUFFER = 20000;
global NESTED_CALL_L2_GAS_BUFFER = 40000;

// CONTRACT CLASS CONSTANTS
global MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS: u64 = 32000;
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const MAX_ARGS_LENGTH = ARGS_HASH_CHUNK_COUNT * ARGS_HASH_CHUNK_LENGTH;
export const INITIALIZATION_SLOT_SEPARATOR = 1000_000_000;
export const INITIAL_L2_BLOCK_NUM = 1;
export const BLOB_SIZE_IN_BYTES = 31 * 4096;
export const NESTED_CALL_L2_GAS_BUFFER = 20000;
export const NESTED_CALL_L2_GAS_BUFFER = 40000;
export const MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS = 32000;
export const MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS = 3000;
export const MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS = 3000;
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
Expand Up @@ -90,8 +90,8 @@ export function initGlobalVariables(overrides?: Partial<GlobalVariables>): Globa
*/
export function initMachineState(overrides?: Partial<AvmMachineState>): AvmMachineState {
return AvmMachineState.fromState({
l2GasLeft: overrides?.l2GasLeft ?? 100e6,
daGasLeft: overrides?.daGasLeft ?? 100e6,
l2GasLeft: overrides?.l2GasLeft ?? 1e8,
daGasLeft: overrides?.daGasLeft ?? 1e8,
});
}

Expand Down
22 changes: 17 additions & 5 deletions yarn-project/simulator/src/avm/opcodes/external_calls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { FunctionSelector, Gas } from '@aztec/circuits.js';
import { padArrayEnd } from '@aztec/foundation/collection';
import { createDebugLogger } from '@aztec/foundation/log';

import { strict as assert } from 'assert';

import { convertAvmResultsToPxResult, createPublicExecution } from '../../public/transitional_adaptors.js';
import type { AvmContext } from '../avm_context.js';
Expand Down Expand Up @@ -57,16 +60,25 @@ abstract class ExternalCall extends Instruction {
const callAddress = memory.getAs<Field>(addrOffset);
const calldataSize = memory.get(argsSizeOffset).toNumber();
const calldata = memory.getSlice(argsOffset, calldataSize).map(f => f.toFr());
const l2Gas = memory.get(gasOffset).toNumber();
const daGas = memory.getAs<Field>(gasOffset + 1).toNumber();
const allocatedL2Gas = memory.get(gasOffset).toNumber();
const allocatedDaGas = memory.getAs<Field>(gasOffset + 1).toNumber();
const functionSelector = memory.getAs<Field>(this.functionSelectorOffset).toFr();
// If we are already in a static call, we propagate the environment.
const callType = context.environment.isStaticCall ? 'STATICCALL' : this.type;

const allocatedGas = { l2Gas, daGas };
assert(
allocatedL2Gas <= context.machineState.l2GasLeft,
`Allocated more L2 gas than available: ${allocatedL2Gas} allocated, ${context.machineState.l2GasLeft} available.`,
);
assert(
allocatedDaGas <= context.machineState.daGasLeft,
`Allocated more DA gas than available: ${allocatedDaGas} allocated, ${context.machineState.daGasLeft} available.`,
);

const memoryOperations = { reads: calldataSize + 5, writes: 1 + this.retSize, indirect: this.indirect };
const totalGas = sumGas(this.gasCost(memoryOperations), allocatedGas);
context.machineState.consumeGas(totalGas);
const allocatedGas = { l2Gas: allocatedL2Gas, daGas: allocatedDaGas };
const totalGasToConsume = sumGas(this.gasCost(memoryOperations), allocatedGas);
context.machineState.consumeGas(totalGasToConsume);

// TRANSITIONAL: This should be removed once the kernel handles and entire enqueued call per circuit
const nestedContext = context.createNestedContractCallContext(
Expand Down

0 comments on commit ce68d73

Please sign in to comment.