gitlab.com/flarenetwork/coreth@v0.1.1/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/ethereum/go-ethereum/common" 33 "gitlab.com/flarenetwork/coreth/consensus" 34 "gitlab.com/flarenetwork/coreth/core/types" 35 "gitlab.com/flarenetwork/coreth/core/vm" 36 //"github.com/ethereum/go-ethereum/log" 37 ) 38 39 // ChainContext supports retrieving headers and consensus parameters from the 40 // current blockchain to be used during transaction processing. 41 type ChainContext interface { 42 // Engine retrieves the chain's consensus engine. 43 Engine() consensus.Engine 44 45 // GetHeader returns the hash corresponding to their hash. 46 GetHeader(common.Hash, uint64) *types.Header 47 } 48 49 // NewEVMBlockContext creates a new context for use in the EVM. 50 func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext { 51 var ( 52 beneficiary common.Address 53 baseFee *big.Int 54 ) 55 56 // If we don't have an explicit author (i.e. not mining), extract from the header 57 if author == nil { 58 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 59 } else { 60 beneficiary = *author 61 } 62 if header.BaseFee != nil { 63 baseFee = new(big.Int).Set(header.BaseFee) 64 } 65 return vm.BlockContext{ 66 CanTransfer: CanTransfer, 67 CanTransferMC: CanTransferMC, 68 Transfer: Transfer, 69 TransferMultiCoin: TransferMultiCoin, 70 GetHash: GetHashFn(header, chain), 71 Coinbase: beneficiary, 72 BlockNumber: new(big.Int).Set(header.Number), 73 Time: new(big.Int).SetUint64(header.Time), 74 Difficulty: new(big.Int).Set(header.Difficulty), 75 BaseFee: baseFee, 76 GasLimit: header.GasLimit, 77 } 78 } 79 80 // NewEVMTxContext creates a new transaction context for a single transaction. 81 func NewEVMTxContext(msg Message) vm.TxContext { 82 return vm.TxContext{ 83 Origin: msg.From(), 84 GasPrice: new(big.Int).Set(msg.GasPrice()), 85 } 86 } 87 88 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 89 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 90 // Cache will initially contain [refHash.parent], 91 // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...] 92 var cache []common.Hash 93 94 return func(n uint64) common.Hash { 95 // If there's no hash cache yet, make one 96 if len(cache) == 0 { 97 cache = append(cache, ref.ParentHash) 98 } 99 if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) { 100 return cache[idx] 101 } 102 // No luck in the cache, but we can start iterating from the last element we already know 103 lastKnownHash := cache[len(cache)-1] 104 lastKnownNumber := ref.Number.Uint64() - uint64(len(cache)) 105 106 for { 107 header := chain.GetHeader(lastKnownHash, lastKnownNumber) 108 if header == nil { 109 break 110 } 111 cache = append(cache, header.ParentHash) 112 lastKnownHash = header.ParentHash 113 lastKnownNumber = header.Number.Uint64() - 1 114 if n == lastKnownNumber { 115 return lastKnownHash 116 } 117 } 118 return common.Hash{} 119 } 120 } 121 122 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 123 // This does not take the necessary gas in to account to make the transfer valid. 124 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 125 return db.GetBalance(addr).Cmp(amount) >= 0 126 } 127 128 func CanTransferMC(db vm.StateDB, addr common.Address, to common.Address, coinID *common.Hash, amount *big.Int) bool { 129 if coinID == nil { 130 return true 131 } 132 if db.GetBalanceMultiCoin(addr, *coinID).Cmp(amount) >= 0 { 133 return true 134 } 135 // insufficient balance 136 return false 137 } 138 139 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 140 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 141 db.SubBalance(sender, amount) 142 db.AddBalance(recipient, amount) 143 } 144 145 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 146 func TransferMultiCoin(db vm.StateDB, sender, recipient common.Address, coinID *common.Hash, amount *big.Int) { 147 if coinID == nil { 148 return 149 } 150 db.SubBalanceMultiCoin(sender, *coinID, amount) 151 db.AddBalanceMultiCoin(recipient, *coinID, amount) 152 }