gh_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/common"
  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 common.Hex2Bytes(self.AddressF) }
  26. func (self Log) Data() []byte { return common.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] = common.Hex2Bytes(topic)
  32. }
  33. return t
  34. }
  35. func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
  36. obj := state.NewStateObject(common.Hex2Bytes(addr), db)
  37. obj.SetBalance(common.Big(account.Balance))
  38. if common.IsHex(account.Code) {
  39. account.Code = account.Code[2:]
  40. }
  41. obj.SetCode(common.Hex2Bytes(account.Code))
  42. obj.SetNonce(common.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), common.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. rexp := helper.FromHex(test.Out)
  104. if bytes.Compare(rexp, ret) != 0 {
  105. t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
  106. }
  107. if isVmTest {
  108. if len(test.Gas) == 0 && err == nil {
  109. t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
  110. } else {
  111. gexp := common.Big(test.Gas)
  112. if gexp.Cmp(gas) != 0 {
  113. t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
  114. }
  115. }
  116. }
  117. for addr, account := range test.Post {
  118. obj := statedb.GetStateObject(helper.FromHex(addr))
  119. if obj == nil {
  120. continue
  121. }
  122. if len(test.Exec) == 0 {
  123. if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
  124. 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(common.Big(account.Balance), obj.Balance()))
  125. }
  126. }
  127. for addr, value := range account.Storage {
  128. v := obj.GetState(helper.FromHex(addr)).Bytes()
  129. vexp := helper.FromHex(value)
  130. if bytes.Compare(v, vexp) != 0 {
  131. t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
  132. }
  133. }
  134. }
  135. if !isVmTest {
  136. statedb.Sync()
  137. if !bytes.Equal(common.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 := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
  150. if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
  151. t.Errorf("bloom mismatch")
  152. }
  153. }
  154. */
  155. }
  156. }
  157. //statedb.Trie().PrintRoot()
  158. }
  159. logger.Flush()
  160. }
  161. // I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
  162. func TestVMArithmetic(t *testing.T) {
  163. const fn = "../files/VMTests/vmArithmeticTest.json"
  164. RunVmTest(fn, t)
  165. }
  166. func TestBitwiseLogicOperation(t *testing.T) {
  167. const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json"
  168. RunVmTest(fn, t)
  169. }
  170. func TestBlockInfo(t *testing.T) {
  171. const fn = "../files/VMTests/vmBlockInfoTest.json"
  172. RunVmTest(fn, t)
  173. }
  174. func TestEnvironmentalInfo(t *testing.T) {
  175. const fn = "../files/VMTests/vmEnvironmentalInfoTest.json"
  176. RunVmTest(fn, t)
  177. }
  178. func TestFlowOperation(t *testing.T) {
  179. const fn = "../files/VMTests/vmIOandFlowOperationsTest.json"
  180. RunVmTest(fn, t)
  181. }
  182. func TestLogTest(t *testing.T) {
  183. const fn = "../files/VMTests/vmLogTest.json"
  184. RunVmTest(fn, t)
  185. }
  186. func TestPerformance(t *testing.T) {
  187. t.Skip()
  188. const fn = "../files/VMTests/vmPerformance.json"
  189. RunVmTest(fn, t)
  190. }
  191. func TestPushDupSwap(t *testing.T) {
  192. const fn = "../files/VMTests/vmPushDupSwapTest.json"
  193. RunVmTest(fn, t)
  194. }
  195. func TestVMSha3(t *testing.T) {
  196. const fn = "../files/VMTests/vmSha3Test.json"
  197. RunVmTest(fn, t)
  198. }
  199. func TestVm(t *testing.T) {
  200. const fn = "../files/VMTests/vmtests.json"
  201. RunVmTest(fn, t)
  202. }
  203. func TestVmLog(t *testing.T) {
  204. const fn = "../files/VMTests/vmLogTest.json"
  205. RunVmTest(fn, t)
  206. }
  207. func TestStateExample(t *testing.T) {
  208. const fn = "../files/StateTests/stExample.json"
  209. RunVmTest(fn, t)
  210. }
  211. func TestStateSystemOperations(t *testing.T) {
  212. const fn = "../files/StateTests/stSystemOperationsTest.json"
  213. RunVmTest(fn, t)
  214. }
  215. func TestStatePreCompiledContracts(t *testing.T) {
  216. const fn = "../files/StateTests/stPreCompiledContracts.json"
  217. RunVmTest(fn, t)
  218. }
  219. func TestStateRecursiveCreate(t *testing.T) {
  220. const fn = "../files/StateTests/stRecursiveCreate.json"
  221. RunVmTest(fn, t)
  222. }
  223. func TestStateSpecial(t *testing.T) {
  224. const fn = "../files/StateTests/stSpecialTest.json"
  225. RunVmTest(fn, t)
  226. }
  227. func TestStateRefund(t *testing.T) {
  228. const fn = "../files/StateTests/stRefundTest.json"
  229. RunVmTest(fn, t)
  230. }
  231. func TestStateBlockHash(t *testing.T) {
  232. const fn = "../files/StateTests/stBlockHashTest.json"
  233. RunVmTest(fn, t)
  234. }
  235. func TestStateInitCode(t *testing.T) {
  236. const fn = "../files/StateTests/stInitCodeTest.json"
  237. RunVmTest(fn, t)
  238. }
  239. func TestStateLog(t *testing.T) {
  240. const fn = "../files/StateTests/stLogTests.json"
  241. RunVmTest(fn, t)
  242. }
  243. func TestStateTransaction(t *testing.T) {
  244. t.Skip()
  245. const fn = "../files/StateTests/stTransactionTest.json"
  246. RunVmTest(fn, t)
  247. }