github.com/m3shine/gochain@v2.2.26+incompatible/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/gochain-io/gochain/common" 23 "github.com/gochain-io/gochain/consensus" 24 "github.com/gochain-io/gochain/core/types" 25 "github.com/gochain-io/gochain/core/vm" 26 ) 27 28 // ChainContext supports retrieving headers and consensus parameters from the 29 // current blockchain to be used during transaction processing. 30 type ChainContext interface { 31 // Engine retrieves the chain's consensus engine. 32 Engine() consensus.Engine 33 34 // GetHeader returns the hash corresponding to their hash. 35 GetHeader(common.Hash, uint64) *types.Header 36 } 37 38 // NewEVMContext creates a new context for use in the EVM. 39 func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context { 40 // If we don't have an explicit author (i.e. not mining), extract from the header 41 var beneficiary common.Address 42 if author == nil { 43 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 44 } else { 45 beneficiary = *author 46 } 47 return vm.Context{ 48 CanTransfer: CanTransfer, 49 Transfer: Transfer, 50 GetHash: GetHashFn(header, chain), 51 Origin: msg.From(), 52 Coinbase: beneficiary, 53 BlockNumber: header.Number, 54 Time: header.Time, 55 Difficulty: header.Difficulty, 56 GasLimit: header.GasLimit, 57 GasPrice: msg.GasPrice(), 58 } 59 } 60 61 // NewEVMContextLite is like NewEVMContext, but leaves the Origin and GasPrice to be set later. 62 func NewEVMContextLite(header *types.Header, chain ChainContext, author *common.Address) vm.Context { 63 // If we don't have an explicit author (i.e. not mining), extract from the header 64 var beneficiary common.Address 65 if author == nil { 66 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 67 } else { 68 beneficiary = *author 69 } 70 return vm.Context{ 71 CanTransfer: CanTransfer, 72 Transfer: Transfer, 73 GetHash: GetHashFn(header, chain), 74 Coinbase: beneficiary, 75 BlockNumber: header.Number, 76 Time: header.Time, 77 Difficulty: header.Difficulty, 78 GasLimit: header.GasLimit, 79 } 80 } 81 82 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 83 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 84 var cache map[uint64]common.Hash 85 86 return func(n uint64) common.Hash { 87 // If there's no hash cache yet, make one 88 if cache == nil { 89 cache = map[uint64]common.Hash{ 90 ref.Number.Uint64() - 1: ref.ParentHash, 91 } 92 } 93 // Try to fulfill the request from the cache 94 if hash, ok := cache[n]; ok { 95 return hash 96 } 97 // Not cached, iterate the blocks and cache the hashes 98 for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) { 99 cache[header.Number.Uint64()-1] = header.ParentHash 100 if n == header.Number.Uint64()-1 { 101 return header.ParentHash 102 } 103 } 104 return common.Hash{} 105 } 106 } 107 108 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 109 // This does not take the necessary gas in to account to make the transfer valid. 110 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 111 return db.GetBalance(addr).Cmp(amount) >= 0 112 } 113 114 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 115 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 116 db.SubBalance(sender, amount) 117 db.AddBalance(recipient, amount) 118 }