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.

175 lines
5.1 KiB

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