Add node storage fetch (network download to local node)

This commit is contained in:
Arnaud 2025-10-01 09:31:48 +02:00
parent 31db4f41a1
commit e904355d72
No known key found for this signature in database
GPG Key ID: B8FBC178F10CA7AE
2 changed files with 60 additions and 3 deletions

View File

@ -148,6 +148,10 @@ package main
return codex_storage_list(codexCtx, (CodexCallback) callback, resp);
}
static int cGoCodexStorageFetch(void* codexCtx, char* cid, void* resp) {
return codex_storage_fetch(codexCtx, cid, (CodexCallback) callback, resp);
}
static int cGoCodexStart(void* codexCtx, void* resp) {
return codex_start(codexCtx, (CodexCallback) callback, resp);
}
@ -965,6 +969,33 @@ func (self CodexNode) CodexStorageList() ([]CodexManifest, error) {
return list, err
}
func (self CodexNode) CodexStorageFetch(cid string) (CodexManifest, error) {
bridge := newBridgeCtx()
defer bridge.free()
var cCid = C.CString(cid)
defer C.free(unsafe.Pointer(cCid))
if C.cGoCodexStorageFetch(self.ctx, cCid, bridge.resp) != C.RET_OK {
return CodexManifest{}, bridge.CallError("cGoCodexStorageFetch")
}
value, err := bridge.wait()
if err != nil {
return CodexManifest{}, err
}
var manifest CodexManifest
err = json.Unmarshal([]byte(value), &manifest)
if err != nil {
return CodexManifest{}, err
}
manifest.Cid = cid
return manifest, nil
}
func (self CodexNode) CodexStart() error {
bridge := newBridgeCtx()
defer bridge.free()
@ -1210,6 +1241,13 @@ func main() {
}
log.Println("Storage List content:", manifests)
manifest, err = node.CodexStorageFetch(cid)
if err != nil {
log.Fatal("Error happened:", err.Error())
}
log.Println("Storage Fetch content:", manifest)
// }
// err = node.CodexConnect(peerId, []string{})

View File

@ -1,6 +1,11 @@
{.push raises: [].}
## This file contains the node storage request.
## 4 operations are available:
## - LIST: list all manifests stored in the node.
## - DELETE: Deletes either a single block or an entire dataset from the local node.
## - FETCH: download a file from the network to the local node.
## - SPACE: get the amount of space used by the local node.
import std/[options]
import chronos
@ -11,7 +16,7 @@ import ../../alloc
import ../../../codex/manifest
from ../../../codex/codex import CodexServer, node
from ../../../codex/node import iterateManifests
from ../../../codex/node import iterateManifests, fetchManifest, fetchDatasetAsyncTask
from libp2p import Cid, init, `$`
logScope:
@ -67,9 +72,23 @@ proc delete(
return err("DELETE operation not implemented yet.")
proc fetch(
codex: ptr CodexServer, cid: cstring
codex: ptr CodexServer, cCid: cstring
): Future[Result[string, string]] {.async: (raises: []).} =
return err("FETCH operation not implemented yet.")
let cid = Cid.init($cCid)
if cid.isErr:
return err("Failed to fetch the data: cannot parse cid: " & $cCid)
try:
let node = codex[].node
let manifest = await node.fetchManifest(cid.get())
if manifest.isErr:
return err("Failed to fetch the data: " & manifest.error.msg)
node.fetchDatasetAsyncTask(manifest.get())
return ok(serde.toJson(manifest.get()))
except CancelledError:
return err("Failed to fetch the data: download cancelled.")
proc space(
codex: ptr CodexServer