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.

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