diff --git a/CHANGELOG.md b/CHANGELOG.md index b05f1e9e..6edb5845 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## HEAD - iovnscli: fix has-superuser bool flag bug +- iovnsd: fix duplicate blockchain targets ID - remove flush domain feature ## v0.2.4 diff --git a/x/domain/handler.go b/x/domain/handler.go index 2c3a5bfa..61923681 100644 --- a/x/domain/handler.go +++ b/x/domain/handler.go @@ -233,8 +233,15 @@ func handleMsgRegisterAccount(ctx sdk.Context, k keeper.Keeper, msg *types.MsgRe func validateBlockchainTargets(targets []types.BlockchainAddress, conf configuration.Config) error { validBlockchainID := regexp.MustCompile(conf.ValidBlockchainID) validBlockchainAddress := regexp.MustCompile(conf.ValidBlockchainAddress) + // create blockchain targets set to identify duplicates + sets := make(map[string]struct{}, len(targets)) // iterate over targets to check their validity for _, target := range targets { + // check if blockchain ID was already specified + if _, ok := sets[target.ID]; ok { + return fmt.Errorf("duplicate blockchain ID: %s", target) + } + sets[target.ID] = struct{}{} // is blockchain id valid? if !validBlockchainID.MatchString(target.ID) { return fmt.Errorf("%s is not a valid blockchain ID", target.ID) diff --git a/x/domain/handler_test.go b/x/domain/handler_test.go index e653984f..f0088907 100644 --- a/x/domain/handler_test.go +++ b/x/domain/handler_test.go @@ -2107,3 +2107,43 @@ func Test_handlerMsgTransferDomain(t *testing.T) { runTests(t, cases) } + +func Test_validateBlockchainTargets(t *testing.T) { + type args struct { + targets []types.BlockchainAddress + conf configuration.Config + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "duplicate blockchain target", + args: args{ + targets: []types.BlockchainAddress{ + { + ID: "duplicate", + Address: "does not matter", + }, + { + ID: "duplicate", + Address: "does not matter", + }, + }, + conf: configuration.Config{ + ValidBlockchainID: regexMatchAll, + ValidBlockchainAddress: regexMatchAll, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := validateBlockchainTargets(tt.args.targets, tt.args.conf); (err != nil) != tt.wantErr { + t.Errorf("validateBlockchainTargets() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}