main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors:
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "bytes"
  21. "encoding/json"
  22. "fmt"
  23. "io"
  24. "io/ioutil"
  25. "log"
  26. "math/big"
  27. "os"
  28. "strconv"
  29. "strings"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/ethutil"
  32. "github.com/ethereum/go-ethereum/logger"
  33. "github.com/ethereum/go-ethereum/state"
  34. "github.com/ethereum/go-ethereum/tests/helper"
  35. "github.com/ethereum/go-ethereum/vm"
  36. )
  37. type Log struct {
  38. AddressF string `json:"address"`
  39. DataF string `json:"data"`
  40. TopicsF []string `json:"topics"`
  41. BloomF string `json:"bloom"`
  42. }
  43. func (self Log) Address() []byte { return ethutil.Hex2Bytes(self.AddressF) }
  44. func (self Log) Data() []byte { return ethutil.Hex2Bytes(self.DataF) }
  45. func (self Log) RlpData() interface{} { return nil }
  46. func (self Log) Topics() [][]byte {
  47. t := make([][]byte, len(self.TopicsF))
  48. for i, topic := range self.TopicsF {
  49. t[i] = ethutil.Hex2Bytes(topic)
  50. }
  51. return t
  52. }
  53. type Account struct {
  54. Balance string
  55. Code string
  56. Nonce string
  57. Storage map[string]string
  58. }
  59. func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *state.StateObject {
  60. obj := state.NewStateObject(ethutil.Hex2Bytes(addr), db)
  61. obj.SetBalance(ethutil.Big(account.Balance))
  62. if ethutil.IsHex(account.Code) {
  63. account.Code = account.Code[2:]
  64. }
  65. obj.SetCode(ethutil.Hex2Bytes(account.Code))
  66. obj.SetNonce(ethutil.Big(account.Nonce).Uint64())
  67. return obj
  68. }
  69. type VmTest struct {
  70. Callcreates interface{}
  71. //Env map[string]string
  72. Env Env
  73. Exec map[string]string
  74. Transaction map[string]string
  75. Logs []Log
  76. Gas string
  77. Out string
  78. Post map[string]Account
  79. Pre map[string]Account
  80. PostStateRoot string
  81. }
  82. type Env struct {
  83. CurrentCoinbase string
  84. CurrentDifficulty string
  85. CurrentGasLimit string
  86. CurrentNumber string
  87. CurrentTimestamp interface{}
  88. PreviousHash string
  89. }
  90. func RunVmTest(r io.Reader) (failed int) {
  91. tests := make(map[string]VmTest)
  92. data, _ := ioutil.ReadAll(r)
  93. err := json.Unmarshal(data, &tests)
  94. if err != nil {
  95. log.Fatalln(err)
  96. }
  97. for name, test := range tests {
  98. db, _ := ethdb.NewMemDatabase()
  99. statedb := state.New(nil, db)
  100. for addr, account := range test.Pre {
  101. obj := StateObjectFromAccount(db, addr, account)
  102. statedb.SetStateObject(obj)
  103. }
  104. env := make(map[string]string)
  105. env["currentCoinbase"] = test.Env.CurrentCoinbase
  106. env["currentDifficulty"] = test.Env.CurrentDifficulty
  107. env["currentGasLimit"] = test.Env.CurrentGasLimit
  108. env["currentNumber"] = test.Env.CurrentNumber
  109. env["previousHash"] = test.Env.PreviousHash
  110. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  111. env["currentTimestamp"] = strconv.Itoa(int(n))
  112. } else {
  113. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  114. }
  115. ret, logs, _, _ := helper.RunState(statedb, env, test.Transaction)
  116. statedb.Sync()
  117. rexp := helper.FromHex(test.Out)
  118. if bytes.Compare(rexp, ret) != 0 {
  119. fmt.Printf("FAIL: %s's return failed. Expected %x, got %x\n", name, rexp, ret)
  120. failed = 1
  121. }
  122. for addr, account := range test.Post {
  123. obj := statedb.GetStateObject(helper.FromHex(addr))
  124. if obj == nil {
  125. continue
  126. }
  127. if len(test.Exec) == 0 {
  128. if obj.Balance().Cmp(ethutil.Big(account.Balance)) != 0 {
  129. fmt.Printf("FAIL: %s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(ethutil.Big(account.Balance), obj.Balance()))
  130. failed = 1
  131. }
  132. }
  133. for addr, value := range account.Storage {
  134. v := obj.GetState(helper.FromHex(addr)).Bytes()
  135. vexp := helper.FromHex(value)
  136. if bytes.Compare(v, vexp) != 0 {
  137. fmt.Printf("FAIL: %s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, ethutil.BigD(vexp), ethutil.BigD(v))
  138. failed = 1
  139. }
  140. }
  141. }
  142. if !bytes.Equal(ethutil.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
  143. fmt.Printf("FAIL: %s's : Post state root error. Expected %s, got %x\n", name, test.PostStateRoot, statedb.Root())
  144. failed = 1
  145. }
  146. if len(test.Logs) > 0 {
  147. if len(test.Logs) != len(logs) {
  148. fmt.Printf("FAIL: log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
  149. failed = 1
  150. } else {
  151. /*
  152. fmt.Println("A", test.Logs)
  153. fmt.Println("B", logs)
  154. for i, log := range test.Logs {
  155. genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
  156. if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) {
  157. t.Errorf("bloom mismatch")
  158. }
  159. }
  160. */
  161. }
  162. }
  163. logger.Flush()
  164. }
  165. return
  166. }
  167. func main() {
  168. helper.Logger.SetLogLevel(5)
  169. vm.Debug = true
  170. if len(os.Args) > 1 {
  171. os.Exit(RunVmTest(strings.NewReader(os.Args[1])))
  172. } else {
  173. os.Exit(RunVmTest(os.Stdin))
  174. }
  175. }