gh_test.go 8.4 KB

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