forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package live | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/tracing" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/eth/tracers" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
func init() { | ||
tracers.LiveDirectory.Register("logger", newLoggerTracer) | ||
} | ||
|
||
type logger struct { | ||
tx *types.Transaction | ||
from common.Address | ||
start time.Time | ||
reads uint64 | ||
writes uint64 | ||
calls uint64 | ||
logs uint64 | ||
creates uint64 | ||
faults uint64 | ||
} | ||
|
||
func newLoggerTracer(_ json.RawMessage) (*tracing.Hooks, error) { | ||
t := &logger{} | ||
return &tracing.Hooks{ | ||
OnTxStart: t.OnTxStart, | ||
OnTxEnd: t.OnTxEnd, | ||
OnOpcode: t.OnOpcode, | ||
OnFault: t.OnFault, | ||
}, nil | ||
} | ||
|
||
func (t *logger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { | ||
switch vm.OpCode(op) { | ||
case vm.SLOAD, vm.BALANCE, vm.EXTCODEHASH, vm.EXTCODESIZE, vm.EXTCODECOPY: | ||
t.reads++ | ||
case vm.SSTORE: | ||
t.writes++ | ||
case vm.LOG0, vm.LOG1, vm.LOG2, vm.LOG3, vm.LOG4: | ||
t.logs++ | ||
case vm.CALL, vm.CALLCODE, vm.DELEGATECALL, vm.STATICCALL: | ||
t.calls++ | ||
case vm.CREATE, vm.CREATE2: | ||
t.creates++ | ||
} | ||
} | ||
|
||
func (t *logger) OnFault(pc uint64, op byte, gas, cost uint64, _ tracing.OpContext, depth int, err error) { | ||
t.faults++ | ||
} | ||
|
||
func (t *logger) OnTxStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) { | ||
t.tx = tx | ||
t.from = from | ||
t.start = time.Now() | ||
t.reads, t.writes, t.calls, t.logs, t.creates, t.faults = 0, 0, 0, 0, 0, 0 | ||
} | ||
|
||
func (t *logger) OnTxEnd(receipt *types.Receipt, err error) { | ||
duration := time.Since(t.start) | ||
to := "" | ||
if t.tx.To() != nil { | ||
to = t.tx.To().Hex() | ||
} | ||
log.Info( | ||
"OnTxEnd", | ||
"hash", t.tx.Hash().Hex(), | ||
"from", t.from.Hex(), | ||
"to", to, | ||
"value", t.tx.Value().String(), | ||
"size", len(t.tx.Data()), | ||
"nonce", t.tx.Nonce(), | ||
"gas", receipt.GasUsed, | ||
"price", t.tx.GasPrice().String(), | ||
"duration", duration.Nanoseconds(), | ||
"reads", t.reads, | ||
"writes", t.writes, | ||
"calls", t.calls, | ||
"logs", t.logs, | ||
"creates", t.creates, | ||
"faults", t.faults, | ||
"error", err, | ||
) | ||
} |