diff --git a/codex/contracts/interactions/clientinteractions.nim b/codex/contracts/interactions/clientinteractions.nim index a71aa46f..e44d86ba 100644 --- a/codex/contracts/interactions/clientinteractions.nim +++ b/codex/contracts/interactions/clientinteractions.nim @@ -47,8 +47,8 @@ proc new*(_: type ClientInteractions, ClientInteractions.new("ws://localhost:8545", account) proc start*(self: ClientInteractions) {.async.} = - await self.purchasing.start() await procCall ContractInteractions(self).start() + await self.purchasing.start() proc stop*(self: ClientInteractions) {.async.} = await self.purchasing.stop() diff --git a/codex/contracts/interactions/hostinteractions.nim b/codex/contracts/interactions/hostinteractions.nim index cf69bb9c..65893d5e 100644 --- a/codex/contracts/interactions/hostinteractions.nim +++ b/codex/contracts/interactions/hostinteractions.nim @@ -57,9 +57,9 @@ proc new*(_: type HostInteractions, HostInteractions.new("ws://localhost:8545", account, repo) method start*(self: HostInteractions) {.async.} = + await procCall ContractInteractions(self).start() await self.sales.start() await self.proving.start() - await procCall ContractInteractions(self).start() method stop*(self: HostInteractions) {.async.} = await self.sales.stop() diff --git a/codex/contracts/interactions/interactions.nim b/codex/contracts/interactions/interactions.nim index a8101492..7020bd88 100644 --- a/codex/contracts/interactions/interactions.nim +++ b/codex/contracts/interactions/interactions.nim @@ -38,8 +38,8 @@ proc prepare*( return success((signer, deploy)) -method start*(interactions: ContractInteractions) {.async, base.} = - await interactions.clock.start() +method start*(self: ContractInteractions) {.async, base.} = + await self.clock.start() -method stop*(interactions: ContractInteractions) {.async, base.} = - await interactions.clock.stop() +method stop*(self: ContractInteractions) {.async, base.} = + await self.clock.stop() diff --git a/codex/node.nim b/codex/node.nim index 7ceaf404..ad6e65a1 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -372,9 +372,16 @@ proc start*(node: CodexNodeRef) {.async.} = try: await contracts.start() except CatchableError as error: - error "Unable to start contract interactions: ", error=error.msg + error "Unable to start host contract interactions: ", error=error.msg node.contracts.host = HostInteractions.none + if contracts =? node.contracts.client: + try: + await contracts.start() + except CatchableError as error: + error "Unable to start client contract interactions: ", error=error.msg + node.contracts.client = ClientInteractions.none + node.networkId = node.switch.peerInfo.peerId notice "Started codex node", id = $node.networkId, addrs = node.switch.peerInfo.addrs diff --git a/codex/rest/api.nim b/codex/rest/api.nim index f293e862..2e8416f1 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -247,7 +247,10 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter = without contracts =? node.contracts.host: return RestApiResponse.error(Http503, "Sales unavailable") - let json = %contracts.sales.available + without unused =? (await contracts.sales.reservations.unused), err: + return RestApiResponse.error(Http500, err.msg) + + let json = %unused return RestApiResponse.response($json) router.rawApi( diff --git a/codex/sales/reservations.nim b/codex/sales/reservations.nim index 1ae231bf..d07cea0b 100644 --- a/codex/sales/reservations.nim +++ b/codex/sales/reservations.nim @@ -8,6 +8,7 @@ ## those terms. import std/typetraits + import pkg/chronos import pkg/chronicles import pkg/upraises @@ -274,6 +275,18 @@ proc availabilities*( iter.next = next return success iter +proc unused*(r: Reservations): Future[?!seq[Availability]] {.async.} = + var ret: seq[Availability] = @[] + + without availabilities =? (await r.availabilities), err: + return failure(err) + + for a in availabilities: + if availability =? (await a) and not availability.used: + ret.add availability + + return success(ret) + proc find*( self: Reservations, size, duration, minPrice: UInt256,