fix: add check for duplicate continuous fund in x/protocolpool genesis (#24559)

This commit is contained in:
Alex | Interchain Labs 2025-04-23 17:10:52 -04:00 committed by GitHub
parent 6a54aaa90e
commit bb91d18a01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package types
import (
"errors"
"fmt"
"cosmossdk.io/math"
)
@ -33,5 +34,17 @@ func (gs *GenesisState) Validate() error {
return errors.New("total percentage cannot be greater than 100")
}
seenContinuousFunds := make(map[string]struct{})
for _, fund := range gs.ContinuousFunds {
if err := fund.Validate(); err != nil {
return fmt.Errorf("invalid continuousfund: %w", err)
}
if _, ok := seenContinuousFunds[fund.Recipient]; ok {
return fmt.Errorf("duplicated continuous fund recipient address: %s", fund.Recipient)
}
seenContinuousFunds[fund.Recipient] = struct{}{}
}
return gs.Params.Validate()
}

View File

@ -93,6 +93,39 @@ func TestValidateGenesis(t *testing.T) {
},
expectedErr: "total percentage cannot be greater than 100",
},
{
name: "invalid genesis state with invalid continuous fund (percentage sum > 1)",
genesisState: &types.GenesisState{
ContinuousFunds: []types.ContinuousFund{
{
Recipient: "cosmos1dupaddress",
Percentage: math.LegacyMustNewDecFromStr("1.1"),
Expiry: nil,
},
},
Params: types.DefaultParams(),
},
expectedErr: "percentage cannot be greater than one",
},
{
name: "invalid genesis state with duplicate recipient addresses",
genesisState: &types.GenesisState{
ContinuousFunds: []types.ContinuousFund{
{
Recipient: "cosmos1dupaddress",
Percentage: math.LegacyMustNewDecFromStr("0.4"),
Expiry: nil,
},
{
Recipient: "cosmos1dupaddress", // duplicate
Percentage: math.LegacyMustNewDecFromStr("0.3"),
Expiry: nil,
},
},
Params: types.DefaultParams(),
},
expectedErr: "duplicated continuous fund recipient address",
},
}
for _, tc := range tests {