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.

213 lines
6.0 KiB

  1. package rpctest
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/tendermint/tendermint/account"
  6. _ "github.com/tendermint/tendermint/config/tendermint_test"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. var wsTyp = "JSONRPC"
  10. //--------------------------------------------------------------------------------
  11. // Test the websocket service
  12. // make a simple connection to the server
  13. func TestWSConnect(t *testing.T) {
  14. con := newWSCon(t)
  15. con.Close()
  16. }
  17. // receive a new block message
  18. func _TestWSNewBlock(t *testing.T) {
  19. con := newWSCon(t)
  20. eid := types.EventStringNewBlock()
  21. subscribe(t, con, eid)
  22. defer func() {
  23. unsubscribe(t, con, eid)
  24. con.Close()
  25. }()
  26. waitForEvent(t, con, eid, true, func() {}, func(eid string, b []byte) error {
  27. fmt.Println("Check:", string(b))
  28. return nil
  29. })
  30. }
  31. // receive a few new block messages in a row, with increasing height
  32. func TestWSBlockchainGrowth(t *testing.T) {
  33. if testing.Short() {
  34. t.Skip("skipping test in short mode.")
  35. }
  36. con := newWSCon(t)
  37. eid := types.EventStringNewBlock()
  38. subscribe(t, con, eid)
  39. defer func() {
  40. unsubscribe(t, con, eid)
  41. con.Close()
  42. }()
  43. // listen for NewBlock, ensure height increases by 1
  44. unmarshalValidateBlockchain(t, con, eid)
  45. }
  46. // send a transaction and validate the events from listening for both sender and receiver
  47. func TestWSSend(t *testing.T) {
  48. toAddr := user[1].Address
  49. amt := int64(100)
  50. con := newWSCon(t)
  51. eidInput := types.EventStringAccInput(user[0].Address)
  52. eidOutput := types.EventStringAccOutput(toAddr)
  53. subscribe(t, con, eidInput)
  54. subscribe(t, con, eidOutput)
  55. defer func() {
  56. unsubscribe(t, con, eidInput)
  57. unsubscribe(t, con, eidOutput)
  58. con.Close()
  59. }()
  60. waitForEvent(t, con, eidInput, true, func() {
  61. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  62. broadcastTx(t, wsTyp, tx)
  63. }, unmarshalValidateSend(amt, toAddr))
  64. waitForEvent(t, con, eidOutput, true, func() {}, unmarshalValidateSend(amt, toAddr))
  65. }
  66. // ensure events are only fired once for a given transaction
  67. func TestWSDoubleFire(t *testing.T) {
  68. if testing.Short() {
  69. t.Skip("skipping test in short mode.")
  70. }
  71. con := newWSCon(t)
  72. eid := types.EventStringAccInput(user[0].Address)
  73. subscribe(t, con, eid)
  74. defer func() {
  75. unsubscribe(t, con, eid)
  76. con.Close()
  77. }()
  78. amt := int64(100)
  79. toAddr := user[1].Address
  80. // broadcast the transaction, wait to hear about it
  81. waitForEvent(t, con, eid, true, func() {
  82. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  83. broadcastTx(t, wsTyp, tx)
  84. }, func(eid string, b []byte) error {
  85. return nil
  86. })
  87. // but make sure we don't hear about it twice
  88. waitForEvent(t, con, eid, false, func() {
  89. }, func(eid string, b []byte) error {
  90. return nil
  91. })
  92. }
  93. // create a contract, wait for the event, and send it a msg, validate the return
  94. func TestWSCallWait(t *testing.T) {
  95. if testing.Short() {
  96. t.Skip("skipping test in short mode.")
  97. }
  98. con := newWSCon(t)
  99. eid1 := types.EventStringAccInput(user[0].Address)
  100. subscribe(t, con, eid1)
  101. defer func() {
  102. unsubscribe(t, con, eid1)
  103. con.Close()
  104. }()
  105. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  106. code, returnCode, returnVal := simpleContract()
  107. var contractAddr []byte
  108. // wait for the contract to be created
  109. waitForEvent(t, con, eid1, true, func() {
  110. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  111. receipt := broadcastTx(t, wsTyp, tx)
  112. contractAddr = receipt.ContractAddr
  113. }, unmarshalValidateCall(amt, returnCode))
  114. // susbscribe to the new contract
  115. amt = int64(10001)
  116. eid2 := types.EventStringAccOutput(contractAddr)
  117. subscribe(t, con, eid2)
  118. defer func() {
  119. unsubscribe(t, con, eid2)
  120. }()
  121. // get the return value from a call
  122. data := []byte{0x1}
  123. waitForEvent(t, con, eid2, true, func() {
  124. tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
  125. receipt := broadcastTx(t, wsTyp, tx)
  126. contractAddr = receipt.ContractAddr
  127. }, unmarshalValidateCall(amt, returnVal))
  128. }
  129. // create a contract and send it a msg without waiting. wait for contract event
  130. // and validate return
  131. func TestWSCallNoWait(t *testing.T) {
  132. if testing.Short() {
  133. t.Skip("skipping test in short mode.")
  134. }
  135. con := newWSCon(t)
  136. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  137. code, _, returnVal := simpleContract()
  138. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  139. receipt := broadcastTx(t, wsTyp, tx)
  140. contractAddr := receipt.ContractAddr
  141. // susbscribe to the new contract
  142. amt = int64(10001)
  143. eid := types.EventStringAccOutput(contractAddr)
  144. subscribe(t, con, eid)
  145. defer func() {
  146. unsubscribe(t, con, eid)
  147. con.Close()
  148. }()
  149. // get the return value from a call
  150. data := []byte{0x1}
  151. waitForEvent(t, con, eid, true, func() {
  152. tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
  153. broadcastTx(t, wsTyp, tx)
  154. }, unmarshalValidateCall(amt, returnVal))
  155. }
  156. // create two contracts, one of which calls the other
  157. func TestWSCallCall(t *testing.T) {
  158. if testing.Short() {
  159. t.Skip("skipping test in short mode.")
  160. }
  161. con := newWSCon(t)
  162. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  163. code, _, returnVal := simpleContract()
  164. txid := new([]byte)
  165. // deploy the two contracts
  166. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  167. receipt := broadcastTx(t, wsTyp, tx)
  168. contractAddr1 := receipt.ContractAddr
  169. code, _, _ = simpleCallContract(contractAddr1)
  170. tx = makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  171. receipt = broadcastTx(t, wsTyp, tx)
  172. contractAddr2 := receipt.ContractAddr
  173. // susbscribe to the new contracts
  174. amt = int64(10001)
  175. eid1 := types.EventStringAccReceive(contractAddr1)
  176. subscribe(t, con, eid1)
  177. defer func() {
  178. unsubscribe(t, con, eid1)
  179. con.Close()
  180. }()
  181. // call contract2, which should call contract1, and wait for ev1
  182. // let the contract get created first
  183. waitForEvent(t, con, eid1, true, func() {
  184. }, func(eid string, b []byte) error {
  185. return nil
  186. })
  187. // call it
  188. waitForEvent(t, con, eid1, true, func() {
  189. tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee)
  190. broadcastTx(t, wsTyp, tx)
  191. *txid = account.HashSignBytes(chainID, tx)
  192. }, unmarshalValidateCallCall(user[0].Address, returnVal, txid))
  193. }