runtime.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2014 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. )
  26. // Config is a basic type specifying certain configuration flags for running
  27. // the EVM.
  28. type Config struct {
  29. Difficulty *big.Int
  30. Origin common.Address
  31. Coinbase common.Address
  32. BlockNumber *big.Int
  33. Time *big.Int
  34. GasLimit *big.Int
  35. GasPrice *big.Int
  36. Value *big.Int
  37. DisableJit bool // "disable" so it's enabled by default
  38. Debug bool
  39. State *state.StateDB
  40. GetHashFn func(n uint64) common.Hash
  41. }
  42. // sets defaults on the config
  43. func setDefaults(cfg *Config) {
  44. if cfg.Difficulty == nil {
  45. cfg.Difficulty = new(big.Int)
  46. }
  47. if cfg.Time == nil {
  48. cfg.Time = big.NewInt(time.Now().Unix())
  49. }
  50. if cfg.GasLimit == nil {
  51. cfg.GasLimit = new(big.Int).Set(common.MaxBig)
  52. }
  53. if cfg.GasPrice == nil {
  54. cfg.GasPrice = new(big.Int)
  55. }
  56. if cfg.Value == nil {
  57. cfg.Value = new(big.Int)
  58. }
  59. if cfg.BlockNumber == nil {
  60. cfg.BlockNumber = new(big.Int)
  61. }
  62. if cfg.GetHashFn == nil {
  63. cfg.GetHashFn = func(n uint64) common.Hash {
  64. return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
  65. }
  66. }
  67. }
  68. // Execute executes the code using the input as call data during the execution.
  69. // It returns the EVM's return value, the new state and an error if it failed.
  70. //
  71. // Executes sets up a in memory, temporarily, environment for the execution of
  72. // the given code. It enabled the JIT by default and make sure that it's restored
  73. // to it's original state afterwards.
  74. func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
  75. if cfg == nil {
  76. cfg = new(Config)
  77. }
  78. setDefaults(cfg)
  79. // defer the call to setting back the original values
  80. defer func(debug, forceJit, enableJit bool) {
  81. vm.Debug = debug
  82. vm.ForceJit = forceJit
  83. vm.EnableJit = enableJit
  84. }(vm.Debug, vm.ForceJit, vm.EnableJit)
  85. vm.ForceJit = !cfg.DisableJit
  86. vm.EnableJit = !cfg.DisableJit
  87. vm.Debug = cfg.Debug
  88. if cfg.State == nil {
  89. db, _ := ethdb.NewMemDatabase()
  90. cfg.State, _ = state.New(common.Hash{}, db)
  91. }
  92. var (
  93. vmenv = NewEnv(cfg, cfg.State)
  94. sender = cfg.State.CreateAccount(cfg.Origin)
  95. receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
  96. )
  97. // set the receiver's (the executing contract) code for execution.
  98. receiver.SetCode(code)
  99. // Call the code with the given configuration.
  100. ret, err := vmenv.Call(
  101. sender,
  102. receiver.Address(),
  103. input,
  104. cfg.GasLimit,
  105. cfg.GasPrice,
  106. cfg.Value,
  107. )
  108. if cfg.Debug {
  109. vm.StdErrFormat(vmenv.StructLogs())
  110. }
  111. return ret, cfg.State, err
  112. }
  113. // Call executes the code given by the contract's address. It will return the
  114. // EVM's return value or an error if it failed.
  115. //
  116. // Call, unlike Execute, requires a config and also requires the State field to
  117. // be set.
  118. func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
  119. setDefaults(cfg)
  120. // defer the call to setting back the original values
  121. defer func(debug, forceJit, enableJit bool) {
  122. vm.Debug = debug
  123. vm.ForceJit = forceJit
  124. vm.EnableJit = enableJit
  125. }(vm.Debug, vm.ForceJit, vm.EnableJit)
  126. vm.ForceJit = !cfg.DisableJit
  127. vm.EnableJit = !cfg.DisableJit
  128. vm.Debug = cfg.Debug
  129. vmenv := NewEnv(cfg, cfg.State)
  130. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  131. // Call the code with the given configuration.
  132. ret, err := vmenv.Call(
  133. sender,
  134. address,
  135. input,
  136. cfg.GasLimit,
  137. cfg.GasPrice,
  138. cfg.Value,
  139. )
  140. if cfg.Debug {
  141. vm.StdErrFormat(vmenv.StructLogs())
  142. }
  143. return ret, err
  144. }