Skip to content

Commit

Permalink
Add txsearch subcommand
Browse files Browse the repository at this point in the history
This command queries transactions from a running shuttermint node and
prints them to stdout.
  • Loading branch information
schmir committed Mar 2, 2021
1 parent b6dc22f commit 3508d0e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
2 changes: 2 additions & 0 deletions shuttermint/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package cmd implements the shuttermint subcommands
package cmd

import (
Expand Down Expand Up @@ -33,6 +34,7 @@ func init() {
rootCmd.AddCommand(config.ConfigCmd)
rootCmd.AddCommand(keyperCmd)
rootCmd.AddCommand(showCmd)
rootCmd.AddCommand(txsearchCmd)
rootCmd.AddCommand(bootstrapCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(deployCmd)
Expand Down
5 changes: 3 additions & 2 deletions shuttermint/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ var showFlags struct {
Height int64
}

// keyperCmd represents the keyper command
var showCmd = &cobra.Command{
Use: "show",
Short: "Show the internal state of a Shuttermint node",
Args: cobra.NoArgs,
Long: `This command queries transactions from a running shuttermint node and rebuilds the
internal shutter state object according to the results. It then prints the result to stdout.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
showMain()
},
Expand Down
102 changes: 102 additions & 0 deletions shuttermint/cmd/txsearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/client/http"

"github.com/brainbot-com/shutter/shuttermint/keyper/observe"
"github.com/brainbot-com/shutter/shuttermint/keyper/shutterevents"
)

var txsearchFlags struct {
ShuttermintURL string
FromHeight, ToHeight int64
}

var txsearchCmd = &cobra.Command{
Use: "txsearch",
Short: "Search for shuttermint transactions",
Long: `This command queries transactions from a running shuttermint node and prints them to
stdout.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
txsearchMain()
},
}

func init() {
txsearchCmd.PersistentFlags().StringVarP(
&txsearchFlags.ShuttermintURL,
"shuttermint-url",
"s",
"http://localhost:26657",
"Shuttermint RPC URL",
)
txsearchCmd.PersistentFlags().Int64VarP(
&txsearchFlags.FromHeight,
"from-height",
"",
0,
"search txs from this height",
)
txsearchCmd.PersistentFlags().Int64VarP(
&txsearchFlags.ToHeight,
"to-height",
"",
-1,
"search txs till this height",
)
}

func txsearchShutter(shuttermintURL string, fromHeight, toHeight int64) {
var cl client.Client
cl, err := http.New(shuttermintURL, "/websocket")
if err != nil {
panic(err)
}

s := observe.NewShutter()
if toHeight == -1 {
toHeight, err = s.LastCommittedHeight(context.Background(), cl)
if err != nil {
panic(err)
}
}

query := fmt.Sprintf("tx.height >= %d and tx.height <= %d", fromHeight, toHeight)

perPage := 100
page := 1
ctx := context.Background()
for {
res, err := cl.TxSearch(ctx, query, false, &page, &perPage, "")
if err != nil {
panic(err)
}
for _, tx := range res.Txs {
fmt.Printf("==> TX height=%d hash=%s\n", tx.Height, tx.Hash)
events := tx.TxResult.GetEvents()
for _, ev := range events {
x, err := shutterevents.MakeEvent(ev, tx.Height)
if err != nil {
log.Printf("Error: malformed event: %s ev=%+v", err, ev)
} else {
fmt.Printf(" %#v\n", x)
}
}
}
if page*perPage >= res.TotalCount {
break
}
page++
}
}

func txsearchMain() {
txsearchShutter(txsearchFlags.ShuttermintURL, txsearchFlags.FromHeight, txsearchFlags.ToHeight)
}

0 comments on commit 3508d0e

Please sign in to comment.