[purchasing] start purchase through REST API

This commit is contained in:
Mark Spanbroek 2022-05-11 09:01:31 +02:00 committed by markspanbroek
parent 7d03e83bfc
commit 466f109193
3 changed files with 33 additions and 4 deletions

View File

@ -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,

View File

@ -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

View File

@ -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,