Bitcoin Mechanics - How bitcoin works

Diving Deep into Bitcoin – How Bitcoin Works

Now that we have learned how bitcoin achieves decentralization, let us in this article take a deep look at how bitcoin works and the internal mechanics that dive it.

How Bitcoin Works: Transactions in Bitcoin

Transaction in bitcoin plays an important role on how bitcoin works. Bitcoin uses transaction based ledger as opposed to the account based ledger. Below is an example of the transaction-based ledger:

Tx ledger - how bitcoin works

In the above diagram, at the time of genesis (transaction 1), we see that the 25 Bitcoins are credited to Amanda. Amanda then pays to Bryan 17 bitcoins where all coins that Amanda had were consumed in one input resulting in paying 17 bitcoins to Bryan and 8 back to Amanda in transaction 2. Again in transaction 3, Bryan wants to pay Calvin for a service and thus all of his coins are consumed completely and 8 coins are paid to Calvin and rest are paid to Bryan.

Let’s say in the 4th transaction Amanda again wants to pay David 6 coins, so in order to make sure Amanda has sufficient coins to pay and not double spending coins, we can do a backward finite scan to check whether Amanda has sufficient coins to pay David. And thus we see that Amanda has 8 coins which get totally consumed in the 4th transaction paying out David with 6 coins and Amanda with 2 coins. In transaction-based ledger output of the previous transaction is consumed completely in the input for another transaction. And thus this is how a transaction based ledger works.

NB: The input 2[1] means the 1st output of transaction 2 goes into the input. Again 2[0] means the 0th output of transaction 2 goes into the input and so on. And even though each block contains multiple transactions, one transaction per block is shown for simplification.

Merging Payments

In transaction-based ledger, payments can be merged into a single payout. For example:

merge payments - how bitcoin works

Continuing our last example, we can see that in transaction 3, Amanda pays 4 coins to Calvin and in transaction 4, Calvin pays 2 coins to Bryan. So, if Bryan wishes he can combine the two transactions 2 and 4 where he was paid 17 and 2 coins respectively and pay to himself to a new address in transaction 5.

Joint Payments

Joint payments are also possible in bitcoin transactions where two different individuals who want to pay the same third person can do so in one transaction. Let us have a look at an example:

joint payments - how bitcoin works

In the above scenario, we can see that both Bryan and Amanda wants to pay Calvin and thus the same can be done in one transaction instead of two transactions. Both Amanda and Bryan’s coins are consumed completely in the input of transaction 3 resulting in paying a total of 6 coins to Calvin and 4, 15 coins to Amanda and Bryan respectively.



How Bitcoin Works: Bitcoin Scripts

The scripting language used in bitcoin is inspired by a language called Forth. The scripts are simple and compact and executed exactly once. They also have support for cryptography. It is a stack-based language and thus there are no loops in the language. Every instruction is executed only once. This language is also not a Turing complete language as miners run them which is submitted by arbitrary participants in the network. The input and output address hashes in any transaction are nothing but individual scripts. This input and output scripts are combined and concatenated and the whole script should run without error for a transaction to be successful.

The input scripts are called ScriptSig as they contain signature along with the public key. The output scripts, on the other hand, are called ScriptPubKey as they contain the public key address of the output.

Example of a Typical Bitcoin Script

btc Script - how bitcoin works

<sig><pubkey> This part of the script is specified by the recipient in the ScriptSig/ Input script.

OP_DUP OP_HASH160 <pubkeyHash> OP_EQUALVERIFY OP_CHECKSIG This part is specified by the sender of the coin in output script.

The sender of the coins specifies the public key of the recipient and the recipient to redeem the coins then specifies the signature using the specified public key.

With that said, we have learned that both the input and output scripts should be combined and run and there should be no error, and as this is a stack-based language, the two commands used to push and pop for every element. So, with that in mind let us have a look how this script is executed.

script in action - how bitcoin works

  1. The element<sig> is pushed in the stack.
  2. <pubKey> is next pushed into the stack.
  3. OP_DUP says that take the top value of stack and pop it out, duplicate it and then push it. Thus we have another <pubKey>.
  4. OP_HASH160 function says that compute the cryptographic hash of the top value of stack.
  5. Next, the <pubKeyHash> is pushed into the stack which is specified by the sender of the coin, the public key which is used to generate the signature to redeem coins.
  6. OP_EQUALVERIFY checks whether the public key specified by sender and recipient are same or not. We assume that they were same.
  7. OP_CHECKSIG checks for the valid signature. The input to this function is the whole transaction.

The outcome of the scripts is either valid or invalid. If invalid, the transaction is not included in the blockchain.

General Bitcoin Scripts in Practice

  • Simple signature checks - The above eg
  • MULTISIG – used for joint payments/ merge payments.
  • Pay to Script hash
  • Errors, Proof of burn

The functionOP_CHECKMULTISIG is used to check for multiple signatures generally in join payments where we specify a threshold t and n number of public keys. The verification requires t number of signatures. This function has a bug from the time of genesis, which is, it pops out an extra amount of data from the stack resulting in a loss in information. So, to prevent this, miners put some garbage value in the stack such that while pop out, the function does not damage any critical information.

The functionOP_RETURN is used to write some data into the blockchain forever which costs a few coins to be destroyed. These coins can never be redeemed. This is called proof of burn. This is used to write an arbitrary value like name, timestamp or any other important information to the blockchain.

How Bitcoin Works: Application of Bitcoin Scripts

There are several applications available for bitcoin scripts. Some of the major ones are:

1.      Escrow Transaction

Let us assume that we have a scenario where Amanda wants to buy some stuff from Calvin. However, neither Amanda wants to pay before the goods arrive nor Calvin wants to ship the goods before the money arrives. In this kind of cases comes on the scene the role of escrows. Escrows are a third-party organization that is responsible for taking the amount Amanda wants to pay to Calvin and pay to Calvin only after the goods are shipped and settling disputes if any.

A <--->   ESCROW <---> C

Say in the normal case, Amanda pays to Calvin however, if there is a case where Amanda received the goods and still do not want to pay then the escrow can settle the dispute by paying the desired amount to Calvin and vice versa.

2.      Green Address

A --> BANK --> C

Assume we have a situation where Amanda wants to pay Calvin for a service but Amanda is in a Faraday cage that is without the internet connection. In this case, Amanda can tell her bank to pay Calvin on behalf of her from her account. Now, the bank can send the money to Calvin by showing the proof that they have never done double spend using the particular address, thus convincing him to trust the bank. However, this system has a huge disadvantage and it is the bank. Here Calvin has to trust the bank and if the bank misbehaves then the whole trust in the system will collapse.

3.      Micro-Payments

Consider we have a situation where Amanda wants to pay Calvin for drinking water service. However, the problem is that she needs several litres of water to drink every day and paying for every litre every day becomes a problem as she would incur a huge transaction cost. In those scenarios where one person wants to pay low amounts of money to avail any service, micropayments might be the suitable solution.

In micropayments, the first and last transactions enter the blockchain only. So, at first, Amanda along with Calvin signs a transaction of a total of 100 BTC opening a new channel between them. Say that Amanda, every day consumes on an average 4 litres of water whose price is 0.5 BTC/ litre and pays Calvin weekly. Thus for every litre, she pays 0.5 BTC which is a total of 14 BTC per week. At the end of the week, the transaction channel is closed and the final value i.e. 86 coins -> A and 14 coins -> C is published in the blockchain.

Now, the situation might get worse if after Amanda demands closing the channel, Calvin denies signing the last transaction. Thus Amanda cannot get back the rest of her coins which will remain idle in the network. In this scenarios, the parameter LockedTIME comes into play. This locked time states that a transaction cannot be published until a certain block number or time has reached. So, once that threshold reaches, automatically the channel gets closed and last known transaction is published in the blockchain returning the rest of the coins to Amanda and paying Calvin as of the published transaction.

Although the system might look alluring there is one disadvantage to it, which is, it is hard to prevent double spend attack as all the transactions do not make to the blockchain.

Bitcoin Blocks- Why is it Necessary

Bitcoins blocks allow grouping several transactions thus creating a single unit of work for the miners. It is also used to make the hash chain of blocks shorter which can be beneficial in verifying the history of the blockchain data structure.

A High-Level View of Block Data Structure

Block DS - How bitcoin works

Here we can see that the headers of the blocks contain the hash of the next block. The transactions in the blocks are also stored as hash pointers in form of Merkle tree.

Hope you liked the article and learned something. If you did, comment below and do not forget to share.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.