Skip to content

Commit 08e97c3

Browse files
authored
Change dec coin amount of to iterative too (#265)
## Describe your changes and provide context This changes amount of for dec coins to be iterative too it dangerously assumes that the coins are in sorted order and returns 0 often if it's not. ## Testing performed to validate your change Added a unit test for this, it would fail with the old logic ![image](https://github.com/sei-protocol/sei-cosmos/assets/18161326/d2d5d34a-5c86-494e-9619-a3e72080c753)
1 parent 0861789 commit 08e97c3

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

sei-cosmos/types/dec_coin.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -476,30 +476,13 @@ func (coins DecCoins) Empty() bool {
476476
func (coins DecCoins) AmountOf(denom string) Dec {
477477
mustValidateDenom(denom)
478478

479-
switch len(coins) {
480-
case 0:
481-
return ZeroDec()
482-
483-
case 1:
484-
coin := coins[0]
479+
for _, coin := range coins {
485480
if coin.Denom == denom {
486481
return coin.Amount
487482
}
488-
return ZeroDec()
489-
490-
default:
491-
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
492-
coin := coins[midIdx]
493-
494-
switch {
495-
case denom < coin.Denom:
496-
return coins[:midIdx].AmountOf(denom)
497-
case denom == coin.Denom:
498-
return coin.Amount
499-
default:
500-
return coins[midIdx+1:].AmountOf(denom)
501-
}
502483
}
484+
485+
return ZeroDec()
503486
}
504487

505488
// IsEqual returns true if the two sets of DecCoins have the same value.

sei-cosmos/types/dec_coin_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,44 @@ func (s *decCoinTestSuite) TestDecCoins_AddDecCoinWithIsValid() {
652652
}
653653
}
654654
}
655+
656+
func (s *decCoinTestSuite) TestAmountOf() {
657+
case0 := sdk.DecCoins{}
658+
case1 := sdk.DecCoins{
659+
sdk.NewDecCoin("gold", sdk.ZeroInt()),
660+
}
661+
case2 := sdk.DecCoins{
662+
sdk.NewDecCoin("tree", sdk.NewIntWithDecimal(3, 2)),
663+
sdk.NewDecCoin("mineral", sdk.NewIntWithDecimal(5, 2)),
664+
sdk.NewDecCoin("gas", sdk.NewIntWithDecimal(1, 2)),
665+
}
666+
case3 := sdk.DecCoins{
667+
sdk.NewDecCoin("tree", sdk.NewIntWithDecimal(1, 2)),
668+
sdk.NewDecCoin("mineral", sdk.NewIntWithDecimal(1, 2)),
669+
sdk.NewDecCoin("abc", sdk.NewIntWithDecimal(1, 4)),
670+
}
671+
case4 := sdk.DecCoins{
672+
sdk.NewDecCoin("gas", sdk.NewIntWithDecimal(1, 2)),
673+
}
674+
675+
cases := []struct {
676+
coins sdk.DecCoins
677+
amountOfGAS sdk.Dec
678+
amountOfMINERAL sdk.Dec
679+
amountOfTREE sdk.Dec
680+
}{
681+
{case0, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()},
682+
{case1, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()},
683+
{case2, sdk.NewDecFromInt(sdk.NewIntWithDecimal(1, 2)), sdk.NewDecFromInt(sdk.NewIntWithDecimal(5, 2)), sdk.NewDecFromInt(sdk.NewIntWithDecimal(3, 2))},
684+
{case3, sdk.NewDecFromInt(sdk.NewIntWithDecimal(0, 0)), sdk.NewDecFromInt(sdk.NewIntWithDecimal(1, 2)), sdk.NewDecFromInt(sdk.NewIntWithDecimal(1, 2))},
685+
{case4, sdk.NewDecFromInt(sdk.NewIntWithDecimal(1, 2)), sdk.ZeroDec(), sdk.ZeroDec()},
686+
}
687+
688+
for _, tc := range cases {
689+
s.Require().Equal(tc.amountOfGAS, tc.coins.AmountOf("gas"), "coins: %s", tc.coins)
690+
s.Require().Equal(tc.amountOfMINERAL, tc.coins.AmountOf("mineral"), "coins: %s", tc.coins)
691+
s.Require().Equal(tc.amountOfTREE, tc.coins.AmountOf("tree"), "coins: %s", tc.coins)
692+
}
693+
694+
s.Require().Panics(func() { cases[0].coins.AmountOf("10Invalid") })
695+
}

sei-cosmos/x/auth/ante/validator_tx_fee.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func CheckTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx, simulate bo
2424
// is only ran on check tx.
2525
if ctx.IsCheckTx() && !simulate {
2626
feeParams := paramsKeeper.GetFeesParams(ctx)
27-
minGasPrices := GetMinimumGasPricesWanted(feeParams.GetGlobalMinimumGasPrices(), ctx.MinGasPrices())
27+
minGasPrices := GetMinimumGasPricesWantedSorted(feeParams.GetGlobalMinimumGasPrices(), ctx.MinGasPrices())
2828
if !minGasPrices.IsZero() {
2929
requiredFees := make(sdk.Coins, len(minGasPrices))
3030

@@ -51,8 +51,8 @@ func CheckTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.Tx, simulate bo
5151
return feeCoins, priority, nil
5252
}
5353

54-
func GetMinimumGasPricesWanted(globalMinimumGasPrices, validatorMinimumGasPrices sdk.DecCoins) sdk.DecCoins {
55-
return globalMinimumGasPrices.UnionMax(validatorMinimumGasPrices)
54+
func GetMinimumGasPricesWantedSorted(globalMinimumGasPrices, validatorMinimumGasPrices sdk.DecCoins) sdk.DecCoins {
55+
return globalMinimumGasPrices.UnionMax(validatorMinimumGasPrices).Sort()
5656
}
5757

5858
// getTxPriority returns a naive tx priority based on the amount of the smallest denomination of the gas price

sei-cosmos/x/auth/ante/validator_tx_fee_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
1313
globalMinGasPrices := sdk.NewDecCoins()
1414
validatorMinGasPrices := sdk.NewDecCoins()
1515

16-
minGasWanted := ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
16+
minGasWanted := ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
1717

1818
expectedMinGasWanted := sdk.NewDecCoins()
1919

@@ -23,7 +23,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
2323
globalMinGasPrices = sdk.NewDecCoins()
2424
validatorMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)))
2525

26-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
26+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
2727

2828
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)))
2929

@@ -33,7 +33,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
3333
globalMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("bar", sdk.NewInt(2)))
3434
validatorMinGasPrices = sdk.NewDecCoins()
3535

36-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
36+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
3737

3838
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("bar", sdk.NewInt(2)))
3939

@@ -43,7 +43,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
4343
globalMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)))
4444
validatorMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("bar", sdk.NewInt(3)), sdk.NewDecCoin("baz", sdk.NewInt(4)))
4545

46-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
46+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
4747

4848
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(3)), sdk.NewDecCoin("baz", sdk.NewInt(4)))
4949

@@ -53,7 +53,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
5353
globalMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)))
5454
validatorMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("baz", sdk.NewInt(3)), sdk.NewDecCoin("qux", sdk.NewInt(4)))
5555

56-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
56+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
5757

5858
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)), sdk.NewDecCoin("baz", sdk.NewInt(3)), sdk.NewDecCoin("qux", sdk.NewInt(4)))
5959

@@ -63,7 +63,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
6363
globalMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)))
6464
validatorMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(3)), sdk.NewDecCoin("bar", sdk.NewInt(4)))
6565

66-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
66+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
6767

6868
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(3)), sdk.NewDecCoin("bar", sdk.NewInt(4)))
6969

@@ -73,7 +73,7 @@ func TestGetMinimumGasWanted(t *testing.T) {
7373
globalMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)))
7474
validatorMinGasPrices = sdk.NewDecCoins(sdk.NewDecCoin("baz", sdk.NewInt(3)), sdk.NewDecCoin("qux", sdk.NewInt(4)))
7575

76-
minGasWanted = ante.GetMinimumGasPricesWanted(globalMinGasPrices, validatorMinGasPrices)
76+
minGasWanted = ante.GetMinimumGasPricesWantedSorted(globalMinGasPrices, validatorMinGasPrices)
7777

7878
expectedMinGasWanted = sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(1)), sdk.NewDecCoin("bar", sdk.NewInt(2)), sdk.NewDecCoin("baz", sdk.NewInt(3)), sdk.NewDecCoin("qux", sdk.NewInt(4)))
7979

0 commit comments

Comments
 (0)