From 033aefc6dd2f2d0a029168679e9e08b8b20431ed Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Thu, 20 Jun 2024 12:50:55 +0300 Subject: [PATCH] validate genesis --- x/tokenfactory/keeper/genesis.go | 6 ++++-- x/tokenfactory/types/errors.go | 23 +++++++++++---------- x/tokenfactory/types/genesis.go | 4 ++++ x/tokenfactory/types/genesis_test.go | 30 ++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/x/tokenfactory/keeper/genesis.go b/x/tokenfactory/keeper/genesis.go index 7957edf90..f013f6ac0 100644 --- a/x/tokenfactory/keeper/genesis.go +++ b/x/tokenfactory/keeper/genesis.go @@ -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) + } } } } diff --git a/x/tokenfactory/types/errors.go b/x/tokenfactory/types/errors.go index c20e41980..2ade10b9d 100644 --- a/x/tokenfactory/types/errors.go +++ b/x/tokenfactory/types/errors.go @@ -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") ) diff --git a/x/tokenfactory/types/genesis.go b/x/tokenfactory/types/genesis.go index fc265c124..683f49df7 100644 --- a/x/tokenfactory/types/genesis.go +++ b/x/tokenfactory/types/genesis.go @@ -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 diff --git a/x/tokenfactory/types/genesis_test.go b/x/tokenfactory/types/genesis_test.go index 7e19f69b7..e3039050b 100644 --- a/x/tokenfactory/types/genesis_test.go +++ b/x/tokenfactory/types/genesis_test.go @@ -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()