Bitcoin Transactions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

Blockchain

Why Blockchain?

• Disruptive technology - change traditional methods. Ex- horse carts replaced by


cars and buses since less e ort and more speed. Email replaced traditional mails
• Just like Internet changed the way communication happened, Blockchain changed
trust ecosystem.
• What is trust? Can we trust a brand that sells expensive co ee and claims to import
co ee from some foreign country? Can we trust an NGO whether the donated
money is actually used for the needy?
ff
ff
ff
What is Blockchain?
• The idea originated from a research paper written by Stuart Haber and W Scott Stornetta.
• Blockchain is a decentralized distributed immutable ledger which is completely
transparent and veri able.
fi
• What if a noti cation is sent on your mobile when the shopkeeper alters the record?
fi
• Blockchain replaces ledger book with a block. Blocks represent transactions.
• Once some data is fed into a block or a transaction is written into a block, it cannot
be altered or tempered with - immutable.
• The system is decentralized - no central server or agency is there for monitoring.
All the machines are considered as peers.
• The ledger is distributed over many systems connected in a network.
• Once a transactional block is added to the blockchain on one computer, all the other
computers connected to the network and part of the blockchain, start showing the newly
added block.
• Any changes made by A are visible to all others. Identical ledger is stored with all peers -
transparency.
Applications
Product Tracking
• Track the origin of the product and the entire route of the product supply chain.
• Ex- A supermarket in Denmark has implemented blockchain. Every product has a
barcode which on scanning gives the complete details about the product.
• This prevents selling of fake products.
Smart Contract
International Wire Transfer
Healthcare Systems
Smart Contract
• It is a program written in a programming language which runs on ethereum
blockchain.
• Ex- Let A delivers fruits to a shopkeeper B. For the fruits to be kept fresh, a
temperature is maintained in the container carried by A. On delivery of the fruits, B
pays money to A. However, the fruits delivered to B may be stale or B can deny to
pay money to A. Both parties may face losses when there is lack of trust.
• Once the program is fed into a block of the blockchain, it becomes immutable and
will be executed.
• No courts or a third centralized party is needed to resolve cases.
International Wire Transfer
• Transferring money from India to some other country, ex - USA, requires this process.
• Go to sender bank and deposit money. Sender bank doesn’t directly transfer to receiver
bank. Instead it transfers to a correspondent bank for veri cation and paper work. The
money is then transferred to the receiver bank.
• This leads to huge processing fees (10-30%) and is a slow process.
• Blockchain requires 1-2% fees and is very fast.

fi
Healthcare Systems
• A person X residing in city A moves to city B. However, his medical history was
maintained with City A hospital.
• If X has an emergency in city B, his admission may be delayed due to several
checkups.
• If a blockchain for X is maintained, A may update the blockchain and store X’s
history in terms of blocks.
• B can access X’s blockchain for the medical history.
What’s inside a Block?

• A block consists of several elds - Block No. , Data, Nonce, Timestamp, Prev Hash,
Hash etc.
• Block No. Indicates which block it is in the blockchain.
fi
Wh t’s inside Block?

• Data represents the actual transactional data for which the block was added into the
blockchain.
• Each block in the blockchain is represented by a Hashcode (unique Fingerprint) which
is computed on the entire block contents - Block No, Nonce, Data, Timestamp and Prev
Hash.
• Prev Hash is the Hashcode of the previous block in the blockchain and Hash is the
Hashcode of the current block computed on all the content including Block No., Data,
Nonce, Timestamp and Prev Hash.
• Nonce - Number used only once. Block No., Prev Hash, Timestamp and Transaction data
cannot be altered. In case some other targeted hash value is needed, nonce value can be
changed.
• Timestamp - a record that pinpoints when a speci c transaction or event occurred,
often detailing the exact date and time
a
a
fi
• Each block points to the previous block using prev hash just like the prev pointer in
double linked list.
• The Prev Hash eld of the rst block is empty and it is usually called Genesis Block.
fi
fi
SHA256
• Secure Hash Algorithm is used for generating Hashcode comprising of 256 bits in
Blockchain.
• Takes a message as input, encrypt it and generate a 64 digit hex number or hash.
• Each hex digit is 4 bits hence total 64*4 = 256 bits.
Five requirements of hashing algorithm
• One way - Input data is provided to the algorithm to generated the encrypted
message. However, the encrypted message can’t be decrypted to get back the
original data.
• Deterministic - Same input data will generate the same output no matter how
many no. of times the input is provided to the algorithm.
• Fast Computation - The algorithm should be fast otherwise much time will be
required for encryption.
• Withstand collisions - Given a hashing function H, no arbitrary x and x' can be
found where H(x)=H(x'). In words, no two x's can be found where the hashing
function would create a collision.
• Avalanche E ect - A little change in the input data leads to signi cant change in
the encrypted output.
ff
fi
Mining
• The process of generating a hash value in a target area.
• The generated hashcode is then assigned to a block and the block is then added to the blockchain.
• Di erent nonce values may lead to di erent hash codes. Mining involves trying di erent values of nonce to
generated a targeted Hashcode, since other entries can’t be modi ed.
• Mining requires high computation power and is time consuming. People who perform mining are called
miners.
• There is no direct or predictable relationship between nonce value and generated Hashcodes.
• Visit https://demoblockchain.org/blockchain

FFFF……….FFFF

Target (‘0000’)

Generate a hash value


Below this target number
0000………0000
ff
ff
fi
ff
Reward
Mempool - where transactions
reside temporarily

Find Nonce value to


Proof of Work (PoW)
generate target hash
• 4 Billion possible values of nonce, hence 4 billion di erent values of hash can be generated.
• There are not enough nonce to generate the valid hash. It may be possible that the valid
hash is not obtained.
ff
• To solve this issue, Timestamp was introduced in the block. It represents unix
time - number of seconds that have elapsed since 00:00:00 Coordinated Universal
Time (UTC), Thursday, 1 January 1970.
• The 1 January 1970 00:00:00 UTC was selected arbitrarily by Unix engineers because
it was considered a convenient date to work with.
• In 40 seconds, 4 billion nonce values are tested. In 1 sec - 0.1 billion nonce values will be tested.
• After 1 second, the value of timestamp will be changed and starting nonce from beginning will
generate a new set of hashes. So nonce are not exhausted.
• However, Current hash rate is more than 650 million trillion hashes/second. (blockchain.com ->
charts -> total hash rate). So all nonce will be exhausted in <<<<<<1 sec. What should miners do
in idle time till timestamp is not changed?
Centralized Network
• In a centralized network, there is a central server where all the data resides. The
client machines request the server for user speci c data.
• Ex - Banks, Social Media etc.
Disadvantage
• This system can be easily hacked since all the data resides on a single machine. This
compromises data privacy.
• If the server is down, the entire network will be down.

fi
Distributed P2P Network
• All the machines are considered as peers. The data is distributed among all peers.
• The peers can request each other for data. Data copies reside on multiple peers.
• Even if a system is hacked, the data will be available on other peers.
Distributed P2P Network in Blockchain
• All the machines have their own copy of the blockchain.
• If a block is mined let’s say by miner A, it will be added to A’s blockchain.
• Once the block is inserted, all the miners connected to A - B, E, F, are informed and their
corresponding blockchains are updated after veri cation of the block.

fi
• B,E and F further inform their neighbor nodes about the newly added block and this
continues until the block is added in the entire network.
Immutable Ledger

• Let’s say we have a blockchain with six blocks - A,B,C,D,E and F. Suppose a hacker
attacks and tempers block C.
• This tempering leads to corruption of all the blocks ahead of C since the block D
contains the Prev Hash which is the Hashcode value of C. If C is tempered, its
Hashcode is also signi cantly changed which leads to changes in D.
• Now, since D’s contents are changed, its Hashcode is also changed signi cantly and
this continues. Hence, the user get to know it has been hacked.
fi
fi
• However, the blockchain is not stored on a single node or machine. It’s local copy is
distributed over all the nodes connected to the network.
• After xed interval of times, the blockchain is veri ed for consistency on all the
nodes. If the blockchain is corrupted on one of the nodes, it will be recovered to its
original state since the majority says otherwise (Majority Voting).
• Hence, in order to actually corrupt a blockchain, the hacker must corrupt on at least
51% of the machines, which is impossible to do so within the stipulated time in
which the blockchain is veri ed again.
fi
fi
fi
Byzantine Generals Problem
• There are four generals belonging to the same army, attacking the enemy kingdom
from di erent directions.
• All four have to decide when they should attack.
• They communicate with each other and inform each other on whether they should
attack or not.
• If any of them is a traitor, he may deny to attack and the army may lose.
• Same problem is encountered in distributed systems where there is no centralized
system to guide whether the information provided by a node is correct or not.
• Solution : Byzantine Fault Tolerance - If less than or equal to 1/3 nodes are
corrupted or say otherwise, accept what is said by 2/3 of the population.
ff
Consensus Protocol
• Used to prevent attacks
• Used for solving competing chain problem
• Mainly two types - Proof of Work (POW) and Proof of Stake (POS)
• POW is used in Bitcoin. Whenever a miner solves the mathematical problem of
nding a target hash, the block is added to the blockchain after veri cation from all
the nodes.
• This is called POW and the miner is then rewarded with bitcoins.
• Let’s say a miner A adds a malicious block in the end of the blockchain. Then there
are no blocks ahead of it that will be corrupted.
• However, this doesn’t practically happen since a lot of computational power is
required for mining and if a corrupt block is identi ed then the miners lose their
reward.
• Also, before adding the block to the blockchain, all the other nodes verify the
validity of the block by running an algorithm.
fi
fi
fi
Competing Chain Problem
• Let two miners A and C mine di erent blocks at the same time. Now both will
transfer this information to the rest of the network.

ff
• Since E and F are closer to A and B is closer to C, the information reaches these nodes earlier.
• F and E validate the A’s block and B validate the C’s block and add to their blockchain.
• There now exists a con ict since di erent sets of blockchain exist on di erent set of nodes.

fl
ff
ff
• Consensus protocol says that only the blockchain having maximum nodes will be
considered valid. Others will be discarded.
• All the nodes now wait for the next block to be mined and added. Whichever miner is
successful in doing so, it informs its subnetwork of the same and that subnetwork
becomes more powerful.
• The other subnetworks then discard their added block and accept the longest blockchain.
• The discarded blocks are called orphan blocks.
• The consensus protocol of blockchain is much better than the byzantine fault
tolerance as it only needs a 51% majority while BFT needs approximately 66%.
• All the transaction in the orphan blocks will be dropped and the miner that had
mined the block will not get any reward.
• So that’s why wait for 5 additional blocks to be added before assuming payment to
be successful.
Bitcoin
• Cryptocurrency based on Blockchain technology
• Founded by Satoshi Nakamoto in 2008
• There is a central authority for every country that determines how much amount of
money will be oated in the system so that there is a balance between supply and
demand
• Two principles given by Satoshi Nakamoto: The Halving and Block Frequency
• The Halving: After every four years or after every 210000 block is added, whichever is
earlier, the new bitcoins introduced in the system in terms of rewards to miners will be
halved. Ultimately, it will reach 0.
• Block Frequency: This states that on an average, it will take 10 minutes to create a
new block. Visit https://www.blockchain.com/explorer
fl
The H lving

• This process is expected till 2140 because there is a limit on the supply of Bitcoin - 21 million.
This means a maximum of 21 million bitcoins can be produced. This makes the bitcoin
currency rare - like gold, which can’t be made at home. All this is automatically handled by
bitcoin protocol.
• If supply of money is increased too much, the value of currency is decreased.
• After 2140, miners will be getting the transaction fee from users since the users will be too
much by then.
a
Mining Pool
Big budget miners
Small budget miners
With GPUs
With CPUs

• Small budget miners are at a disadvantage if there are big budget miners with expensie technologies
since big budget miners have more hashing power and therefore can reach the target hash earlier.
• Small budget miners therefore create mining pools to combine their hashing power
and share pro ts as per their hashing power. Then they can compete with big
miners.
• Becoming a part of a large mining pool may provide more hashing power but may
lead to less rewards.
fi
MemPool
• Area of memory which consists of thousands of uncon rmed transactions. Transactions are temporarily
stored there before they are assigned to a block.
• A transaction fee is associated with each transaction that are given to miners if they succeed in generating
target hash.
• Let’s say four transactions are allowed to be placed in a block.

fi
• What do miners do when all nonce are exhausted and timestamp has not been
changed?
• Miners replace the least fee transaction with another one from the Mempool. This
changes hash signi cantly because of Avalanche e ect.
• Then all values of nonce are tried again to generate target hash.
fi
ff
• At last, if still none are exhausted but target hash is not reached, the timestamp is
updated and the nonce generation starts again.
• Now the higher fee transaction can again be checked.
• Low fee transactions may never be picked up and will be removed after 72 hours.
How does Mempool works?

• Mempool is not a centralized area. It is associated with all the nodes and miners.
Nodes are systems that are not mining but only performing transactions. Miners

Nodes
Other Systems are informed of the
Transaction done by this Node
Let this miner mines a particular
block using the last two transactions
and inform its neighbors.
After veri cation, all the other
Systems also add the block to their
Blockchain.
fi
Transaction
• For transfer of cryptocurrency, wallet is created. For ex- Google Pay is used for transfer of
digital money.
• Unspent Transaction Output (UTXO) refers to a transaction output that can be used as input
in a new transaction.
• In Banking Sector, all the transaction amounts are added and stored. When spent, the amount
is subtracted from the stored value.
• However, in cryptocurrency, the ledgers are immutable. Hence the data cannot be modi ed. So
nd a transaction where amount is greater than or equal to the amount to be spent.

Wallet
fi
fi
• Everything is recorded as new transactions in the Blockchain and re ected in the
wallet.
• If some money is spent by an NGO, we would be able to see where that money go.

fl

You might also like