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.

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