Introduction to Blockchain
Introduction to Blockchain
Blockchain
Introduction to Blockchain
• A block in a blockchain is a group or collection of information.
• The information is added to the block in a blockchain, by connecting it with
other blocks in the chronological order and creating a chain of blocks linked
together.
• Let’s take the example of Google spreadsheet or MS Excel (Windows). This
spreadsheet is shared among different computer networks, where everyone has
a copy. The spreadsheet contains data and information of transactions made by
individuals. Anyone with a copy can access that spreadsheet but no one can
edit or change it. This is how the blockchain works. It uses blocks, whereas
spreadsheet works with “rows” and “columns”.
• The blockchain is a distributed ledger. This means that a ledger is spread across
the network between all peers in the network, and each peer holds a copy of the
complete ledger.
2
Blockchain Basics – Key
Elements
• Peer-to-peer Network
• Distributed Ledger System
• Key Cryptography
• Hashing
• Proof-of-Work
• Merkle Tree and Merkle Root
3
Peer-to-Peer Network
• A peer-to-peer network is a decentralized and distributed network.
• In a P2P network, the participant’s nodes are not linked to a central body or
server. Instead, all the nodes/computers are independent and are directly
linked with one another to carry out transactions or information exchange.
• In a public blockchain, it is not necessary to know and trust each node in a
network personally.
• The network and security are so designed such that authentic and legitimate
transactions can take place even if the identities of sender and receiver stay
anonymous.
• A peer-to-peer network is a secure network with direct contact between two
nodes. A participant node of such a network has no geographical constraints,
that is, it can be a part of the network from anywhere in the world.
4
5
Distributed Ledger System
• Blockchain works on a distributed ledger system. By distributed ledger, we mean
that the data record is not kept by any one central authority rather, it is
available at every node of a P2P network.
• Every participant node in a network keeps updated copies of the data record or
ledger in a blockchain.
• For instance, if you and I are two nodes in the Bitcoin public network, we both
will have the same copy of the record of bitcoin transactions that took place till
date.
• The record will not just show us our individual transaction but the transactions
that took place between any two nodes in the network. This makes the entire
network fraud-proof because everyone has a legitimate copy of the records and
no one can falsify the transactions or double-spend the same bitcoins.
6
7
Key Cryptography
• Now, after understanding how a network is laid out to carry out
transactions. The next natural question is of security of our data and
the authenticity of participant nodes. If ‘A’ wants to send a message
or confidential information to ‘B’ in a public network with hundreds of
nodes, what is the guarantee that it reaches only ‘B’ and not ‘C’?
• How can ‘B’ assure that the message is coming from ‘A’? How can ‘A’
and ‘B’ both make sure that the message was not changed in the
middle of transmitting?
8
• Well, the answer to every question and solution to this problem of
security, data integrity, and user authenticity is “Asymmetric key
cryptography”.
• In such type of cryptographic arrangement, every node has two
keys; Private key and Public key. The private key is known only to the
node owner whereas the public key is known to all the nodes in the
network.
• The combination of a private key and a public key makes a unique
digital signature for every node. This makes the node authentic to
carry out transactions and verifications.
9
10
Hashing
• Hashing is a non-reversible process of encrypting a block’s
information. Every block in a blockchain is composed of a block
header and transaction data.
Hashing is encrypting an input value of variable length into an
output of fixed length using SHA256 (Secure Hash Algorithm 256)
algorithm.
• For instance, look at the image below to see how hashing encrypts a
text value into a hash value.
11
Similarly, any kind of information contained in a block is encrypted by hashing. No
two words or sentences or numbers have the same hash. There is a new hash for even a
minor change in a message like change in case of an alphabet, etc. Hashing converts
readable information into a non-readable form. This is how important information is
secured in blockchain. 12
Proof-of-Work
• Now, this is an interesting concept of ensuring the safety of data in a
blockchain. By doing proof-of-work, nodes permanently add a block
into the blockchain.
• The chain of blocks keeps the data securely by hashing and linking.
• If a node made a new block and it wants to add that block
permanently into the blockchain as a legitimate block, the system
gives a mathematical puzzle to each such node.
• The nodes must have significant computational power to solve this
puzzle. A single node takes an average of 10 minutes to find the
correct answer to a hash problem. When a node finds the correct
answer, the block gets further in the race of getting added
permanently in the blockchain.
13
• If a good number of nodes solve the puzzle for the same block, it gets
added in the blockchain instantly. There are several terms related to
Proof-of-Work that we need to know such as Nonce, difficulty target,
and Merkle root.
14
Proof of Work
17
Merkle Tree and Merkle Root
• As we know, all the transactions taking place within a blockchain
network stay in the blockchain as a flat file or in database. A particular
set of transactions together forms a block and that block gets added
into the blockchain.
• Now, an interesting thing to note here is how to efficiently store a
huge number of transactions in a block? If we take all the transactions
as it is and keep it in a block, the block size will get unmanageably
large.
18
• Merkle tree and root are a
solution to this problem.
Merkle trees structure the
data in such a way that at the
end of it there is only one root
representing the entire tree.
The individual transaction
makes the bottom-most tier of
the Merkle tree and is known
as leaf nodes. As we can see in
the diagram below, Tx100,
Tx101, etc. are transactions
stored in the block.
19
• Every leaf node is a hash of
transactional data, and the
non-leaf node is a hash of
its previous hashes.
Merkle trees are in a
binary tree, so it requires
an even number of leaf
nodes. If there is an odd
number of transactions,
the last hash will be
duplicated once to create
an even number of leaf
nodes.
20
Merkle trees have three
benefits:
The Merkle Tree maintains the integrity of the data. If any single detail
of transactions or order of the transaction's changes, then these
changes reflected in the hash of that transaction. This change would
cascade up the Merkle Tree to the Merkle Root, changing the value of
the Merkle root and thus invalidating the block. So everyone can see
that Merkle tree allows for a quick and simple test of whether a specific
transaction is included in the set or not.
• It provides a means to maintain the integrity and validity of data.
• It helps in saving the memory or disk space as the proofs, computationally
easy and fast.
• Their proofs and management require tiny amounts of information to be
transmitted across networks.
21
Blockchain Network
• A blockchain network consists of multiple nodes that have a point-to-point or
peer-to-peer connection.
• Each node in a blockchain network is capable of doing transactions, verifying
other transactions and doing proof-of-work.
• Let us consider a scenario where there are 500 nodes spread across the globe
making a blockchain network. Node A (from USA) wants to send some
information to node B (in London). Both the nodes A and B will have their set of
private and public keys. Before A sends the information packet to B, it will encrypt
the message first with A’s private key and then again with B’s public key.
• This is to ensure that the message goes only to B and B can confirm that A has
sent this message out of so many nodes.
22
MERKLE PATRICIA TRIE
• A trie, or a digital tree, is an ordered tree data structure used to store
a dataset.
• A Merkle Patricia Trie provides a cryptographically authenticated data
structure that can be used to store all (key, value) bindings.
• Merkle Patricia Tries are fully deterministic, meaning that tries with
the same (key, value) bindings is guaranteed to be identical—down to
the last byte.
• This means that they have the same root hash, providing the holy
grail of O(log(n)) efficiency for inserts, lookups and deletes. Moreover,
they are simpler to understand and code than more complex
comparison-based alternatives, like red-black trees.
23
PATRICIA TRIE
24
GAS
• All Ethereum accounts and smart contracts are executed through the Ethereum Virtual
Machine (EVM). Performing any kind of operations on the EVM, such as conducting a
transaction, requires some amount of gas. The more complex an operation, the more
units of gas it will cost to execute.
• At a high level, the total amount of gas a user ends up spending to execute a transaction
is calculated by multiplying the price of gas (set by the user) by the number of units of
gas required to execute the transaction.
• Total gas spent (transaction fee) = gas price (base fee + priority fee) * units of gas tx
requires
• For context, a standard peer-to-peer ETH transfer requires no more than 21,000 units of
gas. More complex transactions, however, could use hundreds of thousands of units, if
not more depending on the smart contract functions called.
25
Example
• For example, Rahul needs to give 1 ETH to Shubham. During the
transaction, the gas boundary is 21000 units, and the gas price is 200
gwei.
Total fee costs = Gas units (limit) * Gas price per unit
= 21000 * 200
= 4,200,000 gwei (0.0042 ETH)
• When Rahul dispatched the funds, 1.0042 ETH would be subtracted
from Rahul’s account.
• Shubham would be credited 1.0000 ETH only as 0.0042 is the fess of
Miner.
26
Now after upgradation, increased advantages presented by the
difference contain more profitable trade fee computation, typically
more rapid trade inclusion, and canceling the ETH distribution by
burning a portion of trade fees.
• Beginning with the peer-to-peer network upgrade, each block unit
has a ground fee, the lowest price per unit of gas for inclusion in this
block unit, estimated by the network founded on request.
• Because the ground fee of the trade fee is burnt, users are also hoped
to put a tip in their dealings.
• The information pays miners for managing and breeding user trades
in blocks and is hoped to be set automatically by most wallets.
27
Total fee after upgradation = Gas units (limit) * (Base fee + Tip)
• For example, Shubham has to pay 1 ETH to Rahul. This process has a
gas limit of 21000 units and a base fee of 100 gwei. Shubham includes
a tip of 10 gwei.
Total fee after upgradation = Gas units (limit) * (Base fee + Tip)
= 21000 * (100 + 10)
= 2,310,000 gwei (0.00231 ETH).
28
Transactions and Fee
• Transaction fees are fees that users pay to send a transaction or
interact with a smart contract on a blockchain network. While gas
fees can refer to transaction fees on any blockchain, the term is
mainly used to describe the Ethereum network transaction fees.
29
• There are two main reasons users need to pay fees when sending a
transaction. The first reason is to pay miners or validators (also known
as nodes) for securing the network. Proof-of-work (PoW) blockchains
have miners who validate transactions by using their computing
power to solve complex algorithms. In contrast, proof-of-stake (PoS)
blockchains have validators who stake their tokens to secure the
network.
• The second reason users pay transaction fees is to enable the
operation of smart contracts. Smart contracts are programs that
automatically execute once certain conditions have been met.
30
• When Shubham sends the funds, 1.00231 ETH will be subtracted from
Shubham’s account.
• Rahul will be credited 1.0000 ETH and 0.00021 ETH will be received
by the miner as the tip.
• A ground fee of 0.0021 ETH is ignited.
Gas Fee
• The gas fees include Base, Priority, Max, and calculating fees. Let’s
discuss these terms in detail.
31
On the receiving end, B will have to decrypt the message first using B’s private key and
then with A’s public key. Also, a request of verification of this transaction between A and
B is sent to all the 498 participant nodes in the network. As soon as all the nodes verify a
transaction, the system adds it in a new block with other transactions.
32
Components of a Block/ Block
structure
• Two main elements of a block are header and transaction data. The
block header is an important component as it contains all the
metadata (data about data) of that block.
• Block
• A single block in the blockchain has a Block header, Transaction
counter, Block size and Transaction data. Thus, a single block in the
blockchain contains information about the data and other aspects of
the block. Have a detailed look at the fields that a block contains.
33
Field Field Size Description
34
Block header
• After learning about the contents of a block, let us have a closer look
at the contents of a block header. A block header contains the
metadata about a block and the transactions. The block header for
each block is an important part as it has all the essential details for
that block. It also has the unique fingerprint (hash) of the same and
previous block which helps in maintaining the integrity of the blocks
in the blockchain. In the table given below, you will find six main
components or fields of a block header.
35
Field Field Size Description
The version number of the block to
Block version 4 bytes keep a track of software or protocol
updates.
Previous block hash 32 bytes Hash of the preceding block.
36
• The encrypted value of all these six fields is called the Block hash.
Blockchain nodes use the block hash of one block (preceding) to link it
with the next block. The system creates a block hash using
cryptographic methods of SHA256 algorithm. It is an irreversible
process where the contents of a block once hashed cannot be de-
hashed or decrypted to its original value. The block hashes are like a
digital fingerprint or signature for every block, making it secure.
• For instance, the block hash of the first bitcoin block
was 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1
b60a8ce26f.
37
38
Blockchain Structure
• Blocks are also indexed in a blockchain.
• The first block, also known as Genesis block which is numbered as 0, the next block 1
and so on.
• Every time when a node adds a new block by doing correct proof-of-work, the
system creates a unique hash value for that block. The next incoming block takes this
hash value as “previous block hash” to form a secure link in the chain of blocks.
• This structure of blockchain is tamper-proof because any change in the transaction
data in one block of the blockchain is going to change the hash value of that block.
Then that block’s hash value will not match with the “previous hash value” stored in
the next block. This mismatch will spread across the entire chain, disrupting its
integrity.
• Thus, for someone to make any change in the blockchain data, they need immense
computing power to recalculate the hash of the entire blockchain.
39
Blockchain Structure
40
Properties of Blockchain
Some key properties that make blockchain far superior and better than
the traditional system of information ledgering are as follows:
42
43
How Blockchain Works?
• The first block of the chain is called the “Genesis Block”. Genesis
blocks may also be called “Block 0” or “Day-Zero Block”.
• Each client initially has some balance amount, for example, client A
has five coins, client B also has five coins, whereas client C has ten
coins.
• The nonce is a 32-bit random or pseudo-random arbitrary number
assigned by the miner to each block in order to ensure old
communication is not replayed.
• Furthermore, it can be used as an initialization vector cryptographic
hashing functions.
44
45
46
Mining
o The process by which transactions are verified and added to a blockchain.
Source: https://marmelab.com/blog/2016/05/12/blockchain-expliquee-aux-developpeurs-web-la-
theorie.html
{"chain":
[{
"index":0,
"hashid":"first-block-doesnt-need-it",
"timestamp":1541152127213,
"proof-of-work":"xyz",
"content":
{
"from":"network",
"to":"simone",
"amount":1
}
}]
}
• A record is made of each transaction. This record, which contains certain details of the people
making the transaction, is authenticated using the digital signature of each.
• Each transaction is verified to ensure its validity. This verification process is completed by the
computers connected to the network, each of which independently checks to ensure that the
trade is legitimate. Because this is a decentralized process, it means that every node in the
network needs to agree before the process can be completed.
• Once verified, each transaction is added to a block that gets hashed. “Blocks” are basically
groups of transaction records, and each one is unique. Each block also carries a code known as
a hash value (or hash digest), which both uniquely identifies it and calls out its position within
the blockchain. The hash also ensures the integrity of the data to show that it hasn’t been
modified since it was recorded in the block.
• Once complete, the block is added to the end of the blockchain. This brings us to the end of
the blockchain creation and verification process. Once one block is complete, another block
will soon follow — typically within just a matter of minutes.
Three Pillars of Blockchain
• Decentralization
• Transparency
• Immutability
Blockchain is an emerging technology with many
advantages in an increasingly digital world:
• Highly Secure
It uses a digital signature feature to conduct fraud-free transactions making it
impossible to corrupt or change the data of an individual by the other users
without a specific digital signature.
• Decentralized System
Conventionally, you need the approval of regulatory authorities like a government
or bank for transactions; however, with Blockchain, transactions are done with the
mutual consensus of users resulting in smoother, safer, and faster transactions.
• Automation Capability
It is programmable and can generate systematic actions, events, and payments
automatically when the criteria of the trigger are met.