[marketplace] persist slot index

For loading the sales state from chain, the slot index was not previously persisted in the contract.

This commit will retrieve the slot index from the contract when the sales state is loaded.
This commit is contained in:
Eric Mastro 2023-02-13 17:49:22 +11:00
parent 1588c923b9
commit 340122be33
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
8 changed files with 33 additions and 35 deletions

View File

@ -73,13 +73,15 @@ method getHost(market: OnChainMarket,
else: else:
return none Address return none Address
method getRequestFromSlotId*(market: OnChainMarket, method getActiveSlot*(
slotId: SlotId): Future[?StorageRequest] {.async.} = market: OnChainMarket,
slotId: SlotId): Future[?(StorageRequest, UInt256)] {.async.} =
try: try:
return some await market.contract.getRequestFromSlotId(slotId) return some await market.contract.getActiveSlot(slotId)
except ProviderError as e: except ProviderError as e:
if e.revertReason.contains("Slot is free"): if e.revertReason.contains("Slot is free"):
return none StorageRequest return none (StorageRequest, UInt256)
raise e raise e
method fillSlot(market: OnChainMarket, method fillSlot(market: OnChainMarket,

View File

@ -45,7 +45,7 @@ proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId) {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId) {.contract.} proc freeSlot*(marketplace: Marketplace, id: SlotId) {.contract.}
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.} proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.}
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.} proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}
proc getRequestFromSlotId*(marketplace: Marketplace, id: SlotId): StorageRequest {.contract, view.} proc getActiveSlot*(marketplace: Marketplace, id: SlotId): (StorageRequest, UInt256) {.contract, view.}
proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.} proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.}
proc mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.} proc mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.}

View File

@ -53,8 +53,10 @@ method getHost*(market: Market,
slotIndex: UInt256): Future[?Address] {.base, async.} = slotIndex: UInt256): Future[?Address] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method getRequestFromSlotId*(market: Market, method getActiveSlot*(
slotId: SlotId): Future[?StorageRequest] {.base, async.} = market: Market,
slotId: SlotId): Future[?(StorageRequest, UInt256)] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method fillSlot*(market: Market, method fillSlot*(market: Market,

View File

@ -55,15 +55,6 @@ proc randomSlotIndex(numSlots: uint64): UInt256 =
let slotIndex = rng.rand(numSlots - 1) let slotIndex = rng.rand(numSlots - 1)
return slotIndex.u256 return slotIndex.u256
proc findSlotIndex(numSlots: uint64,
requestId: RequestId,
slotId: SlotId): ?UInt256 =
for i in 0..<numSlots:
if slotId(requestId, i.u256) == slotId:
return some i.u256
return none UInt256
proc handleRequest(sales: Sales, proc handleRequest(sales: Sales,
requestId: RequestId, requestId: RequestId,
ask: StorageAsk) {.async.} = ask: StorageAsk) {.async.} =
@ -101,16 +92,12 @@ proc load*(sales: Sales) {.async.} =
for slotId in slotIds: for slotId in slotIds:
# TODO: this needs to be optimised # TODO: this needs to be optimised
if request =? await market.getRequestFromSlotId(slotId): if (request, slotIndex) =? (await market.getActiveSlot(slotId)):
let availability = await sales.reservations.find(request.ask.slotSize, let availability = await sales.reservations.find(
request.ask.duration, request.ask.slotSize,
request.ask.pricePerSlot, request.ask.duration,
used = true) request.ask.pricePerSlot,
used = true)
without slotIndex =? findSlotIndex(request.ask.slots,
request.id,
slotId):
raiseAssert "could not find slot index"
let agent = newSalesAgent( let agent = newSalesAgent(
sales, sales,

View File

@ -102,12 +102,15 @@ method getRequest(market: MockMarket,
return some request return some request
return none StorageRequest return none StorageRequest
method getRequestFromSlotId*(market: MockMarket, method getActiveSlot*(
slotId: SlotId): Future[?StorageRequest] {.async.} = market: MockMarket,
slotId: SlotId): Future[?(StorageRequest, UInt256)] {.async.} =
for slot in market.filled: for slot in market.filled:
if slotId(slot.requestId, slot.slotIndex) == slotId: if slotId(slot.requestId, slot.slotIndex) == slotId and
return await market.getRequest(slot.requestId) request =? await market.getRequest(slot.requestId):
return none StorageRequest return some (request, slot.slotIndex)
return none (StorageRequest, UInt256)
method requestState*(market: MockMarket, method requestState*(market: MockMarket,
requestId: RequestId): Future[?RequestState] {.async.} = requestId: RequestId): Future[?RequestState] {.async.} =

View File

@ -254,14 +254,18 @@ ethersuite "On-Chain Market":
await token.approve(marketplace.address, request.price) await token.approve(marketplace.address, request.price)
await market.requestStorage(request) await market.requestStorage(request)
let slotId = request.slotId(slotIndex) let slotId = request.slotId(slotIndex)
check (await market.getRequestFromSlotId(slotId)) == none StorageRequest check:
(await market.getActiveSlot(slotId)) ==
none (StorageRequest, UInt256)
test "can retrieve request details from slot id": test "can retrieve request details from slot id":
await token.approve(marketplace.address, request.price) await token.approve(marketplace.address, request.price)
await market.requestStorage(request) await market.requestStorage(request)
await market.fillSlot(request.id, slotIndex, proof) await market.fillSlot(request.id, slotIndex, proof)
let slotId = request.slotId(slotIndex) let slotId = request.slotId(slotIndex)
check (await market.getRequestFromSlotId(slotId)) == some request check:
(await market.getActiveSlot(slotId)) ==
some (request, slotIndex)
test "retrieves correct slot state when request is unknown": test "retrieves correct slot state when request is unknown":
let slotId = request.slotId(slotIndex) let slotId = request.slotId(slotIndex)

@ -1 +1 @@
Subproject commit cde543626236bd48188354d842cbe1513052c560 Subproject commit 974e5470b50957411a0c9efa1b4948aa01b90d62

2
vendor/questionable vendored

@ -1 +1 @@
Subproject commit 30e4184a99c8c1ba329925912d2c5d4b09acf8cc Subproject commit e7afaeac498c66209f6b2f2ce6667143fbce3bc1