github.com/MetalBlockchain/subnet-evm@v0.4.9/core/evm.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2016 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package core 28 29 import ( 30 "math/big" 31 32 "github.com/MetalBlockchain/subnet-evm/consensus" 33 "github.com/MetalBlockchain/subnet-evm/core/types" 34 "github.com/MetalBlockchain/subnet-evm/core/vm" 35 "github.com/ethereum/go-ethereum/common" 36 ) 37 38 // ChainContext supports retrieving headers and consensus parameters from the 39 // current blockchain to be used during transaction processing. 40 type ChainContext interface { 41 // Engine retrieves the chain's consensus engine. 42 Engine() consensus.Engine 43 44 // GetHeader returns the header corresponding to the hash/number argument pair. 45 GetHeader(common.Hash, uint64) *types.Header 46 } 47 48 // NewEVMBlockContext creates a new context for use in the EVM. 49 func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext { 50 var ( 51 beneficiary common.Address 52 baseFee *big.Int 53 ) 54 55 // If we don't have an explicit author (i.e. not mining), extract from the header 56 if author == nil { 57 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 58 } else { 59 beneficiary = *author 60 } 61 if header.BaseFee != nil { 62 baseFee = new(big.Int).Set(header.BaseFee) 63 } 64 return vm.BlockContext{ 65 CanTransfer: CanTransfer, 66 Transfer: Transfer, 67 GetHash: GetHashFn(header, chain), 68 Coinbase: beneficiary, 69 BlockNumber: new(big.Int).Set(header.Number), 70 Time: new(big.Int).SetUint64(header.Time), 71 Difficulty: new(big.Int).Set(header.Difficulty), 72 BaseFee: baseFee, 73 GasLimit: header.GasLimit, 74 } 75 } 76 77 // NewEVMTxContext creates a new transaction context for a single transaction. 78 func NewEVMTxContext(msg Message) vm.TxContext { 79 return vm.TxContext{ 80 Origin: msg.From(), 81 GasPrice: new(big.Int).Set(msg.GasPrice()), 82 } 83 } 84 85 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 86 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 87 // Cache will initially contain [refHash.parent], 88 // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...] 89 var cache []common.Hash 90 91 return func(n uint64) common.Hash { 92 if ref.Number.Uint64() <= n { 93 // This situation can happen if we're doing tracing and using 94 // block overrides. 95 return common.Hash{} 96 } 97 // If there's no hash cache yet, make one 98 if len(cache) == 0 { 99 cache = append(cache, ref.ParentHash) 100 } 101 if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) { 102 return cache[idx] 103 } 104 // No luck in the cache, but we can start iterating from the last element we already know 105 lastKnownHash := cache[len(cache)-1] 106 lastKnownNumber := ref.Number.Uint64() - uint64(len(cache)) 107 108 for { 109 header := chain.GetHeader(lastKnownHash, lastKnownNumber) 110 if header == nil { 111 break 112 } 113 cache = append(cache, header.ParentHash) 114 lastKnownHash = header.ParentHash 115 lastKnownNumber = header.Number.Uint64() - 1 116 if n == lastKnownNumber { 117 return lastKnownHash 118 } 119 } 120 return common.Hash{} 121 } 122 } 123 124 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 125 // This does not take the necessary gas in to account to make the transfer valid. 126 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 127 return db.GetBalance(addr).Cmp(amount) >= 0 128 } 129 130 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 131 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 132 db.SubBalance(sender, amount) 133 db.AddBalance(recipient, amount) 134 }