eth/tracers: add onlyTopCall option to callTracer #25430 (#1291)

This commit is contained in:
Daniel Liu 2025-09-08 23:38:19 +08:00 committed by GitHub
parent bfc00e775b
commit 67efb14ff3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View file

@ -124,7 +124,7 @@ type jsTracer struct {
// The methods `result` and `fault` are required to be present.
// The methods `step`, `enter`, and `exit` are optional, but note that
// `enter` and `exit` always go together.
func newJsTracer(code string, ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
func newJsTracer(code string, ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
if c, ok := assetTracers[code]; ok {
code = c
}
@ -176,6 +176,17 @@ func newJsTracer(code string, ctx *tracers.Context, _ json.RawMessage) (tracers.
t.exit = exit
t.result = result
t.fault = fault
// Pass in config
if setup, ok := goja.AssertFunction(obj.Get("setup")); ok {
cfgStr := "{}"
if cfg != nil {
cfgStr = string(cfg)
}
if _, err := setup(obj, vm.ToValue(cfgStr)); err != nil {
return nil, err
}
}
// Setup objects carrying data to JS. These are created once and re-used.
t.log = &steplog{
vm: vm,

View file

@ -292,3 +292,33 @@ func TestEnterExit(t *testing.T) {
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
}
}
func TestSetup(t *testing.T) {
// Test empty config
_, err := newJsTracer(`{setup: function(cfg) { if (cfg !== "{}") { throw("invalid empty config") } }, fault: function() {}, result: function() {}}`, new(tracers.Context), nil)
if err != nil {
t.Error(err)
}
cfg, err := json.Marshal(map[string]string{"foo": "bar"})
if err != nil {
t.Fatal(err)
}
// Test no setup func
_, err = newJsTracer(`{fault: function() {}, result: function() {}}`, new(tracers.Context), cfg)
if err != nil {
t.Fatal(err)
}
// Test config value
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)
if err != nil {
t.Fatal(err)
}
have, err := tracer.GetResult()
if err != nil {
t.Fatal(err)
}
if string(have) != `"bar"` {
t.Errorf("tracer returned wrong result. have: %s, want: \"bar\"\n", string(have))
}
}