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.

261 lines
7.7 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. var doNothing = func(eid string, b []byte) error { return nil }
  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.ChainID != chainID {
  18. t.Fatal(fmt.Errorf("ChainID mismatch: got %s expected %s",
  19. resp.ChainID, chainID))
  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, user[0].Address)
  34. if acc == nil {
  35. t.Fatalf("Account was nil")
  36. }
  37. if bytes.Compare(acc.Address, user[0].Address) != 0 {
  38. t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, user[0].Address)
  39. }
  40. }
  41. func testSignedTx(t *testing.T, typ string) {
  42. amt := uint64(100)
  43. toAddr := user[1].Address
  44. testOneSignTx(t, typ, toAddr, amt)
  45. toAddr = user[2].Address
  46. testOneSignTx(t, typ, toAddr, amt)
  47. toAddr = user[3].Address
  48. testOneSignTx(t, typ, toAddr, amt)
  49. }
  50. func testOneSignTx(t *testing.T, typ string, addr []byte, amt uint64) {
  51. tx := makeDefaultSendTx(t, typ, addr, amt)
  52. tx2 := signTx(t, typ, tx, user[0])
  53. tx2hash := account.HashSignBytes(chainID, tx2)
  54. tx.SignInput(chainID, 0, user[0])
  55. txhash := account.HashSignBytes(chainID, tx)
  56. if bytes.Compare(txhash, tx2hash) != 0 {
  57. t.Fatal("Got different signatures for signing via rpc vs tx_utils")
  58. }
  59. tx_ := signTx(t, typ, tx, user[0])
  60. tx = tx_.(*types.SendTx)
  61. checkTx(t, user[0].Address, user[0], tx)
  62. }
  63. func testBroadcastTx(t *testing.T, typ string) {
  64. amt := uint64(100)
  65. toAddr := user[1].Address
  66. tx := makeDefaultSendTxSigned(t, typ, toAddr, amt)
  67. receipt := broadcastTx(t, typ, tx)
  68. if receipt.CreatesContract > 0 {
  69. t.Fatal("This tx does not create a contract")
  70. }
  71. if len(receipt.TxHash) == 0 {
  72. t.Fatal("Failed to compute tx hash")
  73. }
  74. pool := node.MempoolReactor().Mempool
  75. txs := pool.GetProposalTxs()
  76. if len(txs) != mempoolCount {
  77. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount)
  78. }
  79. tx2 := txs[mempoolCount-1].(*types.SendTx)
  80. n, err := new(int64), new(error)
  81. buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
  82. tx.WriteSignBytes(chainID, buf1, n, err)
  83. tx2.WriteSignBytes(chainID, buf2, n, err)
  84. if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
  85. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  86. }
  87. }
  88. func testGetStorage(t *testing.T, typ string) {
  89. con := newWSCon(t)
  90. eid := types.EventStringNewBlock()
  91. subscribe(t, con, eid)
  92. defer func() {
  93. unsubscribe(t, con, eid)
  94. con.Close()
  95. }()
  96. amt, gasLim, fee := uint64(1100), uint64(1000), uint64(1000)
  97. code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
  98. tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
  99. receipt := broadcastTx(t, typ, tx)
  100. if receipt.CreatesContract == 0 {
  101. t.Fatal("This tx creates a contract")
  102. }
  103. if len(receipt.TxHash) == 0 {
  104. t.Fatal("Failed to compute tx hash")
  105. }
  106. contractAddr := receipt.ContractAddr
  107. if len(contractAddr) == 0 {
  108. t.Fatal("Creates contract but resulting address is empty")
  109. }
  110. // allow it to get mined
  111. waitForEvent(t, con, eid, true, func() {}, doNothing)
  112. mempoolCount = 0
  113. v := getStorage(t, typ, contractAddr, []byte{0x1})
  114. got := LeftPadWord256(v)
  115. expected := LeftPadWord256([]byte{0x5})
  116. if got.Compare(expected) != 0 {
  117. t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes())
  118. }
  119. }
  120. func testCallCode(t *testing.T, typ string) {
  121. client := clients[typ]
  122. // add two integers and return the result
  123. code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
  124. data := []byte{}
  125. expected := []byte{0xb}
  126. callCode(t, client, code, data, expected)
  127. // pass two ints as calldata, add, and return the result
  128. code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
  129. data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...)
  130. expected = []byte{0xb}
  131. callCode(t, client, code, data, expected)
  132. }
  133. func testCall(t *testing.T, typ string) {
  134. con := newWSCon(t)
  135. eid := types.EventStringNewBlock()
  136. subscribe(t, con, eid)
  137. defer func() {
  138. unsubscribe(t, con, eid)
  139. con.Close()
  140. }()
  141. client := clients[typ]
  142. // create the contract
  143. amt, gasLim, fee := uint64(6969), uint64(1000), uint64(1000)
  144. code, _, _ := simpleContract()
  145. tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
  146. receipt := broadcastTx(t, typ, tx)
  147. if receipt.CreatesContract == 0 {
  148. t.Fatal("This tx creates a contract")
  149. }
  150. if len(receipt.TxHash) == 0 {
  151. t.Fatal("Failed to compute tx hash")
  152. }
  153. contractAddr := receipt.ContractAddr
  154. if len(contractAddr) == 0 {
  155. t.Fatal("Creates contract but resulting address is empty")
  156. }
  157. // allow it to get mined
  158. waitForEvent(t, con, eid, true, func() {}, doNothing)
  159. mempoolCount = 0
  160. // run a call through the contract
  161. data := []byte{}
  162. expected := []byte{0xb}
  163. callContract(t, client, contractAddr, data, expected)
  164. }
  165. func testNameReg(t *testing.T, typ string) {
  166. client := clients[typ]
  167. con := newWSCon(t)
  168. eid := types.EventStringNewBlock()
  169. subscribe(t, con, eid)
  170. defer func() {
  171. unsubscribe(t, con, eid)
  172. con.Close()
  173. }()
  174. types.MinNameRegistrationPeriod = 1
  175. // register a new name, check if its there
  176. // since entries ought to be unique and these run against different clients, we append the typ
  177. name := "ye_old_domain_name_" + typ
  178. data := "if not now, when"
  179. fee := uint64(1000)
  180. numDesiredBlocks := uint64(2)
  181. amt := fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
  182. tx := makeDefaultNameTx(t, typ, name, data, amt, fee)
  183. broadcastTx(t, typ, tx)
  184. // commit block
  185. waitForEvent(t, con, eid, true, func() {}, doNothing)
  186. mempoolCount = 0
  187. entry := getNameRegEntry(t, typ, name)
  188. if entry.Data != data {
  189. t.Fatal(fmt.Sprintf("Err on entry.Data: Got %s, expected %s", entry.Data, data))
  190. }
  191. if bytes.Compare(entry.Owner, user[0].Address) != 0 {
  192. t.Fatal(fmt.Sprintf("Err on entry.Owner: Got %s, expected %s", entry.Owner, user[0].Address))
  193. }
  194. // update the data as the owner, make sure still there
  195. numDesiredBlocks = uint64(2)
  196. data = "these are amongst the things I wish to bestow upon the youth of generations come: a safe supply of honey, and a better money. For what else shall they need"
  197. amt = fee + numDesiredBlocks*types.NameCostPerByte*types.NameCostPerBlock*types.BaseEntryCost(name, data)
  198. tx = makeDefaultNameTx(t, typ, name, data, amt, fee)
  199. broadcastTx(t, typ, tx)
  200. // commit block
  201. waitForEvent(t, con, eid, true, func() {}, doNothing)
  202. mempoolCount = 0
  203. entry = getNameRegEntry(t, typ, name)
  204. if entry.Data != data {
  205. t.Fatal(fmt.Sprintf("Err on entry.Data: Got %s, expected %s", entry.Data, data))
  206. }
  207. // try to update as non owner, should fail
  208. nonce := getNonce(t, typ, user[1].Address)
  209. data2 := "this is not my beautiful house"
  210. tx = types.NewNameTxWithNonce(user[1].PubKey, name, data2, amt, fee, nonce)
  211. tx.Sign(user[1])
  212. _, err := client.BroadcastTx(tx)
  213. if err == nil {
  214. t.Fatal("Expected error on NameTx")
  215. }
  216. // commit block
  217. waitForEvent(t, con, eid, true, func() {}, doNothing)
  218. // now the entry should be expired, so we can update as non owner
  219. _, err = client.BroadcastTx(tx)
  220. waitForEvent(t, con, eid, true, func() {}, doNothing)
  221. mempoolCount = 0
  222. entry = getNameRegEntry(t, typ, name)
  223. if entry.Data != data2 {
  224. t.Fatal(fmt.Sprintf("Error on entry.Data: Got %s, expected %s", entry.Data, data2))
  225. }
  226. if bytes.Compare(entry.Owner, user[1].Address) != 0 {
  227. t.Fatal(fmt.Sprintf("Err on entry.Owner: Got %s, expected %s", entry.Owner, user[1].Address))
  228. }
  229. }