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>
  • Loading branch information
3 people committed Apr 30, 2021
1 parent e25f80d commit 6fbded9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
# Changelog

## [Unreleased]
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P

## Features
* [\#8965](https://github.com/cosmos/cosmos-sdk/pull/8965) cosmos reflection now provides more information on the application such as: deliverable msgs, sdk.Config info etc (still in alpha stage).
Expand Down
40 changes: 22 additions & 18 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,28 +799,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 6fbded9

Please sign in to comment.