Skip to content

Commit

Permalink
fix: store validator reward set via single key (#207) (#208)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2b86b02)

Co-authored-by: Adam Wozniak <[email protected]>
  • Loading branch information
mergify[bot] and adamewozniak authored Jun 12, 2023
1 parent bf95b8e commit 75a4474
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
8 changes: 1 addition & 7 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ func CalcPrices(ctx sdk.Context, params types.Params, k keeper.Keeper) error {
}

// Get the validators which can earn rewards in this Slash Window.
validatorRewardSet := types.ValidatorRewardSet{
ValidatorSet: []string{},
}
k.CurrentValidatorRewardSet(ctx, func(valRewardSet types.ValidatorRewardSet) bool {
validatorRewardSet = valRewardSet
return false
})
validatorRewardSet := k.GetValidatorRewardSet(ctx)

// update miss counting & slashing
voteTargetsLen := len(params.MandatoryList)
Expand Down
6 changes: 1 addition & 5 deletions x/oracle/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,7 @@ func (q querier) ValidatorRewardSet(

ctx := sdk.UnwrapSDKContext(goCtx)

validatorRewardSet := types.ValidatorRewardSet{}
q.CurrentValidatorRewardSet(ctx, func(valSet types.ValidatorRewardSet) bool {
validatorRewardSet = valSet
return false
})
validatorRewardSet := q.GetValidatorRewardSet(ctx)

return &types.QueryValidatorRewardSetResponse{
Validators: validatorRewardSet,
Expand Down
29 changes: 10 additions & 19 deletions x/oracle/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ojo-network/ojo/util"
"github.com/ojo-network/ojo/x/oracle/types"
)

Expand Down Expand Up @@ -79,28 +78,20 @@ func (k Keeper) SetValidatorRewardSet(ctx sdk.Context) {

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&validatorRewardSet)
store.Set(types.KeyValidatorRewardSet(uint64(ctx.BlockHeight())), bz)
store.Set(types.KeyValidatorRewardSet(), bz)
}

// CurrentValidatorRewardSet returns the latest ValidatorRewardSet in the store.
func (k Keeper) CurrentValidatorRewardSet(
ctx sdk.Context,
handler func(types.ValidatorRewardSet) bool,
) {
func (k Keeper) GetValidatorRewardSet(ctx sdk.Context) types.ValidatorRewardSet {
store := ctx.KVStore(k.storeKey)

// make sure we have one zero byte to correctly separate blocknum
prefix := util.ConcatBytes(1, types.KeyPrefixValidatorRewardSet)
iter := sdk.KVStoreReversePrefixIteratorPaginated(store, prefix, 1, 1)
defer iter.Close()

for ; iter.Valid(); iter.Next() {
validatorRewardSet := types.ValidatorRewardSet{
ValidatorSet: []string{},
}
k.cdc.MustUnmarshal(iter.Value(), &validatorRewardSet)
if handler(validatorRewardSet) {
break
}
bz := store.Get(types.KeyValidatorRewardSet())
if bz == nil {
return types.ValidatorRewardSet{}
}

var rewardSet types.ValidatorRewardSet
k.cdc.MustUnmarshal(bz, &rewardSet)

return rewardSet
}
4 changes: 2 additions & 2 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func KeyHistoricPrice(denom string, blockNum uint64) (key []byte) {
}

// KeyValidatorRewardSet - stored by *block*
func KeyValidatorRewardSet(blockNum uint64) (key []byte) {
return util.ConcatBytes(0, KeyPrefixValidatorRewardSet, util.UintWithNullPrefix(blockNum))
func KeyValidatorRewardSet() (key []byte) {
return util.ConcatBytes(0, KeyPrefixValidatorRewardSet)
}

// ParseDenomAndBlockFromKey returns the denom and block contained in the *key*
Expand Down

0 comments on commit 75a4474

Please sign in to comment.