In the context of blockchain, consensus is the agreement on the state of the blockchain among all the nodes in a network.
In general, consensus means agreement. In the context of blockchain, consensus is the agreement on the state of the blockchain among all the nodes in a network.
The basic characteristic of blockchains is that there’s no single point of control and failure. This means users can query and verify the data from any of the nodes in a network. For this to work, the nodes must agree on the state of the blockchain so that users get the same results when querying multiple nodes for verification of data.
Transactions are used to update state in a blockchain. For example, a transfer transaction can be used to update the balances of multiple users.
Order of Transactions
The main area where consensus is needed is the order of transactions, within a block. If all nodes could agree on the order in which the transactions happened, then they would individually arrive at the same state after processing the respective blocks.
For example, if a user has a balance of 5 tokens and they transfer 4 tokens to one user and 3 tokens to another one. In total, they tried to spend more than they had. In this case, the order of these two transactions would decide which one would be accepted and which one would be rejected by the chain. One of these would be marked as an invalid transaction. If the order of transactions is not agreed upon by all the nodes, then the state of blockchain (balances for these users) would be inconsistent across the network.
To achieve consensus in blockchains, there are several algorithms. The basic idea is:
- Transactions are grouped and packaged together as blocks.
- Based on the consensus algorithm and its block production rules, one of the nodes proposes a block. This could be either using a round-robin fashion or by winning a race.
- For each newly proposed block, the order and validity of transactions are checked against the current state of the chain.
- When the new block is accepted by the chain, the order of transactions in this new block must be followed by all nodes, hence reaching a consensus on the state.