util.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 tests
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "os"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. )
  31. var (
  32. ForceJit bool
  33. EnableJit bool
  34. )
  35. func init() {
  36. glog.SetV(0)
  37. if os.Getenv("JITVM") == "true" {
  38. ForceJit = true
  39. EnableJit = true
  40. }
  41. }
  42. func checkLogs(tlog []Log, logs vm.Logs) error {
  43. if len(tlog) != len(logs) {
  44. return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
  45. } else {
  46. for i, log := range tlog {
  47. if common.HexToAddress(log.AddressF) != logs[i].Address {
  48. return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
  49. }
  50. if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
  51. return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
  52. }
  53. if len(log.TopicsF) != len(logs[i].Topics) {
  54. return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
  55. } else {
  56. for j, topic := range log.TopicsF {
  57. if common.HexToHash(topic) != logs[i].Topics[j] {
  58. return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
  59. }
  60. }
  61. }
  62. genBloom := common.LeftPadBytes(types.LogsBloom(vm.Logs{logs[i]}).Bytes(), 256)
  63. if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
  64. return fmt.Errorf("bloom mismatch")
  65. }
  66. }
  67. }
  68. return nil
  69. }
  70. type Account struct {
  71. Balance string
  72. Code string
  73. Nonce string
  74. Storage map[string]string
  75. }
  76. type Log struct {
  77. AddressF string `json:"address"`
  78. DataF string `json:"data"`
  79. TopicsF []string `json:"topics"`
  80. BloomF string `json:"bloom"`
  81. }
  82. func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
  83. func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
  84. func (self Log) RlpData() interface{} { return nil }
  85. func (self Log) Topics() [][]byte {
  86. t := make([][]byte, len(self.TopicsF))
  87. for i, topic := range self.TopicsF {
  88. t[i] = common.Hex2Bytes(topic)
  89. }
  90. return t
  91. }
  92. func makePreState(db ethdb.Database, accounts map[string]Account) *state.StateDB {
  93. statedb, _ := state.New(common.Hash{}, db)
  94. for addr, account := range accounts {
  95. insertAccount(statedb, addr, account)
  96. }
  97. return statedb
  98. }
  99. func insertAccount(state *state.StateDB, saddr string, account Account) {
  100. if common.IsHex(account.Code) {
  101. account.Code = account.Code[2:]
  102. }
  103. addr := common.HexToAddress(saddr)
  104. state.SetCode(addr, common.Hex2Bytes(account.Code))
  105. state.SetNonce(addr, common.Big(account.Nonce).Uint64())
  106. state.SetBalance(addr, common.Big(account.Balance))
  107. for a, v := range account.Storage {
  108. state.SetState(addr, common.HexToHash(a), common.HexToHash(v))
  109. }
  110. }
  111. type VmEnv struct {
  112. CurrentCoinbase string
  113. CurrentDifficulty string
  114. CurrentGasLimit string
  115. CurrentNumber string
  116. CurrentTimestamp interface{}
  117. PreviousHash string
  118. }
  119. type VmTest struct {
  120. Callcreates interface{}
  121. //Env map[string]string
  122. Env VmEnv
  123. Exec map[string]string
  124. Transaction map[string]string
  125. Logs []Log
  126. Gas string
  127. Out string
  128. Post map[string]Account
  129. Pre map[string]Account
  130. PostStateRoot string
  131. }
  132. type RuleSet struct {
  133. HomesteadBlock *big.Int
  134. DAOForkBlock *big.Int
  135. DAOForkSupport bool
  136. }
  137. func (r RuleSet) IsHomestead(n *big.Int) bool {
  138. return n.Cmp(r.HomesteadBlock) >= 0
  139. }
  140. type Env struct {
  141. ruleSet RuleSet
  142. depth int
  143. state *state.StateDB
  144. skipTransfer bool
  145. initial bool
  146. Gas *big.Int
  147. origin common.Address
  148. parent common.Hash
  149. coinbase common.Address
  150. number *big.Int
  151. time *big.Int
  152. difficulty *big.Int
  153. gasLimit *big.Int
  154. vmTest bool
  155. evm *vm.EVM
  156. }
  157. func NewEnv(ruleSet RuleSet, state *state.StateDB) *Env {
  158. env := &Env{
  159. ruleSet: ruleSet,
  160. state: state,
  161. }
  162. return env
  163. }
  164. func NewEnvFromMap(ruleSet RuleSet, state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env {
  165. env := NewEnv(ruleSet, state)
  166. env.origin = common.HexToAddress(exeValues["caller"])
  167. env.parent = common.HexToHash(envValues["previousHash"])
  168. env.coinbase = common.HexToAddress(envValues["currentCoinbase"])
  169. env.number = common.Big(envValues["currentNumber"])
  170. env.time = common.Big(envValues["currentTimestamp"])
  171. env.difficulty = common.Big(envValues["currentDifficulty"])
  172. env.gasLimit = common.Big(envValues["currentGasLimit"])
  173. env.Gas = new(big.Int)
  174. env.evm = vm.New(env, vm.Config{
  175. EnableJit: EnableJit,
  176. ForceJit: ForceJit,
  177. })
  178. return env
  179. }
  180. func (self *Env) RuleSet() vm.RuleSet { return self.ruleSet }
  181. func (self *Env) Vm() vm.Vm { return self.evm }
  182. func (self *Env) Origin() common.Address { return self.origin }
  183. func (self *Env) BlockNumber() *big.Int { return self.number }
  184. func (self *Env) Coinbase() common.Address { return self.coinbase }
  185. func (self *Env) Time() *big.Int { return self.time }
  186. func (self *Env) Difficulty() *big.Int { return self.difficulty }
  187. func (self *Env) Db() vm.Database { return self.state }
  188. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  189. func (self *Env) VmType() vm.Type { return vm.StdVmTy }
  190. func (self *Env) GetHash(n uint64) common.Hash {
  191. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  192. }
  193. func (self *Env) AddLog(log *vm.Log) {
  194. self.state.AddLog(log)
  195. }
  196. func (self *Env) Depth() int { return self.depth }
  197. func (self *Env) SetDepth(i int) { self.depth = i }
  198. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  199. if self.skipTransfer {
  200. if self.initial {
  201. self.initial = false
  202. return true
  203. }
  204. }
  205. return self.state.GetBalance(from).Cmp(balance) >= 0
  206. }
  207. func (self *Env) SnapshotDatabase() int {
  208. return self.state.Snapshot()
  209. }
  210. func (self *Env) RevertToSnapshot(snapshot int) {
  211. self.state.RevertToSnapshot(snapshot)
  212. }
  213. func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {
  214. if self.skipTransfer {
  215. return
  216. }
  217. core.Transfer(from, to, amount)
  218. }
  219. func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  220. if self.vmTest && self.depth > 0 {
  221. caller.ReturnGas(gas, price)
  222. return nil, nil
  223. }
  224. ret, err := core.Call(self, caller, addr, data, gas, price, value)
  225. self.Gas = gas
  226. return ret, err
  227. }
  228. func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  229. if self.vmTest && self.depth > 0 {
  230. caller.ReturnGas(gas, price)
  231. return nil, nil
  232. }
  233. return core.CallCode(self, caller, addr, data, gas, price, value)
  234. }
  235. func (self *Env) DelegateCall(caller vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  236. if self.vmTest && self.depth > 0 {
  237. caller.ReturnGas(gas, price)
  238. return nil, nil
  239. }
  240. return core.DelegateCall(self, caller, addr, data, gas, price)
  241. }
  242. func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  243. if self.vmTest {
  244. caller.ReturnGas(gas, price)
  245. nonce := self.state.GetNonce(caller.Address())
  246. obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce))
  247. return nil, obj.Address(), nil
  248. } else {
  249. return core.Create(self, caller, data, gas, price, value)
  250. }
  251. }
  252. type Message struct {
  253. from common.Address
  254. to *common.Address
  255. value, gas, price *big.Int
  256. data []byte
  257. nonce uint64
  258. }
  259. func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message {
  260. return Message{from, to, value, gas, price, data, nonce}
  261. }
  262. func (self Message) Hash() []byte { return nil }
  263. func (self Message) From() (common.Address, error) { return self.from, nil }
  264. func (self Message) FromFrontier() (common.Address, error) { return self.from, nil }
  265. func (self Message) To() *common.Address { return self.to }
  266. func (self Message) GasPrice() *big.Int { return self.price }
  267. func (self Message) Gas() *big.Int { return self.gas }
  268. func (self Message) Value() *big.Int { return self.value }
  269. func (self Message) Nonce() uint64 { return self.nonce }
  270. func (self Message) CheckNonce() bool { return true }
  271. func (self Message) Data() []byte { return self.data }