0% found this document useful (0 votes)
2 views

Introduction to Blockchain - Chapter 2

This document provides an introduction to blockchain technology, detailing the structure of blocks and block headers, the role of cryptographic hashes, and the lifecycle of transactions. It explains key concepts such as Merkle trees, transaction types, and the UTXO model, along with the importance of consensus mechanisms in maintaining blockchain integrity. Additionally, it discusses the genesis block, stale and orphan blocks, and the process of transaction fees and pools.

Uploaded by

Hermona Addisu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Blockchain - Chapter 2

This document provides an introduction to blockchain technology, detailing the structure of blocks and block headers, the role of cryptographic hashes, and the lifecycle of transactions. It explains key concepts such as Merkle trees, transaction types, and the UTXO model, along with the importance of consensus mechanisms in maintaining blockchain integrity. Additionally, it discusses the genesis block, stale and orphan blocks, and the process of transaction fees and pools.

Uploaded by

Hermona Addisu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to

Blockchain
CSEg5304
CHAPTER TWO
A DA M A S C I E N C E A N D T E C H N OLOGY U N IV E R S IT Y
C OE E C – C S E D E PA RT M E N T
Outline
BUILDING BLOCKS OF BLOCKCHAIN
 Structure of Blocks
 Structure of Block Headers
 Cryptographic Hashes
 Transactions
 Merkle Trees
 Transaction Lifecycle
 Consensus

2
Structure of a Block
A Block is merely a selection of transactions bundled together and organized logically. A
transaction is a record of an event, for example, the event of transferring cash from a sender's
account to a beneficiary's account. A block is made up of transactions, and its size varies
depending on the type and design of the blockchain in use.
The following table shows the Structure of a Block:

Field Size Description


Block Size 4 bytes This is the size of the block.
Block Header 80 bytes This includes fields from the block header described in the next
section
Transaction Counter Variable This field contains the total number of transactions in the block,
including the coinbase transaction. Size ranges from 1-9 bytes
Transactions Variable All transactions in the block

3
Structure of a Block Header
Field Size Description
Version 4 bytes The block version number that dictates the block validation rules to
follow.
Previous Block’s 32 bytes This is a double SHA-256 hash of the previous block's header.
Header Hash
Merkle Root Hash 32 bytes This is a double SHA-256 hash of the Merkle tree of all transactions
included in the block.
Timestamp 4 bytes This field contains the approximate creation time of the block in the
Unix epoch time format. More precisely, this is the time when the
miner has started hashing the header. (The time from the miner's
point of view.)
Difficulty target 4 bytes This is the current difficulty target of the network/block.
Nonce 4 bytes This is an arbitrary number that miners change repeatedly to produce
a hash that is lower than the difficulty target.

4
Block
Header
The two most important fields
in the BlockHeader are:
 hashPrevBlock, which
provides a snapshot of what
the Bitcoin network looked
like in the previous block
 hashMerkleRoot, a
snapshot of all the
transactions included in the
current block.

5
Visualization of
Block, Block Header
and Merkle Root
Each block contains transactions
and block headers.
Each block header specifies the
characteristics about the previous,
current and upcoming blocks.
Merkle root trees are how blocks
maintain their integrity, it is a hash
that allows proof of the validity of
the blockchain.

6
The Genesis Block
Genesis Block is the first block in the Bitcoin blockchain. The genesis block was
hardcoded in the bitcoin core software. It is in the chainparams.cpp file (
https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp):
Transactions and blocks are added in the blockchain only after strict rule
checking.
Block height is the number of blocks before a particular block in the blockchain.
The current height (as of March 3, 2025) of the blockchain is 886,131 blocks. (
https://www.blockchain.com/explorer/assets/btc)
 Each block contains one or more transactions, out of which the first transaction is
a Coinbase Transaction.

7
State Blocks and Orphan Blocks
Stale Blocks are created when a block is solved and every other miner who is
still working to find a solution to the hash puzzle is working on that block.
Orphan Blocks are also called detached blocks and were accepted at one point
in time by the network as valid blocks but were rejected when a proven longer
chain was created that did not include this initially accepted block. They are not
part of the main chain and can occur at times when two miners manage to
produce the blocks at the same time.
New blocks are added to the blockchain approximately every 10 minutes and
network difficulty is adjusted dynamically every 2016 blocks in order to maintain
a steady addition of new blocks to the network.
Network difficulty is calculated using the following equation:
Target = Previous target * Time/2016 * 10 minutes
8
Bitcoin Block
#170

Bitcoin block #170, which


records a transaction of 10
BTC sent from Satoshi
Nakamoto to developer
and early blockchain
pioneer Hal Finney.

9
Cryptographic Hashes
A cryptographic hash is a function that converts any form of data into a fixed-size string. Hashes have
the following attributes that make them attractive for blockchain:
 No matter the input, the resulting hash will always be a fixed length. For example, the hash
generated by SHA-256 will always be 256 bits long.
 A hash is a one-way encryption, meaning it is easy to encrypt the data.
 Conversely, it is extremely difficult, if not impossible, to decrypt the hash back to the original input
data.
 A hash is deterministic. This means every time the same input data is entered, the resulting hash
will always be the same. It is also easy to re-create a hash later using the same inputs and compare
it to the original to see if any tampering or corruption of data has occurred.
 Any slight change to the input data makes the resulting hash look very different. This adds to the
difficulty of decrypting a hash.
 Cryptographic hashes are collision resistant. It is extremely unlikely to find two different input
values that yield the same hash value. This means every unique input will have a unique output.

10
Hashes
There are many different cryptographic hash algorithms. Two of the most common are:
o SHA-256, commonly used by Bitcoin
o Keccak-256, commonly used by Ethereum
Scenario: A common use case for a hash is a secure website storing a hash of your password in its database.
Let’s say your password for the website www.store.com is FNj`{:;`k#F43rQ\.
For extra protection the website’s database will store not the password, but a hash of the password. If the
website uses the hash function SHA-256, the resulting string stored in the database will be:

Then, when you log in, the website only needs to verify the entered password by comparing the hash of the
string you typed in to the hash stored in its database.
This process makes the website more secure because if a hacker breaks into the database, they’ll only get the
hashes of customer passwords.
11
Block Hashes
A block hash is a snapshot of what the entire blockchain looked like at
the moment that block was created. In accounting terms, it’s like a
balance sheet for the entire network.
Every node in the network refers to the block hash to verify that its
view of the network is the exact same as everyone else’s.
If there’s even one minor difference in a node’s ledger, its hash will
look significantly different.
This is what makes blockchain tamper-evident; if the content
experiences tampering or corruption, the resulting hash will no longer
be the same.
Why it would be hard to change a past transaction?
Why it’s difficult to roll back bitcoin transactions?

12
Transactions
 A transaction is basically a list of inputs and a list of outputs.
 Each input identifies an address that is acting as the source of funds, plus an unspent
transaction that address has received in the past.
 It also contains a digital signature proving that the owner of that address has authorized the
transaction.
 Each output identifies the Bitcoin address receiving the funds and the amount that address
will receive.
 Transactions are not encrypted and are publicly visible in the blockchain.
 Blocks are made up of transactions and these can be viewed using any online blockchain
explorer.

13
Transactions
 Transactions can be as simple as just sending some bitcoins to a bitcoin address, or it can be
quite complex depending on the requirements.
 Each transaction is composed of at least one input and output.
Inputs can be thought of as coins being spent that have been created in a previous transaction
and outputs as coins being created.
If a transaction is minting new coins, then there is no input and therefore no signature is
needed.
If a transaction is to send coins to some other user (a bitcoin address), then it needs to be
signed by the sender with their private key and a reference is also required to the previous
transaction in order to show the origin of the coins.
Coins are, in fact, unspent transaction outputs represented in Satoshis.
Bitcoin transactions follow a unique type of accounting called UTXO, which stands for Unspent
Transaction Output.

14
UTXO Model
Bitcoin transactions follow a unique type of accounting called Unspent Transaction Output
(UTXO). A bitcoin transaction is essentially a list of inputs and a list of outputs. Each input
identifies a Bitcoin address that is providing the funds, and an unspent transaction that address
has received in the past.
Similarly, each output represents the Bitcoin address receiving the funds and the amount that
address receives. The difference between the input and the output is the Transaction Fee, which
will be earned by the bitcoin miner.
Each input also contains a digital signature, proving that the owner of that Bitcoin address
authorizes that transaction.

15
Transaction Fees
Bitcoin transaction fees can vary depending on network capacity, how quickly confirmation is
needed, and other factors.
 The largest amount of data a blockchain block can hold is referred to as the Block Size Limit.
Because there is a limit on the number of transactions that can be recorded on a block—the
current limit is 1 MB of data, or roughly 3,500 transactions per block—a higher fee may be
required for greater urgency.
There is essentially a competition in place for getting miners to confirm a transaction: higher
fees mean faster confirmation.
 Transaction fees are charged by the miners. The fee charged is dependent upon the size and
weight of the transaction. Transaction fees are calculated by subtracting the sum of the inputs
and the sum of the outputs.
A simple formula can be used: fee = sum(inputs) - sum(outputs)

16
Transaction Pools
All transactions end up in the memory pool, from where miners pick up transactions based on
their priority to include them in the proposed block. From a transaction fee point of view, a
transaction with a higher fee will be picked up sooner by the miners.
There are different rules based on which fee is calculated for various types of actions, such as
sending transactions, inclusion in blocks, and relaying by nodes.
Fees are not fixed by the Bitcoin protocol and are not mandatory; even a transaction with no
fee will be processed in due course but may take a very long time.
The time for transaction confirmation usually ranges from 10 minutes to over 12 hours in
some cases. Transaction time is dependent on transaction fees and network activity.
Transaction pools (also known as Memory Pools), these pools are basically created in local
memory (computer RAM) by nodes in order to maintain a temporary list of transactions that
are not yet confirmed in a block.

17
Transaction Data Structure
A transaction at a high level contains metadata, inputs, and outputs.
Field Size Description
Version number 4 bytes Used to specify rules to be used by the miners and nodes for transaction
processing.
Input counter 1-9 bytes The number (positive integer) of inputs included in the transaction.
List of inputs Variable Each input is composed of several fields, including Previous Tx hash,
Previous Txout-index, Txin-script length, List of inputs Variable Txin-script,
and optional sequence number. The first transaction in a block is also called
a coinbase transaction. It specifies one or more transaction inputs.
Output counter 1-9 bytes A positive integer representing the number of outputs.
List of outputs Variable Outputs included in the transaction
Lock time 4 bytes This field defines the earliest time when a transaction becomes valid. It is
either a Unix timestamp or block height.

18
Anatomy of Bitcoin Transaction

19
Transaction: Input
and Output
Generally, each input spends a previous
output. Each output is considered as Unspent
Transaction Output (UTXO) until an input
consumes it.
UTXO is an unspent transaction output that
can be spent as an input to a new transaction.
Outputs have three fields, and they contain
instructions for sending bitcoins. The first field
contains the amount of Satoshis.
The second field contains the size of the
locking script.
The third field contains a locking script that
holds the conditions that need to be met in
order for the output to be spent
20
Types of Transactions
There are various scripts available in Bitcoin to handle the value transfer from the source to the
destination. The following are the standard transaction types:
1. Pay to Public Key Hash (P2PKH): P2PKH is the most commonly used transaction type and is
used to send transactions to the bitcoin addresses.
2. Pay to Script Hash (P2SH): P2SH is used in order to send transactions to a script hash (that is,
the addresses starting with 3) and was standardized in BIP16.
3. MultiSig (Pay to MultiSig): M-of-N MultiSig transaction script is a complex type of script
where it is possible to construct a script that required multiple signatures to be valid in order
to redeem a transaction.
4. Pay to Pubkey: This script is a very simple script that is commonly used in Coinbase
Transactions. It is now obsolete and was used in an old version of bitcoin.
5. Null data/OP_RETURN: This script is used to store arbitrary data on the blockchain for a fee.
The limit of the message is 40 bytes.

21
Merkle Root
The Merkle root is used to show a snapshot of the state of all the transactions in the current block, stored
in just 256 bits. The name comes from computer scientist Ralph Merkle, who came up with Merkle trees,
which are digital signature data structures.
The Merkle root has a special purpose aside from capturing the transaction snapshot. When a node in
the network wants to ensure it has the exact same list of transactions as every other node, it does not
need to compare each transaction individually.
Instead, it only needs to compare its Merkle root with every other node’s Merkle root. This allows for the
building of light software clients that do not require storing the entire blockchain to validate their own
transactions.
To calculate the Merkle root, you first create a Merkle tree, where the leaves are the transactions in the
current block.
By moving up the Merkle tree and generating hashes of all the leaves, you eventually reach the Merkle
root (yes, the Merkle tree is an upside-down tree). If the number of transactions is odd, then the last
transaction is replicated in order to continue this process. The Merkle root is an important value that
helps to generate the block hash
22
Structure of a Merkle
Root
A Merkle tree is a binary tree in which the
inputs are first placed at the leaves (node with
no children), and then the values of pairs of
child nodes are hashed together to produce a
value for the parent node (internal node) until
a single hash value known as Merkle root is
achieved.
HA is the transaction (tx) hash of the first
transaction, HB is the tx hash of the second
transaction, and so on.
HAB is the hash of HA + HB => HA+HB =
SHA256( SHA256 (HA + HB )).
The hash function for Bitcoin is double SHA-
256.

23
The important takeaway here is
that the Merkle root can be used
to quickly detect tampering in
blockchain nodes.
If there has been any tampering or
corruption of transactions in the
blockchain on any given node, its
Merkle root hash will no longer
match that of the other nodes.

24
The Coinbase Transaction
A coinbase transaction or generation transaction is always created by a miner and is the first transaction in a
block. It is used to create new coins.
It includes a special field, also called coinbase, which acts as an input to the coinbase transaction.
It holds two important values:
Block reward
This is the reward a miner receives from the network for performing the work to discover a block and doing
their part to provide processing power to the Bitcoin network. The reward comes in the form of new bitcoin
being added to the world supply.
Transaction fees
This is the sum of all the transaction fees that are included in each transaction that gets added to the current
block. There are often more transactions waiting to be processed than can fit into a block, generating a
marketplace for transaction fees. The faster the miner wants a transaction to be processed, the higher the fee.

25
Coinbase transactions
This transaction also allows up to 100 bytes of arbitrary data that can be used to store arbitrary data.
In the genesis block, this transaction included the most famous comment taken from The Times
newspaper: "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks."
This message is a proof that the genesis block was not mined earlier than January 3, 2009.
This is because first Bitcoin block (genesis block) was created on January 3, 2009 and this news excerpt
was taken from that day's newspaper.
A coinbase transaction input has the same number of fields as usual transaction input, but the structure
contains coinbase data size and coinbase data fields instead of unlocking script size and unlocking script
fields. Output Index and Sequence #, set to all zeros and set to 0xFFFFFFFF.

26
Bitcoin Address
A bitcoin address is created by taking the corresponding public key of a private key and hashing it
twice, first with the SHA-256 algorithm and then with RIPEMD-160. The resultant 160-bit hash is then
prefixed with a version number and finally encoded with a Base58Check encoding scheme. The
bitcoin addresses are 26-35 characters long and begin with digit 1 or 3.
This address can be shared with anybody for receiving and sending, a bit like a username or email
address. The private key is kept secret and is used to unlock stored cryptocurrency, somewhat like
how you use a password to access your bank account.
Bitcoin private keys are used to digitally sign transactions. That’s how the owner of a Bitcoin address
proves to the Bitcoin network that they are the rightful owner of that address, and how they
authorize a transaction.
Here’s an example
of what they look like:

27
Series of events involved in generating
keys, addresses, executing a bitcoin
transaction, where a block is mined
onto blockchain.

Transaction Malleability
Transaction malleability in Bitcoin was
introduced due to a bug in the bitcoin
implementation.
Due to this bug, it became possible for
an adversary to change the
transaction ID of a transaction, thus
resulting in a scenario where it would
appear that a certain transaction has
not been executed. This can allow
scenarios where double deposits or
withdrawals can occur

28
Transaction Security
Bitcoin transactions are push transactions, meaning that the sender—the one pushing the funds
out of an account they control—is the one to initiate the transaction. In contrast, a pull transaction
is initiated by the receiver.
Pull transactions are significantly less secure because they require the sender to share their
account details with the receiver. To compensate for this weakness, pull payment networks (like
Visa) provide chargebacks, or the ability to dispute a transaction and ask for a refund. As push
transactions, bitcoin transactions are significantly more secure.
When initiating a transaction, a sender never has to reveal any of their account information. The
only way a fraudulent transaction can take place is if an unauthorized person gets a copy of
someone’s private key.
With the technology available today, it is considered to be impossible to guess or reverse engineer
what someone’s private key is. The only way to guess a private key is through brute force—trying
every possible combination. A private key is a 256-bit number, which means there are potential
combinations to try: =

29
The combined total power of the Bitcoin network is greater than that of any of the world’s
supercomputers.
Currently the bitcoin hash rate—an estimate of how many hashes are being generated by all the
miners trying to solve any given block—is 90 exahashes per second. That works out to
something like pow(2,128)/ (90000000000000000000*3600*24*365)=119,892,034,120 per
year.
(The use of pow(2,128) is because ECDSA, the cryptographic algorithm used to generate a bit ‐
coin private key, can be cracked in proportion to the square root of the key size.)
So, if you harnessed the processing power of all the miners in the Bitcoin network, it would take
them this long to go through every combination: 4,589,678,828,851^37 years

30
Chain of
Transactions
The transactions form a
chain, where the inputs from
the latest transaction
correspond to outputs from
previous transactions.

31
Transaction’s Lifecycle
The following are the four main stages a transaction goes through:
1. Broadcast: The first step is generating a valid bitcoin transaction and then broadcasting the
transaction details to the Bitcoin network.
2. Unconfirmed/Mempool: As every miner in the network receives the transaction, it places that
transaction into its memory pool, or mempool. The mempool is a collection of all the bitcoin
transactions that are in an unconfirmed state and are still considered active. By default, if a
transaction has been sitting in the mem pool for more than two weeks, it is considered inactive and
is dropped from the mempool.
3. Confirmed by Miner: When a miner discovers a new block, the miner decides which transactions
to include in that block, choosing from transactions that are sitting in the mempool. Miners choose
transactions in order of transaction fees, starting with the highest ones. A transaction is considered
confirmed by a miner when that miner adds a block containing that transaction to its blockchain.
4. Confirmed by the Network: As a block is buried under newer blocks, the chances that the Bitcoin
network has achieved consensus to include that block increase.
32
Confirmations
Bitcoin wallets, and most people in the
industry, consider a transaction to be safely
confirmed by the network when that
transaction has reached at least six
confirmations.
A confirmation involves a miner adding a block
that contains transactions to the chain.
A transaction is considered to be confirmed by
the entire Bitcoin network when the network
has achieved consensus to include the
transaction’s block in the blockchain.

33
Consensus
Consensus is a way of reaching agreement between various participants who have shared values
and goals, and it is an important component of how blockchain networks succeed.
A blockchain is a living, constantly updating document. As time goes on, more and more
transactions are added to it. Users of a centralized payments network like Pay ‐ Pal trust that the
central authority will update its ledger with new transactions as time goes on. But in a decentralized
payments network like Bitcoin, there is no central authority—just thousands of anonymous miners
powering the network.
So, who should users trust to update Bitcoin’s blockchain with a new block of transactions?
Gaining that trust is called achieving consensus. It is a process that all the miners powering the
network use for the following two purposes:
Block discovery - To agree on which miner gets the right to add a block of transactions.
Validation of transactions - To agree that the transactions included in that new block are
legitimate.
[Consensus will be discussed more in-depth in Chapter 4]
34
35

You might also like