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