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.

127 lines
2.6 KiB

9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-wire"
  8. "github.com/tendermint/tmsp/types"
  9. "github.com/codegangsta/cli"
  10. )
  11. func main() {
  12. app := cli.NewApp()
  13. app.Name = "cli"
  14. app.Usage = "cli [command] [args...]"
  15. app.Flags = []cli.Flag{
  16. cli.StringFlag{
  17. Name: "address",
  18. Value: "tcp://127.0.0.1:8080",
  19. Usage: "address of application socket",
  20. },
  21. }
  22. app.Commands = []cli.Command{
  23. {
  24. Name: "append_tx",
  25. Usage: "Append a new tx to application",
  26. Action: func(c *cli.Context) {
  27. cmdAppendTx(c)
  28. },
  29. },
  30. {
  31. Name: "get_hash",
  32. Usage: "Get application Merkle root hash",
  33. Action: func(c *cli.Context) {
  34. cmdGetHash(c)
  35. },
  36. },
  37. {
  38. Name: "commit",
  39. Usage: "Commit the application state",
  40. Action: func(c *cli.Context) {
  41. cmdCommit(c)
  42. },
  43. },
  44. {
  45. Name: "rollback",
  46. Usage: "Roll back the application state to the latest commit",
  47. Action: func(c *cli.Context) {
  48. cmdRollback(c)
  49. },
  50. },
  51. }
  52. app.Run(os.Args)
  53. }
  54. //--------------------------------------------------------------------------------
  55. // Append a new tx to application
  56. func cmdAppendTx(c *cli.Context) {
  57. args := c.Args() // Args to AppendTx
  58. conn, err := Connect(c.GlobalString("address"))
  59. if err != nil {
  60. Exit(err.Error())
  61. }
  62. res, err := write(conn, types.RequestAppendTx{[]byte(args[0])})
  63. if err != nil {
  64. Exit(err.Error())
  65. }
  66. fmt.Println("Sent tx:", args[0], "response:", res)
  67. }
  68. // Get application Merkle root hash
  69. func cmdGetHash(c *cli.Context) {
  70. conn, err := Connect(c.GlobalString("address"))
  71. if err != nil {
  72. Exit(err.Error())
  73. }
  74. res, err := write(conn, types.RequestGetHash{})
  75. if err != nil {
  76. Exit(err.Error())
  77. }
  78. fmt.Println("Got hash:", Fmt("%X", res.(types.ResponseGetHash).Hash))
  79. }
  80. // Commit the application state
  81. func cmdCommit(c *cli.Context) {
  82. conn, err := Connect(c.GlobalString("address"))
  83. if err != nil {
  84. Exit(err.Error())
  85. }
  86. _, err = write(conn, types.RequestCommit{})
  87. if err != nil {
  88. Exit(err.Error())
  89. }
  90. fmt.Println("Committed.")
  91. }
  92. // Roll back the application state to the latest commit
  93. func cmdRollback(c *cli.Context) {
  94. conn, err := Connect(c.GlobalString("address"))
  95. if err != nil {
  96. Exit(err.Error())
  97. }
  98. _, err = write(conn, types.RequestRollback{})
  99. if err != nil {
  100. Exit(err.Error())
  101. }
  102. fmt.Println("Rolled back.")
  103. }
  104. //--------------------------------------------------------------------------------
  105. func write(conn net.Conn, req types.Request) (types.Response, error) {
  106. var n int
  107. var err error
  108. wire.WriteBinary(req, conn, &n, &err)
  109. if err != nil {
  110. return nil, err
  111. }
  112. var res types.Response
  113. wire.ReadBinaryPtr(&res, conn, 0, &n, &err)
  114. return res, err
  115. }