eth/tracers/js: add coinbase addr to ctx #30231 (#1484)

Add coinbase address to javascript tracer context.

This PR adds the `coinbase` address to `jsTracer.ctx`, allowing access
to the coinbase address (fee receipient) in custom JavaScript tracers.

Example usage:

```javascript
result: function(ctx) {
  return toAddress(ctx.coinbase);
}
```

This change enables custom tracers to access coinbase address,
previously unavailable, enhancing their capabilities to match built-in
tracers.

Co-authored-by: Dylan Vassallo <dylan.vassallo@hotmail.com>
This commit is contained in:
Daniel Liu 2025-09-13 10:49:17 +08:00 committed by GitHub
parent ea239d5b15
commit 1597be6879
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -253,6 +253,12 @@ func (t *jsTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from
return
}
t.ctx["gasPrice"] = gasPriceBig
coinbase, err := t.toBuf(t.vm, env.Coinbase.Bytes())
if err != nil {
t.err = err
return
}
t.ctx["coinbase"] = t.vm.ToValue(coinbase)
}
// OnTxEnd implements the Tracer interface and is invoked at the end of

View file

@ -153,6 +153,9 @@ func TestTracer(t *testing.T) {
want: "",
fail: "reached limit for padding memory slice: 1049568 at step (<eval>:1:83(20)) in server-side tracer function 'step'",
contract: []byte{byte(vm.PUSH1), byte(0xff), byte(vm.PUSH1), byte(0x00), byte(vm.MSTORE8), byte(vm.STOP)},
}, { // tests ctx.coinbase
code: "{lengths: [], step: function(log) { }, fault: function() {}, result: function(ctx) { var coinbase = ctx.coinbase; return toAddress(coinbase); }}",
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
},
} {
if have, err := execTracer(tt.code, tt.contract); tt.want != string(have) || tt.fail != err {