From b7ab9481d94cdc098c65cb435811c205f5024a9d Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 14 Jun 2022 09:47:05 +0200 Subject: [PATCH] [market] Update to latest dagger-contracts --- codex/contracts/market.nim | 38 ++++--------- codex/market.nim | 26 +++------ tests/codex/helpers/mockmarket.nim | 87 ++++++++-------------------- tests/contracts/testMarket.nim | 91 ++++++------------------------ 4 files changed, 60 insertions(+), 182 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 4dadef03..92aa1b69 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -31,16 +31,10 @@ method requestStorage(market: OnChainMarket, await market.contract.requestStorage(request) return request -method offerStorage(market: OnChainMarket, - offer: StorageOffer): - Future[StorageOffer] {.async.} = - var offer = offer - offer.host = await market.signer.getAddress() - await market.contract.offerStorage(offer) - return offer - -method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} = - await market.contract.selectOffer(offerId) +method fulfillRequest(market: OnChainMarket, + requestId: array[32, byte], + proof: seq[byte]) {.async.} = + await market.contract.fulfillRequest(requestId, proof) method subscribeRequests(market: OnChainMarket, callback: OnRequest): @@ -50,24 +44,14 @@ method subscribeRequests(market: OnChainMarket, let subscription = await market.contract.subscribe(StorageRequested, onEvent) return OnChainMarketSubscription(eventSubscription: subscription) -method subscribeOffers(market: OnChainMarket, - requestId: array[32, byte], - callback: OnOffer): - Future[MarketSubscription] {.async.} = - proc onEvent(event: StorageOffered) {.upraises:[].} = +method subscribeFulfillment(market: OnChainMarket, + requestId: array[32, byte], + callback: OnFulfillment): + Future[MarketSubscription] {.async.} = + proc onEvent(event: RequestFulfilled) {.upraises:[].} = if event.requestId == requestId: - callback(event.offer) - let subscription = await market.contract.subscribe(StorageOffered, onEvent) - return OnChainMarketSubscription(eventSubscription: subscription) - -method subscribeSelection(market: OnChainMarket, - requestId: array[32, byte], - callback: OnSelect): - Future[MarketSubscription] {.async.} = - proc onSelect(event: OfferSelected) {.upraises: [].} = - if event.requestId == requestId: - callback(event.offerId) - let subscription = await market.contract.subscribe(OfferSelected, onSelect) + callback(event.requestId) + let subscription = await market.contract.subscribe(RequestFulfilled, onEvent) return OnChainMarketSubscription(eventSubscription: subscription) method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} = diff --git a/codex/market.nim b/codex/market.nim index 9a36420e..9692f2cf 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -11,20 +11,16 @@ type Market* = ref object of RootObj Subscription* = ref object of RootObj OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} - OnOffer* = proc(offer: StorageOffer) {.gcsafe, upraises:[].} - OnSelect* = proc(offerId: array[32, byte]) {.gcsafe, upraises: [].} + OnFulfillment* = proc(requestId: array[32, byte]) {.gcsafe, upraises: [].} method requestStorage*(market: Market, request: StorageRequest): Future[StorageRequest] {.base, async.} = raiseAssert("not implemented") -method offerStorage*(market: Market, - offer: StorageOffer): - Future[StorageOffer] {.base, async.} = - raiseAssert("not implemented") - -method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} = +method fulfillRequest*(market: Market, + requestId: array[32, byte], + proof: seq[byte]) {.base, async.} = raiseAssert("not implemented") method subscribeRequests*(market: Market, @@ -32,16 +28,10 @@ method subscribeRequests*(market: Market, Future[Subscription] {.base, async.} = raiseAssert("not implemented") -method subscribeOffers*(market: Market, - requestId: array[32, byte], - callback: OnOffer): - Future[Subscription] {.base, async.} = - raiseAssert("not implemented") - -method subscribeSelection*(market: Market, - requestId: array[32, byte], - callback: OnSelect): - Future[Subscription] {.base, async.} = +method subscribeFulfillment*(market: Market, + requestId: array[32, byte], + callback: OnFulfillment): + Future[Subscription] {.base, async.} = raiseAssert("not implemented") method unsubscribe*(subscription: Subscription) {.base, async, upraises:[].} = diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 48b7f591..15f25a32 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -1,6 +1,4 @@ import std/sequtils -import std/heapqueue -import pkg/questionable import pkg/codex/market export market @@ -8,61 +6,37 @@ export market type MockMarket* = ref object of Market requested*: seq[StorageRequest] - offered*: seq[StorageOffer] - selected*: seq[array[32, byte]] + fulfilled*: seq[Fulfillment] subscriptions: Subscriptions - time: UInt256 - waiting: HeapQueue[Expiry] + Fulfillment* = object + requestId: array[32, byte] + proof: seq[byte] Subscriptions = object onRequest: seq[RequestSubscription] - onOffer: seq[OfferSubscription] - onSelect: seq[SelectSubscription] + onFulfillment: seq[FulfillmentSubscription] RequestSubscription* = ref object of Subscription market: MockMarket callback: OnRequest - OfferSubscription* = ref object of Subscription + FulfillmentSubscription* = ref object of Subscription market: MockMarket requestId: array[32, byte] - callback: OnOffer - SelectSubscription* = ref object of Subscription - market: MockMarket - requestId: array[32, byte] - callback: OnSelect - Expiry = object - future: Future[void] - expiry: UInt256 + callback: OnFulfillment method requestStorage*(market: MockMarket, request: StorageRequest): Future[StorageRequest] {.async.} = market.requested.add(request) - let subscriptions = market.subscriptions.onRequest - for subscription in subscriptions: + for subscription in market.subscriptions.onRequest: subscription.callback(request.id, request.ask) return request -method offerStorage*(market: MockMarket, - offer: StorageOffer): - Future[StorageOffer] {.async.} = - market.offered.add(offer) - let subscriptions = market.subscriptions.onOffer - for subscription in subscriptions: - if subscription.requestId == offer.requestId: - subscription.callback(offer) - return offer - -proc findOffer(market: MockMarket, id: array[32, byte]): ?StorageOffer = - for offer in market.offered: - if offer.id == id: - return some offer - -method selectOffer*(market: MockMarket, id: array[32, byte]) {.async.} = - market.selected.add(id) - let subscriptions = market.subscriptions.onSelect - for subscription in subscriptions: - if offer =? market.findOffer(id): - if subscription.requestId == offer.requestId: - subscription.callback(id) +method fulfillRequest*(market: MockMarket, + requestId: array[32, byte], + proof: seq[byte]) {.async.} = + market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof)) + for subscription in market.subscriptions.onFulfillment: + if subscription.requestId == requestId: + subscription.callback(requestId) method subscribeRequests*(market: MockMarket, callback: OnRequest): @@ -74,35 +48,20 @@ method subscribeRequests*(market: MockMarket, market.subscriptions.onRequest.add(subscription) return subscription -method subscribeOffers*(market: MockMarket, - requestId: array[32, byte], - callback: OnOffer): - Future[Subscription] {.async.} = - let subscription = OfferSubscription( +method subscribeFulfillment*(market: MockMarket, + requestId: array[32, byte], + callback: OnFulfillment): + Future[Subscription] {.async.} = + let subscription = FulfillmentSubscription( market: market, requestId: requestId, callback: callback ) - market.subscriptions.onOffer.add(subscription) - return subscription - -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) + market.subscriptions.onFulfillment.add(subscription) return subscription method unsubscribe*(subscription: RequestSubscription) {.async.} = subscription.market.subscriptions.onRequest.keepItIf(it != subscription) -method unsubscribe*(subscription: OfferSubscription) {.async.} = - subscription.market.subscriptions.onOffer.keepItIf(it != subscription) - -method unsubscribe*(subscription: SelectSubscription) {.async.} = - subscription.market.subscriptions.onSelect.keepItIf(it != subscription) +method unsubscribe*(subscription: FulfillmentSubscription) {.async.} = + subscription.market.subscriptions.onFulfillment.keepItIf(it != subscription) diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 4c3b7b52..7b92175d 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -6,6 +6,7 @@ import ./examples import ./time ethersuite "On-Chain Market": + let proof = seq[byte].example var market: OnChainMarket var storage: Storage @@ -61,96 +62,40 @@ ethersuite "On-Chain Market": check receivedAsks == @[request.ask] await subscription.unsubscribe() - test "supports storage offers": + test "supports fulfilling of requests": await token.approve(storage.address, request.ask.maxPrice) discard await market.requestStorage(request) - check (await market.offerStorage(offer)) == offer + await market.fulfillRequest(request.id, proof) - test "sets host address when submitting storage offer": + test "support fulfillment subscriptions": await token.approve(storage.address, request.ask.maxPrice) discard await market.requestStorage(request) - var offerWithoutHost = offer - offerWithoutHost.host = Address.default - let submitted = await market.offerStorage(offerWithoutHost) - check submitted.host == accounts[0] - - test "supports offer subscriptions": - await token.approve(storage.address, request.ask.maxPrice) - discard await market.requestStorage(request) - var received: seq[StorageOffer] - proc onOffer(offer: StorageOffer) = - received.add(offer) - let subscription = await market.subscribeOffers(request.id, onOffer) - discard await market.offerStorage(offer) - check received == @[offer] + var receivedIds: seq[array[32, byte]] + proc onFulfillment(id: array[32, byte]) = + receivedIds.add(id) + let subscription = await market.subscribeFulfillment(request.id, onFulfillment) + await market.fulfillRequest(request.id, proof) + check receivedIds == @[request.id] await subscription.unsubscribe() - test "subscribes only to offers for a certain request": + test "subscribes only to fulfillmentof a certain request": var otherRequest = StorageRequest.example - var otherOffer = StorageOffer.example otherRequest.client = accounts[0] - otherOffer.host = accounts[0] - otherOffer.requestId = otherRequest.id - otherOffer.price = otherrequest.ask.maxPrice await token.approve(storage.address, request.ask.maxPrice) discard await market.requestStorage(request) await token.approve(storage.address, otherrequest.ask.maxPrice) discard await market.requestStorage(otherRequest) - var submitted: seq[StorageOffer] - proc onOffer(offer: StorageOffer) = - submitted.add(offer) + var receivedIds: seq[array[32, byte]] + proc onFulfillment(id: array[32, byte]) = + receivedIds.add(id) - let subscription = await market.subscribeOffers(request.id, onOffer) + let subscription = await market.subscribeFulfillment(request.id, onFulfillment) - discard await market.offerStorage(offer) - discard await market.offerStorage(otherOffer) + await market.fulfillRequest(request.id, proof) + await market.fulfillRequest(otherRequest.id, proof) - check submitted == @[offer] - - await subscription.unsubscribe() - - test "supports selection of an offer": - await token.approve(storage.address, request.ask.maxPrice) - discard await market.requestStorage(request) - discard await market.offerStorage(offer) - - var selected: seq[array[32, byte]] - proc onSelect(offerId: array[32, byte]) = - selected.add(offerId) - let subscription = await market.subscribeSelection(request.id, onSelect) - - await market.selectOffer(offer.id) - - check selected == @[offer.id] - - await subscription.unsubscribe() - - test "subscribes only to selection for a certain request": - var otherRequest = StorageRequest.example - var otherOffer = StorageOffer.example - otherRequest.client = accounts[0] - otherOffer.host = accounts[0] - otherOffer.requestId = otherRequest.id - otherOffer.price = otherrequest.ask.maxPrice - - await token.approve(storage.address, request.ask.maxPrice) - discard await market.requestStorage(request) - discard await market.offerStorage(offer) - await token.approve(storage.address, otherrequest.ask.maxPrice) - discard await market.requestStorage(otherRequest) - discard await market.offerStorage(otherOffer) - - var selected: seq[array[32, byte]] - proc onSelect(offerId: array[32, byte]) = - selected.add(offerId) - - let subscription = await market.subscribeSelection(request.id, onSelect) - - await market.selectOffer(offer.id) - await market.selectOffer(otherOffer.id) - - check selected == @[offer.id] + check receivedIds == @[request.id] await subscription.unsubscribe()