Handling Deep Reorg in Reorg Scanner #1
0xsuryansh
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Background
The current reorg scanner is fairly simplistic and lacks the ability to handle "deep" reorgs that extend beyond a single block. In scenarios like these, it merely displays each consecutive block number as partaking in a reorg. Enhancements to this tool could include the ability to consolidate these into grouped deep reorgs, as well as possibly showcasing instances where multiple branches exist at each reorg junction.
Current implimentation of the reorg sanner
The current scanner works by going through the "Header" table, which stores information about each block. Each entry in the table includes the block number and the block hash. The scanner looks for entries with the same block number but different hashes, which signify a reorg.
Here's a simplified breakdown of the code :
Function FindReorgs: Initialize RemoteCursor for Header table in blockchain database Call findReorgsInternally function with RemoteCursor Output total blocks set and blocks with reorgs (wrong blocks) End Function Function findReorgsInternally: Initialize empty set map for blocks and empty slice for wrong blocks For each entry in Header table using Next method: If scanning is interrupted: Return error End If Skip if entry is empty Extract block number from entry If block number is already in set (meaning it's a reorg): Add block number to wrongBlocks slice End If Add block number to set End For Return set, wrongBlocks and any errors occurred End FunctionApproach towards scanning deep reorg
Here's a high level idea towards how we can approach this
Iteration 1 : Identifying deep reorgs
To modify this existing code to scan for deep reorgs, we need to keep track of consecutive block numbers that are part of the same reorg. Additionally, we need to store not just a single block hash for each block number in the set map, but all block hashes for that block number.
Here's a modification of the
findReorgsInternallyfunction:Now the deepReorgs slice will contain the block numbers of deep reorgs.
Note that int this case block numbers will be repeated if they are part of different deep reorgs.
Limitations of the above approach (iteration 1)
The above approach for identifying deep reorgs does not distinguish between deep reorgs and their respective forks.
The code provided detects deep reorgs by identifying when there is a change in the block hash for the same block number over consecutive blocks. However, it does not track the different branches that might be created during a fork.
Iteration 2 : Identifying deep reorgs and branches
When a reorg occurs, the parent hash of the new blocks will not match the hash of the previous block at the same height. By keeping track of parent-child relationships between blocks, we can determine when a reorg has occurred and also trace the separate branches of the blockchain.
Here's how the code for identifying branches would look like
The deepReorgs map, represents a sort of "inverted" tree or forest structure where each key is associated with multiple values (hashes of blocks and their parents)
This data structure can help us keep track of all the different versions of a block.
Iteration 3 : Representing the Reorgs as a DAG
Creating a DAG to represent a blockchain, including reorganizations, involves structuring each block as a node and each parent-child relationship between blocks as a directed edge. We will be maintaining a DAG where each block points to its parent block, and forks in the blockchain are represented as nodes in the DAG with more than one child.
Each block is represented as a node in a Directed Acyclic Graph (DAG). Each node points to its parent node, and also contains a list of its children nodes, representing blocks that are directly built upon it. A reorganization (or a fork in the blockchain) is represented as a node with more than one child.
This is the updated code for DAG approach:
The allReorgs map, contains information about reorganizations at different heights and their lengths.
In summary, this function analyzes the blocks in a blockchain to identify deep reorganizations. It constructs a map to represent the main chain and another map to store information about reorganizations at different heights and their lengths.
Beta Was this translation helpful? Give feedback.
All reactions