2022-03-28 10:28:22 +00:00
|
|
|
import std/sequtils
|
2022-11-08 07:10:17 +00:00
|
|
|
import std/tables
|
|
|
|
import std/hashes
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/market
|
2022-03-23 12:57:48 +00:00
|
|
|
|
2022-03-30 10:51:28 +00:00
|
|
|
export market
|
2022-11-08 07:10:17 +00:00
|
|
|
export tables
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-03-23 12:57:48 +00:00
|
|
|
type
|
|
|
|
MockMarket* = ref object of Market
|
2022-11-08 07:10:17 +00:00
|
|
|
activeRequests*: Table[Address, seq[RequestId]]
|
2022-03-24 12:48:18 +00:00
|
|
|
requested*: seq[StorageRequest]
|
2022-11-08 07:10:17 +00:00
|
|
|
requestEnds*: Table[RequestId, SecondsSince1970]
|
|
|
|
state*: Table[RequestId, RequestState]
|
2022-06-14 07:47:05 +00:00
|
|
|
fulfilled*: seq[Fulfillment]
|
2022-07-21 12:32:38 +00:00
|
|
|
filled*: seq[Slot]
|
2022-11-08 07:10:17 +00:00
|
|
|
withdrawn*: seq[RequestId]
|
2022-07-04 12:14:56 +00:00
|
|
|
signer: Address
|
2022-03-28 10:28:22 +00:00
|
|
|
subscriptions: Subscriptions
|
2022-06-14 07:47:05 +00:00
|
|
|
Fulfillment* = object
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId*: RequestId
|
2022-07-05 08:37:55 +00:00
|
|
|
proof*: seq[byte]
|
|
|
|
host*: Address
|
2022-07-21 12:32:38 +00:00
|
|
|
Slot* = object
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId*: RequestId
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex*: UInt256
|
|
|
|
proof*: seq[byte]
|
|
|
|
host*: Address
|
2022-03-28 10:28:22 +00:00
|
|
|
Subscriptions = object
|
|
|
|
onRequest: seq[RequestSubscription]
|
2022-06-14 07:47:05 +00:00
|
|
|
onFulfillment: seq[FulfillmentSubscription]
|
2022-07-21 12:32:38 +00:00
|
|
|
onSlotFilled: seq[SlotFilledSubscription]
|
2022-08-18 06:01:47 +00:00
|
|
|
onRequestCancelled: seq[RequestCancelledSubscription]
|
2022-11-08 07:10:17 +00:00
|
|
|
onRequestFailed: seq[RequestFailedSubscription]
|
2022-03-28 10:28:22 +00:00
|
|
|
RequestSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
|
|
|
callback: OnRequest
|
2022-06-14 07:47:05 +00:00
|
|
|
FulfillmentSubscription* = ref object of Subscription
|
2022-03-31 08:24:06 +00:00
|
|
|
market: MockMarket
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId
|
2022-06-14 07:47:05 +00:00
|
|
|
callback: OnFulfillment
|
2022-07-21 12:32:38 +00:00
|
|
|
SlotFilledSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256
|
|
|
|
callback: OnSlotFilled
|
2022-08-18 06:01:47 +00:00
|
|
|
RequestCancelledSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId
|
2022-08-18 06:01:47 +00:00
|
|
|
callback: OnRequestCancelled
|
2022-11-08 07:10:17 +00:00
|
|
|
RequestFailedSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
|
|
|
requestId: RequestId
|
|
|
|
callback: OnRequestCancelled
|
|
|
|
|
|
|
|
proc hash*(address: Address): Hash =
|
|
|
|
hash(address.toArray)
|
|
|
|
|
|
|
|
proc hash*(requestId: RequestId): Hash =
|
|
|
|
hash(requestId.toArray)
|
2022-03-23 12:57:48 +00:00
|
|
|
|
2022-07-04 12:14:56 +00:00
|
|
|
proc new*(_: type MockMarket): MockMarket =
|
|
|
|
MockMarket(signer: Address.example)
|
|
|
|
|
|
|
|
method getSigner*(market: MockMarket): Future[Address] {.async.} =
|
|
|
|
return market.signer
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} =
|
2022-03-24 12:48:18 +00:00
|
|
|
market.requested.add(request)
|
2022-07-05 12:19:01 +00:00
|
|
|
var subscriptions = market.subscriptions.onRequest
|
|
|
|
for subscription in subscriptions:
|
2022-04-11 18:03:55 +00:00
|
|
|
subscription.callback(request.id, request.ask)
|
2022-11-08 07:10:17 +00:00
|
|
|
|
|
|
|
method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} =
|
|
|
|
return market.activeRequests[market.signer]
|
2022-03-28 10:28:22 +00:00
|
|
|
|
2022-06-15 12:12:34 +00:00
|
|
|
method getRequest(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: RequestId): Future[?StorageRequest] {.async.} =
|
2022-06-15 12:12:34 +00:00
|
|
|
for request in market.requested:
|
|
|
|
if request.id == id:
|
|
|
|
return some request
|
|
|
|
return none StorageRequest
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method getState*(market: MockMarket,
|
|
|
|
requestId: RequestId): Future[?RequestState] {.async.} =
|
|
|
|
return market.state.?[requestId]
|
|
|
|
|
|
|
|
method getRequestEnd*(market: MockMarket,
|
|
|
|
id: RequestId): Future[SecondsSince1970] {.async.} =
|
|
|
|
return market.requestEnds[id]
|
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
method getHost(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256): Future[?Address] {.async.} =
|
|
|
|
for slot in market.filled:
|
|
|
|
if slot.requestId == requestId and slot.slotIndex == slotIndex:
|
|
|
|
return some slot.host
|
|
|
|
return none Address
|
|
|
|
|
2022-07-21 12:48:35 +00:00
|
|
|
proc emitSlotFilled*(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:48:35 +00:00
|
|
|
slotIndex: UInt256) =
|
|
|
|
var subscriptions = market.subscriptions.onSlotFilled
|
|
|
|
for subscription in subscriptions:
|
|
|
|
if subscription.requestId == requestId and
|
|
|
|
subscription.slotIndex == slotIndex:
|
|
|
|
subscription.callback(requestId, slotIndex)
|
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
proc emitRequestCancelled*(market: MockMarket,
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId) =
|
2022-08-18 06:01:47 +00:00
|
|
|
var subscriptions = market.subscriptions.onRequestCancelled
|
|
|
|
for subscription in subscriptions:
|
|
|
|
if subscription.requestId == requestId:
|
|
|
|
subscription.callback(requestId)
|
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
proc emitRequestFulfilled*(market: MockMarket, requestId: RequestId) =
|
2022-07-21 12:48:35 +00:00
|
|
|
var subscriptions = market.subscriptions.onFulfillment
|
|
|
|
for subscription in subscriptions:
|
|
|
|
if subscription.requestId == requestId:
|
|
|
|
subscription.callback(requestId)
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
proc emitRequestFailed*(market: MockMarket, requestId: RequestId) =
|
|
|
|
var subscriptions = market.subscriptions.onRequestFailed
|
|
|
|
for subscription in subscriptions:
|
|
|
|
if subscription.requestId == requestId:
|
|
|
|
subscription.callback(requestId)
|
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
proc fillSlot*(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256,
|
|
|
|
proof: seq[byte],
|
|
|
|
host: Address) =
|
|
|
|
let slot = Slot(
|
|
|
|
requestId: requestId,
|
|
|
|
slotIndex: slotIndex,
|
|
|
|
proof: proof,
|
|
|
|
host: host
|
|
|
|
)
|
|
|
|
market.filled.add(slot)
|
2022-07-21 12:48:35 +00:00
|
|
|
market.emitSlotFilled(requestId, slotIndex)
|
2022-07-21 12:32:38 +00:00
|
|
|
|
|
|
|
method fillSlot*(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256,
|
|
|
|
proof: seq[byte]) {.async.} =
|
|
|
|
market.fillSlot(requestId, slotIndex, proof, market.signer)
|
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
method withdrawFunds*(market: MockMarket,
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId) {.async.} =
|
2022-11-08 07:10:17 +00:00
|
|
|
market.withdrawn.add(requestId)
|
2022-08-18 06:01:47 +00:00
|
|
|
market.emitRequestCancelled(requestId)
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
method subscribeRequests*(market: MockMarket,
|
|
|
|
callback: OnRequest):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = RequestSubscription(
|
|
|
|
market: market,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onRequest.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
method subscribeFulfillment*(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-06-14 07:47:05 +00:00
|
|
|
callback: OnFulfillment):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = FulfillmentSubscription(
|
2022-03-31 08:24:06 +00:00
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
callback: callback
|
|
|
|
)
|
2022-06-14 07:47:05 +00:00
|
|
|
market.subscriptions.onFulfillment.add(subscription)
|
2022-03-31 08:24:06 +00:00
|
|
|
return subscription
|
|
|
|
|
2022-07-21 12:32:38 +00:00
|
|
|
method subscribeSlotFilled*(market: MockMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-21 12:32:38 +00:00
|
|
|
slotIndex: UInt256,
|
|
|
|
callback: OnSlotFilled):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = SlotFilledSubscription(
|
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
slotIndex: slotIndex,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onSlotFilled.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
method subscribeRequestCancelled*(market: MockMarket,
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId,
|
2022-08-18 06:01:47 +00:00
|
|
|
callback: OnRequestCancelled):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = RequestCancelledSubscription(
|
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onRequestCancelled.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method subscribeRequestFailed*(market: MockMarket,
|
|
|
|
requestId: RequestId,
|
|
|
|
callback: OnRequestFailed):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = RequestFailedSubscription(
|
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onRequestFailed.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
method unsubscribe*(subscription: RequestSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onRequest.keepItIf(it != subscription)
|
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
method unsubscribe*(subscription: FulfillmentSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onFulfillment.keepItIf(it != subscription)
|
2022-07-21 12:32:38 +00:00
|
|
|
|
|
|
|
method unsubscribe*(subscription: SlotFilledSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onSlotFilled.keepItIf(it != subscription)
|
2022-08-18 06:01:47 +00:00
|
|
|
|
|
|
|
method unsubscribe*(subscription: RequestCancelledSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onRequestCancelled.keepItIf(it != subscription)
|
2022-11-08 07:10:17 +00:00
|
|
|
|
|
|
|
method unsubscribe*(subscription: RequestFailedSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onRequestFailed.keepItIf(it != subscription)
|