@@ -85,7 +85,7 @@ func runTrace(tracer tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCon
8585func TestTracer (t * testing.T ) {
8686 execTracer := func (code string ) ([]byte , string ) {
8787 t .Helper ()
88- tracer , err := newJsTracer (code , nil )
88+ tracer , err := newJsTracer (code , nil , nil )
8989 if err != nil {
9090 t .Fatal (err )
9191 }
@@ -149,7 +149,7 @@ func TestTracer(t *testing.T) {
149149
150150func TestHalt (t * testing.T ) {
151151 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 )
153153 if err != nil {
154154 t .Fatal (err )
155155 }
@@ -163,7 +163,7 @@ func TestHalt(t *testing.T) {
163163}
164164
165165func 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 )
167167 if err != nil {
168168 t .Fatal (err )
169169 }
@@ -187,7 +187,7 @@ func TestHaltBetweenSteps(t *testing.T) {
187187func TestNoStepExec (t * testing.T ) {
188188 execTracer := func (code string ) []byte {
189189 t .Helper ()
190- tracer , err := newJsTracer (code , nil )
190+ tracer , err := newJsTracer (code , nil , nil )
191191 if err != nil {
192192 t .Fatal (err )
193193 }
@@ -221,7 +221,7 @@ func TestIsPrecompile(t *testing.T) {
221221 chaincfg .IstanbulBlock = big .NewInt (200 )
222222 chaincfg .BerlinBlock = big .NewInt (300 )
223223 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 )
225225 if err != nil {
226226 t .Fatal (err )
227227 }
@@ -235,7 +235,7 @@ func TestIsPrecompile(t *testing.T) {
235235 t .Errorf ("tracer should not consider blake2f as precompile in byzantium" )
236236 }
237237
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 )
239239 blockCtx = vm.BlockContext {BlockNumber : big .NewInt (250 )}
240240 res , err = runTrace (tracer , & vmContext {blockCtx , txCtx }, chaincfg )
241241 if err != nil {
@@ -248,14 +248,14 @@ func TestIsPrecompile(t *testing.T) {
248248
249249func TestEnterExit (t * testing.T ) {
250250 // 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 {
252252 t .Fatal ("tracer creation should've failed without exit() definition" )
253253 }
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 {
255255 t .Fatal (err )
256256 }
257257 // 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 )
259259 if err != nil {
260260 t .Fatal (err )
261261 }
@@ -274,3 +274,33 @@ func TestEnterExit(t *testing.T) {
274274 t .Errorf ("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n " , have , want )
275275 }
276276}
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