|
| 1 | +// Copyright 2021 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package native |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "math/big" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + "sync/atomic" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/ethereum/go-ethereum/common" |
| 29 | + "github.com/ethereum/go-ethereum/core/vm" |
| 30 | + "github.com/ethereum/go-ethereum/eth/tracers" |
| 31 | +) |
| 32 | + |
| 33 | +func init() { |
| 34 | + tracers.RegisterNativeTracer("callTracerNative", NewCallTracer) |
| 35 | +} |
| 36 | + |
| 37 | +type callFrame struct { |
| 38 | + Type string `json:"type"` |
| 39 | + From string `json:"from"` |
| 40 | + To string `json:"to,omitempty"` |
| 41 | + Value string `json:"value,omitempty"` |
| 42 | + Gas string `json:"gas"` |
| 43 | + GasUsed string `json:"gasUsed"` |
| 44 | + Input string `json:"input"` |
| 45 | + Output string `json:"output,omitempty"` |
| 46 | + Error string `json:"error,omitempty"` |
| 47 | + Calls []callFrame `json:"calls,omitempty"` |
| 48 | +} |
| 49 | + |
| 50 | +type callTracer struct { |
| 51 | + callstack []callFrame |
| 52 | + interrupt uint32 // Atomic flag to signal execution interruption |
| 53 | + reason error // Textual reason for the interruption |
| 54 | +} |
| 55 | + |
| 56 | +// NewCallTracer returns a native go tracer which tracks |
| 57 | +// call frames of a tx, and implements vm.EVMLogger. |
| 58 | +func NewCallTracer() tracers.Tracer { |
| 59 | + // First callframe contains tx context info |
| 60 | + // and is populated on start and end. |
| 61 | + t := &callTracer{callstack: make([]callFrame, 1)} |
| 62 | + return t |
| 63 | +} |
| 64 | + |
| 65 | +func (t *callTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { |
| 66 | + t.callstack[0] = callFrame{ |
| 67 | + Type: "CALL", |
| 68 | + From: addrToHex(from), |
| 69 | + To: addrToHex(to), |
| 70 | + Input: bytesToHex(input), |
| 71 | + Gas: uintToHex(gas), |
| 72 | + Value: bigToHex(value), |
| 73 | + } |
| 74 | + if create { |
| 75 | + t.callstack[0].Type = "CREATE" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) { |
| 80 | + t.callstack[0].GasUsed = uintToHex(gasUsed) |
| 81 | + if err != nil { |
| 82 | + t.callstack[0].Error = err.Error() |
| 83 | + if err.Error() == "execution reverted" && len(output) > 0 { |
| 84 | + t.callstack[0].Output = bytesToHex(output) |
| 85 | + } |
| 86 | + } else { |
| 87 | + t.callstack[0].Output = bytesToHex(output) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (t *callTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { |
| 92 | +} |
| 93 | + |
| 94 | +func (t *callTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) { |
| 95 | +} |
| 96 | + |
| 97 | +func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { |
| 98 | + // Skip if tracing was interrupted |
| 99 | + if atomic.LoadUint32(&t.interrupt) > 0 { |
| 100 | + // TODO: env.Cancel() |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + call := callFrame{ |
| 105 | + Type: typ.String(), |
| 106 | + From: addrToHex(from), |
| 107 | + To: addrToHex(to), |
| 108 | + Input: bytesToHex(input), |
| 109 | + Gas: uintToHex(gas), |
| 110 | + Value: bigToHex(value), |
| 111 | + } |
| 112 | + t.callstack = append(t.callstack, call) |
| 113 | +} |
| 114 | + |
| 115 | +func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) { |
| 116 | + size := len(t.callstack) |
| 117 | + if size <= 1 { |
| 118 | + return |
| 119 | + } |
| 120 | + // pop call |
| 121 | + call := t.callstack[size-1] |
| 122 | + t.callstack = t.callstack[:size-1] |
| 123 | + size -= 1 |
| 124 | + |
| 125 | + call.GasUsed = uintToHex(gasUsed) |
| 126 | + if err == nil { |
| 127 | + call.Output = bytesToHex(output) |
| 128 | + } else { |
| 129 | + call.Error = err.Error() |
| 130 | + if call.Type == "CREATE" || call.Type == "CREATE2" { |
| 131 | + call.To = "" |
| 132 | + } |
| 133 | + } |
| 134 | + t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call) |
| 135 | +} |
| 136 | + |
| 137 | +func (t *callTracer) GetResult() (json.RawMessage, error) { |
| 138 | + if len(t.callstack) != 1 { |
| 139 | + return nil, errors.New("incorrect number of top-level calls") |
| 140 | + } |
| 141 | + res, err := json.Marshal(t.callstack[0]) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return json.RawMessage(res), t.reason |
| 146 | +} |
| 147 | + |
| 148 | +func (t *callTracer) Stop(err error) { |
| 149 | + t.reason = err |
| 150 | + atomic.StoreUint32(&t.interrupt, 1) |
| 151 | +} |
| 152 | + |
| 153 | +func bytesToHex(s []byte) string { |
| 154 | + return "0x" + common.Bytes2Hex(s) |
| 155 | +} |
| 156 | + |
| 157 | +func bigToHex(n *big.Int) string { |
| 158 | + if n == nil { |
| 159 | + return "" |
| 160 | + } |
| 161 | + return "0x" + n.Text(16) |
| 162 | +} |
| 163 | + |
| 164 | +func uintToHex(n uint64) string { |
| 165 | + return "0x" + strconv.FormatUint(n, 16) |
| 166 | +} |
| 167 | + |
| 168 | +func addrToHex(a common.Address) string { |
| 169 | + return strings.ToLower(a.Hex()) |
| 170 | +} |
0 commit comments