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