From 466f109193ecde38fa4f3ece0fac7b6e78c78ee0 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 11 May 2022 09:01:31 +0200 Subject: [PATCH] [purchasing] start purchase through REST API --- dagger/node.nim | 30 ++++++++++++++++++++++++++++-- dagger/purchasing.nim | 3 +++ dagger/rest/api.nim | 4 ++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/dagger/node.nim b/dagger/node.nim index 8ba37409..202b607e 100644 --- a/dagger/node.nim +++ b/dagger/node.nim @@ -212,7 +212,7 @@ proc requestStorage*(self: DaggerNodeRef, duration: UInt256, nodes: uint, tolerance: uint, - maxPrice: UInt256): Future[?!Cid] {.async.} = + maxPrice: UInt256): Future[?!array[32, byte]] {.async.} = ## Initiate a request for storage sequence, this might ## be a multistep procedure. ## @@ -224,6 +224,10 @@ proc requestStorage*(self: DaggerNodeRef, ## trace "Received a request for storage!", cid, duration, nodes, tolerance, maxPrice + without contracts =? self.contracts: + trace "Purchasing not available" + return failure "Purchasing not available" + without blk =? (await self.blockStore.getBlock(cid)), error: trace "Unable to retrieve manifest block", cid return failure(error) @@ -258,7 +262,29 @@ proc requestStorage*(self: DaggerNodeRef, trace "Unable to store encoded manifest block", cid = encodedBlk.cid return failure("Unable to store encoded manifest block") - return encodedBlk.cid.success + let request = StorageRequest( + ask: StorageAsk( + size: encoded.size.u256, + duration: duration, + maxPrice: maxPrice + ), + content: StorageContent( + cid: $encodedBlk.cid, + erasure: StorageErasure( + totalChunks: encoded.len.uint64, + totalNodes: 1, # TODO: store on multiple nodes + nodeId: 0 # TODO: store on multiple nodes + ), + por: StoragePor( + u: @[], # TODO: PoR setup + publicKey: @[], # TODO: PoR setup + name: @[] # TODO: PoR setup + ) + ) + ) + + let purchase = contracts.purchasing.purchase(request) + return success purchase.id proc new*( T: type DaggerNodeRef, diff --git a/dagger/purchasing.nim b/dagger/purchasing.nim index 7eefa6d2..f24a86cd 100644 --- a/dagger/purchasing.nim +++ b/dagger/purchasing.nim @@ -83,3 +83,6 @@ proc start(purchase: Purchase) = proc wait*(purchase: Purchase) {.async.} = await purchase.future + +func id*(purchase: Purchase): array[32, byte] = + purchase.request.id diff --git a/dagger/rest/api.nim b/dagger/rest/api.nim index ac9b96c5..bf2dd747 100644 --- a/dagger/rest/api.nim +++ b/dagger/rest/api.nim @@ -196,14 +196,14 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter = without params =? StorageRequestParams.fromJson(body), error: return RestApiResponse.error(Http400, error.msg) - without storageCid =? await node.requestStorage(cid, + without purchaseId =? await node.requestStorage(cid, params.duration, nodes, tolerance, params.maxPrice), error: return RestApiResponse.error(Http500, error.msg) - return RestApiResponse.response($storageCid) + return RestApiResponse.response(purchaseId.toHex) router.rawApi( MethodPost,