diff --git a/storage/stores/repostore/operations.nim b/storage/stores/repostore/operations.nim index a6fdb992..d83c46b4 100644 --- a/storage/stores/repostore/operations.nim +++ b/storage/stores/repostore/operations.nim @@ -157,7 +157,7 @@ proc updateBlockMetadata*( BlockMetadata( size: currBlockMd.size, expiry: max(currBlockMd.expiry, minExpiry), - refCount: currBlockMd.refCount + plusRefCount - minusRefCount, + refCount: max(0, currBlockMd.refCount + plusRefCount - minusRefCount), ).some else: raise newException( diff --git a/storage/stores/repostore/store.nim b/storage/stores/repostore/store.nim index feec3807..6864e0bf 100644 --- a/storage/stores/repostore/store.nim +++ b/storage/stores/repostore/store.nim @@ -272,7 +272,8 @@ method delBlock*( error "Failed to delete leaf metadata, block will remain on disk.", err = err.msg return failure(err) - if err =? + if leafMd.blkCid.mcodec == BlockCodec and + err =? (await self.updateBlockMetadata(leafMd.blkCid, minusRefCount = 1)).errorOption: if not (err of BlockNotFoundError): return failure(err)