Skip to content

Commit

Permalink
fix event command and add new command event search
Browse files Browse the repository at this point in the history
  • Loading branch information
bluele committed Jul 23, 2019
1 parent 1521bc6 commit 2070ab1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
55 changes: 50 additions & 5 deletions pkg/client/cmd/contract/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func EventCMD() *cobra.Command {
Short: "This provides you to pub/sub events",
}

// common
const (
flagContractAddress = "address"
flagEventName = "event"
Expand All @@ -46,7 +47,7 @@ func EventCMD() *cobra.Command {
}
defer cl.Stop()
id := common.RandStr(8)
q := fmt.Sprintf("contract.address='%v' AND event.name='%v'", viper.GetString(flagContractAddress), viper.GetString(flagEventName))
q := fmt.Sprintf("tm.event='Tx' AND contract.address='%v' AND contract.event.name='%v'", viper.GetString(flagContractAddress), viper.GetString(flagEventName))
fmt.Printf("subscription-id=%#v query=%#v\n", id, q)
out, err := cl.Subscribe(context.Background(), id, q)
if err != nil {
Expand All @@ -56,15 +57,17 @@ func EventCMD() *cobra.Command {
etx := ev.Data.(types.EventDataTx)
fmt.Printf("TxID=0x%x\n", etx.Tx.Hash())
for _, ev := range etx.Result.Events {
if ev.Type != "contract" {
continue
}
for _, tag := range ev.Attributes {
k := string(tag.GetKey())
if k == "event.data" {
if k := string(tag.GetKey()); k == "event.data" {
ev, err := contract.ParseEventData(tag.GetValue())
if err != nil {
return err
}
fmt.Println(ev.String())
} else if k == "event.name" || k == "contract.address" {
} else if k == "event.name" || k == "address" {
// skip
} else {
fmt.Printf("unknown event: %v\n", tag)
Expand All @@ -78,7 +81,49 @@ func EventCMD() *cobra.Command {
subscribeCmd.Flags().String(flagContractAddress, "", "contract address for subscription")
subscribeCmd.Flags().String(flagEventName, "", "event name for subscription")
util.CheckRequiredFlag(subscribeCmd, flagContractAddress, flagEventName)

eventCmd.AddCommand(subscribeCmd)

// search
const (
flagCount = "count"
)

var searchCmd = &cobra.Command{
Use: "search",
Short: "Search Txs using events",
RunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())
ctx, err := client.NewClientContextFromViper()
if err != nil {
return err
}
cl, err := ctx.GetNode()
if err != nil {
return err
}
q := fmt.Sprintf("contract.address='%v' AND contract.event.name='%v'", viper.GetString(flagContractAddress), viper.GetString(flagEventName))
res, err := cl.TxSearch(q, true, 0, 0)
if err != nil {
return err
}

if viper.GetBool(flagCount) {
fmt.Print(len(res.Txs))
return nil
} else {
for _, tx := range res.Txs {
fmt.Println(tx.TxResult.String())
}
}
return nil
},
}

searchCmd.Flags().String(flagContractAddress, "", "contract address for subscription")
searchCmd.Flags().String(flagEventName, "", "event name for subscription")
searchCmd.Flags().Bool(flagCount, false, "if true, only print count of txs")
util.CheckRequiredFlag(searchCmd, flagContractAddress, flagEventName)
eventCmd.AddCommand(searchCmd)

return eventCmd
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func CreateConfig(moniker, root string) (*cfg.Config, error) {
c.P2P.RecvRate = 5120000
c.P2P.SendRate = 5120000
c.Consensus.TimeoutCommit = 5000 * time.Millisecond
c.TxIndex.IndexTags = "contract.address,event.data,event.name"
c.TxIndex.IndexTags = "contract.address,contract.event.data,contract.event.name"
return c, unmarshalWithViper(viper.GetViper(), c)
}

Expand Down
9 changes: 2 additions & 7 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,8 @@ func handleContractCallTx(ctx types.Context, cm *contract.ContractManager, envm
if err != nil {
return transaction.ErrInvalidCall(transaction.DefaultCodespace, err.Error()).Result()
}
tags = append(tags, makeTag("contract.address", []byte(tx.Address.Hex())))

// var attrs []types.Attribute
// for _, tag := range tags {
// attrs = append(attrs, types.Attribute(tag))
// }
e := types.Event{Type: "sample"}
tags = append(tags, makeTag("address", []byte(tx.Address.Hex())))
e := types.Event{Type: "contract"}
e.Attributes = tags

return types.Result{
Expand Down

0 comments on commit 2070ab1

Please sign in to comment.