Skip to content

Commit

Permalink
Add support for deleting attribute after name removed
Browse files Browse the repository at this point in the history
This fixes an issue uncovered during simulation testing where deleting an attribute would fail when the name was no longer present for ownership verification.
  • Loading branch information
iramiller committed Jun 9, 2021
1 parent 26230f6 commit c524739
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion x/attribute/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func (k Keeper) DeleteAttribute(ctx sdk.Context, acc sdk.AccAddress, name string
}
// Verify name resolves to owner
if !k.nameKeeper.ResolvesTo(ctx, name, owner) {
return fmt.Errorf("\"%s\" does not resolve to address \"%s\"", name, owner.String())
if k.nameKeeper.NameExists(ctx, name) {
return fmt.Errorf("\"%s\" does not resolve to address \"%s\"", name, owner.String())
}
// else name does not exist (anymore) so we can't enforce permission check on delete here, proceed.
}
// Delete all keys that match the name prefix
store := ctx.KVStore(k.storeKey)
Expand Down
12 changes: 8 additions & 4 deletions x/attribute/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ func SimulateMsgDeleteAttribute(k keeper.Keeper, ak authkeeper.AccountKeeperI, b

randomAttribute := attributes[r.Intn(len(attributes))]

// the name associated with this attribute may no longer exist so use the attribute account as a backup account for "owner"
var ownerAddress string
nr, err := nk.GetRecordByName(ctx, randomAttribute.Name)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDeleteAttribute, "name record for existing attributes not found"), nil, err
if err == nil {
ownerAddress = nr.Address
} else {
ownerAddress = randomAttribute.Address
}

simAccount, _ := simtypes.FindAccount(accs, mustGetAddress(nr.Address))
msg := types.NewMsgDeleteAttributeRequest(mustGetAddress(randomAttribute.Address), mustGetAddress(nr.Address), randomAttribute.Name)
simAccount, _ := simtypes.FindAccount(accs, mustGetAddress(ownerAddress))
msg := types.NewMsgDeleteAttributeRequest(mustGetAddress(randomAttribute.Address), mustGetAddress(ownerAddress), randomAttribute.Name)

return Dispatch(r, app, ctx, ak, bk, simAccount, chainID, msg)
}
Expand Down
1 change: 1 addition & 0 deletions x/attribute/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type NameKeeper interface {
ResolvesTo(ctx sdk.Context, name string, addr sdk.AccAddress) bool
Normalize(ctx sdk.Context, name string) (string, error)
GetRecordByName(ctx sdk.Context, name string) (record *nametypes.NameRecord, err error)
NameExists(ctx sdk.Context, name string) bool
}

0 comments on commit c524739

Please sign in to comment.