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.

166 lines
4.7 KiB

  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/merkle"
  9. "github.com/tendermint/tendermint/rpc/core"
  10. "github.com/tendermint/tendermint/state"
  11. "github.com/tendermint/tendermint/types"
  12. "io/ioutil"
  13. "net/http"
  14. "testing"
  15. "time"
  16. )
  17. func TestHTTPStatus(t *testing.T) {
  18. resp, err := http.Get(requestAddr + "status")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer resp.Body.Close()
  23. body, err := ioutil.ReadAll(resp.Body)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. var response struct {
  28. Result core.ResponseStatus `json:"result"`
  29. Error string `json:"error"`
  30. Id string `json:"id"`
  31. JSONRPC string `json:"jsonrpc"`
  32. }
  33. binary.ReadJSON(&response, body, &err)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. result := response.Result
  38. fmt.Println(">>>", result)
  39. return
  40. }
  41. func TestHTTPGenPriv(t *testing.T) {
  42. resp, err := http.Get(requestAddr + "unsafe/gen_priv_account")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if resp.StatusCode != 200 {
  47. t.Fatal(resp)
  48. }
  49. defer resp.Body.Close()
  50. body, err := ioutil.ReadAll(resp.Body)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. var response struct {
  55. Result core.ResponseGenPrivAccount `json:"result"`
  56. Error string `json:"error"`
  57. Id string `json:"id"`
  58. JSONRPC string `json:"jsonrpc"`
  59. }
  60. binary.ReadJSON(&response, body, &err)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. fmt.Println(">>>", response)
  65. }
  66. func TestHTTPGetAccount(t *testing.T) {
  67. byteAddr, _ := hex.DecodeString(userAddr)
  68. acc := getAccount(t, "HTTP", byteAddr)
  69. if acc == nil {
  70. t.Fatalf("Account was nil")
  71. }
  72. if bytes.Compare(acc.Address, byteAddr) != 0 {
  73. t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, byteAddr)
  74. }
  75. }
  76. func TestHTTPSignedTx(t *testing.T) {
  77. byteAddr, _ := hex.DecodeString(userAddr)
  78. var byteKey [64]byte
  79. oh, _ := hex.DecodeString(userPriv)
  80. copy(byteKey[:], oh)
  81. amt := uint64(100)
  82. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  83. tx, priv := signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
  84. checkTx(t, byteAddr, priv, tx.(*types.SendTx))
  85. toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  86. tx, priv = signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
  87. checkTx(t, byteAddr, priv, tx.(*types.SendTx))
  88. toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  89. tx, priv = signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
  90. checkTx(t, byteAddr, priv, tx.(*types.SendTx))
  91. }
  92. func TestHTTPBroadcastTx(t *testing.T) {
  93. byteAddr, _ := hex.DecodeString(userAddr)
  94. var byteKey [64]byte
  95. oh, _ := hex.DecodeString(userPriv)
  96. copy(byteKey[:], oh)
  97. amt := uint64(100)
  98. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  99. tx, receipt := broadcastTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
  100. if receipt.CreatesContract > 0 {
  101. t.Fatal("This tx does not create a contract")
  102. }
  103. if len(receipt.TxHash) == 0 {
  104. t.Fatal("Failed to compute tx hash")
  105. }
  106. pool := node.MempoolReactor().Mempool
  107. txs := pool.GetProposalTxs()
  108. if len(txs) != mempoolCount+1 {
  109. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount+1)
  110. }
  111. tx2 := txs[mempoolCount].(*types.SendTx)
  112. mempoolCount += 1
  113. if bytes.Compare(merkle.HashFromBinary(tx), merkle.HashFromBinary(tx2)) != 0 {
  114. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  115. }
  116. }
  117. func TestHTTPGetStorage(t *testing.T) {
  118. priv := state.LoadPrivValidator(".tendermint/priv_validator.json")
  119. _ = priv
  120. //core.SetPrivValidator(priv)
  121. byteAddr, _ := hex.DecodeString(userAddr)
  122. var byteKey [64]byte
  123. oh, _ := hex.DecodeString(userPriv)
  124. copy(byteKey[:], oh)
  125. amt := uint64(1100)
  126. code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
  127. _, receipt := broadcastTx(t, "HTTP", byteAddr, nil, code, byteKey, amt, 1000, 1000)
  128. if receipt.CreatesContract == 0 {
  129. t.Fatal("This tx creates a contract")
  130. }
  131. if len(receipt.TxHash) == 0 {
  132. t.Fatal("Failed to compute tx hash")
  133. }
  134. contractAddr := receipt.ContractAddr
  135. if len(contractAddr) == 0 {
  136. t.Fatal("Creates contract but resulting address is empty")
  137. }
  138. time.Sleep(time.Second * 20)
  139. mempoolCount -= 1
  140. v := getStorage(t, contractAddr, []byte{0x1})
  141. got := RightPadWord256(v)
  142. expected := RightPadWord256([]byte{0x5})
  143. if got.Compare(expected) != 0 {
  144. t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes())
  145. }
  146. }
  147. /*tx.Inputs[0].Signature = mint.priv.PrivKey.Sign(account.SignBytes(tx))
  148. err = mint.MempoolReactor.BroadcastTx(tx)
  149. return hex.EncodeToString(merkle.HashFromBinary(tx)), err*/