runtime.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/state"
  23. "github.com/ethereum/go-ethereum/core/vm"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb"
  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. DisableJit bool // "disable" so it's enabled by default
  41. Debug bool
  42. EVMConfig vm.Config
  43. State *state.StateDB
  44. GetHashFn func(n uint64) common.Hash
  45. }
  46. // sets defaults on the config
  47. func setDefaults(cfg *Config) {
  48. if cfg.ChainConfig == nil {
  49. cfg.ChainConfig = &params.ChainConfig{
  50. ChainId: big.NewInt(1),
  51. HomesteadBlock: new(big.Int),
  52. DAOForkBlock: new(big.Int),
  53. DAOForkSupport: false,
  54. EIP150Block: new(big.Int),
  55. EIP155Block: new(big.Int),
  56. EIP158Block: new(big.Int),
  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.GasLimit == 0 {
  66. cfg.GasLimit = math.MaxUint64
  67. }
  68. if cfg.GasPrice == nil {
  69. cfg.GasPrice = 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.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
  80. }
  81. }
  82. }
  83. // Execute executes the code using the input as call data during the execution.
  84. // It returns the EVM's return value, the new state and an error if it failed.
  85. //
  86. // Executes sets up a in memory, temporarily, environment for the execution of
  87. // the given code. It enabled the JIT by default and make sure that it's restored
  88. // to it's 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. if cfg.State == nil {
  95. db, _ := ethdb.NewMemDatabase()
  96. cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(db))
  97. }
  98. var (
  99. address = common.StringToAddress("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.StringToAddress("contract"),
  110. input,
  111. cfg.GasLimit,
  112. cfg.Value,
  113. )
  114. return ret, cfg.State, err
  115. }
  116. // Create executes the code using the EVM create method
  117. func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
  118. if cfg == nil {
  119. cfg = new(Config)
  120. }
  121. setDefaults(cfg)
  122. if cfg.State == nil {
  123. db, _ := ethdb.NewMemDatabase()
  124. cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(db))
  125. }
  126. var (
  127. vmenv = NewEnv(cfg)
  128. sender = vm.AccountRef(cfg.Origin)
  129. )
  130. // Call the code with the given configuration.
  131. code, address, leftOverGas, err := vmenv.Create(
  132. sender,
  133. input,
  134. cfg.GasLimit,
  135. cfg.Value,
  136. )
  137. return code, address, leftOverGas, err
  138. }
  139. // Call executes the code given by the contract's address. It will return the
  140. // EVM's return value or an error if it failed.
  141. //
  142. // Call, unlike Execute, requires a config and also requires the State field to
  143. // be set.
  144. func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) {
  145. setDefaults(cfg)
  146. vmenv := NewEnv(cfg)
  147. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  148. // Call the code with the given configuration.
  149. ret, leftOverGas, err := vmenv.Call(
  150. sender,
  151. address,
  152. input,
  153. cfg.GasLimit,
  154. cfg.Value,
  155. )
  156. return ret, leftOverGas, err
  157. }