Skip to content

Commit

Permalink
Make the yield estimation reject height that would invalidate the cac…
Browse files Browse the repository at this point in the history
…he, fixes #147
  • Loading branch information
LeCodeurDuDimanche committed Nov 26, 2021
1 parent 9d22ced commit bc35e2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 6 additions & 8 deletions x/starname/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,14 @@ func (k Keeper) addOrRemoveFeesSum(ctx sdk.Context, height uint64, add bool) {
}

// GetBlockFeesSum retrieves the current value for the sum of the last n blocks
func (k Keeper) GetBlockFeesSum(ctx sdk.Context, maxBlocksInSum uint64) (sdk.Coins, uint64) {
//FIXME: the block height is not updated when querying at a different height (only the stores are)
// So this line prevent to query from a different height (and will make the cms panic)
// Querying at previous heights also cause problems for the cached sliding sum
func (k Keeper) GetBlockFeesSum(ctx sdk.Context, maxBlocksInSum uint64) (sdk.Coins, uint64, error) {
currentHeight := uint64(ctx.BlockHeight())

// Solves a bug where currentHeight is less than the last computed height, we always want to
// have the latest available data no matter what
// We force the query height to be greater than the latest computed height of the cached sliding sum otherwise querying
// at different height would cause high latency as the sum would have to be recomputed.
if currentHeight < slidingSum.lastComputedHeight {
currentHeight = slidingSum.lastComputedHeight
return nil, 0, fmt.Errorf("querying at past height is forbidden because of performance issues: queried " +
"height %v when last known height is %v", currentHeight, slidingSum.lastComputedHeight)
}

if currentHeight < maxBlocksInSum {
Expand Down Expand Up @@ -208,7 +206,7 @@ func (k Keeper) GetBlockFeesSum(ctx sdk.Context, maxBlocksInSum uint64) (sdk.Coi
}
}

return slidingSum.feesSum, slidingSum.feesSumCount
return slidingSum.feesSum, slidingSum.feesSumCount, nil
}

// GetBlockFees returns the fees collected at a specific height
Expand Down
5 changes: 4 additions & 1 deletion x/starname/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,11 @@ func (q grpcQuerier) Yield(ctx context.Context, _ *types.QueryYieldRequest) (*ty

func calculateYield(ctx sdk.Context, keeper *Keeper) (sdk.Dec, error) {

totalFees, numBlocks := keeper.GetBlockFeesSum(ctx, NumBlocksInAWeek)
totalFees, numBlocks, err := keeper.GetBlockFeesSum(ctx, NumBlocksInAWeek)

if err != nil {
return sdk.ZeroDec(), sdkerrors.Wrapf(err, "could not compute the fees for the latest blocks at height %v", ctx.BlockHeight())
}
if numBlocks != NumBlocksInAWeek {
return sdk.ZeroDec(), fmt.Errorf("not enough data to estimate yield: current height %v is smaller than %v",
ctx.BlockHeight(), NumBlocksInAWeek)
Expand Down

0 comments on commit bc35e2e

Please sign in to comment.