> For the complete documentation index, see [llms.txt](https://emerald-orbs.gitbook.io/document/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emerald-orbs.gitbook.io/document/consensus-mechanism.md).

# Consensus Mechanism

## dBFT 2.0 Algorithm

#### Terms <a href="#terms" id="terms"></a>

| **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 number *v* starts from 0 in each round and increases progressively upon consensus failure until the approval of the block proposal, and then is reset to 0. |

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FGQu2iK8bhieXqcFXw0Nf%2FAUTHENTICA%20(2).png?alt=media&amp;token=41000262-06b4-479d-a3f1-e6b97fb2202a" alt=""><figcaption></figcaption></figure>

## Consensus Message <a href="#consensus-message" id="consensus-message"></a>

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 <a href="#consensus-flow" id="consensus-flow"></a>

#### 3-Stage Consensus Flow

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FqjGU72axstuMiBrYjDpq%2Fimage.png?alt=media&amp;token=3ed23fec-58b7-4c87-88e3-630ff7e7e17a" alt=""><figcaption></figcaption></figure>

A round of consensus consists of 4 steps, as shown in the Figure above:

1. Speaker starts consensus by broadcasting a Prepare Request message
2. Delegates broadcast Prepare Response after receiving the Prepare Request message
3. Validators broadcast Commit after receiving enough Prepare Response messages
4. Validators produce & broadcast a new block after receiving enough Commit messages

Here we introduce two variables as follows:

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FJcPWQx3bjFEivLa9scsM%2Fimage.png?alt=media&amp;token=2f3441ea-1d2f-4bc6-94e3-bb5380c54f47" alt=""><figcaption></figcaption></figure>

where *N* is the number of validators.

A normal algorithm flow is shown below.

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FGQu2iK8bhieXqcFXw0Nf%2FAUTHENTICA%20(2).png?alt=media&amp;token=41000262-06b4-479d-a3f1-e6b97fb2202a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FkrHeGVmGRwsUZplhhihF%2Fimage.png?alt=media&amp;token=8ec1e32a-c023-4263-b558-58a0bc18b6f7" alt=""><figcaption></figcaption></figure>

**1) Initialize local consensus information**

1. Initialize consensus context
2. Set the validator whose index equals *(h - v) mod N* as the speaker. Here h is current block height, v is the current view, and N is the number of validators
3. Set timeout period. For speaker, if it's handling RecoveryMessage from other nodes, timeout is set to 2v+1 *Tblock, where* Tblock *is Block time and currently 15s. Otherwise, if the speaker has not participated in block generation in last consensus, timeout is set to* Tblock\*. 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.
4. 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:
  1. 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
  2. Package and broadcast each 500 selected transactions
  3. Set timeout to (2v+1 - k(v))\*Tblock, where

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FS4SQ5kJdhEGzwsnEW8Ph%2Fimage.png?alt=media&amp;token=fa31b147-d049-4ac6-979b-427ded14663c" alt=""><figcaption></figcaption></figure>

For delegates:

* In case of receiving Prepare Request from the speaker before timeout:
  1. Verify the validity of the message and whether it conforms to the local consensus context
  2. Prolong local timeout by

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FFUsez2ktVoNil3bktsF8%2Fimage.png?alt=media&amp;token=150f7c8e-2cd0-408e-8653-94074c640e1d" alt=""><figcaption></figcaption></figure>

1. Update local consensus context
2. 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
3. Ask for transactions not found in step 4 from other nodes
4. Otherwise, attempt to change view

**4) Broadcast Prepare Response**

* If a delegate collects all transactions required in Prepare Request before timeout:
  1. 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
  2. Broadcast Prepare Response message
  3. Prolong local timeout by

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FtEqWNftYgKe9UzehTpzW%2Fimage.png?alt=media&amp;token=b65215b1-12fc-403e-b249-8aec23826d76" alt=""><figcaption></figcaption></figure>

* 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:
    1. Verify the validity of the message and whether it conforms to the local consensus context
    2. Prolong local timeout by

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FlhvEHNHjILpO2cHJW3PF%2Fimage.png?alt=media&amp;token=aad47644-a5cd-4c62-aef5-3a5095855bf0" alt=""><figcaption></figcaption></figure>

* 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:
    1. Verify the validity of the message and whether it conforms to the local consensus context
    2. Prolong local timeout by

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FpeOpZ9c8gFqwao9LVfWM%2Fimage.png?alt=media&amp;token=156bf282-3664-43a8-8c12-b7951419164a" alt=""><figcaption></figcaption></figure>

* 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 <a href="#change-view-request" id="change-view-request"></a>

**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**

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FE1yx9BghzoRhAE3CFS3i%2Fimage.png?alt=media&amp;token=aa1131e3-3cb0-482d-b4ff-8317bc33dd59" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FGQu2iK8bhieXqcFXw0Nf%2FAUTHENTICA%20(2).png?alt=media&amp;token=41000262-06b4-479d-a3f1-e6b97fb2202a" alt=""><figcaption></figcaption></figure>

1. Set the timeout to 2v+2 \* Tblock
2. 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
3. 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:

1. If the message's view is not greater than the local view, this message will be handled as Recovery Request
2. Verify the validity of the message
3. 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

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FjHUU1auhbPpnztl7svcm%2Fimage.png?alt=media&amp;token=b85090bb-2b58-4477-8fce-2a5adee4a134" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FGQu2iK8bhieXqcFXw0Nf%2FAUTHENTICA%20(2).png?alt=media&amp;token=41000262-06b4-479d-a3f1-e6b97fb2202a" alt=""><figcaption></figcaption></figure>

#### 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:

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FjCiiwY7hSJidZkVmcWif%2Fimage.png?alt=media&amp;token=4396153e-b9a2-4a15-9977-e9d5acabf5fd" alt=""><figcaption></figcaption></figure>

, where j is the index of Recovery Request sender

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FkPg1xfNMZz7l45H2F9WD%2Fimage.png?alt=media&amp;token=dcf30d1e-ff73-400d-8318-9e2d5d072a14" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1618855010-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHfoFpxwK7lnCS5rAwk8R%2Fuploads%2FGQu2iK8bhieXqcFXw0Nf%2FAUTHENTICA%20(2).png?alt=media&amp;token=41000262-06b4-479d-a3f1-e6b97fb2202a" alt=""><figcaption></figcaption></figure>

<br>
