github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/vm/contracts.go (about)

     1  // Copyright 2014 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 vm
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/vntchain/go-vnt/common"
    23  	"github.com/vntchain/go-vnt/core/vm/election"
    24  	inter "github.com/vntchain/go-vnt/core/vm/interface"
    25  )
    26  
    27  // PrecompiledContract is the basic interface for native Go contracts. The implementation
    28  // requires a deterministic gas count based on the input size of the Run method of the
    29  // contract.
    30  type PrecompiledContract interface {
    31  	RequiredGas(input []byte) uint64                                              // RequiredPrice calculates the contract gas use
    32  	Run(context inter.ChainContext, input []byte, value *big.Int) ([]byte, error) // Run runs the precompiled contract
    33  }
    34  
    35  // PrecompiledContractsHubble contains the default set of pre-compiled VNT
    36  // contracts used in the Hubble release.
    37  var PrecompiledContractsHubble = map[common.Address]PrecompiledContract{
    38  	common.BytesToAddress([]byte{9}): &election.Election{},
    39  }
    40  
    41  // RunPrecompiledContract runs and evaluates the output of a precompiled contract.
    42  func RunPrecompiledContract(context inter.ChainContext, p PrecompiledContract, input []byte, contract inter.Contract) (ret []byte, err error) {
    43  	gas := p.RequiredGas(input)
    44  	if contract.UseGas(gas) {
    45  		return p.Run(context, input, contract.Value())
    46  	}
    47  	return nil, ErrOutOfGas
    48  }