2022-03-28 10:28:22 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/heapqueue
|
2022-03-31 08:24:06 +00:00
|
|
|
import pkg/questionable
|
2022-03-23 12:57:48 +00:00
|
|
|
import pkg/dagger/market
|
|
|
|
|
2022-03-30 10:51:28 +00:00
|
|
|
export market
|
|
|
|
|
2022-03-23 12:57:48 +00:00
|
|
|
type
|
|
|
|
MockMarket* = ref object of Market
|
2022-03-24 12:48:18 +00:00
|
|
|
requested*: seq[StorageRequest]
|
2022-03-28 10:28:22 +00:00
|
|
|
offered*: seq[StorageOffer]
|
|
|
|
selected*: seq[array[32, byte]]
|
|
|
|
subscriptions: Subscriptions
|
|
|
|
time: UInt256
|
|
|
|
waiting: HeapQueue[Expiry]
|
|
|
|
Subscriptions = object
|
|
|
|
onRequest: seq[RequestSubscription]
|
|
|
|
onOffer: seq[OfferSubscription]
|
2022-03-31 08:24:06 +00:00
|
|
|
onSelect: seq[SelectSubscription]
|
2022-03-28 10:28:22 +00:00
|
|
|
RequestSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
|
|
|
callback: OnRequest
|
|
|
|
OfferSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
|
|
|
requestId: array[32, byte]
|
|
|
|
callback: OnOffer
|
2022-03-31 08:24:06 +00:00
|
|
|
SelectSubscription* = ref object of Subscription
|
|
|
|
market: MockMarket
|
|
|
|
requestId: array[32, byte]
|
|
|
|
callback: OnSelect
|
2022-03-28 10:28:22 +00:00
|
|
|
Expiry = object
|
|
|
|
future: Future[void]
|
|
|
|
expiry: UInt256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} =
|
2022-03-24 12:48:18 +00:00
|
|
|
market.requested.add(request)
|
2022-03-31 08:24:06 +00:00
|
|
|
let subscriptions = market.subscriptions.onRequest
|
|
|
|
for subscription in subscriptions:
|
2022-03-28 10:28:22 +00:00
|
|
|
subscription.callback(request)
|
|
|
|
|
|
|
|
method offerStorage*(market: MockMarket, offer: StorageOffer) {.async.} =
|
|
|
|
market.offered.add(offer)
|
2022-03-31 08:24:06 +00:00
|
|
|
let subscriptions = market.subscriptions.onOffer
|
|
|
|
for subscription in subscriptions:
|
2022-03-28 10:28:22 +00:00
|
|
|
if subscription.requestId == offer.requestId:
|
|
|
|
subscription.callback(offer)
|
|
|
|
|
2022-03-31 08:24:06 +00:00
|
|
|
proc findOffer(market: MockMarket, id: array[32, byte]): ?StorageOffer =
|
|
|
|
for offer in market.offered:
|
|
|
|
if offer.id == id:
|
|
|
|
return some offer
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
method selectOffer*(market: MockMarket, id: array[32, byte]) {.async.} =
|
|
|
|
market.selected.add(id)
|
2022-03-31 08:24:06 +00:00
|
|
|
let subscriptions = market.subscriptions.onSelect
|
|
|
|
for subscription in subscriptions:
|
|
|
|
if offer =? market.findOffer(id):
|
|
|
|
if subscription.requestId == offer.requestId:
|
|
|
|
subscription.callback(id)
|
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
|
|
|
|
|
|
|
|
method subscribeOffers*(market: MockMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
callback: OnOffer):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = OfferSubscription(
|
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onOffer.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-03-31 08:24:06 +00:00
|
|
|
method subscribeSelection*(market: MockMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
callback: OnSelect):
|
|
|
|
Future[Subscription] {.async.} =
|
|
|
|
let subscription = SelectSubscription(
|
|
|
|
market: market,
|
|
|
|
requestId: requestId,
|
|
|
|
callback: callback
|
|
|
|
)
|
|
|
|
market.subscriptions.onSelect.add(subscription)
|
|
|
|
return subscription
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
method unsubscribe*(subscription: RequestSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onRequest.keepItIf(it != subscription)
|
|
|
|
|
|
|
|
method unsubscribe*(subscription: OfferSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onOffer.keepItIf(it != subscription)
|
|
|
|
|
2022-03-31 08:24:06 +00:00
|
|
|
method unsubscribe*(subscription: SelectSubscription) {.async.} =
|
|
|
|
subscription.market.subscriptions.onSelect.keepItIf(it != subscription)
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
func `<`(a, b: Expiry): bool =
|
|
|
|
a.expiry < b.expiry
|
|
|
|
|
2022-03-30 11:01:44 +00:00
|
|
|
method getTime*(market: MockMarket): Future[UInt256] {.async.} =
|
|
|
|
return market.time
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
method waitUntil*(market: MockMarket, expiry: UInt256): Future[void] =
|
|
|
|
let future = Future[void]()
|
|
|
|
if expiry > market.time:
|
|
|
|
market.waiting.push(Expiry(future: future, expiry: expiry))
|
|
|
|
else:
|
|
|
|
future.complete()
|
|
|
|
future
|
|
|
|
|
|
|
|
proc advanceTimeTo*(market: MockMarket, time: UInt256) =
|
|
|
|
doAssert(time >= market.time)
|
|
|
|
market.time = time
|
|
|
|
while market.waiting.len > 0 and market.waiting[0].expiry <= time:
|
|
|
|
market.waiting.pop().future.complete()
|