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