runtime.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. package runtime
  17. import (
  18. "math/big"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/params"
  25. )
  26. // The default, always homestead, rule set for the vm env
  27. type ruleSet struct{}
  28. func (ruleSet) IsHomestead(*big.Int) bool { return true }
  29. func (ruleSet) GasTable(*big.Int) params.GasTable {
  30. return params.GasTableHomesteadGasRepriceFork
  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 *big.Int
  42. GasPrice *big.Int
  43. Value *big.Int
  44. DisableJit bool // "disable" so it's enabled by default
  45. Debug bool
  46. State *state.StateDB
  47. GetHashFn func(n uint64) common.Hash
  48. }
  49. // sets defaults on the config
  50. func setDefaults(cfg *Config) {
  51. if cfg.ChainConfig == nil {
  52. cfg.ChainConfig = &params.ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), false, new(big.Int), common.Hash{}, new(big.Int), new(big.Int), new(big.Int)}
  53. }
  54. if cfg.Difficulty == nil {
  55. cfg.Difficulty = new(big.Int)
  56. }
  57. if cfg.Time == nil {
  58. cfg.Time = big.NewInt(time.Now().Unix())
  59. }
  60. if cfg.GasLimit == nil {
  61. cfg.GasLimit = new(big.Int).Set(common.MaxBig)
  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. }
  78. // Execute executes the code using the input as call data during the execution.
  79. // It returns the EVM's return value, the new state and an error if it failed.
  80. //
  81. // Executes sets up a in memory, temporarily, environment for the execution of
  82. // the given code. It enabled the JIT by default and make sure that it's restored
  83. // to it's original state afterwards.
  84. func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
  85. if cfg == nil {
  86. cfg = new(Config)
  87. }
  88. setDefaults(cfg)
  89. if cfg.State == nil {
  90. db, _ := ethdb.NewMemDatabase()
  91. cfg.State, _ = state.New(common.Hash{}, db)
  92. }
  93. var (
  94. vmenv = NewEnv(cfg, cfg.State)
  95. sender = cfg.State.CreateAccount(cfg.Origin)
  96. receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
  97. )
  98. // set the receiver's (the executing contract) code for execution.
  99. receiver.SetCode(crypto.Keccak256Hash(code), code)
  100. // Call the code with the given configuration.
  101. ret, err := vmenv.Call(
  102. sender,
  103. receiver.Address(),
  104. input,
  105. cfg.GasLimit,
  106. cfg.GasPrice,
  107. cfg.Value,
  108. )
  109. return ret, cfg.State, err
  110. }
  111. // Call executes the code given by the contract's address. It will return the
  112. // EVM's return value or an error if it failed.
  113. //
  114. // Call, unlike Execute, requires a config and also requires the State field to
  115. // be set.
  116. func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
  117. setDefaults(cfg)
  118. vmenv := NewEnv(cfg, cfg.State)
  119. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  120. // Call the code with the given configuration.
  121. ret, err := vmenv.Call(
  122. sender,
  123. address,
  124. input,
  125. cfg.GasLimit,
  126. cfg.GasPrice,
  127. cfg.Value,
  128. )
  129. return ret, err
  130. }