An Introduction to Blockchain Technology
What is Blockchain?
Let's use an analogy to understand what a blockchain is and how it works.
When you interact with web2 applications such as Instagram or Facebook, you use a web browser to connect to a central server over a network. All the code of this web application lives on this central server, and all the data lives in a central database. Anytime you use the web2 application, you must communicate with the central server on the web.
Drawbacks of web2 applications
Imagine if we were building a voting application using a web2 application architecture having a centralized database, we would run into a few problems:
- The data on the database could be changed: it could be counted more than once, or removed entirely.
- The source code on the web server could also be changed at any time.
Instead of having a network, a central server and a database, the blockchain is a network and a database all in one. A blockchain is a peer-to-peer network of computers, called nodes, that share all the data and the code in the network. So, if you're a device connected to the blockchain, you are a node in the network, and you talk to all the other computer nodes in the network. Every node has a copy of all the data and code on the blockchain. There are no more central servers. Just a bunch of computers that talk to one another on the same network.
Instead of a centralized database, all the transaction data that is shared across the nodes in the blockchain is contained in bundles of records called blocks, which are chained together to create the public ledger. This public ledger represents all the data in the blockchain. All the data in the public ledger is secured by cryptographic hashing, and validated by a consensus algorithm. Nodes on the network participate to ensure that all copies of the data distributed across the network are the same.
What would it look like for a user of a web3 application supposedly a voting application on the blockchain? Well, the user needs an account with a wallet address with some Ether, Ethereum's cryptocurrency. Once they connect to the network, they cast their vote and pay a small transaction fee to write this transaction to the blockchain. This transaction fee is called "gas". Whenever the vote is cast, some of the nodes on the network, called miners, compete to complete this transaction. This exists in networks using the Proof of Work (PoW) algorithm. The miner who completed this transaction is awarded the Ether that we paid to vote. Proof of Work has its drawbacks since it takes a lot of time to complete transactions, Ethereum 1.0 can handle 5 transactions per second. Layer 2 networks such as Harmony and Polygon using Proof of Stake algorithm to validate transactions take less time and can handle 2,000 transactions per second. Works are currently on going to merge Ethereum 1.0 and Ethereum 2.0 to have the entire Ethereum network use Proof of Stake algorithm.
What is a Smart Contract?
Ethereum blockchain allows us to execute code with Ethereum Virtual Machine (EVM) on the blockchain with something called a smart contract. Smart contracts are where all the business logic of our application lives. If you're a backend developer, I guess this is where I welcome y'all to the team of developers who take in logic and churn out code that implements the logic. Smart contracts are in charge of reading and writing data to the blockchain, as well as executing business logic. Smart contracts are written in a programming language called Solidity, which looks a lot like JavaScript.
The function of smart contracts on the blockchain is very similar to a microservice on the web. If the public ledger represents the database layer of the blockchain, then smart contracts are where all the business logic that transacts with that data lives. Also, they're called smart contracts because they represent a covenant or agreement.
Traditional front-end client written in HTML, CSS and Javascript is still present in a web3 application. But instead of talking to a back-end server, this client will connect to a blockchain network such as Ethereum, Polygon, Harmony, Rinkeby etc