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

feat(x/distribution): panic on set community pool #23507

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions x/distribution/types/fee_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func (f FeePool) ValidateGenesis() error {
return fmt.Errorf("negative DecimalPool in distribution fee pool, is %v", f.DecimalPool)
}

if f.CommunityPool.IsAnyNegative() { // TODO(@julienrbrt) in v0.53, panic if the community pool is set
return fmt.Errorf("negative CommunityPool in distribution fee pool, is %v", f.CommunityPool)
// Negative values in CommunityPool represent an invalid state that should never occur
// We panic instead of returning an error to prevent the chain from starting with an invalid state
if f.CommunityPool.IsAnyNegative() {
Copy link
Member

@julienrbrt julienrbrt Jan 25, 2025

Choose a reason for hiding this comment

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

The comment means to panic if it is set at all, as it should be specified in protocolpool.

Copy link
Author

@DeVikingMark DeVikingMark Feb 3, 2025

Choose a reason for hiding this comment

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

The comment means to panic if it is set at all, as it should be specified in protocolpool.

@julienrbrt Made an update and it seems that code works as expected—if CommunityPool has any non-zero value, it triggers a panic because those values should now be set in the protocolpool module.

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks!

panic(fmt.Sprintf("negative CommunityPool in distribution fee pool, is %v", f.CommunityPool))
}

return nil
Expand Down
103 changes: 96 additions & 7 deletions x/distribution/types/fee_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,101 @@ import (
)

func TestValidateGenesis(t *testing.T) {
fp := types.InitialFeePool()
require.Nil(t, fp.ValidateGenesis())
testCases := []struct {
name string
feePool types.FeePool
shouldPanic bool
expectError bool
}{
{
name: "valid fee pool",
feePool: types.InitialFeePool(),
shouldPanic: false,
expectError: false,
},
{
name: "negative decimal pool",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{{Denom: "stake", Amount: math.LegacyNewDec(-1)}},
CommunityPool: sdk.DecCoins{},
},
shouldPanic: false,
expectError: true,
},
{
name: "negative community pool",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{},
CommunityPool: sdk.DecCoins{{Denom: "stake", Amount: math.LegacyNewDec(-1)}},
},
shouldPanic: true,
expectError: false,
},
{
name: "multiple coins in community pool with one negative value",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{},
CommunityPool: sdk.DecCoins{
{Denom: "stake", Amount: math.LegacyNewDec(1)},
{Denom: "atom", Amount: math.LegacyNewDec(-2)},
},
},
shouldPanic: true,
expectError: false,
},
{
name: "multiple coins in community pool with all positive values",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{},
CommunityPool: sdk.DecCoins{
{Denom: "stake", Amount: math.LegacyNewDec(1)},
{Denom: "atom", Amount: math.LegacyNewDec(2)},
},
},
shouldPanic: false,
expectError: false,
},
{
name: "nil decimal pool",
feePool: types.FeePool{
CommunityPool: sdk.DecCoins{},
},
shouldPanic: false,
expectError: false,
},
{
name: "nil community pool",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{},
},
shouldPanic: false,
expectError: false,
},
{
name: "zero value community pool",
feePool: types.FeePool{
DecimalPool: sdk.DecCoins{},
CommunityPool: sdk.DecCoins{{Denom: "stake", Amount: math.LegacyNewDec(0)}},
},
shouldPanic: false,
expectError: false,
},
}

fp2 := types.FeePool{CommunityPool: sdk.DecCoins{{Denom: "stake", Amount: math.LegacyNewDec(-1)}}}
require.NotNil(t, fp2.ValidateGenesis())

fp3 := types.FeePool{DecimalPool: sdk.DecCoins{{Denom: "stake", Amount: math.LegacyNewDec(-1)}}}
require.NotNil(t, fp3.ValidateGenesis())
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.shouldPanic {
require.Panics(t, func() {
tc.feePool.ValidateGenesis()
}, "ValidateGenesis should panic for test case: %s", tc.name)
} else {
err := tc.feePool.ValidateGenesis()
if tc.expectError {
require.Error(t, err, "ValidateGenesis should return error for test case: %s", tc.name)
} else {
require.NoError(t, err, "ValidateGenesis should not return error for valid FeePool in test case: %s", tc.name)
}
}
})
}
}