From 8e337478f2943c63eefab4cd45ecc23f4b1e7217 Mon Sep 17 00:00:00 2001 From: E M <5089238+emizzle@users.noreply.github.com> Date: Wed, 25 Feb 2026 20:13:54 +1100 Subject: [PATCH] Prevent further underflow when deleting blocks --- storage/stores/repostore/operations.nim | 2 +- storage/stores/repostore/store.nim | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) 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)