2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-01-10 15:32:56 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/presto
|
|
|
|
import pkg/libp2p
|
2022-04-06 00:34:29 +00:00
|
|
|
import pkg/stew/base10
|
2022-05-11 08:51:59 +00:00
|
|
|
import pkg/stew/byteutils
|
2022-05-12 21:52:03 +00:00
|
|
|
import pkg/confutils
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/libp2p/routing_record
|
|
|
|
|
|
|
|
import ../node
|
2022-04-06 00:34:29 +00:00
|
|
|
import ../blocktype
|
2022-05-12 21:52:03 +00:00
|
|
|
import ../conf
|
2022-04-21 08:12:16 +00:00
|
|
|
import ../contracts
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
import ./json
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc validate(
|
|
|
|
pattern: string,
|
|
|
|
value: string): int
|
|
|
|
{.gcsafe, raises: [Defect].} =
|
|
|
|
0
|
|
|
|
|
|
|
|
proc encodeString(cid: type Cid): Result[string, cstring] =
|
|
|
|
ok($cid)
|
|
|
|
|
|
|
|
proc decodeString(T: type Cid, value: string): Result[Cid, cstring] =
|
2022-04-06 00:34:29 +00:00
|
|
|
Cid
|
|
|
|
.init(value)
|
|
|
|
.mapErr do(e: CidError) -> cstring:
|
|
|
|
case e
|
|
|
|
of CidError.Incorrect: "Incorrect Cid"
|
|
|
|
of CidError.Unsupported: "Unsupported Cid"
|
|
|
|
of CidError.Overrun: "Overrun Cid"
|
|
|
|
else: "Error parsing Cid"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
proc encodeString(peerId: PeerID): Result[string, cstring] =
|
|
|
|
ok($peerId)
|
|
|
|
|
|
|
|
proc decodeString(T: type PeerID, value: string): Result[PeerID, cstring] =
|
|
|
|
PeerID.init(value)
|
|
|
|
|
|
|
|
proc encodeString(address: MultiAddress): Result[string, cstring] =
|
|
|
|
ok($address)
|
|
|
|
|
|
|
|
proc decodeString(T: type MultiAddress, value: string): Result[MultiAddress, cstring] =
|
|
|
|
MultiAddress
|
|
|
|
.init(value)
|
|
|
|
.mapErr do(e: string) -> cstring: cstring(e)
|
|
|
|
|
2022-04-06 00:34:29 +00:00
|
|
|
proc decodeString(T: type SomeUnsignedInt, value: string): Result[T, cstring] =
|
|
|
|
Base10.decode(T, value)
|
|
|
|
|
|
|
|
proc encodeString(value: SomeUnsignedInt): Result[string, cstring] =
|
|
|
|
ok(Base10.toString(value))
|
|
|
|
|
|
|
|
proc decodeString(T: type Duration, value: string): Result[T, cstring] =
|
|
|
|
let v = ? Base10.decode(uint32, value)
|
|
|
|
ok(v.minutes)
|
|
|
|
|
|
|
|
proc encodeString(value: Duration): Result[string, cstring] =
|
|
|
|
ok($value)
|
|
|
|
|
|
|
|
proc decodeString(T: type bool, value: string): Result[T, cstring] =
|
|
|
|
try:
|
|
|
|
ok(value.parseBool())
|
|
|
|
except CatchableError as exc:
|
|
|
|
let s: cstring = exc.msg
|
|
|
|
err(s) # err(exc.msg) won't compile
|
|
|
|
|
|
|
|
proc encodeString(value: bool): Result[string, cstring] =
|
|
|
|
ok($value)
|
|
|
|
|
2022-04-21 08:12:16 +00:00
|
|
|
proc decodeString(_: type UInt256, value: string): Result[UInt256, cstring] =
|
|
|
|
try:
|
|
|
|
ok UInt256.fromHex(value)
|
|
|
|
except ValueError as e:
|
|
|
|
err e.msg.cstring
|
|
|
|
|
2022-05-11 08:51:59 +00:00
|
|
|
proc decodeString(_: type array[32, byte],
|
|
|
|
value: string): Result[array[32, byte], cstring] =
|
|
|
|
try:
|
|
|
|
ok array[32, byte].fromHex(value)
|
|
|
|
except ValueError as e:
|
|
|
|
err e.msg.cstring
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
|
2022-01-10 15:32:56 +00:00
|
|
|
var router = RestRouter.init(validate)
|
|
|
|
router.api(
|
|
|
|
MethodGet,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/connect/{peerId}") do (
|
2022-01-10 15:32:56 +00:00
|
|
|
peerId: PeerID,
|
|
|
|
addrs: seq[MultiAddress]) -> RestApiResponse:
|
2022-01-10 20:35:52 +00:00
|
|
|
## Connect to a peer
|
|
|
|
##
|
|
|
|
## If `addrs` param is supplied, it will be used to
|
|
|
|
## dial the peer, otherwise the `peerId` is used
|
|
|
|
## to invoke peer discovery, if it succeeds
|
|
|
|
## the returned addresses will be used to dial
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if peerId.isErr:
|
|
|
|
return RestApiResponse.error(
|
|
|
|
Http400,
|
|
|
|
$peerId.error())
|
|
|
|
|
|
|
|
let addresses = if addrs.isOk and addrs.get().len > 0:
|
|
|
|
addrs.get()
|
|
|
|
else:
|
2022-04-13 16:32:35 +00:00
|
|
|
without peerRecord =? (await node.findPeer(peerId.get())):
|
2022-01-10 15:32:56 +00:00
|
|
|
return RestApiResponse.error(
|
|
|
|
Http400,
|
|
|
|
"Unable to find Peer!")
|
2022-04-13 16:32:35 +00:00
|
|
|
peerRecord.addresses.mapIt(it.address)
|
2022-01-20 01:07:30 +00:00
|
|
|
try:
|
|
|
|
await node.connect(peerId.get(), addresses)
|
|
|
|
return RestApiResponse.response("Successfully connected to peer")
|
|
|
|
except DialFailedError as e:
|
|
|
|
return RestApiResponse.error(Http400, "Unable to dial peer")
|
|
|
|
except CatchableError as e:
|
|
|
|
return RestApiResponse.error(Http400, "Unknown error dialling peer")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
router.api(
|
|
|
|
MethodGet,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/download/{id}") do (
|
2022-01-10 15:32:56 +00:00
|
|
|
id: Cid, resp: HttpResponseRef) -> RestApiResponse:
|
2022-01-10 20:35:52 +00:00
|
|
|
## Download a file from the node in a streaming
|
|
|
|
## manner
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if id.isErr:
|
|
|
|
return RestApiResponse.error(
|
|
|
|
Http400,
|
|
|
|
$id.error())
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
var
|
|
|
|
stream: LPStream
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
var bytes = 0
|
|
|
|
try:
|
2022-04-06 00:34:29 +00:00
|
|
|
without stream =? (await node.retrieve(id.get())), error:
|
|
|
|
return RestApiResponse.error(Http404, error.msg)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-01-20 01:07:30 +00:00
|
|
|
resp.addHeader("Content-Type", "application/octet-stream")
|
2022-01-10 15:32:56 +00:00
|
|
|
await resp.prepareChunked()
|
2022-03-30 02:43:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
while not stream.atEof:
|
|
|
|
var
|
2022-04-06 00:34:29 +00:00
|
|
|
buff = newSeqUninitialized[byte](BlockSize)
|
2022-01-10 15:32:56 +00:00
|
|
|
len = await stream.readOnce(addr buff[0], buff.len)
|
|
|
|
|
|
|
|
buff.setLen(len)
|
|
|
|
if buff.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
bytes += buff.len
|
2022-02-03 14:02:18 +00:00
|
|
|
trace "Sending chunk", size = buff.len
|
2022-01-10 15:32:56 +00:00
|
|
|
await resp.sendChunk(addr buff[0], buff.len)
|
2022-02-03 14:02:18 +00:00
|
|
|
await resp.finish()
|
2022-01-10 15:32:56 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Excepting streaming blocks", exc = exc.msg
|
|
|
|
return RestApiResponse.error(Http500)
|
|
|
|
finally:
|
|
|
|
trace "Sent bytes", cid = id.get(), bytes
|
2022-03-30 02:43:35 +00:00
|
|
|
if not stream.isNil:
|
|
|
|
await stream.close()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
router.rawApi(
|
2022-04-06 00:34:29 +00:00
|
|
|
MethodPost,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/storage/request/{cid}") do (cid: Cid) -> RestApiResponse:
|
2022-04-06 00:34:29 +00:00
|
|
|
## Create a request for storage
|
|
|
|
##
|
2022-05-10 12:13:39 +00:00
|
|
|
## cid - the cid of a previously uploaded dataset
|
2022-04-06 00:34:29 +00:00
|
|
|
## duration - the duration of the contract
|
2022-05-10 12:13:39 +00:00
|
|
|
## maxPrice - the maximum price the client is willing to pay
|
2022-04-06 00:34:29 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
# TODO: store on multiple nodes
|
|
|
|
let nodes: uint = 1
|
|
|
|
let tolerance: uint = 0
|
2022-04-06 00:34:29 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
without cid =? cid.tryGet.catch, error:
|
|
|
|
return RestApiResponse.error(Http400, error.msg)
|
2022-04-06 00:34:29 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
let body = await request.getBody()
|
2022-04-06 00:34:29 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
without params =? StorageRequestParams.fromJson(body), error:
|
|
|
|
return RestApiResponse.error(Http400, error.msg)
|
|
|
|
|
2022-05-11 07:01:31 +00:00
|
|
|
without purchaseId =? await node.requestStorage(cid,
|
2022-05-10 12:13:39 +00:00
|
|
|
params.duration,
|
|
|
|
nodes,
|
|
|
|
tolerance,
|
2022-05-18 11:28:34 +00:00
|
|
|
params.maxPrice,
|
|
|
|
params.expiry), error:
|
2022-05-10 12:13:39 +00:00
|
|
|
return RestApiResponse.error(Http500, error.msg)
|
|
|
|
|
2022-05-11 07:01:31 +00:00
|
|
|
return RestApiResponse.response(purchaseId.toHex)
|
2022-04-06 00:34:29 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
router.rawApi(
|
|
|
|
MethodPost,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/upload") do (
|
2022-01-10 15:32:56 +00:00
|
|
|
) -> RestApiResponse:
|
2022-01-10 20:35:52 +00:00
|
|
|
## Upload a file in a streamming manner
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Handling file upload"
|
|
|
|
var bodyReader = request.getBodyReader()
|
|
|
|
if bodyReader.isErr():
|
|
|
|
return RestApiResponse.error(Http500)
|
|
|
|
|
|
|
|
# Attempt to handle `Expect` header
|
2022-01-10 20:35:52 +00:00
|
|
|
# some clients (curl), wait 1000ms
|
2022-01-10 15:32:56 +00:00
|
|
|
# before giving up
|
|
|
|
#
|
|
|
|
await request.handleExpect()
|
|
|
|
|
|
|
|
let
|
|
|
|
reader = bodyReader.get()
|
|
|
|
stream = BufferStream.new()
|
|
|
|
storeFut = node.store(stream)
|
|
|
|
|
|
|
|
var bytes = 0
|
|
|
|
try:
|
|
|
|
while not reader.atEof:
|
|
|
|
var
|
2022-04-06 00:34:29 +00:00
|
|
|
buff = newSeqUninitialized[byte](BlockSize)
|
2022-01-10 15:32:56 +00:00
|
|
|
len = await reader.readOnce(addr buff[0], buff.len)
|
|
|
|
|
|
|
|
buff.setLen(len)
|
|
|
|
if len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
trace "Got chunk from endpoint", len = buff.len
|
|
|
|
await stream.pushData(buff)
|
|
|
|
bytes += len
|
|
|
|
|
|
|
|
await stream.pushEof()
|
2022-04-06 00:34:29 +00:00
|
|
|
without cid =? (await storeFut), error:
|
|
|
|
return RestApiResponse.error(Http500, error.msg)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
trace "Uploaded file", bytes, cid = $cid
|
|
|
|
return RestApiResponse.response($cid)
|
|
|
|
except CancelledError as exc:
|
|
|
|
await reader.closeWait()
|
|
|
|
return RestApiResponse.error(Http500)
|
|
|
|
except AsyncStreamError:
|
|
|
|
await reader.closeWait()
|
|
|
|
return RestApiResponse.error(Http500)
|
|
|
|
finally:
|
|
|
|
await stream.close()
|
|
|
|
await reader.closeWait()
|
|
|
|
|
|
|
|
# if we got here something went wrong?
|
|
|
|
return RestApiResponse.error(Http500)
|
|
|
|
|
|
|
|
router.api(
|
|
|
|
MethodGet,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/info") do () -> RestApiResponse:
|
2022-01-10 20:35:52 +00:00
|
|
|
## Print rudimentary node information
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
var addrs: string
|
|
|
|
for a in node.switch.peerInfo.addrs:
|
|
|
|
addrs &= "- " & $a & "\n"
|
|
|
|
|
|
|
|
return RestApiResponse.response(
|
|
|
|
"Id: " & $node.switch.peerInfo.peerId &
|
2022-05-12 21:52:03 +00:00
|
|
|
"\nAddrs: \n" & addrs &
|
|
|
|
"\nRoot Dir: " & $conf.dataDir)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-09 14:51:08 +00:00
|
|
|
router.api(
|
|
|
|
MethodGet,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/sales/availability") do () -> RestApiResponse:
|
2022-05-09 14:51:08 +00:00
|
|
|
## Returns storage that is for sale
|
|
|
|
|
|
|
|
without contracts =? node.contracts:
|
|
|
|
return RestApiResponse.error(Http503, "Sales unavailable")
|
|
|
|
|
|
|
|
let json = %contracts.sales.available
|
|
|
|
return RestApiResponse.response($json)
|
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
router.rawApi(
|
|
|
|
MethodPost,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/sales/availability") do () -> RestApiResponse:
|
2022-04-21 08:12:16 +00:00
|
|
|
## Add available storage to sell
|
|
|
|
##
|
|
|
|
## size - size of available storage in bytes
|
|
|
|
## duration - maximum time the storage should be sold for (in seconds)
|
|
|
|
## minPrice - minimum price to be paid (in amount of tokens)
|
|
|
|
|
2022-05-09 14:51:08 +00:00
|
|
|
without contracts =? node.contracts:
|
|
|
|
return RestApiResponse.error(Http503, "Sales unavailable")
|
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
let body = await request.getBody()
|
2022-04-21 08:12:16 +00:00
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
without availability =? Availability.fromJson(body), error:
|
|
|
|
return RestApiResponse.error(Http400, error.msg)
|
2022-04-21 08:12:16 +00:00
|
|
|
|
|
|
|
contracts.sales.add(availability)
|
2022-05-09 14:51:08 +00:00
|
|
|
|
|
|
|
let json = %availability
|
|
|
|
return RestApiResponse.response($json)
|
2022-04-21 08:12:16 +00:00
|
|
|
|
2022-05-11 08:51:59 +00:00
|
|
|
router.api(
|
|
|
|
MethodGet,
|
2022-05-19 19:56:03 +00:00
|
|
|
"/api/codex/v1/storage/purchases/{id}") do (
|
2022-05-11 08:51:59 +00:00
|
|
|
id: array[32, byte]) -> RestApiResponse:
|
|
|
|
|
|
|
|
without contracts =? node.contracts:
|
|
|
|
return RestApiResponse.error(Http503, "Purchasing unavailable")
|
|
|
|
|
|
|
|
without id =? id.tryGet.catch, error:
|
|
|
|
return RestApiResponse.error(Http400, error.msg)
|
|
|
|
|
|
|
|
without purchase =? contracts.purchasing.getPurchase(id):
|
|
|
|
return RestApiResponse.error(Http404)
|
|
|
|
|
|
|
|
let json = %purchase
|
|
|
|
|
|
|
|
return RestApiResponse.response($json)
|
|
|
|
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return router
|