From 075c6313440bd1aeb6a1bbe20d4ae7734d831ee6 Mon Sep 17 00:00:00 2001 From: 0xsaxit Date: Wed, 13 Sep 2023 19:50:03 +1000 Subject: [PATCH 1/3] remove abandoned record ids from request records --- RELEASE.md | 2 +- types/constants.go | 2 +- x/gov/keeper/identity_registrar.go | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 2cb0195d..fb082a68 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,3 +1,3 @@ Features: -- Add a gov gRPC endpoint & CLI for querying custom prefixes +- Remove abandoned record ids from request records. diff --git a/types/constants.go b/types/constants.go index c87ebdef..8cecf810 100644 --- a/types/constants.go +++ b/types/constants.go @@ -3,6 +3,6 @@ package types const ( // we set page iteration limit for safety PageIterationLimit = 512 - SekaiVersion = "v0.3.29" + SekaiVersion = "v0.3.30" CosmosVersion = "v0.45.10" ) diff --git a/x/gov/keeper/identity_registrar.go b/x/gov/keeper/identity_registrar.go index 15c41ebe..ea23aee9 100644 --- a/x/gov/keeper/identity_registrar.go +++ b/x/gov/keeper/identity_registrar.go @@ -242,15 +242,37 @@ func (k Keeper) DeleteIdentityRecords(ctx sdk.Context, address sdk.AccAddress, k } } + recordIdMap := make(map[uint64]bool) for _, recordId := range recordIds { prevRecord := k.GetIdentityRecordById(ctx, recordId) if prevRecord == nil { return sdkerrors.Wrap(types.ErrInvalidIdentityRecordId, fmt.Sprintf("identity record with specified id does NOT exist: id=%d", recordId)) } + recordIdMap[recordId] = true k.DeleteIdentityRecordById(ctx, recordId) } + // remove record ids from verification request list + requests := k.GetAllIdRecordsVerifyRequests(ctx) + for _, request := range requests { + recordIds := []uint64{} + for _, recordid := range request.RecordIds { + if !recordIdMap[recordid] { + recordIds = append(recordIds, recordid) + } + } + + if len(recordIds) == 0 { + err := k.CancelIdentityRecordsVerifyRequest(ctx, sdk.MustAccAddressFromBech32(request.Address), request.Id) + if err != nil { + return err + } + } else { + request.RecordIds = recordIds + k.SetIdentityRecordsVerifyRequest(ctx, request) + } + } return nil } From d2a226b74835c4129085df7e081ce07cef647959 Mon Sep 17 00:00:00 2001 From: tj327 Date: Tue, 19 Sep 2023 00:06:12 +1000 Subject: [PATCH 2/3] decrease time complex for deleting verify request --- x/gov/keeper/identity_registrar.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/x/gov/keeper/identity_registrar.go b/x/gov/keeper/identity_registrar.go index ea23aee9..209e3fe5 100644 --- a/x/gov/keeper/identity_registrar.go +++ b/x/gov/keeper/identity_registrar.go @@ -254,7 +254,7 @@ func (k Keeper) DeleteIdentityRecords(ctx sdk.Context, address sdk.AccAddress, k } // remove record ids from verification request list - requests := k.GetAllIdRecordsVerifyRequests(ctx) + requests := k.GetIdRecordsVerifyRequestsByRequester(ctx, address) for _, request := range requests { recordIds := []uint64{} for _, recordid := range request.RecordIds { @@ -373,6 +373,21 @@ func (k Keeper) SetIdentityRecordsVerifyRequest(ctx sdk.Context, request types.I // RequestIdentityRecordsVerify defines a method to request verify request from specific verifier func (k Keeper) RequestIdentityRecordsVerify(ctx sdk.Context, address, verifier sdk.AccAddress, recordIds []uint64, tip sdk.Coin) (uint64, error) { requestId := k.GetLastIdRecordVerifyRequestId(ctx) + 1 + store := ctx.KVStore(k.storeKey) + prefix := types.IdentityRecordByAddressPrefix(address.String()) + iterator := sdk.KVStorePrefixIterator(store, prefix) + defer iterator.Close() + + idsMap := make(map[uint64]bool) + for ; iterator.Valid(); iterator.Next() { + idsMap[sdk.BigEndianToUint64(iterator.Value())] = true + } + + for _, recordId := range recordIds { + if !idsMap[recordId] { + return requestId, sdkerrors.Wrap(types.ErrInvalidIdentityRecordId, fmt.Sprintf("executor is not owner of the identity record: id=%d", recordId)) + } + } lastRecordEditDate := time.Time{} for _, recordId := range recordIds { From 070e3672355a751625c79f0e46bc1dcb7a1b623c Mon Sep 17 00:00:00 2001 From: tj327 Date: Tue, 19 Sep 2023 00:43:29 +1000 Subject: [PATCH 3/3] fix integration test issues --- x/gov/keeper/identity_registrar_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/x/gov/keeper/identity_registrar_test.go b/x/gov/keeper/identity_registrar_test.go index 253ec7a7..58760cb7 100644 --- a/x/gov/keeper/identity_registrar_test.go +++ b/x/gov/keeper/identity_registrar_test.go @@ -458,12 +458,12 @@ func TestKeeper_IdentityRecordApproveFlow(t *testing.T) { Verifiers: []string{addr3.String()}, }) - // request id record 1 and 2 to addr3 by addr1 again - reqId, err = app.CustomGovKeeper.RequestIdentityRecordsVerify(ctx, addr1, addr3, []uint64{1, 2}, sdk.NewInt64Coin(sdk.DefaultBondDenom, 200)) + // request id record 2 to addr3 by addr1 again + reqId, err = app.CustomGovKeeper.RequestIdentityRecordsVerify(ctx, addr2, addr3, []uint64{2}, sdk.NewInt64Coin(sdk.DefaultBondDenom, 200)) require.Equal(t, reqId, uint64(3)) require.NoError(t, err) - coins = app.BankKeeper.GetAllBalances(ctx, addr1) - require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9400)}) + coins = app.BankKeeper.GetAllBalances(ctx, addr2) + require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9800)}) err = app.CustomGovKeeper.HandleIdentityRecordsVerifyRequest(ctx, addr3, 3, true) require.NoError(t, err) coins = app.BankKeeper.GetAllBalances(ctx, addr3) @@ -534,11 +534,11 @@ func TestKeeper_IdentityRecordApproveFlow(t *testing.T) { // check get queries requests := app.CustomGovKeeper.GetIdRecordsVerifyRequestsByRequester(ctx, addr1) - require.Len(t, requests, 1) + require.Len(t, requests, 0) requests = app.CustomGovKeeper.GetIdRecordsVerifyRequestsByApprover(ctx, addr1) require.Len(t, requests, 0) requests = app.CustomGovKeeper.GetAllIdRecordsVerifyRequests(ctx) - require.Len(t, requests, 1) + require.Len(t, requests, 0) // remove all and query again app.CustomGovKeeper.DeleteIdRecordsVerifyRequest(ctx, 5) @@ -550,7 +550,7 @@ func TestKeeper_IdentityRecordApproveFlow(t *testing.T) { require.Equal(t, reqId, uint64(6)) require.NoError(t, err) coins = app.BankKeeper.GetAllBalances(ctx, addr2) - require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9600)}) + require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9400)}) cacheCtx, _ := ctx.CacheContext() err = app.CustomGovKeeper.CancelIdentityRecordsVerifyRequest(cacheCtx, addr3, 6) require.Error(t, err) @@ -559,7 +559,7 @@ func TestKeeper_IdentityRecordApproveFlow(t *testing.T) { request = app.CustomGovKeeper.GetIdRecordsVerifyRequest(ctx, 6) require.Nil(t, request) coins = app.BankKeeper.GetAllBalances(ctx, addr2) - require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9800)}) + require.Equal(t, coins, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 9600)}) // try deleting request after request creation reqId, err = app.CustomGovKeeper.RequestIdentityRecordsVerify(ctx, addr2, addr3, []uint64{2}, sdk.NewInt64Coin(sdk.DefaultBondDenom, 200))