runtime.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/core/vm"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // The default, always homestead, rule set for the vm env
  28. type ruleSet struct{}
  29. func (ruleSet) IsHomestead(*big.Int) bool { return true }
  30. func (ruleSet) GasTable(*big.Int) params.GasTable {
  31. return params.GasTableHomesteadGasRepriceFork
  32. }
  33. // Config is a basic type specifying certain configuration flags for running
  34. // the EVM.
  35. type Config struct {
  36. ChainConfig *params.ChainConfig
  37. Difficulty *big.Int
  38. Origin common.Address
  39. Coinbase common.Address
  40. BlockNumber *big.Int
  41. Time *big.Int
  42. GasLimit *big.Int
  43. GasPrice *big.Int
  44. Value *big.Int
  45. DisableJit bool // "disable" so it's enabled by default
  46. Debug bool
  47. EVMConfig vm.Config
  48. State *state.StateDB
  49. GetHashFn func(n uint64) common.Hash
  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. EIP155Block: new(big.Int),
  61. EIP158Block: new(big.Int),
  62. }
  63. }
  64. if cfg.Difficulty == nil {
  65. cfg.Difficulty = new(big.Int)
  66. }
  67. if cfg.Time == nil {
  68. cfg.Time = big.NewInt(time.Now().Unix())
  69. }
  70. if cfg.GasLimit == nil {
  71. cfg.GasLimit = new(big.Int).Set(common.MaxBig)
  72. }
  73. if cfg.GasPrice == nil {
  74. cfg.GasPrice = new(big.Int)
  75. }
  76. if cfg.Value == nil {
  77. cfg.Value = new(big.Int)
  78. }
  79. if cfg.BlockNumber == nil {
  80. cfg.BlockNumber = new(big.Int)
  81. }
  82. if cfg.GetHashFn == nil {
  83. cfg.GetHashFn = func(n uint64) common.Hash {
  84. return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
  85. }
  86. }
  87. }
  88. // Execute executes the code using the input as call data during the execution.
  89. // It returns the EVM's return value, the new state and an error if it failed.
  90. //
  91. // Executes sets up a in memory, temporarily, environment for the execution of
  92. // the given code. It enabled the JIT by default and make sure that it's restored
  93. // to it's original state afterwards.
  94. func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
  95. if cfg == nil {
  96. cfg = new(Config)
  97. }
  98. setDefaults(cfg)
  99. if cfg.State == nil {
  100. db, _ := ethdb.NewMemDatabase()
  101. cfg.State, _ = state.New(common.Hash{}, db)
  102. }
  103. var (
  104. vmenv = NewEnv(cfg, cfg.State)
  105. sender = cfg.State.CreateAccount(cfg.Origin)
  106. receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
  107. )
  108. // set the receiver's (the executing contract) code for execution.
  109. receiver.SetCode(crypto.Keccak256Hash(code), code)
  110. // Call the code with the given configuration.
  111. ret, err := vmenv.Call(
  112. sender,
  113. receiver.Address(),
  114. input,
  115. cfg.GasLimit,
  116. cfg.Value,
  117. )
  118. return ret, cfg.State, err
  119. }
  120. // Create executes the code using the EVM create method
  121. func Create(input []byte, cfg *Config) ([]byte, common.Address, error) {
  122. if cfg == nil {
  123. cfg = new(Config)
  124. }
  125. setDefaults(cfg)
  126. if cfg.State == nil {
  127. db, _ := ethdb.NewMemDatabase()
  128. cfg.State, _ = state.New(common.Hash{}, db)
  129. }
  130. var (
  131. vmenv = NewEnv(cfg, cfg.State)
  132. sender = cfg.State.CreateAccount(cfg.Origin)
  133. )
  134. // Call the code with the given configuration.
  135. return vmenv.Create(
  136. sender,
  137. input,
  138. cfg.GasLimit,
  139. cfg.Value,
  140. )
  141. }
  142. // Call executes the code given by the contract's address. It will return the
  143. // EVM's return value or an error if it failed.
  144. //
  145. // Call, unlike Execute, requires a config and also requires the State field to
  146. // be set.
  147. func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
  148. setDefaults(cfg)
  149. vmenv := NewEnv(cfg, cfg.State)
  150. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  151. // Call the code with the given configuration.
  152. ret, err := vmenv.Call(
  153. sender,
  154. address,
  155. input,
  156. cfg.GasLimit,
  157. cfg.Value,
  158. )
  159. return ret, err
  160. }