[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:
return none Address
method getRequestFromSlotId*(market: OnChainMarket,
slotId: SlotId): Future[?StorageRequest] {.async.} =
method getActiveSlot*(
market: OnChainMarket,
slotId: SlotId): Future[?(StorageRequest, UInt256)] {.async.} =
try:
return some await market.contract.getRequestFromSlotId(slotId)
return some await market.contract.getActiveSlot(slotId)
except ProviderError as e:
if e.revertReason.contains("Slot is free"):
return none StorageRequest
return none (StorageRequest, UInt256)
raise e
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 getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.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 mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.}

View File

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

View File

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

View File

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

View File

@ -254,14 +254,18 @@ ethersuite "On-Chain Market":
await token.approve(marketplace.address, request.price)
await market.requestStorage(request)
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":
await token.approve(marketplace.address, request.price)
await market.requestStorage(request)
await market.fillSlot(request.id, slotIndex, proof)
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":
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