fix: return correct code when missing CID is a Manifest CID; add back CORS headers

This commit is contained in:
gmega 2025-02-07 14:27:46 -03:00
parent f99dbafe3c
commit b6f2b5fe24
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
6 changed files with 38 additions and 10 deletions

View File

@ -280,7 +280,8 @@ proc deleteEntireDataset(self: CodexNodeRef, cid: Cid): Future[?!void] {.async.}
var store = self.networkStore.localStore
if not (await cid in store):
return failure("Dataset's manifest is not available locally - nothing to delete")
# As per the contract for delete*, an absent dataset is not an error.
return success()
without manifestBlock =? await store.getBlock(cid), err:
return failure(err)
@ -288,10 +289,10 @@ proc deleteEntireDataset(self: CodexNodeRef, cid: Cid): Future[?!void] {.async.}
without manifest =? Manifest.decode(manifestBlock), err:
return failure(err)
let maxContinousRuntime = initDuration(milliseconds = 100)
let runtimeQuota = initDuration(milliseconds = 100)
var lastIdle = getTime()
for i in 0 ..< manifest.blocksCount:
if (getTime() - lastIdle) >= maxContinousRuntime:
if (getTime() - lastIdle) >= runtimeQuota:
await idleAsync()
lastIdle = getTime()

View File

@ -278,7 +278,11 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
if err =? (await node.delete(cid.get())).errorOption:
return RestApiResponse.error(Http500, err.msg, headers = headers)
return RestApiResponse.response("", Http204, headers = headers)
if corsOrigin =? allowedOrigin:
resp.setCorsHeaders("DELETE", corsOrigin)
resp.status = Http204
await resp.sendBody("")
router.api(MethodPost, "/api/codex/v1/data/{cid}/network") do(
cid: Cid, resp: HttpResponseRef

View File

@ -8,6 +8,7 @@ import pkg/codex/stores
import pkg/codex/blocktype as bt
import pkg/codex/sales
import pkg/codex/merkletree
import pkg/codex/manifest
import ../examples
export examples
@ -51,6 +52,15 @@ proc example*(_: type BlockExcPeerCtx): BlockExcPeerCtx =
proc example*(_: type Cid): Cid =
bt.Block.example.cid
proc example*(_: type Manifest): Manifest =
Manifest.new(
treeCid = Cid.example,
blockSize = 256.NBytes,
datasetSize = 4096.NBytes,
filename = "example.txt".some,
mimetype = "text/plain".some,
)
proc example*(_: type MultiHash, mcodec = Sha256HashCodec): MultiHash =
let bytes = newSeqWith(256, rand(uint8))
MultiHash.digest($mcodec, bytes).tryGet()

View File

@ -15,9 +15,7 @@ import pkg/codex/rng
import ../helpers
proc storeManifest*(
store: BlockStore, manifest: Manifest
): Future[?!bt.Block] {.async.} =
proc makeManifestBlock*(manifest: Manifest): ?!bt.Block =
without encodedVerifiable =? manifest.encode(), err:
trace "Unable to encode manifest"
return failure(err)
@ -26,6 +24,15 @@ proc storeManifest*(
trace "Unable to create block from manifest"
return failure(error)
success blk
proc storeManifest*(
store: BlockStore, manifest: Manifest
): Future[?!bt.Block] {.async.} =
without blk =? makeManifestBlock(manifest), err:
trace "Unable to create manifest block", err = err.msg
return failure(err)
if err =? (await store.putBlock(blk)).errorOption:
trace "Unable to store manifest block", cid = blk.cid, err = err.msg
return failure(err)

View File

@ -2,9 +2,11 @@ import std/httpclient
import std/sequtils
from pkg/libp2p import `==`, `$`, Cid
import pkg/codex/units
import pkg/codex/manifest
import ./twonodes
import ../examples
import ../codex/examples
import ../codex/slots/helpers
import json
twonodessuite "REST API":
@ -274,7 +276,12 @@ twonodessuite "REST API":
response = client1.downloadRaw($cid, local = true)
check response.status == "404 Not Found"
test "should return 200 when attempting delete of non-existing dataset",
twoNodesConfig:
test "should return 200 when attempting delete of non-existing block", twoNodesConfig:
let response = client1.deleteRaw($(Cid.example()))
check response.status == "204 No Content"
test "should return 200 when attempting delete of non-existing dataset",
twoNodesConfig:
let cid = Manifest.example().makeManifestBlock().get.cid
let response = client1.deleteRaw($cid)
check response.status == "204 No Content"

View File

@ -1,4 +1,3 @@
import std/os
import std/macros
import pkg/questionable
import ./multinodes