github.com/core-coin/go-core/v2@v2.1.9/core/cvm.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/core-coin/go-core/v2/common"
    23  	"github.com/core-coin/go-core/v2/consensus"
    24  	"github.com/core-coin/go-core/v2/core/types"
    25  	"github.com/core-coin/go-core/v2/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  // NewCVMBlockContext creates a new context for use in the CVM.
    39  func NewCVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
    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.BlockContext{
    48  		CanTransfer: CanTransfer,
    49  		Transfer:    Transfer,
    50  		GetHash:     GetHashFn(header, chain),
    51  		Coinbase:    beneficiary,
    52  		BlockNumber: new(big.Int).Set(header.Number),
    53  		Time:        new(big.Int).SetUint64(header.Time),
    54  		Difficulty:  new(big.Int).Set(header.Difficulty),
    55  		EnergyLimit: header.EnergyLimit,
    56  	}
    57  }
    58  
    59  // NewCVMTxContext creates a new transaction context for a single transaction.
    60  func NewCVMTxContext(msg Message) vm.TxContext {
    61  	return vm.TxContext{
    62  		Origin:      msg.From(),
    63  		EnergyPrice: new(big.Int).Set(msg.EnergyPrice()),
    64  	}
    65  }
    66  
    67  // GetHashFn returns a GetHashFunc which retrieves header hashes by number
    68  func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
    69  	// Cache will initially contain [refHash.parent],
    70  	// Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
    71  	var cache []common.Hash
    72  
    73  	return func(n uint64) common.Hash {
    74  		// If there's no hash cache yet, make one
    75  		if len(cache) == 0 {
    76  			cache = append(cache, ref.ParentHash)
    77  		}
    78  		if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
    79  			return cache[idx]
    80  		}
    81  		// No luck in the cache, but we can start iterating from the last element we already know
    82  		lastKnownHash := cache[len(cache)-1]
    83  		lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
    84  
    85  		for {
    86  			header := chain.GetHeader(lastKnownHash, lastKnownNumber)
    87  			if header == nil {
    88  				break
    89  			}
    90  			cache = append(cache, header.ParentHash)
    91  			lastKnownHash = header.ParentHash
    92  			lastKnownNumber = header.Number.Uint64() - 1
    93  			if n == lastKnownNumber {
    94  				return lastKnownHash
    95  			}
    96  		}
    97  		return common.Hash{}
    98  	}
    99  }
   100  
   101  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
   102  // This does not take the necessary energy in to account to make the transfer valid.
   103  func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
   104  	return db.GetBalance(addr).Cmp(amount) >= 0
   105  }
   106  
   107  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
   108  func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
   109  	db.SubBalance(sender, amount)
   110  	db.AddBalance(recipient, amount)
   111  }