github.com/energicryptocurrency/go-energi@v1.1.7/core/evm.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package core
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/energicryptocurrency/go-energi/common"
    24  	"github.com/energicryptocurrency/go-energi/consensus"
    25  	"github.com/energicryptocurrency/go-energi/core/types"
    26  	"github.com/energicryptocurrency/go-energi/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  	if author == nil {
    44  		beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
    45  	} else {
    46  		beneficiary = *author
    47  	}
    48  	return vm.Context{
    49  		CanTransfer: CanTransfer,
    50  		Transfer:    Transfer,
    51  		GetHash:     GetHashFn(header, chain),
    52  		Origin:      msg.From(),
    53  		Coinbase:    beneficiary,
    54  		BlockNumber: new(big.Int).Set(header.Number),
    55  		Time:        new(big.Int).SetUint64(header.Time),
    56  		Difficulty:  new(big.Int).Set(header.Difficulty),
    57  		GasLimit:    header.GasLimit,
    58  		GasPrice:    new(big.Int).Set(msg.GasPrice()),
    59  	}
    60  }
    61  
    62  // GetHashFn returns a GetHashFunc which retrieves header hashes by number
    63  func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
    64  	var cache map[uint64]common.Hash
    65  
    66  	return func(n uint64) common.Hash {
    67  		// If there's no hash cache yet, make one
    68  		if cache == nil {
    69  			cache = map[uint64]common.Hash{
    70  				ref.Number.Uint64() - 1: ref.ParentHash,
    71  			}
    72  		}
    73  		// Try to fulfill the request from the cache
    74  		if hash, ok := cache[n]; ok {
    75  			return hash
    76  		}
    77  		// Not cached, iterate the blocks and cache the hashes
    78  		for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
    79  			cache[header.Number.Uint64()-1] = header.ParentHash
    80  			if n == header.Number.Uint64()-1 {
    81  				return header.ParentHash
    82  			}
    83  		}
    84  		return common.Hash{}
    85  	}
    86  }
    87  
    88  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
    89  // This does not take the necessary gas in to account to make the transfer valid.
    90  func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
    91  	if amount.Cmp(common.Big0) != 0 && IsBlacklisted(db, addr) {
    92  		return false
    93  	}
    94  
    95  	return db.GetBalance(addr).Cmp(amount) >= 0
    96  }
    97  
    98  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
    99  func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
   100  	db.SubBalance(sender, amount)
   101  	db.AddBalance(recipient, amount)
   102  }