Skip to content

Commit

Permalink
Improve readability in handleQueryP2P (cosmos#9205)
Browse files Browse the repository at this point in the history
* 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 <marbar3778@yahoo.com>
Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>

(cherry picked from commit 6fbded9)
  • Loading branch information
jeebster authored and zakir committed Apr 13, 2022
1 parent b85fa9d commit 64fe04e
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <addr|id> <parameter>",
),
)
}

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 <addr|id> <parameter>",
),
)
return resp
}

func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
Expand Down

0 comments on commit 64fe04e

Please sign in to comment.