Skip to content

Commit

Permalink
fix: check for duplicates in genesis info accounts (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Nov 14, 2024
1 parent 5deae85 commit fb0ffb7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
24 changes: 23 additions & 1 deletion x/rollapp/genesisbridge/genesis_bridge_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"cosmossdk.io/math"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/stretchr/testify/require"

"github.com/dymensionxyz/dymension/v3/x/rollapp/genesisbridge"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/stretchr/testify/require"
)

func TestGenesisBridgeData_ValidateBasic(t *testing.T) {
Expand Down Expand Up @@ -161,6 +162,27 @@ func TestGenesisBridgeData_ValidateBasic(t *testing.T) {
},
wantErr: true,
},
{
name: "duplicate genesis accounts",
data: genesisbridge.GenesisBridgeData{
GenesisInfo: genesisbridge.GenesisBridgeInfo{
GenesisChecksum: "checksum",
Bech32Prefix: "prefix",
NativeDenom: types.DenomMetadata{
Base: "base",
Display: "display",
Exponent: 18,
},
InitialSupply: math.NewInt(1000),
GenesisAccounts: []types.GenesisAccount{
{Address: "dym17g9cn4ss0h0dz5qhg2cg4zfnee6z3ftg3q6v58"},
{Address: "dym17g9cn4ss0h0dz5qhg2cg4zfnee6z3ftg3q6v58"}, // duplicate account
},
},
NativeDenom: validMetadata,
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down
15 changes: 9 additions & 6 deletions x/rollapp/types/genesis_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package types

import (
"errors"
fmt "fmt"
"fmt"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -48,10 +48,8 @@ func (gi GenesisInfo) Validate() error {
}
}

if !gi.InitialSupply.IsNil() {
if !gi.InitialSupply.IsPositive() {
return ErrInvalidInitialSupply
}
if !gi.InitialSupply.IsNil() && !gi.InitialSupply.IsPositive() {
return ErrInvalidInitialSupply
}

// validate max limit of genesis accounts
Expand All @@ -60,17 +58,22 @@ func (gi GenesisInfo) Validate() error {
return fmt.Errorf("too many genesis accounts: %d", len(gi.GenesisAccounts.Accounts))
}

accountSet := make(map[string]struct{})
for _, a := range gi.GenesisAccounts.Accounts {
if err := a.ValidateBasic(); err != nil {
return errors.Join(gerrc.ErrInvalidArgument, err)
}
if _, exists := accountSet[a.Address]; exists {
return fmt.Errorf("duplicate genesis account: %s", a.Address)
}
accountSet[a.Address] = struct{}{}
}
}
return nil
}

func (a GenesisAccount) ValidateBasic() error {
if !a.Amount.IsPositive() {
if !a.Amount.IsNil() && !a.Amount.IsPositive() {
return fmt.Errorf("invalid amount: %s %s", a.Address, a.Amount)
}

Expand Down

0 comments on commit fb0ffb7

Please sign in to comment.