[sales] Choose a slot index at random
This commit is contained in:
parent
b4b6fdb7c6
commit
057de4e928
|
@ -4,6 +4,7 @@ import pkg/upraises
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/nimcrypto
|
import pkg/nimcrypto
|
||||||
import pkg/chronicles
|
import pkg/chronicles
|
||||||
|
import ./rng
|
||||||
import ./market
|
import ./market
|
||||||
import ./clock
|
import ./clock
|
||||||
|
|
||||||
|
@ -48,6 +49,7 @@ type
|
||||||
ask: StorageAsk
|
ask: StorageAsk
|
||||||
availability: Availability
|
availability: Availability
|
||||||
request: ?StorageRequest
|
request: ?StorageRequest
|
||||||
|
slotIndex: ?UInt256
|
||||||
subscription: ?Subscription
|
subscription: ?Subscription
|
||||||
running: ?Future[void]
|
running: ?Future[void]
|
||||||
waiting: ?Future[void]
|
waiting: ?Future[void]
|
||||||
|
@ -114,13 +116,20 @@ proc finish(agent: SalesAgent, success: bool) =
|
||||||
waiting.cancel()
|
waiting.cancel()
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
if onSale =? agent.sales.onSale and request =? agent.request:
|
if onSale =? agent.sales.onSale and
|
||||||
onSale(agent.availability, request, 0.u256) # TODO: slot index
|
request =? agent.request and
|
||||||
|
slotIndex =? agent.slotIndex:
|
||||||
|
onSale(agent.availability, request, slotIndex)
|
||||||
else:
|
else:
|
||||||
if onClear =? agent.sales.onClear and request =? agent.request:
|
if onClear =? agent.sales.onClear and request =? agent.request:
|
||||||
onClear(agent.availability, request)
|
onClear(agent.availability, request)
|
||||||
agent.sales.add(agent.availability)
|
agent.sales.add(agent.availability)
|
||||||
|
|
||||||
|
proc selectSlot(agent: SalesAgent) =
|
||||||
|
let rng = Rng.instance
|
||||||
|
let slotIndex = rng.rand(agent.ask.slots - 1)
|
||||||
|
agent.slotIndex = some slotIndex.u256
|
||||||
|
|
||||||
proc onSlotFilled(agent: SalesAgent,
|
proc onSlotFilled(agent: SalesAgent,
|
||||||
requestId: array[32, byte],
|
requestId: array[32, byte],
|
||||||
slotIndex: UInt256) {.async.} =
|
slotIndex: UInt256) {.async.} =
|
||||||
|
@ -132,14 +141,14 @@ proc onSlotFilled(agent: SalesAgent,
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
agent.finish(success = false)
|
agent.finish(success = false)
|
||||||
|
|
||||||
proc subscribeSlotFilled(agent: SalesAgent) {.async.} =
|
proc subscribeSlotFilled(agent: SalesAgent, slotIndex: UInt256) {.async.} =
|
||||||
proc onSlotFilled(requestId: array[32, byte],
|
proc onSlotFilled(requestId: array[32, byte],
|
||||||
slotIndex: UInt256) {.gcsafe, upraises:[].} =
|
slotIndex: UInt256) {.gcsafe, upraises:[].} =
|
||||||
asyncSpawn agent.onSlotFilled(requestId, slotIndex)
|
asyncSpawn agent.onSlotFilled(requestId, slotIndex)
|
||||||
let market = agent.sales.market
|
let market = agent.sales.market
|
||||||
let subscription = await market.subscribeSlotFilled(agent.requestId,
|
let subscription = await market.subscribeSlotFilled(agent.requestId,
|
||||||
0.u256,
|
slotIndex,
|
||||||
onSlotFilled) # TODO: slot index
|
onSlotFilled)
|
||||||
agent.subscription = some subscription
|
agent.subscription = some subscription
|
||||||
|
|
||||||
proc waitForExpiry(agent: SalesAgent) {.async.} =
|
proc waitForExpiry(agent: SalesAgent) {.async.} =
|
||||||
|
@ -162,7 +171,11 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
|
|
||||||
sales.remove(availability)
|
sales.remove(availability)
|
||||||
|
|
||||||
await agent.subscribeSlotFilled()
|
agent.selectSlot()
|
||||||
|
without slotIndex =? agent.slotIndex:
|
||||||
|
raiseAssert "no slot selected"
|
||||||
|
|
||||||
|
await agent.subscribeSlotFilled(slotIndex)
|
||||||
|
|
||||||
agent.request = await market.getRequest(agent.requestId)
|
agent.request = await market.getRequest(agent.requestId)
|
||||||
without request =? agent.request:
|
without request =? agent.request:
|
||||||
|
@ -173,7 +186,7 @@ proc start(agent: SalesAgent) {.async.} =
|
||||||
|
|
||||||
await onStore(request.content.cid, availability)
|
await onStore(request.content.cid, availability)
|
||||||
let proof = await onProve(request.content.cid)
|
let proof = await onProve(request.content.cid)
|
||||||
await market.fillSlot(request.id, 0.u256, proof) # TODO: slot index
|
await market.fillSlot(request.id, slotIndex, proof)
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
raise
|
raise
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
|
|
Loading…
Reference in New Issue