Block synchronization
Introduction
In the course of operation of the blockchain platform, an important prerequisite for transaction validation is the presence of a complete chain of blockchain blocks on the node. Only if a complete blockchain is available, the node gets the opportunity to become trusted and to participate in consensus.
Description of synchronization
Description of synchronization
Data storage structures
Node uses three storage types to store the received blocks:
- BlockChain
- tmpStorage
- rndStorage
BlockChain is the primary repository and is the base for transaction validation, and its completeness is critical to the consensus procedure.
tmpStorage is a temporary storage where continuous chains of blocks that are not in the blockchain are collected. After the continuous chain of successive blocks is formed, the chain is transferred to the blockchain and removed from tmpStorage
rndStorage is a storage for blocks that arrived from the network, but were not included in BlockChain and tmpStorage. When the missing block is requested, rndStorage is checked first, and only then the request is sent to other nodes of the network
New blocks come from the network with different statuses:
- NewBlock
- RequestedBlock
- NewBlock. This status means that the block was released by the writing node at the end of the
- RequestedBlock. The status means that this block was requested from the blockchain storage of any neighbouring node.
BlockChain
A primary data store in which blocks are interlinked by electronic signature and hash and form a continuous chain.
tmpStorage
If the serial number of a block from the network with the NewBlock status is greater than the last block of BlockChain more than 1, the block is placed in tmpStorage, and all missing previous blocks are searched. After the chain is lined up, it is transferred to the blockchain.
rndStorage
If any block with the RequestedBlock status is obtained, but it does not fit into the built tmpStorage chain (the number of the incoming block is greater than the last number of the tmpStorage block more than 1), it is placed in rndStorage.
Types of blocks received
The following types of blocks that can be received and sent by a node are highlighted:
NewBlock. The block that was released as a result of the round, and which is to be added to the blockchain
RequestedBlock. The block that was sent by any node in response to a block synchronization request.
Detection of missing blocks
The new incoming block with the "NewBlock" status should suit by its number in a chain of blockchain (its sequence number is following the last block stored in the blockchain).
In the event that this condition is not met, and the block with the "NewBlock" status has a sequence number differing for more than 1 from the number of the last blockchain block, the process of blocks synchronization begins.
- The number of the incoming block not corresponding to the main sequence of blockchain is stored in the tmpStorage container. All blocks starting from this one are placed in the container where the block number is stored as the key. The number of this block is stored as N(b)
- Next, the blocks to be installed in the blockchain are queried using the synchronization request mechanism. The rndStorage structure is checked, and if the searched blocks are not present there, requests to neighbouring nodes containing numbers of the required blocks N(s)<N(b) are formed. If the blocks are present in the rndStorage container, they are moved to the tmpStorage container.
- In response to synchronization requests, blocks of RequestedBlock type must come to the node. If the condition that N(s) < N(b) is met, the block is placed in the tmpStorage container. Otherwise, the block is placed in the rndStorage container.
- Synchronization requests continue until the missing blocks up to N(b) are received, after which the continuous part of the chain in the form of sequentially saved blocks is transferred to the blockchain.