github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/evm.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "github.com/SmartMeshFoundation/Spectrum/log" 21 "math/big" 22 23 "github.com/SmartMeshFoundation/Spectrum/common" 24 "github.com/SmartMeshFoundation/Spectrum/consensus" 25 "github.com/SmartMeshFoundation/Spectrum/core/types" 26 "github.com/SmartMeshFoundation/Spectrum/core/vm" 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 // NewEVMContext creates a new context for use in the EVM. 40 func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context { 41 // If we don't have an explicit author (i.e. not mining), extract from the header 42 var beneficiary common.Address 43 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 44 /* 45 if author == nil { 46 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 47 } else { 48 beneficiary = *author 49 } 50 */ 51 log.Debug("<<NewEVMContext.SetCoinbase>>", "num", header.Number, "ignore_author", author, "beneficiary", beneficiary) 52 return vm.Context{ 53 CanTransfer: CanTransfer, 54 Transfer: Transfer, 55 GetHash: GetHashFn(header, chain), 56 Origin: msg.From(), 57 Coinbase: beneficiary, 58 BlockNumber: new(big.Int).Set(header.Number), 59 Time: new(big.Int).Set(header.Time), 60 Difficulty: new(big.Int).Set(header.Difficulty), 61 GasLimit: new(big.Int).Set(header.GasLimit), 62 GasPrice: new(big.Int).Set(msg.GasPrice()), 63 } 64 } 65 66 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 67 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 68 return func(n uint64) common.Hash { 69 for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) { 70 if header.Number.Uint64() == n { 71 return header.Hash() 72 } 73 } 74 75 return common.Hash{} 76 } 77 } 78 79 // CanTransfer checks wether there are enough funds in the address' account to make a transfer. 80 // This does not take the necessary gas in to account to make the transfer valid. 81 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 82 b := db.GetBalance(addr) 83 log.Debug("<<evm.CanTransfer>>", "addr", addr.Hex(), "amount", amount, "balance", b, "cantransfer", b.Cmp(amount) >= 0) 84 return b.Cmp(amount) >= 0 85 } 86 87 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 88 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 89 db.SubBalance(sender, amount) 90 db.AddBalance(recipient, amount) 91 }