Welcome to ATech Store !

PART (4)

Merkle Trees 

Blockchains use Merkle trees for fast and efficient validation of data. Merkle trees summarize the entire set of data in a block by creating a root hash of that data. The root hash is found by repeatedly hashing pairs of child nodes of data until only one node is left. The last remaining child node is known as the Merkle root. 

Mining a Block 


Blockchains are distributed when computers download the blockchain. These computers are known as nodes. When a block is filled up, it is validated through group consensus before it can be added to the chain of previously validated blocks. There are a number of Blockchain consensus mechanisms but regardless of the consensus type used, it is important to note that all transaction data on a chained block is assumed to be trustworthy and the chained data has not been tampered with due to the validation of data by group consensus. 

Mining a Block 

Consensus is a way to ensure the nodes on the network verify the transactions and agree with their order and existence on the ledger. In the case of applications like a cryptocurrency, this process is critical to prevent double spending or other invalid data being written to the underlying ledger, which is a database of all the transactions. 

With consensus, there are different solutions that fit different situations. When deciding to use a specific consensus mechanism, you’re taking on an opportunity cost (e.g. security, speed, etc.). The main difference between consensus mechanisms is the way in which they delegate and reward the verification of transactions. It’s important to mention that most blockchain ecosystems have a hybrid of different consensus mechanisms. There is no need to choose one over the other.

When transactions are broadcast to the blockchain network it takes time for these transactions to be confirmed. It does this because transactions are verified by groups. When a transaction is initiated, it is sent to a pool with other unconfirmed transactions. Nodes group these transactions and then select blocks to be added to the chain. Each block is chained by including data from the previous block and the number of blocks in the chain is the block height. If two blocks were to be added to the chain at the same time the chain with the larger block height is selected to be the primary chain. 

A blockchain gets more secure over time. If there are more blocks confirmed that means that there would be a smaller chance of a different chain to be selected as the primary one. 
A fork is a change of protocol. There are two types of forks, a hard and soft fork. A hard fork is a fork where the data is not backward compatible. This results in a new blockchain being created. A soft fork occurs when data is backward compatible, resulting in a change that would not create a new blockchain.

Types of Consensus 

Proof of Work 

Bitcoin implemented Byzantine Fault Tolerance through a validation system called Proof of Work. In Proof of Work consensus, when a block is validated each node competes to solve a guessing game problem to validate the block of data. This problem is non-computational and random guesses are most efficient. Nodes are called Miners and each miner attempts to guess a piece of data called the "nonce" to succeed in validating a block. All block data plus the current guess (nonce) are run through a cryptographic hash - if the resulting output matches the current level of "difficulty" (usually expressed as a fixed number of leading zeros) the miner has guessed the right answer. 

This difficulty is adjusted by the network to correspond to load and to keep the average block mining time consistent with the schedule the platform defines (the buzzer from our example in chapter 6). A nonce is the random data that is combined with the block data which will produce a hash output matching the current difficulty level of the Blockchain. Any miner who thinks they have the correct answer will share it with all other miners. Miners will confirm the answer is correct by using the nonce with their block data to try to get a result that matches the difficulty setting. If 51% or more of the miners agree with the proposed nonce, the transactions on the winner's block are considered to be correct and the miner with the correct answer will be rewarded (reward is given in platform tokens). If the majority of miners do not agree with the nonce, no reward is given and the work performed is a sunk cost as validation did not occur. 

Bitcoin-Blockchain | by Tech 4 Atech


Any nodes that do not have the correct block data will reconcile by copying the validated block from neighboring nodes. Proof of Work consensus creates a game theory incentive for each node to behave accurately and honestly; any dishonest participants will incur real-world costs in guessing the nonce for a zero percent chance of being rewarded with a payout. 

Proof of Stake 

Proof of Stake is a newer Blockchain consensus system that has been proposed as an alternative to Proof of Work consensus to overcome the scalability and cost concerns in PoW. Proof of Stake removes the guessing game from the validation of blocks so mining no longer requires powerful and specialized hardware. This vastly reduces the energy consumption of the network as well. Proof of Stake consensus uses a system where “validator” nodes each give or pay a stake in order to validate transactions. When it’s time for group consensus, all who wish to participate lock up funds in a stake. 

A random node is selected and the hash of that node’s block data is shown to all other participants. All other nodes wager on the validity of the block transactions. If the majority agree with the proposed block, the random node is rewarded as are all who wagered on that node. If the majority disagree, the random node loses their stake, gets no reward, and a new node is randomly selected to share their block data. The game theory incentive toward honesty and accuracy is maintained, only the mechanics of how it's enforced are changed. The key difference with this consensus is that no computing is ever performed during consensus, only wagering and any kind of device can wager, regardless of computing power. 

Other Consensus Mechanisms 

Proof of Activity – is a hybrid of PoW and PoS. Empty template blocks are mined (PoW) then filled with transactions which are validated via PoS. 

Proof of Burn – is where coins are “burned” by sending them to an address where they cannot be retrieved. The more coins burned, the better the chances of being selected to mine the next block. 

Proof of Capacity – is where hard drive space is staked to participate. The most space ‘staked', the better the odds of being selected to mine the next block. The consensus algorithm here generates large datasets called ‘plots’ which consume storage. 

Proof of Elapsed Time – was created by Intel to run on their trusted execution environment. It is similar to PoW but far more energy efficient. The concern is this requires trust in Intel and can be viewed as a central authority. 

Proof of Authority - uses a set of “authorities” which are nodes that are explicitly allowed to create new blocks and secure the Blockchain. This is a replacement for PoW but only for Private Blockchains. Nodes have to earn the right to become a validator/authority.



Blockchain 2.0 and Ethereum 


When Bitcoin went live in 2009, blockchain was nothing more than a record keeping device, a place to permanently record data for future use. Bitcoin and other platforms which only offer the ability to store and retrieve data are often referred to as "blockchain 1.0" platforms. 
In 2015 Ethereum introduced the concept of "blockchain 2.0" platforms by introducing the concept of Smart Contracts. The ability for developers to include custom logic and rules in their transactions now meant blockchain could do more than just data storage, it was now a fully-fledged application development platform. Business processes could be modeled and automated on the same platform that transaction data lived on. 

Today, Ethereum is one of the most widely-used blockchain platforms. Smart Contracts, also known as chain code, are a way to program rules and decision points into transactions and processes on a blockchain. For those from a development background, a Smart Contract can be thought of a class in traditional programming terms. Smart Contracts are published to the blockchain directly and allow one to automate transactions and ensure they all follow the same rules. Each Smart Contract, along with the transactions it performs, exists as records or transactions on the blockchain. Therefore, Smart Contracts also live as permanent entities on the blockchain – this is an important point to keep in mind when evaluating a Smart Contract as a potential solution component. 

Smart Contracts provide: 

• Autonomy: Smart Contracts can be developed by anyone, no need for intermediaries such as lawyers, brokers, or auditors 
• Backup: A Blockchain and Smart Contracts deployed to it provide a permanent record, allowing for auditing, insight, and traceability even if the creator is no longer in business 
• Efficiency: Removing process intermediaries often results in significant process efficiency gains 
• Accuracy: Replacing human intermediaries with executable code ensures the process will always be performed the same 
• Cost Savings: Replacing intermediaries often provides significant cost reduction 

Gas in Ethereum 
Specific to the Ethereum Blockchain is the concept of gas. This concept was born out of a limitation the developers of Ethereum saw with Bitcoin, specifically its programming language called Bitcoin Script. One of the major limitations of Bitcoin Script is the inability to perform loops or iterations in the language. 

This severely limits the types of functions developers can create in Bitcoin Script. This limitation was intentional – the developers of Bitcoin did not want to create a mechanism by which malicious or simply inexperienced developers could put the platform into an infinite loop. Ethereum developers introduced the concept of gas to allow for functionality lacking in Bitcoin Script in order to provide developers Turing complete Smart Contract development languages such as Solidity and Viper. 

Gas is simply how users pay for the cost of a transaction to be processed or validated on the Ethereum Blockchain. Gas is a separate reward given to all miners independently of the consensus mining reward. Gas is used to compensate all nodes on the network for the cost incurred in recording a single transaction. Every transaction (write) on the Ethereum Blockchain must be submitted with gas, any unused gas is returned to the user. 

Note that reading data from the Blockchain is not considered the transaction, and therefore does not incur a gas cost. The concept of gas not only pays for the cost of recording a transaction on their copy of the ledger, but it also prevents infinite loops and closes certain security vulnerabilities. An infinite loop would require infinite gas, and infinite funds with which to purchase that gas. 

It is important to note that gas is only consumed when data is written to the Blockchain. Reading from the Blockchain consumes no gas. 

A function (in a Smart Contract), which runs out of gas, will be terminated and no gas will be returned to the user. It is also important to note that the management of gas is handled at the protocol level – the protocol itself will remove Ether from a user's wallet, convert it to the requested amount of gas, and return any unspent gas to the wallet after converting it back into Ether. The user does not need to intervene or even be aware that this occurring. This also means developers only need to consider the gas costs of the transactions in their functions; they do not need to worry about managing the conversion of currency into gas and gas back into currency. 

Finally, calculating the amount of gas needed to process a transaction is possible but a general rule of thumb is, the more write operations in a Smart Contract there are, the more gas required. Gas is decoupled from Ether so that real gas price costs remain constant while Ether prices can continue to be volatile. If NOT using an Ethereum Blockchain, it is important to determine how one will implement their own gas/fee/incentive system, or otherwise compensate nodes for the act of recording new individual transactions. Gas is also tied to the type of operation being performed – more complex operations will require more gas than simple ones. Online calculators, such as Eth Gas Station (available online at ethgasstation.info), are available to help developers and architects estimate gas costs based on the operations being performed. 

Blockchain and Smart Contracts | by Tech 4 Atech