From 64fe04ea3e120d95253653313868c8a4314ea984 Mon Sep 17 00:00:00 2001 From: Greg Morrison Date: Fri, 30 Apr 2021 17:34:28 +0800 Subject: [PATCH] Improve readability in handleQueryP2P (#9205) * refactor(abci handleP2PQuery): improve readability via control flow * refactor(acbi handleQueryP2P): fix logic control flow to ensure early return * refactor(abci handleQueryP2P): move var declaration to prevent unnecessary initialization * support(CHANGELOG): add description for abci handleQueryP2P changes Co-authored-by: Marko Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> (cherry picked from commit 6fbded9664e2796d5ccbd68310bec82eb2412213) --- baseapp/abci.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 965969918fd9..c9b68302839a 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -819,28 +819,32 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.R func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery { // "/p2p" prefix for p2p queries - if len(path) >= 4 { - cmd, typ, arg := path[1], path[2], path[3] - switch cmd { - case "filter": - switch typ { - case "addr": - return app.FilterPeerByAddrPort(arg) - - case "id": - return app.FilterPeerByID(arg) - } + if len(path) < 4 { + return sdkerrors.QueryResult( + sdkerrors.Wrap( + sdkerrors.ErrUnknownRequest, "path should be p2p filter ", + ), + ) + } - default: - return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'")) + var resp abci.ResponseQuery + + cmd, typ, arg := path[1], path[2], path[3] + switch cmd { + case "filter": + switch typ { + case "addr": + resp = app.FilterPeerByAddrPort(arg) + + case "id": + resp = app.FilterPeerByID(arg) } + + default: + resp = sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'")) } - return sdkerrors.QueryResult( - sdkerrors.Wrap( - sdkerrors.ErrUnknownRequest, "expected path is p2p filter ", - ), - ) + return resp } func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {