sidebar_label | sidebar_position |
---|---|
'Consensus Algorithm' | 1 |
dBFT 2.0 Algorithm
Terms
Term | Definition |
---|---|
Consensus Node | Nodes that can propose a new block and vote for the proposed block |
Normal Node | Nodes that can transfer and create transactions, are also ledges, but can neither propose new blocks nor vote |
Speaker | Validator in charge of creating and broadcasting a proposal block to the network |
Delegate | Validator responsible for voting on the block proposal |
Candidate | Account nominated for validator election |
Validator | Account elected from candidates to take part in consensus |
View | Referred to the dataset used during a round of consensus. View numbervstarts from 0 in each round and increases progressively upon consensus failure until the approval of the block proposal, and then is reset to 0. |
Consensus Message
Six types of consensus messages are defined in dBFT2.0:
Message | Definition |
---|---|
Prepare Request | Message for starting a new round of consensus |
Prepare Response | Message informing other validators that all necessary transactions have been collected for block creation |
Commit | Message informing other validators that enough Prepare Response messages have been collected |
Change View Request | Message of view changing attempt |
Recovery Request | Request for consensus data synchronization |
Recovery Message | Response to Recovery Request message |
Consensus Flow
3-Stage Consensus Flow
A round of consensus consists of 4 steps, as shown in the Figure above:
-
Speaker starts consensus by broadcasting a Prepare Request message
-
Delegates broadcast Prepare Response after receiving the Prepare Request message
-
Validators broadcast Commit after receiving enough Prepare Response messages
-
Validators produce & broadcast a new block after receiving enough Commit messages
Here we introduce two variables as follows:
whereNis the number of validators.
A normal algorithm flow is shown below.
1) Initialize local consensus information
-
Initialize consensus context
-
Set the validator whose index equals(h - v) mod Nas the speaker. Here h is current block height, v is the current view, and N is the number of validators
-
Set timeout period. For speaker, if it's handling RecoveryMessage from other nodes, timeout is set to 2v+1*Tblock, whereTblockis Block time and currently 15s. Otherwise, if the speaker has not participated in block generation in last consensus, timeout is set toTblock. If the speaker has participated, calculate D = current time - time of last block's generation. Set timeout to Tblock- D if D < Tblock, or to 0.
-
Broadcast the Recovery Request message to acquire the current consensus context if consensus is newly started.
2) Validators listen to the network and collect transactions until timeout
3) Start consensus
-
For speaker:
-
Select transactions from memory pool according to consensus policy after timeout, create and broadcast Prepare Request message with these transactions' hashes to start a new round of consensus
-
Package and broadcast each 500 selected transactions
-
Set timeout to (2v+1- k(v))*Tblock, where
-
-
For delegates:
-
In case of receiving Prepare Request from the speaker before timeout:
-
Verify the validity of the message and whether it conforms to the local consensus context
-
Prolong local timeout by
-
Update local consensus context
-
For each hash contained in the message, attempt to acquire corresponding transactions from memory pool or unverified transaction pool, and add these transactions to consensus context
-
Ask for transactions not found in step 4 from other nodes
-
-
Otherwise, attempt to change view
-
4) Broadcast Prepare Response
-
If a delegate collects all transactions required in Prepare Request before timeout:
-
For each transaction received, in case of transaction verification failure or against consensus policy, attempt to change view, otherwise add the transaction to consensus context
-
Broadcast Prepare Response message
-
Prolong local timeout by
-
-
Otherwise, attempt to change view
5) Collect Prepare Response and broadcast Commit
-
For the speaker and delegates who have received Prepare Request, if Prepare Response messages from M different delegates are received before timeout:
-
For each Prepare Response message received:
-
Verify the validity of the message and whether it conforms to the local consensus context
-
Prolong local timeout by
-
-
Broadcast Commit message
-
-
Otherwise, attempt to change view
6) Collect Commit message and create new block
-
For each validator already having all transactions required in Prepare Request message, in case of Commit messages from M different validators received:
-
For each Commit message received:
-
Verify the validity of the message and whether it conforms to the local consensus context
-
Prolong local timeout by
-
-
Create and broadcast the new block
-
-
Otherwise, broadcast the Recovery Message, and set the timeout to 2*Tblock
7) Go back to step 1 to start a new round of consensus
Change View Request
Triggering conditions
-
If the transaction verification fails, the delegate will broadcast Change View Request attempting to replace speaker
-
In case of timeout while waiting for Prepare Request or Prepare Response, the delegate will broadcast Change View Request, attempting to replace the speaker
Flow
-
Set the timeout to 2v+2* Tblock
-
If the sum of nodes with Commit sent and fault nodes (referring to the validators from which no other validator receives messages during a block time) is greater than F, broadcast Recovery Request message
-
Otherwise, broadcast Change View Request message, and check the amount of Change View Request received. If not less than M validators reach consensus upon view changing, change local view, initialize local consensus context, and determine the next round's speaker according to new view.
Process logic
When a validator receives Change View Request message:
-
If the message's view is not greater than the local view, this message will be handled as Recovery Request
-
Verify the validity of the message
-
Check the amount of Change View Request received. If not less than M validators reach consensus upon view changing, change the local view, initialize local consensus context, and determine next round's speaker according to new view
Recovery Request Message
Triggering conditions
-
Broadcast Recovery Request message upon enabling the consensus policy to update local consensus context
-
Upon creating Change View Request, if there are not enough active validators (sum of nodes with Commit sent and fault nodes is greater than F), broadcast Recovery Request message to update the local consensus context
Process logic
Upon receiving Recovery Request, a validator will generate and broadcast Recovery Message only if the following conditions are met:
-
This node has already broadcast Commit message
-
This node's index belongs to the given interval:, where j is the index of Recovery Request sender
Recovery Message
Content
-
Change View Request messages from no more than M delegates
-
Prepare Request/Response messages
-
Commit messages
Triggering conditions
-
Upon receiving Recovery Request message, if this node has already broadcast Commit message or its index belongs to the given interval:, where j is the index of Recovery Request sender
-
Upon receiving Change View Request message, if the message's view is not greater than the local view, this message is handled as Recovery Request
-
In case of a timeout while waiting for Commit message, broadcast Recovery Message to resend Commit message (common in network issues)
Process flow
-
Verify the validity of the message and the local consensus context. If the message's view is greater than the local view, and this node has already sent Commit message, ignore this message
-
Otherwise, if the message's view is greater than the local view, handle Change View Request messages inside
-
If the message's view equals local view:
-
Handle Prepare Request message inside
-
If this node has neither sent nor received Prepare Request message, handle Prepare Request message inside
-
Otherwise if this node is the speaker, broadcast Prepare Request message
-
-
Handle Prepare Response messages inside
-
-
If the message view is not greater than the local view, handle Commit messages inside
The mechanism with Change View Request, Recovery Request and Recovery Message can keep consensus safe from timeout caused by the network, abnormal nodes (malicious nodes, fault nodes, etc.) and other issues.
Consensus Policy
Consensus policy is used in the following scenarios:
-
Upon receiving transactions from other nodes, nodes will perform verification to filter out transactions against consensus policy
-
Upon receiving transactions, the consensus module needs to verify whether these transactions satisfy the consensus policy, if not, it will attempt to change the view
-
The Validator needs to filter transactions in its context upon enabling the consensus policy, only confirmed transactions can be added into the memory pool
-
The speaker needs to select transactions from memory pool according to the consensus policy for new Prepare Request