From 19fe1b85e35826bdb1d1515019f618d6f3dc302d Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 19 Jan 2023 17:00:43 +1100 Subject: [PATCH] [marketplace] use requestId for mySlots --- codex/contracts/market.nim | 4 ++-- codex/market.nim | 2 +- codex/sales.nim | 10 ++++++++-- codex/sales/statemachine.nim | 2 +- tests/codex/helpers/mockmarket.nim | 2 +- tests/codex/testsales.nim | 4 ++++ 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index baa12637..975e84ce 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -31,8 +31,8 @@ method getSigner*(market: OnChainMarket): Future[Address] {.async.} = method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} = return await market.contract.myRequests -method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} = - return await market.contract.mySlots +method mySlots*(market: OnChainMarket, requestId: RequestId): Future[seq[SlotId]] {.async.} = + return await market.contract.mySlots(requestId) method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} = await market.contract.requestStorage(request) diff --git a/codex/market.nim b/codex/market.nim index 713f1ac0..1dd4e5f4 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -28,7 +28,7 @@ method requestStorage*(market: Market, method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} = raiseAssert("not implemented") -method mySlots*(market: Market): Future[seq[SlotId]] {.base, async.} = +method mySlots*(market: Market, requestId: RequestId): Future[seq[SlotId]] {.base, async.} = raiseAssert("not implemented") method getRequest*(market: Market, diff --git a/codex/sales.nim b/codex/sales.nim index 4c00c1d4..78f19dd4 100644 --- a/codex/sales.nim +++ b/codex/sales.nim @@ -61,6 +61,7 @@ proc handleRequest(sales: Sales, requestId: RequestId, ask: StorageAsk) {.async.} = let availability = sales.findAvailability(ask) + # TODO: check if random slot is actually available (not already filled) let slotIndex = randomSlotIndex(ask.slots) let agent = newSalesAgent( sales, @@ -78,13 +79,18 @@ proc load*(sales: Sales) {.async.} = let market = sales.market # TODO: restore availability from disk - - let slotIds = await market.mySlots() + let requestIds = await market.myRequests() + var slotIds: seq[SlotId] + # TODO: this needs to be optimised + for requestId in requestIds: + let reqSlotIds = await market.mySlots(requestId) + slotIds.add reqSlotIds for slotId in slotIds: # TODO: this needs to be optimised if slot =? await market.getSlot(slotId): if request =? await market.getRequest(slot.requestId): let availability = sales.findAvailability(request.ask) + # TODO: restore slot index from chain, do not assign a new index let slotIndex = randomSlotIndex(request.ask.slots) let agent = newSalesAgent( sales, diff --git a/codex/sales/statemachine.nim b/codex/sales/statemachine.nim index 610208e3..568e00ba 100644 --- a/codex/sales/statemachine.nim +++ b/codex/sales/statemachine.nim @@ -32,7 +32,7 @@ type ask*: StorageAsk availability*: ?Availability # TODO: when availability persistence is added, change this to not optional request*: ?StorageRequest - slotIndex*: ?UInt256 + slotIndex*: ?UInt256 # TODO: disallow optional; SalesAgents should always have a slotIndex failed*: market.Subscription fulfilled*: market.Subscription slotFilled*: market.Subscription diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 3b649ed2..bca0727b 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -74,7 +74,7 @@ method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} = method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} = return market.activeRequests[market.signer] -method mySlots*(market: MockMarket): Future[seq[SlotId]] {.async.} = +method mySlots*(market: MockMarket, requestId: RequestId): Future[seq[SlotId]] {.async.} = return market.activeSlots[market.signer] method getRequest(market: MockMarket, diff --git a/tests/codex/testsales.nim b/tests/codex/testsales.nim index 1a5e0b2b..bebe7712 100644 --- a/tests/codex/testsales.nim +++ b/tests/codex/testsales.nim @@ -550,6 +550,8 @@ suite "Sales state machine": host: me) await fillSlot(slot1.slotIndex) market.activeSlots[me] = @[request.slotId(0.u256), request.slotId(1.u256)] + market.requested = @[request] + market.activeRequests[me] = @[request.id] await sales.load() let expected = SalesAgent(sales: sales, @@ -560,6 +562,8 @@ suite "Sales state machine": # randomly selected for the agent, and we also won't know the value of # `failed`/`fulfilled`/`cancelled` futures, so we need to compare # the properties we know + # TODO: when calling sales.load(), slot index should be restored and not + # randomly re-assigned, so this may no longer be needed proc `==` (agent0, agent1: SalesAgent): bool = return agent0.sales == agent1.sales and agent0.requestId == agent1.requestId and