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.

172 lines
4.3 KiB

  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/tendermint/tendermint/binary"
  8. "github.com/tendermint/tendermint/config"
  9. "github.com/tendermint/tendermint/merkle"
  10. "github.com/tendermint/tendermint/rpc"
  11. "github.com/tendermint/tendermint/rpc/core"
  12. "github.com/tendermint/tendermint/types"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "testing"
  17. )
  18. func TestJSONStatus(t *testing.T) {
  19. s := rpc.JsonRpc{
  20. JsonRpc: "2.0",
  21. Method: "status",
  22. Params: []string{},
  23. Id: 0,
  24. }
  25. b, err := json.Marshal(s)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. buf := bytes.NewBuffer(b)
  30. resp, err := http.Post(requestAddr, "text/json", buf)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. defer resp.Body.Close()
  35. body, err := ioutil.ReadAll(resp.Body)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. status := new(struct {
  40. Status string
  41. Data core.ResponseStatus
  42. Error string
  43. })
  44. err = json.Unmarshal(body, status)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if status.Data.Network != config.App().GetString("Network") {
  49. t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s", status.Data.Network, config.App().Get("Network")))
  50. }
  51. }
  52. func TestJSONGenPriv(t *testing.T) {
  53. s := rpc.JsonRpc{
  54. JsonRpc: "2.0",
  55. Method: "unsafe/gen_priv_account",
  56. Params: []string{},
  57. Id: 0,
  58. }
  59. b, err := json.Marshal(s)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. buf := bytes.NewBuffer(b)
  64. resp, err := http.Post(requestAddr, "text/json", buf)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if resp.StatusCode != 200 {
  69. t.Fatal(resp)
  70. }
  71. defer resp.Body.Close()
  72. body, err := ioutil.ReadAll(resp.Body)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. var status struct {
  77. Status string
  78. Data core.ResponseGenPrivAccount
  79. Error string
  80. }
  81. binary.ReadJSON(&status, body, &err)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if len(status.Data.PrivAccount.Address) == 0 {
  86. t.Fatal("Failed to generate an address")
  87. }
  88. }
  89. func TestJSONGetAccount(t *testing.T) {
  90. byteAddr, _ := hex.DecodeString(userAddr)
  91. acc := getAccount(t, "JSONRPC", byteAddr)
  92. if bytes.Compare(acc.Address, byteAddr) != 0 {
  93. t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, byteAddr)
  94. }
  95. }
  96. func TestJSONSignedTx(t *testing.T) {
  97. byteAddr, _ := hex.DecodeString(userAddr)
  98. var byteKey [64]byte
  99. oh, _ := hex.DecodeString(userPriv)
  100. copy(byteKey[:], oh)
  101. amt := uint64(100)
  102. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  103. tx, priv := signTx(t, "JSONRPC", byteAddr, toAddr, byteKey, amt)
  104. checkTx(t, byteAddr, priv, tx)
  105. toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  106. tx, priv = signTx(t, "JSONRPC", byteAddr, toAddr, byteKey, amt)
  107. checkTx(t, byteAddr, priv, tx)
  108. toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  109. tx, priv = signTx(t, "JSONRPC", byteAddr, toAddr, byteKey, amt)
  110. checkTx(t, byteAddr, priv, tx)
  111. }
  112. func TestJSONBroadcastTx(t *testing.T) {
  113. byteAddr, _ := hex.DecodeString(userAddr)
  114. var byteKey [64]byte
  115. oh, _ := hex.DecodeString(userPriv)
  116. copy(byteKey[:], oh)
  117. amt := uint64(100)
  118. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  119. tx, priv := signTx(t, "JSONRPC", byteAddr, toAddr, byteKey, amt)
  120. checkTx(t, byteAddr, priv, tx)
  121. n, w := new(int64), new(bytes.Buffer)
  122. var err error
  123. binary.WriteJSON(tx, w, n, &err)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. b := w.Bytes()
  128. var status struct {
  129. Status string
  130. Data core.ResponseBroadcastTx
  131. Error string
  132. }
  133. requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &status)
  134. if status.Status == "ERROR" {
  135. t.Fatal(status.Error)
  136. }
  137. receipt := status.Data.Receipt
  138. if receipt.CreatesContract > 0 {
  139. t.Fatal("This tx does not create a contract")
  140. }
  141. if len(receipt.TxHash) == 0 {
  142. t.Fatal("Failed to compute tx hash")
  143. }
  144. pool := node.MempoolReactor().Mempool
  145. txs := pool.GetProposalTxs()
  146. if len(txs) != mempoolCount+1 {
  147. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount+1)
  148. }
  149. tx2 := txs[mempoolCount].(*types.SendTx)
  150. mempoolCount += 1
  151. if bytes.Compare(merkle.HashFromBinary(tx), merkle.HashFromBinary(tx2)) != 0 {
  152. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  153. }
  154. }
  155. /*tx.Inputs[0].Signature = mint.priv.PrivKey.Sign(account.SignBytes(tx))
  156. err = mint.MempoolReactor.BroadcastTx(tx)
  157. return hex.EncodeToString(merkle.HashFromBinary(tx)), err*/