runtime.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 specifing 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. GetHashFn func(n uint64) common.Hash
  40. }
  41. // sets defaults on the config
  42. func setDefaults(cfg *Config) {
  43. if cfg.Difficulty == nil {
  44. cfg.Difficulty = new(big.Int)
  45. }
  46. if cfg.Time == nil {
  47. cfg.Time = big.NewInt(time.Now().Unix())
  48. }
  49. if cfg.GasLimit == nil {
  50. cfg.GasLimit = new(big.Int).Set(common.MaxBig)
  51. }
  52. if cfg.GasPrice == nil {
  53. cfg.GasPrice = new(big.Int)
  54. }
  55. if cfg.Value == nil {
  56. cfg.Value = new(big.Int)
  57. }
  58. if cfg.BlockNumber == nil {
  59. cfg.BlockNumber = new(big.Int)
  60. }
  61. if cfg.GetHashFn == nil {
  62. cfg.GetHashFn = func(n uint64) common.Hash {
  63. return common.BytesToHash(crypto.Sha3([]byte(new(big.Int).SetUint64(n).String())))
  64. }
  65. }
  66. }
  67. // Execute executes the code using the input as call data during the execution.
  68. // It returns the EVM's return value, the new state and an error if it failed.
  69. //
  70. // Executes sets up a in memory, temporarily, environment for the execution of
  71. // the given code. It enabled the JIT by default and make sure that it's restored
  72. // to it's original state afterwards.
  73. func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
  74. if cfg == nil {
  75. cfg = new(Config)
  76. }
  77. setDefaults(cfg)
  78. // defer the call to setting back the original values
  79. defer func(debug, forceJit, enableJit bool) {
  80. vm.Debug = debug
  81. vm.ForceJit = forceJit
  82. vm.EnableJit = enableJit
  83. }(vm.Debug, vm.ForceJit, vm.EnableJit)
  84. vm.ForceJit = !cfg.DisableJit
  85. vm.EnableJit = !cfg.DisableJit
  86. vm.Debug = cfg.Debug
  87. var (
  88. db, _ = ethdb.NewMemDatabase()
  89. statedb, _ = state.New(common.Hash{}, db)
  90. vmenv = NewEnv(cfg, statedb)
  91. sender = statedb.CreateAccount(cfg.Origin)
  92. receiver = statedb.CreateAccount(common.StringToAddress("contract"))
  93. )
  94. // set the receiver's (the executing contract) code for execution.
  95. receiver.SetCode(code)
  96. // Call the code with the given configuration.
  97. ret, err := vmenv.Call(
  98. sender,
  99. receiver.Address(),
  100. input,
  101. cfg.GasLimit,
  102. cfg.GasPrice,
  103. cfg.Value,
  104. )
  105. if cfg.Debug {
  106. vm.StdErrFormat(vmenv.StructLogs())
  107. }
  108. return ret, statedb, err
  109. }