Skip to content

Commit

Permalink
chore(avm-simulator): better type env getters
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Mar 5, 2024
1 parent a3ee1f7 commit 4225a1e
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions yarn-project/simulator/src/avm/opcodes/environment_getters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Fr } from '@aztec/circuits.js';

import type { AvmContext } from '../avm_context.js';
import type { AvmExecutionEnvironment } from '../avm_execution_environment.js';
import { Field } from '../avm_memory_types.js';
import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
import { Instruction } from './instruction.js';

type GetterResult = Fr | number | bigint;

abstract class GetterInstruction extends Instruction {
// Informs (de)serialization. See Instruction.deserialize.
static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT32];
Expand All @@ -18,14 +22,14 @@ abstract class GetterInstruction extends Instruction {
context.machineState.incrementPc();
}

protected abstract getIt(env: AvmExecutionEnvironment): any;
protected abstract getIt(env: AvmExecutionEnvironment): GetterResult;
}

export class Address extends GetterInstruction {
static type: string = 'ADDRESS';
static readonly opcode: Opcode = Opcode.ADDRESS;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.address;
}
}
Expand All @@ -34,7 +38,7 @@ export class StorageAddress extends GetterInstruction {
static type: string = 'STORAGEADDRESS';
static readonly opcode: Opcode = Opcode.STORAGEADDRESS;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.storageAddress;
}
}
Expand All @@ -43,7 +47,7 @@ export class Sender extends GetterInstruction {
static type: string = 'SENDER';
static readonly opcode: Opcode = Opcode.SENDER;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.sender;
}
}
Expand All @@ -52,7 +56,7 @@ export class Origin extends GetterInstruction {
static type: string = 'ORIGIN';
static readonly opcode: Opcode = Opcode.ORIGIN;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.origin;
}
}
Expand All @@ -61,7 +65,7 @@ export class FeePerL1Gas extends GetterInstruction {
static type: string = 'FEEPERL1GAS';
static readonly opcode: Opcode = Opcode.FEEPERL1GAS;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.feePerL1Gas;
}
}
Expand All @@ -70,7 +74,7 @@ export class FeePerL2Gas extends GetterInstruction {
static type: string = 'FEEPERL2GAS';
static readonly opcode: Opcode = Opcode.FEEPERL2GAS;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.feePerL2Gas;
}
}
Expand All @@ -79,7 +83,7 @@ export class FeePerDAGas extends GetterInstruction {
static type: string = 'FEEPERDAGAS';
static readonly opcode: Opcode = Opcode.FEEPERDAGAS;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.feePerDaGas;
}
}
Expand All @@ -88,7 +92,7 @@ export class Portal extends GetterInstruction {
static type: string = 'PORTAL';
static readonly opcode: Opcode = Opcode.PORTAL;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.portal.toField();
}
}
Expand All @@ -97,7 +101,7 @@ export class ChainId extends GetterInstruction {
static type: string = 'CHAINID';
static readonly opcode: Opcode = Opcode.CHAINID;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.globals.chainId;
}
}
Expand All @@ -106,7 +110,7 @@ export class Version extends GetterInstruction {
static type: string = 'VERSION';
static readonly opcode: Opcode = Opcode.VERSION;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.globals.version;
}
}
Expand All @@ -115,7 +119,7 @@ export class BlockNumber extends GetterInstruction {
static type: string = 'BLOCKNUMBER';
static readonly opcode: Opcode = Opcode.BLOCKNUMBER;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.globals.blockNumber;
}
}
Expand All @@ -124,7 +128,7 @@ export class Timestamp extends GetterInstruction {
static type: string = 'TIMESTAMP';
static readonly opcode: Opcode = Opcode.TIMESTAMP;

protected getIt(env: AvmExecutionEnvironment): any {
protected getIt(env: AvmExecutionEnvironment): GetterResult {
return env.globals.timestamp;
}
}
Expand Down

0 comments on commit 4225a1e

Please sign in to comment.