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.

92 lines
2.3 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "os"
  5. "time"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-process"
  8. "github.com/tendermint/tmsp/client"
  9. "github.com/tendermint/tmsp/types"
  10. )
  11. //----------------------------------------
  12. func StartApp(tmspApp string) *process.Process {
  13. // Start the app
  14. //outBuf := NewBufferCloser(nil)
  15. proc, err := process.StartProcess("tmsp_app",
  16. "",
  17. "bash",
  18. []string{"-c", tmspApp},
  19. nil,
  20. os.Stdout,
  21. )
  22. if err != nil {
  23. panic("running tmsp_app: " + err.Error())
  24. }
  25. // TODO a better way to handle this?
  26. time.Sleep(time.Second)
  27. return proc
  28. }
  29. func StartClient(tmspType string) tmspcli.Client {
  30. // Start client
  31. client, err := tmspcli.NewClient("tcp://127.0.0.1:46658", tmspType, true)
  32. if err != nil {
  33. panic("connecting to tmsp_app: " + err.Error())
  34. }
  35. return client
  36. }
  37. func SetOption(client tmspcli.Client, key, value string) {
  38. res := client.SetOptionSync(key, value)
  39. _, _, log := res.Code, res.Data, res.Log
  40. if res.IsErr() {
  41. panic(Fmt("setting %v=%v: \nlog: %v", key, value, log))
  42. }
  43. }
  44. func Commit(client tmspcli.Client, hashExp []byte) {
  45. res := client.CommitSync()
  46. _, data, log := res.Code, res.Data, res.Log
  47. if res.IsErr() {
  48. panic(Fmt("committing %v\nlog: %v", log))
  49. }
  50. if !bytes.Equal(res.Data, hashExp) {
  51. panic(Fmt("Commit hash was unexpected. Got %X expected %X",
  52. data, hashExp))
  53. }
  54. }
  55. func AppendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  56. res := client.AppendTxSync(txBytes)
  57. code, data, log := res.Code, res.Data, res.Log
  58. if code != codeExp {
  59. panic(Fmt("AppendTx response code was unexpected. Got %v expected %v. Log: %v",
  60. code, codeExp, log))
  61. }
  62. if !bytes.Equal(data, dataExp) {
  63. panic(Fmt("AppendTx response data was unexpected. Got %X expected %X",
  64. data, dataExp))
  65. }
  66. }
  67. func CheckTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  68. res := client.CheckTxSync(txBytes)
  69. code, data, log := res.Code, res.Data, res.Log
  70. if res.IsErr() {
  71. panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, log))
  72. }
  73. if code != codeExp {
  74. panic(Fmt("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
  75. code, codeExp, log))
  76. }
  77. if !bytes.Equal(data, dataExp) {
  78. panic(Fmt("CheckTx response data was unexpected. Got %X expected %X",
  79. data, dataExp))
  80. }
  81. }