gh_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package vm
  2. import (
  3. "bytes"
  4. "math/big"
  5. "strconv"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/ethdb"
  8. "github.com/ethereum/go-ethereum/ethutil"
  9. "github.com/ethereum/go-ethereum/logger"
  10. "github.com/ethereum/go-ethereum/state"
  11. "github.com/ethereum/go-ethereum/tests/helper"
  12. )
  13. type Account struct {
  14. Balance string
  15. Code string
  16. Nonce string
  17. Storage map[string]string
  18. }
  19. type Log struct {
  20. AddressF string `json:"address"`
  21. DataF string `json:"data"`
  22. TopicsF []string `json:"topics"`
  23. BloomF string `json:"bloom"`
  24. }
  25. func (self Log) Address() []byte { return ethutil.Hex2Bytes(self.AddressF) }
  26. func (self Log) Data() []byte { return ethutil.Hex2Bytes(self.DataF) }
  27. func (self Log) RlpData() interface{} { return nil }
  28. func (self Log) Topics() [][]byte {
  29. t := make([][]byte, len(self.TopicsF))
  30. for i, topic := range self.TopicsF {
  31. t[i] = ethutil.Hex2Bytes(topic)
  32. }
  33. return t
  34. }
  35. func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *state.StateObject {
  36. obj := state.NewStateObject(ethutil.Hex2Bytes(addr), db)
  37. obj.SetBalance(ethutil.Big(account.Balance))
  38. if ethutil.IsHex(account.Code) {
  39. account.Code = account.Code[2:]
  40. }
  41. obj.SetCode(ethutil.Hex2Bytes(account.Code))
  42. obj.SetNonce(ethutil.Big(account.Nonce).Uint64())
  43. return obj
  44. }
  45. type Env struct {
  46. CurrentCoinbase string
  47. CurrentDifficulty string
  48. CurrentGasLimit string
  49. CurrentNumber string
  50. CurrentTimestamp interface{}
  51. PreviousHash string
  52. }
  53. type VmTest struct {
  54. Callcreates interface{}
  55. //Env map[string]string
  56. Env Env
  57. Exec map[string]string
  58. Transaction map[string]string
  59. Logs []Log
  60. Gas string
  61. Out string
  62. Post map[string]Account
  63. Pre map[string]Account
  64. PostStateRoot string
  65. }
  66. func RunVmTest(p string, t *testing.T) {
  67. tests := make(map[string]VmTest)
  68. helper.CreateFileTests(t, p, &tests)
  69. for name, test := range tests {
  70. db, _ := ethdb.NewMemDatabase()
  71. statedb := state.New(nil, db)
  72. for addr, account := range test.Pre {
  73. obj := StateObjectFromAccount(db, addr, account)
  74. statedb.SetStateObject(obj)
  75. for a, v := range account.Storage {
  76. obj.SetState(helper.FromHex(a), ethutil.NewValue(helper.FromHex(v)))
  77. }
  78. }
  79. // XXX Yeah, yeah...
  80. env := make(map[string]string)
  81. env["currentCoinbase"] = test.Env.CurrentCoinbase
  82. env["currentDifficulty"] = test.Env.CurrentDifficulty
  83. env["currentGasLimit"] = test.Env.CurrentGasLimit
  84. env["currentNumber"] = test.Env.CurrentNumber
  85. env["previousHash"] = test.Env.PreviousHash
  86. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  87. env["currentTimestamp"] = strconv.Itoa(int(n))
  88. } else {
  89. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  90. }
  91. var (
  92. ret []byte
  93. gas *big.Int
  94. err error
  95. logs state.Logs
  96. )
  97. isVmTest := len(test.Exec) > 0
  98. if isVmTest {
  99. ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec)
  100. } else {
  101. ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction)
  102. }
  103. statedb.Sync()
  104. rexp := helper.FromHex(test.Out)
  105. if bytes.Compare(rexp, ret) != 0 {
  106. t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
  107. }
  108. if isVmTest {
  109. if len(test.Gas) == 0 && err == nil {
  110. t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
  111. } else {
  112. gexp := ethutil.Big(test.Gas)
  113. if gexp.Cmp(gas) != 0 {
  114. t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
  115. }
  116. }
  117. }
  118. for addr, account := range test.Post {
  119. obj := statedb.GetStateObject(helper.FromHex(addr))
  120. if obj == nil {
  121. continue
  122. }
  123. if len(test.Exec) == 0 {
  124. if obj.Balance().Cmp(ethutil.Big(account.Balance)) != 0 {
  125. t.Errorf("%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()))
  126. }
  127. }
  128. for addr, value := range account.Storage {
  129. v := obj.GetState(helper.FromHex(addr)).Bytes()
  130. vexp := helper.FromHex(value)
  131. if bytes.Compare(v, vexp) != 0 {
  132. t.Errorf("%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))
  133. }
  134. }
  135. }
  136. if !isVmTest {
  137. if !bytes.Equal(ethutil.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
  138. t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
  139. }
  140. }
  141. if len(test.Logs) > 0 {
  142. if len(test.Logs) != len(logs) {
  143. t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
  144. } else {
  145. /*
  146. fmt.Println("A", test.Logs)
  147. fmt.Println("B", logs)
  148. for i, log := range test.Logs {
  149. genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
  150. if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) {
  151. t.Errorf("bloom mismatch")
  152. }
  153. }
  154. */
  155. }
  156. }
  157. }
  158. logger.Flush()
  159. }
  160. // I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
  161. func TestVMArithmetic(t *testing.T) {
  162. const fn = "../files/VMTests/vmArithmeticTest.json"
  163. RunVmTest(fn, t)
  164. }
  165. func TestBitwiseLogicOperation(t *testing.T) {
  166. const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json"
  167. RunVmTest(fn, t)
  168. }
  169. func TestBlockInfo(t *testing.T) {
  170. const fn = "../files/VMTests/vmBlockInfoTest.json"
  171. RunVmTest(fn, t)
  172. }
  173. func TestEnvironmentalInfo(t *testing.T) {
  174. const fn = "../files/VMTests/vmEnvironmentalInfoTest.json"
  175. RunVmTest(fn, t)
  176. }
  177. func TestFlowOperation(t *testing.T) {
  178. const fn = "../files/VMTests/vmIOandFlowOperationsTest.json"
  179. RunVmTest(fn, t)
  180. }
  181. func TestLogTest(t *testing.T) {
  182. const fn = "../files/VMTests/vmLogTest.json"
  183. RunVmTest(fn, t)
  184. }
  185. func TestPerformance(t *testing.T) {
  186. t.Skip()
  187. const fn = "../files/VMTests/vmPerformance.json"
  188. RunVmTest(fn, t)
  189. }
  190. func TestPushDupSwap(t *testing.T) {
  191. const fn = "../files/VMTests/vmPushDupSwapTest.json"
  192. RunVmTest(fn, t)
  193. }
  194. func TestVMSha3(t *testing.T) {
  195. const fn = "../files/VMTests/vmSha3Test.json"
  196. RunVmTest(fn, t)
  197. }
  198. func TestVm(t *testing.T) {
  199. const fn = "../files/VMTests/vmtests.json"
  200. RunVmTest(fn, t)
  201. }
  202. func TestVmLog(t *testing.T) {
  203. const fn = "../files/VMTests/vmLogTest.json"
  204. RunVmTest(fn, t)
  205. }
  206. func TestStateExample(t *testing.T) {
  207. const fn = "../files/StateTests/stExample.json"
  208. RunVmTest(fn, t)
  209. }
  210. func TestStateSystemOperations(t *testing.T) {
  211. const fn = "../files/StateTests/stSystemOperationsTest.json"
  212. RunVmTest(fn, t)
  213. }
  214. func TestStatePreCompiledContracts(t *testing.T) {
  215. const fn = "../files/StateTests/stPreCompiledContracts.json"
  216. RunVmTest(fn, t)
  217. }
  218. func TestStateRecursiveCreate(t *testing.T) {
  219. const fn = "../files/StateTests/stRecursiveCreate.json"
  220. RunVmTest(fn, t)
  221. }
  222. func TestStateSpecial(t *testing.T) {
  223. const fn = "../files/StateTests/stSpecialTest.json"
  224. RunVmTest(fn, t)
  225. }
  226. func TestStateRefund(t *testing.T) {
  227. const fn = "../files/StateTests/stRefundTest.json"
  228. RunVmTest(fn, t)
  229. }
  230. func TestStateBlockHash(t *testing.T) {
  231. const fn = "../files/StateTests/stBlockHashTest.json"
  232. RunVmTest(fn, t)
  233. }
  234. func TestStateInitCode(t *testing.T) {
  235. const fn = "../files/StateTests/stInitCodeTest.json"
  236. RunVmTest(fn, t)
  237. }
  238. func TestStateLog(t *testing.T) {
  239. const fn = "../files/StateTests/stLogTests.json"
  240. RunVmTest(fn, t)
  241. }
  242. func TestStateTransaction(t *testing.T) {
  243. t.Skip()
  244. const fn = "../files/StateTests/stTransactionTest.json"
  245. RunVmTest(fn, t)
  246. }