2022-09-21 02:50:11 +00:00
|
|
|
import std/options
|
2022-03-29 14:17:20 +00:00
|
|
|
import pkg/chronos
|
2022-09-20 07:17:40 +00:00
|
|
|
import pkg/ethers/testing
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
import pkg/stew/byteutils
|
2022-05-19 19:56:03 +00:00
|
|
|
import codex/contracts
|
|
|
|
import codex/contracts/testtoken
|
2022-11-08 07:10:17 +00:00
|
|
|
import codex/storageproofs
|
2022-05-11 12:17:07 +00:00
|
|
|
import ../ethertest
|
2022-03-28 15:24:28 +00:00
|
|
|
import ./examples
|
2022-03-29 14:17:20 +00:00
|
|
|
import ./time
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
ethersuite "On-Chain Market":
|
2022-11-08 07:10:17 +00:00
|
|
|
let proof = exampleProof()
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
var market: OnChainMarket
|
2023-01-19 05:58:04 +00:00
|
|
|
var marketplace: Marketplace
|
2022-03-28 15:24:28 +00:00
|
|
|
var token: TestToken
|
2022-03-29 07:47:49 +00:00
|
|
|
var request: StorageRequest
|
2022-07-20 13:59:07 +00:00
|
|
|
var slotIndex: UInt256
|
2022-11-08 07:10:17 +00:00
|
|
|
var periodicity: Periodicity
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
let deployment = deployment()
|
2023-01-19 05:58:04 +00:00
|
|
|
marketplace = Marketplace.new(!deployment.address(Marketplace), provider.getSigner())
|
2022-03-28 15:24:28 +00:00
|
|
|
token = TestToken.new(!deployment.address(TestToken), provider.getSigner())
|
2022-07-20 13:59:07 +00:00
|
|
|
await token.mint(accounts[0], 1_000_000_000.u256)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
let config = await marketplace.config()
|
|
|
|
let collateral = config.collateral.initialAmount
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, collateral)
|
|
|
|
await marketplace.deposit(collateral)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2023-01-19 05:58:04 +00:00
|
|
|
market = OnChainMarket.new(marketplace)
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
periodicity = Periodicity(seconds: config.proofs.period)
|
2022-03-28 15:24:28 +00:00
|
|
|
|
2022-03-29 07:47:49 +00:00
|
|
|
request = StorageRequest.example
|
|
|
|
request.client = accounts[0]
|
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
slotIndex = (request.ask.slots div 2).u256
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
proc waitUntilProofRequired(slotId: SlotId) {.async.} =
|
|
|
|
let currentPeriod = periodicity.periodOf(await provider.currentTime())
|
|
|
|
await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
|
|
|
|
while not (
|
2023-01-19 05:58:04 +00:00
|
|
|
(await marketplace.isProofRequired(slotId)) and
|
|
|
|
(await marketplace.getPointer(slotId)) < 250
|
2022-11-08 07:10:17 +00:00
|
|
|
):
|
|
|
|
await provider.advanceTime(periodicity.seconds)
|
|
|
|
|
2022-03-28 15:28:51 +00:00
|
|
|
test "fails to instantiate when contract does not have a signer":
|
2023-01-19 05:58:04 +00:00
|
|
|
let storageWithoutSigner = marketplace.connect(provider)
|
2022-03-28 15:28:51 +00:00
|
|
|
expect AssertionError:
|
|
|
|
discard OnChainMarket.new(storageWithoutSigner)
|
|
|
|
|
2022-07-04 12:14:56 +00:00
|
|
|
test "knows signer address":
|
|
|
|
check (await market.getSigner()) == (await provider.getSigner().getAddress())
|
|
|
|
|
2023-01-19 05:58:04 +00:00
|
|
|
test "supports marketplace requests":
|
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-06-15 12:12:34 +00:00
|
|
|
test "can retrieve previously submitted requests":
|
|
|
|
check (await market.getRequest(request.id)) == none StorageRequest
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-09-07 11:02:00 +00:00
|
|
|
let r = await market.getRequest(request.id)
|
|
|
|
check (r) == some request
|
|
|
|
|
|
|
|
test "supports withdrawing of funds":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-09-07 11:02:00 +00:00
|
|
|
await provider.advanceTimeTo(request.expiry)
|
|
|
|
await market.withdrawFunds(request.id)
|
2022-06-15 12:12:34 +00:00
|
|
|
|
2022-03-31 08:46:03 +00:00
|
|
|
test "supports request subscriptions":
|
2022-08-17 02:29:44 +00:00
|
|
|
var receivedIds: seq[RequestId]
|
2022-04-11 18:03:55 +00:00
|
|
|
var receivedAsks: seq[StorageAsk]
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onRequest(id: RequestId, ask: StorageAsk) =
|
2022-04-11 18:03:55 +00:00
|
|
|
receivedIds.add(id)
|
|
|
|
receivedAsks.add(ask)
|
2022-03-29 07:47:49 +00:00
|
|
|
let subscription = await market.subscribeRequests(onRequest)
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-04-11 18:03:55 +00:00
|
|
|
check receivedIds == @[request.id]
|
|
|
|
check receivedAsks == @[request.ask]
|
2022-03-31 08:46:03 +00:00
|
|
|
await subscription.unsubscribe()
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
test "supports filling of slots":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-07-20 13:59:07 +00:00
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
test "can retrieve host that filled slot":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-07-27 14:54:34 +00:00
|
|
|
check (await market.getHost(request.id, slotIndex)) == none Address
|
2022-07-20 13:59:07 +00:00
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
2022-07-27 14:54:34 +00:00
|
|
|
check (await market.getHost(request.id, slotIndex)) == some accounts[0]
|
2022-07-04 10:11:18 +00:00
|
|
|
|
2022-07-28 12:33:17 +00:00
|
|
|
test "support slot filled subscriptions":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-08-17 02:29:44 +00:00
|
|
|
var receivedIds: seq[RequestId]
|
2022-07-28 12:33:17 +00:00
|
|
|
var receivedSlotIndices: seq[UInt256]
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onSlotFilled(id: RequestId, slotIndex: UInt256) =
|
2022-07-28 12:33:17 +00:00
|
|
|
receivedIds.add(id)
|
|
|
|
receivedSlotIndices.add(slotIndex)
|
|
|
|
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
|
|
|
check receivedIds == @[request.id]
|
|
|
|
check receivedSlotIndices == @[slotIndex]
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
|
|
|
test "subscribes only to a certain slot":
|
|
|
|
var otherSlot = slotIndex - 1
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-07-28 12:33:17 +00:00
|
|
|
var receivedSlotIndices: seq[UInt256]
|
2022-08-17 02:29:44 +00:00
|
|
|
proc onSlotFilled(requestId: RequestId, slotIndex: UInt256) =
|
2022-07-28 12:33:17 +00:00
|
|
|
receivedSlotIndices.add(slotIndex)
|
|
|
|
let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled)
|
|
|
|
await market.fillSlot(request.id, slotIndex - 1, proof)
|
|
|
|
check receivedSlotIndices.len == 0
|
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
|
|
|
check receivedSlotIndices == @[slotIndex]
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
test "support fulfillment subscriptions":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-08-17 02:29:44 +00:00
|
|
|
var receivedIds: seq[RequestId]
|
|
|
|
proc onFulfillment(id: RequestId) =
|
2022-06-14 07:47:05 +00:00
|
|
|
receivedIds.add(id)
|
|
|
|
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
2022-07-20 13:59:07 +00:00
|
|
|
for slotIndex in 0..<request.ask.slots:
|
|
|
|
await market.fillSlot(request.id, slotIndex.u256, proof)
|
2022-06-14 07:47:05 +00:00
|
|
|
check receivedIds == @[request.id]
|
2022-03-29 09:20:07 +00:00
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
test "subscribes only to fulfillment of a certain request":
|
2022-03-29 09:20:07 +00:00
|
|
|
var otherRequest = StorageRequest.example
|
|
|
|
otherRequest.client = accounts[0]
|
|
|
|
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, otherRequest.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(otherRequest)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
var receivedIds: seq[RequestId]
|
|
|
|
proc onFulfillment(id: RequestId) =
|
2022-06-14 07:47:05 +00:00
|
|
|
receivedIds.add(id)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
for slotIndex in 0..<request.ask.slots:
|
|
|
|
await market.fillSlot(request.id, slotIndex.u256, proof)
|
|
|
|
for slotIndex in 0..<otherRequest.ask.slots:
|
|
|
|
await market.fillSlot(otherRequest.id, slotIndex.u256, proof)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
check receivedIds == @[request.id]
|
2022-03-29 09:20:07 +00:00
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
2022-09-07 11:02:00 +00:00
|
|
|
|
|
|
|
test "support request cancelled subscriptions":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2022-09-07 11:02:00 +00:00
|
|
|
|
|
|
|
var receivedIds: seq[RequestId]
|
|
|
|
proc onRequestCancelled(id: RequestId) =
|
|
|
|
receivedIds.add(id)
|
|
|
|
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
|
|
|
|
|
|
|
|
await provider.advanceTimeTo(request.expiry)
|
|
|
|
await market.withdrawFunds(request.id)
|
|
|
|
check receivedIds == @[request.id]
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
test "support request failed subscriptions":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
|
|
|
|
|
|
|
var receivedIds: seq[RequestId]
|
|
|
|
proc onRequestFailed(id: RequestId) =
|
|
|
|
receivedIds.add(id)
|
|
|
|
let subscription = await market.subscribeRequestFailed(request.id, onRequestFailed)
|
|
|
|
|
|
|
|
for slotIndex in 0..<request.ask.slots:
|
|
|
|
await market.fillSlot(request.id, slotIndex.u256, proof)
|
|
|
|
for slotIndex in 0..request.ask.maxSlotLoss:
|
|
|
|
let slotId = request.slotId(slotIndex.u256)
|
|
|
|
while true:
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
let slotState = await marketplace.slotState(slotId)
|
|
|
|
if slotState == SlotState.Free:
|
|
|
|
break
|
|
|
|
await waitUntilProofRequired(slotId)
|
|
|
|
let missingPeriod = periodicity.periodOf(await provider.currentTime())
|
|
|
|
await provider.advanceTime(periodicity.seconds)
|
|
|
|
await marketplace.markProofAsMissing(slotId, missingPeriod)
|
2022-11-08 07:10:17 +00:00
|
|
|
check receivedIds == @[request.id]
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
2022-09-07 11:02:00 +00:00
|
|
|
test "subscribes only to a certain request cancellation":
|
2022-11-08 07:10:17 +00:00
|
|
|
var otherRequest = request
|
|
|
|
otherRequest.nonce = Nonce.example
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, otherRequest.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(otherRequest)
|
2022-09-07 11:02:00 +00:00
|
|
|
|
|
|
|
var receivedIds: seq[RequestId]
|
|
|
|
proc onRequestCancelled(requestId: RequestId) =
|
|
|
|
receivedIds.add(requestId)
|
|
|
|
|
|
|
|
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
|
|
|
|
await provider.advanceTimeTo(request.expiry) # shares expiry with otherRequest
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.withdrawFunds(otherRequest.id)
|
2022-09-07 11:02:00 +00:00
|
|
|
check receivedIds.len == 0
|
|
|
|
await market.withdrawFunds(request.id)
|
|
|
|
check receivedIds == @[request.id]
|
|
|
|
await subscription.unsubscribe()
|
2022-09-21 02:50:11 +00:00
|
|
|
|
|
|
|
test "request is none when unknown":
|
|
|
|
check isNone await market.getRequest(request.id)
|
2022-11-08 07:10:17 +00:00
|
|
|
|
|
|
|
test "can retrieve active requests":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
|
|
|
var request2 = StorageRequest.example
|
|
|
|
request2.client = accounts[0]
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request2.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request2)
|
|
|
|
check (await market.myRequests()) == @[request.id, request2.id]
|
|
|
|
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
test "retrieves correct request state when request is unknown":
|
|
|
|
check (await market.requestState(request.id)) == none RequestState
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
test "can retrieve request state":
|
2023-01-19 05:58:04 +00:00
|
|
|
await token.approve(marketplace.address, request.price)
|
2022-11-08 07:10:17 +00:00
|
|
|
await market.requestStorage(request)
|
|
|
|
for slotIndex in 0..<request.ask.slots:
|
|
|
|
await market.fillSlot(request.id, slotIndex.u256, proof)
|
[marketplace] Load sales state from chain (#306)
* [marketplace] get active slots from chain
# Conflicts:
# codex/contracts/market.nim
* [marketplace] make on chain event callbacks async
# Conflicts:
# tests/codex/helpers/mockmarket.nim
* [marketplace] make availability optional for node restart
# Conflicts:
# tests/codex/testsales.nim
* [marketplace] add async state machine
Allows for `enterAsync` to be cancelled.
* [marketplace] move sale process to async state machine
* [marketplace] sales state machine tests
* bump dagger-contracts
* [marketplace] fix ci issue with chronicles output
* PR comments
- add slotIndex to `SalesAgent` constructor
- remove `SalesAgent.init`
- rename `SalesAgent.init` to `start` and `SalesAgent.deinit` to `stop`.
- rename `SalesAgent. populateRequest` to `SalesAgent.retreiveRequest`.
- move availability removal to the downloading state. once availability is persisted to disk, it should survive node restarts.
-
* [marketplace] handle slot filled by other host
Handle the case when in the downloading, proving, or filling states, that another host fills the slot.
* [marketplace] use requestId for mySlots
* [marketplace] infer slot index from slotid
prevents reassigning a random slot index when restoring state from chain
* [marketplace] update to work with latest contracts
* [marketplace] clean up
* [marketplace] align with contract changes
- getState / state > requestState
- getSlot > getRequestFromSlotId
- support MarketplaceConfig
- support slotState, remove unneeded Slot type
- collateral > config.collateral.initialAmount
- remove proofPeriod contract call
- Revert reason “Slot empty” > “Slot is free”
- getProofEnd > read SlotState
Tests for changes
* [marketplace] add missing file
* [marketplace] bump codex-contracts-eth
* [config] remove unused imports
* [sales] cleanup
* [sales] fix: do not crash when fetching state fails
* [sales] make slotIndex non-optional
* Rebase and update NBS commit
Rebase on top of main and update NBS commit to the CI fix.
* [marketplace] use async subscription event handlers
* [marketplace] support slotIndex no longer optional
Previously, SalesAgent.slotIndex had been moved to not optional. However, there were still many places where optionality was assumed. This commit removes those assumuptions.
* [marketplace] sales state machine: use slotState
Use `slotState` instead of `requestState` for sales state machine.
* [marketplace] clean up
* [statemachine] adds a statemachine for async workflows
Allows events to be scheduled synchronously.
See https://github.com/status-im/nim-codex/pull/344
Co-Authored-By: Ben Bierens <thatbenbierens@gmail.com>
Co-Authored-By: Eric Mastro <eric.mastro@gmail.com>
* [market] make market callbacks synchronous
* [statemachine] export Event
* [statemachine] ensure that no errors are raised
* [statemachine] add machine parameter to run method
* [statemachine] initialize queue on start
* [statemachine] check futures before cancelling them
* [sales] use new async state machine
- states use new run() method and event mechanism
- StartState starts subscriptions and loads request
* [statemachine] fix unsusbscribe before subscribe
* [sales] replace old state transition tests
* [sales] separate state machine from sales data
* [sales] remove reference from SalesData to Sales
* [sales] separate sales context from sales
* [sales] move decoupled types into their own modules
* [sales] move retrieveRequest to SalesData
* [sales] move subscription logic into SalesAgent
* [sales] unsubscribe when finished or errored
* [build] revert back to released version of nim-ethers
* [sales] remove SaleStart state
* [sales] add missing base method
* [sales] move asyncSpawn helper to utils
* [sales] fix imports
* [sales] remove unused variables
* [sales statemachine] add async state machine error handling (#349)
* [statemachine] add error handling to asyncstatemachine
- add error handling to catch errors during state.run
- Sales: add ErrorState to identify which state to transition to during an error. This had to be added to SalesAgent constructor due to circular dependency issues, otherwise it would have been added directly to SalesAgent.
- Sales: when an error during run is encountered, the SaleErrorState is constructed with the error, and by default (base impl) will return the error state, so the machine can transition to it. This can be overridden by individual states if needed.
* [sales] rename onSaleFailed to onSaleErrored
Because there is already a state named SaleFailed which is meant to react to an onchain RequestFailed event and also because this callback is called from SaleErrored, renaming to onSaleErrored prevents ambiguity and confusion as to what has happened at the callback callsite.
* [statemachine] forward error to state directly
without going through a machine method first
* [statemachine] remove unnecessary error handling
AsyncQueueFullError is already handled in schedule()
* [statemachine] test that cancellation ignores onError
* [sales] simplify error handling in states
Rely on the state machine error handling instead
of catching errors in the state run method
---------
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
* [statemachine] prevent memory leaks
prevent memory leaks and nil access defects by:
- allowing multiple subscribe/unsubscribes of salesagent
- disallowing individual salesagent subscription calls to be made externally (requires the .subscribed check)
- allowing mutiple start/stops of asyncstatemachine
- disregard asyncstatemachine schedules if machine not yet started
* [salesagent] add salesagent-specific tests
1. test multiple subscribe/unsubscribes
2. test scheduling machine without being started
3. test subscriptions are working correctly with external events
4. test errors can be overridden at the state level for ErrorHandlingStates.
---------
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
Co-authored-by: Mark Spanbroek <mark@spanbroek.net>
Co-authored-by: Ben Bierens <thatbenbierens@gmail.com>
2023-03-08 13:34:26 +00:00
|
|
|
check (await market.requestState(request.id)) == some RequestState.Started
|
|
|
|
|
|
|
|
test "can retrieve active slots":
|
|
|
|
await token.approve(marketplace.address, request.price)
|
|
|
|
await market.requestStorage(request)
|
|
|
|
await market.fillSlot(request.id, slotIndex - 1, proof)
|
|
|
|
await market.fillSlot(request.id, slotIndex, proof)
|
|
|
|
let slotId1 = request.slotId(slotIndex - 1)
|
|
|
|
let slotId2 = request.slotId(slotIndex)
|
|
|
|
check (await market.mySlots()) == @[slotId1, slotId2]
|
|
|
|
|
|
|
|
test "returns none when slot is empty":
|
|
|
|
await token.approve(marketplace.address, request.price)
|
|
|
|
await market.requestStorage(request)
|
|
|
|
let slotId = request.slotId(slotIndex)
|
|
|
|
check (await market.getRequestFromSlotId(slotId)) == none StorageRequest
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
test "retrieves correct slot state when request is unknown":
|
|
|
|
let slotId = request.slotId(slotIndex)
|
|
|
|
check (await market.slotState(slotId)) == SlotState.Free
|
|
|
|
|
|
|
|
test "retrieves correct slot state once filled":
|
|
|
|
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.slotState(slotId)) == SlotState.Filled
|