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 1 commit
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
28 changes: 14 additions & 14 deletions x/gov/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keepe
var params QueryProposalsParams
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", errRes.Error()))
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("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -64,7 +64,7 @@ func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
var params QueryProposalParams
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 - %s", errRes.Error()))
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

proposal := keeper.GetProposal(ctx, params.ProposalID)
Expand All @@ -74,7 +74,7 @@ func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper

res, errRes = wire.MarshalJSONIndent(keeper.cdc, proposal)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -89,13 +89,13 @@ func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
var params QueryDepositParams
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 - %s", errRes.Error()))
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositer)
res, errRes = wire.MarshalJSONIndent(keeper.cdc, deposit)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -110,13 +110,13 @@ func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Kee
var params QueryVoteParams
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", errRes.Error()))
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
res, errRes = wire.MarshalJSONIndent(keeper.cdc, vote)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -130,7 +130,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
var params QueryDepositParams
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)
if errRes != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", errRes.Error()))
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

var deposits []Deposit
Expand All @@ -143,7 +143,7 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper

res, errRes = wire.MarshalJSONIndent(keeper.cdc, deposits)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -158,7 +158,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
errRes := keeper.cdc.UnmarshalJSON(req.Data, &params)

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

var votes []Vote
Expand All @@ -171,7 +171,7 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke

res, errRes = wire.MarshalJSONIndent(keeper.cdc, votes)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
Expand All @@ -187,7 +187,7 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
var proposalID int64
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 - %s", errRes.Error()))
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

proposal := keeper.GetProposal(ctx, proposalID)
Expand All @@ -207,7 +207,7 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke

res, errRes = wire.MarshalJSONIndent(keeper.cdc, tallyResult)
if errRes != nil {
panic("could not marshal result to JSON")
panic(fmt.Sprintf("could not marshal result to JSON:\n%s", errRes.Error()))
}
return res, nil
}
35 changes: 15 additions & 20 deletions x/stake/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,33 @@ func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)

i := 0
for ; ; i++ {
if !iterator.Valid() {
break
}
for ; iterator.Valid(); iterator.Next() {
addr := iterator.Key()[1:]
validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value())
validators = append(validators, validator)
iterator.Next()
i++
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}
iterator.Close()
return validators
}

// Get the set of all validators, retrieve a maxRetrieve number of records
func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators []types.Validator) {
func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve ...int16) (validators []types.Validator) {
if len(maxRetrieve) == 0 {
return k.GetAllValidators(ctx)
}
validators = make([]types.Validator, maxRetrieve[0])

store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)

validators = make([]types.Validator, maxRetrieve)
i := 0
for ; ; i++ {
if !iterator.Valid() || i > int(maxRetrieve-1) {
break
}
for ; iterator.Valid() && i < int(maxRetrieve[0]); iterator.Next() {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
addr := iterator.Key()[1:]
validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value())

validators[i] = validator
iterator.Next()
i++
}
iterator.Close()
return validators[:i] // trim
Expand All @@ -123,7 +122,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []types.Validat
address := GetAddressFromValBondedIndexKey(iterator.Key())
validator, found := k.GetValidator(ctx, address)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address))
panic(types.ErrNoValidatorFound(types.DefaultCodespace))
}

validators[i] = validator
Expand All @@ -142,20 +141,16 @@ func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []types.Validator {
validators := make([]types.Validator, maxValidators)
iterator := sdk.KVStoreReversePrefixIterator(store, ValidatorsByPowerIndexKey) // largest to smallest
i := 0
for {
if !iterator.Valid() || i > int(maxValidators-1) {
break
}
for ; iterator.Valid() && i < int(maxValidators); iterator.Next() {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
address := iterator.Value()
validator, found := k.GetValidator(ctx, address)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address))
panic(types.ErrNoValidatorFound(types.DefaultCodespace))
}
if validator.Status == sdk.Bonded {
validators[i] = validator
i++
}
iterator.Next()
}
iterator.Close()
return validators[:i] // trim
Expand Down Expand Up @@ -412,7 +407,7 @@ func (k Keeper) UpdateBondedValidators(
var found bool
validator, found = k.GetValidator(ctx, ownerAddr)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr))
panic(types.ErrNoValidatorFound(types.DefaultCodespace))
}
}

Expand Down
15 changes: 13 additions & 2 deletions x/stake/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ func TestSetValidator(t *testing.T) {

resVals = keeper.GetValidatorsByPower(ctx)
require.Equal(t, 1, len(resVals))
assert.True(ValEq(t, validator, resVals[0]))
require.True(ValEq(t, validator, resVals[0]))

resVals = keeper.GetValidators(ctx)
require.Equal(t, 1, len(resVals))
require.True(ValEq(t, validator, resVals[0]))

resVals = keeper.GetValidators(ctx, 10)
require.Equal(t, 1, len(resVals))
require.True(ValEq(t, validator, resVals[0]))

updates := keeper.GetTendermintUpdates(ctx)
require.Equal(t, 1, len(updates))
Expand Down Expand Up @@ -265,7 +273,10 @@ func TestValidatorBasics(t *testing.T) {
_, found := keeper.GetValidator(ctx, addrVals[0])
require.False(t, found)
resVals := keeper.GetValidatorsBonded(ctx)
assert.Zero(t, len(resVals))
require.Zero(t, len(resVals))

resVals = keeper.GetValidators(ctx)
require.Zero(t, len(resVals))

pool = keeper.GetPool(ctx)
assert.True(sdk.DecEq(t, sdk.ZeroDec(), pool.BondedTokens))
Expand Down
31 changes: 15 additions & 16 deletions x/stake/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,23 @@ func NewQuerier(k keeper.Keeper) sdk.Querier {

// Params for queries:
// - 'custom/stake/delegator'
// - 'custom/stake/delegator/txs'
// - 'custom/stake/delegator/validators'
// - 'custom/stake/delegatorValidators'
// - 'custom/stake/validator'
type QueryAddressParams struct {
accountAddr sdk.AccAddress
AccountAddr sdk.AccAddress
}

// Params for queries
// - 'custom/stake/delegator/delegations'
// - 'custom/stake/delegator/unbonding_delegations'
// - 'custom/stake/delegator/validator'
// - 'custom/stake/delegation'
// - 'custom/stake/unbonding-delegation'
// - 'custom/stake/delegatorValidator'
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
type QueryBondsParams struct {
delegatorAddr sdk.AccAddress
validatorAddr sdk.AccAddress
DelegatorAddr sdk.AccAddress
ValidatorAddr sdk.AccAddress
}

func queryValidators(ctx sdk.Context, path []string, req abci.RequestQuery, k keeper.Keeper) (res []byte, err sdk.Error) {
validators := k.GetAllValidators(ctx)

validators := k.GetValidators(ctx)
res, errRes := wire.MarshalJSONIndent(k.Codec(), validators)
if err != nil {
panic(errRes.Error())
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -72,7 +70,7 @@ func queryValidator(ctx sdk.Context, path []string, req abci.RequestQuery, k kee
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data: \n%s", err.Error()))
}

validator, found := k.GetValidator(ctx, params.accountAddr)
validator, found := k.GetValidator(ctx, params.AccountAddr)
if !found {
return []byte{}, ErrNoValidatorFound(DefaultCodespace)
}
Expand All @@ -85,7 +83,7 @@ func queryValidator(ctx sdk.Context, path []string, req abci.RequestQuery, k kee
}

// TODO query with limit
// func queryDelegator(ctx sdk.Context, path []string, req abci.RequestQuery, k keeper.Keeper) (res []byte, err sdk.Error) {
// func queryDelegator(ctx sdk.Context, req abci.RequestQuery, k keeper.Keeper) (res []byte, err sdk.Error) {
// var params QueryAddressParams
// errRes := k.Codec().UnmarshalJSON(req.Data, &params)
// if errRes != nil {
Expand All @@ -102,12 +100,13 @@ func queryValidator(ctx sdk.Context, path []string, req abci.RequestQuery, k kee
// TODO query with limit
func queryDelegatorValidators(ctx sdk.Context, path []string, req abci.RequestQuery, k keeper.Keeper) (res []byte, err sdk.Error) {
var params QueryAddressParams

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

validators := k.GetDelegatorValidators(ctx, params.accountAddr)
validators := k.GetDelegatorValidators(ctx, params.AccountAddr)

res, errRes = wire.MarshalJSONIndent(k.Codec(), validators)
if errRes != nil {
Expand All @@ -123,7 +122,7 @@ func queryDelegatorValidator(ctx sdk.Context, path []string, req abci.RequestQue
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

validator := k.GetDelegatorValidator(ctx, params.delegatorAddr, params.validatorAddr)
validator := k.GetDelegatorValidator(ctx, params.DelegatorAddr, params.ValidatorAddr)

res, errRes = wire.MarshalJSONIndent(k.Codec(), validator)
if errRes != nil {
Expand All @@ -139,7 +138,7 @@ func queryDelegation(ctx sdk.Context, path []string, req abci.RequestQuery, k ke
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

delegation, found := k.GetDelegation(ctx, params.delegatorAddr, params.validatorAddr)
delegation, found := k.GetDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
if !found {
return []byte{}, ErrNoDelegation(DefaultCodespace)
}
Expand All @@ -158,7 +157,7 @@ func queryUnbondingDelegation(ctx sdk.Context, path []string, req abci.RequestQu
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data :\n%s", errRes.Error()))
}

unbond, found := k.GetUnbondingDelegation(ctx, params.delegatorAddr, params.validatorAddr)
unbond, found := k.GetUnbondingDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
if !found {
return []byte{}, ErrNoUnbondingDelegation(DefaultCodespace)
}
Expand Down