github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/eth/tracers/native/prestate.go (about) 1 // Copyright 2022 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 "math/big" 22 "sync/atomic" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/common/hexutil" 27 "github.com/ethereum/go-ethereum/core/vm" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/eth/tracers" 30 ) 31 32 func init() { 33 register("prestateTracer", newPrestateTracer) 34 } 35 36 type prestate = map[common.Address]*account 37 type account struct { 38 Balance string `json:"balance"` 39 Nonce uint64 `json:"nonce"` 40 Code string `json:"code"` 41 Storage map[common.Hash]common.Hash `json:"storage"` 42 } 43 44 type prestateTracer struct { 45 env *vm.EVM 46 prestate prestate 47 create bool 48 to common.Address 49 gasLimit uint64 // Amount of gas bought for the whole tx 50 interrupt uint32 // Atomic flag to signal execution interruption 51 reason error // Textual reason for the interruption 52 } 53 54 func newPrestateTracer(ctx *tracers.Context) tracers.Tracer { 55 // First callframe contains tx context info 56 // and is populated on start and end. 57 return &prestateTracer{prestate: prestate{}} 58 } 59 60 // CaptureStart implements the EVMLogger interface to initialize the tracing operation. 61 func (t *prestateTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { 62 t.env = env 63 t.create = create 64 t.to = to 65 66 t.lookupAccount(from) 67 t.lookupAccount(to) 68 69 // The recipient balance includes the value transferred. 70 toBal := hexutil.MustDecodeBig(t.prestate[to].Balance) 71 toBal = new(big.Int).Sub(toBal, value) 72 t.prestate[to].Balance = hexutil.EncodeBig(toBal) 73 74 // The sender balance is after reducing: value and gasLimit. 75 // We need to re-add them to get the pre-tx balance. 76 fromBal := hexutil.MustDecodeBig(t.prestate[from].Balance) 77 gasPrice := env.TxContext.GasPrice 78 consumedGas := new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(t.gasLimit)) 79 fromBal.Add(fromBal, new(big.Int).Add(value, consumedGas)) 80 t.prestate[from].Balance = hexutil.EncodeBig(fromBal) 81 t.prestate[from].Nonce-- 82 } 83 84 // CaptureEnd is called after the call finishes to finalize the tracing. 85 func (t *prestateTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) { 86 if t.create { 87 // Exclude created contract. 88 delete(t.prestate, t.to) 89 } 90 } 91 92 // CaptureState implements the EVMLogger interface to trace a single step of VM execution. 93 func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { 94 stack := scope.Stack 95 stackData := stack.Data() 96 stackLen := len(stackData) 97 switch { 98 case stackLen >= 1 && (op == vm.SLOAD || op == vm.SSTORE): 99 slot := common.Hash(stackData[stackLen-1].Bytes32()) 100 t.lookupStorage(scope.Contract.Address(), slot) 101 case stackLen >= 1 && (op == vm.EXTCODECOPY || op == vm.EXTCODEHASH || op == vm.EXTCODESIZE || op == vm.BALANCE || op == vm.SELFDESTRUCT): 102 addr := common.Address(stackData[stackLen-1].Bytes20()) 103 t.lookupAccount(addr) 104 case stackLen >= 5 && (op == vm.DELEGATECALL || op == vm.CALL || op == vm.STATICCALL || op == vm.CALLCODE): 105 addr := common.Address(stackData[stackLen-2].Bytes20()) 106 t.lookupAccount(addr) 107 case op == vm.CREATE: 108 addr := scope.Contract.Address() 109 nonce := t.env.StateDB.GetNonce(addr) 110 t.lookupAccount(crypto.CreateAddress(addr, nonce)) 111 case stackLen >= 4 && op == vm.CREATE2: 112 offset := stackData[stackLen-2] 113 size := stackData[stackLen-3] 114 init := scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 115 inithash := crypto.Keccak256(init) 116 salt := stackData[stackLen-4] 117 t.lookupAccount(crypto.CreateAddress2(scope.Contract.Address(), salt.Bytes32(), inithash)) 118 } 119 } 120 121 // CaptureFault implements the EVMLogger interface to trace an execution fault. 122 func (t *prestateTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) { 123 } 124 125 // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct). 126 func (t *prestateTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { 127 } 128 129 // CaptureExit is called when EVM exits a scope, even if the scope didn't 130 // execute any code. 131 func (t *prestateTracer) CaptureExit(output []byte, gasUsed uint64, err error) { 132 } 133 134 func (t *prestateTracer) CaptureTxStart(gasLimit uint64) { 135 t.gasLimit = gasLimit 136 } 137 138 func (t *prestateTracer) CaptureTxEnd(restGas uint64) {} 139 140 // GetResult returns the json-encoded nested list of call traces, and any 141 // error arising from the encoding or forceful termination (via `Stop`). 142 func (t *prestateTracer) GetResult() (json.RawMessage, error) { 143 res, err := json.Marshal(t.prestate) 144 if err != nil { 145 return nil, err 146 } 147 return json.RawMessage(res), t.reason 148 } 149 150 // Stop terminates execution of the tracer at the first opportune moment. 151 func (t *prestateTracer) Stop(err error) { 152 t.reason = err 153 atomic.StoreUint32(&t.interrupt, 1) 154 } 155 156 // lookupAccount fetches details of an account and adds it to the prestate 157 // if it doesn't exist there. 158 func (t *prestateTracer) lookupAccount(addr common.Address) { 159 if _, ok := t.prestate[addr]; ok { 160 return 161 } 162 t.prestate[addr] = &account{ 163 Balance: bigToHex(t.env.StateDB.GetBalance(addr)), 164 Nonce: t.env.StateDB.GetNonce(addr), 165 Code: bytesToHex(t.env.StateDB.GetCode(addr)), 166 Storage: make(map[common.Hash]common.Hash), 167 } 168 } 169 170 // lookupStorage fetches the requested storage slot and adds 171 // it to the prestate of the given contract. It assumes `lookupAccount` 172 // has been performed on the contract before. 173 func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) { 174 if _, ok := t.prestate[addr].Storage[key]; ok { 175 return 176 } 177 t.prestate[addr].Storage[key] = t.env.StateDB.GetState(addr, key) 178 }