github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/evm.go (about) 1 // Copyright 2016 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 core 18 19 import ( 20 "math/big" 21 22 "github.com/scroll-tech/go-ethereum/common" 23 "github.com/scroll-tech/go-ethereum/consensus" 24 "github.com/scroll-tech/go-ethereum/core/types" 25 "github.com/scroll-tech/go-ethereum/core/vm" 26 "github.com/scroll-tech/go-ethereum/params" 27 ) 28 29 // ChainContext supports retrieving headers and consensus parameters from the 30 // current blockchain to be used during transaction processing. 31 type ChainContext interface { 32 // Engine retrieves the chain's consensus engine. 33 Engine() consensus.Engine 34 35 // GetHeader returns the hash corresponding to their hash. 36 GetHeader(common.Hash, uint64) *types.Header 37 } 38 39 // NewEVMBlockContext creates a new context for use in the EVM. 40 func NewEVMBlockContext(header *types.Header, chain ChainContext, chainConfig *params.ChainConfig, author *common.Address) vm.BlockContext { 41 var ( 42 beneficiary common.Address 43 baseFee *big.Int 44 ) 45 46 // If we don't have an explicit author (i.e. not mining), extract from the header 47 if chainConfig.Scroll.FeeVaultEnabled() { 48 beneficiary = *chainConfig.Scroll.FeeVaultAddress 49 } else if author == nil { 50 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 51 } else { 52 beneficiary = *author 53 } 54 if header.BaseFee != nil { 55 baseFee = new(big.Int).Set(header.BaseFee) 56 } 57 return vm.BlockContext{ 58 CanTransfer: CanTransfer, 59 Transfer: Transfer, 60 GetHash: GetHashFn(header, chain), 61 Coinbase: beneficiary, 62 BlockNumber: new(big.Int).Set(header.Number), 63 Time: new(big.Int).SetUint64(header.Time), 64 Difficulty: new(big.Int).Set(header.Difficulty), 65 BaseFee: baseFee, 66 GasLimit: header.GasLimit, 67 } 68 } 69 70 // NewEVMTxContext creates a new transaction context for a single transaction. 71 func NewEVMTxContext(msg Message) vm.TxContext { 72 return vm.TxContext{ 73 Origin: msg.From(), 74 To: msg.To(), 75 GasPrice: new(big.Int).Set(msg.GasPrice()), 76 } 77 } 78 79 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 80 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 81 // Cache will initially contain [refHash.parent], 82 // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...] 83 var cache []common.Hash 84 85 return func(n uint64) common.Hash { 86 // If there's no hash cache yet, make one 87 if len(cache) == 0 { 88 cache = append(cache, ref.ParentHash) 89 } 90 if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) { 91 return cache[idx] 92 } 93 // No luck in the cache, but we can start iterating from the last element we already know 94 lastKnownHash := cache[len(cache)-1] 95 lastKnownNumber := ref.Number.Uint64() - uint64(len(cache)) 96 97 for { 98 header := chain.GetHeader(lastKnownHash, lastKnownNumber) 99 if header == nil { 100 break 101 } 102 cache = append(cache, header.ParentHash) 103 lastKnownHash = header.ParentHash 104 lastKnownNumber = header.Number.Uint64() - 1 105 if n == lastKnownNumber { 106 return lastKnownHash 107 } 108 } 109 return common.Hash{} 110 } 111 } 112 113 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 114 // This does not take the necessary gas in to account to make the transfer valid. 115 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 116 return db.GetBalance(addr).Cmp(amount) >= 0 117 } 118 119 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 120 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 121 db.SubBalance(sender, amount) 122 db.AddBalance(recipient, amount) 123 }