gh_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package vm
  2. import (
  3. "bytes"
  4. "math/big"
  5. "strconv"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/ethdb"
  9. "github.com/ethereum/go-ethereum/ethutil"
  10. "github.com/ethereum/go-ethereum/logger"
  11. "github.com/ethereum/go-ethereum/state"
  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 ethutil.Hex2Bytes(self.AddressF) }
  27. func (self Log) Data() []byte { return ethutil.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] = ethutil.Hex2Bytes(topic)
  33. }
  34. return t
  35. }
  36. func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *state.StateObject {
  37. obj := state.NewStateObject(ethutil.Hex2Bytes(addr), db)
  38. obj.SetBalance(ethutil.Big(account.Balance))
  39. if ethutil.IsHex(account.Code) {
  40. account.Code = account.Code[2:]
  41. }
  42. obj.Code = ethutil.Hex2Bytes(account.Code)
  43. obj.Nonce = ethutil.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. }
  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. helper.Logger.SetLogLevel(4)
  71. if name != "TransactionNonceCheck2" {
  72. continue
  73. }
  74. db, _ := ethdb.NewMemDatabase()
  75. statedb := state.New(nil, db)
  76. for addr, account := range test.Pre {
  77. obj := StateObjectFromAccount(db, addr, account)
  78. statedb.SetStateObject(obj)
  79. for a, v := range account.Storage {
  80. obj.SetState(helper.FromHex(a), ethutil.NewValue(helper.FromHex(v)))
  81. }
  82. }
  83. // XXX Yeah, yeah...
  84. env := make(map[string]string)
  85. env["currentCoinbase"] = test.Env.CurrentCoinbase
  86. env["currentDifficulty"] = test.Env.CurrentDifficulty
  87. env["currentGasLimit"] = test.Env.CurrentGasLimit
  88. env["currentNumber"] = test.Env.CurrentNumber
  89. env["previousHash"] = test.Env.PreviousHash
  90. if n, ok := test.Env.CurrentTimestamp.(float64); ok {
  91. env["currentTimestamp"] = strconv.Itoa(int(n))
  92. } else {
  93. env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
  94. }
  95. var (
  96. ret []byte
  97. gas *big.Int
  98. err error
  99. logs state.Logs
  100. )
  101. isVmTest := len(test.Exec) > 0
  102. if isVmTest {
  103. ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec)
  104. } else {
  105. ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction)
  106. }
  107. rexp := helper.FromHex(test.Out)
  108. if bytes.Compare(rexp, ret) != 0 {
  109. t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
  110. }
  111. if isVmTest {
  112. if len(test.Gas) == 0 && err == nil {
  113. t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
  114. } else {
  115. gexp := ethutil.Big(test.Gas)
  116. if gexp.Cmp(gas) != 0 {
  117. t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
  118. }
  119. }
  120. }
  121. for addr, account := range test.Post {
  122. obj := statedb.GetStateObject(helper.FromHex(addr))
  123. if obj == nil {
  124. continue
  125. }
  126. if len(test.Exec) == 0 {
  127. if obj.Balance().Cmp(ethutil.Big(account.Balance)) != 0 {
  128. 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()))
  129. }
  130. }
  131. for addr, value := range account.Storage {
  132. v := obj.GetState(helper.FromHex(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()[0:4], addr, vexp, v, ethutil.BigD(vexp), ethutil.BigD(v))
  136. }
  137. }
  138. }
  139. if len(test.Logs) > 0 {
  140. for i, log := range test.Logs {
  141. genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 64)
  142. if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) {
  143. t.Errorf("bloom mismatch")
  144. }
  145. }
  146. }
  147. }
  148. logger.Flush()
  149. }
  150. // I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
  151. func TestVMArithmetic(t *testing.T) {
  152. const fn = "../files/VMTests/vmArithmeticTest.json"
  153. RunVmTest(fn, t)
  154. }
  155. func TestSystemOperations(t *testing.T) {
  156. const fn = "../files/VMTests/vmSystemOperationsTest.json"
  157. RunVmTest(fn, t)
  158. }
  159. func TestBitwiseLogicOperation(t *testing.T) {
  160. const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json"
  161. RunVmTest(fn, t)
  162. }
  163. func TestBlockInfo(t *testing.T) {
  164. const fn = "../files/VMTests/vmBlockInfoTest.json"
  165. RunVmTest(fn, t)
  166. }
  167. func TestEnvironmentalInfo(t *testing.T) {
  168. const fn = "../files/VMTests/vmEnvironmentalInfoTest.json"
  169. RunVmTest(fn, t)
  170. }
  171. func TestFlowOperation(t *testing.T) {
  172. const fn = "../files/VMTests/vmIOandFlowOperationsTest.json"
  173. RunVmTest(fn, t)
  174. }
  175. func TestPushDupSwap(t *testing.T) {
  176. const fn = "../files/VMTests/vmPushDupSwapTest.json"
  177. RunVmTest(fn, t)
  178. }
  179. func TestVMSha3(t *testing.T) {
  180. const fn = "../files/VMTests/vmSha3Test.json"
  181. RunVmTest(fn, t)
  182. }
  183. func TestVm(t *testing.T) {
  184. const fn = "../files/VMTests/vmtests.json"
  185. RunVmTest(fn, t)
  186. }
  187. func TestVmLog(t *testing.T) {
  188. const fn = "../files/VMTests/vmLogTest.json"
  189. RunVmTest(fn, t)
  190. }
  191. func TestStateSystemOperations(t *testing.T) {
  192. const fn = "../files/StateTests/stSystemOperationsTest.json"
  193. RunVmTest(fn, t)
  194. }
  195. func TestStatePreCompiledContracts(t *testing.T) {
  196. const fn = "../files/StateTests/stPreCompiledContracts.json"
  197. RunVmTest(fn, t)
  198. }
  199. func TestStateRecursiveCreate(t *testing.T) {
  200. const fn = "../files/StateTests/stRecursiveCreate.json"
  201. RunVmTest(fn, t)
  202. }
  203. func TestStateSpecial(t *testing.T) {
  204. const fn = "../files/StateTests/stSpecialTest.json"
  205. RunVmTest(fn, t)
  206. }
  207. func TestStateRefund(t *testing.T) {
  208. const fn = "../files/StateTests/stRefundTest.json"
  209. RunVmTest(fn, t)
  210. }
  211. func TestStateBlockHash(t *testing.T) {
  212. const fn = "../files/StateTests/stBlockHashTest.json"
  213. RunVmTest(fn, t)
  214. }
  215. func TestStateInitCode(t *testing.T) {
  216. const fn = "../files/StateTests/stInitCodeTest.json"
  217. RunVmTest(fn, t)
  218. }
  219. func TestStateLog(t *testing.T) {
  220. const fn = "../files/StateTests/stLogTests.json"
  221. RunVmTest(fn, t)
  222. }
  223. func TestStateTransaction(t *testing.T) {
  224. t.Skip()
  225. const fn = "../files/StateTests/stTransactionTest.json"
  226. RunVmTest(fn, t)
  227. }