Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed how profiles-related data are deleted #784

Merged
merged 9 commits into from
Mar 22, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
module: x/profiles
pull_request: 784
description: Fixed how the profiles data are deleted (DTag transfer requests, chain link and application links)
backward_compatible: true
date: 2022-03-21T12:10:14.687869312Z
32 changes: 10 additions & 22 deletions x/profiles/keeper/keeper_app_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (k Keeper) SaveApplicationLink(ctx sdk.Context, link types.ApplicationLink)
return nil
}

// HasApplicationLink tells whether the given application link exists
func (k Keeper) HasApplicationLink(ctx sdk.Context, user, application, username string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.UserApplicationLinkKey(user, application, username))
}

// GetApplicationLink returns the link for the given application and username.
// If the link is not found returns an error instead.
func (k Keeper) GetApplicationLink(ctx sdk.Context, user, application, username string) (types.ApplicationLink, bool, error) {
Expand Down Expand Up @@ -86,27 +92,10 @@ func (k Keeper) GetApplicationLinkByClientID(ctx sdk.Context, clientID string) (

// DeleteApplicationLink removes the application link associated to the given user,
// for the given application and username
func (k Keeper) DeleteApplicationLink(ctx sdk.Context, user string, application, username string) error {
// Get the link to obtain the client id
link, found, err := k.GetApplicationLink(ctx, user, application, username)
if err != nil {
return err
}

if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "application link not found")
}

if link.User != user {
return sdkerrors.Wrap(sdkerrors.ErrorInvalidSigner, "cannot delete the application link of another user")
}

// Delete the data
func (k Keeper) DeleteApplicationLink(ctx sdk.Context, appLink types.ApplicationLink) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.UserApplicationLinkKey(user, application, username))
store.Delete(types.ApplicationLinkClientIDKey(link.OracleRequest.ClientID))

return nil
store.Delete(types.UserApplicationLinkKey(appLink.User, appLink.Data.Application, appLink.Data.Username))
store.Delete(types.ApplicationLinkClientIDKey(appLink.OracleRequest.ClientID))
}

// DeleteAllUserApplicationLinks delete all the applications links associated with the given user
Expand All @@ -117,8 +106,7 @@ func (k Keeper) DeleteAllUserApplicationLinks(ctx sdk.Context, user string) {
return false
})

store := ctx.KVStore(k.storeKey)
for _, link := range links {
store.Delete(types.UserApplicationLinkKey(link.User, link.Data.Application, link.Data.Username))
k.DeleteApplicationLink(ctx, link)
}
}
124 changes: 90 additions & 34 deletions x/profiles/keeper/keeper_app_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,13 @@ func (suite *KeeperTestSuite) Test_GetApplicationLinkByClientID() {

func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
testCases := []struct {
name string
store func(store sdk.Context)
user string
application string
username string
shouldErr bool
name string
store func(ctx sdk.Context)
link types.ApplicationLink
check func(ctx sdk.Context)
}{
{
name: "wrong user returns error",
name: "different user does not delete link",
store: func(ctx sdk.Context) {
address := "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773"
link := types.NewApplicationLink(
Expand All @@ -341,13 +339,29 @@ func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
err := suite.k.SaveApplicationLink(ctx, link)
suite.Require().NoError(err)
},
user: "user",
application: "twitter",
username: "twitteruser",
shouldErr: true,
link: types.NewApplicationLink(
"cosmos1xvvggrlgjkhu4rva9j500rc52za2smxhluvftc",
types.NewData("twitter", "twitteruser"),
types.ApplicationLinkStateInitialized,
types.NewOracleRequest(
0,
1,
types.NewOracleRequestCallData("twitter", "calldata"),
"client_id",
),
nil,
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
),
check: func(ctx sdk.Context) {
suite.Require().True(suite.k.HasApplicationLink(ctx,
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
"twitter",
"twitteruser",
))
},
},
{
name: "wrong application returns error",
name: "different application does not delete the link",
store: func(ctx sdk.Context) {
address := "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773"
link := types.NewApplicationLink(
Expand All @@ -368,13 +382,29 @@ func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
err := suite.k.SaveApplicationLink(ctx, link)
suite.Require().NoError(err)
},
user: "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
application: "github",
username: "twitteruser",
shouldErr: true,
link: types.NewApplicationLink(
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
types.NewData("github", "twitteruser"),
types.ApplicationLinkStateInitialized,
types.NewOracleRequest(
0,
1,
types.NewOracleRequestCallData("twitter", "calldata"),
"client_id",
),
nil,
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
),
check: func(ctx sdk.Context) {
suite.Require().True(suite.k.HasApplicationLink(ctx,
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
"twitter",
"twitteruser",
))
},
},
{
name: "wrong username returns error",
name: "different username does not delete the link",
store: func(ctx sdk.Context) {
address := "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773"
link := types.NewApplicationLink(
Expand All @@ -395,10 +425,26 @@ func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
err := suite.k.SaveApplicationLink(ctx, link)
suite.Require().NoError(err)
},
user: "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
application: "twitter",
username: "twitter-user",
shouldErr: true,
link: types.NewApplicationLink(
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
types.NewData("twitter", "another-user"),
types.ApplicationLinkStateInitialized,
types.NewOracleRequest(
0,
1,
types.NewOracleRequestCallData("twitter", "calldata"),
"client_id",
),
nil,
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
),
check: func(ctx sdk.Context) {
suite.Require().True(suite.k.HasApplicationLink(ctx,
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
"twitter",
"twitteruser",
))
},
},
{
name: "valid request deletes link",
Expand All @@ -422,10 +468,26 @@ func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
err := suite.k.SaveApplicationLink(ctx, link)
suite.Require().NoError(err)
},
user: "cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
application: "twitter",
username: "twitteruser",
shouldErr: false,
link: types.NewApplicationLink(
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
types.NewData("twitter", "twitteruser"),
types.ApplicationLinkStateInitialized,
types.NewOracleRequest(
0,
1,
types.NewOracleRequestCallData("twitter", "calldata"),
"client_id",
),
nil,
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
),
check: func(ctx sdk.Context) {
suite.Require().False(suite.k.HasApplicationLink(ctx,
"cosmos10nsdxxdvy9qka3zv0lzw8z9cnu6kanld8jh773",
"twitter",
"twitteruser",
))
},
},
}

Expand All @@ -437,15 +499,9 @@ func (suite *KeeperTestSuite) Test_DeleteApplicationLink() {
tc.store(ctx)
}

err := suite.k.DeleteApplicationLink(ctx, tc.user, tc.application, tc.username)
if tc.shouldErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)

_, found, err := suite.k.GetApplicationLink(ctx, tc.user, tc.application, tc.username)
suite.Require().NoError(err)
suite.Require().False(found)
suite.k.DeleteApplicationLink(ctx, tc.link)
if tc.check != nil {
tc.check(ctx)
}
})
}
Expand Down
22 changes: 9 additions & 13 deletions x/profiles/keeper/keeper_chain_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (k Keeper) SaveChainLink(ctx sdk.Context, link types.ChainLink) error {
return nil
}

// HasChainLink tells whether the given chain link exists or not
func (k Keeper) HasChainLink(ctx sdk.Context, owner, chainName, target string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.ChainLinksStoreKey(owner, chainName, target))
}

// GetChainLink returns the chain link for the given owner, chain name and target.
// If such link does not exist, returns false instead.
func (k Keeper) GetChainLink(ctx sdk.Context, owner, chainName, target string) (types.ChainLink, bool) {
Expand All @@ -63,17 +69,9 @@ func (k Keeper) GetChainLink(ctx sdk.Context, owner, chainName, target string) (
}

// DeleteChainLink deletes the link associated with the given address and chain name
func (k Keeper) DeleteChainLink(ctx sdk.Context, owner, chainName, target string) error {
func (k Keeper) DeleteChainLink(ctx sdk.Context, link types.ChainLink) {
store := ctx.KVStore(k.storeKey)
key := types.ChainLinksStoreKey(owner, chainName, target)
if !store.Has(key) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"chain link between %s and %s for chain name %s not found",
owner, target, chainName,
)
}
store.Delete(types.ChainLinksStoreKey(owner, chainName, target))
return nil
store.Delete(types.ChainLinksStoreKey(link.User, link.ChainConfig.Name, link.GetAddressData().GetValue()))
}

// DeleteAllUserChainLinks deletes all the chain links associated with the given user
Expand All @@ -84,9 +82,7 @@ func (k Keeper) DeleteAllUserChainLinks(ctx sdk.Context, user string) {
return false
})

store := ctx.KVStore(k.storeKey)
for _, link := range links {
address := link.Address.GetCachedValue().(types.AddressData)
store.Delete(types.ChainLinksStoreKey(link.User, link.ChainConfig.Name, address.GetValue()))
k.DeleteChainLink(ctx, link)
}
}
Loading