github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/eth/tracers/native/call.go (about) 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 register("callTracer", 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 env *vm.EVM 52 callstack []callFrame 53 interrupt uint32 // Atomic flag to signal execution interruption 54 reason error // Textual reason for the interruption 55 } 56 57 // newCallTracer returns a native go tracer which tracks 58 // call frames of a tx, and implements vm.EVMLogger. 59 func newCallTracer(ctx *tracers.Context) tracers.Tracer { 60 // First callframe contains tx context info 61 // and is populated on start and end. 62 return &callTracer{callstack: make([]callFrame, 1)} 63 } 64 65 // CaptureStart implements the EVMLogger interface to initialize the tracing operation. 66 func (t *callTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { 67 t.env = env 68 t.callstack[0] = callFrame{ 69 Type: "CALL", 70 From: addrToHex(from), 71 To: addrToHex(to), 72 Input: bytesToHex(input), 73 Gas: uintToHex(gas), 74 Value: bigToHex(value), 75 } 76 if create { 77 t.callstack[0].Type = "CREATE" 78 } 79 } 80 81 // CaptureEnd is called after the call finishes to finalize the tracing. 82 func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) { 83 t.callstack[0].GasUsed = uintToHex(gasUsed) 84 if err != nil { 85 t.callstack[0].Error = err.Error() 86 if err.Error() == "execution reverted" && len(output) > 0 { 87 t.callstack[0].Output = bytesToHex(output) 88 } 89 } else { 90 t.callstack[0].Output = bytesToHex(output) 91 } 92 } 93 94 // CaptureState implements the EVMLogger interface to trace a single step of VM execution. 95 func (t *callTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { 96 } 97 98 // CaptureFault implements the EVMLogger interface to trace an execution fault. 99 func (t *callTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) { 100 } 101 102 // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct). 103 func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { 104 // Skip if tracing was interrupted 105 if atomic.LoadUint32(&t.interrupt) > 0 { 106 t.env.Cancel() 107 return 108 } 109 110 call := callFrame{ 111 Type: typ.String(), 112 From: addrToHex(from), 113 To: addrToHex(to), 114 Input: bytesToHex(input), 115 Gas: uintToHex(gas), 116 Value: bigToHex(value), 117 } 118 t.callstack = append(t.callstack, call) 119 } 120 121 // CaptureExit is called when EVM exits a scope, even if the scope didn't 122 // execute any code. 123 func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) { 124 size := len(t.callstack) 125 if size <= 1 { 126 return 127 } 128 // pop call 129 call := t.callstack[size-1] 130 t.callstack = t.callstack[:size-1] 131 size -= 1 132 133 call.GasUsed = uintToHex(gasUsed) 134 if err == nil { 135 call.Output = bytesToHex(output) 136 } else { 137 call.Error = err.Error() 138 if call.Type == "CREATE" || call.Type == "CREATE2" { 139 call.To = "" 140 } 141 } 142 t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call) 143 } 144 145 func (*callTracer) CaptureTxStart(gasLimit uint64) {} 146 147 func (*callTracer) CaptureTxEnd(restGas uint64) {} 148 149 // GetResult returns the json-encoded nested list of call traces, and any 150 // error arising from the encoding or forceful termination (via `Stop`). 151 func (t *callTracer) GetResult() (json.RawMessage, error) { 152 if len(t.callstack) != 1 { 153 return nil, errors.New("incorrect number of top-level calls") 154 } 155 res, err := json.Marshal(t.callstack[0]) 156 if err != nil { 157 return nil, err 158 } 159 return json.RawMessage(res), t.reason 160 } 161 162 // Stop terminates execution of the tracer at the first opportune moment. 163 func (t *callTracer) Stop(err error) { 164 t.reason = err 165 atomic.StoreUint32(&t.interrupt, 1) 166 } 167 168 func bytesToHex(s []byte) string { 169 return "0x" + common.Bytes2Hex(s) 170 } 171 172 func bigToHex(n *big.Int) string { 173 if n == nil { 174 return "" 175 } 176 return "0x" + n.Text(16) 177 } 178 179 func uintToHex(n uint64) string { 180 return "0x" + strconv.FormatUint(n, 16) 181 } 182 183 func addrToHex(a common.Address) string { 184 return strings.ToLower(a.Hex()) 185 }