github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/cmd/evm/internal/t8ntool/execution.go (about) 1 // Copyright 2020 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 t8ntool 18 19 import ( 20 "fmt" 21 "math/big" 22 "os" 23 24 "golang.org/x/crypto/sha3" 25 26 "github.com/scroll-tech/go-ethereum/common" 27 "github.com/scroll-tech/go-ethereum/common/math" 28 "github.com/scroll-tech/go-ethereum/consensus/ethash" 29 "github.com/scroll-tech/go-ethereum/consensus/misc" 30 "github.com/scroll-tech/go-ethereum/core" 31 "github.com/scroll-tech/go-ethereum/core/rawdb" 32 "github.com/scroll-tech/go-ethereum/core/state" 33 "github.com/scroll-tech/go-ethereum/core/types" 34 "github.com/scroll-tech/go-ethereum/core/vm" 35 "github.com/scroll-tech/go-ethereum/crypto" 36 "github.com/scroll-tech/go-ethereum/ethdb" 37 "github.com/scroll-tech/go-ethereum/log" 38 "github.com/scroll-tech/go-ethereum/params" 39 "github.com/scroll-tech/go-ethereum/rlp" 40 "github.com/scroll-tech/go-ethereum/rollup/fees" 41 "github.com/scroll-tech/go-ethereum/trie" 42 ) 43 44 type Prestate struct { 45 Env stEnv `json:"env"` 46 Pre core.GenesisAlloc `json:"pre"` 47 } 48 49 // ExecutionResult contains the execution status after running a state test, any 50 // error that might have occurred and a dump of the final state if requested. 51 type ExecutionResult struct { 52 StateRoot common.Hash `json:"stateRoot"` 53 TxRoot common.Hash `json:"txRoot"` 54 ReceiptRoot common.Hash `json:"receiptsRoot"` 55 LogsHash common.Hash `json:"logsHash"` 56 Bloom types.Bloom `json:"logsBloom" gencodec:"required"` 57 Receipts types.Receipts `json:"receipts"` 58 Rejected []*rejectedTx `json:"rejected,omitempty"` 59 Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"` 60 GasUsed math.HexOrDecimal64 `json:"gasUsed"` 61 } 62 63 type ommer struct { 64 Delta uint64 `json:"delta"` 65 Address common.Address `json:"address"` 66 } 67 68 //go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go 69 type stEnv struct { 70 Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` 71 Difficulty *big.Int `json:"currentDifficulty"` 72 ParentDifficulty *big.Int `json:"parentDifficulty"` 73 GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` 74 Number uint64 `json:"currentNumber" gencodec:"required"` 75 Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` 76 ParentTimestamp uint64 `json:"parentTimestamp,omitempty"` 77 BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"` 78 Ommers []ommer `json:"ommers,omitempty"` 79 BaseFee *big.Int `json:"currentBaseFee,omitempty"` 80 ParentUncleHash common.Hash `json:"parentUncleHash"` 81 } 82 83 type stEnvMarshaling struct { 84 Coinbase common.UnprefixedAddress 85 Difficulty *math.HexOrDecimal256 86 ParentDifficulty *math.HexOrDecimal256 87 GasLimit math.HexOrDecimal64 88 Number math.HexOrDecimal64 89 Timestamp math.HexOrDecimal64 90 ParentTimestamp math.HexOrDecimal64 91 BaseFee *math.HexOrDecimal256 92 } 93 94 type rejectedTx struct { 95 Index int `json:"index"` 96 Err string `json:"error"` 97 } 98 99 // Apply applies a set of transactions to a pre-state 100 func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, 101 txs types.Transactions, miningReward int64, 102 getTracerFn func(txIndex int, txHash common.Hash) (tracer vm.EVMLogger, err error)) (*state.StateDB, *ExecutionResult, error) { 103 104 // Capture errors for BLOCKHASH operation, if we haven't been supplied the 105 // required blockhashes 106 var hashError error 107 getHash := func(num uint64) common.Hash { 108 if pre.Env.BlockHashes == nil { 109 hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num) 110 return common.Hash{} 111 } 112 h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)] 113 if !ok { 114 hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num) 115 } 116 return h 117 } 118 var ( 119 statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre) 120 signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number)) 121 gaspool = new(core.GasPool) 122 blockHash = common.Hash{0x13, 0x37} 123 rejectedTxs []*rejectedTx 124 includedTxs types.Transactions 125 gasUsed = uint64(0) 126 receipts = make(types.Receipts, 0) 127 txIndex = 0 128 ) 129 gaspool.AddGas(pre.Env.GasLimit) 130 vmContext := vm.BlockContext{ 131 CanTransfer: core.CanTransfer, 132 Transfer: core.Transfer, 133 Coinbase: pre.Env.Coinbase, 134 BlockNumber: new(big.Int).SetUint64(pre.Env.Number), 135 Time: new(big.Int).SetUint64(pre.Env.Timestamp), 136 Difficulty: pre.Env.Difficulty, 137 GasLimit: pre.Env.GasLimit, 138 GetHash: getHash, 139 } 140 // If currentBaseFee is defined, add it to the vmContext. 141 if pre.Env.BaseFee != nil { 142 vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee) 143 } 144 // If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's 145 // done in StateProcessor.Process(block, ...), right before transactions are applied. 146 if chainConfig.DAOForkSupport && 147 chainConfig.DAOForkBlock != nil && 148 chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 { 149 misc.ApplyDAOHardFork(statedb) 150 } 151 152 for i, tx := range txs { 153 msg, err := tx.AsMessage(signer, pre.Env.BaseFee) 154 if err != nil { 155 log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err) 156 rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) 157 continue 158 } 159 tracer, err := getTracerFn(txIndex, tx.Hash()) 160 if err != nil { 161 return nil, nil, err 162 } 163 vmConfig.Tracer = tracer 164 vmConfig.Debug = (tracer != nil) 165 statedb.Prepare(tx.Hash(), txIndex) 166 txContext := core.NewEVMTxContext(msg) 167 snapshot := statedb.Snapshot() 168 evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig) 169 170 l1DataFee, err := fees.CalculateL1DataFee(tx, statedb) 171 if err != nil { 172 log.Info("rejected tx due to fees.CalculateL1DataFee", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err) 173 rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) 174 continue 175 } 176 177 // (ret []byte, usedGas uint64, failed bool, err error) 178 msgResult, err := core.ApplyMessage(evm, msg, gaspool, l1DataFee) 179 if err != nil { 180 statedb.RevertToSnapshot(snapshot) 181 log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err) 182 rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) 183 continue 184 } 185 includedTxs = append(includedTxs, tx) 186 if hashError != nil { 187 return nil, nil, NewError(ErrorMissingBlockhash, hashError) 188 } 189 gasUsed += msgResult.UsedGas 190 191 // Receipt: 192 { 193 var root []byte 194 if chainConfig.IsByzantium(vmContext.BlockNumber) { 195 statedb.Finalise(true) 196 } else { 197 root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes() 198 } 199 200 // Create a new receipt for the transaction, storing the intermediate root and 201 // gas used by the tx. 202 receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed} 203 if msgResult.Failed() { 204 receipt.Status = types.ReceiptStatusFailed 205 } else { 206 receipt.Status = types.ReceiptStatusSuccessful 207 } 208 receipt.TxHash = tx.Hash() 209 receipt.GasUsed = msgResult.UsedGas 210 211 // If the transaction created a contract, store the creation address in the receipt. 212 if msg.To() == nil { 213 receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce()) 214 } 215 216 // Set the receipt logs and create the bloom filter. 217 receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash) 218 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 219 // These three are non-consensus fields: 220 //receipt.BlockHash 221 //receipt.BlockNumber 222 receipt.TransactionIndex = uint(txIndex) 223 receipts = append(receipts, receipt) 224 } 225 226 txIndex++ 227 } 228 statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)) 229 // Add mining reward? 230 if miningReward > 0 { 231 // Add mining reward. The mining reward may be `0`, which only makes a difference in the cases 232 // where 233 // - the coinbase suicided, or 234 // - there are only 'bad' transactions, which aren't executed. In those cases, 235 // the coinbase gets no txfee, so isn't created, and thus needs to be touched 236 var ( 237 blockReward = big.NewInt(miningReward) 238 minerReward = new(big.Int).Set(blockReward) 239 perOmmer = new(big.Int).Div(blockReward, big.NewInt(32)) 240 ) 241 for _, ommer := range pre.Env.Ommers { 242 // Add 1/32th for each ommer included 243 minerReward.Add(minerReward, perOmmer) 244 // Add (8-delta)/8 245 reward := big.NewInt(8) 246 reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta)) 247 reward.Mul(reward, blockReward) 248 reward.Div(reward, big.NewInt(8)) 249 statedb.AddBalance(ommer.Address, reward) 250 } 251 statedb.AddBalance(pre.Env.Coinbase, minerReward) 252 } 253 // Commit block 254 root, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber)) 255 if err != nil { 256 fmt.Fprintf(os.Stderr, "Could not commit state: %v", err) 257 return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err)) 258 } 259 execRs := &ExecutionResult{ 260 StateRoot: root, 261 TxRoot: types.DeriveSha(includedTxs, trie.NewStackTrie(nil)), 262 ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)), 263 Bloom: types.CreateBloom(receipts), 264 LogsHash: rlpHash(statedb.Logs()), 265 Receipts: receipts, 266 Rejected: rejectedTxs, 267 Difficulty: (*math.HexOrDecimal256)(vmContext.Difficulty), 268 GasUsed: (math.HexOrDecimal64)(gasUsed), 269 } 270 return statedb, execRs, nil 271 } 272 273 func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB { 274 sdb := state.NewDatabaseWithConfig(db, &trie.Config{Preimages: true}) 275 statedb, _ := state.New(common.Hash{}, sdb, nil) 276 for addr, a := range accounts { 277 statedb.SetCode(addr, a.Code) 278 statedb.SetNonce(addr, a.Nonce) 279 statedb.SetBalance(addr, a.Balance) 280 for k, v := range a.Storage { 281 statedb.SetState(addr, k, v) 282 } 283 } 284 // Commit and re-open to start with a clean state. 285 root, _ := statedb.Commit(false) 286 statedb, _ = state.New(root, sdb, nil) 287 return statedb 288 } 289 290 func rlpHash(x interface{}) (h common.Hash) { 291 hw := sha3.NewLegacyKeccak256() 292 rlp.Encode(hw, x) 293 hw.Sum(h[:0]) 294 return h 295 } 296 297 // calcDifficulty is based on ethash.CalcDifficulty. This method is used in case 298 // the caller does not provide an explicit difficulty, but instead provides only 299 // parent timestamp + difficulty. 300 // Note: this method only works for ethash engine. 301 func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64, 302 parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int { 303 uncleHash := parentUncleHash 304 if uncleHash == (common.Hash{}) { 305 uncleHash = types.EmptyUncleHash 306 } 307 parent := &types.Header{ 308 ParentHash: common.Hash{}, 309 UncleHash: uncleHash, 310 Difficulty: parentDifficulty, 311 Number: new(big.Int).SetUint64(number - 1), 312 Time: parentTime, 313 } 314 return ethash.CalcDifficulty(config, currentTime, parent) 315 }