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.

277 lines
8.2 KiB

  1. package rpctest
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/tendermint/tendermint/account"
  9. . "github.com/tendermint/tendermint/common"
  10. "github.com/tendermint/tendermint/consensus"
  11. nm "github.com/tendermint/tendermint/node"
  12. "github.com/tendermint/tendermint/p2p"
  13. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  14. cclient "github.com/tendermint/tendermint/rpc/core_client"
  15. "github.com/tendermint/tendermint/state"
  16. "github.com/tendermint/tendermint/types"
  17. )
  18. // global variables for use across all tests
  19. var (
  20. rpcAddr = "127.0.0.1:36657" // Not 46657
  21. requestAddr = "http://" + rpcAddr + "/"
  22. websocketAddr = "ws://" + rpcAddr + "/events"
  23. node *nm.Node
  24. mempoolCount = 0
  25. // make keys
  26. userPriv = "C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
  27. user = makeUsers(2)
  28. chainID string
  29. clients = map[string]cclient.Client{
  30. "JSONRPC": cclient.NewClient(requestAddr, "JSONRPC"),
  31. "HTTP": cclient.NewClient(requestAddr, "HTTP"),
  32. }
  33. )
  34. func makeUsers(n int) []*account.PrivAccount {
  35. accounts := []*account.PrivAccount{}
  36. for i := 0; i < n; i++ {
  37. secret := []byte("mysecret" + strconv.Itoa(i))
  38. user := account.GenPrivAccountFromSecret(secret)
  39. accounts = append(accounts, user)
  40. }
  41. // include our validator
  42. var byteKey [64]byte
  43. userPrivByteSlice, _ := hex.DecodeString(userPriv)
  44. copy(byteKey[:], userPrivByteSlice)
  45. privAcc := account.GenPrivAccountFromKey(byteKey)
  46. accounts[0] = privAcc
  47. return accounts
  48. }
  49. // create a new node and sleep forever
  50. func newNode(ready chan struct{}) {
  51. // Create & start node
  52. node = nm.NewNode()
  53. l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"), false)
  54. node.AddListener(l)
  55. node.Start()
  56. // Run the RPC server.
  57. node.StartRPC()
  58. ready <- struct{}{}
  59. // Sleep forever
  60. ch := make(chan struct{})
  61. <-ch
  62. }
  63. // initialize config and create new node
  64. func init() {
  65. chainID = config.GetString("chain_id")
  66. // Save new priv_validator file.
  67. priv := &state.PrivValidator{
  68. Address: user[0].Address,
  69. PubKey: account.PubKeyEd25519(user[0].PubKey.(account.PubKeyEd25519)),
  70. PrivKey: account.PrivKeyEd25519(user[0].PrivKey.(account.PrivKeyEd25519)),
  71. }
  72. priv.SetFile(config.GetString("priv_validator_file"))
  73. priv.Save()
  74. consensus.RoundDuration0 = 2 * time.Second
  75. consensus.RoundDurationDelta = 1 * time.Second
  76. // start a node
  77. ready := make(chan struct{})
  78. go newNode(ready)
  79. <-ready
  80. }
  81. //-------------------------------------------------------------------------------
  82. // some default transaction functions
  83. func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt uint64) *types.SendTx {
  84. nonce := getNonce(t, typ, user[0].Address)
  85. tx := types.NewSendTx()
  86. tx.AddInputWithNonce(user[0].PubKey, amt, nonce)
  87. tx.AddOutput(addr, amt)
  88. return tx
  89. }
  90. func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt uint64) *types.SendTx {
  91. tx := makeDefaultSendTx(t, typ, addr, amt)
  92. tx.SignInput(chainID, 0, user[0])
  93. return tx
  94. }
  95. func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee uint64) *types.CallTx {
  96. nonce := getNonce(t, typ, user[0].Address)
  97. tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce)
  98. tx.Sign(chainID, user[0])
  99. return tx
  100. }
  101. //-------------------------------------------------------------------------------
  102. // rpc call wrappers (fail on err)
  103. // get an account's nonce
  104. func getNonce(t *testing.T, typ string, addr []byte) uint64 {
  105. client := clients[typ]
  106. ac, err := client.GetAccount(addr)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. if ac.Account == nil {
  111. return 0
  112. }
  113. return uint64(ac.Account.Sequence)
  114. }
  115. // get the account
  116. func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
  117. client := clients[typ]
  118. ac, err := client.GetAccount(addr)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. return ac.Account
  123. }
  124. // sign transaction
  125. func signTx(t *testing.T, typ string, tx types.Tx, privAcc *account.PrivAccount) types.Tx {
  126. client := clients[typ]
  127. resp, err := client.SignTx(tx, []*account.PrivAccount{privAcc})
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. return resp.Tx
  132. }
  133. // broadcast transaction
  134. func broadcastTx(t *testing.T, typ string, tx types.Tx) ctypes.Receipt {
  135. client := clients[typ]
  136. resp, err := client.BroadcastTx(tx)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. mempoolCount += 1
  141. return resp.Receipt
  142. }
  143. // dump all storage for an account. currently unused
  144. func dumpStorage(t *testing.T, addr []byte) ctypes.ResponseDumpStorage {
  145. client := clients["HTTP"]
  146. resp, err := client.DumpStorage(addr)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. return *resp
  151. }
  152. func getStorage(t *testing.T, typ string, addr, key []byte) []byte {
  153. client := clients[typ]
  154. resp, err := client.GetStorage(addr, key)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. return resp.Value
  159. }
  160. func callCode(t *testing.T, client cclient.Client, code, data, expected []byte) {
  161. resp, err := client.CallCode(code, data)
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. ret := resp.Return
  166. // NOTE: we don't flip memory when it comes out of RETURN (?!)
  167. if bytes.Compare(ret, LeftPadWord256(expected).Bytes()) != 0 {
  168. t.Fatalf("Conflicting return value. Got %x, expected %x", ret, expected)
  169. }
  170. }
  171. func callContract(t *testing.T, client cclient.Client, address, data, expected []byte) {
  172. resp, err := client.Call(address, data)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. ret := resp.Return
  177. // NOTE: we don't flip memory when it comes out of RETURN (?!)
  178. if bytes.Compare(ret, LeftPadWord256(expected).Bytes()) != 0 {
  179. t.Fatalf("Conflicting return value. Got %x, expected %x", ret, expected)
  180. }
  181. }
  182. //--------------------------------------------------------------------------------
  183. // utility verification function
  184. func checkTx(t *testing.T, fromAddr []byte, priv *account.PrivAccount, tx *types.SendTx) {
  185. if bytes.Compare(tx.Inputs[0].Address, fromAddr) != 0 {
  186. t.Fatal("Tx input addresses don't match!")
  187. }
  188. signBytes := account.SignBytes(chainID, tx)
  189. in := tx.Inputs[0] //(*types.SendTx).Inputs[0]
  190. if err := in.ValidateBasic(); err != nil {
  191. t.Fatal(err)
  192. }
  193. // Check signatures
  194. // acc := getAccount(t, byteAddr)
  195. // NOTE: using the acc here instead of the in fails; it is nil.
  196. if !in.PubKey.VerifyBytes(signBytes, in.Signature) {
  197. t.Fatal(types.ErrTxInvalidSignature)
  198. }
  199. }
  200. // simple contract returns 5 + 6 = 0xb
  201. func simpleContract() ([]byte, []byte, []byte) {
  202. // this is the code we want to run when the contract is called
  203. contractCode := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
  204. // the is the code we need to return the contractCode when the contract is initialized
  205. lenCode := len(contractCode)
  206. // push code to the stack
  207. //code := append([]byte{byte(0x60 + lenCode - 1)}, RightPadWord256(contractCode).Bytes()...)
  208. code := append([]byte{0x7f}, RightPadWord256(contractCode).Bytes()...)
  209. // store it in memory
  210. code = append(code, []byte{0x60, 0x0, 0x52}...)
  211. // return whats in memory
  212. //code = append(code, []byte{0x60, byte(32 - lenCode), 0x60, byte(lenCode), 0xf3}...)
  213. code = append(code, []byte{0x60, byte(lenCode), 0x60, 0x0, 0xf3}...)
  214. // return init code, contract code, expected return
  215. return code, contractCode, LeftPadBytes([]byte{0xb}, 32)
  216. }
  217. // simple call contract calls another contract
  218. func simpleCallContract(addr []byte) ([]byte, []byte, []byte) {
  219. gas1, gas2 := byte(0x1), byte(0x1)
  220. value := byte(0x1)
  221. inOff, inSize := byte(0x0), byte(0x0) // no call data
  222. retOff, retSize := byte(0x0), byte(0x20)
  223. // this is the code we want to run (call a contract and return)
  224. contractCode := []byte{0x60, retSize, 0x60, retOff, 0x60, inSize, 0x60, inOff, 0x60, value, 0x73}
  225. contractCode = append(contractCode, addr...)
  226. contractCode = append(contractCode, []byte{0x61, gas1, gas2, 0xf1, 0x60, 0x20, 0x60, 0x0, 0xf3}...)
  227. // the is the code we need to return; the contractCode when the contract is initialized
  228. // it should copy the code from the input into memory
  229. lenCode := len(contractCode)
  230. memOff := byte(0x0)
  231. inOff = byte(0xc) // length of code before codeContract
  232. length := byte(lenCode)
  233. code := []byte{0x60, length, 0x60, inOff, 0x60, memOff, 0x37}
  234. // return whats in memory
  235. code = append(code, []byte{0x60, byte(lenCode), 0x60, 0x0, 0xf3}...)
  236. code = append(code, contractCode...)
  237. // return init code, contract code, expected return
  238. return code, contractCode, LeftPadBytes([]byte{0xb}, 32)
  239. }