|
@ -1,9 +1,13 @@ |
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bufio" |
|
|
|
|
|
"encoding/hex" |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"io" |
|
|
"net" |
|
|
"net" |
|
|
"os" |
|
|
"os" |
|
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
. "github.com/tendermint/go-common" |
|
|
. "github.com/tendermint/go-common" |
|
|
"github.com/tendermint/go-wire" |
|
|
"github.com/tendermint/go-wire" |
|
@ -12,6 +16,9 @@ import ( |
|
|
"github.com/codegangsta/cli" |
|
|
"github.com/codegangsta/cli" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// connection is a global variable so it can be reused by the console
|
|
|
|
|
|
var conn net.Conn |
|
|
|
|
|
|
|
|
func main() { |
|
|
func main() { |
|
|
app := cli.NewApp() |
|
|
app := cli.NewApp() |
|
|
app.Name = "cli" |
|
|
app.Name = "cli" |
|
@ -24,6 +31,34 @@ func main() { |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
app.Commands = []cli.Command{ |
|
|
app.Commands = []cli.Command{ |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "batch", |
|
|
|
|
|
Usage: "Run a batch of tmsp commands against an application", |
|
|
|
|
|
Action: func(c *cli.Context) { |
|
|
|
|
|
cmdBatch(app, c) |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "console", |
|
|
|
|
|
Usage: "Start an interactive tmsp console for multiple commands", |
|
|
|
|
|
Action: func(c *cli.Context) { |
|
|
|
|
|
cmdConsole(app, c) |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "info", |
|
|
|
|
|
Usage: "Get some info about the application", |
|
|
|
|
|
Action: func(c *cli.Context) { |
|
|
|
|
|
cmdInfo(c) |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "set_option", |
|
|
|
|
|
Usage: "Set an option on the application", |
|
|
|
|
|
Action: func(c *cli.Context) { |
|
|
|
|
|
cmdSetOption(c) |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
{ |
|
|
{ |
|
|
Name: "append_tx", |
|
|
Name: "append_tx", |
|
|
Usage: "Append a new tx to application", |
|
|
Usage: "Append a new tx to application", |
|
@ -53,46 +88,115 @@ func main() { |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
app.Before = before |
|
|
app.Run(os.Args) |
|
|
app.Run(os.Args) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func before(c *cli.Context) error { |
|
|
|
|
|
if conn == nil { |
|
|
|
|
|
var err error |
|
|
|
|
|
conn, err = Connect(c.GlobalString("address")) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
// Append a new tx to application
|
|
|
|
|
|
func cmdAppendTx(c *cli.Context) { |
|
|
|
|
|
args := c.Args() // Args to AppendTx
|
|
|
|
|
|
conn, err := Connect(c.GlobalString("address")) |
|
|
|
|
|
|
|
|
func cmdBatch(app *cli.App, c *cli.Context) { |
|
|
|
|
|
bufReader := bufio.NewReader(os.Stdin) |
|
|
|
|
|
for { |
|
|
|
|
|
line, more, err := bufReader.ReadLine() |
|
|
|
|
|
if more { |
|
|
|
|
|
Exit("input line is too long") |
|
|
|
|
|
} else if err == io.EOF { |
|
|
|
|
|
break |
|
|
|
|
|
} else if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
args := []string{"tmsp"} |
|
|
|
|
|
args = append(args, strings.Split(string(line), " ")...) |
|
|
|
|
|
app.Run(args) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func cmdConsole(app *cli.App, c *cli.Context) { |
|
|
|
|
|
for { |
|
|
|
|
|
fmt.Printf("> ") |
|
|
|
|
|
bufReader := bufio.NewReader(os.Stdin) |
|
|
|
|
|
line, more, err := bufReader.ReadLine() |
|
|
|
|
|
if more { |
|
|
|
|
|
Exit("input is too long") |
|
|
|
|
|
} else if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
args := []string{"tmsp"} |
|
|
|
|
|
args = append(args, strings.Split(string(line), " ")...) |
|
|
|
|
|
app.Run(args) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get some info from the application
|
|
|
|
|
|
func cmdInfo(c *cli.Context) { |
|
|
|
|
|
res, err := makeRequest(conn, types.RequestInfo{}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
|
res, err := makeRequest(conn, types.RequestAppendTx{[]byte(args[0])}) |
|
|
|
|
|
|
|
|
fmt.Println(res) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Set an option on the application
|
|
|
|
|
|
func cmdSetOption(c *cli.Context) { |
|
|
|
|
|
args := c.Args() |
|
|
|
|
|
if len(args) != 2 { |
|
|
|
|
|
Exit("set_option takes 2 arguments (key, value)") |
|
|
|
|
|
} |
|
|
|
|
|
_, err := makeRequest(conn, types.RequestSetOption{args[0], args[1]}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
|
fmt.Println("Sent tx:", args[0], "response:", res) |
|
|
|
|
|
|
|
|
fmt.Printf("%s=%s\n", args[0], args[1]) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get application Merkle root hash
|
|
|
|
|
|
func cmdGetHash(c *cli.Context) { |
|
|
|
|
|
conn, err := Connect(c.GlobalString("address")) |
|
|
|
|
|
|
|
|
// Append a new tx to application
|
|
|
|
|
|
func cmdAppendTx(c *cli.Context) { |
|
|
|
|
|
args := c.Args() |
|
|
|
|
|
if len(args) != 1 { |
|
|
|
|
|
Exit("append_tx takes 1 argument") |
|
|
|
|
|
} |
|
|
|
|
|
txString := args[0] |
|
|
|
|
|
tx := []byte(txString) |
|
|
|
|
|
if len(txString) > 2 && strings.HasPrefix(txString, "0x") { |
|
|
|
|
|
var err error |
|
|
|
|
|
tx, err = hex.DecodeString(txString[2:]) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res, err := makeRequest(conn, types.RequestAppendTx{tx}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
fmt.Println("Sent tx:", txString, "response:", res) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get application Merkle root hash
|
|
|
|
|
|
func cmdGetHash(c *cli.Context) { |
|
|
res, err := makeRequest(conn, types.RequestGetHash{}) |
|
|
res, err := makeRequest(conn, types.RequestGetHash{}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
|
fmt.Println("Got hash:", Fmt("%X", res.(types.ResponseGetHash).Hash)) |
|
|
|
|
|
|
|
|
fmt.Printf("%X\n", res.(types.ResponseGetHash).Hash) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Commit the application state
|
|
|
// Commit the application state
|
|
|
func cmdCommit(c *cli.Context) { |
|
|
func cmdCommit(c *cli.Context) { |
|
|
conn, err := Connect(c.GlobalString("address")) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
_, err = makeRequest(conn, types.RequestCommit{}) |
|
|
|
|
|
|
|
|
_, err := makeRequest(conn, types.RequestCommit{}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
@ -101,11 +205,7 @@ func cmdCommit(c *cli.Context) { |
|
|
|
|
|
|
|
|
// Roll back the application state to the latest commit
|
|
|
// Roll back the application state to the latest commit
|
|
|
func cmdRollback(c *cli.Context) { |
|
|
func cmdRollback(c *cli.Context) { |
|
|
conn, err := Connect(c.GlobalString("address")) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Exit(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
_, err = makeRequest(conn, types.RequestRollback{}) |
|
|
|
|
|
|
|
|
_, err := makeRequest(conn, types.RequestRollback{}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Exit(err.Error()) |
|
|
Exit(err.Error()) |
|
|
} |
|
|
} |
|
|