Skip to content

Commit

Permalink
validate genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0n00gler committed Jun 20, 2024
1 parent 688e7f1 commit 033aefc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
6 changes: 4 additions & 2 deletions x/tokenfactory/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
panic(err)
}

if err := k.setBeforeSendHook(ctx, genDenom.Denom, genDenom.HookContractAddress); err != nil {
panic(err)
if genDenom.HookContractAddress != "" {
if err := k.setBeforeSendHook(ctx, genDenom.Denom, genDenom.HookContractAddress); err != nil {
panic(err)
}
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions x/tokenfactory/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

// x/tokenfactory module sentinel errors
var (
ErrDenomExists = errorsmod.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = errorsmod.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = errorsmod.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = errorsmod.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = errorsmod.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = errorsmod.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = errorsmod.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = errorsmod.Register(ModuleName, 10, "denom does not exist")
ErrBurnFromModuleAccount = errorsmod.Register(ModuleName, 11, "burning from Module Account is not allowed")
ErrTrackBeforeSendOutOfGas = errorsmod.Register(ModuleName, 12, "gas meter hit maximum limit")
ErrDenomExists = errorsmod.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = errorsmod.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = errorsmod.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = errorsmod.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = errorsmod.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = errorsmod.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = errorsmod.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = errorsmod.Register(ModuleName, 10, "denom does not exist")
ErrBurnFromModuleAccount = errorsmod.Register(ModuleName, 11, "burning from Module Account is not allowed")
ErrTrackBeforeSendOutOfGas = errorsmod.Register(ModuleName, 12, "gas meter hit maximum limit")
ErrInvalidHookContractAddress = errorsmod.Register(ModuleName, 13, "invalid hook contract address")
)
4 changes: 4 additions & 0 deletions x/tokenfactory/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (gs GenesisState) Validate() error {
return errorsmod.Wrapf(ErrInvalidAuthorityMetadata, "Invalid admin address (%s)", err)
}
}

if _, err := sdk.AccAddressFromBech32(denom.HookContractAddress); denom.HookContractAddress != "" && err != nil {
return errorsmod.Wrapf(ErrInvalidHookContractAddress, "Invalid hook contract address (%s)", err)
}
}

return nil
Expand Down
30 changes: 30 additions & 0 deletions x/tokenfactory/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ func TestGenesisState_Validate(t *testing.T) {
},
valid: false,
},
{
desc: "empty hook address",
genState: &types.GenesisState{
FactoryDenoms: []types.GenesisDenom{
{
Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/bitcoin",
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "",
},
},
},
valid: true,
},
{
desc: "invalid hook address",
genState: &types.GenesisState{
FactoryDenoms: []types.GenesisDenom{
{
Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/bitcoin",
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "sfsdfsdfsdfs",
},
},
},
valid: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
Expand Down

0 comments on commit 033aefc

Please sign in to comment.