Skip to content

Fix/thread witness validation#259

Merged
BlobMaster41 merged 9 commits intomainfrom
fix/thread-witness-validation
Mar 12, 2026
Merged

Fix/thread witness validation#259
BlobMaster41 merged 9 commits intomainfrom
fix/thread-witness-validation

Conversation

@BlobMaster41
Copy link
Contributor

@BlobMaster41 BlobMaster41 commented Mar 12, 2026

Description

Introduce a dedicated WITNESS thread and supporting pipeline to offload block-witness generation and peer witness handling. Changes include:

  • New ThreadTypes.WITNESS and MessageType entries for witness-related messages.
  • New WitnessThread and WitnessThreadManager to receive forwarded block events and peer witness data, buffer early peer messages, and coordinate witness processing.
  • WitnessSerializer utilities to reconstruct Long instances lost by structured-clone when crossing worker boundaries (reconstructBlockWitness / reconstructSyncResponse).
  • BlockWitnessManager: added selfWitness queue, concurrent self-proof generation (draining logic, MAX_CONCURRENT_SELF_PROOFS), and queueSelfWitness API to decouple/async self-witness creation.
  • P2PManager: methods to queue self proofs, update consensus height, broadcast/request witnesses, and forward incoming peer witness messages/responses to the WITNESS thread.
  • PoC & PoCThread: forward BLOCK_PROCESSED events to the WITNESS thread, add APIs to request/broadcast witnesses, and reconstruct witnesses on receipt from worker threads.
  • Threading/Services updates: wire up the WITNESS thread in Core, PoCThreadManager, BitcoinRPCThreadManager, ServicesConfigurations and WorkerConfigurations so the thread is created and linked.
  • Tests: add unit tests for WitnessSerializer and WitnessThread behavior.
  • Minor config change: use btc.sample.conf as configPath in Config.ts.

Rationale: separate heavy/IO-bound witness proof generation into its own worker to avoid blocking P2P/indexer threads, handle structured-clone degradation of Longs across worker boundaries, and allow controlled concurrency for self-generated proof work.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Performance improvement
  • Consensus change (changes that affect state calculation or validation)
  • Refactoring (no functional changes)
  • Documentation update
  • CI/CD changes
  • Dependencies update

Checklist

Build & Tests

  • npm install completes without errors
  • npm run build completes without errors
  • npm test passes all tests

Code Quality

  • Code follows the project's coding standards
  • No new compiler warnings introduced
  • Error handling is appropriate
  • Logging is appropriate for debugging and monitoring

Documentation

  • Code comments added for complex logic
  • Public APIs are documented
  • README updated (if applicable)

Security

  • No sensitive data (keys, credentials) committed
  • No new security vulnerabilities introduced
  • RPC endpoints properly authenticated
  • Input validation in place for external data

OP_NET Node Specific

  • Changes are compatible with existing network state
  • Consensus logic changes are documented and tested
  • State transitions are deterministic
  • WASM VM execution is reproducible across nodes
  • P2P protocol changes are backward-compatible (or migration planned)
  • Database schema changes include migration path
  • Epoch finality and PoC/PoW logic unchanged (or documented if changed)

Testing

Consensus Impact

Related Issues


By submitting this PR, I confirm that my contribution is made under the terms of the project's license.

Introduce a dedicated WITNESS thread and supporting pipeline to offload block-witness generation and peer witness handling. Changes include:

- New ThreadTypes.WITNESS and MessageType entries for witness-related messages.
- New WitnessThread and WitnessThreadManager to receive forwarded block events and peer witness data, buffer early peer messages, and coordinate witness processing.
- WitnessSerializer utilities to reconstruct Long instances lost by structured-clone when crossing worker boundaries (reconstructBlockWitness / reconstructSyncResponse).
- BlockWitnessManager: added selfWitness queue, concurrent self-proof generation (draining logic, MAX_CONCURRENT_SELF_PROOFS), and queueSelfWitness API to decouple/async self-witness creation.
- P2PManager: methods to queue self proofs, update consensus height, broadcast/request witnesses, and forward incoming peer witness messages/responses to the WITNESS thread.
- PoC & PoCThread: forward BLOCK_PROCESSED events to the WITNESS thread, add APIs to request/broadcast witnesses, and reconstruct witnesses on receipt from worker threads.
- Threading/Services updates: wire up the WITNESS thread in Core, PoCThreadManager, BitcoinRPCThreadManager, ServicesConfigurations and WorkerConfigurations so the thread is created and linked.
- Tests: add unit tests for WitnessSerializer and WitnessThread behavior.
- Minor config change: use btc.sample.conf as configPath in Config.ts.

Rationale: separate heavy/IO-bound witness proof generation into its own worker to avoid blocking P2P/indexer threads, handle structured-clone degradation of Longs across worker boundaries, and allow controlled concurrency for self-generated proof work.
@BlobMaster41 BlobMaster41 added bug Something isn't working enhancement New feature or request feature fix labels Mar 12, 2026
@BlobMaster41 BlobMaster41 requested a review from Copilot March 12, 2026 09:19
Comment out creation of the ThreadTypes.WITNESS thread to prevent it from starting during initialization (likely temporary/diagnostic). Update config path to load config/btc.conf instead of the sample config/btc.sample.conf. Also apply a minor import formatting cleanup in Core.ts.
@BlobMaster41 BlobMaster41 force-pushed the fix/thread-witness-validation branch from 530f72e to 3cf2f75 Compare March 12, 2026 09:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a dedicated WITNESS worker thread and message pipeline to move block-witness proof generation and peer witness handling off the P2P/Indexer paths, while adding utilities to reconstruct Long values degraded by structured-clone across worker boundaries.

Changes:

  • Add ThreadTypes.WITNESS plus witness-specific MessageType entries and wire the new thread into Core + thread managers/configuration.
  • Implement WitnessThread/WitnessThreadManager and WitnessSerializer to buffer early peer messages and reconstruct Long fields after cross-thread messaging.
  • Update witness/proof flow in BlockWitnessManager, P2PManager, PoC, and PoCThread, and add unit tests for serializer/thread behavior.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/witness/setup.ts Shared witness test setup (Promise.safeAll polyfill).
tests/witness/WitnessThread.test.ts Unit/integration-style tests for WitnessThread message flow and forwarding behavior.
tests/witness/WitnessSerializer.test.ts Tests for reconstructing Long values after structured clone.
src/src/threading/thread/enums/ThreadTypes.ts Adds new WITNESS thread type.
src/src/threading/enum/MessageType.ts Adds witness-specific inter-thread message types.
src/src/services/ServicesConfigurations.ts Registers WITNESS thread targets and worker resource limits.
src/src/poc/witness/WitnessThreadManager.ts New thread manager for WITNESS worker(s) and link setup.
src/src/poc/witness/WitnessThread.ts New witness worker handling block events, buffering, and peer witness processing.
src/src/poc/witness/WitnessSerializer.ts Utilities to reconstruct Long instances for witness packets crossing worker boundaries.
src/src/poc/peer/OPNetPeer.ts Adjusts witness-response callback typing/behavior for the updated witness flow.
src/src/poc/networking/server/ServerPeerNetworking.ts Adjusts sync-response handling to match updated witness-response callback behavior.
src/src/poc/networking/p2p/BlockWitnessManager.ts Adds self-witness queue + controlled concurrency for self proof generation.
src/src/poc/networking/P2PManager.ts Adds APIs for queuing/self proofs, forwarding peer witness messages to WITNESS thread, and witness broadcast/request helpers.
src/src/poc/PoCThreadManager.ts Links P2P thread with the new WITNESS thread.
src/src/poc/PoCThread.ts Handles witness thread requests (broadcast/request peers) and reconstructs witnesses before use.
src/src/poc/PoC.ts Forwards BLOCK_PROCESSED to WITNESS thread and keeps P2P-side consensus height updated.
src/src/config/Config.ts Changes default config path.
src/src/blockchain-indexer/rpc/BitcoinRPCThreadManager.ts Adds link creation with WITNESS thread.
src/src/Core.ts Ensures WITNESS thread is created when PoC is enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Swap the initialization order of the WITNESS and P2P threads in Core.ts when POC is enabled so the WITNESS thread is created before the P2P thread. This ensures startup order/dependency requirements are respected during initialization.
Introduce a distinct WITNESS_HEIGHT_UPDATE message and refactor witness handling so block height is broadcast to all witness instances while proof generation is round‑robined to a single instance. PoC.onBlockProcessed is serialized via a promise lock, broadcasts WITNESS_HEIGHT_UPDATE to all witnesses, awaits it, then sends WITNESS_BLOCK_PROCESSED to one witness. WitnessThread now handles WITNESS_HEIGHT_UPDATE to set currentBlock and flush buffered peer messages; WITNESS_BLOCK_PROCESSED no longer sets height or receives an onHeightSet callback. Added handling for WITNESS messages in BitcoinRPCThread and a small comment in WitnessThreadManager. Updated tests to reflect the new message flow and behavior.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Replace busy-wait polling in BlockWitnessManager with a slotWaiters queue to await concurrency slots and wake a waiter when a slot frees, reducing CPU usage. Ensure Long values are parsed as unsigned by passing the unsigned flag to Long.fromString/fromNumber in WitnessSerializer. Add error handling for the async onBlockWitnessResponse call in WitnessThread to log rejections instead of leaving unhandled promise rejections. Update tests to await async onBlockProcessed behavior and add tests covering serialized height broadcasts and burst handling to ensure ordering and no dropped blocks.
@BlobMaster41 BlobMaster41 requested a review from Copilot March 12, 2026 10:12
@BlobMaster41 BlobMaster41 merged commit cde8f0a into main Mar 12, 2026
13 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +156 to +159
/**
* Replay peer witness messages that were buffered before the first
* WITNESS_BLOCK_PROCESSED set the current block height.
*/
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above flushPendingPeerMessages() refers to WITNESS_BLOCK_PROCESSED setting the current block height, but the code path that sets the height (and triggers the flush) is WITNESS_HEIGHT_UPDATE. Updating this comment will keep the buffering/flush behavior understandable and consistent with the implementation.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request feature fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants