@@ -85,7 +85,7 @@ func runTrace(tracer tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCon
85
85
func TestTracer (t * testing.T ) {
86
86
execTracer := func (code string ) ([]byte , string ) {
87
87
t .Helper ()
88
- tracer , err := newJsTracer (code , nil )
88
+ tracer , err := newJsTracer (code , nil , nil )
89
89
if err != nil {
90
90
t .Fatal (err )
91
91
}
@@ -149,7 +149,7 @@ func TestTracer(t *testing.T) {
149
149
150
150
func TestHalt (t * testing.T ) {
151
151
timeout := errors .New ("stahp" )
152
- tracer , err := newJsTracer ("{step: function() { while(1); }, result: function() { return null; }, fault: function(){}}" , nil )
152
+ tracer , err := newJsTracer ("{step: function() { while(1); }, result: function() { return null; }, fault: function(){}}" , nil , nil )
153
153
if err != nil {
154
154
t .Fatal (err )
155
155
}
@@ -163,7 +163,7 @@ func TestHalt(t *testing.T) {
163
163
}
164
164
165
165
func TestHaltBetweenSteps (t * testing.T ) {
166
- tracer , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }}" , nil )
166
+ tracer , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }}" , nil , nil )
167
167
if err != nil {
168
168
t .Fatal (err )
169
169
}
@@ -187,7 +187,7 @@ func TestHaltBetweenSteps(t *testing.T) {
187
187
func TestNoStepExec (t * testing.T ) {
188
188
execTracer := func (code string ) []byte {
189
189
t .Helper ()
190
- tracer , err := newJsTracer (code , nil )
190
+ tracer , err := newJsTracer (code , nil , nil )
191
191
if err != nil {
192
192
t .Fatal (err )
193
193
}
@@ -221,7 +221,7 @@ func TestIsPrecompile(t *testing.T) {
221
221
chaincfg .IstanbulBlock = big .NewInt (200 )
222
222
chaincfg .BerlinBlock = big .NewInt (300 )
223
223
txCtx := vm.TxContext {GasPrice : big .NewInt (100000 )}
224
- tracer , err := newJsTracer ("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}" , nil )
224
+ tracer , err := newJsTracer ("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}" , nil , nil )
225
225
if err != nil {
226
226
t .Fatal (err )
227
227
}
@@ -235,7 +235,7 @@ func TestIsPrecompile(t *testing.T) {
235
235
t .Errorf ("tracer should not consider blake2f as precompile in byzantium" )
236
236
}
237
237
238
- tracer , _ = newJsTracer ("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}" , nil )
238
+ tracer , _ = newJsTracer ("{addr: toAddress('0000000000000000000000000000000000000009'), res: null, step: function() { this.res = isPrecompiled(this.addr); }, fault: function() {}, result: function() { return this.res; }}" , nil , nil )
239
239
blockCtx = vm.BlockContext {BlockNumber : big .NewInt (250 )}
240
240
res , err = runTrace (tracer , & vmContext {blockCtx , txCtx }, chaincfg )
241
241
if err != nil {
@@ -248,14 +248,14 @@ func TestIsPrecompile(t *testing.T) {
248
248
249
249
func TestEnterExit (t * testing.T ) {
250
250
// test that either both or none of enter() and exit() are defined
251
- if _ , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}" , new (tracers.Context )); err == nil {
251
+ if _ , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}" , new (tracers.Context ), nil ); err == nil {
252
252
t .Fatal ("tracer creation should've failed without exit() definition" )
253
253
}
254
- if _ , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}" , new (tracers.Context )); err != nil {
254
+ if _ , err := newJsTracer ("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}" , new (tracers.Context ), nil ); err != nil {
255
255
t .Fatal (err )
256
256
}
257
257
// test that the enter and exit method are correctly invoked and the values passed
258
- tracer , err := newJsTracer ("{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, step: function() {}, fault: function() {}, result: function() { return {enters: this.enters, exits: this.exits, enterGas: this.enterGas, gasUsed: this.gasUsed} }, enter: function(frame) { this.enters++; this.enterGas = frame.getGas(); }, exit: function(res) { this.exits++; this.gasUsed = res.getGasUsed(); }}" , new (tracers.Context ))
258
+ tracer , err := newJsTracer ("{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, step: function() {}, fault: function() {}, result: function() { return {enters: this.enters, exits: this.exits, enterGas: this.enterGas, gasUsed: this.gasUsed} }, enter: function(frame) { this.enters++; this.enterGas = frame.getGas(); }, exit: function(res) { this.exits++; this.gasUsed = res.getGasUsed(); }}" , new (tracers.Context ), nil )
259
259
if err != nil {
260
260
t .Fatal (err )
261
261
}
@@ -274,3 +274,33 @@ func TestEnterExit(t *testing.T) {
274
274
t .Errorf ("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n " , have , want )
275
275
}
276
276
}
277
+
278
+ func TestSetup (t * testing.T ) {
279
+ // Test empty config
280
+ _ , err := newJsTracer (`{setup: function(cfg) { if (cfg !== "{}") { throw("invalid empty config") } }, fault: function() {}, result: function() {}}` , new (tracers.Context ), nil )
281
+ if err != nil {
282
+ t .Error (err )
283
+ }
284
+
285
+ cfg , err := json .Marshal (map [string ]string {"foo" : "bar" })
286
+ if err != nil {
287
+ t .Fatal (err )
288
+ }
289
+ // Test no setup func
290
+ _ , err = newJsTracer (`{fault: function() {}, result: function() {}}` , new (tracers.Context ), cfg )
291
+ if err != nil {
292
+ t .Fatal (err )
293
+ }
294
+ // Test config value
295
+ tracer , err := newJsTracer ("{config: null, setup: function(cfg) { this.config = JSON.parse(cfg) }, step: function() {}, fault: function() {}, result: function() { return this.config.foo }}" , new (tracers.Context ), cfg )
296
+ if err != nil {
297
+ t .Fatal (err )
298
+ }
299
+ have , err := tracer .GetResult ()
300
+ if err != nil {
301
+ t .Fatal (err )
302
+ }
303
+ if string (have ) != `"bar"` {
304
+ t .Errorf ("tracer returned wrong result. have: %s, want: \" bar\" \n " , string (have ))
305
+ }
306
+ }
0 commit comments