runtime.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. RuleSet vm.RuleSet
  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. State *state.StateDB
  48. GetHashFn func(n uint64) common.Hash
  49. }
  50. // sets defaults on the config
  51. func setDefaults(cfg *Config) {
  52. if cfg.RuleSet == nil {
  53. cfg.RuleSet = ruleSet{}
  54. }
  55. if cfg.Difficulty == nil {
  56. cfg.Difficulty = new(big.Int)
  57. }
  58. if cfg.Time == nil {
  59. cfg.Time = big.NewInt(time.Now().Unix())
  60. }
  61. if cfg.GasLimit == nil {
  62. cfg.GasLimit = new(big.Int).Set(common.MaxBig)
  63. }
  64. if cfg.GasPrice == nil {
  65. cfg.GasPrice = new(big.Int)
  66. }
  67. if cfg.Value == nil {
  68. cfg.Value = new(big.Int)
  69. }
  70. if cfg.BlockNumber == nil {
  71. cfg.BlockNumber = new(big.Int)
  72. }
  73. if cfg.GetHashFn == nil {
  74. cfg.GetHashFn = func(n uint64) common.Hash {
  75. return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
  76. }
  77. }
  78. }
  79. // Execute executes the code using the input as call data during the execution.
  80. // It returns the EVM's return value, the new state and an error if it failed.
  81. //
  82. // Executes sets up a in memory, temporarily, environment for the execution of
  83. // the given code. It enabled the JIT by default and make sure that it's restored
  84. // to it's original state afterwards.
  85. func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
  86. if cfg == nil {
  87. cfg = new(Config)
  88. }
  89. setDefaults(cfg)
  90. if cfg.State == nil {
  91. db, _ := ethdb.NewMemDatabase()
  92. cfg.State, _ = state.New(common.Hash{}, db)
  93. }
  94. var (
  95. vmenv = NewEnv(cfg, cfg.State)
  96. sender = cfg.State.CreateAccount(cfg.Origin)
  97. receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
  98. )
  99. // set the receiver's (the executing contract) code for execution.
  100. receiver.SetCode(crypto.Keccak256Hash(code), code)
  101. // Call the code with the given configuration.
  102. ret, err := vmenv.Call(
  103. sender,
  104. receiver.Address(),
  105. input,
  106. cfg.GasLimit,
  107. cfg.GasPrice,
  108. cfg.Value,
  109. )
  110. return ret, cfg.State, err
  111. }
  112. // Call executes the code given by the contract's address. It will return the
  113. // EVM's return value or an error if it failed.
  114. //
  115. // Call, unlike Execute, requires a config and also requires the State field to
  116. // be set.
  117. func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
  118. setDefaults(cfg)
  119. vmenv := NewEnv(cfg, cfg.State)
  120. sender := cfg.State.GetOrNewStateObject(cfg.Origin)
  121. // Call the code with the given configuration.
  122. ret, err := vmenv.Call(
  123. sender,
  124. address,
  125. input,
  126. cfg.GasLimit,
  127. cfg.GasPrice,
  128. cfg.Value,
  129. )
  130. return ret, err
  131. }