Chain Protocol

There are chain related protocols: chain import , chain export and chain reorganize

Blockchain Chain

Chain Importion

Chain importion is to import a new chain into its local canonical chain. To be specific, it iterates over the blocks of the new chain and write them to its local blockchaini separately with five steps: (1) set the block total difficulty, which is the sum of the block's difficulty and parent block's total difficulty; (2) process the transactions inside the block; (3) write the block to the chain database; (4) if the block's td is less than the canonical chain's td, it will form a fork, writing it to the side chain; (5) if the block's td is greater than canonical chain's td, inserting the block to the canonical chain and reset the td for the blockchain.

Chain Exportion

Chain exportion is to export the whole blockchain data to a file, which can be imported by other nodes to synchronize with it.

Chain Reorganization

If a new block's total difficulty is higher than the current block's total difficulty, it will add the block to the local canonical chain. At this point, if the block's parent hash is not equal to the current block's hash, it will reorganize the current block and the new block. Therefore, chain reorganization takes two blocks to reconstruct the local blockchain. To be specific, it looks for the parent of the new block and append it to the parent to form a new chain, and then calculate the transaction difference between the new chain and old chain, and then removing the corresponding transactions receipts from the database and move those transactions back to the txpool for future blocks.