github.com/klaytn/klaytn@v1.10.2/blockchain/vm/runtime/runtime.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/runtime/runtime.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package runtime
    22  
    23  import (
    24  	"math"
    25  	"math/big"
    26  	"time"
    27  
    28  	"github.com/klaytn/klaytn/blockchain/state"
    29  	"github.com/klaytn/klaytn/blockchain/types"
    30  	"github.com/klaytn/klaytn/blockchain/vm"
    31  	"github.com/klaytn/klaytn/common"
    32  	"github.com/klaytn/klaytn/crypto"
    33  	"github.com/klaytn/klaytn/params"
    34  	"github.com/klaytn/klaytn/storage/database"
    35  )
    36  
    37  // Config is a basic type specifying certain configuration flags for running
    38  // the EVM.
    39  type Config struct {
    40  	ChainConfig *params.ChainConfig
    41  	BlockScore  *big.Int
    42  	Origin      common.Address
    43  	Coinbase    common.Address
    44  	BlockNumber *big.Int
    45  	Time        *big.Int
    46  	GasLimit    uint64
    47  	GasPrice    *big.Int
    48  	Value       *big.Int
    49  	Debug       bool
    50  	EVMConfig   vm.Config
    51  	BaseFee     *big.Int
    52  
    53  	State     *state.StateDB
    54  	GetHashFn func(n uint64) common.Hash
    55  }
    56  
    57  // sets defaults on the config
    58  func setDefaults(cfg *Config) {
    59  	if cfg.ChainConfig == nil {
    60  		cfg.ChainConfig = &params.ChainConfig{
    61  			ChainID:                  big.NewInt(1),
    62  			IstanbulCompatibleBlock:  new(big.Int),
    63  			LondonCompatibleBlock:    new(big.Int),
    64  			EthTxTypeCompatibleBlock: new(big.Int),
    65  			KoreCompatibleBlock:      new(big.Int),
    66  		}
    67  	}
    68  
    69  	if cfg.BlockScore == nil {
    70  		cfg.BlockScore = new(big.Int)
    71  	}
    72  	if cfg.Time == nil {
    73  		cfg.Time = big.NewInt(time.Now().Unix())
    74  	}
    75  	if cfg.GasLimit == 0 {
    76  		cfg.GasLimit = math.MaxUint64
    77  	}
    78  	if cfg.GasPrice == nil {
    79  		cfg.GasPrice = new(big.Int)
    80  	}
    81  	if cfg.Value == nil {
    82  		cfg.Value = new(big.Int)
    83  	}
    84  	if cfg.BlockNumber == nil {
    85  		cfg.BlockNumber = new(big.Int)
    86  	}
    87  	if cfg.GetHashFn == nil {
    88  		cfg.GetHashFn = func(n uint64) common.Hash {
    89  			return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
    90  		}
    91  	}
    92  	if cfg.BaseFee == nil {
    93  		cfg.BaseFee = new(big.Int).SetUint64(params.ZeroBaseFee)
    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  // Executes sets up a in memory, temporarily, environment for the execution of
   101  // the given code. It makes sure that it's restored to it's 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  		memDBManager := database.NewMemoryDBManager()
   110  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(memDBManager), 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.BlockNumber)
   117  	)
   118  	if rules.IsKore {
   119  		cfg.State.PrepareAccessList(cfg.Origin, common.Address{}, &address, vm.ActivePrecompiles(rules))
   120  	}
   121  	cfg.State.CreateSmartContractAccount(address, params.CodeFormatEVM, cfg.ChainConfig.Rules(cfg.BlockNumber))
   122  	// set the receiver's (the executing contract) code for execution.
   123  	cfg.State.SetCode(address, code)
   124  	// Call the code with the given configuration.
   125  	ret, _, err := vmenv.Call(
   126  		sender,
   127  		common.BytesToAddress([]byte("contract")),
   128  		input,
   129  		cfg.GasLimit,
   130  		cfg.Value,
   131  	)
   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  		memDBManager := database.NewMemoryDBManager()
   145  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(memDBManager), nil)
   146  	}
   147  	var (
   148  		vmenv  = NewEnv(cfg)
   149  		sender = vm.AccountRef(cfg.Origin)
   150  		rules  = cfg.ChainConfig.Rules(vmenv.BlockNumber)
   151  	)
   152  	if rules.IsKore {
   153  		cfg.State.PrepareAccessList(cfg.Origin, common.Address{}, nil, vm.ActivePrecompiles(rules))
   154  	}
   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  		params.CodeFormatEVM,
   162  	)
   163  	return code, address, leftOverGas, err
   164  }
   165  
   166  // Call executes the code given by the contract's address. It will return the
   167  // EVM's return value or an error if it failed.
   168  //
   169  // Call, unlike Execute, requires a config and also requires the State field to
   170  // be set.
   171  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   172  	setDefaults(cfg)
   173  
   174  	var (
   175  		vmenv         = NewEnv(cfg)
   176  		senderAccount = cfg.State.GetOrNewStateObject(cfg.Origin)
   177  		sender        = types.NewAccountRefWithFeePayer(senderAccount.Address(), senderAccount.Address())
   178  		rules         = cfg.ChainConfig.Rules(vmenv.BlockNumber)
   179  	)
   180  	if rules.IsKore {
   181  		cfg.State.PrepareAccessList(cfg.Origin, common.Address{}, &address, vm.ActivePrecompiles(rules))
   182  	}
   183  	// Call the code with the given configuration.
   184  	ret, leftOverGas, err := vmenv.Call(
   185  		sender,
   186  		address,
   187  		input,
   188  		cfg.GasLimit,
   189  		cfg.Value,
   190  	)
   191  
   192  	return ret, leftOverGas, err
   193  }