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.

91 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. "bash",
  17. []string{"-c", tmspApp},
  18. nil,
  19. os.Stdout,
  20. )
  21. if err != nil {
  22. panic("running tmsp_app: " + err.Error())
  23. }
  24. // TODO a better way to handle this?
  25. time.Sleep(time.Second)
  26. return proc
  27. }
  28. func StartClient(tmspType string) tmspcli.Client {
  29. // Start client
  30. client, err := tmspcli.NewClient("tcp://127.0.0.1:46658", tmspType, true)
  31. if err != nil {
  32. panic("connecting to tmsp_app: " + err.Error())
  33. }
  34. return client
  35. }
  36. func SetOption(client tmspcli.Client, key, value string) {
  37. res := client.SetOptionSync(key, value)
  38. _, _, log := res.Code, res.Data, res.Log
  39. if res.IsErr() {
  40. panic(Fmt("setting %v=%v: \nlog: %v", key, value, log))
  41. }
  42. }
  43. func Commit(client tmspcli.Client, hashExp []byte) {
  44. res := client.CommitSync()
  45. _, data, log := res.Code, res.Data, res.Log
  46. if res.IsErr() {
  47. panic(Fmt("committing %v\nlog: %v", log))
  48. }
  49. if !bytes.Equal(res.Data, hashExp) {
  50. panic(Fmt("Commit hash was unexpected. Got %X expected %X",
  51. data, hashExp))
  52. }
  53. }
  54. func AppendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  55. res := client.AppendTxSync(txBytes)
  56. code, data, log := res.Code, res.Data, res.Log
  57. if code != codeExp {
  58. panic(Fmt("AppendTx response code was unexpected. Got %v expected %v. Log: %v",
  59. code, codeExp, log))
  60. }
  61. if !bytes.Equal(data, dataExp) {
  62. panic(Fmt("AppendTx response data was unexpected. Got %X expected %X",
  63. data, dataExp))
  64. }
  65. }
  66. func CheckTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  67. res := client.CheckTxSync(txBytes)
  68. code, data, log := res.Code, res.Data, res.Log
  69. if res.IsErr() {
  70. panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, log))
  71. }
  72. if code != codeExp {
  73. panic(Fmt("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
  74. code, codeExp, log))
  75. }
  76. if !bytes.Equal(data, dataExp) {
  77. panic(Fmt("CheckTx response data was unexpected. Got %X expected %X",
  78. data, dataExp))
  79. }
  80. }