[Part-3] Blockchain Simplified Notes NPTEL

Divya Jain
MoatFund
Published in
9 min readAug 15, 2018

--

Bitcoin Introduction.

Bitcoin is a decentralized digital currency that enables instant payments to anyone, anywhere in the world.

Bitcoin uses peer-to-peer technology to operate with no central authority.

Main Operations:

  • Transaction Management: Transfer bitcoins from a user to another across the world.
  • Money Issuance: No central authority regulates monetary base.

Creation of Coins:

  • Controlled Supply: In this currency is created by the nodes of a peer-to-peer network. The Bitcoin generation algorithm defines, in advance, how currency will be created and at what rate. Any currency that is generated by a malicious user that does not follow the rules will be rejected by the network and thus is worthless.
  • New bitcoins are generated during mining when a user discovers a new block.
  • Roughly every 4 years, the number of bitcoins that can be “mined” in a block reduces by 50%. Originally the block reward was 50 bitcoins; it halved in November 2012; it then halved again in July 2016.
  • In the end, no more than 21 million bitcoins will ever exist.
  • Miners will get less reward for mining bitcoin as the time progresses. Hence, the transaction fees will increase to encourage the miners to complete transaction quickly.

Transactions:

  • Bitcoin uses public key cryptography to verify the digital signature.
  • Every user can have one or more wallet addresses but every address will always have a pair of public and private keys.

Example of transaction: Suppose Alice wants to send some bitcoins to Bob. Then, the following will take place:

  • Bob sends his address to Alice.
  • Alice adds Bob’s address and the amount of bitcoins to transfer to a message: a ‘transaction’ message.
  • Alice signs the transaction with her private key, and announces her public key for signature verification.
  • Alice broadcasts the transaction on the Bitcoin network for all to see.

The first are done manually by humans. Last are taken care by Bitcoin client software itself.

Handling Double Spending:

When the same money is spent more than once, it is called Double Spending. For example, Alice has 50 bitcoins and she sent 50 bitcoins to Bob and 50 bitcoins to Eve simultaneously. Then, only one of the transaction will be successful because double spending is not allowed in Bitcoin.

Bitcoin prevents double spending by following ways:

  • Details about the transaction are sent and forwarded to all or as many other computers as possible.
  • A constantly growing chain of blocks that contains a record of all transactions is collectively maintained by all computers (each has a full copy).
  • To be accepted in the chain, transaction blocks must be valid and must include proof of work (one block generated by the network every 10 minutes).
  • Blocks are chained in a way so that, if any one is modified, all following blocks will have to be recomputed.
  • When multiple valid continuations to this chain appear, only the longest such branch is accepted and it is then extended further.

Anonymity:

  • There are no specific usernames, emails, passwords to hold bitcoins.
  • Each balance is simply associated with an address and its public-private key pair.
  • Transacting parties do not need to know each other’s identity in the same way that a store owner does not know a cash-paying customer’s name.
  • A Bitcoin address mathematically corresponds to a public key and looks like this: 1PHYrmdJ22MKbJevpb3MBNpVckjZHt89hz.
  • A single person can have multiple addresses making it difficult to know what amount of bitcoins the person holds.

Bitcoin Script.

Bitcoin Script is a programming language to validate bitcoin transactions. A script is essentially a list of instructions recorded with each transaction that describe how the next person wanting to spend the Bitcoins being transferred can gain access to them.

Bitcoin script is FORTH like language which is processed left to right and is based on stack. It is not turing complete, i.e. doesn’t support loops.

FORTH language:

Example of Postfix Notation:

Suppose there is a mathematical expression: (25 * 10 + 50). In Postfix notation, this will be written as 25 10 * 50 + CR.

Now, we will solve this using stack. Remember following rules:

  • Insert operands on the top of stack.
  • Take two operands out of the stack when you encounter a operator and put the result in the stack.
  • Repeat above until CR is left.

Let’s solve expression 25 10 * 50 + CR. :

  • Push 25, 10 in the stack.
  • Multiply(*) is encountered. Pop 10 and 25. Push the result back in the stack.
  • Push 50 in the stack.
  • Addition operator(+) is encountered. Pop 50 and 250. Perform 50+250 and push the result back in the stack.
  • CR moves the output to the new line and . prints the output to user.

Sample Code of FORTH:

FLOOR5 ( n -- n' )   DUP 6 < IF DROP 5 ELSE 1 - THEN ;

Here, FLOOR5 is a subroutine. DUP duplicates the number on the stack. DROP discards the value on the stack.

The above can be written in C as:

int floor5(int v) {
return (v < 6) ? 5 : (v - 1); //Conditional Operator
}

Implementation of Bitcoin Script.

A transaction is characterised by two components:

  • Output of transaction: Sender send’s bitcoins.
  • Input of transaction: Receiver receives bitcoins.

Traditionally, Alice sends her public key and signature along with the transaction to Bob. Then, Bob verifies the origin of transaction.

Now, Bitcoin transfers script (scriptSig, ScriptPubKey)instead of public key and signature to Bob. Bob can spend coins only if both the scripts return true after execution.

What is the benefit of Scripting?

Scripting provides the flexibility to change the parameters of what’s needed to spend transferred Bitcoins. For example, the scripting system could be used to require two private keys, or a combination of several keys, or even no keys at all.

A transaction is valid if nothing in the combined script triggers failure and the top stack item is True (non-zero) when the script exits.

Only the intended user will be able to spend bitcoin because only receiver’s digital signature will return true on solving the script.

Example of Bitcoin Script:

Suppose Alice sends some bitcoins to Bob. She sends the output script ScriptPubKey to Bob.

Courtesy: Cryptcompare

We have,

scriptSig = <Bob’s signature> <Bob’s public key>

scriptPubKey = OP_DUP OP_HASH160 <Bob’s public address> OP_EQUALVERIFY OP_CHECKSIG

Both the scripts will be combined such that input is followed by the output. The code will look like:

<sig> <pubKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

Let’s solve this using Reverse Polish Notation:

  • Initially the stack is empty. Push <sig> and <pubKey> operands.
  • OP_DUP is an operator that duplicates element at the top of stack.
  • OP_HASH160 operator hashes the input twice: first with SHA-256 and then with RIPEMD-160. So, <pubKey> will be hased and <pubHash> will be obtained.
  • <pubKeyHash> operand will be pushed now. The stack will become:
  • OP_EQUALVERIFY operator will pop out <pubKeyHash> and <pubHash> . It will compare both and if the value is equal, transaction continues otherwise the operation is stopped and the script is failed.
  • OP_CHECKSIG operator will pop out <pubKey> and <sig> and checks to see their validity to know that they are valid signatures and public addresses.

This is the most common type of Bitcoin transaction called as P2PKH aka The pay to public key hash.

Read some interesting Scripts and more operations here.

Bitcoin Peer to Peer Network.

  • It is an ad-hoc network with random topology, Bitcoin protocol runs on TCP port 8333.
  • All nodes (users) in the bitcoin network are treated equally.
  • New nodes can join any time, non-responding nodes are removed after 3 hours.

How to join this P2P network?

Suppose you want to join an existing Bitcoin P2P network, then you will follow the following steps:

  • You send the request message to join the network. There are certain nodes in the network known as seed nodes which provide the initial information to the new node.
  • You send a message to seed node to provide the peer addresses.
  • In response, seed node sends a set of addresses to consider as peers.
  • Among this set, you select some random addresses and make them as peers by making virtual links with them.
  • You ask the peers to send the most recent information of blockchain. After you receive the information, you compare it and keep the copy which is transferred by most number of peers(>50%).
  • Now, you can start the transaction in the network.

Transactions in Bitcoin Network.

Representation of transaction:

When Alice sends 10 bitcoins to Bob, it will be represented as:

A->B: BTC 10

Mechanism:

  • Alice sends 10 bitcoins(along with scripts) to Bob after getting the most recent information.
  • This transaction will be broadcast to all the peers.
  • They will validate the transaction using script. If it is valid, they will broadcast this to neighbouring peers.
  • This process will continue and each node will receive copy of transaction.
  • If a node gets the same transaction information from more than one neighbouring peer, it will keep the copy first received and discard all copies.
  • If there is more than one transaction happening in the network, then the transactions are stored in order they are received by the node. So, different nodes may have different transaction pools.

Mining Mechanism:

  • Miners are certain nodes in the network that have great computational power. Not all the nodes in network are miners.
  • Miners collect all the transactions flooded in the network and start mining.
  • The miner who solves the puzzle first generates a new block in the network.
  • That new block will get flooded in the network.
  • It may be possible that multiple miners mine same new block for a transaction or different blocks for different transactions simultaneously.
  • As seen earlier, only the longest block chain will be accepted and other nodes will be considered as orphaned. The orphaned block chain is called fork.

Reliable Transactions:

  • There should be no conflict between two transactions.
  • User must not be able to double spend the bitcoins.
  • The script matches with a pre-given set of whitelist scripts — avoid unusual
    scripts, avoid infinite loops.

51% Rule:

The copy of the block received from more than 51% of the neighbouring blocks is accepted and is broadcasted further in the network. All the other copies can be discarded.

Block Propagation Latency:

Mean time = 12.6 seconds.

95% of the nodes can see the block within 40 seconds.

Conclusion.

Bitcoin is an interesting and important part of studying blockchain. Thanks to Satoshi Nakamota!

You can always dig deep about bitcoin from the official website: https://en.bitcoin.it. Now you can start your assignment-2 for the course.

In the next article, we will study about distributed consensus. In case you are willing to revise the topic, here are the links to previous parts:

I am always open to discussions so shoot your thoughts about this topic in the comment section below.

This is a series of notes based on the Blockchain Course by NPTEL, which serves as a primer for understanding the blockchain fundamentals.

About MoatFund.

We are continuously assisting people get educated about how rapidly this technology is advancing and revolutionising our world. Our mission is to create a decentralised fund managing protocol operated on smart contracts, which is publicly accessible and can control the entire network of fund managing capabilities on blockchain. Here’s Everything You Need to Know About MoatFund, All In One Place.

--

--

Divya Jain
MoatFund

Jainism Influencer. Writer. Nature cure student. Blogger @ jaindivya.com