2021-11-23 16:55:16 +00:00
|
|
|
mode = ScriptMode.Verbose
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
version = "0.1.0"
|
2022-05-19 19:56:03 +00:00
|
|
|
author = "Codex Team"
|
2021-10-29 19:30:52 +00:00
|
|
|
description = "p2p data durability engine"
|
2021-02-26 00:23:22 +00:00
|
|
|
license = "MIT"
|
2022-04-12 00:42:18 +00:00
|
|
|
binDir = "build"
|
|
|
|
srcDir = "."
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-21 22:02:05 +00:00
|
|
|
requires "nim >= 1.2.0"
|
|
|
|
requires "asynctest >= 0.3.2 & < 0.4.0"
|
|
|
|
requires "bearssl >= 0.1.4"
|
|
|
|
requires "chronicles >= 0.7.2"
|
|
|
|
requires "chronos >= 2.5.2"
|
|
|
|
requires "confutils"
|
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 02:50:30 +00:00
|
|
|
requires "ethers >= 0.5.0 & < 0.6.0"
|
2023-06-21 22:02:05 +00:00
|
|
|
requires "libbacktrace"
|
|
|
|
requires "libp2p"
|
|
|
|
requires "metrics"
|
|
|
|
requires "nimcrypto >= 0.4.1"
|
|
|
|
requires "nitro >= 0.5.1 & < 0.6.0"
|
|
|
|
requires "presto"
|
|
|
|
requires "protobuf_serialization >= 0.2.0 & < 0.3.0"
|
|
|
|
requires "questionable >= 0.10.6 & < 0.11.0"
|
|
|
|
requires "secp256k1"
|
|
|
|
requires "stew"
|
|
|
|
requires "upraises >= 0.1.0 & < 0.2.0"
|
|
|
|
requires "toml_serialization"
|
|
|
|
requires "https://github.com/status-im/lrucache.nim#1.2.2"
|
|
|
|
requires "leopard >= 0.1.0 & < 0.2.0"
|
|
|
|
requires "blscurve"
|
|
|
|
requires "libp2pdht"
|
|
|
|
requires "eth"
|
2022-04-12 00:42:18 +00:00
|
|
|
|
|
|
|
when declared(namedBin):
|
|
|
|
namedBin = {
|
2022-05-19 19:56:03 +00:00
|
|
|
"codex/codex": "codex"
|
2022-04-12 00:42:18 +00:00
|
|
|
}.toTable()
|
2021-11-23 16:55:16 +00:00
|
|
|
|
|
|
|
### Helper functions
|
|
|
|
proc buildBinary(name: string, srcDir = "./", params = "", lang = "c") =
|
|
|
|
if not dirExists "build":
|
|
|
|
mkDir "build"
|
|
|
|
# allow something like "nim nimbus --verbosity:0 --hints:off nimbus.nims"
|
|
|
|
var extra_params = params
|
2022-06-22 21:37:05 +00:00
|
|
|
when compiles(commandLineParams):
|
|
|
|
for param in commandLineParams:
|
|
|
|
extra_params &= " " & param
|
|
|
|
else:
|
|
|
|
for i in 2..<paramCount():
|
|
|
|
extra_params &= " " & paramStr(i)
|
|
|
|
|
2021-11-23 16:55:16 +00:00
|
|
|
exec "nim " & lang & " --out:build/" & name & " " & extra_params & " " & srcDir & name & ".nim"
|
|
|
|
|
2023-06-22 10:32:18 +00:00
|
|
|
proc test(name: string, srcDir = "tests/", params = "", lang = "c") =
|
|
|
|
buildBinary name, srcDir, params
|
2021-11-23 16:55:16 +00:00
|
|
|
exec "build/" & name
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
task codex, "build codex binary":
|
2022-06-14 23:34:56 +00:00
|
|
|
buildBinary "codex", params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE"
|
2022-04-21 14:33:20 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
task testCodex, "Build & run Codex tests":
|
2023-06-22 10:32:18 +00:00
|
|
|
test "testCodex", params = "-d:codex_enable_proof_failures=true"
|
2022-03-15 14:24:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
task testContracts, "Build & run Codex Contract tests":
|
2022-04-12 00:42:18 +00:00
|
|
|
test "testContracts"
|
2022-03-21 10:37:47 +00:00
|
|
|
|
2022-04-21 14:33:20 +00:00
|
|
|
task testIntegration, "Run integration tests":
|
2023-06-22 10:32:18 +00:00
|
|
|
buildBinary "codex", params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE -d:codex_enable_proof_failures=true"
|
2022-04-21 14:33:20 +00:00
|
|
|
test "testIntegration"
|
|
|
|
|
2022-03-28 08:18:53 +00:00
|
|
|
task test, "Run tests":
|
2022-05-19 19:56:03 +00:00
|
|
|
testCodexTask()
|
2022-03-28 08:18:53 +00:00
|
|
|
|
|
|
|
task testAll, "Run all tests":
|
2022-05-19 19:56:03 +00:00
|
|
|
testCodexTask()
|
2022-03-15 14:24:03 +00:00
|
|
|
testContractsTask()
|
2022-04-21 14:33:20 +00:00
|
|
|
testIntegrationTask()
|