From 72ae2ceabc2c1ac93b1913c156f1a9cefa749b5e Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 15 May 2024 14:13:20 +0200 Subject: [PATCH] index controlled block deleting --- codex/node.nim | 10 +++++----- codex/rest/api.nim | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/codex/node.nim b/codex/node.nim index c9a810dd..ad5204f4 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -514,7 +514,7 @@ proc requestStorage*( let purchase = await contracts.purchasing.purchase(request) success purchase.id -proc debugDelete*(self: CodexNodeRef, cid: Cid, nBlocks: int): Future[?!int] {.async.} = +proc debugDelete*(self: CodexNodeRef, cid: Cid, blockIndices: seq[int]): Future[?!int] {.async.} = without manifest =? (await self.fetchManifest(cid)), err: trace "Unable to fetch manifest for cid", cid, err = err.msg return failure(err) @@ -524,15 +524,15 @@ proc debugDelete*(self: CodexNodeRef, cid: Cid, nBlocks: int): Future[?!int] {.a treeCid = manifest.treeCid var - index = -1 deleted = 0 - while deleted < nBlocks and index < totalBlocks: - inc index + for index in blockIndices: + if index >= totalBlocks: + return failure("Index out of range. Index: " & $index & " totalBlocks: " & $totalBlocks) without hasBlock =? (await self.networkStore.hasBlock(treeCid, index)), err: return failure(err) if not hasBlock: - continue + return failure("Tree does not have this index: " & $index & " totalBlocks: " & $totalBlocks) else: if err =? (await self.networkStore.delBlock(treeCid, index)).errorOption: return failure(err) diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 7c17e5da..c654fd43 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -640,7 +640,7 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) = when codex_enable_api_debug_delete: type DebugDeleteParams = object - nBlocks* {.serialize.}: int + blockIndices* {.serialize.}: seq[int] router.rawApi( MethodPost, @@ -653,12 +653,12 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) = without params =? DebugDeleteParams.fromJson(body), error: return RestApiResponse.error(Http400, error.msg) - warn "debug/delete is deleting blocks", cid = $cid, nBlocks = params.nBlocks + warn "debug/delete is deleting blocks", cid = $cid, nBlocks = params.blockIndices.len - without deleted =? (await node.debugDelete(cid, params.nBlocks)), err: + without deleted =? (await node.debugDelete(cid, params.blockIndices)), err: return RestApiResponse.error(Http500, err.msg) - warn "debug/delete has deleted blocks", cid = $cid, nBlocks = params.nBlocks, deleted + warn "debug/delete has deleted blocks", cid = $cid, deleted return RestApiResponse.response($deleted) except CatchableError as exc: