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.

192 lines
5.4 KiB

  1. package rpctest
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/tendermint/tendermint/account"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/types"
  8. "testing"
  9. )
  10. func testStatus(t *testing.T, typ string) {
  11. client := clients[typ]
  12. resp, err := client.Status()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if resp.Network != config.GetString("network") {
  17. t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s",
  18. resp.Network, config.Get("network")))
  19. }
  20. }
  21. func testGenPriv(t *testing.T, typ string) {
  22. client := clients[typ]
  23. resp, err := client.GenPrivAccount()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. if len(resp.PrivAccount.Address) == 0 {
  28. t.Fatal("Failed to generate an address")
  29. }
  30. }
  31. func testGetAccount(t *testing.T, typ string) {
  32. acc := getAccount(t, typ, user[0].Address)
  33. if acc == nil {
  34. t.Fatalf("Account was nil")
  35. }
  36. if bytes.Compare(acc.Address, user[0].Address) != 0 {
  37. t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, user[0].Address)
  38. }
  39. }
  40. func testSignedTx(t *testing.T, typ string) {
  41. amt := uint64(100)
  42. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  43. testOneSignTx(t, typ, toAddr, amt)
  44. toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  45. testOneSignTx(t, typ, toAddr, amt)
  46. toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
  47. testOneSignTx(t, typ, toAddr, amt)
  48. }
  49. func testOneSignTx(t *testing.T, typ string, addr []byte, amt uint64) {
  50. tx := makeDefaultSendTx(t, typ, addr, amt)
  51. tx2 := signTx(t, typ, tx, user[0])
  52. tx2hash := account.HashSignBytes(tx2)
  53. tx.SignInput(0, user[0])
  54. txhash := account.HashSignBytes(tx)
  55. if bytes.Compare(txhash, tx2hash) != 0 {
  56. t.Fatal("Got different signatures for signing via rpc vs tx_utils")
  57. }
  58. tx_ := signTx(t, typ, tx, user[0])
  59. tx = tx_.(*types.SendTx)
  60. checkTx(t, user[0].Address, user[0], tx)
  61. }
  62. func testBroadcastTx(t *testing.T, typ string) {
  63. amt := uint64(100)
  64. toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
  65. tx := makeDefaultSendTxSigned(t, typ, toAddr, amt)
  66. receipt := broadcastTx(t, typ, tx)
  67. if receipt.CreatesContract > 0 {
  68. t.Fatal("This tx does not create a contract")
  69. }
  70. if len(receipt.TxHash) == 0 {
  71. t.Fatal("Failed to compute tx hash")
  72. }
  73. pool := node.MempoolReactor().Mempool
  74. txs := pool.GetProposalTxs()
  75. if len(txs) != mempoolCount {
  76. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount)
  77. }
  78. tx2 := txs[mempoolCount-1].(*types.SendTx)
  79. n, err := new(int64), new(error)
  80. buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
  81. tx.WriteSignBytes(buf1, n, err)
  82. tx2.WriteSignBytes(buf2, n, err)
  83. if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
  84. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  85. }
  86. }
  87. func testGetStorage(t *testing.T, typ string) {
  88. con := newWSCon(t)
  89. eid := types.EventStringNewBlock()
  90. subscribe(t, con, eid)
  91. defer func() {
  92. unsubscribe(t, con, eid)
  93. con.Close()
  94. }()
  95. amt, gasLim, fee := uint64(1100), uint64(1000), uint64(1000)
  96. code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
  97. tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
  98. receipt := broadcastTx(t, typ, tx)
  99. if receipt.CreatesContract == 0 {
  100. t.Fatal("This tx creates a contract")
  101. }
  102. if len(receipt.TxHash) == 0 {
  103. t.Fatal("Failed to compute tx hash")
  104. }
  105. contractAddr := receipt.ContractAddr
  106. if len(contractAddr) == 0 {
  107. t.Fatal("Creates contract but resulting address is empty")
  108. }
  109. // allow it to get mined
  110. waitForEvent(t, con, eid, true, func() {
  111. }, func(eid string, b []byte) error {
  112. return nil
  113. })
  114. mempoolCount = 0
  115. v := getStorage(t, typ, contractAddr, []byte{0x1})
  116. got := LeftPadWord256(v)
  117. expected := LeftPadWord256([]byte{0x5})
  118. if got.Compare(expected) != 0 {
  119. t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes())
  120. }
  121. }
  122. func testCallCode(t *testing.T, typ string) {
  123. client := clients[typ]
  124. // add two integers and return the result
  125. code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
  126. data := []byte{}
  127. expected := []byte{0xb}
  128. callCode(t, client, code, data, expected)
  129. // pass two ints as calldata, add, and return the result
  130. code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
  131. data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...)
  132. expected = []byte{0xb}
  133. callCode(t, client, code, data, expected)
  134. }
  135. func testCall(t *testing.T, typ string) {
  136. con := newWSCon(t)
  137. eid := types.EventStringNewBlock()
  138. subscribe(t, con, eid)
  139. defer func() {
  140. unsubscribe(t, con, eid)
  141. con.Close()
  142. }()
  143. client := clients[typ]
  144. // create the contract
  145. amt, gasLim, fee := uint64(6969), uint64(1000), uint64(1000)
  146. code, _, _ := simpleContract()
  147. tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
  148. receipt := broadcastTx(t, typ, tx)
  149. if receipt.CreatesContract == 0 {
  150. t.Fatal("This tx creates a contract")
  151. }
  152. if len(receipt.TxHash) == 0 {
  153. t.Fatal("Failed to compute tx hash")
  154. }
  155. contractAddr := receipt.ContractAddr
  156. if len(contractAddr) == 0 {
  157. t.Fatal("Creates contract but resulting address is empty")
  158. }
  159. // allow it to get mined
  160. waitForEvent(t, con, eid, true, func() {
  161. }, func(eid string, b []byte) error {
  162. return nil
  163. })
  164. mempoolCount = 0
  165. // run a call through the contract
  166. data := []byte{}
  167. expected := []byte{0xb}
  168. callContract(t, client, contractAddr, data, expected)
  169. }