diff --git a/dagger/clock.nim b/dagger/clock.nim index 00728888..38ec7349 100644 --- a/dagger/clock.nim +++ b/dagger/clock.nim @@ -1,6 +1,12 @@ +import pkg/chronos + type Clock* = ref object of RootObj SecondsSince1970* = int64 method now*(clock: Clock): SecondsSince1970 {.base.} = raiseAssert "not implemented" + +proc waitUntil*(clock: Clock, time: SecondsSince1970) {.async.} = + while clock.now() < time: + await sleepAsync(1.seconds) diff --git a/dagger/contracts/market.nim b/dagger/contracts/market.nim index 71ea5557..4dadef03 100644 --- a/dagger/contracts/market.nim +++ b/dagger/contracts/market.nim @@ -10,21 +10,17 @@ type OnChainMarket* = ref object of Market contract: Storage signer: Signer - pollInterval*: Duration MarketSubscription = market.Subscription EventSubscription = ethers.Subscription OnChainMarketSubscription = ref object of MarketSubscription eventSubscription: EventSubscription -const DefaultPollInterval = 3.seconds - func new*(_: type OnChainMarket, contract: Storage): OnChainMarket = without signer =? contract.signer: raiseAssert("Storage contract should have a signer") OnChainMarket( contract: contract, signer: signer, - pollInterval: DefaultPollInterval ) method requestStorage(market: OnChainMarket, @@ -46,15 +42,6 @@ method offerStorage(market: OnChainMarket, method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} = await market.contract.selectOffer(offerId) -method getTime(market: OnChainMarket): Future[UInt256] {.async.} = - let provider = market.contract.provider - let blck = !await provider.getBlock(BlockTag.latest) - return blck.timestamp - -method waitUntil*(market: OnChainMarket, expiry: UInt256) {.async.} = - while not ((time =? await market.getTime()) and (time >= expiry)): - await sleepAsync(market.pollInterval) - method subscribeRequests(market: OnChainMarket, callback: OnRequest): Future[MarketSubscription] {.async.} = diff --git a/dagger/market.nim b/dagger/market.nim index c2594849..9a36420e 100644 --- a/dagger/market.nim +++ b/dagger/market.nim @@ -27,12 +27,6 @@ method offerStorage*(market: Market, method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} = raiseAssert("not implemented") -method getTime*(market: Market): Future[UInt256] {.base, async.} = - raiseAssert("not implemented") - -method waitUntil*(market: Market, expiry: UInt256) {.base, async.} = - raiseAssert("not implemented") - method subscribeRequests*(market: Market, callback: OnRequest): Future[Subscription] {.base, async.} = diff --git a/dagger/purchasing.nim b/dagger/purchasing.nim index cc351f26..bc0f2cb0 100644 --- a/dagger/purchasing.nim +++ b/dagger/purchasing.nim @@ -88,7 +88,7 @@ proc run(purchase: Purchase) {.async.} = let market = purchase.market purchase.request = await market.requestStorage(purchase.request) let subscription = await market.subscribeOffers(purchase.request.id, onOffer) - await market.waitUntil(purchase.request.expiry) + await purchase.clock.waitUntil(purchase.request.expiry.truncate(int64)) await purchase.selectOffer() await subscription.unsubscribe() diff --git a/dagger/sales.nim b/dagger/sales.nim index c2610a18..67210c0d 100644 --- a/dagger/sales.nim +++ b/dagger/sales.nim @@ -114,7 +114,7 @@ proc subscribeSelect(negotiation: Negotiation) {.async.} = proc waitForExpiry(negotiation: Negotiation) {.async.} = without offer =? negotiation.offer: return - await negotiation.sales.market.waitUntil(offer.expiry) + await negotiation.sales.clock.waitUntil(offer.expiry.truncate(int64)) negotiation.finish(success = false) proc start(negotiation: Negotiation) {.async.} = diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index d580384f..7b32579a 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -154,23 +154,3 @@ ethersuite "On-Chain Market": check selected == @[offer.id] await subscription.unsubscribe() - - test "can retrieve current block time": - let latestBlock = !await provider.getBlock(BlockTag.latest) - check (await market.getTime()) == latestBlock.timestamp - - test "supports waiting for expiry of a request or offer": - let pollInterval = 200.milliseconds - market.pollInterval = pollInterval - - proc waitForPoll {.async.} = - await sleepAsync(pollInterval * 2) - - let future = market.waitUntil(request.expiry) - check not future.completed - await provider.advanceTimeTo(request.expiry - 1) - await waitForPoll() - check not future.completed - await provider.advanceTimeTo(request.expiry) - await waitForPoll() - check future.completed diff --git a/tests/dagger/helpers/mockmarket.nim b/tests/dagger/helpers/mockmarket.nim index d846e706..1852be03 100644 --- a/tests/dagger/helpers/mockmarket.nim +++ b/tests/dagger/helpers/mockmarket.nim @@ -106,23 +106,3 @@ method unsubscribe*(subscription: OfferSubscription) {.async.} = method unsubscribe*(subscription: SelectSubscription) {.async.} = subscription.market.subscriptions.onSelect.keepItIf(it != subscription) - -func `<`(a, b: Expiry): bool = - a.expiry < b.expiry - -method getTime*(market: MockMarket): Future[UInt256] {.async.} = - return market.time - -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() diff --git a/tests/dagger/testpurchasing.nim b/tests/dagger/testpurchasing.nim index 772567db..dcec41af 100644 --- a/tests/dagger/testpurchasing.nim +++ b/tests/dagger/testpurchasing.nim @@ -27,7 +27,7 @@ suite "Purchasing": proc purchaseAndWait(request: StorageRequest) {.async.} = let purchase = purchasing.purchase(request) - market.advanceTimeTo(market.requested[^1].expiry) + clock.set(market.requested[^1].expiry.truncate(int64)) await purchase.wait() test "submits a storage request when asked": @@ -90,7 +90,7 @@ suite "Purchasing": offer2.price = 10.u256 discard await market.offerStorage(offer1) discard await market.offerStorage(offer2) - market.advanceTimeTo(request.expiry) + clock.set(request.expiry.truncate(int64)) await purchase.wait() check market.selected[0] == offer2.id @@ -104,7 +104,7 @@ suite "Purchasing": offer2.expiry = expired discard await market.offerStorage(offer1) discard await market.offerStorage(offer2) - market.advanceTimeTo(request.expiry) + clock.set(request.expiry.truncate(int64)) await purchase.wait() check market.selected[0] == offer1.id @@ -121,6 +121,6 @@ suite "Purchasing": offer2.expiry = getTime().toUnix().u256 + expiryMargin - 1 discard await market.offerStorage(offer1) discard await market.offerStorage(offer2) - market.advanceTimeTo(request.expiry) + clock.set(request.expiry.truncate(int64)) await purchase.wait() check market.selected[0] == offer1.id diff --git a/tests/dagger/testsales.nim b/tests/dagger/testsales.nim index d5a6f5ce..72997a44 100644 --- a/tests/dagger/testsales.nim +++ b/tests/dagger/testsales.nim @@ -74,7 +74,7 @@ suite "Sales": test "sets expiry time of offer": sales.add(availability) - let now = getTime().toUnix().u256 + let now = clock.now().u256 discard await market.requestStorage(request) check market.offered[0].expiry == now + sales.offerExpiryInterval @@ -111,6 +111,6 @@ suite "Sales": sales.add(availability) discard await market.requestStorage(request) let offer = market.offered[0] - market.advanceTimeTo(offer.expiry) - await sleepAsync(chronos.seconds(1)) + clock.set(offer.expiry.truncate(int64)) + await sleepAsync(chronos.seconds(2)) check sales.available.contains(availability)