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.

176 lines
5.1 KiB

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