github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/core/vm/runtime/runtime.go (about)

     1  // Copyright 2015 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 MiningReward, 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 runtime
    18  
    19  import (
    20  	"math"
    21  	"math/big"
    22  	"time"
    23  
    24  	"github.com/insight-chain/inb-go/common"
    25  	"github.com/insight-chain/inb-go/core/state"
    26  	"github.com/insight-chain/inb-go/core/vm"
    27  	"github.com/insight-chain/inb-go/crypto"
    28  	"github.com/insight-chain/inb-go/ethdb"
    29  	"github.com/insight-chain/inb-go/params"
    30  )
    31  
    32  // Config is a basic type specifying certain configuration flags for running
    33  // the EVM.
    34  type Config struct {
    35  	ChainConfig *params.ChainConfig
    36  	Difficulty  *big.Int
    37  	Origin      common.Address
    38  	Coinbase    common.Address
    39  	BlockNumber *big.Int
    40  	Time        *big.Int
    41  	GasLimit    uint64
    42  	GasPrice    *big.Int
    43  	Value       *big.Int
    44  	Debug       bool
    45  	EVMConfig   vm.Config
    46  
    47  	State     *state.StateDB
    48  	GetHashFn func(n uint64) common.Hash
    49  }
    50  
    51  // sets defaults on the config
    52  func setDefaults(cfg *Config) {
    53  	if cfg.ChainConfig == nil {
    54  		cfg.ChainConfig = &params.ChainConfig{
    55  			ChainID:        big.NewInt(1),
    56  			HomesteadBlock: new(big.Int),
    57  			EIP150Block:    new(big.Int),
    58  			EIP155Block:    new(big.Int),
    59  			EIP158Block:    new(big.Int),
    60  		}
    61  	}
    62  
    63  	if cfg.Difficulty == nil {
    64  		cfg.Difficulty = new(big.Int)
    65  	}
    66  	if cfg.Time == nil {
    67  		cfg.Time = big.NewInt(time.Now().Unix())
    68  	}
    69  	if cfg.GasLimit == 0 {
    70  		cfg.GasLimit = math.MaxUint64
    71  	}
    72  	if cfg.GasPrice == nil {
    73  		cfg.GasPrice = new(big.Int)
    74  	}
    75  	if cfg.Value == nil {
    76  		cfg.Value = new(big.Int)
    77  	}
    78  	if cfg.BlockNumber == nil {
    79  		cfg.BlockNumber = new(big.Int)
    80  	}
    81  	if cfg.GetHashFn == nil {
    82  		cfg.GetHashFn = func(n uint64) common.Hash {
    83  			return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
    84  		}
    85  	}
    86  }
    87  
    88  // Execute executes the code using the input as call data during the execution.
    89  // It returns the EVM's return value, the new state and an error if it failed.
    90  //
    91  // Executes sets up a in memory, temporarily, environment for the execution of
    92  // the given code. It makes sure that it's restored to it's original state afterwards.
    93  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
    94  	if cfg == nil {
    95  		cfg = new(Config)
    96  	}
    97  	setDefaults(cfg)
    98  
    99  	if cfg.State == nil {
   100  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
   101  	}
   102  	var (
   103  		address = common.BytesToAddress([]byte("contract"))
   104  		vmenv   = NewEnv(cfg)
   105  		sender  = vm.AccountRef(cfg.Origin)
   106  	)
   107  	cfg.State.CreateAccount(address)
   108  	// set the receiver's (the executing contract) code for execution.
   109  	cfg.State.SetCode(address, code)
   110  	// Call the code with the given configuration.
   111  	ret, _, err,_ := vmenv.Call(
   112  		sender,
   113  		common.BytesToAddress([]byte("contract")),
   114  		input,
   115  		cfg.GasLimit,
   116  		cfg.Value,
   117  	)
   118  
   119  	return ret, cfg.State, err
   120  }
   121  
   122  // Create executes the code using the EVM create method
   123  func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
   124  	if cfg == nil {
   125  		cfg = new(Config)
   126  	}
   127  	setDefaults(cfg)
   128  
   129  	if cfg.State == nil {
   130  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
   131  	}
   132  	var (
   133  		vmenv  = NewEnv(cfg)
   134  		sender = vm.AccountRef(cfg.Origin)
   135  	)
   136  
   137  	// Call the code with the given configuration.
   138  	code, address, leftOverGas, err := vmenv.Create(
   139  		sender,
   140  		input,
   141  		cfg.GasLimit,
   142  		cfg.Value,
   143  	)
   144  	return code, address, leftOverGas, err
   145  }
   146  
   147  // Call executes the code given by the contract's address. It will return the
   148  // EVM's return value or an error if it failed.
   149  //
   150  // Call, unlike Execute, requires a config and also requires the State field to
   151  // be set.
   152  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   153  	setDefaults(cfg)
   154  
   155  	vmenv := NewEnv(cfg)
   156  
   157  	sender := cfg.State.GetOrNewStateObject(cfg.Origin)
   158  	// Call the code with the given configuration.
   159  	ret, leftOverGas, err,_ := vmenv.Call(
   160  		sender,
   161  		address,
   162  		input,
   163  		cfg.GasLimit,
   164  		cfg.Value,
   165  	)
   166  
   167  	return ret, leftOverGas, err
   168  }