monorepo
monorepo copied to clipboard
Deal with block reorgs
TODO at DB level
- [x] Add block_hash to output and input table
- [x] Create blocks_header table (block_height, block_hash, prev_hash?)
- [x] Add new table syncBlockStatus (block_height)
- [x] Rename table syncStatus to syncTxStatus
PROCESSES
SYNC_TX (parallel): KEEP SYNCING
- [x] But instead of asking to RPC the next block, get it from DB blocks_header table
- [x] Always insert into "Input" and "Output" tables. If "duplicated key" error is received over output.pk, the zmq already inserted the rows. This means we should update the block_hash value of all the rows of that block_id.
SYNC_BLOCK (sequential):
-
[x] Fill block_header table UNTIL REACHING THE TIP (do not confuse with the initialization process for blockheads download, both cases we receive an error from the node)
-
Detect block reorganization (for each block synced):
-
[X] Get from RPC the current_block to sync
-
[X] Select previous block and get its hash: #"SELECT block_hash FROM block_headers where block_height == current_block -1"
-
[X] Check if the current_block.prev_hash matches the previous block hash. If not, launch "searchChainForkOrigin(current_block)" function
-
Search the origin of the fork (current_block)
-
[X] Ask to RPC for the current_block. Compare with block_headers, if different, continue backwards (get current_block -1 from RPC, compare with block_headers. If different, continue backwards...and so on). If matching, mark range of blocks [found + 1, current_block] to be deleted, and resync from block "found".
-
[X] In resume status, check if there are orphaned blocks, update input and output blockHashes, delete blocks
AGED TXs on mempool
- [ ] Periodically check all tx over mempool to remove the aged ones (see RPC getrawmempool)
Some example explanied on block reogrs https://medium.com/@nicolas.vargas_35315/what-are-blockchain-reorgs-and-how-to-detect-them-when-consuming-nodes-762300f4512e
RPC getchaintips https://developer.bitcoin.org/reference/rpc/getchaintips.html
How to simulate a block reorg on regtest https://bitcoin.stackexchange.com/questions/114044/how-can-i-simulate-a-reorg-for-testing