github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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/kisexp/xdchain/common" 23 "github.com/kisexp/xdchain/consensus" 24 "github.com/kisexp/xdchain/core/mps" 25 "github.com/kisexp/xdchain/core/state" 26 "github.com/kisexp/xdchain/core/types" 27 "github.com/kisexp/xdchain/core/vm" 28 "github.com/kisexp/xdchain/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 // Config retrieves the chain's fork configuration 41 Config() *params.ChainConfig 42 43 // Quorum 44 45 // QuorumConfig retrieves the Quorum chain's configuration 46 QuorumConfig() *QuorumChainConfig 47 48 // PrivateStateManager returns the private state manager 49 PrivateStateManager() mps.PrivateStateManager 50 51 // CheckAndSetPrivateState updates the private state as a part contract state extension 52 CheckAndSetPrivateState(txLogs []*types.Log, privateState *state.StateDB, psi types.PrivateStateIdentifier) 53 54 // End Quorum 55 } 56 57 // NewEVMBlockContext creates a new context for use in the EVM. 58 func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext { 59 // If we don't have an explicit author (i.e. not mining), extract from the header 60 var beneficiary common.Address 61 if author == nil { 62 beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation 63 } else { 64 beneficiary = *author 65 } 66 return vm.BlockContext{ 67 CanTransfer: CanTransfer, 68 Transfer: Transfer, 69 GetHash: GetHashFn(header, chain), 70 Coinbase: beneficiary, 71 BlockNumber: new(big.Int).Set(header.Number), 72 Time: new(big.Int).SetUint64(header.Time), 73 Difficulty: new(big.Int).Set(header.Difficulty), 74 GasLimit: header.GasLimit, 75 } 76 } 77 78 // NewEVMTxContext creates a new transaction context for a single transaction. 79 func NewEVMTxContext(msg Message) vm.TxContext { 80 return vm.TxContext{ 81 Origin: msg.From(), 82 GasPrice: new(big.Int).Set(msg.GasPrice()), 83 } 84 } 85 86 // GetHashFn returns a GetHashFunc which retrieves header hashes by number 87 func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash { 88 // Cache will initially contain [refHash.parent], 89 // Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...] 90 var cache []common.Hash 91 92 return func(n uint64) common.Hash { 93 // If there's no hash cache yet, make one 94 if len(cache) == 0 { 95 cache = append(cache, ref.ParentHash) 96 } 97 if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) { 98 return cache[idx] 99 } 100 // No luck in the cache, but we can start iterating from the last element we already know 101 lastKnownHash := cache[len(cache)-1] 102 lastKnownNumber := ref.Number.Uint64() - uint64(len(cache)) 103 104 for { 105 header := chain.GetHeader(lastKnownHash, lastKnownNumber) 106 if header == nil { 107 break 108 } 109 cache = append(cache, header.ParentHash) 110 lastKnownHash = header.ParentHash 111 lastKnownNumber = header.Number.Uint64() - 1 112 if n == lastKnownNumber { 113 return lastKnownHash 114 } 115 } 116 return common.Hash{} 117 } 118 } 119 120 // CanTransfer checks whether there are enough funds in the address' account to make a transfer. 121 // This does not take the necessary gas in to account to make the transfer valid. 122 func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { 123 return db.GetBalance(addr).Cmp(amount) >= 0 124 } 125 126 // Transfer subtracts amount from sender and adds amount to recipient using the given Db 127 func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { 128 db.SubBalance(sender, amount) 129 db.AddBalance(recipient, amount) 130 }