Avalanche Fundamentals — Consensus Mechanism Explained!
Learning about the Avalanche Ecosystem — Part I
What is a Consensus Mechanism?
Consensus plays a crucial role in blockchain networks. The main objective of a consensus mechanism is to create a single version of truth. i.e. Universally accepted by all the participants in the network. These participants can reach consensus by following a couple of steps called the consensus protocol. This is their way to decide what state of the system is valid and all the state changes. There are many different consensus mechanisms.
Double Spending Attack
Double spending attack is an attack when a user is trying to spend funds that they don’t have. This is done by creating multiple transactions referring to the same funds.
Let’s understand this with an example
I. Alice owns 5 AVAX (AVAX is the native cryptocurrency of the Avalanche platform).
II. Alice now issues two transactions at same time to two different validators. First stating “Send 5 AVAX to Bob” and Second stating “Send 5 AVAX to Charlie”. And tries to execute the payments on the same time.
III. Both of these transactions are valid as she has 5 AVAX and she can send it to both Bob and Charlie. But as she has only 5 AVAX, She can’t do both.
IV. The system has to decide which of these two transactions is valid and where the funds will go and its important to understand that there’s very limited notion of time in blockchain systems so the validators as collective have no way of to identify which transaction was submitted to the system first.
V. Now the validators have to go through the consensus protocol to determine the decision. And different blockchain systems have different consensus mechanisms.
So, What is Avalanche Consensus?
- The avalanche consensus relies on the principle of Repeated Sub-Sampling Voting.
- When we look at our validators, each validator might have an initial preference or no preference at all of which of these two transactions is valid or should be included in the blockchain.
- Let’s say we have a room full of n validators i.e. 25. So the first parameter of the consensus we have is the number of participants (n).
- So now we have to decide on a sample size (k). So let’s say each round of sub-sampling we are asking 5 of the validators. This has to be a number between 1 and n.
- We are going to ask 5 random validators and they’re gonna tell us something. Should the funds go to Charlie or to Bob.
- Now we have the Quorum size (α) — the fraction of the sample size we need to convince us to change our preference. Ex. We have a clear majority of 3 out of 5 that have the same preference, we are going to change our preference.
- In our example let’s say we are asking 5 different validators and 3 of them say that we think we should send the funds to Bob. Since we have a simple majority, at this point we will change our view on the system. we’re gonna change our personal preference also to Bob.
- Then comes the concept of decision threshold (β). That is if I repeat this again and again, how many times in a row do I need to hear the same majority of the Quorum having the same opinion.
- Let’s say that the decision threshold is 8. So if we repeatedly perform sub-sample voting, asking 5 random validators of the 25, 8 times in a row, and 3 of the 5 (Quorum) will repeatedly tell that they think it should be Bob. Their preference is Bob.
- Then I log in my final decision and will not change my mind anymore.
In Short:
- Number of Validators = Number of participants in network = n
- Out of these n Validators, k (sample size) random validators will be selected and asked for their decision on which transaction to prefer.
- Out of these k Validators, The preference will be selected when the same preference has a majority i.e. votes > Quorum size (α).
- This is done consecutively for a number of times which is called the Decision Threshold (β).
- Finally after a preference is decided, The system will log in their decision and not change their mind anymore.
Illustration:
This is done a number of times which is known as the Decision Threshold (β).
Blue Means transaction to Bob is being preferred by the participant and Yellow Means Charlie.
Pseudo-Code of the Mechanism
preference := bob
consecutiveSuccesses := 0
while not decided:
ask k random people their preference
if >= a give the same response :
preference := response with >= a
if preference == old preference:
consecutiveSuccesses += 1
else:
consecutiveSuccesses = 1
else:
consecutiveSuccesses = 0
if consecutiveSuccesses > β
decide(preference)
How to decide if a consensus mechanism is good?
There are two parameters we can use to determine if a consensus mechanism is good:
- Time to finality: From the moment I submitted transaction to the system, how long does it take till it’s finalized and cannot be changed.
- Throughput: How many transactions can be decided on per second.
Comparison with other blockchain networks:
+-----------+---------------------+------------------+
| Network | Throughput | Time to Finality |
+-----------+---------------------+------------------+
| Bitcoin | 7 TPS | 60 min |
| Ethereum | 30 TPS | 6.4 min |
| Avalanche | 2500 TPS per Subnet | ~0.8 seconds |
+-----------+---------------------+------------------+
TPS = Transactions Per Second
Visual Representation of the above data Comparing Avalanche with Ethereum and Bitcoin
Demonstration of Snowball protocol in Action
Introduced by Team Rocket, Scalable and Probabilistic Leaderless BFT Consensus through Metastability (2020)Avalanche Consensus, the Snow algorithm similarly solves the agreement problem in the face of Byzantine nodes. Snow consensus powers Avalanche to achieve a distributed consensus on the chain without relying on any trusted third party.
Link to the Demo: https://tedyin.com/archive/snow-bft-demo/
Link to Github Repository: https://github.com/Determinant/snow-bft-demo
Finalization
In the common case when a transaction has no conflicts, finalization happens very quickly. When conflicts exist, honest validators quickly cluster around conflicting transactions, entering a positive feedback loop until all correct validators prefer that transaction. This leads to the acceptance of non-conflicting transactions and the rejection of conflicting transactions.
References:
- https://academy.avax.network/
- https://www.gyuho.dev/nakamoto-bitcoin-vs-snow-avalanche-consensus.html
- https://www.avax.network/blog/time-to-finality-ttf-the-ultimate-metric-for-blockchain-speed
- https://assets.website-files.com/5d80TL;DR307810123f5ffbb34d6e/6009805681b416f34dcae012_Avalanche%20Consensus%20Whitepaper.pdf