Skip to content

Commit

Permalink
Apply suggestions from code review and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Mar 27, 2024
1 parent 47c385d commit de82a20
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"./json-rpc/client": "./dest/json-rpc/client/index.js",
"./log": "./dest/log/index.js",
"./mutex": "./dest/mutex/index.js",
"./number": "./dest/number/index.js",
"./fields": "./dest/fields/index.js",
"./retry": "./dest/retry/index.js",
"./running-promise": "./dest/running-promise/index.js",
Expand Down
9 changes: 0 additions & 9 deletions yarn-project/foundation/src/number/index.ts

This file was deleted.

15 changes: 8 additions & 7 deletions yarn-project/simulator/src/avm/avm_gas_cost.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AvmMachineState } from './avm_machine_state.js';
import { TypeTag } from './avm_memory_types.js';
import { AvmSimulator } from './avm_simulator.js';
import { initContext, initExecutionEnvironment } from './fixtures/index.js';
import { initContext } from './fixtures/index.js';
import { Add, CalldataCopy, Div, Mul, Set as SetInstruction, Sub } from './opcodes/index.js';
import { encodeToBytecode } from './serialization/bytecode_serialization.js';

Expand All @@ -19,13 +18,15 @@ describe('AVM simulator: dynamic gas costs per instruction', () => {
[new Div(/*indirect=*/ 3, /*inTag=*/ TypeTag.UINT8, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 3), [20, 0, 0]],
] as const)('computes gas cost for %s', async (instruction, [l2GasCost, l1GasCost, daGasCost]) => {
const bytecode = encodeToBytecode([instruction]);
const context = initContext();
const {
l2GasLeft: initialL2GasLeft,
daGasLeft: initialDaGasLeft,
l1GasLeft: initialL1GasLeft,
} = context.machineState;

const context = initContext({ env: initExecutionEnvironment({ calldata: [] }) });
const initialState = AvmMachineState.fromState(context.machineState);
const { l2GasLeft: initialL2GasLeft, daGasLeft: initialDaGasLeft, l1GasLeft: initialL1GasLeft } = initialState;
const simulator = new AvmSimulator(context);
await new AvmSimulator(context).executeBytecode(bytecode);

await simulator.executeBytecode(bytecode);
expect(initialL2GasLeft - context.machineState.l2GasLeft).toEqual(l2GasCost);
expect(initialL1GasLeft - context.machineState.l1GasLeft).toEqual(l1GasCost);
expect(initialDaGasLeft - context.machineState.daGasLeft).toEqual(daGasCost);
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/avm/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export function initGlobalVariables(overrides?: Partial<GlobalVariables>): Globa
*/
export function initMachineState(overrides?: Partial<AvmMachineState>): AvmMachineState {
return AvmMachineState.fromState({
l1GasLeft: overrides?.l1GasLeft ?? 1e6,
l2GasLeft: overrides?.l2GasLeft ?? 1e6,
daGasLeft: overrides?.daGasLeft ?? 1e6,
l1GasLeft: overrides?.l1GasLeft ?? 100e6,
l2GasLeft: overrides?.l2GasLeft ?? 100e6,
daGasLeft: overrides?.daGasLeft ?? 100e6,
});
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/avm/opcodes/addressing_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum AddressingMode {
export class Addressing {
public constructor(
/** The addressing mode for each operand. The length of this array is the number of operands of the instruction. */
private readonly modePerOperand: AddressingMode[],
public readonly modePerOperand: AddressingMode[],
) {
assert(modePerOperand.length <= 8, 'At most 8 operands are supported');
}
Expand Down
11 changes: 7 additions & 4 deletions yarn-project/simulator/src/avm/opcodes/arithmetic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { countSetBits } from '@aztec/foundation/number';

import type { AvmContext } from '../avm_context.js';
import { GasCost, GasCostConstants, getGasCostMultiplierFromTypeTag, makeGasCost } from '../avm_gas_cost.js';
import { Field, MemoryValue, TypeTag } from '../avm_memory_types.js';
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
import { Addressing, AddressingMode } from './addressing_mode.js';
import { Instruction } from './instruction.js';
import { ThreeOperandInstruction } from './instruction_impl.js';

Expand All @@ -21,9 +20,13 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
}

protected gasCost(): GasCost {
const indirectCount = Addressing.fromWire(this.indirect).modePerOperand.filter(
mode => mode === AddressingMode.INDIRECT,
).length;

const l2Gas =
countSetBits(this.indirect) * GasCostConstants.ARITHMETIC_COST_PER_INDIRECT_ACCESS +
GasCostConstants.ARITHMETIC_COST_PER_BYTE * getGasCostMultiplierFromTypeTag(this.inTag);
indirectCount * GasCostConstants.ARITHMETIC_COST_PER_INDIRECT_ACCESS +
getGasCostMultiplierFromTypeTag(this.inTag) * GasCostConstants.ARITHMETIC_COST_PER_BYTE;
return makeGasCost({ l2Gas });
}

Expand Down

0 comments on commit de82a20

Please sign in to comment.