Week1 02 Blockchain Platforms
Week1 02 Blockchain Platforms
COMP6452
Software Architecture
for Blockchain
Applications
• In this session, we’ll discuss some of the fundamentals behind blockchains (BCs)
& DLTS. We do this while using several popular blockchain platforms as
examples.
1
Outline
Cryptography basics
Bitcoin
Ethereum
Hyperledger
2 |
• We’ll first start with a quick introduction to cryptography. This will provide us
with some background needed to discuss blockchains’ design and
implementation details.
• Then we’ll discuss 3 popular BC platforms. Under each platform we’ll revisit TXs,
blocks, and ledgers structures in more details.
• Bitcoin and Ethereum are public BCs, while Hyperledger Fabric is a private (aka
consortium) BC.
• This will be only an introduction. Some of the concepts like mining and
consensus will be discussed in detailed later.
2
Cryptography Basics
3
Blockchain
Replicated & distributed ledger – Linked list with hash pointers
• Collection of ordered TXs form a block’s body
• Summary of those TXs & hash of previous block forms a block’s header
• Collection of blocks form a blockchain
• Based on Public-Key Cryptography & Hashing Latest
Genesis Block
Block
H(Previous H(Previous H(Previous
block) block) block)
Transaction 1 Transaction 1
Transaction 2 Transaction 1 Transaction 2
Transaction 3 Transaction 2 Transaction 3
… …
4 |
• In the previous class, we talked about BCs goal, which is to replace the central
trusted authority with a network of computers such that we establish a
decentralised, trustless environment.
• In the latter part of the last lecture, we also discussed about challenges
associated in building a
• unreliable networks
4
• timing & ordering issues
4
Hashing
Converts a large volume of data into a
Input Value Hash
small datum value
Maps arbitrary-sized data to fixed-sized
data
Returned value is called hash value,
hash code, digest, or simply hash
Algorithms
• MD5, SHA, SHA-3, KECCAK
• 64, 128, 160, 224, 256, 384 & 512 bits
Source: https://en.wikipedia.org/wiki/
Cryptographic_hash_function 5 |
• Hashing is the process of converting a large amount of data into a small datum.
• In the top figure, given a document, we use a special function to derive a small
bit string that sort of becomes a “fingerprint” for the document.
• We chose this function such that it can capture even a minor change in the
document by producing a significantly different bit string from the original bit
string (see next figure).
• The function can take in arbitrary long input and produce a fix-sized output.
• Such a function is called a hash function, and the resulting bit string is called the
hash, hash value, hash code, or digest.
• Some of the popular hash functions are MD5 and variants of SHA like SHA2 and
SHA3.
• MD stands for message digest and SHA stands for Secure Hash Algorithm.
SHA-3 belongs to a family of algorithms called KECCAK.
5
like 64-bit, 128, 160, 224, 256 bits and so on.
• Many BCs use 256 hash values, e.g., Bitcoin use SHA-3-256 and Ethereum
uses KECCAK-256
5
Properties of Cryptographic Hash
Functions
Deterministic
• Same message always results in the same hash
A small change to a message change hash value so extensively that
old & new hash values appear uncorrelated
• Called “Consistent Hashing”
Quick to compute hash value for any message/document
One-way functions
• Infeasible to invert (i.e., generate a message from its hash value) except by trying
all possible messages
Infeasible to find 2 different messages with the same hash value
• If happens, it’s called a “Hash Collision”
6 |
• While there are many hash functions, hash functions used for cryptographic
purposes usually have the following desirable properties:
• The hash value should be deterministic where the same input always
results in the same hash.
6
message from its hash value is to try all possible messages.
6
Merkle Tree
H( ) H( )
A binary tree built
using hashes
H( ) H( ) H( ) H( ) Allow efficient &
secure verification of
contents of large data
H( ) H( ) H( ) H( ) H( ) H( ) H( ) H( )
structures
Can efficiently
H(Tx1) H(Tx2) H(Tx3) H(Tx4) H(Tx5) H(Tx6) H(Tx7) H(Tx8)
demonstrate a leaf
node is a part of a
given hash tree
7 |
7
root hash will change.
• Also, in BCs, we can verify whether a given TX is included in a block
using the Merkle Tree. Given a TX and hashes of its adjacent and
parent hashes, we can verify whether the same root hash is
produced in Θ(log n) time.
• For example, the block header will contain only the root hash,
To check whether TX4 is in the block, we need only to
compute H(TX4), concatenate it with H(TX3) and find the next
level hash. Then concatenate that hash with the hash from
the left of the tree (i.e., H(H(TX1) + H(TX2)) ) and calculate the
next level hash, and so on..,
• Such a proof is called a Merkle proof.
• In Bitcoin, if there are an odd number of TXs, the TX without a
partner is hashed with a copy of itself. Similarly, any hash without a
partner is hashed with itself.
7
Public-Key Cryptography
Is a cryptographic system that uses pairs of keys:
• Public key – May be disseminated widely
• Private key – Known only to the owner
Aka asymmetric cryptography
Effective security only requires keeping the private
key private
Easy to create new key pairs
• Algorithms – RSA, ECC
• 128, 256, 384, 512, 1024, 4096 bits
Used heavily in blockchain
• Losing your private key can mean loss of assets Source:
• If hackers can get your private key, they can steal your https://en.wikipedia.org/wiki/Public-
key_cryptography
assets
8 |
• The other key is called the Private key and is expected to be known only
to the owner.
• Because these cryptographic systems use a different key, they are also called
asymmetric cryptography.
• It's easy to create new key pairs using algorithms such as RSA and ECC (Elliptic
curve cryptography).
8
random number.
• Typical key lengths are 128, 256, 384, 512, 1024, 2048, and 4096 bits.
• With RSA we use key lengths over 2048 today. ECC keys are relatively
short as they are more effective in withstanding attacks. E.g., 384-bit ECC
has similar strength as 4096-bit RSA key.
• E.g., when Alice sends $300 to Bob, she needs to sign her TX using her
private key (as she’s the only one that is supposed to know the private
key).
• If Alice loses her private key, it same as her losing control of her assets.
8
Encryption & Digital Signatures
Source: https://en.wikipedia.org/wiki/Public-key_cryptography 9 |
• Encryption
• So, he encrypts the message using Alice's public key, which is well
known. Anyone who knows Alice’s public key can send her a secure
message.
• However, to decrypt the message Alice needs to use her private key. As
Alice is the only one who knows the private key, no one can read the
message other than Alice.
• So, use the public key to encrypt and the private key to decrypt.
• Digital signatures
9
• In this case, Alice wants to prove that she is the one who signed the
message, i.e., Alice is trying to prove her authenticity (much like putting
her signature on a paper).
• So, the message is signed with Alice's private key and Bob or anyone else
can verify it using her public key.
• No one else can impersonate Alice unless they know her private key
9
Public-Key Cryptography in Blockchain
Use private key to control an account Transfer 5 BTC
to Bob
Sign
• Control means the ability to act on behalf
of the account Transfer 5 BTC
Alice‘s private key
key Anyone
10 |
• Whereas each account is known based on its public key. Hence, anyone can use
her public key to verify that the TX was signed by Alice.
• Note that Alice's account is represented as a bit string like in the slide.
10
name (more on this later).
10
Transactions, Blocks, &
Ledger Structures
Now that we understand terms like hashing, Merkle Tree, and digital signatures, let’s
discuss the structure of a TX, block, and ledger.
11
Cryptocurrency
Digital currency baked into a blockchain & secured by
cryptography
• Accounting & validation rules are hard-coded in the platform’s base layer
by developers
• A platform typically has one base cryptocurrency
• aka native currency
Not centrally issued, e.g., Bitcoin & Ethereum
Can be mined or purchased from cryptocurrency exchanges
Usually only on public blockchains
Usually don’t represent other rights/assets
12 |
• Here’s a single slide introduction to cryptocurrencies which are digital assets (aka
crypto-assets).
• A cryptocurrency is baked into a blockchain & secured by cryptography.
• Accounting & validation rules are hard-coded in the platform’s base layer
(aka BC protocol) by developers.
• A platform typically has one base cryptocurrency. However, other forms of
currencies/tokens may exist for governance, staking, etc.
• aka native currency
• Cryptocurrencies are not centrally issued by a central party like a central/reserve
bank, e.g., Bitcoin & Ethereum.
• They can be mined by joining the network or purchased from cryptocurrency
exchanges.
• They usually exist only on public blockchains.
• In most cases, they usually don’t represent other rights/assets like a land title.
Coloured coins are an exception whereas a Bitcoin UTXO represents some other
asset.
12
1st Gen Blockchains — Cryptocurrency
Users:
• Create TXs,
• Sign them
Send 2 BTC from • Announce them to
my account to network
Bob. Miners:
Signed: Alice
• Receive TXs
• Include them in a
new block
• (Try to) append new
block to the chain of
blocks
• When a TX is part of
the data structure, it
has taken place
Exchanges:
• Users can trade
Bitcoin with fiat &
cryptocurrencies
Source: Andreas M. Antonopoulos, Mastering Bitcoin-Unlocking Digital Cryptocurrencies
13 |
• Users create TXs, sign them, & announce them to the Bitcoin network.
• Miners receive TXs, include them in a new block, and try to append a new block
to the chain of blocks. If successful, the block and its TXs are considered to have
taken place.
• Exchanges are used to trade Bitcoin with other fiat and cryptocurrencies.
13
Bitcoin
14 |
• Bitcoin is the 1st cryptocurrency built on the idea of a BC. Both the
BC network and currency are called Bitcoin and the symbol is BTC.
• It was proposed in a 2018 white paper by a pseudonym called
Satoshi Nakamoto.
• The white paper never used the word BC. But as Bitcoin
followed the linked-list structure forming a chain of blocks, BC
became a synonym for the data structure that links blocks. It
is also used to refer to the network of nodes too.
• Implementation appeared in Jan. 2009.
• On 3 January 2009, the Bitcoin network was created when
Nakamoto mined the starting block of the chain, known as the
genesis block.
• The software was published by Satoshi Nakamoto under the
14
name "Bitcoin", and later renamed to "Bitcoin Core" to
distinguish it from the network, It' it’s also known as the
Satoshi client.
14
Source: https://bitnodes.io
15 |
• As per the statistics collected a few min ago, there are ~19K nodes in the
network. This number can vary a bit and is typically above 15K.
• The map shows the concentration of nodes where location can be estimated.
So, for a large portion of nodes, we don’t know the location.
• It also shows the nodes with IPv4 and IPv6 addresses and the ones that are
behind a VPN (virtual private network)
• You can get a bit more details on the live map by visiting the bitnodes.io site.
15
Accounts & States
ID Asset
Alice 500
An account is associated with a cryptographic key pair
Bob 1000
• Public key – Used to create the address of an account
Charlie 500
• Private key – Sign TXs sent from the account
Dave Plot 123 @ 2015
State of the blockchain Sweet Bowen QUE,
• Account balances of all users Mango Org. Cert #
• Result from the genesis block (very 1st block) & set of TXs 45781
included since
• Some accounts can be pre-loaded with an initial account balance at the
genesis
As TXs are grouped into blocks, when a new block is
added the entire system moves from one discrete state
to another
16 |
• Let's discuss a few definitions which can be generalised to other BCs too.
• The private key is used to sign TXs sent from the account (i.e.,
authenticate TXs).
• The state of the BC is the account balance of all users. This is also referred to as
the Global state or World state.
• Account balance is the sum of BTCs (named UTXOs, discussed in the next
slides) that an account has control over. Only only Alice, Bob, and
Charlie’s balances but also other state maintained by the network
(keeping track of other data is more complicated, and will be discussed
later).
16
• Each UTXO is bound to the owner’s public key
• The state captures results from the genesis block (the very 1st block) &
set of TXs included since.
• As TXs are grouped into blocks, the entire system moves from one discrete state
to another through the creation of a new block.
16
Transactions
Transfer currency from source addresses
to destination addresses
Contains 1+ inputs & 1+ outputs
• If sum of outputs is less than sum of inputs,
the difference is a fee to the miner
• TX fee is an incentive for miners to contribute
computing power & storage
Contains proof of ownership for each
input, in the form of a digital signature of
owner
TX output is bind to owner’s public key
17 |
• Bitcoin TXs have an interesting structure and are used to transfer Bitcoins from
source addresses to destination addresses, like from one bank account to
another.
• A TX contains one or more inputs & one or more outputs. E.g., TX0 has 1 input
and 2 outputs.
• You can think of this as paying a merchant using multiple notes and coins
and getting the balance in the same way.
• Each of those outputs can be spent later in another TX. Think of the balance you
get from the merchant, which can be spent somewhere else.
• The difference between input & output values is taken as the TX fee by the
miner. E.g., here 100,000 Satoshis 100K comes in. A Satoshi is like a cent. The
total value of outputs is 90K. The remaining 10K is the TX fee.
• Satoshi is the smallest unit of BTC and it's one 100 millionth of a single
Bitcoin (eight decimal places as 0.00000001 BTC). Millibitcoin (mBTC) is
17
1⁄1000 of a bitcoin.
• These TX fees are the incentive for miners to contribute computing power,
storage, and bandwidth.
17
Transaction Format
Linked TXs
• Outputs of TXs become inputs of a new TX
Bitcoin addresses don’t contain “coins”
balance
• Different to a bank account
• Store Unspent Transaction Outputs (UTXO)
Balance of an address/account
• Sum of values of all of UTXOs associated with
the address
State of the blockchain
• All the UTXOs in system
18 |
• Bitcoin TXs are linked where the output of one or more TXs becomes inputs to a
new TX.
• Different inputs and outputs of the same TX are identified based on their
index number.
• Instead, these unspent outputs are called UTXO (Unspent TX Output). UTXOs
are more like notes and coins in your wallet.
18
• Actually, a UTXO is bound to the hash of the public key known as the
address (an address also has a version number and a checksum to detect
errors when specifying an address).
• So if you want to find how many Bitcoins you own, you need to sum all
UTXOs associated with your address. Following the notes and coins
analogy in your wallet, if you want to know how much money you have,
you need to count them.
• A TX has a few other attributes like version number, locktime, and script which
are not shown. See
https://developer.bitcoin.org/reference/transactions.html for details
18
Blocks
A container of TXs
Identified by block hash
Linked to previous block
Includes a timestamp
• Not very accurate
Include a nonce
• Proof of ability to produce the block
Use a Merkle tree to capture
ordered list of TXs
Max block size is 4 MB
Max TX size is 100K bytes Source: https://blog.scottlogic.com/2016/04/04/ jenny-from-the-
blockchain.html
19 |
19
hours into the future.
• A block also includes a nonce (a random number), which is used as
proof of the ability to produce the block
• It can be considered a magic number that miners need to find
to prove that they successfully build a block (more on this
discussed later).
• Then the ordered list of TXs is summarised using a Merkle tree
• The block header is 80 bytes in length.
• The Max block size in Bitcoin is now 4 MB, early days it was 1 MB.
• 4 MB comes from 4 million weight units, which is a
measurement used to compare the size of different TXs to
each other in proportion to the consensus-enforced maximum
block size limit. It won’t be typically reached unless all TXs are
formatted to minimise what’s included in the block. The
typical max is 2.3MB if all TXs are segwit TXs. See
https://en.bitcoin.it/wiki/Weight_units
• There’s quite a lot of disagreement about whether the block
size should be changed. Based on different block sizes, there
are even sister BCs that were formed out of Bitcoin.
• E.g., supporters of large blocks who were dissatisfied with the
activation of SegWit forked the software on 1 August 2017 to
create Bitcoin Cash, becoming one of many forks of Bitcoin
such as Bitcoin Gold.
• Max TX size is 100,000 bytes. While a typical TX is ~500 bytes
19
Mining – Creating a New Block
Receiving a new block
• End of one round is the beginning of next round
• Validate new block
• Remove TXs of newly announced block from TX pool
Aggregation
• Aggregate subset of the remaining valid TXs
• Add coinbase TX as the 1st TX of the next block
• Mining is the process of creating a new block. This diagram shows the overall
mining process.
• Once a new valid block is built and propagated to other nodes in the network,
miners start building the next block while using the new block's hash as the
previous hash.
• Aggregation
• First, they remove TXs of the newly announced block from a pool of
pending TXs (aka transaction pool) as those are already in the block that
was just announced.
• Then add Coinbase TX as the 1st TX to the TX list for the next block.
20
(used as an input) for at least 100 blocks. This is related to the
forking discussed in the next slide.
• Header construction
• Next, build the header by including the hash of the previous block and
Merkle Root to summarize all the included TXs.
• Solve puzzle
• Propagation
20
Right to Build a Block n-bit nonce
m-bit
block data
21 |
• Let's discuss a bit about the mining process in Bitcoin.
• Once a set of pending TXs is included in a block, miners need to solve a puzzle
to build a block. Once they solve the puzzle only, they can
announce/propagate/broadcast the new block into the network.
• The miner who solves this puzzle 1st is considered to have the right to build the
block.
• Bitcoin miners solve a puzzle called hash cash to build a valid block.
Ethereum 1.0 used an algorithm called Ethash (Ethereum 2.0 doesn’t
use PoW).
21
no shortcuts to finding the answer.
• On one side you have the data that reflect the content of the block
header like Merkle root, previous hash, and timestamp.
• On the other side, you have the nonce, a large random number.
• You then check whether the resulting hash value satisfies a certain
properly. E.g., For here we check whether the hash has 4 zeros as the
prefix. Such a hash is called a valid hash, and the associate block is called
a valid block.
• If not, you must retry again while changing the nonce. You can't change
the m-bit data, as it reflects the content of the block. If you want to do
that you have to do more work than just trying another nonce.
• There's no easy way to guess what nonce would work. So, you
must try nonce values until you get lucky.
• While it's difficult to find what nonce work, easy to validate whether a given
nonce satisfies the condition.
• Just concatenate the claimed nonce with the block header, calculate the
21
hash, and check whether the hash value satisfies the acceptance
threshold.
• The difficulty of the problem is automatically adjusted with time to overcome
increasing computing power to maintain average inter-block time.
• The actual mechanism may slightly change from the given figure, e.g., instead of
changing nonce on the block, miners may add a random value to coinbase TX to
keep changing the block header until block difficulty is achieved. See
https://en.bitcoin.it/wiki/Mining
21
Who can Build a Block?
Multiple miners might find & announce next blocks at the same time
Tie breaker – Treat the longest history of blocks as the main chain
• One that received most computation
• Referred to as Nakamoto Consensus Orphan block
Block Block
n+1 n+1
Block )))))))))฿ Block )))))))))฿ Confirmation blocks
... n ... n
))))))฿ Block ))))))฿ Block Block Block
n+1 n+1 n+2 n+3
)))฿ )))฿ ))))))฿ )))))))))฿
22 |
• By finding a valid hash miners gain the right to build the next block. However,
multiple miners can solve the problem simultaneously
• This is quite possible in a global network like Bitcoin where it could take
a few seconds for a newly generated block to propagate to a large
fraction of the network.
• Such blocks will not be identical in content at least because the coinbase
TX is bound to the miner’s address.
• Nakamoto proposed to treat the longest history of blocks and claimed it as the
main chain.
• The longest chain is also the one that received the most computation
(more blocks behind it means more computation spent to solve the PoW
puzzle).
• In the figure, block n has 2 successors. Miners still allow subsequent blocks to
22
be built while using these successors as the previous block.
• Eventually, one of the forked chains will be longer than the others. In this
example, the bottom chain will be accepted as the longer chain.
• The top n+1 block is dropped from the chain of blocks, and all its TXs go back to
the TX pool to be included in a block in the future. Such a block is called an
orphan block.
• Therefore, creating a block doesn't mean the block is finalized. Even the
block reward is not guaranteed.
• When forking is occasional, Nakamoto (see Bitcoin whitepaper) showed that the
probability that a block is no longer in the longest chain rapidly reduces to zero
as more blocks are built along the same chain of blocks.
• Therefore, in Bitcoin and several other BCs, after a TX gets included in a block,
we wait for more blocks to be formed along the same chain.
• Each such block built after the block containing a TX of interest is called
a confirmation block.
• As multiple blocks may have the same block height (i.e., no of blocks in the
longest chain since the Genesis block), it’s not unique. Hence, block hash is the
unique identifier of a block.
22
Nakamoto Consensus
To determine with high probability that a TX is permanently
included:
• Wait for several blocks (6 blocks by default) to be added after 1st inclusion
of the TX in a block
• Each of these subsequent blocks is called a “confirmation block”
• Once sufficiently many confirmations occurred after the TX inclusion in
the block then TX is considered committed/finalised
Unlike many traditional TX commit semantics:
• Commit only has a probabilistic guarantee
• A longer chain could appear – although it may be very, very unlikely
23 |
• Let's talk a bit about the Nakamoto Consensus.
• To determine with a high probability that a block and its TXs are permanently
included (i.e., in the longest chain), we need to wait for several new blocks to be
added after the 1st inclusion of the TX in a block.
• This way, we can give enough time for any forks to get resolved. Again, here we
are not talking about physical time, but the need to wait for enough new blocks.
23
• Commit only has a probabilistic guarantee as there is a non-zero
probability (though extremely small) that a block may not be in the
longest chain event after 10 or 12 blocks.
• In general, most BCs don’t fully comply with ACID (Atomicity, Consistency,
Isolation, Durable) properties associated with centralised databases.
23
Transactions Lifecycle
Tx
Tx in pool Tx in block(s)
committed
all blocks containing Tx
part of shorter chain
superseded
Tx dropped Tx outdated
24 |
24
• However, if the BC forks and the block with the TX isn’t included in
the longest (or heaviest) chain, the block is discarded and all the TXs
in the block are sent back to the TX pool.
• While waiting in the pool, a TX may get dropped too. This could
happen when the pending list of TXs is too long or the TX waited in
the pool for a long time without being included in a block.
• These parameters are BC platform-specific and under the
discretion of the miner.
• Typically, miners prefer to include TXs willing to pay a high TX
fee. Hence, when the TX pool is too full, minors 1st drop TXs
with a low TX fee. Also, miners may even define a minimum
TX fee to be included in the TX pool.
• A dropped TXs may be resubmitted with the same or a higher TX
fee.
• While a TX is pending in the pool, another TX can be submitted with
a higher TX fee and (same nonce in Ethereum) to replace the
existing TX. In that case, the original TX becomes outdated.
• However, this may not work always as altruistic miners may
retain a TX in the pool regardless of its TX fee.
• In practice, a few other complex scenarios could determine the
lifecycle of a TX. However, this figure is abstract enough for the
content of this class.
24
Mining Reward
25 |
• Miners who build valid blocks get rewarded for their effort in 2 ways:
• Block reward – New cryptocurrencies generated as part of block generation
are assigned to the miner (included through coinbase TX).
• TX fees – Fees paid by users to get their TXs included in the block, e.g., the
difference between inputs and outputs in a Bitcoin TX.
• On the left, we have a screenshot of a mining reward from a Bitcoin block. It also
shows the block difficulty and miner “SlushPool”.
• This shows that SlushPool received 6.25 BTC as the block reward and
0.2449… BTC as TX fee.
• On the right we have such information from Ethereum:
• At the top, we have a list of TXs that were included in the block.
• Miner is Nanopool
• While there are both a block reward and TXs fees, a bit of Ether is
destroyed as well.
• The London upgrade included Ethereum Improvement Proposal
("EIP") 1559, a mechanism for reducing transaction fee volatility.
The mechanism causes a portion of the Ether paid in transaction
fees for each block to be destroyed rather than given to the miner,
reducing the inflation rate of Ether and potentially resulting in
25
periods of deflation.
25
Mining Reward (Cont.)
Miners who build valid blocks get rewarded for their effort in 2 ways
1. Block reward
• With each block, new cryptocurrency is generated & assigned to the miner
• Bitcoin – Block reward is added as a special TX into the block, called “coinbase
TX”
• 6.25 BTC since May 2020, 12.5 BTC reward in 2016, 50 BTC initially
• Reward halved every 210,000 blocks
• In Ethereum, block reward is credited to miner’s address
• 2 ETH since block# 7,280,000, 3 ETH between 4,370,000 & 7,279,999, 5 ETH initially
2. TX fees
• Miners can collect fees from TXs they include in the block
• Higher TX fee à Higher chance of TX getting included in a block
26 |
• Miners who build valid blocks get rewarded for their effort in 2 ways:
1. Block reward
• With each block built, new cryptocurrencies are generated and assigned
to the miner.
• Bitcoin
• Reward halved every 210,000 blocks, e.g., 6.25 BTC since May
2020, 12.5 BTC reward in 2016, and 50 BTC initially.
• Eventually, the reward will round down to zero, and the limit of
21 million bitcoins will be reached approximately in 2140. The
miner’s effort will then be rewarded by TX fees only.
• Ethereum (we are jumping the Ethereum discussion a bit here just for
26
the same of a comparison)
1. TX fees
• Miners can collect fees from TXs they include in the block.
• Hence, they are incentivised to prioritise TXs that pay higher fees.
• TX fees can be complex and how they are charged changes across BC
platforms, e.g.,
26
Question
Which of the following statement(s) is True?
✓ A. As the number of ledger copies increases, it becomes
difficult to maintain the consistency of the ledger.
X B. Given a hash value, we can derive the corresponding
message.
X C. An attacker can fabricate a TX using Alice’s public key to
show that Alice is engaged in illicit TXs (e.g., money
laundering).
X D. As soon as a Bitcoin TX is included in a block, it is safe to
assume the TX is final (e.g., Charlie can ship a bicycle to
Alice).
27 |
A. As discussed in 1st class, keeping many ledger copies synchronised is very hard
B. A good hash function is irreversible
C. Digital signatures use the private key, which must be protected
D. A TX that got included in a block may not be in the longest/heaviest chain of
blocks after a while
27
Ethereum
2nd generation Ethereum 1.0 (Eth1) Ethereum 2.0 (Eth2)
blockchain that Consensus Proof of Work (PoW) Proof of Stake (PoS)
can execute
Inter-block time Average 13-15 sec Regular 12 sec
programs called
Smart Contracts Confirmation 12 Max 64
• Ledger that can blocks
store/transact any Ledger Replicated Replicated
kind of data
Performance Low High*
Native currency is Power consumption High Low
Ether (ETH)
* Not yet in operation
28 |
• As Ethereum fully supports smart contracts (SCs), it's considered a 2nd
generation BC.
• SCs are small programs that execute on a DLT. More on SCs in the next
class.
• With the introduction of SCs, the Ethereum ledger enabled the storage
and transaction of any kind of data.
28
• ETH balance denominated in Wei (1018 Wei = 1 Ether)
• Today, we have 2nd version of Ethereum that has a few notable differences from
its initial version:
• Consensus
• The original PoW algorithm was called Ethash. The key difference
between Ethash and Bitcoin’s Hashcash is that it's also memory
intensive making it difficult to solve the puzzle with hardware
optimisations such as ASICs (Application-Specific Integrated
Circuits). GPUs (Graphics Processing Units) were used in
Ethereum.
• Initial inter-block time was 13-15 sec. Now blocks are generally more
regular at every 12 seconds.
• Ethereum 2.0 is still undergoing transition. E.g., while the current ledger
is fully replicated, Ethereum plans to decentralise the ledger using a
technique called sharding.
• Sharding will split the ledger into different segments and each
segment will have a relatively smaller number of replicas. This
enables parallel execution of TXs.
28
• Due to sharding and the slight reduction in inter-block size, Ethereum 2.0
is expected to handle a much higher number of TXs. Hence, throughout
(one of the performance metrics that count the number of TXs
processed within a unit of time) is expected to be 2 orders of
magnitudes higher than current performance.
• As PoS doesn’t waste power to solve a puzzle like in PoW, the power
consumption of Ethereum 2.0 is several orders of magnitude lower than
what it used to be before Sep 2022. It is estimated that the power
consumption is dropped by ~99.95%.
28
Accounts & Transactions
Uses account-balance model
An account is bound to owner’s public key
A TX is uniquely identified by its hash
TXs are sequenced using a nonce
Once included in a block, block no, actual
gas used, actual fee, etc., are available
• While Bitcoin uses UTXO, Ethereum uses the typical account-balance model
where the ledger maintains the balance of each account.
• TXs from the same account are ordered by a sequence number called the
nonce. This nonce doesn’t have anything to do with the nonce in a block used
by PoW protocols.
• As we saw with Etherscan a TX has a hash, from & to address, nonce, value or
data, and a TX fee.
• In Ethereum, TX fee has 2 parts, a gas limit and a gas price. We’ll soon discuss
gas…
• Gas price - is the amount of Ether the TX owner is willing to pay for a
29
unit of gas
• Once a TX is included in a block, block no, actual gas used, actual fee, etc., are
available.
29
Transaction Fees
Sender decides TX fee to offer
• Can be 0. Minimum on some platforms
Miners prefer TXs with higher fees
• Altruistic mining Source: Ingo Weber et al., “On availability for blockchain-based
systems”, 2017 IEEE 36th Symposium on Reliable Distributed
Higher TX fees à Fast inclusion in a block Systems (SRDS).
TX fees are dynamic
• No of pending TXs
• Cryptocurrency price
• TX urgency of users
• Errors & attacks
30 |
• E.g., the CDF shows that Ethereum TXs with high fees are included faster,
while TXs with low fees can be significantly delayed. Even for the same TX
fee, some TXs may experience substantial delays.
• TX fees are dynamic and depend on many factors:
• No of pending TXs – As the block size is limited (in terms of the block size
or gas limit) when too many TXs are pending to be included in a block we
have a high demand and limited supply scenario. Similar to other goods in a
market, the TX fee goes up as the TX senders with high urgency will start
30
offering higher prices.
• Cryptocurrency price – Typically when the price increases concerning say
USD, TXs fee goes down as miners’ effort to maintain their computing
infrastructure is reflected in USD. However, when the cryptocurrency price
goes up, there can also be an increase in no of TXs increase the no of
pending TXs in the pool.
• TX urgency – Users’ urgency depends on the use case, this is common
during initial coin offerings (ICOs), gambling, and some NFT (non-fungible
token) sales.
• Errors are not uncommon where someone may accidentally put TX value as
TX fee. Or an attacker may pay someone’s currency as a TXs fee to any
miner who’s able to build that block.
• Due to multiple such factors, TXs fees can vary widely.
• We could use services such as Etherscan Gas Tracker (14 Gwei | Ethereum Gas
Tracker | Etherscan) or ETH Gas Station (no longer operational) to get an estimate
on a suitable TX fee depending on how soon we want the TX to be included in a
block
• Wallets like Metamask and Ethereum node can also give you an estimate.
• These services use various algorithms ranging from calculating
mean/median value and moving averages to machine learning.
30
Gas in Ethereum
A unit of accounting for calculating TX fees
• Based on computational complexity & storage needed to execute a certain
operation/instruction
• A fee to limit resource usage
𝑇𝑋 𝑓𝑒𝑒 = 𝐺𝑎𝑠 𝑙𝑖𝑚𝑖𝑡 × 𝐺𝑎𝑠 𝑝𝑟𝑖𝑐𝑒
Gas limit
• A fixed gas cost per TX (base cost of 21,000 gas)
• Plus, a variable gas cost for data (dependent on size) & execution of a SC method
(charged per bytecode instruction)
• Additional gas cost for deployment of new contracts
Gas price
• Reflects how much Ether-per-gas the TX sender is willing to pay
• Clients need set the gas price to market price & their urgency
• Use gas price recommendations from sources like
https://etherscan.io/gastracker
31 |
31
• The offered TX fee consists of 2 parts, a gas limit and a gas price.
• This is like the fuel cost of going on a trip. Depending on the
efficiency of your car, you will need a certain number of litres
of fuel. Then there is the fuel price. So, the total fuel cost is =
no litters of fuel x price per litre.
• Gas limit:
• There is a fixed gas cost (aka base cost) for a basic
cryptocurrency TX and currently set to 21,000 units.
• If your TX includes data or executes an SC method, then you
must pay additional gas in proportion to the size of the data
or the complexity of bytecode instructions that are executed
by the SC.
• Further, additional gas cost needs to be paid for the
deployment of a new contract.
• This is also useful for the SC user, particularly to guard against
errors such as infinite loops where bad code could quickly
exhaust our Ether.
• However, if we set a low limit, we run the risk of exceeding
the gas limit before our code finishes execution. If so, TX is
reverted, but you lose the TX fee.
• Anyway, we don’t want to set a very high limit as Ethereum
nodes don’t also like excessively gas-consuming TXs due to
the risk of denial service (DoS) attacks.
• We can estimate how much gas an SC-related TX may
consume. However, in practice, we set a slightly higher gas
limit to accommodate any unforeseen changes in the size of
data or code behaviour.
• Gas price
31
• Is the fee we are willing to pay for a single unit of gas? This
should be specified in ET.
• Gas prices are typically denominated in Gwei, a subunit
of ETH equal to 10−9 ETH.
• While this at the user’s discretion, if you offer to pay a much
lower value compared to the market price, your TX could get
delayed or even get dropped. Alternatively, you could increase
the chance of including TX faster by offering to pay a higher
gas price.
• Gas price is dynamic and reflects the number of pending TXs.
Some recommendations are available, e.g., from
https://etherscan.io/gastracker
• Set higher gas prices if the inclusion of TX is urgent
• Set lower gas prices if TX inclusion is not (time) critical
31
Gas Limit
TX gas limit
• Maximum gas TX sender is willing to pay
• Gas used <= gas limit
• Else, TX will fail & state changes are reverted. Sender is charged up to the gas
limit
Block gas limit
• Sum of gas used by TXs included in a block can’t exceed this limit
• Limits complexity for a new block
• Set by the miners
• Max block size is ~30 million gas
• Max 1,428 TXs/block (Bitcoin 1,500 TXs/block)
• Most blocks under a few KB (Bitcoin 1 MB)
• An upper bound on TX throughput
• Nontrivial to understand how the bound relates to TX throughput
32 |
32
variations the user has power over, e.g., underbidding the market price.
• The current max block size is around 30,000,000 gas. About 18 months ago
it was 10M, which increased to 30M in a couple of steps.
• In a 30M gas block, we can include up to 1,428 TXs in a block (min of
21,000 gas per TX). Whereas in Bitcoin we can include ~1,500 TXs/block.
• Because larger blocks mean more TXs per block, it increases the number of
TXs processed within a unit time, aka TX throughput.
• While a higher block gas limit helps to increase the TX throughput, it
is nontrivial to understand how the bound relates to throughput
(more on this in a later class).
32
Block Format
Maintain state of all accounts
• Aka World/global state
• Include account balances, data stored, &
smart contracts
List of TXs
List of TX receipts (i.e., effects of
TXs)
Ethereum uses 3 Merkle trees
(known as Trie), one each for
integrity of:
• World State
• TXs
• TX Receipts
Source: https://ethereum.stackexchange.com/
Questions/268/ethereum-block-architecture 33 |
• A block keeps track of the entire state known as the world or global state. The
world state consists of:
• All account-balances
• SC code
• As we can see from the diagram, an Ethereum block is more complicated than
Bitcoin because it also keeps track of TXs and outputs/results of TXs.
• The results of a TX is called the TXs receipt and they reflect changes in
the ledger state due to the TX.
• Each of these data is captured in a block as the root of the respective Merkel
tree.
33
Ethereum Protocol
Ethereum’s inter-block time > block propagation time around the
globe
• Bitcoin inter-block time >> block propagation time around the globe
• Small block size helps to propagate faster
Ethereum 1.0 with PoW
• Multiple competing blocks were more likely
• GHOST (Greedy Heaviest Observed Subtree) protocol linked orphaned
blocks (called uncles) to increase the weight of chain
• Uncle block miners received 87.5% of a standard block reward
Ethereum 2.0 with PoS
• Only 1 validator is pseudo-randomly chosen to propose a block
34 |
• 12-15 sec inter-block time is only marginally higher than the time to
propagate the newly generated block to a large fraction of the
network.
• Small blocks help Ethereum to achieve short inter-block time,
as they can be propagated to other BC nodes much faster.
• Whereas Bitcoin has a 10 min inter-block time giving ample
time for a block to propagate.
• In Ethereum 1.0, due to the lower inter-block time, it was quite likely
that multiple competing valid blocks were created simultaneously
while another miner’s newly created block continued to propagate
the network.
• To overcome this problem Ethereum 1.0 used a protocol
called GHOST (Greedy Heaviest Observed Subtree).
34
• GHOST is a bit complicated. The main idea was to link up
orphaned blocks (in Ethereum’s terminology they are called
uncle blocks) with successor blocks to make a computationally
heavier chain. Following Nakamoto consensus heaviest chain
eventually wins (not the longest chain of blocks).
34
Ethereum 2.0 Clients & Nodes
Client – Implementation of Ethereum that
verifies data against protocol rules
Node – Instance of Ethereum client
software that is connected to others
Ethereum 1.0
• Single client for TX execution & consensus
Ethereum 2.0
• Execution client (aka Execution Engine, EL client, Source: ethereum.org
or Eth1 client) – Execute TXs & hold world state
• Consensus client (aka Beacon Node, CL client, or
Eth2 client) – Implements PoS consensus
35 |
35
Question
Mark True or False for each the following statements about a Bitcoin & Ethereum
True False
36 |
• 2nd question is wrong as how states are managed across the 2 BCs are reversed,
i.e., Ethereum uses an account-balance model while Bitcoin uses UTXO.
• 3rd question arise from 1st question. Due to non-zero probability a TX that got
included in a block may eventually belong to an orphan block. The TXs on that
block then go to the TX pool. While waiting in the TX pool, some of those TXs
can get dropped, particularly if they have been waiting in the pool for a very
long time. See Slide 24.
• This is quite unlikely in Ethereum 2.0 due to its PoS design (as there will
not be any uncle blocks). However, under a major attack on the
blockchain, this may be possible, but quite unlikely.
36
calculation based on the numbers in Slide 32.
36
Hyperledger
An umbrella project of a set of open-source blockchains &
related tools
• Global collaboration, hosted by Linux Foundation since Dec. 2015
Hyperledger Fabric is an enterprise blockchain framework
• Private & permissioned blockchain
• Modular architecture, e.g., can change consensus algorithm
• Smart contracts are called “Chaincode” – Go, node.js, or Java
No concept of TX fee
Can achieve much higher TX throughput with low latency
• 1,000+ public TXs or 500+ private TXs per second
37 |
37
• No concept of the TX fee.
37
Hyperledger Fabric – Transactions & Blocks
Source: https://hyperledger-
fabric.readthedocs.io/en/rel
ease-2.0/ledger/ledger.html
38 |
• As the 2nd figure shows, the ledger includes both the world state and blocks
including their TXs.
• Here keys are like account numbers and values are the state/balance.
Being a 2nd generation BC, HLF can store any form of data are value.
• If you are familiar with key-value stored in NoSQL databases HLF is not
very different. HLF uses LevelDB or CouchDB as the underlying data store
for the ledger. The key-value store in HLF is more visible compared to
38
that in Ethereum which uses the same ideas though it’s not directly
visible to developers.
38
Hyperledger Fabric Transaction Lifecycle
39 |
• The nodes responsible for endorsement will check the authenticity of the TX,
whether it is valid given the ledger state, and then put its digital signature as an
endorsement of the TX.
• Once the TX sender collects enough endorsement as per the set endorsement
policy, TX is assumed to be in the “created” state.
39
endorsed by any 2 of Organisation B, C, and D”
• Once a TX reaches the orderer, it validates the endorsement based on the pre-
defined policy.
• Then the block is broadcasted to all the nodes in the network to update their
ledger.
• However, instead of updating the ledger, the state of the ledger before
and after executing the TX is recorded as a read-write set (rw-set). The
read set includes the ledger state before executing the TX and the write
set includes the state after executing the TX.
2. The endorsed TX is sent to a special node called the orderer that orders TXs into
a block.
• It then builds a block by packing multiple valid TXs into it. This essentially
forms a global order among concurrent TXs.
• The orderer then broadcasts the block to all nodes in the network.
39
• Orderer is stateless and just orders TXs as it wishes.
• If it’s invalid or the ledger state is changed between the collection of the
read-write set and validation, it’s dropped.
• This could happen due to concurrent TXs, where the state read by one
TX may have been updated by another TX that got finalised. If so, the
client needs to resubmit the TX.
• In practice, rw-set conflicts are possible but rare, and you can
design your application to minimise such scenarios.
• This executes à order à Validate model is different from the approach taken
by BCs like Bitcoin and Ethereum where they follow Execute à Validate à
Order TXs model. All these steps happen as part of the block-building process.
39
Hyperledger Fabric Network
Membership Service Provider (MSP)
• Users & nodes must enrol with MSP & have
known identities
• MSP is trusted
• Public keys as cryptographic certificates tied to
organisations, network components, & users
Channels
• Subnetworks – Allow a group of members to
create a private ledger
• Built for scenarios where business
confidentiality are important à Reduced
transparency is acceptable
Private Data Collections (PDC)
• Hide data in a TX from other channel members
40 |
• As seen from the figure, Hyperledger Fabric (HLF) network has a set of
components and nodes. The figure is taken from the textbook.
• All participants have known identities, and they need to enrol with the
MSP.
• MSP is trusted by all the participants – Here we see that the trust
assumption is relaxed compared to public BCs.
40
• MSP is just a service, not a physical node.
• Channels
• Channels are like subnetworks within the main network, that allow a
group of members to create a private ledger.
• Let’s talk about clients, orderer, and endorsers in the next slide
40
Hyperledger Fabric Node Types
Client
• Connects to peers to communicate with
blockchain on behalf of users
• Send TXs & observe updates
• Can connect to (multiple) channels, but is
unaware of other existing channels
Peer
• Receives ordered TXs from orderer,
Orderer
commits TXs, & maintains ledger state • Validates & orders TXs into a block,
• Can play a special role like endorser then broadcasts it to the network
• TXs invoking chaincode needs to be • Provides a communication channel
endorsed before being committed
• TXs must satisfy channel & chaincode- between clients & peers
specify endorsement policies • Single orderer service prevents
multiple competing blocks
41 |
41
• Peers are the nodes of the BC network.
• A peer maintains the ledger state and commits TXs ordered by the
orderer.
• Before endorsing a TX, the endorser will first validate the TX,
execute it, and create a read-write set.
• As the name implies, the Orderer orders TXs within the Hyperledger network
creating a global order of TXs
• As the orderer is the only one that builds a block, it’s not possible to
have multiple competing blocks like Bitcoin and Ethereum.
41
Question
Which of the following statement(s) is True?
X A. As Hyperledger uses a Membership Service Provider (MSP),
it’s not required to sign TXs
X B. Consensus algorithm in Hyperledger Fabric is based on PoW
✓ C. Both Ethereum & Hyperledger Fabric maintain World State as
a set of accounts & balances
✓ D. Finality (i.e., time to confirm a TX) in Hyperledger Fabric is
immediate
42 |
42