Skip to content

Commit b955176

Browse files
committed
Add DeleteRepositoryPackfile message
1 parent 176c291 commit b955176

7 files changed

Lines changed: 894 additions & 155 deletions

File tree

proto/gitopia/gitopia/storage/events.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ message EventPackfileUpdated {
2828
string old_name = 5;
2929
}
3030

31+
// EventPackfileDeleted is emitted when a packfile is deleted
32+
message EventPackfileDeleted {
33+
uint64 repository_id = 1;
34+
string name = 2;
35+
string cid = 3;
36+
}
37+
3138
// EventReleaseAssetUpdated is emitted when a release asset is updated
3239
message EventReleaseAssetUpdated {
3340
uint64 repository_id = 1;

proto/gitopia/gitopia/storage/tx.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ service Msg {
2323
// UpdateRepositoryPackfile updates repository packfile CID and root hash
2424
rpc UpdateRepositoryPackfile(MsgUpdateRepositoryPackfile) returns (MsgUpdateRepositoryPackfileResponse);
2525

26+
// DeleteRepositoryPackfile deletes repository packfile
27+
rpc DeleteRepositoryPackfile(MsgDeleteRepositoryPackfile) returns (MsgDeleteRepositoryPackfileResponse);
28+
2629
// UpdateReleaseAsset updates release asset
2730
rpc UpdateReleaseAsset(MsgUpdateReleaseAsset) returns (MsgUpdateReleaseAssetResponse);
2831

@@ -82,6 +85,14 @@ message MsgUpdateRepositoryPackfile {
8285

8386
message MsgUpdateRepositoryPackfileResponse {}
8487

88+
// MsgDeleteRepositoryPackfile defines a message for deleting repository packfile
89+
message MsgDeleteRepositoryPackfile {
90+
string creator = 1;
91+
uint64 repository_id = 2;
92+
}
93+
94+
message MsgDeleteRepositoryPackfileResponse {}
95+
8596
// MsgUpdateReleaseAsset defines a message for updating release asset
8697
message MsgUpdateReleaseAsset {
8798
string creator = 1;

x/storage/keeper/msg_server.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,62 @@ func (k msgServer) UpdateRepositoryPackfile(goCtx context.Context, msg *types.Ms
277277
return &types.MsgUpdateRepositoryPackfileResponse{}, nil
278278
}
279279

280+
func (k msgServer) DeleteRepositoryPackfile(goCtx context.Context, msg *types.MsgDeleteRepositoryPackfile) (*types.MsgDeleteRepositoryPackfileResponse, error) {
281+
ctx := sdk.UnwrapSDKContext(goCtx)
282+
283+
// Check if provider is active
284+
provider, found := k.GetProvider(ctx, msg.Creator)
285+
if !found || provider.Status != types.ProviderStatus_PROVIDER_STATUS_ACTIVE {
286+
return nil, fmt.Errorf("provider is not active")
287+
}
288+
289+
repository, found := k.gitopiaKeeper.GetRepositoryById(ctx, msg.RepositoryId)
290+
if !found {
291+
return nil, fmt.Errorf("repository not found")
292+
}
293+
294+
userQuota, found := k.gitopiaKeeper.GetUserQuota(ctx, repository.Owner.Id)
295+
if !found {
296+
// Create new user quota
297+
userQuota = gitopiatypes.UserQuota{
298+
Address: repository.Owner.Id,
299+
StorageUsed: 0,
300+
}
301+
}
302+
303+
// Check if packfile exists for this repository
304+
packfile, found := k.GetPackfile(ctx, msg.RepositoryId)
305+
if !found {
306+
return nil, fmt.Errorf("packfile not found")
307+
}
308+
309+
// Decrement or remove old cid reference count
310+
if packfile.Cid != "" {
311+
k.DecreaseCidReferenceCount(ctx, packfile.Cid)
312+
if count, found := k.GetCidReferenceCount(ctx, packfile.Cid); found && count.Count == 0 {
313+
k.RemoveCidReferenceCount(ctx, packfile.Cid)
314+
}
315+
}
316+
317+
userQuota.StorageUsed -= uint64(packfile.Size_)
318+
k.gitopiaKeeper.SetUserQuota(ctx, userQuota)
319+
320+
// Remove existing packfile
321+
k.RemovePackfile(ctx, msg.RepositoryId)
322+
323+
// Update total storage size
324+
k.SetTotalStorageSize(ctx, k.GetTotalStorageSize(ctx)-uint64(packfile.Size_))
325+
326+
// Emit event
327+
ctx.EventManager().EmitTypedEvent(&types.EventPackfileDeleted{
328+
RepositoryId: msg.RepositoryId,
329+
Name: packfile.Name,
330+
Cid: packfile.Cid,
331+
})
332+
333+
return &types.MsgDeleteRepositoryPackfileResponse{}, nil
334+
}
335+
280336
func (k msgServer) UpdateReleaseAsset(goCtx context.Context, msg *types.MsgUpdateReleaseAsset) (*types.MsgUpdateReleaseAssetResponse, error) {
281337
ctx := sdk.UnwrapSDKContext(goCtx)
282338

x/storage/types/codec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) {
1515
cdc.RegisterConcrete(&MsgUpdateProvider{}, "storage/UpdateProvider", nil)
1616
cdc.RegisterConcrete(&MsgUpdateParams{}, "storage/UpdateParams", nil)
1717
cdc.RegisterConcrete(&MsgUpdateRepositoryPackfile{}, "storage/UpdateRepositoryPackfile", nil)
18+
cdc.RegisterConcrete(&MsgDeleteRepositoryPackfile{}, "storage/DeleteRepositoryPackfile", nil)
1819
cdc.RegisterConcrete(&MsgSubmitChallengeResponse{}, "storage/SubmitChallengeResponse", nil)
1920
cdc.RegisterConcrete(&MsgWithdrawProviderRewards{}, "storage/WithdrawProviderRewards", nil)
2021
cdc.RegisterConcrete(&MsgUnregisterProvider{}, "storage/UnregisterProvider", nil)
@@ -31,6 +32,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
3132
&MsgUpdateProvider{},
3233
&MsgUpdateParams{},
3334
&MsgUpdateRepositoryPackfile{},
35+
&MsgDeleteRepositoryPackfile{},
3436
&MsgSubmitChallengeResponse{},
3537
&MsgWithdrawProviderRewards{},
3638
&MsgUnregisterProvider{},

0 commit comments

Comments
 (0)