There is a limitation in `downloadInternal` that infinitely loops for peers when attempting to download a block if the node does not have peers. Currently, the block retry counter is only decremented if there exists peers and a download attempt is made, otherwise the loop will continue until peers are found, ad infinium. This fix decrements the retry counter on each loop that no peers are found.
Stale entries in peer.lastSentWants were not removed when blocks were
resolved or cancelled, causing unbounded memory growth. This adds
incremental cleanup during refreshBlockKnowledge, removing up to 2048
stale entries per refresh cycle with proper event loop yielding.
Part of https://github.com/codex-storage/nim-codex/issues/974
Signed-off-by: Chrysostomos Nanakos <chris@include.gr>
Now fetchBatched maintains a sliding window of batchSize blocks in-flight.
When 75% complete, adds next chunk to maintain constant window size.
This ensures blocks are already pending or have been fetched when
StoreStream needs them.
Part of https://github.com/codex-storage/nim-codex/issues/974
Signed-off-by: Chrysostomos Nanakos <chris@include.gr>
Implements delta-based WantList updates to reduce network traffic during
block exchange. Only sends newly added blocks instead of resending the
entire WantList on every refresh.
Also some network related fixes:
- Add TCP_NODELAY flag to prevent Nagle's algorithm delays
- Clear sendConn on stream reset to allow garbage collection
- Improve error handling in NetworkPeer.send()
Part of https://github.com/codex-storage/nim-codex/issues/974
Signed-off-by: Chrysostomos Nanakos <chris@include.gr>
Returns false on duplicate marking attempts instead of logging errors,
eliminating duplicate marking loop in blockPresenceHandler and
preventing duplicate block requests across concurrent flows.
Part of https://github.com/codex-storage/nim-codex/issues/974
- Add codex_block_exchange_discovery_requests_total counter to track peer
discovery frequency
- Add codex_block_exchange_peer_timeouts_total counter to monitor peer
reliability issues
- Add codex_block_exchange_requests_failed_total counter to track request
failure rates
Part of https://github.com/codex-storage/nim-codex/issues/974
Achieves significant memory reduction with equivalent network
performance. The reduced batch size prevents memory pressure
while preserving transfer efficiency, improving overall system
resource utilization.
Part of https://github.com/codex-storage/nim-codex/issues/974
This prevents unbounded peer accumulation while maintaining redundancy.
When peer count exceeds the maximum, the least active peers are removed from
tracking to free resources.
Part of https://github.com/codex-storage/nim-codex/issues/974
Use probabilistic distribution based on peer quality scores, giving all peers
opportunity while favoring better-performing ones. Selection probability is
inversely proportional to score.
Part of https://github.com/codex-storage/nim-codex/issues/974
* checked exceptions in stores
* makes asynciter as much exception safe as it gets
* introduce "SafeAsyncIter" that uses Results and limits exceptions to cancellations
* adds {.push raises: [].} to errors
* uses SafeAsyncIter in "listBlocks" and in "getBlockExpirations"
* simplifies safeasynciter (magic of auto)
* gets rid of ugly casts
* tiny fix in hte way we create raising futures in tests of safeasynciter
* Removes two more casts caused by using checked exceptions
* adds an extended explanation of one more complex SafeAsyncIter test
* adds missing "finishOnErr" param in slice constructor of SafeAsyncIter
* better fix for "Error: Exception can raise an unlisted exception: Exception" error.
---------
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
* cleanup imports and logs
* add BlockHandle type
* revert deps
* refactor: async error handling and future tracking improvements
- Update async procedures to use explicit raises annotation
- Modify TrackedFutures to handle futures with no raised exceptions
- Replace `asyncSpawn` with explicit future tracking
- Update test suites to use `unittest2`
- Standardize error handling across network and async components
- Remove deprecated error handling patterns
This commit introduces a more robust approach to async error handling and future management, improving type safety and reducing potential runtime errors.
* bump nim-serde
* remove asyncSpawn
* rework background downloads and prefetch
* imporove logging
* refactor: enhance async procedures with error handling and raise annotations
* misc cleanup
* misc
* refactor: implement allFinishedFailed to aggregate future results with success and failure tracking
* refactor: update error handling in reader procedures to raise ChunkerError and CancelledError
* refactor: improve error handling in wantListHandler and accountHandler procedures
* refactor: simplify LPStreamReadError creation by consolidating parameters
* refactor: enhance error handling in AsyncStreamWrapper to catch unexpected errors
* refactor: enhance error handling in advertiser and discovery loops to improve resilience
* misc
* refactor: improve code structure and readability
* remove cancellation from addSlotToQueue
* refactor: add assertion for unexpected errors in local store checks
* refactor: prevent tracking of finished futures and improve test assertions
* refactor: improve error handling in local store checks
* remove usage of msgDetail
* feat: add initial implementation of discovery engine and related components
* refactor: improve task scheduling logic by removing unnecessary break statement
* break after scheduling a task
* make taskHandler cancelable
* refactor: update async handlers to raise CancelledError
* refactor(advertiser): streamline error handling and improve task flow in advertise loops
* fix: correct spelling of "divisible" in error messages and comments
* refactor(discovery): simplify discovery task loop and improve error handling
* refactor(engine): filter peers before processing in cancelBlocks procedure
* adding basic retry functionality
* avoid duplicate requests and batch them
* fix cancelling blocks
* properly resolve blocks
* minor cleanup - use `self`
* avoid useless asyncSpawn
* track retries
* limit max inflight and set libp2p maxIncomingStreams
* cleanup
* add basic yield in readLoop
* use tuple instead of object
* cleanup imports and logs
* increase defaults
* wip
* fix prefetch batching
* cleanup
* decrease timeouts to speedup tests
* remove outdated test
* add retry tests
* should track retries
* remove useless test
* use correct block address (index was off by 1)
* remove duplicate noop proc
* add BlockHandle type
* Use BlockHandle type
* add fetchLocal to control batching from local store
* add format target
* revert deps
* adjust quotaMaxBytes
* cleanup imports and logs
* revert deps
* cleanup blocks on cancelled
* terminate erasure and prefetch jobs on stream end
* split storing and retrieving data into separate tests
* track `b.discoveryLoop` future
* misc
* remove useless check
* sends wantBlock to peers with block. wantHave to everyone else
* Cleanup cheapestPeer. Fixes test for peers lists
* Fixes issue where peerWants are only stored for type wantBlock.
* Review comments by Dmitriy
* consistent logging of addresses
* prevents duplicate scheduling. Fixes cancellation
* fast
* Marks cancel-presence situation with todo comment.
* fixtest: testsales enable logging
* Review by Dmitriy: Remember peerWants only if we don't have them.
* rework `wantListHandler` handling
---------
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
- cleans up all instances of `.track` to use the `module.trackedfutures.track(future)` procedure, for better readability
- removes the `track` override that is no longer used in the codebase
* don't unroll wantCids when sending wantBlock message in blockPresenceHandler
* workaround logging
* Fixes logformatting upraises for sequences.
* Applies upraises rule for setProperty of textmode for sequences.
* Replaces upraises with raises
* Removes redundant log in sendWantHave
* add changes to use chronos v4 in compat mode
* switch chronos to compat fix branch
* use nimbus-build-system with configurable Nim repo
* add missing imports
* add missing await
* bump compat
* pin nim version in Makefile
* add await instead of asyncSpawn to advertisement queue loop
* bump DHT to v0.5.0
* allow error state of `onBatch` to propagate upwards in test code
* pin Nim compiler commit to avoid fetching stale branch
* make CI build against branch head instead of merge
* fix handling of return values in testslotqueue
* integration: move REST API tests into their own module
* integration: move upload and download tests into their own module
* integration: move purchasing tests into their own module
* integration: move marketplace tests to the right module
* integration: mine a block *after* starting nodes
To ensure that tests involving multiple nodes do
not start with out-of-sync clocks
* Fix: do not swallow CancellationErrors
* integration: avoid underflow in UInt256
* network: remove unnecessary error handling
No Exceptions can occur, only Defects, because everything
is asyncSpawned.
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
* network: do not raise in asyncSpawned proc
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
---------
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
* Updates logging for file upload
* Restores trace for placing block and proof in repo store
* Reduces logging while transmitting blocks
* unnecessary formatter
* Clean up some more download related traces
* much better
* Review comment by dryajov