@@ -2,7 +2,10 @@ import { PublicExecutionRequest, Tx } from '@aztec/circuit-types';
2
2
import {
3
3
type AvmCircuitInputs ,
4
4
CallContext ,
5
+ type ContractClassPublic ,
6
+ type ContractInstanceWithAddress ,
5
7
DEFAULT_GAS_LIMIT ,
8
+ type FunctionSelector ,
6
9
Gas ,
7
10
GasFees ,
8
11
GasSettings ,
@@ -20,19 +23,16 @@ import {
20
23
computePublicBytecodeCommitment ,
21
24
} from '@aztec/circuits.js' ;
22
25
import { makeContractClassPublic , makeContractInstanceFromClassId } from '@aztec/circuits.js/testing' ;
26
+ import { type ContractArtifact } from '@aztec/foundation/abi' ;
23
27
import { AztecAddress } from '@aztec/foundation/aztec-address' ;
24
28
import { Fr , Point } from '@aztec/foundation/fields' ;
25
29
import { openTmpStore } from '@aztec/kv-store/utils' ;
26
- import { PublicTxSimulator , type WorldStateDB } from '@aztec/simulator' ;
30
+ import { PublicTxSimulator , WorldStateDB } from '@aztec/simulator' ;
27
31
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop' ;
28
32
import { MerkleTrees } from '@aztec/world-state' ;
29
33
30
- import { mock } from 'jest-mock-extended' ;
31
-
32
34
import { getAvmTestContractBytecode , getAvmTestContractFunctionSelector } from '../../avm/fixtures/index.js' ;
33
35
34
- const TIMESTAMP = new Fr ( 99833 ) ;
35
-
36
36
/**
37
37
* If assertionErrString is set, we expect a (non exceptional halting) revert due to a failing assertion and
38
38
* we check that the revert reason error contains this string. However, the circuit must correctly prove the
@@ -49,46 +49,14 @@ export async function simulateAvmTestContractGenerateCircuitInputs(
49
49
50
50
const globalVariables = GlobalVariables . empty ( ) ;
51
51
globalVariables . gasFees = GasFees . empty ( ) ;
52
- globalVariables . timestamp = TIMESTAMP ;
52
+ globalVariables . timestamp = new Fr ( 99833 ) ;
53
53
54
- const worldStateDB = mock < WorldStateDB > ( ) ;
55
54
const telemetry = new NoopTelemetryClient ( ) ;
56
55
const merkleTrees = await ( await MerkleTrees . new ( openTmpStore ( ) , telemetry ) ) . fork ( ) ;
57
- worldStateDB . getMerkleInterface . mockReturnValue ( merkleTrees ) ;
58
-
59
- // Top level contract call
60
- const bytecode = getAvmTestContractBytecode ( 'public_dispatch' ) ;
61
- const dispatchSelector = getAvmTestContractFunctionSelector ( 'public_dispatch' ) ;
62
- const publicFn : PublicFunction = { bytecode, selector : dispatchSelector } ;
63
- const contractClass = makeContractClassPublic ( 0 , publicFn ) ;
64
- const contractInstance = makeContractInstanceFromClassId ( contractClass . id ) ;
65
-
66
- // The values here should match those in `avm_simulator.test.ts`
67
- const instanceGet = new SerializableContractInstance ( {
68
- version : 1 ,
69
- salt : new Fr ( 0x123 ) ,
70
- deployer : new AztecAddress ( new Fr ( 0x456 ) ) ,
71
- contractClassId : new Fr ( 0x789 ) ,
72
- initializationHash : new Fr ( 0x101112 ) ,
73
- publicKeys : new PublicKeys (
74
- new Point ( new Fr ( 0x131415 ) , new Fr ( 0x161718 ) , false ) ,
75
- new Point ( new Fr ( 0x192021 ) , new Fr ( 0x222324 ) , false ) ,
76
- new Point ( new Fr ( 0x252627 ) , new Fr ( 0x282930 ) , false ) ,
77
- new Point ( new Fr ( 0x313233 ) , new Fr ( 0x343536 ) , false ) ,
78
- ) ,
79
- } ) . withAddress ( contractInstance . address ) ;
80
- worldStateDB . getContractInstance
81
- . mockResolvedValueOnce ( contractInstance )
82
- . mockResolvedValueOnce ( instanceGet ) // test gets deployer
83
- . mockResolvedValueOnce ( instanceGet ) // test gets class id
84
- . mockResolvedValueOnce ( instanceGet ) // test gets init hash
85
- . mockResolvedValue ( contractInstance ) ;
86
- worldStateDB . getContractClass . mockResolvedValue ( contractClass ) ;
87
- worldStateDB . getBytecode . mockResolvedValue ( bytecode ) ;
88
- worldStateDB . getBytecodeCommitment . mockResolvedValue ( computePublicBytecodeCommitment ( bytecode ) ) ;
89
-
90
- const storageValue = new Fr ( 5 ) ;
91
- worldStateDB . storageRead . mockResolvedValue ( Promise . resolve ( storageValue ) ) ;
56
+ const contractDataSource = new MockedAvmTestContractDataSource ( ) ;
57
+ const worldStateDB = new WorldStateDB ( merkleTrees , contractDataSource ) ;
58
+
59
+ const contractInstance = contractDataSource . contractInstance ;
92
60
93
61
const simulator = new PublicTxSimulator (
94
62
merkleTrees ,
@@ -99,7 +67,12 @@ export async function simulateAvmTestContractGenerateCircuitInputs(
99
67
/*doMerkleOperations=*/ true ,
100
68
) ;
101
69
102
- const callContext = new CallContext ( sender , contractInstance . address , dispatchSelector , /*isStaticCall=*/ false ) ;
70
+ const callContext = new CallContext (
71
+ sender ,
72
+ contractInstance . address ,
73
+ contractDataSource . fnSelector ,
74
+ /*isStaticCall=*/ false ,
75
+ ) ;
103
76
const executionRequest = new PublicExecutionRequest ( callContext , calldata ) ;
104
77
105
78
const tx : Tx = createTxForPublicCall ( executionRequest ) ;
@@ -159,3 +132,81 @@ export function createTxForPublicCall(
159
132
160
133
return tx ;
161
134
}
135
+
136
+ class MockedAvmTestContractDataSource {
137
+ private fnName = 'public_dispatch' ;
138
+ private bytecode : Buffer ;
139
+ public fnSelector : FunctionSelector ;
140
+ private publicFn : PublicFunction ;
141
+ private contractClass : ContractClassPublic ;
142
+ public contractInstance : ContractInstanceWithAddress ;
143
+ private bytecodeCommitment : Fr ;
144
+ private otherContractInstance : ContractInstanceWithAddress ;
145
+
146
+ constructor ( ) {
147
+ this . bytecode = getAvmTestContractBytecode ( this . fnName ) ;
148
+ this . fnSelector = getAvmTestContractFunctionSelector ( this . fnName ) ;
149
+ this . publicFn = { bytecode : this . bytecode , selector : this . fnSelector } ;
150
+ this . contractClass = makeContractClassPublic ( 0 , this . publicFn ) ;
151
+ this . contractInstance = makeContractInstanceFromClassId ( this . contractClass . id ) ;
152
+ this . bytecodeCommitment = computePublicBytecodeCommitment ( this . bytecode ) ;
153
+ // The values here should match those in `avm_simulator.test.ts`
154
+ this . otherContractInstance = new SerializableContractInstance ( {
155
+ version : 1 ,
156
+ salt : new Fr ( 0x123 ) ,
157
+ deployer : new AztecAddress ( new Fr ( 0x456 ) ) ,
158
+ contractClassId : new Fr ( 0x789 ) ,
159
+ initializationHash : new Fr ( 0x101112 ) ,
160
+ publicKeys : new PublicKeys (
161
+ new Point ( new Fr ( 0x131415 ) , new Fr ( 0x161718 ) , false ) ,
162
+ new Point ( new Fr ( 0x192021 ) , new Fr ( 0x222324 ) , false ) ,
163
+ new Point ( new Fr ( 0x252627 ) , new Fr ( 0x282930 ) , false ) ,
164
+ new Point ( new Fr ( 0x313233 ) , new Fr ( 0x343536 ) , false ) ,
165
+ ) ,
166
+ } ) . withAddress ( this . contractInstance . address ) ;
167
+ }
168
+
169
+ getPublicFunction ( _address : AztecAddress , _selector : FunctionSelector ) : Promise < PublicFunction > {
170
+ return Promise . resolve ( this . publicFn ) ;
171
+ }
172
+
173
+ getBlockNumber ( ) : Promise < number > {
174
+ throw new Error ( 'Method not implemented.' ) ;
175
+ }
176
+
177
+ getContractClass ( _id : Fr ) : Promise < ContractClassPublic > {
178
+ return Promise . resolve ( this . contractClass ) ;
179
+ }
180
+
181
+ getBytecodeCommitment ( _id : Fr ) : Promise < Fr > {
182
+ return Promise . resolve ( this . bytecodeCommitment ) ;
183
+ }
184
+
185
+ addContractClass ( _contractClass : ContractClassPublic ) : Promise < void > {
186
+ return Promise . resolve ( ) ;
187
+ }
188
+
189
+ getContract ( address : AztecAddress ) : Promise < ContractInstanceWithAddress > {
190
+ if ( address . equals ( this . contractInstance . address ) ) {
191
+ return Promise . resolve ( this . contractInstance ) ;
192
+ } else {
193
+ return Promise . resolve ( this . otherContractInstance ) ;
194
+ }
195
+ }
196
+
197
+ getContractClassIds ( ) : Promise < Fr [ ] > {
198
+ throw new Error ( 'Method not implemented.' ) ;
199
+ }
200
+
201
+ getContractArtifact ( _address : AztecAddress ) : Promise < ContractArtifact | undefined > {
202
+ throw new Error ( 'Method not implemented.' ) ;
203
+ }
204
+
205
+ getContractFunctionName ( _address : AztecAddress , _selector : FunctionSelector ) : Promise < string > {
206
+ return Promise . resolve ( this . fnName ) ;
207
+ }
208
+
209
+ addContractArtifact ( _address : AztecAddress , _contract : ContractArtifact ) : Promise < void > {
210
+ return Promise . resolve ( ) ;
211
+ }
212
+ }
0 commit comments