github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/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/tacshi/go-ethereum/common"
    24  	"github.com/tacshi/go-ethereum/core/rawdb"
    25  	"github.com/tacshi/go-ethereum/core/state"
    26  	"github.com/tacshi/go-ethereum/core/vm"
    27  	"github.com/tacshi/go-ethereum/crypto"
    28  	"github.com/tacshi/go-ethereum/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        uint64
    40  	GasLimit    uint64
    41  	GasPrice    *big.Int
    42  	Value       *big.Int
    43  	Debug       bool
    44  	EVMConfig   vm.Config
    45  	BaseFee     *big.Int
    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  			BerlinBlock:         new(big.Int),
    69  			LondonBlock:         new(big.Int),
    70  		}
    71  	}
    72  
    73  	if cfg.Difficulty == nil {
    74  		cfg.Difficulty = new(big.Int)
    75  	}
    76  	if cfg.GasLimit == 0 {
    77  		cfg.GasLimit = math.MaxUint64
    78  	}
    79  	if cfg.GasPrice == nil {
    80  		cfg.GasPrice = new(big.Int)
    81  	}
    82  	if cfg.Value == nil {
    83  		cfg.Value = new(big.Int)
    84  	}
    85  	if cfg.BlockNumber == nil {
    86  		cfg.BlockNumber = new(big.Int)
    87  	}
    88  	if cfg.GetHashFn == nil {
    89  		cfg.GetHashFn = func(n uint64) common.Hash {
    90  			return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
    91  		}
    92  	}
    93  	if cfg.BaseFee == nil {
    94  		cfg.BaseFee = big.NewInt(params.InitialBaseFee)
    95  	}
    96  }
    97  
    98  // Execute executes the code using the input as call data during the execution.
    99  // It returns the EVM's return value, the new state and an error if it failed.
   100  //
   101  // Execute sets up an in-memory, temporary, environment for the execution of
   102  // the given code. It makes sure that it's restored to its original state afterwards.
   103  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
   104  	if cfg == nil {
   105  		cfg = new(Config)
   106  	}
   107  	setDefaults(cfg)
   108  
   109  	if cfg.State == nil {
   110  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   111  	}
   112  	var (
   113  		address = common.BytesToAddress([]byte("contract"))
   114  		vmenv   = NewEnv(cfg)
   115  		sender  = vm.AccountRef(cfg.Origin)
   116  		rules   = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time, vmenv.Context.ArbOSVersion)
   117  	)
   118  	// Execute the preparatory steps for state transition which includes:
   119  	// - prepare accessList(post-berlin)
   120  	// - reset transient storage(eip 1153)
   121  	cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil)
   122  	cfg.State.CreateAccount(address)
   123  	// set the receiver's (the executing contract) code for execution.
   124  	cfg.State.SetCode(address, code)
   125  	// Call the code with the given configuration.
   126  	ret, _, err := vmenv.Call(
   127  		sender,
   128  		common.BytesToAddress([]byte("contract")),
   129  		input,
   130  		cfg.GasLimit,
   131  		cfg.Value,
   132  	)
   133  	return ret, cfg.State, err
   134  }
   135  
   136  // Create executes the code using the EVM create method
   137  func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
   138  	if cfg == nil {
   139  		cfg = new(Config)
   140  	}
   141  	setDefaults(cfg)
   142  
   143  	if cfg.State == nil {
   144  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   145  	}
   146  	var (
   147  		vmenv  = NewEnv(cfg)
   148  		sender = vm.AccountRef(cfg.Origin)
   149  		rules  = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time, vmenv.Context.ArbOSVersion)
   150  	)
   151  	// Execute the preparatory steps for state transition which includes:
   152  	// - prepare accessList(post-berlin)
   153  	// - reset transient storage(eip 1153)
   154  	cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, nil, vm.ActivePrecompiles(rules), nil)
   155  	// Call the code with the given configuration.
   156  	code, address, leftOverGas, err := vmenv.Create(
   157  		sender,
   158  		input,
   159  		cfg.GasLimit,
   160  		cfg.Value,
   161  	)
   162  	return code, address, leftOverGas, err
   163  }
   164  
   165  // Call executes the code given by the contract's address. It will return the
   166  // EVM's return value or an error if it failed.
   167  //
   168  // Call, unlike Execute, requires a config and also requires the State field to
   169  // be set.
   170  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   171  	setDefaults(cfg)
   172  
   173  	var (
   174  		vmenv   = NewEnv(cfg)
   175  		sender  = cfg.State.GetOrNewStateObject(cfg.Origin)
   176  		statedb = cfg.State
   177  		rules   = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time, vmenv.Context.ArbOSVersion)
   178  	)
   179  	// Execute the preparatory steps for state transition which includes:
   180  	// - prepare accessList(post-berlin)
   181  	// - reset transient storage(eip 1153)
   182  	statedb.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil)
   183  
   184  	// Call the code with the given configuration.
   185  	ret, leftOverGas, err := vmenv.Call(
   186  		sender,
   187  		address,
   188  		input,
   189  		cfg.GasLimit,
   190  		cfg.Value,
   191  	)
   192  	return ret, leftOverGas, err
   193  }