github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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"
    21  	"math/big"
    22  	"time"
    23  
    24  	"github.com/kisexp/xdchain/common"
    25  	"github.com/kisexp/xdchain/core/rawdb"
    26  	"github.com/kisexp/xdchain/core/state"
    27  	"github.com/kisexp/xdchain/core/vm"
    28  	"github.com/kisexp/xdchain/crypto"
    29  	"github.com/kisexp/xdchain/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  			DAOForkBlock:        new(big.Int),
    58  			DAOForkSupport:      false,
    59  			EIP150Block:         new(big.Int),
    60  			EIP150Hash:          common.Hash{},
    61  			EIP155Block:         new(big.Int),
    62  			EIP158Block:         new(big.Int),
    63  			ByzantiumBlock:      new(big.Int),
    64  			ConstantinopleBlock: new(big.Int),
    65  			PetersburgBlock:     new(big.Int),
    66  			IstanbulBlock:       new(big.Int),
    67  			MuirGlacierBlock:    new(big.Int),
    68  			YoloV2Block:         nil,
    69  		}
    70  	}
    71  
    72  	if cfg.Difficulty == nil {
    73  		cfg.Difficulty = new(big.Int)
    74  	}
    75  	if cfg.Time == nil {
    76  		cfg.Time = big.NewInt(time.Now().Unix())
    77  	}
    78  	if cfg.GasLimit == 0 {
    79  		cfg.GasLimit = math.MaxUint64
    80  	}
    81  	if cfg.GasPrice == nil {
    82  		cfg.GasPrice = new(big.Int)
    83  	}
    84  	if cfg.Value == nil {
    85  		cfg.Value = new(big.Int)
    86  	}
    87  	if cfg.BlockNumber == nil {
    88  		cfg.BlockNumber = new(big.Int)
    89  	}
    90  	if cfg.GetHashFn == nil {
    91  		cfg.GetHashFn = func(n uint64) common.Hash {
    92  			return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
    93  		}
    94  	}
    95  }
    96  
    97  // Execute executes the code using the input as call data during the execution.
    98  // It returns the EVM's return value, the new state and an error if it failed.
    99  //
   100  // Execute sets up an in-memory, temporary, environment for the execution of
   101  // the given code. It makes sure that it's restored to its original state afterwards.
   102  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
   103  	if cfg == nil {
   104  		cfg = new(Config)
   105  	}
   106  	setDefaults(cfg)
   107  
   108  	if cfg.State == nil {
   109  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   110  	}
   111  	var (
   112  		address = common.BytesToAddress([]byte("contract"))
   113  		vmenv   = NewEnv(cfg)
   114  		sender  = vm.AccountRef(cfg.Origin)
   115  	)
   116  	if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
   117  		cfg.State.AddAddressToAccessList(cfg.Origin)
   118  		cfg.State.AddAddressToAccessList(address)
   119  		for _, addr := range vmenv.ActivePrecompiles() {
   120  			cfg.State.AddAddressToAccessList(addr)
   121  		}
   122  	}
   123  	cfg.State.CreateAccount(address)
   124  	// set the receiver's (the executing contract) code for execution.
   125  	cfg.State.SetCode(address, code)
   126  	// Call the code with the given configuration.
   127  	ret, _, err := vmenv.Call(
   128  		sender,
   129  		common.BytesToAddress([]byte("contract")),
   130  		input,
   131  		cfg.GasLimit,
   132  		cfg.Value,
   133  	)
   134  
   135  	return ret, cfg.State, err
   136  }
   137  
   138  // Create executes the code using the EVM create method
   139  func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
   140  	if cfg == nil {
   141  		cfg = new(Config)
   142  	}
   143  	setDefaults(cfg)
   144  
   145  	if cfg.State == nil {
   146  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   147  	}
   148  	var (
   149  		vmenv  = NewEnv(cfg)
   150  		sender = vm.AccountRef(cfg.Origin)
   151  	)
   152  	if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
   153  		cfg.State.AddAddressToAccessList(cfg.Origin)
   154  		for _, addr := range vmenv.ActivePrecompiles() {
   155  			cfg.State.AddAddressToAccessList(addr)
   156  		}
   157  	}
   158  
   159  	// Call the code with the given configuration.
   160  	code, address, leftOverGas, err := vmenv.Create(
   161  		sender,
   162  		input,
   163  		cfg.GasLimit,
   164  		cfg.Value,
   165  	)
   166  	return code, address, leftOverGas, err
   167  }
   168  
   169  // Call executes the code given by the contract's address. It will return the
   170  // EVM's return value or an error if it failed.
   171  //
   172  // Call, unlike Execute, requires a config and also requires the State field to
   173  // be set.
   174  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   175  	setDefaults(cfg)
   176  
   177  	vmenv := NewEnv(cfg)
   178  
   179  	sender := cfg.State.GetOrNewStateObject(cfg.Origin)
   180  	if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
   181  		cfg.State.AddAddressToAccessList(cfg.Origin)
   182  		cfg.State.AddAddressToAccessList(address)
   183  		for _, addr := range vmenv.ActivePrecompiles() {
   184  			cfg.State.AddAddressToAccessList(addr)
   185  		}
   186  	}
   187  
   188  	// Call the code with the given configuration.
   189  	ret, leftOverGas, err := vmenv.Call(
   190  		sender,
   191  		address,
   192  		input,
   193  		cfg.GasLimit,
   194  		cfg.Value,
   195  	)
   196  
   197  	return ret, leftOverGas, err
   198  }