nim-codex/tests/codex/helpers/mockmarket.nim

97 lines
3.2 KiB
Nim
Raw Normal View History

2022-03-28 10:28:22 +00:00
import std/sequtils
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-03-23 12:57:48 +00:00
type
MockMarket* = ref object of Market
requested*: seq[StorageRequest]
fulfilled*: seq[Fulfillment]
2022-07-04 12:14:56 +00:00
signer: Address
2022-03-28 10:28:22 +00:00
subscriptions: Subscriptions
Fulfillment* = object
2022-07-05 08:37:55 +00:00
requestId*: array[32, byte]
proof*: seq[byte]
host*: Address
2022-03-28 10:28:22 +00:00
Subscriptions = object
onRequest: seq[RequestSubscription]
onFulfillment: seq[FulfillmentSubscription]
2022-03-28 10:28:22 +00:00
RequestSubscription* = ref object of Subscription
market: MockMarket
callback: OnRequest
FulfillmentSubscription* = ref object of Subscription
market: MockMarket
requestId: array[32, byte]
callback: OnFulfillment
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
method requestStorage*(market: MockMarket,
request: StorageRequest):
Future[StorageRequest] {.async.} =
market.requested.add(request)
for subscription in market.subscriptions.onRequest:
subscription.callback(request.id, request.ask)
return request
2022-03-28 10:28:22 +00:00
2022-06-15 12:12:34 +00:00
method getRequest(market: MockMarket,
id: array[32, byte]): Future[?StorageRequest] {.async.} =
for request in market.requested:
if request.id == id:
return some request
return none StorageRequest
2022-07-04 10:11:18 +00:00
method getHost(market: MockMarket,
id: array[32, byte]): Future[?Address] {.async.} =
for fulfillment in market.fulfilled:
if fulfillment.requestId == id:
return some fulfillment.host
return none Address
proc fulfillRequest*(market: MockMarket,
requestId: array[32, byte],
proof: seq[byte],
host: Address) =
let fulfillment = Fulfillment(requestId: requestId, proof: proof, host: host)
market.fulfilled.add(fulfillment)
for subscription in market.subscriptions.onFulfillment:
if subscription.requestId == requestId:
subscription.callback(requestId)
2022-03-28 10:28:22 +00:00
2022-07-04 10:11:18 +00:00
method fulfillRequest*(market: MockMarket,
requestId: array[32, byte],
proof: seq[byte]) {.async.} =
2022-07-04 12:14:56 +00:00
market.fulfillRequest(requestid, proof, market.signer)
2022-07-04 10:11:18 +00:00
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 subscribeFulfillment*(market: MockMarket,
requestId: array[32, byte],
callback: OnFulfillment):
Future[Subscription] {.async.} =
let subscription = FulfillmentSubscription(
market: market,
requestId: requestId,
callback: callback
)
market.subscriptions.onFulfillment.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: FulfillmentSubscription) {.async.} =
subscription.market.subscriptions.onFulfillment.keepItIf(it != subscription)