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.

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