You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.0 KiB

  1. package rpc
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gorilla/websocket"
  6. "github.com/tendermint/tendermint/binary"
  7. "github.com/tendermint/tendermint/types"
  8. "testing"
  9. )
  10. func unmarshalValidateBlockchain(t *testing.T, con *websocket.Conn, eid string) {
  11. var initBlockN uint
  12. for i := 0; i < 2; i++ {
  13. waitForEvent(t, con, eid, true, func() {}, func(eid string, b []byte) error {
  14. block, err := unmarshalResponseNewBlock(b)
  15. if err != nil {
  16. return err
  17. }
  18. if i == 0 {
  19. initBlockN = block.Header.Height
  20. } else {
  21. if block.Header.Height != initBlockN+uint(i) {
  22. return fmt.Errorf("Expected block %d, got block %d", i, block.Header.Height)
  23. }
  24. }
  25. return nil
  26. })
  27. }
  28. }
  29. func unmarshalValidateSend(amt uint64, toAddr []byte) func(string, []byte) error {
  30. return func(eid string, b []byte) error {
  31. // unmarshal and assert correctness
  32. var response struct {
  33. Event string `json:"event"`
  34. Data types.SendTx `json:"data"`
  35. Error string `json:"error"`
  36. }
  37. var err error
  38. binary.ReadJSON(&response, b, &err)
  39. if err != nil {
  40. return err
  41. }
  42. if response.Error != "" {
  43. return fmt.Errorf(response.Error)
  44. }
  45. if eid != response.Event {
  46. return fmt.Errorf("Eventid is not correct. Got %s, expected %s", response.Event, eid)
  47. }
  48. tx := response.Data
  49. if bytes.Compare(tx.Inputs[0].Address, userByteAddr) != 0 {
  50. return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Inputs[0].Address, userByteAddr)
  51. }
  52. if tx.Inputs[0].Amount != amt {
  53. return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Inputs[0].Amount, amt)
  54. }
  55. if bytes.Compare(tx.Outputs[0].Address, toAddr) != 0 {
  56. return fmt.Errorf("Receivers do not match up! Got %x, expected %x", tx.Outputs[0].Address, userByteAddr)
  57. }
  58. return nil
  59. }
  60. }
  61. func unmarshalValidateCall(amt uint64, returnCode []byte) func(string, []byte) error {
  62. return func(eid string, b []byte) error {
  63. // unmarshall and assert somethings
  64. var response struct {
  65. Event string `json:"event"`
  66. Data struct {
  67. Tx types.CallTx `json:"tx"`
  68. Return []byte `json:"return"`
  69. Exception string `json:"exception"`
  70. } `json:"data"`
  71. Error string `json:"error"`
  72. }
  73. var err error
  74. binary.ReadJSON(&response, b, &err)
  75. if err != nil {
  76. return err
  77. }
  78. if response.Error != "" {
  79. return fmt.Errorf(response.Error)
  80. }
  81. if response.Data.Exception != "" {
  82. return fmt.Errorf(response.Data.Exception)
  83. }
  84. tx := response.Data.Tx
  85. if bytes.Compare(tx.Input.Address, userByteAddr) != 0 {
  86. return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Input.Address, userByteAddr)
  87. }
  88. if tx.Input.Amount != amt {
  89. return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Input.Amount, amt)
  90. }
  91. ret := response.Data.Return
  92. if bytes.Compare(ret, returnCode) != 0 {
  93. return fmt.Errorf("Call did not return correctly. Got %x, expected %x", ret, returnCode)
  94. }
  95. return nil
  96. }
  97. }
  98. func unmarshalValidateCallCall(origin, returnCode []byte, txid *[]byte) func(string, []byte) error {
  99. return func(eid string, b []byte) error {
  100. // unmarshall and assert somethings
  101. var response struct {
  102. Event string `json:"event"`
  103. Data types.EventMsgCall `json:"data"`
  104. Error string `json:"error"`
  105. }
  106. var err error
  107. binary.ReadJSON(&response, b, &err)
  108. if err != nil {
  109. return err
  110. }
  111. if response.Error != "" {
  112. return fmt.Errorf(response.Error)
  113. }
  114. if response.Data.Exception != "" {
  115. return fmt.Errorf(response.Data.Exception)
  116. }
  117. if bytes.Compare(response.Data.Origin, origin) != 0 {
  118. return fmt.Errorf("Origin does not match up! Got %x, expected %x", response.Data.Origin, origin)
  119. }
  120. ret := response.Data.Return
  121. if bytes.Compare(ret, returnCode) != 0 {
  122. return fmt.Errorf("Call did not return correctly. Got %x, expected %x", ret, returnCode)
  123. }
  124. if bytes.Compare(response.Data.TxId, *txid) != 0 {
  125. return fmt.Errorf("TxIds do not match up! Got %x, expected %x", response.Data.TxId, *txid)
  126. }
  127. // calldata := response.Data.CallData
  128. return nil
  129. }
  130. }