gh_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 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. // Log the error if there is one. Error does not mean failing test.
  104. // A test fails if err != nil and post params are specified in the test.
  105. if err != nil {
  106. helper.Log.Infof("%s's: %v\n", name, err)
  107. }
  108. rexp := helper.FromHex(test.Out)
  109. if bytes.Compare(rexp, ret) != 0 {
  110. t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
  111. }
  112. if isVmTest {
  113. if len(test.Gas) == 0 && err == nil {
  114. t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
  115. } else {
  116. gexp := ethutil.Big(test.Gas)
  117. if gexp.Cmp(gas) != 0 {
  118. t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
  119. }
  120. }
  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. 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()))
  130. }
  131. }
  132. for addr, value := range account.Storage {
  133. v := obj.GetState(helper.FromHex(addr)).Bytes()
  134. vexp := helper.FromHex(value)
  135. if bytes.Compare(v, vexp) != 0 {
  136. 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))
  137. }
  138. }
  139. }
  140. if len(test.Logs) > 0 {
  141. for i, log := range test.Logs {
  142. genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 64)
  143. if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) {
  144. t.Errorf("bloom mismatch")
  145. }
  146. }
  147. }
  148. }
  149. logger.Flush()
  150. }
  151. // I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
  152. func TestVMArithmetic(t *testing.T) {
  153. const fn = "../files/vmtests/vmArithmeticTest.json"
  154. RunVmTest(fn, t)
  155. }
  156. func TestBitwiseLogicOperation(t *testing.T) {
  157. const fn = "../files/vmtests/vmBitwiseLogicOperationTest.json"
  158. RunVmTest(fn, t)
  159. }
  160. func TestBlockInfo(t *testing.T) {
  161. const fn = "../files/vmtests/vmBlockInfoTest.json"
  162. RunVmTest(fn, t)
  163. }
  164. func TestEnvironmentalInfo(t *testing.T) {
  165. const fn = "../files/vmtests/vmEnvironmentalInfoTest.json"
  166. RunVmTest(fn, t)
  167. }
  168. func TestFlowOperation(t *testing.T) {
  169. const fn = "../files/vmtests/vmIOandFlowOperationsTest.json"
  170. RunVmTest(fn, t)
  171. }
  172. func TestPushDupSwap(t *testing.T) {
  173. const fn = "../files/vmtests/vmPushDupSwapTest.json"
  174. RunVmTest(fn, t)
  175. }
  176. func TestVMSha3(t *testing.T) {
  177. const fn = "../files/vmtests/vmSha3Test.json"
  178. RunVmTest(fn, t)
  179. }
  180. func TestVm(t *testing.T) {
  181. const fn = "../files/vmtests/vmtests.json"
  182. RunVmTest(fn, t)
  183. }
  184. func TestVmLog(t *testing.T) {
  185. const fn = "../files/vmtests/vmLogTest.json"
  186. RunVmTest(fn, t)
  187. }
  188. func TestStateSystemOperations(t *testing.T) {
  189. const fn = "../files/StateTests/stSystemOperationsTest.json"
  190. RunVmTest(fn, t)
  191. }
  192. func TestStatePreCompiledContracts(t *testing.T) {
  193. const fn = "../files/StateTests/stPreCompiledContracts.json"
  194. RunVmTest(fn, t)
  195. }
  196. func TestStateRecursiveCreate(t *testing.T) {
  197. const fn = "../files/StateTests/stRecursiveCreate.json"
  198. RunVmTest(fn, t)
  199. }
  200. func TestStateSpecial(t *testing.T) {
  201. const fn = "../files/StateTests/stSpecialTest.json"
  202. RunVmTest(fn, t)
  203. }
  204. func TestStateRefund(t *testing.T) {
  205. const fn = "../files/StateTests/stRefundTest.json"
  206. RunVmTest(fn, t)
  207. }