-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slot-reservations): Add SaleSlotReserving state (#917)
* convert EthersError to MarketError * change `canReserveSlot` and `reserveSlot` parameters Parameters for `canReserveSlot` and `reserveSlot` were changed from `SlotId` to `RequestId` and `UInt256 slotIndex`. * Add SaleSlotReserving Adds a new state, SaleSlotReserving, that attempts to reserve a slot before downloading. If the slot cannot be reserved, the state moves to SaleIgnored. On error, the state moves to SaleErrored. SaleIgnored is also updated to pass in `reprocessSlot` and `returnBytes`, controlling the behaviour in the Sales module after the slot is ignored. This is because previously it was assumed that SaleIgnored was only reached when there was no Availability. This is no longer the case, since SaleIgnored can now be reached when a slot cannot be reserved. * Update SalePreparing Specify `reprocessSlot` and `returnBytes` when moving to `SaleIgnored` from `SalePreparing`. Update tests to include test for a raised CatchableError. * Fix unit test * Modify `canReserveSlot` and `reverseSlot` params after rebase * Update MockMarket with new `canReserveSlot` and `reserveSlot` params * fix after rebase also bump codex-contracts-eth to master
- Loading branch information
Showing
11 changed files
with
214 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pkg/questionable | ||
import pkg/questionable/results | ||
import pkg/metrics | ||
|
||
import ../../logutils | ||
import ../../market | ||
import ../salesagent | ||
import ../statemachine | ||
import ./errorhandling | ||
import ./cancelled | ||
import ./failed | ||
import ./filled | ||
import ./ignored | ||
import ./downloading | ||
import ./errored | ||
|
||
type | ||
SaleSlotReserving* = ref object of ErrorHandlingState | ||
|
||
logScope: | ||
topics = "marketplace sales reserving" | ||
|
||
method `$`*(state: SaleSlotReserving): string = "SaleSlotReserving" | ||
|
||
method onCancelled*(state: SaleSlotReserving, request: StorageRequest): ?State = | ||
return some State(SaleCancelled()) | ||
|
||
method onFailed*(state: SaleSlotReserving, request: StorageRequest): ?State = | ||
return some State(SaleFailed()) | ||
|
||
method onSlotFilled*(state: SaleSlotReserving, requestId: RequestId, | ||
slotIndex: UInt256): ?State = | ||
return some State(SaleFilled()) | ||
|
||
method run*(state: SaleSlotReserving, machine: Machine): Future[?State] {.async.} = | ||
let agent = SalesAgent(machine) | ||
let data = agent.data | ||
let context = agent.context | ||
let market = context.market | ||
|
||
logScope: | ||
requestId = data.requestId | ||
slotIndex = data.slotIndex | ||
|
||
let canReserve = await market.canReserveSlot(data.requestId, data.slotIndex) | ||
if canReserve: | ||
try: | ||
trace "Reserving slot" | ||
await market.reserveSlot(data.requestId, data.slotIndex) | ||
except MarketError as e: | ||
return some State( SaleErrored(error: e) ) | ||
|
||
trace "Slot successfully reserved" | ||
return some State( SaleDownloading() ) | ||
|
||
else: | ||
# do not re-add this slot to the queue, and return bytes from Reservation to | ||
# the Availability | ||
debug "Slot cannot be reserved, ignoring" | ||
return some State( SaleIgnored(reprocessSlot: false, returnBytes: true) ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pkg/chronos | ||
import pkg/questionable | ||
import pkg/codex/contracts/requests | ||
import pkg/codex/sales/states/slotreserving | ||
import pkg/codex/sales/states/downloading | ||
import pkg/codex/sales/states/cancelled | ||
import pkg/codex/sales/states/failed | ||
import pkg/codex/sales/states/filled | ||
import pkg/codex/sales/states/ignored | ||
import pkg/codex/sales/states/errored | ||
import pkg/codex/sales/salesagent | ||
import pkg/codex/sales/salescontext | ||
import pkg/codex/sales/reservations | ||
import pkg/codex/stores/repostore | ||
import ../../../asynctest | ||
import ../../helpers | ||
import ../../examples | ||
import ../../helpers/mockmarket | ||
import ../../helpers/mockreservations | ||
import ../../helpers/mockclock | ||
|
||
asyncchecksuite "sales state 'SlotReserving'": | ||
let request = StorageRequest.example | ||
let slotIndex = (request.ask.slots div 2).u256 | ||
var market: MockMarket | ||
var clock: MockClock | ||
var agent: SalesAgent | ||
var state: SaleSlotReserving | ||
var context: SalesContext | ||
|
||
setup: | ||
market = MockMarket.new() | ||
clock = MockClock.new() | ||
|
||
state = SaleSlotReserving.new() | ||
context = SalesContext( | ||
market: market, | ||
clock: clock | ||
) | ||
|
||
agent = newSalesAgent(context, | ||
request.id, | ||
slotIndex, | ||
request.some) | ||
|
||
test "switches to cancelled state when request expires": | ||
let next = state.onCancelled(request) | ||
check !next of SaleCancelled | ||
|
||
test "switches to failed state when request fails": | ||
let next = state.onFailed(request) | ||
check !next of SaleFailed | ||
|
||
test "switches to filled state when slot is filled": | ||
let next = state.onSlotFilled(request.id, slotIndex) | ||
check !next of SaleFilled | ||
|
||
test "run switches to downloading when slot successfully reserved": | ||
let next = await state.run(agent) | ||
check !next of SaleDownloading | ||
|
||
test "run switches to ignored when slot reservation not allowed": | ||
market.setCanReserveSlot(false) | ||
let next = await state.run(agent) | ||
check !next of SaleIgnored | ||
|
||
test "run switches to errored when slot reservation errors": | ||
let error = newException(MarketError, "some error") | ||
market.setReserveSlotThrowError(some error) | ||
let next = !(await state.run(agent)) | ||
check next of SaleErrored | ||
let errored = SaleErrored(next) | ||
check errored.error == error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule codex-contracts-eth
updated
from 33010b to 807fc9