chore: Improve P2P & mempool robustness and reorg handling#280
Merged
BlobMaster41 merged 6 commits intomainfrom Apr 10, 2026
Merged
chore: Improve P2P & mempool robustness and reorg handling#280BlobMaster41 merged 6 commits intomainfrom
BlobMaster41 merged 6 commits intomainfrom
Conversation
Drop the unused `currentSolutionsHeight` field and its assignment in the challenge-loading flow. Add an explicit `e: unknown` type on a catch handler to avoid implicit any, and apply minor import/whitespace reformatting and a small comment tweak. No functional behavior changes intended. Always refetch challenges; update tests Bump README to v1.0.3 and clarify refetch behavior in BitcoinTransactionVerificatorV2: block height is not a safe cache key under reorgs, so always refetch when onBlockChange is called and rely on targetSolutionsHeight to collapse in-flight duplicate requests. Tests updated to remove reliance on the private currentSolutionsHeight field, asserting against allowedChallenges behavior instead (including failure/retry semantics and reorg scenarios), and to capture the initial promise where appropriate. Improve P2P & mempool robustness and reorg handling Multiple robustness and correctness fixes across networking and mempool code: - BitcoinTransactionVerificatorV2: Harden onBlockChange by adding target/current height tracking, non-poisoning queue (separate caller-visible promise and trailing .catch), retries with exponential backoff, abortable sleep to cancel stale retries, and staleness checks to avoid applying outdated solutions. Adds tests covering retries, abandonment, reorgs, and queue health. - Mempool.watchBlockchain: Track latestObservedHeight to avoid stale callbacks and compute fullSync against Bitcoin Core tip (with 1-block tolerance) instead of a tautological diff. Adds tests for fullSync behavior. - P2PConfigurations: Replace hardcoded noAnnounce list with announceFilter using isPrivate to filter private addresses. - AddressExtractor: Correct protocol parsing by treating valued protocols consistently (avoid mis-parsing components without values). - P2PManager & PeerChecker: Improve blacklist/unreachable handling (avoid duplicate processing), increase blacklist purge timeout, add isMonitoringHealth guard, skip unreachable peers when dialing/re-adding, better logging, and ensure external dial failures are recorded. Small fixes to event handlers and address filtering. - BlockWitnessManager: Adjust reorg check from >= to > to avoid incorrect reorg handling when equal. Overall this commit fixes race conditions, prevents internal queues from being poisoned by transient failures, improves reorg correctness, and tightens P2P peer/address handling. Tests were added for core mempool/verificator behaviours.
d62248b to
3a58782
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves robustness and correctness across the P2P layer and mempool/block-change handling, with added regression tests targeting prior audit findings and reorg/race edge cases.
Changes:
- Harden mempool/block-change processing against races and transient failures (staleness tracking, retries/backoff, non-poisoning queues).
- Improve P2P peer handling (unreachable/blacklist behavior, connection-health monitoring guards, address announcement filtering).
- Add extensive tests covering mempool/verificator behavior and reorg/watchdog race scenarios; bump README version.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/witness/WitnessThread.test.ts | Minor test formatting/expectation layout tweaks. |
| tests/vm/deployContractAtAddress.test.ts | Test formatting refactors; preserves existing behavior. |
| tests/reorg/watchdog/toctouCurrentHeader.test.ts | Reformats async expect blocks; minor comment formatting. |
| tests/reorg/watchdog/doubleRevertRace.test.ts | Reformats spies/expect blocks; minor comment formatting. |
| tests/reorg/blockindexer/stuckFlagRecovery.test.ts | Reformats calls/spies; minor comment formatting. |
| tests/mempool/MempoolWatchBlockchain.test.ts | New tests validating watchBlockchain fullSync/staleness behavior. |
| tests/mempool/BitcoinTransactionVerificatorV2.test.ts | New tests validating retry/backoff, staleness abandonment, and queue health. |
| src/src/poc/networking/PeerChecker.ts | Avoid duplicate “unreachable” handling; centralize failure tracking for external dials. |
| src/src/poc/networking/P2PManager.ts | Improve unreachable/blacklist handling, prevent duplicate processing, guard health monitoring, skip unreachable peers. |
| src/src/poc/networking/p2p/BlockWitnessManager.ts | Fix reorg boundary condition by changing >= to >. |
| src/src/poc/networking/AddressExtractor.ts | Fix multiaddr component parsing by distinguishing valued vs valueless protocols. |
| src/src/poc/mempool/verificator/bitcoin/v2/BitcoinTransactionVerificatorV2.ts | Add non-poisoning queue, target-height tracking, retries w/ exponential backoff, and staleness checks. |
| src/src/poc/mempool/manager/Mempool.ts | Track latest observed height and compute fullSync vs Bitcoin Core tip with staleness guards. |
| src/src/poc/configurations/P2PConfigurations.ts | Replace hardcoded noAnnounce with announceFilter using isPrivate. |
| README.md | Version bump to v1.0.3. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Reorganize P2PConfigurations imports and apply a minor stylistic tweak; remove an unused TRANSPORT_PROTOCOLS constant. - P2PConfigurations.ts: reorder multiaddr/Multiaddr imports and add parentheses around the announceFilter arrow parameter for consistency. - AddressExtractor.ts: delete the unused TRANSPORT_PROTOCOLS set. These are small cleanup changes to improve code style and remove dead code. Update BitcoinTransactionVerificatorV2.ts Add recommended Bitcoin mainnet node setup Adds a "Recommended Bitcoin Mainnet Node Setup" section to the README with instructions to install Bitcoin Core and a sample bitcoin.conf. The snippet includes RPC and node settings (rpcuser, rpcpassword, server, txindex, prune, datadir, rpcport, rpcworkqueue, rpcthreads, timeouts, mempool settings, etc.) and notes to adjust rpcuser, rpcpassword, and datadir for your environment.
920c914 to
ddaf396
Compare
Introduce a patch-aware parsing flow for epoch graffiti: add a patchHeight constant and use readBytesWithLength for blocks before the patch (preserving v1.0.1 parse behavior and keeping resync consistency), while using raw readBytes for blocks at/after the patch. Keeps the max-length check and throws an error when graffiti is too long.
Reorder the declaration of GRAFFITI_LENGTH_PATCH_BLOCK_HEIGHT so patchHeight is defined immediately after maxLength and before the graffiti variable. This groups consensus-related constants together for clarity; no functional behavior is changed.
Add handling for legacy graffiti encoding in DeploymentTransaction: import BinaryWriter and Submission, and introduce getGraffiti(submission) which conditionally length-prefixes graffiti for blocks before OPNetConsensus.consensusEpochPatches.GRAFFITI_LENGTH_PATCH_BLOCK_HEIGHT. Use getGraffiti when constructing deployment witness data to preserve backward-compatible serialization of graffiti across the consensus patch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Multiple robustness and correctness fixes across networking and mempool code:
BitcoinTransactionVerificatorV2: Harden onBlockChange by adding target/current height tracking, non-poisoning queue (separate caller-visible promise and trailing .catch), retries with exponential backoff, abortable sleep to cancel stale retries, and staleness checks to avoid applying outdated solutions. Adds tests covering retries, abandonment, reorgs, and queue health.
Mempool.watchBlockchain: Track latestObservedHeight to avoid stale callbacks and compute fullSync against Bitcoin Core tip (with 1-block tolerance) instead of a tautological diff. Adds tests for fullSync behavior.
P2PConfigurations: Replace hardcoded noAnnounce list with announceFilter using isPrivate to filter private addresses.
AddressExtractor: Correct protocol parsing by treating valued protocols consistently (avoid mis-parsing components without values).
P2PManager & PeerChecker: Improve blacklist/unreachable handling (avoid duplicate processing), increase blacklist purge timeout, add isMonitoringHealth guard, skip unreachable peers when dialing/re-adding, better logging, and ensure external dial failures are recorded. Small fixes to event handlers and address filtering.
BlockWitnessManager: Adjust reorg check from >= to > to avoid incorrect reorg handling when equal.
Overall this commit fixes race conditions, prevents internal queues from being poisoned by transient failures, improves reorg correctness, and tightens P2P peer/address handling. Tests were added for core mempool/verificator behaviours.
Type of Change
Checklist
Build & Tests
npm installcompletes without errorsnpm run buildcompletes without errorsnpm testpasses all testsCode Quality
Documentation
Security
OP_NET Node Specific
Testing
Consensus Impact
Related Issues
By submitting this PR, I confirm that my contribution is made under the terms of the project's license.