Skip to content

Fix/deep reorg#260

Open
BlobMaster41 wants to merge 5 commits intomainfrom
fix/deep-reorg
Open

Fix/deep reorg#260
BlobMaster41 wants to merge 5 commits intomainfrom
fix/deep-reorg

Conversation

@BlobMaster41
Copy link
Contributor

@BlobMaster41 BlobMaster41 commented Mar 13, 2026

Description

Add reorg detection when RPC reports a tip at or below already-processed heights: BlockIndexer.onBlockChange now detects height regressions, calls onHeightRegressionDetected which reverts the chain and restarts tasks. ReorgWatchdog gains an additional canonical-hash check and restores the blockchain on mismatch. ChainSynchronisation can fetch header-only blocks in resync mode to avoid downloading full tx data. PoC improves blockProcessedLock handling to avoid lock jams and logs failures; BlockWitnessManager adds an onBlockWitness path and deduplicates witness handling. Misc: reduce WITNESS max instances to 2, adjust VMStorage method signature formatting, and minor comment/whitespace cleanups. Add extensive tests covering reorg edge cases, watchdog behavior, fetcher/watchBlockChanges, and witness thread behavior.

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.

Add reorg detection when RPC reports a tip at or below already-processed heights: BlockIndexer.onBlockChange now detects height regressions, calls onHeightRegressionDetected which reverts the chain and restarts tasks. ReorgWatchdog gains an additional canonical-hash check and restores the blockchain on mismatch. ChainSynchronisation can fetch header-only blocks in resync mode to avoid downloading full tx data. PoC improves blockProcessedLock handling to avoid lock jams and logs failures; BlockWitnessManager adds an onBlockWitness path and deduplicates witness handling. Misc: reduce WITNESS max instances to 2, adjust VMStorage method signature formatting, and minor comment/whitespace cleanups. Add extensive tests covering reorg edge cases, watchdog behavior, fetcher/watchBlockChanges, and witness thread behavior.
@BlobMaster41 BlobMaster41 added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request core component dependencies Pull requests that update a dependency file fix labels Mar 13, 2026
@BlobMaster41 BlobMaster41 requested a review from Copilot March 13, 2026 05:13
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

This PR hardens reorg handling across the indexer by detecting height regressions / same-height competing blocks, restoring canonical state when mismatches are found, and reducing unnecessary data fetching during resyncs. It also adjusts witness-thread behavior and expands the test suite around these edge cases.

Changes:

  • Add height-regression detection in BlockIndexer.onBlockChange and same-height canonical hash verification in ReorgWatchdog.verifyChainReorgForBlock.
  • Add a resync “header-only” fetch path in ChainSynchronisation to avoid downloading full transaction data.
  • Improve PoC witness dispatch locking/error handling, adjust witness-thread scaling, and add/extend reorg + witness tests.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/witness/WitnessThread.test.ts Test refactors/formatting; import ordering and mocking adjustments.
tests/reorg/watchdog/sameHeightReorgEdgeCases.test.ts New watchdog edge-case coverage for same-height hash comparisons and restore behavior.
tests/reorg/watchdog/sameHeightReorg.test.ts New “same-height reorg” tests validating canonical-hash checks.
tests/reorg/watchdog/reorgDetection.test.ts Updates existing watchdog tests to use onBlockChange + Reflect helpers for private methods.
tests/reorg/sync/queryBlockResync.test.ts New tests describing header-only resync “contract” behavior.
tests/reorg/purge/mempoolPreservation.test.ts Minor comment tweak.
tests/reorg/fetcher/watchBlockChangesReorg.test.ts New tests for RPC block-change notifications and reorg-adjacent behaviors.
tests/reorg/edge-cases/integration.test.ts Minor comment tweaks in integration edge cases.
tests/reorg/blockindexer/startupPurge.test.ts Comment tweaks around startup purge behavior.
tests/reorg/blockindexer/onBlockChangeReorg.test.ts New tests describing prior BlockIndexer reorg-detection gaps.
tests/reorg/blockindexer/onBlockChangeEdgeCases.test.ts New tests covering regression detection guard/boundary cases.
src/src/vm/storage/databases/VMMongoStorage.ts Minor comment change in purge path.
src/src/vm/storage/VMStorage.ts Signature formatting change for revertDataUntilBlock.
src/src/services/ServicesConfigurations.ts Reduce WITNESS thread max instances from 4 → 2.
src/src/poc/witness/WitnessThreadManager.ts Clarify thread-linking comment.
src/src/poc/witness/WitnessThread.ts Minor comment tweak.
src/src/poc/witness/WitnessSerializer.ts Import/function signature formatting changes.
src/src/poc/networking/p2p/BlockWitnessManager.ts Reorders/deduplicates onBlockWitness placement.
src/src/poc/mempool/manager/Mempool.ts Minor comment tweak.
src/src/poc/PoC.ts Prevent blockProcessedLock from jamming on rejection; log dispatch failures.
src/src/blockchain-indexer/sync/classes/ChainSynchronisation.ts Add header-only queryBlockHeaderOnly for resync mode.
src/src/blockchain-indexer/processor/reorg/ReorgWatchdog.ts Add same-height canonical hash mismatch detection + restore.
src/src/blockchain-indexer/processor/BlockIndexer.ts Add height regression detection + revert/restart path.
src/src/api/Server.ts Add CORS maxAge for preflight caching.
src/src/Core.ts Import formatting cleanup.

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

Add a comprehensive BlockIndexer height-regression test covering the full revert flow (tests/reorg/blockindexer/heightRegression.test.ts). Remove legacy/overlapping tests that focused on vulnerable behaviors (tests/reorg/blockindexer/onBlockChangeReorg.test.ts and tests/reorg/sync/queryBlockResync.test.ts). Update RPCBlockFetcher tests to emphasize hash-based change detection and error recovery, and add a rapid same-height hash-flip scenario. Clean up ReorgWatchdog same-height test by removing large-gap performance cases and minor formatting changes. Overall this reorganizes and refocuses reorg-related tests toward explicit revert behavior and hash-change detection.
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

This PR strengthens deep reorg handling across the indexer pipeline by detecting same-height/height-regression reorgs earlier, restoring canonical chain state when hashes diverge, and expanding test coverage around these edge cases.

Changes:

  • Add height-regression detection in BlockIndexer.onBlockChange() and canonical-hash verification in ReorgWatchdog.verifyChainReorgForBlock().
  • Add resync “header-only” fetch path in ChainSynchronisation plus PoC/witness-thread robustness tweaks (lock handling, witness handling, thread instance limits).
  • Add/extend Vitest suites covering watchdog/indexer/fetcher/witness reorg edge cases and behavior.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/witness/WitnessThread.test.ts Adjusts witness thread tests/mocking layout and formatting.
tests/reorg/watchdog/sameHeightReorgEdgeCases.test.ts New edge-case suite for same-height hash comparison and restore behavior.
tests/reorg/watchdog/sameHeightReorg.test.ts New “same-height reorg” regression tests.
tests/reorg/watchdog/reorgDetection.test.ts Refactors tests to use onBlockChange and reflective helpers for private methods.
tests/reorg/purge/mempoolPreservation.test.ts Minor comment update.
tests/reorg/fetcher/watchBlockChangesReorg.test.ts New tests asserting hash-based change detection and polling recovery.
tests/reorg/edge-cases/integration.test.ts Minor comment updates around batching expectations.
tests/reorg/blockindexer/startupPurge.test.ts Minor comment updates for startup purge behavior.
tests/reorg/blockindexer/onBlockChangeEdgeCases.test.ts New tests for onBlockChange regression detection guards and boundaries.
tests/reorg/blockindexer/heightRegression.test.ts New tests for full revert flow on detected height regression.
src/src/vm/storage/databases/VMMongoStorage.ts Comment tweak around target epoch purging.
src/src/vm/storage/VMStorage.ts Signature formatting for revertDataUntilBlock.
src/src/services/ServicesConfigurations.ts Reduces WITNESS thread max instances from 4 → 2.
src/src/poc/witness/WitnessThreadManager.ts Documents RPC link being established from the RPC manager side.
src/src/poc/witness/WitnessThread.ts Minor comment tweak around buffering/flush behavior.
src/src/poc/witness/WitnessSerializer.ts Import formatting + sync response reconstruction signature formatting.
src/src/poc/networking/p2p/BlockWitnessManager.ts Moves/keeps onBlockWitness path and witness queue handling.
src/src/poc/mempool/manager/Mempool.ts Comment tweak for sequential broadcast path.
src/src/poc/PoC.ts Prevents blockProcessedLock from jamming on failures; logs broadcast/dispatch failures.
src/src/blockchain-indexer/sync/classes/ChainSynchronisation.ts Adds header-only fetch in resync mode (queryBlockHeaderOnly).
src/src/blockchain-indexer/processor/reorg/ReorgWatchdog.ts Adds same-height canonical block-hash mismatch detection and restore call.
src/src/blockchain-indexer/processor/BlockIndexer.ts Adds height regression detection & onHeightRegressionDetected() revert/restart path.
src/src/api/Server.ts Adds CORS maxAge.
src/src/Core.ts Import formatting cleanup.
package.json Bumps @btc-vision/transaction version.

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

Comment on lines +345 to +351
// In resync mode, only download block headers, no transaction data needed.
// Transactions are preserved from the original sync; only headers/witnesses
// are re-generated.
if (Config.DEV.RESYNC_BLOCK_HEIGHTS) {
return this.queryBlockHeaderOnly(blockNumber);
}

Comment on lines +6 to +7
// First, let's get the class itself:
import { WitnessThread } from '../../src/src/poc/witness/WitnessThread.js';
} from '../networking/protobuf/packets/blockchain/common/BlockHeaderWitness.js';
import { ISyncBlockHeaderResponse } from '../networking/protobuf/packets/blockchain/responses/SyncBlockHeadersResponse.js';
import {
ISyncBlockHeaderResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core component dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants