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

fix: x/oracle end_blocker panic #2185

Merged
merged 6 commits into from
Aug 5, 2023
Merged

fix: x/oracle end_blocker panic #2185

merged 6 commits into from
Aug 5, 2023

Conversation

kosegor
Copy link
Contributor

@kosegor kosegor commented Aug 3, 2023

This PR contains a fix for the chain halting during the EndBlocker function. The scenario to reproduce it is as follows:

  • Start a fresh chain with median_stamp_period < historic_stamp_period.
  • Populate x/leverage registry with tokens and wait until x/oracle EndBlocker executes.
  • A panic will raise halting the chain with the following error: panic: denom: ETH: empty price list passed in

@kosegor kosegor requested a review from a team as a code owner August 3, 2023 18:53
@codecov
Copy link

codecov bot commented Aug 3, 2023

Codecov Report

Merging #2185 (2f316d4) into main (7f05ad4) will decrease coverage by 6.61%.
Report is 170 commits behind head on main.
The diff coverage is 81.67%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2185      +/-   ##
==========================================
- Coverage   75.38%   68.78%   -6.61%     
==========================================
  Files         100      157      +57     
  Lines        8025    11730    +3705     
==========================================
+ Hits         6050     8068    +2018     
- Misses       1589     3120    +1531     
- Partials      386      542     +156     
Files Changed Coverage Δ
ante/spam_prevention.go 75.92% <ø> (ø)
util/coin/coin.go 12.50% <0.00%> (ø)
x/incentive/codec.go 47.82% <ø> (+9.89%) ⬆️
x/incentive/keeper/invariants.go 0.00% <0.00%> (ø)
x/incentive/keeper/store.go 70.67% <ø> (ø)
x/incentive/keeper/unbonding.go 80.45% <ø> (ø)
x/incentive/keeper/update.go 52.11% <ø> (ø)
x/incentive/keys.go 100.00% <ø> (ø)
x/incentive/msgs.go 77.41% <ø> (-0.81%) ⬇️
x/incentive/params.go 89.28% <ø> (-10.72%) ⬇️
... and 58 more

... and 60 files with indirect coverage changes

@adamewozniak
Copy link
Collaborator

This doesn’t look like a bug in endblocker, rather a condition we shouldn’t expect to be in the genesis file. Imo we shouldn’t merge this in

@toteki
Copy link
Member

toteki commented Aug 3, 2023

Maybe we can fix one layer lower in the functions - missing medians shouldn't be a chain halt, just like missing prices aren't.

We could do:

func (k Keeper) CalcAndSetHistoricMedian(
	ctx sdk.Context,
	denom string,
) error {
	historicPrices := k.historicPrices(ctx, denom, k.MaximumPriceStamps(ctx))
	if len(historicPrices) == 0 {
		return nil
	}
	median, err := decmath.Median(historicPrices)
	if err != nil {
		return errors.Wrap(err, "denom: "+denom)
	}

	block := uint64(ctx.BlockHeight())
	k.SetHistoricMedian(ctx, denom, block, median)
	return k.calcAndSetHistoricMedianDeviation(ctx, denom, median, historicPrices)
}

(the return nil and its if statement are the only changes)

@adamewozniak

@adamewozniak
Copy link
Collaborator

adamewozniak commented Aug 3, 2023

Maybe we can fix one layer lower in the functions - missing medians shouldn't be a chain halt, just like missing prices aren't.

We could do:


func (k Keeper) CalcAndSetHistoricMedian(

	ctx sdk.Context,

	denom string,

) error {

	historicPrices := k.historicPrices(ctx, denom, k.MaximumPriceStamps(ctx))

	if len(historicPrices) == 0 {

		return nil

	}

	median, err := decmath.Median(historicPrices)

	if err != nil {

		return errors.Wrap(err, "denom: "+denom)

	}



	block := uint64(ctx.BlockHeight())

	k.SetHistoricMedian(ctx, denom, block, median)

	return k.calcAndSetHistoricMedianDeviation(ctx, denom, median, historicPrices)

}

(the return nil and its if statement are the only changes)

@adamewozniak

Seems like a much more appropriate fix imo. Maybe a comment / log in this function that "this should never happen"?

@kosegor
Copy link
Contributor Author

kosegor commented Aug 3, 2023

I applied @toteki solution. @adamewozniak check it out.

Copy link
Collaborator

@adamewozniak adamewozniak left a comment

Choose a reason for hiding this comment

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

Lgtm, one nit

x/oracle/keeper/historic_price.go Show resolved Hide resolved
@kosegor kosegor enabled auto-merge August 4, 2023 22:00
@kosegor kosegor added this pull request to the merge queue Aug 5, 2023
Merged via the queue into main with commit 58ca854 Aug 5, 2023
25 checks passed
@kosegor kosegor deleted the egor/oracle-fix branch August 5, 2023 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants