44 "encoding/hex"
55 "fmt"
66 "net/http"
7+ "strings"
78
89 "github.com/gorilla/mux"
910 "github.com/spf13/cobra"
@@ -26,18 +27,18 @@ func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
2627 Short : "Matches this txhash over all committed blocks" ,
2728 Args : cobra .ExactArgs (1 ),
2829 RunE : func (cmd * cobra.Command , args []string ) error {
29- // find the key to look up the account
30- hashHexStr := args [0 ]
31-
3230 cliCtx := context .NewCLIContext ().WithCodec (cdc )
3331
34- output , err := queryTx (cdc , cliCtx , hashHexStr )
32+ output , err := queryTx (cdc , cliCtx , args [ 0 ] )
3533 if err != nil {
3634 return err
3735 }
3836
39- fmt .Println (string (output ))
40- return nil
37+ if output .Empty () {
38+ return fmt .Errorf ("No transaction found with hash %s" , args [0 ])
39+ }
40+
41+ return cliCtx .PrintOutput (output )
4142 },
4243 }
4344
@@ -48,50 +49,46 @@ func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
4849 return cmd
4950}
5051
51- func queryTx (cdc * codec.Codec , cliCtx context.CLIContext , hashHexStr string ) ([] byte , error ) {
52+ func queryTx (cdc * codec.Codec , cliCtx context.CLIContext , hashHexStr string ) (out sdk. TxResponse , err error ) {
5253 hash , err := hex .DecodeString (hashHexStr )
5354 if err != nil {
54- return nil , err
55+ return out , err
5556 }
5657
5758 node , err := cliCtx .GetNode ()
5859 if err != nil {
59- return nil , err
60+ return out , err
6061 }
6162
6263 res , err := node .Tx (hash , ! cliCtx .TrustNode )
6364 if err != nil {
64- return nil , err
65+ return out , err
6566 }
6667
6768 if ! cliCtx .TrustNode {
68- err := ValidateTxResult (cliCtx , res )
69- if err != nil {
70- return nil , err
69+ if err = ValidateTxResult (cliCtx , res ); err != nil {
70+ return out , err
7171 }
7272 }
7373
74- info , err := formatTxResult (cdc , res )
75- if err != nil {
76- return nil , err
74+ if out , err = formatTxResult (cdc , res ); err != nil {
75+ return out , err
7776 }
7877
79- if cliCtx .Indent {
80- return cdc .MarshalJSONIndent (info , "" , " " )
81- }
82- return cdc .MarshalJSON (info )
78+ return out , nil
8379}
8480
8581// ValidateTxResult performs transaction verification
8682func ValidateTxResult (cliCtx context.CLIContext , res * ctypes.ResultTx ) error {
87- check , err := cliCtx .Verify (res .Height )
88- if err != nil {
89- return err
90- }
91-
92- err = res .Proof .Validate (check .Header .DataHash )
93- if err != nil {
94- return err
83+ if ! cliCtx .TrustNode {
84+ check , err := cliCtx .Verify (res .Height )
85+ if err != nil {
86+ return err
87+ }
88+ err = res .Proof .Validate (check .Header .DataHash )
89+ if err != nil {
90+ return err
91+ }
9592 }
9693 return nil
9794}
@@ -118,17 +115,26 @@ func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) {
118115
119116// REST
120117
121- // transaction query REST handler
118+ // QueryTxRequestHandlerFn transaction query REST handler
122119func QueryTxRequestHandlerFn (cdc * codec.Codec , cliCtx context.CLIContext ) http.HandlerFunc {
123120 return func (w http.ResponseWriter , r * http.Request ) {
124121 vars := mux .Vars (r )
125122 hashHexStr := vars ["hash" ]
126123
127124 output , err := queryTx (cdc , cliCtx , hashHexStr )
128125 if err != nil {
126+ if strings .Contains (err .Error (), hashHexStr ) {
127+ rest .WriteErrorResponse (w , http .StatusNotFound , err .Error ())
128+ return
129+ }
129130 rest .WriteErrorResponse (w , http .StatusInternalServerError , err .Error ())
130131 return
131132 }
133+
134+ if output .Empty () {
135+ rest .WriteErrorResponse (w , http .StatusNotFound , fmt .Sprintf ("no transaction found with hash %s" , hashHexStr ))
136+ }
137+
132138 rest .PostProcessResponse (w , cdc , output , cliCtx .Indent )
133139 }
134140}
0 commit comments