github.com/Night-mk/quorum@v21.1.0+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 "context" 21 "math/big" 22 "reflect" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/consensus" 26 "github.com/ethereum/go-ethereum/core/types" 27 "github.com/ethereum/go-ethereum/core/vm" 28 "github.com/ethereum/go-ethereum/multitenancy" 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 multitenancy.ContextAware 35 // Engine retrieves the chain's consensus engine. 36 Engine() consensus.Engine 37 38 // GetHeader returns the hash corresponding to their hash. 39 GetHeader(common.Hash, uint64) *types.Header 40 } 41 42 // NewEVMContext creates a new context for use in the EVM. 43 func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context { 44 // If we don't have an explicit author (i.e. not mining), extract from the header 45 var beneficiary common.Address 46 if author == nil { 47 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 48 } else { 49 beneficiary = *author 50 } 51 supportsMultitenancy := false 52 // mainly to overcome lost of test cases which pass ChainContext as nil value 53 // nil interface requires this check to make sure we don't get nil pointer reference error 54 if chain != nil && !reflect.ValueOf(chain).IsNil() { 55 _, supportsMultitenancy = chain.SupportsMultitenancy(nil) 56 } 57 return vm.Context{ 58 CanTransfer: CanTransfer, 59 Transfer: Transfer, 60 GetHash: GetHashFn(header, chain), 61 Origin: msg.From(), 62 Coinbase: beneficiary, 63 BlockNumber: new(big.Int).Set(header.Number), 64 Time: new(big.Int).SetUint64(header.Time), 65 Difficulty: new(big.Int).Set(header.Difficulty), 66 GasLimit: header.GasLimit, 67 GasPrice: new(big.Int).Set(msg.GasPrice()), 68 69 SupportsMultitenancy: supportsMultitenancy, 70 } 71 } 72 73 // Quorum 74 // 75 // This EVM context is meant for simulation when doing multitenancy check. 76 // It enriches the given EVM context with multitenancy-specific references 77 func NewMultitenancyAwareEVMContext(ctx context.Context, evmCtx vm.Context) vm.Context { 78 if f, ok := ctx.Value(multitenancy.CtxKeyAuthorizeCreateFunc).(multitenancy.AuthorizeCreateFunc); ok { 79 evmCtx.AuthorizeCreateFunc = f 80 } 81 if f, ok := ctx.Value(multitenancy.CtxKeyAuthorizeMessageCallFunc).(multitenancy.AuthorizeMessageCallFunc); ok { 82 evmCtx.AuthorizeMessageCallFunc = f 83 } 84 return evmCtx 85 } 86 87 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 88 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 89 var cache map[uint64]common.Hash 90 91 return func(n uint64) common.Hash { 92 // If there's no hash cache yet, make one 93 if cache == nil { 94 cache = map[uint64]common.Hash{ 95 ref.Number.Uint64() - 1: ref.ParentHash, 96 } 97 } 98 // Try to fulfill the request from the cache 99 if hash, ok := cache[n]; ok { 100 return hash 101 } 102 // Not cached, iterate the blocks and cache the hashes 103 for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) { 104 cache[header.Number.Uint64()-1] = header.ParentHash 105 if n == header.Number.Uint64()-1 { 106 return header.ParentHash 107 } 108 } 109 return common.Hash{} 110 } 111 } 112 113 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 114 // This does not take the necessary gas in to account to make the transfer valid. 115 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 116 return db.GetBalance(addr).Cmp(amount) >= 0 117 } 118 119 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 120 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 121 db.SubBalance(sender, amount) 122 db.AddBalance(recipient, amount) 123 }