2023-08-21 10:26:43 +00:00
|
|
|
import pkg/questionable
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/codex/contracts/requests
|
|
|
|
import pkg/codex/sales/states/initialproving
|
|
|
|
import pkg/codex/sales/states/cancelled
|
|
|
|
import pkg/codex/sales/states/failed
|
|
|
|
import pkg/codex/sales/states/filling
|
2024-01-17 19:24:34 +00:00
|
|
|
import pkg/codex/sales/states/errored
|
2023-08-21 10:26:43 +00:00
|
|
|
import pkg/codex/sales/salesagent
|
|
|
|
import pkg/codex/sales/salescontext
|
2023-12-11 10:29:15 +00:00
|
|
|
import pkg/codex/market
|
2024-01-29 20:03:51 +00:00
|
|
|
|
|
|
|
import ../../../asynctest
|
2023-08-21 10:26:43 +00:00
|
|
|
import ../../examples
|
|
|
|
import ../../helpers
|
2023-12-11 10:29:15 +00:00
|
|
|
import ../../helpers/mockmarket
|
2024-03-12 12:10:14 +00:00
|
|
|
import ../../helpers/mockclock
|
|
|
|
import ../helpers/periods
|
2023-08-21 10:26:43 +00:00
|
|
|
|
|
|
|
asyncchecksuite "sales state 'initialproving'":
|
2024-02-07 06:50:35 +00:00
|
|
|
let proof = Groth16Proof.example
|
2023-08-21 10:26:43 +00:00
|
|
|
let request = StorageRequest.example
|
|
|
|
let slotIndex = (request.ask.slots div 2).u256
|
2023-12-11 10:29:15 +00:00
|
|
|
let market = MockMarket.new()
|
2024-03-12 12:10:14 +00:00
|
|
|
let clock = MockClock.new()
|
2023-08-21 10:26:43 +00:00
|
|
|
|
|
|
|
var state: SaleInitialProving
|
|
|
|
var agent: SalesAgent
|
2023-12-11 10:29:15 +00:00
|
|
|
var receivedChallenge: ProofChallenge
|
2023-08-21 10:26:43 +00:00
|
|
|
|
|
|
|
setup:
|
2024-02-07 06:50:35 +00:00
|
|
|
let onProve = proc (slot: Slot, challenge: ProofChallenge): Future[?!Groth16Proof] {.async.} =
|
2023-12-11 10:29:15 +00:00
|
|
|
receivedChallenge = challenge
|
2024-01-17 19:24:34 +00:00
|
|
|
return success(proof)
|
2023-12-11 10:29:15 +00:00
|
|
|
let context = SalesContext(
|
|
|
|
onProve: onProve.some,
|
2024-03-12 12:10:14 +00:00
|
|
|
market: market,
|
|
|
|
clock: clock
|
2023-12-11 10:29:15 +00:00
|
|
|
)
|
2023-08-21 10:26:43 +00:00
|
|
|
agent = newSalesAgent(context,
|
|
|
|
request.id,
|
|
|
|
slotIndex,
|
|
|
|
request.some)
|
|
|
|
state = SaleInitialProving.new()
|
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
proc allowProofToStart {.async.} =
|
|
|
|
# wait until we're in initialproving state
|
|
|
|
await sleepAsync(10.millis)
|
|
|
|
# it won't start proving until the next period
|
|
|
|
await clock.advanceToNextPeriod(market)
|
|
|
|
|
2023-08-21 10:26:43 +00:00
|
|
|
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
|
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
test "waits for the beginning of the period to get the challenge":
|
|
|
|
let future = state.run(agent)
|
|
|
|
await sleepAsync(10.millis)
|
|
|
|
check not future.finished
|
|
|
|
await allowProofToStart()
|
|
|
|
discard await future
|
|
|
|
|
|
|
|
test "waits another period when the proof pointer is about to wrap around":
|
|
|
|
market.proofPointer = 250
|
|
|
|
let future = state.run(agent)
|
|
|
|
await allowProofToStart()
|
|
|
|
await sleepAsync(10.millis)
|
|
|
|
check not future.finished
|
|
|
|
market.proofPointer = 100
|
|
|
|
await allowProofToStart()
|
|
|
|
discard await future
|
2023-12-11 10:29:15 +00:00
|
|
|
|
|
|
|
test "onProve callback provides proof challenge":
|
|
|
|
market.proofChallenge = ProofChallenge.example
|
|
|
|
|
|
|
|
let future = state.run(agent)
|
2024-03-12 12:10:14 +00:00
|
|
|
await allowProofToStart()
|
|
|
|
|
|
|
|
discard await future
|
2023-12-11 10:29:15 +00:00
|
|
|
|
|
|
|
check receivedChallenge == market.proofChallenge
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
test "switches to filling state when initial proving is complete":
|
|
|
|
let future = state.run(agent)
|
|
|
|
await allowProofToStart()
|
|
|
|
let next = await future
|
|
|
|
|
|
|
|
check !next of SaleFilling
|
|
|
|
check SaleFilling(!next).proof == proof
|
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
test "switches to errored state when onProve callback fails":
|
2024-02-07 06:50:35 +00:00
|
|
|
let onProveFailed: OnProve = proc(slot: Slot, challenge: ProofChallenge): Future[?!Groth16Proof] {.async.} =
|
2024-01-17 19:24:34 +00:00
|
|
|
return failure("oh no!")
|
|
|
|
|
|
|
|
let proofFailedContext = SalesContext(
|
|
|
|
onProve: onProveFailed.some,
|
2024-03-12 12:10:14 +00:00
|
|
|
market: market,
|
|
|
|
clock: clock
|
2024-01-17 19:24:34 +00:00
|
|
|
)
|
|
|
|
agent = newSalesAgent(proofFailedContext,
|
|
|
|
request.id,
|
|
|
|
slotIndex,
|
|
|
|
request.some)
|
|
|
|
|
2024-03-12 12:10:14 +00:00
|
|
|
let future = state.run(agent)
|
|
|
|
await allowProofToStart()
|
|
|
|
let next = await future
|
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
check !next of SaleErrored
|