[clock] Replace Market.waitUntil() with Clock.waitUntil()
This commit is contained in:
parent
2992229c3a
commit
09a7aa3eed
|
@ -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)
|
||||
|
|
|
@ -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.} =
|
||||
|
|
|
@ -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.} =
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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.} =
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue