From 64554b6839f2bbc6d714448a9efba5f984d68193 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 10 Jan 2019 11:00:41 -0500 Subject: [PATCH 1/6] Added ResponseHandler field to CLIContext --- client/context/broadcast.go | 4 ++++ client/context/context.go | 36 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/client/context/broadcast.go b/client/context/broadcast.go index c844e519d120..413edc4d7e04 100644 --- a/client/context/broadcast.go +++ b/client/context/broadcast.go @@ -163,6 +163,10 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast ) } + if ctx.ResponseHandler != nil { + ctx.ResponseHandler(res) + } + io.WriteString(ctx.Output, resStr) } diff --git a/client/context/context.go b/client/context/context.go index a253a88fae55..01f35bfa15f1 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -3,6 +3,7 @@ package context import ( "bytes" "fmt" + "github.com/tendermint/tendermint/rpc/core/types" "io" "os" "path/filepath" @@ -26,28 +27,31 @@ import ( var verifier tmlite.Verifier +type ResponseHandler = func(res *core_types.ResultBroadcastTxCommit) + // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. type CLIContext struct { - Codec *codec.Codec - AccDecoder auth.AccountDecoder - Client rpcclient.Client - Output io.Writer + Codec *codec.Codec + AccDecoder auth.AccountDecoder + Client rpcclient.Client + Output io.Writer OutputFormat string - Height int64 - NodeURI string - From string - AccountStore string - TrustNode bool - UseLedger bool - Async bool - PrintResponse bool - Verifier tmlite.Verifier - Simulate bool - GenerateOnly bool + Height int64 + NodeURI string + From string + AccountStore string + TrustNode bool + UseLedger bool + Async bool + PrintResponse bool + ResponseHandler ResponseHandler + Verifier tmlite.Verifier + Simulate bool + GenerateOnly bool FromAddress sdk.AccAddress FromName string - Indent bool + Indent bool } // NewCLIContext returns a new initialized CLIContext with parameters from the From 465c660fa55145d4e4e9283945db10ad59f598b6 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 10 Jan 2019 13:43:29 -0500 Subject: [PATCH 2/6] Change ResponseHandler -> ResponsePrinter, set to resStr --- client/context/broadcast.go | 14 +++++++------- client/context/context.go | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/client/context/broadcast.go b/client/context/broadcast.go index 413edc4d7e04..9c5993f79c19 100644 --- a/client/context/broadcast.go +++ b/client/context/broadcast.go @@ -158,13 +158,13 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast resStr := fmt.Sprintf("Committed at block %d (tx hash: %s)\n", res.Height, res.Hash.String()) if ctx.PrintResponse { - resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n", - res.Height, res.Hash.String(), res.DeliverTx, - ) - } - - if ctx.ResponseHandler != nil { - ctx.ResponseHandler(res) + if ctx.ResponsePrinter != nil { + resStr = ctx.ResponsePrinter(res) + } else { + resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n", + res.Height, res.Hash.String(), res.DeliverTx, + ) + } } io.WriteString(ctx.Output, resStr) diff --git a/client/context/context.go b/client/context/context.go index 01f35bfa15f1..1332cb517ec6 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -27,7 +27,7 @@ import ( var verifier tmlite.Verifier -type ResponseHandler = func(res *core_types.ResultBroadcastTxCommit) +type ResponsePrinter = func(res *core_types.ResultBroadcastTxCommit) string // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. @@ -45,7 +45,7 @@ type CLIContext struct { UseLedger bool Async bool PrintResponse bool - ResponseHandler ResponseHandler + ResponsePrinter ResponsePrinter Verifier tmlite.Verifier Simulate bool GenerateOnly bool From 707f364467c405166daa6a02a61937310e155e8b Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 29 Jan 2019 13:55:24 -0500 Subject: [PATCH 3/6] Change ResponsePrinter -> ResponseHandler making the override more generic --- client/context/broadcast.go | 14 +++++++------- client/context/context.go | 7 +++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/client/context/broadcast.go b/client/context/broadcast.go index 9c5993f79c19..971140363812 100644 --- a/client/context/broadcast.go +++ b/client/context/broadcast.go @@ -131,6 +131,10 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast return res, err } + if ctx.ResponseHandler != nil { + return ctx.ResponseHandler(res) + } + if ctx.OutputFormat == "json" { // Since JSON is intended for automated scripts, always include response in // JSON mode. @@ -158,13 +162,9 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast resStr := fmt.Sprintf("Committed at block %d (tx hash: %s)\n", res.Height, res.Hash.String()) if ctx.PrintResponse { - if ctx.ResponsePrinter != nil { - resStr = ctx.ResponsePrinter(res) - } else { - resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n", - res.Height, res.Hash.String(), res.DeliverTx, - ) - } + resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n", + res.Height, res.Hash.String(), res.DeliverTx, + ) } io.WriteString(ctx.Output, resStr) diff --git a/client/context/context.go b/client/context/context.go index 1332cb517ec6..9d94df62fb03 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -3,7 +3,6 @@ package context import ( "bytes" "fmt" - "github.com/tendermint/tendermint/rpc/core/types" "io" "os" "path/filepath" @@ -21,14 +20,13 @@ import ( tmlite "github.com/tendermint/tendermint/lite" tmliteProxy "github.com/tendermint/tendermint/lite/proxy" rpcclient "github.com/tendermint/tendermint/rpc/client" + ctypes "github.com/tendermint/tendermint/rpc/core/types" sdk "github.com/cosmos/cosmos-sdk/types" ) var verifier tmlite.Verifier -type ResponsePrinter = func(res *core_types.ResultBroadcastTxCommit) string - // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. type CLIContext struct { @@ -45,7 +43,8 @@ type CLIContext struct { UseLedger bool Async bool PrintResponse bool - ResponsePrinter ResponsePrinter + // Overrides the default CLI transaction response handling + ResponseHandler func(res *ctypes.ResultBroadcastTxCommit) (*ctypes.ResultBroadcastTxCommit, error) Verifier tmlite.Verifier Simulate bool GenerateOnly bool From 6e1f27eea1794bb580b092029e8ececcd7990001 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 29 Jan 2019 16:13:55 -0500 Subject: [PATCH 4/6] Update docstring, cleanup indentation --- client/context/context.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index 9d94df62fb03..d2e666e953a0 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -30,26 +30,26 @@ var verifier tmlite.Verifier // CLIContext implements a typical CLI context created in SDK modules for // transaction handling and queries. type CLIContext struct { - Codec *codec.Codec - AccDecoder auth.AccountDecoder - Client rpcclient.Client - Output io.Writer + Codec *codec.Codec + AccDecoder auth.AccountDecoder + Client rpcclient.Client + Output io.Writer OutputFormat string - Height int64 - NodeURI string - From string - AccountStore string - TrustNode bool - UseLedger bool - Async bool - PrintResponse bool - // Overrides the default CLI transaction response handling + Height int64 + NodeURI string + From string + AccountStore string + TrustNode bool + UseLedger bool + Async bool + PrintResponse bool + // Overrides the default CLI transaction response handling with a custom handler ResponseHandler func(res *ctypes.ResultBroadcastTxCommit) (*ctypes.ResultBroadcastTxCommit, error) Verifier tmlite.Verifier Simulate bool GenerateOnly bool - FromAddress sdk.AccAddress - FromName string + FromAddress sdk.AccAddress + FromName string Indent bool } From b2410f6ba7c04dd8d1d2af8f968c9ef133755bec Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 29 Jan 2019 17:27:35 -0500 Subject: [PATCH 5/6] Simplified ResponseHandler - no need for it to handle the return value of broadcastTxCommit --- client/context/broadcast.go | 3 ++- client/context/context.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/client/context/broadcast.go b/client/context/broadcast.go index 971140363812..5cb1adb8e56e 100644 --- a/client/context/broadcast.go +++ b/client/context/broadcast.go @@ -132,7 +132,8 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast } if ctx.ResponseHandler != nil { - return ctx.ResponseHandler(res) + ctx.ResponseHandler(res) + return res, nil } if ctx.OutputFormat == "json" { diff --git a/client/context/context.go b/client/context/context.go index d2e666e953a0..c5a42339fcb4 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -44,7 +44,7 @@ type CLIContext struct { Async bool PrintResponse bool // Overrides the default CLI transaction response handling with a custom handler - ResponseHandler func(res *ctypes.ResultBroadcastTxCommit) (*ctypes.ResultBroadcastTxCommit, error) + ResponseHandler func(res *ctypes.ResultBroadcastTxCommit) Verifier tmlite.Verifier Simulate bool GenerateOnly bool From d0006b5272baecb352e3ff58984a7d372474c7bb Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Wed, 30 Jan 2019 14:18:41 -0500 Subject: [PATCH 6/6] Renamed ResponseHandler -> TxResponseHandler. Created DefaultTxResponseHandler from body of broadcastTxCommit. --- client/context/broadcast.go | 17 +++++++++++------ client/context/context.go | 14 +++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/client/context/broadcast.go b/client/context/broadcast.go index 5cb1adb8e56e..8be256b53aac 100644 --- a/client/context/broadcast.go +++ b/client/context/broadcast.go @@ -131,11 +131,16 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast return res, err } - if ctx.ResponseHandler != nil { - ctx.ResponseHandler(res) - return res, nil + if ctx.TxResponseHandler != nil { + err = ctx.TxResponseHandler(res) + } else { + err = ctx.DefaultTxResponseHandler(res) } + return res, err + +} +func (ctx CLIContext) DefaultTxResponseHandler(res *ctypes.ResultBroadcastTxCommit) error { if ctx.OutputFormat == "json" { // Since JSON is intended for automated scripts, always include response in // JSON mode. @@ -149,14 +154,14 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast resJSON := toJSON{res.Height, res.Hash.String(), res.DeliverTx} bz, err := ctx.Codec.MarshalJSON(resJSON) if err != nil { - return res, err + return err } ctx.Output.Write(bz) io.WriteString(ctx.Output, "\n") } - return res, nil + return nil } if ctx.Output != nil { @@ -171,5 +176,5 @@ func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcast io.WriteString(ctx.Output, resStr) } - return res, nil + return nil } diff --git a/client/context/context.go b/client/context/context.go index c5a42339fcb4..709f0c7a5c09 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -44,13 +44,13 @@ type CLIContext struct { Async bool PrintResponse bool // Overrides the default CLI transaction response handling with a custom handler - ResponseHandler func(res *ctypes.ResultBroadcastTxCommit) - Verifier tmlite.Verifier - Simulate bool - GenerateOnly bool - FromAddress sdk.AccAddress - FromName string - Indent bool + TxResponseHandler func(res *ctypes.ResultBroadcastTxCommit) error + Verifier tmlite.Verifier + Simulate bool + GenerateOnly bool + FromAddress sdk.AccAddress + FromName string + Indent bool } // NewCLIContext returns a new initialized CLIContext with parameters from the