diff --git a/pkg/math/voteweighted/math_test.go b/pkg/math/voteweighted/math_test.go index 5e1e7e03a..8c68ada58 100644 --- a/pkg/math/voteweighted/math_test.go +++ b/pkg/math/voteweighted/math_test.go @@ -52,6 +52,7 @@ func (s *MathTestSuite) TestMedian() { validators []validator totalBondedTokens sdkmath.Int expectedPrices map[connecttypes.CurrencyPair]*big.Int + expectedCcvPrices map[connecttypes.CurrencyPair]*big.Int }{ { name: "no providers", @@ -278,6 +279,31 @@ func (s *MathTestSuite) TestMedian() { }: big.NewInt(300), }, }, + { + name: "totalBondedTokens is zero", + providerPrices: aggregator.AggregatedProviderData[string, map[connecttypes.CurrencyPair]*big.Int]{ + validator1.String(): map[connecttypes.CurrencyPair]*big.Int{ + { + Base: "BTC", + Quote: "USD", + }: big.NewInt(100), + }, + }, + validators: []validator{ + { + stake: sdkmath.NewInt(100), + consAddr: validator1, + }, + }, + totalBondedTokens: sdkmath.NewInt(0), + expectedPrices: map[connecttypes.CurrencyPair]*big.Int{}, + expectedCcvPrices: map[connecttypes.CurrencyPair]*big.Int{ + { + Base: "BTC", + Quote: "USD", + }: big.NewInt(100), + }, + }, } for _, tc := range cases { @@ -295,9 +321,16 @@ func (s *MathTestSuite) TestMedian() { // Verify the results. s.Require().Len(defaultResult, len(tc.expectedPrices)) - s.Require().Len(ccvResult, len(tc.expectedPrices)) for currencyPair, expectedPrice := range tc.expectedPrices { s.Require().Equal(expectedPrice, defaultResult[currencyPair]) + } + + expectedCcvPrices := tc.expectedPrices + if tc.expectedCcvPrices != nil { + expectedCcvPrices = tc.expectedCcvPrices + } + s.Require().Len(ccvResult, len(expectedCcvPrices)) + for currencyPair, expectedPrice := range expectedCcvPrices { s.Require().Equal(expectedPrice, ccvResult[currencyPair]) } }) diff --git a/pkg/math/voteweighted/voteweighted.go b/pkg/math/voteweighted/voteweighted.go index 19e7d8f8e..11d3877a3 100644 --- a/pkg/math/voteweighted/voteweighted.go +++ b/pkg/math/voteweighted/voteweighted.go @@ -132,6 +132,13 @@ func Median( // This should never error. panic(err) } + if totalBondedTokens.IsZero() { + logger.Warn( + "total bonded tokens is zero; cannot compute median prices", + "total_bonded_tokens", totalBondedTokens.String(), + ) + return prices + } for currencyPair, info := range priceInfo { // The total voting power % that submitted a price update for the given currency pair must be