Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Queriers for staking to increase Gaia-lite performance #2139

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2ca7e61
Cherry picked commits from prev branch
Aug 24, 2018
5e285e1
Added new keepers for querier functionalities
Aug 24, 2018
ec13fbe
Renaming
Aug 24, 2018
3eacdfa
Fixed gov errors and messages
Aug 24, 2018
682008f
Added Querier to stake and app
Aug 27, 2018
ceae374
Update delegation keepers
Aug 27, 2018
9ba98ab
REST Queriers not working
Aug 27, 2018
8bf6b2c
Fix marshalling error
Aug 27, 2018
cd0ca86
Querier tests working
Aug 28, 2018
01304c9
Pool and params working
Aug 28, 2018
7889374
sdk.NewCoin for test handler
Aug 28, 2018
d3b6c3e
Refactor and renaming
Aug 28, 2018
5fb1547
Update LCD queries and added more tests for queriers
Aug 29, 2018
8cfc507
use sdk.NewCoin
Aug 29, 2018
044b851
Delegator summary query and tests
Aug 29, 2018
685f05a
Added more tests for keeper
Aug 29, 2018
6d9c8b7
Update PENDING.md
Aug 29, 2018
3f5ad5f
Merge branch 'develop' into fedekunze/2009-queriers-staking
fedekunze Aug 29, 2018
f6aed4e
Update stake rest query
Aug 29, 2018
b9b04a0
Format and replaced panics for sdk.Error
Aug 30, 2018
2852b4a
Refactor and addressed comments from Sunny and Aleks
Aug 31, 2018
128deb5
Merge branch 'develop' into fedekunze/2009-queriers-staking
fedekunze Aug 31, 2018
e93a937
Fixed some of the errors produced by addr type change
Aug 31, 2018
80cd207
Fixed remaining errors
Aug 31, 2018
c83172d
Updated and fixed lite tests
Aug 31, 2018
7f3a42c
JSON Header and consistency on errors
Aug 31, 2018
87c1a0f
Increased cov for genesis
Aug 31, 2018
5ecedad
Added comment for maxRetrieve param in keepers
Aug 31, 2018
af757dd
Comment on DelegationWithoutDec
Aug 31, 2018
80db853
Bech32Validator Keepers
Aug 31, 2018
99acd57
Changed Bech validator
Sep 1, 2018
8abb44e
Updated remaining tests and bech32 validator
Sep 1, 2018
55ff4b7
Merge branch 'develop' into fedekunze/2009-queriers-staking
fedekunze Sep 2, 2018
450539b
Addressed most of Rigel's comments
Sep 3, 2018
61efdf6
Updated tests and types
Sep 6, 2018
0bd0417
Make codec to be unexported from keeper
Sep 6, 2018
8a2d144
Moved logic to query_utils and updated tests
Sep 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
AddRoute("gov", gov.NewHandler(app.govKeeper))

app.QueryRouter().
AddRoute("gov", gov.NewQuerier(app.govKeeper))
AddRoute("gov", gov.NewQuerier(app.govKeeper)).
AddRoute("stake", stake.NewQuerier(app.stakeKeeper))

// initialize BaseApp
app.SetInitChainer(app.initChainer)
Expand Down
8 changes: 4 additions & 4 deletions x/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
resTags.AppendTag(tags.Action, tags.ActionProposalDropped)
resTags.AppendTag(tags.ProposalID, proposalIDBytes)

logger.Info("Proposal %d - \"%s\" - didn't mean minimum deposit (had only %s), deleted",
inactiveProposal.GetProposalID(), inactiveProposal.GetTitle(), inactiveProposal.GetTotalDeposit())
logger.Info(fmt.Sprintf("Proposal %d (%s) didn't meet minimum deposit (had only %s), deleted",
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
inactiveProposal.GetProposalID(), inactiveProposal.GetTitle(), inactiveProposal.GetTotalDeposit()))
}

// Check if earliest Active Proposal ended voting period yet
Expand Down Expand Up @@ -143,8 +143,8 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
activeProposal.SetTallyResult(tallyResults)
keeper.SetProposal(ctx, activeProposal)

logger.Info("Proposal %d - \"%s\" - tallied, passed: %v",
activeProposal.GetProposalID(), activeProposal.GetTitle(), passes)
logger.Info(fmt.Sprintf("Proposal %d (%s) tallied. Passed: %v",
activeProposal.GetProposalID(), activeProposal.GetTitle(), passes))

for _, valAddr := range nonVotingVals {
val := keeper.ds.GetValidatorSet().Validator(ctx, valAddr)
Expand Down
144 changes: 72 additions & 72 deletions x/gov/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case "proposals":
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return queryProposals(ctx, path[1:], req, keeper)
case "proposal":
return queryProposal(ctx, path[1:], req, keeper)
case "deposit":
return queryDeposit(ctx, path[1:], req, keeper)
case "vote":
return queryVote(ctx, path[1:], req, keeper)
case "deposits":
return queryDeposits(ctx, path[1:], req, keeper)
case "deposit":
return queryDeposit(ctx, path[1:], req, keeper)
case "votes":
return queryVotes(ctx, path[1:], req, keeper)
case "proposals":
return queryProposals(ctx, path[1:], req, keeper)
case "vote":
return queryVote(ctx, path[1:], req, keeper)
case "tally":
return queryTally(ctx, path[1:], req, keeper)
default:
Expand All @@ -31,28 +31,52 @@ func NewQuerier(keeper Keeper) sdk.Querier {
}
}

// Params for query 'custom/gov/proposals'
type QueryProposalsParams struct {
Voter sdk.AccAddress
Depositer sdk.AccAddress
ProposalStatus ProposalStatus
NumLatestProposals int64
}

func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryProposalsParams
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.NumLatestProposals)

res, errRes = wire.MarshalJSONIndent(keeper.cdc, proposals)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}

// Params for query 'custom/gov/proposal'
type QueryProposalParams struct {
ProposalID int64
}

func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryProposalParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

proposal := keeper.GetProposal(ctx, params.ProposalID)
if proposal == nil {
return []byte{}, ErrUnknownProposal(DefaultCodespace, params.ProposalID)
}

bz, err2 := wire.MarshalJSONIndent(keeper.cdc, proposal)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, proposal)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}

// Params for query 'custom/gov/deposit'
Expand All @@ -63,17 +87,17 @@ type QueryDepositParams struct {

func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryDepositParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositer)
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, deposit)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, deposit)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}

// Params for query 'custom/gov/vote'
Expand All @@ -84,17 +108,17 @@ type QueryVoteParams struct {

func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryVoteParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, vote)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, vote)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}

// Params for query 'custom/gov/deposits'
Expand All @@ -104,9 +128,9 @@ type QueryDepositsParams struct {

func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryDepositParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

var deposits []Deposit
Expand All @@ -117,11 +141,11 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
deposits = append(deposits, deposit)
}

bz, err2 := wire.MarshalJSONIndent(keeper.cdc, deposits)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, deposits)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}

// Params for query 'custom/gov/votes'
Expand All @@ -131,10 +155,10 @@ type QueryVotesParams struct {

func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryVotesParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)

if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

var votes []Vote
Expand All @@ -145,35 +169,11 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
votes = append(votes, vote)
}

bz, err2 := wire.MarshalJSONIndent(keeper.cdc, votes)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}

// Params for query 'custom/gov/proposals'
type QueryProposalsParams struct {
Voter sdk.AccAddress
Depositer sdk.AccAddress
ProposalStatus ProposalStatus
NumLatestProposals int64
}

func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryProposalsParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}

proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.NumLatestProposals)

bz, err2 := wire.MarshalJSONIndent(keeper.cdc, proposals)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, votes)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}

// Params for query 'custom/gov/tally'
Expand All @@ -185,9 +185,9 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
// TODO: Dependant on #1914

var proposalID int64
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
errRes := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo all these refactor changes (for now) to use errRes and res it's bloating this PR - maybe make a refactor PR after this? - Although, I also don't really agree with this modification either - but we can discuss in the next PR

if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

proposal := keeper.GetProposal(ctx, proposalID)
Expand All @@ -205,9 +205,9 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
_, tallyResult, _ = tally(ctx, keeper, proposal)
}

bz, err2 := wire.MarshalJSONIndent(keeper.cdc, tallyResult)
if err2 != nil {
panic("could not marshal result to JSON")
res, errRes = wire.MarshalJSONIndent(keeper.cdc, tallyResult)
if errRes != nil {
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return bz, nil
return res, nil
}
2 changes: 1 addition & 1 deletion x/stake/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Cod
delegationHandlerFn(cliCtx, cdc),
).Methods("GET")

// Query all unbonding_delegations between a delegator and a validator
// Query a unbonding-delegation between a delegator and a validator
r.HandleFunc(
"/stake/delegators/{delegatorAddr}/unbonding_delegations/{validatorAddr}",
unbondingDelegationsHandlerFn(cliCtx, cdc),
Expand Down
Loading