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.

200 lines
6.2 KiB

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