github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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 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 runtime
    18  
    19  import (
    20  	"math/big"
    21  	"time"
    22  
    23  	"github.com/atheioschain/go-atheios/common"
    24  	"github.com/atheioschain/go-atheios/core/state"
    25  	"github.com/atheioschain/go-atheios/core/vm"
    26  	"github.com/atheioschain/go-atheios/crypto"
    27  	"github.com/atheioschain/go-atheios/ethdb"
    28  	"github.com/atheioschain/go-atheios/params"
    29  )
    30  
    31  // Config is a basic type specifying certain configuration flags for running
    32  // the EVM.
    33  type Config struct {
    34  	ChainConfig *params.ChainConfig
    35  	Difficulty  *big.Int
    36  	Origin      common.Address
    37  	Coinbase    common.Address
    38  	BlockNumber *big.Int
    39  	Time        *big.Int
    40  	GasLimit    *big.Int
    41  	GasPrice    *big.Int
    42  	Value       *big.Int
    43  	DisableJit  bool // "disable" so it's enabled by default
    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(8),
    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 == nil {
    70  		cfg.GasLimit = new(big.Int).Set(common.MaxBig)
    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 enabled the JIT by default and make sure that it's restored
    93  // to it's original state afterwards.
    94  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
    95  	if cfg == nil {
    96  		cfg = new(Config)
    97  	}
    98  	setDefaults(cfg)
    99  
   100  	if cfg.State == nil {
   101  		db, _ := ethdb.NewMemDatabase()
   102  		cfg.State, _ = state.New(common.Hash{}, db)
   103  	}
   104  	var (
   105  		vmenv    = NewEnv(cfg, cfg.State)
   106  		sender   = cfg.State.CreateAccount(cfg.Origin)
   107  		receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
   108  	)
   109  	// set the receiver's (the executing contract) code for execution.
   110  	receiver.SetCode(crypto.Keccak256Hash(code), code)
   111  
   112  	// Call the code with the given configuration.
   113  	ret, err := vmenv.Call(
   114  		sender,
   115  		receiver.Address(),
   116  		input,
   117  		cfg.GasLimit,
   118  		cfg.Value,
   119  	)
   120  
   121  	return ret, cfg.State, err
   122  }
   123  
   124  // Create executes the code using the EVM create method
   125  func Create(input []byte, cfg *Config) ([]byte, common.Address, error) {
   126  	if cfg == nil {
   127  		cfg = new(Config)
   128  	}
   129  	setDefaults(cfg)
   130  
   131  	if cfg.State == nil {
   132  		db, _ := ethdb.NewMemDatabase()
   133  		cfg.State, _ = state.New(common.Hash{}, db)
   134  	}
   135  	var (
   136  		vmenv  = NewEnv(cfg, cfg.State)
   137  		sender = cfg.State.CreateAccount(cfg.Origin)
   138  	)
   139  
   140  	// Call the code with the given configuration.
   141  	return vmenv.Create(
   142  		sender,
   143  		input,
   144  		cfg.GasLimit,
   145  		cfg.Value,
   146  	)
   147  }
   148  
   149  // Call executes the code given by the contract's address. It will return the
   150  // EVM's return value or an error if it failed.
   151  //
   152  // Call, unlike Execute, requires a config and also requires the State field to
   153  // be set.
   154  func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
   155  	setDefaults(cfg)
   156  
   157  	vmenv := NewEnv(cfg, cfg.State)
   158  
   159  	sender := cfg.State.GetOrNewStateObject(cfg.Origin)
   160  	// Call the code with the given configuration.
   161  	ret, err := vmenv.Call(
   162  		sender,
   163  		address,
   164  		input,
   165  		cfg.GasLimit,
   166  		cfg.Value,
   167  	)
   168  
   169  	return ret, err
   170  }