nim-dagger/vendor
Eric 1d161d383e
Slot queue (#455)
## Slot queue
Adds a slot queue, as per the [slot queue design](https://github.com/codex-storage/codex-research/blob/master/design/sales.md#slot-queue).

Any time storage is requested, all slots from that request are immediately added to the queue. Finished, Canclled, Failed requests remove all slots with that request id from the queue. SlotFreed events add a new slot to the queue and SlotFilled events remove the slot from the queue. This allows popping of a slot each time one is processed, making things much simpler.

When an entire request of slots is added to the queue, the slot indices are shuffled randomly to hopefully prevent nodes that pick up the same storage requested event from clashing on the first processed slot index. This allowed removal of assigning a random slot index in the SalePreparing state and it also ensured that all SalesAgents will have a slot index assigned to them at the start thus the removal of the optional slotIndex.

Remove slotId from SlotFreed event as it was not being used. RequestId and slotIndex were added to the SlotFreed event earlier and those are now being used

The slot queue invariant that prioritises queue items added to the queue relies on a scoring mechanism to sort them based on the [sort order in the design document](https://github.com/codex-storage/codex-research/blob/master/design/sales.md#sort-order).

When a storage request is handled by the sales module, a slot index was randomly assigned and then the slot was filled. Now, a random slot index is only assigned when adding an entire request to the slot queue. Additionally, the slot is checked that its state is `SlotState.Free` before continuing with the download process.

SlotQueue should always ensure the underlying AsyncHeapQueue has one less than the maximum items, ensuring the SlotQueue can always have space to add an additional item regardless if it’s full or not.

Constructing `SlotQueue.workers` in `SlotQueue.new` calls `newAsyncQueue` which causes side effects, so the construction call had to be moved to `SlotQueue.start`.

Prevent loading request from contract (network request) if there is an existing item in queue for that request.

Check availability before adding request to queue.

Add ability to query market contract for past events. When new availabilities are added, the `onReservationAdded` callback is triggered in which past `StorageRequested` events are queried, and those slots are added to the queue (filtered by availability on `push` and filtered by state in `SalePreparing`).

#### Request Workers
Limit the concurrent requests being processed in the queue by using a limited pool of workers (default = 3). Workers are in a data structure of type `AsyncQueue[SlotQueueWorker]`. This allows us to await a `popFirst` for available workers inside of the main SlotQueue event loop

Add an `onCleanUp` that stops the agents and removes them from the sales module agent list. `onCleanUp` is called from sales end states (eg ignored, cancelled, finished, failed, errored).

Add a `doneProcessing` future to `SlotQueueWorker` to be completed in the `OnProcessSlot` callback. Each `doneProcessing` future created is cancelled and awaited in `SlotQueue.stop` (thanks to `TrackableFuturees`), which forced `stop` to become async.
  - Cancel dispatched workers and the `onProcessSlot` callbacks, prevents zombie callbacks

#### Add TrackableFutures
Allow tracking of futures in a module so they can be cancelled at a later time. Useful for asyncSpawned futures, but works for any future.

### Sales module
The sales module needed to subscribe to request events to ensure that the request queue was managed correctly on each event. In the process of doing this, the sales agents were updated to avoid subscribing to events in each agent, and instead dispatch received events from the sales module to all created sales agents. This would prevent memory leaks on having too many eventemitters subscribed to.
  - prevent removal of agents from sales module while stopping, otherwise the agents seq len is modified while iterating

An additional sales agent state was added, `SalePreparing`, that handles all state machine setup, such as retrieving the request and subscribing to events that were previously in the `SaleDownloading` state.

Once agents have parked in an end state (eg ignored, cancelled, finished, failed, errored), they were not getting cleaned up and the sales module was keeping a handle on their reference. An `onCleanUp` callback was created to be called after the state machine enters an end state, which could prevent a memory leak if the number of requests coming in is high.

Move the SalesAgent callback raises pragmas from the Sales module to the proc definition in SalesAgent. This avoids having to catch `Exception`.
  - remove unneeded error handling as pragmas were moved

Move sales.subscriptions from an object containing named subscriptions to a `seq[Subscription]` directly on the sales object.

Sales tests: shut down repo after sales stop, to fix SIGABRT in CI

### Add async Promise API
  - modelled after JavaScript Promise API
  - alternative to `asyncSpawn` that allows handling of async calls in a synchronous context (including access to the synchronous closure) with less additional procs to be declared
  - Write less code, catch errors that would otherwise defect in asyncspawn, and execute a callback after completion
  - Add cancellation callbacks to utils/then, ensuring cancellations are handled properly

## Dependencies
- bump codex-contracts-eth to support slot queue (https://github.com/codex-storage/codex-contracts-eth/pull/61)
- bump nim-ethers to 0.5.0
- Bump nim-json-rpc submodule to 0bf2bcb

---------

Co-authored-by: Jaremy Creechley <creechley@gmail.com>
2023-07-25 12:50:30 +10:00
..
asynctest@a236a5f0f3 Persist Dht providers (#257) 2022-09-29 20:16:59 -06:00
codex-contracts-eth@230e7276e2 Slot queue (#455) 2023-07-25 12:50:30 +10:00
combparser@ba4464c005 adding submodules 2021-11-25 09:37:12 +01:00
dnsclient.nim@fbb76f8af8 Add DHT (#75) 2022-04-13 10:32:35 -06:00
lrucache.nim@8767ade0b7 Add DHT (#75) 2022-04-13 10:32:35 -06:00
nim-bearssl@f4c4233de4 bump bearssl to latest 2022-09-27 12:56:07 +10:00
nim-blscurve@48d8668c5a Enable stylecheck (#353) 2023-03-10 08:02:54 +01:00
nim-chronicles@7631f7b2ee track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-chronos@6525f4ce1d bumping chronos to latest master (#314) 2022-11-14 22:15:37 -06:00
nim-confutils@2028b41602 feat: adding config and env. variable support (#379) 2023-05-02 15:06:34 +02:00
nim-contract-abi@61f8f59b39 Fix nimble requirements (#305) 2022-11-16 13:10:23 +01:00
nim-datastore@0cde8aeb67 Enable stylecheck (#353) 2023-03-10 08:02:54 +01:00
nim-eth@5885f638e4 Fix vendor/nim-eth (#246) 2022-09-12 21:30:16 -06:00
nim-ethers@9f4f762e21 Slot queue (#455) 2023-07-25 12:50:30 +10:00
nim-faststreams@1b561a9e71 adding submodules 2021-11-25 09:37:12 +01:00
nim-http-utils@e88e231dfc track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-json-rpc@0bf2bcbe74 Slot queue (#455) 2023-07-25 12:50:30 +10:00
nim-json-serialization@e5b18fb710 track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-leopard@1a6f2ab725 Enable stylecheck (#353) 2023-03-10 08:02:54 +01:00
nim-libbacktrace@b29c22ba0e Bump libbacktrace and secp256k1 2022-06-28 11:04:18 +03:00
nim-libp2p@8c2eca18dc Fix for decoding large manifests (#479) 2023-07-19 15:10:14 +02:00
nim-libp2p-dht@bd517f0e8d Dht debug (#446) 2023-06-19 08:21:03 +02:00
nim-metrics@743f81d4f6 adding submodules 2021-11-25 09:37:12 +01:00
nim-nat-traversal@27d314d65c Speed up development (#303) 2022-11-14 14:50:00 +01:00
nim-nitro@6b4c455bf4 Fix version conflict on nimble install 2022-05-19 09:36:47 +02:00
nim-presto@3984431dc0 track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-secp256k1@5340cf1881 Bump libbacktrace and secp256k1 2022-06-28 11:04:18 +03:00
nim-serialization@493d18b829 track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-sqlite3-abi@fda455cfea [build] add github.com/arnetheduck/nim-sqlite3-abi to vendor 2022-08-08 02:12:43 -05:00
nim-stew@e18f5a62af feat: adding config and env. variable support (#379) 2023-05-02 15:06:34 +02:00
nim-taskpools@b3673c7a7a Enable stylecheck (#353) 2023-03-10 08:02:54 +01:00
nim-toml-serialization@86d477136f feat: adding config and env. variable support (#379) 2023-05-02 15:06:34 +02:00
nim-unittest2@02c49b8a99 Node setup (#32) 2022-01-10 09:32:56 -06:00
nim-websock@7b2ed397d6 track latest nim-libp2p's master branch (#248) 2022-11-02 11:40:28 -06:00
nim-zlib@74cdeb54b2 feat: integrate dagger contracts 2022-02-04 15:34:56 +11:00
nimbus-build-system@1cf6a1b18c Fix CI (#339) 2023-02-06 17:15:02 +01:00
nimcrypto@a5742a9a21 adding submodules 2021-11-25 09:37:12 +01:00
questionable@30e4184a99 feat: collateral per slot (#390) 2023-04-14 11:04:17 +02:00
stint@036c71d06a adding submodules 2021-11-25 09:37:12 +01:00
upraises@ff4f8108e4 adding submodules 2021-11-25 09:37:12 +01:00