github.com/annchain/OG@v0.0.9/vm/ovm/vm_context.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 ovm
    18  
    19  import (
    20  	ogTypes "github.com/annchain/OG/arefactor/og_interface"
    21  	"github.com/annchain/OG/common/math"
    22  	vmtypes "github.com/annchain/OG/vm/types"
    23  	"math/big"
    24  )
    25  
    26  // TxContext represents all information that evm needs to know about the tx current processing.
    27  type TxContext struct {
    28  	From  *ogTypes.Address20
    29  	To    *ogTypes.Address20
    30  	Value *math.BigInt
    31  	Data  []byte
    32  
    33  	// Temporarily keep using gas as resource billing
    34  	GasLimit   uint64
    35  	GasPrice   *math.BigInt
    36  	Coinbase   *ogTypes.Address20 // Provides information for COINBASE
    37  	SequenceID uint64             // Provides information for SequenceID
    38  	//Time        *math.BigInt      // Provides information for TIME
    39  	//Difficulty  *math.BigInt      // Provides information for DIFFICULTY
    40  }
    41  
    42  // ChainContext supports retrieving headers and consensus parameters from the
    43  // current blockchain to be used during transaction processing.
    44  type ChainContext interface {
    45  }
    46  
    47  type DefaultChainContext struct {
    48  }
    49  
    50  // NewOVMContext creates a new context for use in the OVM.
    51  func NewOVMContext(chainContext ChainContext, coinBase ogTypes.Address, stateDB vmtypes.StateDB) *vmtypes.Context {
    52  	return &vmtypes.Context{
    53  		CanTransfer: CanTransfer,
    54  		Transfer:    Transfer,
    55  		StateDB:     stateDB,
    56  	}
    57  }
    58  
    59  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
    60  // This does not take the necessary gas in to account to make the transfer valid.
    61  func CanTransfer(db vmtypes.StateDB, addr ogTypes.Address, amount *big.Int) bool {
    62  	return db.GetBalance(addr).Value.Cmp(amount) >= 0
    63  }
    64  
    65  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
    66  func Transfer(db vmtypes.StateDB, sender, recipient ogTypes.Address, amount *big.Int) {
    67  	a := math.NewBigIntFromBigInt(amount)
    68  	db.SubBalance(sender, a)
    69  	db.AddBalance(recipient, a)
    70  }