runtime.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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"
  19. "math/big"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/core/state"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/params"
  27. )
  28. // Config is a basic type specifying certain configuration flags for running
  29. // the EVM.
  30. type Config struct {
  31. ChainConfig *params.ChainConfig
  32. Difficulty *big.Int
  33. Origin common.Address
  34. Coinbase common.Address
  35. BlockNumber *big.Int
  36. Time *big.Int
  37. GasLimit uint64
  38. GasPrice *big.Int
  39. Value *big.Int
  40. Debug bool
  41. EVMConfig vm.Config
  42. State *state.StateDB
  43. GetHashFn func(n uint64) common.Hash
  44. }
  45. // sets defaults on the config
  46. func setDefaults(cfg *Config) {
  47. if cfg.ChainConfig == nil {
  48. cfg.ChainConfig = &params.ChainConfig{
  49. ChainID: big.NewInt(1),
  50. HomesteadBlock: new(big.Int),
  51. DAOForkBlock: new(big.Int),
  52. DAOForkSupport: false,
  53. EIP150Block: new(big.Int),
  54. EIP150Hash: common.Hash{},
  55. EIP155Block: new(big.Int),
  56. EIP158Block: new(big.Int),
  57. ByzantiumBlock: new(big.Int),
  58. ConstantinopleBlock: new(big.Int),
  59. PetersburgBlock: new(big.Int),
  60. IstanbulBlock: new(big.Int),
  61. MuirGlacierBlock: new(big.Int),
  62. YoloV2Block: nil,
  63. }
  64. }
  65. if cfg.Difficulty == nil {
  66. cfg.Difficulty = new(big.Int)
  67. }
  68. if cfg.Time == nil {
  69. cfg.Time = big.NewInt(time.Now().Unix())
  70. }
  71. if cfg.GasLimit == 0 {
  72. cfg.GasLimit = math.MaxUint64
  73. }
  74. if cfg.GasPrice == nil {
  75. cfg.GasPrice = new(big.Int)
  76. }
  77. if cfg.Value == nil {
  78. cfg.Value = new(big.Int)
  79. }
  80. if cfg.BlockNumber == nil {
  81. cfg.BlockNumber = new(big.Int)
  82. }
  83. if cfg.GetHashFn == nil {
  84. cfg.GetHashFn = func(n uint64) common.Hash {
  85. return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
  86. }
  87. }
  88. }
  89. // Execute executes the code using the input as call data during the execution.
  90. // It returns the EVM's return value, the new state and an error if it failed.
  91. //
  92. // Execute sets up an in-memory, temporary, environment for the execution of
  93. // the given code. It makes sure that it's restored to its 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. cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  101. }
  102. var (
  103. address = common.BytesToAddress([]byte("contract"))
  104. vmenv = NewEnv(cfg)
  105. sender = vm.AccountRef(cfg.Origin)
  106. )
  107. if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
  108. cfg.State.AddAddressToAccessList(cfg.Origin)
  109. cfg.State.AddAddressToAccessList(address)
  110. for _, addr := range vmenv.ActivePrecompiles() {
  111. cfg.State.AddAddressToAccessList(addr)
  112. cfg.State.AddAddressToAccessList(addr)
  113. }
  114. }
  115. cfg.State.CreateAccount(address)
  116. // set the receiver's (the executing contract) code for execution.
  117. cfg.State.SetCode(address, code)
  118. // Call the code with the given configuration.
  119. ret, _, err := vmenv.Call(
  120. sender,
  121. common.BytesToAddress([]byte("contract")),
  122. input,
  123. cfg.GasLimit,
  124. cfg.Value,
  125. )
  126. return ret, cfg.State, err
  127. }
  128. // Create executes the code using the EVM create method
  129. func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
  130. if cfg == nil {
  131. cfg = new(Config)
  132. }
  133. setDefaults(cfg)
  134. if cfg.State == nil {
  135. cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  136. }
  137. var (
  138. vmenv = NewEnv(cfg)
  139. sender = vm.AccountRef(cfg.Origin)
  140. )
  141. if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
  142. cfg.State.AddAddressToAccessList(cfg.Origin)
  143. for _, addr := range vmenv.ActivePrecompiles() {
  144. cfg.State.AddAddressToAccessList(addr)
  145. }
  146. }
  147. // Call the code with the given configuration.
  148. code, address, leftOverGas, err := vmenv.Create(
  149. sender,
  150. input,
  151. cfg.GasLimit,
  152. cfg.Value,
  153. )
  154. return code, address, leftOverGas, err
  155. }
  156. // Call executes the code given by the contract's address. It will return the
  157. // EVM's return value or an error if it failed.
  158. //
  159. // Call, unlike Execute, requires a config and also requires the State field to
  160. // be set.
  161. func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
  162. setDefaults(cfg)
  163. vmenv := NewEnv(cfg)
  164. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  165. if cfg.ChainConfig.IsYoloV2(vmenv.Context.BlockNumber) {
  166. cfg.State.AddAddressToAccessList(cfg.Origin)
  167. cfg.State.AddAddressToAccessList(address)
  168. for _, addr := range vmenv.ActivePrecompiles() {
  169. cfg.State.AddAddressToAccessList(addr)
  170. }
  171. }
  172. // Call the code with the given configuration.
  173. ret, leftOverGas, err := vmenv.Call(
  174. sender,
  175. address,
  176. input,
  177. cfg.GasLimit,
  178. cfg.Value,
  179. )
  180. return ret, leftOverGas, err
  181. }