nim-codex/tests/codex/sales/testsalesagent.nim
Eric Mastro 92dc770fa2
[marketplace] check randomly assigned slot index is not already filled
When a storage request is handled by the sales module, a slot index was randomly assigned and then the slot was filled.

Now, the random slot index is checked that its state is `SlotState.Free` before continuing with the download process.

An additional sales agent state was added, preparing, that handles this step of assigning a random slot index. All state machine setup, such as retrieving the request and subscribing to events that were previously in the `SaleDownloading` state have been moved to `SalePreparing`. If all indices of the request have been filled, the state is changed to `SaleIgnored`.

# Conflicts:
#	codex/sales.nim
#	codex/sales/states/downloading.nim
#	codex/sales/states/filling.nim
#	tests/contracts/testInteractions.nim
2023-05-08 10:38:58 +10:00

179 lines
5.7 KiB
Nim

import std/times
import pkg/asynctest
import pkg/chronos
import pkg/codex/sales
import pkg/codex/sales/salesagent
import pkg/codex/sales/salescontext
import pkg/codex/sales/statemachine
import pkg/codex/sales/states/errorhandling
import pkg/codex/proving
import ../helpers/mockmarket
import ../helpers/mockclock
import ../helpers/eventually
import ../examples
var onCancelCalled = false
var onFailedCalled = false
var onSlotFilledCalled = false
var onErrorCalled = false
type
MockState = ref object of SaleState
MockErrorState = ref object of ErrorHandlingState
method `$`*(state: MockState): string = "MockState"
method `$`*(state: MockErrorState): string = "MockErrorState"
method onCancelled*(state: MockState, request: StorageRequest): ?State =
onCancelCalled = true
method onFailed*(state: MockState, request: StorageRequest): ?State =
onFailedCalled = true
method onSlotFilled*(state: MockState, requestId: RequestId,
slotIndex: UInt256): ?State =
onSlotFilledCalled = true
method onError*(state: MockErrorState, err: ref CatchableError): ?State =
onErrorCalled = true
method run*(state: MockErrorState, machine: Machine): Future[?State] {.async.} =
raise newException(ValueError, "failure")
suite "Sales agent":
var request = StorageRequest(
ask: StorageAsk(
slots: 4,
slotSize: 100.u256,
duration: 60.u256,
reward: 10.u256,
),
content: StorageContent(
cid: "some cid"
),
expiry: (getTime() + initDuration(hours=1)).toUnix.u256
)
var agent: SalesAgent
var context: SalesContext
var slotIndex: UInt256
var market: MockMarket
var clock: MockClock
setup:
market = MockMarket.new()
clock = MockClock.new()
context = SalesContext(market: market, clock: clock)
slotIndex = 0.u256
onCancelCalled = false
onFailedCalled = false
onSlotFilledCalled = false
agent = newSalesAgent(context,
request.id,
some slotIndex,
some request)
request.expiry = (getTime() + initDuration(hours=1)).toUnix.u256
teardown:
await agent.stop()
test "can retrieve request":
agent = newSalesAgent(context,
request.id,
some slotIndex,
none StorageRequest)
market.requested = @[request]
await agent.retrieveRequest()
check agent.data.request == some request
test "subscribe assigns subscriptions/futures":
await agent.subscribe()
check not agent.data.cancelled.isNil
check not agent.data.failed.isNil
check not agent.data.fulfilled.isNil
check not agent.data.slotFilled.isNil
test "unsubscribe deassigns subscriptions/futures":
await agent.subscribe()
await agent.unsubscribe()
check agent.data.cancelled.isNil
check agent.data.failed.isNil
check agent.data.fulfilled.isNil
check agent.data.slotFilled.isNil
test "subscribe can be called multiple times, without overwriting subscriptions/futures":
await agent.subscribe()
let cancelled = agent.data.cancelled
let failed = agent.data.failed
let fulfilled = agent.data.fulfilled
let slotFilled = agent.data.slotFilled
await agent.subscribe()
check cancelled == agent.data.cancelled
check failed == agent.data.failed
check fulfilled == agent.data.fulfilled
check slotFilled == agent.data.slotFilled
test "unsubscribe can be called multiple times":
await agent.subscribe()
await agent.unsubscribe()
await agent.unsubscribe()
test "subscribe can be called when request expiry has lapsed":
# succeeds when agent.data.fulfilled.isNil
request.expiry = (getTime() - initDuration(seconds=1)).toUnix.u256
agent.data.request = some request
check agent.data.fulfilled.isNil
await agent.subscribe()
test "current state onCancelled called when cancel emitted":
let state = MockState.new()
agent.start(state)
await agent.subscribe()
clock.set(request.expiry.truncate(int64))
check eventually onCancelCalled
test "cancelled future is finished (cancelled) when fulfillment emitted":
agent.start(MockState.new())
await agent.subscribe()
market.emitRequestFulfilled(request.id)
check eventually agent.data.cancelled.cancelled()
test "current state onFailed called when failed emitted":
agent.start(MockState.new())
await agent.subscribe()
market.emitRequestFailed(request.id)
check eventually onFailedCalled
test "current state onSlotFilled called when slot filled emitted":
agent.start(MockState.new())
await agent.subscribe()
market.emitSlotFilled(request.id, slotIndex)
check eventually onSlotFilledCalled
test "ErrorHandlingState.onError can be overridden at the state level":
agent.start(MockErrorState.new())
check eventually onErrorCalled
test "assigns random slot index for slot that is free":
let slotId0 = slotId(request.id, 0.u256)
market.slotState[slotId0] = SlotState.Filled
check isOk await agent.assignRandomSlotIndex(2)
check agent.data.slotIndex == some 1.u256
test "fails to assign random slot index when all slots filled":
let slotId0 = slotId(request.id, 0.u256)
let slotId1 = slotId(request.id, 1.u256)
market.slotState[slotId0] = SlotState.Filled
market.slotState[slotId1] = SlotState.Filled
let r = await agent.assignRandomSlotIndex(2)
check r.isErr
check r.error of AllSlotsFilledError
check agent.data.slotIndex == none UInt256
test "fails to assign random slot index when invalid number of slots provided":
let r = await agent.assignRandomSlotIndex(0)
check r.isErr
check r.error of ValueError
check agent.data.slotIndex == none UInt256