1
- import {
2
- type AztecAddress ,
3
- Fr ,
4
- type FunctionSelector ,
5
- type GlobalVariables ,
6
- MAX_L2_GAS_PER_ENQUEUED_CALL ,
7
- } from '@aztec/circuits.js' ;
1
+ import { type AztecAddress , Fr , type GlobalVariables , MAX_L2_GAS_PER_ENQUEUED_CALL } from '@aztec/circuits.js' ;
8
2
import { type Logger , createLogger } from '@aztec/foundation/log' ;
9
3
10
4
import { strict as assert } from 'assert' ;
@@ -49,24 +43,35 @@ export class AvmSimulator {
49
43
private tallyPrintFunction = ( ) => { } ;
50
44
private tallyInstructionFunction = ( _a : number , _b : string , _c : Gas ) => { } ;
51
45
46
+ // Test Purposes only: Logger will not have the proper function name. Use this constructor for testing purposes
47
+ // only. Otherwise, use build() below.
52
48
constructor ( private context : AvmContext , private instructionSet : InstructionSet = INSTRUCTION_SET ( ) ) {
53
49
assert (
54
50
context . machineState . gasLeft . l2Gas <= MAX_L2_GAS_PER_ENQUEUED_CALL ,
55
51
`Cannot allocate more than ${ MAX_L2_GAS_PER_ENQUEUED_CALL } to the AVM for execution of an enqueued call` ,
56
52
) ;
57
- this . log = createLogger ( `simulator:avm:core(f: ${ context . environment . functionSelector . toString ( ) } )` ) ;
53
+ this . log = createLogger ( `simulator:avm(calldata[0]: ${ context . environment . calldata [ 0 ] } )` ) ;
58
54
// TODO(palla/log): Should tallies be printed on debug, or only on trace?
59
55
if ( this . log . isLevelEnabled ( 'debug' ) ) {
60
56
this . tallyPrintFunction = this . printOpcodeTallies ;
61
57
this . tallyInstructionFunction = this . tallyInstruction ;
62
58
}
63
59
}
64
60
65
- public static create (
61
+ // Factory to have a proper function name in the logger. Retrieving the name is asynchronous and
62
+ // cannot be done as part of the constructor.
63
+ public static async build ( context : AvmContext ) : Promise < AvmSimulator > {
64
+ const simulator = new AvmSimulator ( context ) ;
65
+ const fnName = await context . persistableState . getPublicFunctionDebugName ( context . environment ) ;
66
+ simulator . log = createLogger ( `simulator:avm(f:${ fnName } )` ) ;
67
+
68
+ return simulator ;
69
+ }
70
+
71
+ public static async create (
66
72
stateManager : AvmPersistableStateManager ,
67
73
address : AztecAddress ,
68
74
sender : AztecAddress ,
69
- functionSelector : FunctionSelector , // may be temporary (#7224)
70
75
transactionFee : Fr ,
71
76
globals : GlobalVariables ,
72
77
isStaticCall : boolean ,
@@ -76,7 +81,6 @@ export class AvmSimulator {
76
81
const avmExecutionEnv = new AvmExecutionEnvironment (
77
82
address ,
78
83
sender ,
79
- functionSelector ,
80
84
/*contractCallDepth=*/ Fr . zero ( ) ,
81
85
transactionFee ,
82
86
globals ,
@@ -86,8 +90,7 @@ export class AvmSimulator {
86
90
87
91
const avmMachineState = new AvmMachineState ( allocatedGas ) ;
88
92
const avmContext = new AvmContext ( stateManager , avmExecutionEnv , avmMachineState ) ;
89
- const instructionSet = INSTRUCTION_SET ( ) ;
90
- return new AvmSimulator ( avmContext , instructionSet ) ;
93
+ return await AvmSimulator . build ( avmContext ) ;
91
94
}
92
95
93
96
/**
@@ -98,11 +101,12 @@ export class AvmSimulator {
98
101
if ( ! bytecode ) {
99
102
// revert, consuming all gas
100
103
const message = `No bytecode found at: ${ this . context . environment . address } . Reverting...` ;
104
+ const fnName = await this . context . persistableState . getPublicFunctionDebugName ( this . context . environment ) ;
101
105
const revertReason = new AvmRevertReason (
102
106
message ,
103
107
/*failingFunction=*/ {
104
108
contractAddress : this . context . environment . address ,
105
- functionSelector : this . context . environment . functionSelector ,
109
+ functionName : fnName ,
106
110
} ,
107
111
/*noirCallStack=*/ [ ] ,
108
112
) ;
@@ -176,7 +180,7 @@ export class AvmSimulator {
176
180
177
181
const output = machineState . getOutput ( ) ;
178
182
const reverted = machineState . getReverted ( ) ;
179
- const revertReason = reverted ? revertReasonFromExplicitRevert ( output , this . context ) : undefined ;
183
+ const revertReason = reverted ? await revertReasonFromExplicitRevert ( output , this . context ) : undefined ;
180
184
const results = new AvmContractCallResult ( reverted , output , machineState . gasLeft , revertReason ) ;
181
185
this . log . debug ( `Context execution results: ${ results . toString ( ) } ` ) ;
182
186
@@ -190,7 +194,7 @@ export class AvmSimulator {
190
194
throw err ;
191
195
}
192
196
193
- const revertReason = revertReasonFromExceptionalHalt ( err , this . context ) ;
197
+ const revertReason = await revertReasonFromExceptionalHalt ( err , this . context ) ;
194
198
// Note: "exceptional halts" cannot return data, hence [].
195
199
const results = new AvmContractCallResult ( /*reverted=*/ true , /*output=*/ [ ] , machineState . gasLeft , revertReason ) ;
196
200
this . log . debug ( `Context execution results: ${ results . toString ( ) } ` ) ;
0 commit comments