github.com/theQRL/go-zond@v0.2.1/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  
    23  	"github.com/theQRL/go-zond/common"
    24  	"github.com/theQRL/go-zond/core/rawdb"
    25  	"github.com/theQRL/go-zond/core/state"
    26  	"github.com/theQRL/go-zond/core/types"
    27  	"github.com/theQRL/go-zond/core/vm"
    28  	"github.com/theQRL/go-zond/crypto"
    29  	"github.com/theQRL/go-zond/params"
    30  )
    31  
    32  // Config is a basic type specifying certain configuration flags for running
    33  // the ZVM.
    34  type Config struct {
    35  	ChainConfig *params.ChainConfig
    36  	Origin      common.Address
    37  	Coinbase    common.Address
    38  	BlockNumber *big.Int
    39  	Time        uint64
    40  	GasLimit    uint64
    41  	GasPrice    *big.Int
    42  	Value       *big.Int
    43  	Debug       bool
    44  	ZVMConfig   vm.Config
    45  	BaseFee     *big.Int
    46  	Random      *common.Hash
    47  
    48  	State     *state.StateDB
    49  	GetHashFn func(n uint64) common.Hash
    50  }
    51  
    52  // sets defaults on the config
    53  func setDefaults(cfg *Config) {
    54  	if cfg.ChainConfig == nil {
    55  		cfg.ChainConfig = &params.ChainConfig{
    56  			ChainID: big.NewInt(1),
    57  		}
    58  	}
    59  
    60  	if cfg.GasLimit == 0 {
    61  		cfg.GasLimit = math.MaxUint64
    62  	}
    63  	if cfg.GasPrice == nil {
    64  		cfg.GasPrice = new(big.Int)
    65  	}
    66  	if cfg.Value == nil {
    67  		cfg.Value = new(big.Int)
    68  	}
    69  	if cfg.BlockNumber == nil {
    70  		cfg.BlockNumber = new(big.Int)
    71  	}
    72  	if cfg.GetHashFn == nil {
    73  		cfg.GetHashFn = func(n uint64) common.Hash {
    74  			return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
    75  		}
    76  	}
    77  	if cfg.BaseFee == nil {
    78  		cfg.BaseFee = big.NewInt(params.InitialBaseFee)
    79  	}
    80  	if cfg.Random == nil {
    81  		cfg.Random = &(common.Hash{})
    82  	}
    83  }
    84  
    85  // Execute executes the code using the input as call data during the execution.
    86  // It returns the ZVM's return value, the new state and an error if it failed.
    87  //
    88  // Execute sets up an in-memory, temporary, environment for the execution of
    89  // the given code. It makes sure that it's restored to its original state afterwards.
    90  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
    91  	if cfg == nil {
    92  		cfg = new(Config)
    93  	}
    94  	setDefaults(cfg)
    95  
    96  	if cfg.State == nil {
    97  		cfg.State, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    98  	}
    99  	var (
   100  		address = common.BytesToAddress([]byte("contract"))
   101  		vmenv   = NewEnv(cfg)
   102  		sender  = vm.AccountRef(cfg.Origin)
   103  		rules   = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time)
   104  	)
   105  	// Execute the preparatory steps for state transition which includes:
   106  	// - prepare accessList
   107  	cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil)
   108  	cfg.State.CreateAccount(address)
   109  	// set the receiver's (the executing contract) code for execution.
   110  	cfg.State.SetCode(address, code)
   111  	// Call the code with the given configuration.
   112  	ret, _, err := vmenv.Call(
   113  		sender,
   114  		common.BytesToAddress([]byte("contract")),
   115  		input,
   116  		cfg.GasLimit,
   117  		cfg.Value,
   118  	)
   119  	return ret, cfg.State, err
   120  }
   121  
   122  // Create executes the code using the ZVM 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(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   131  	}
   132  	var (
   133  		vmenv  = NewEnv(cfg)
   134  		sender = vm.AccountRef(cfg.Origin)
   135  		rules  = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time)
   136  	)
   137  	// Execute the preparatory steps for state transition which includes:
   138  	// - prepare accessList
   139  	cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, nil, vm.ActivePrecompiles(rules), nil)
   140  	// Call the code with the given configuration.
   141  	code, address, leftOverGas, err := vmenv.Create(
   142  		sender,
   143  		input,
   144  		cfg.GasLimit,
   145  		cfg.Value,
   146  	)
   147  	return code, address, leftOverGas, err
   148  }
   149  
   150  // Call executes the code given by the contract's address. It will return the
   151  // ZVM's return value or an error if it failed.
   152  //
   153  // Call, unlike Execute, requires a config and also requires the State field to
   154  // be set.
   155  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   156  	setDefaults(cfg)
   157  
   158  	var (
   159  		vmenv   = NewEnv(cfg)
   160  		sender  = cfg.State.GetOrNewStateObject(cfg.Origin)
   161  		statedb = cfg.State
   162  		rules   = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time)
   163  	)
   164  	// Execute the preparatory steps for state transition which includes:
   165  	// - prepare accessList(post-berlin)
   166  	statedb.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil)
   167  
   168  	// Call the code with the given configuration.
   169  	ret, leftOverGas, err := vmenv.Call(
   170  		sender,
   171  		address,
   172  		input,
   173  		cfg.GasLimit,
   174  		cfg.Value,
   175  	)
   176  	return ret, leftOverGas, err
   177  }