github.com/core-coin/go-core/v2@v2.1.9/core/vm/runtime/runtime.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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/core-coin/go-core/v2/common"
    25  	"github.com/core-coin/go-core/v2/core/rawdb"
    26  	"github.com/core-coin/go-core/v2/core/state"
    27  	"github.com/core-coin/go-core/v2/core/vm"
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/params"
    30  )
    31  
    32  // Config is a basic type specifying certain configuration flags for running
    33  // the CVM.
    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  	EnergyLimit uint64
    42  	EnergyPrice *big.Int
    43  	Value       *big.Int
    44  	Debug       bool
    45  	CVMConfig   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  			NetworkID: big.NewInt(1),
    56  		}
    57  	}
    58  
    59  	if cfg.Difficulty == nil {
    60  		cfg.Difficulty = new(big.Int)
    61  	}
    62  	if cfg.Time == nil {
    63  		cfg.Time = big.NewInt(time.Now().Unix())
    64  	}
    65  	if cfg.EnergyLimit == 0 {
    66  		cfg.EnergyLimit = math.MaxUint64
    67  	}
    68  	if cfg.EnergyPrice == nil {
    69  		cfg.EnergyPrice = new(big.Int)
    70  	}
    71  	if cfg.Value == nil {
    72  		cfg.Value = new(big.Int)
    73  	}
    74  	if cfg.BlockNumber == nil {
    75  		cfg.BlockNumber = new(big.Int)
    76  	}
    77  	if cfg.GetHashFn == nil {
    78  		cfg.GetHashFn = func(n uint64) common.Hash {
    79  			return common.BytesToHash(crypto.SHA3([]byte(new(big.Int).SetUint64(n).String())))
    80  		}
    81  	}
    82  }
    83  
    84  // Execute executes the code using the input as call data during the execution.
    85  // It returns the CVM's return value, the new state and an error if it failed.
    86  //
    87  // Execute sets up an in-memory, temporary, environment for the execution of
    88  // the given code. It makes sure that it's restored to its original state afterwards.
    89  func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
    90  	if cfg == nil {
    91  		cfg = new(Config)
    92  	}
    93  	setDefaults(cfg)
    94  
    95  	if cfg.State == nil {
    96  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    97  	}
    98  	var (
    99  		address = common.BytesToAddress([]byte("contract"))
   100  		vmenv   = NewEnv(cfg)
   101  		sender  = vm.AccountRef(cfg.Origin)
   102  	)
   103  	cfg.State.CreateAccount(address)
   104  	// set the receiver's (the executing contract) code for execution.
   105  	cfg.State.SetCode(address, code)
   106  	// Call the code with the given configuration.
   107  	ret, _, err := vmenv.Call(
   108  		sender,
   109  		common.BytesToAddress([]byte("contract")),
   110  		input,
   111  		cfg.EnergyLimit,
   112  		cfg.Value,
   113  	)
   114  
   115  	return ret, cfg.State, err
   116  }
   117  
   118  // Create executes the code using the CVM create method
   119  func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
   120  	if cfg == nil {
   121  		cfg = new(Config)
   122  	}
   123  	setDefaults(cfg)
   124  
   125  	if cfg.State == nil {
   126  		cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   127  	}
   128  	var (
   129  		vmenv  = NewEnv(cfg)
   130  		sender = vm.AccountRef(cfg.Origin)
   131  	)
   132  
   133  	// Call the code with the given configuration.
   134  	code, address, leftOverEnergy, err := vmenv.Create(
   135  		sender,
   136  		input,
   137  		cfg.EnergyLimit,
   138  		cfg.Value,
   139  	)
   140  	return code, address, leftOverEnergy, err
   141  }
   142  
   143  // Call executes the code given by the contract's address. It will return the
   144  // CVM's return value or an error if it failed.
   145  //
   146  // Call, unlike Execute, requires a config and also requires the State field to
   147  // be set.
   148  func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
   149  	setDefaults(cfg)
   150  
   151  	vmenv := NewEnv(cfg)
   152  
   153  	sender := cfg.State.GetOrNewStateObject(cfg.Origin)
   154  
   155  	// Call the code with the given configuration.
   156  	ret, leftOverEnergy, err := vmenv.Call(
   157  		sender,
   158  		address,
   159  		input,
   160  		cfg.EnergyLimit,
   161  		cfg.Value,
   162  	)
   163  
   164  	return ret, leftOverEnergy, err
   165  }