Introduction to Bitcoin for developers

Curious software engineer
Search for a command to run...

Curious software engineer
No comments yet. Be the first to comment.
In this series, I will treat subjects related to software development, software engineering, and related career.
This article is a mash-up of different sources cited in references and Pixelmatic guidelines aimed to serve as coding and style guides for proto files in the context of gRPC services definitions. Coding and style guides In short, the philosophy of th...
Itβs the summer holidays, after all. So I wanted to spent some time with my 7 years old daughter to teach her programming. β¨οΈ I spent some time before discussing with her some of the basics concept verbally: the different parts of a computer, and bas...

This article is a follow up on my previous one introducing the concept of SE Spectrum: https://sonny.alvesdi.as/a-better-way-to-discuss Existing solutions For long, the easiest way to run a spectrum was to use visual drawing or diagram tools like ...

Introduction to SE Spectrum

One of the greatest tool available on GitHub is Dependabot. For those not familiar with it, in short it's a bot reviewing your list of dependencies and proposing PRs to update them. https://docs.github.com/en/code-security/getting-started/dependabot-...

Photo by Harli Marten on Unsplash As long as I remember I have always been interested by skepticism. And I think one element that really resonated with me has been that conference talk by Phil Plait: https://www.youtube.com/watch?v=FrFRbGjUtJk 15 ...

If you are going to work on software using Bitcoin or Liquid, there are several concepts you need to be familiar with. This article aims at developers and is based on a presentation I held for the Pixelmatic team.
Disclaimer: this is an introduction and it does not cover everything obviously. And I simplified some explanations on purpose.
Bitcoin comes in the form of a daemon, a background process. Which is composed essentially of these components:
We will go through these concepts and I will add some other important notions on top of these subjects.
In essence, the Bitcoin blockchain is a linked list where a node has a link to its parent.
Bitcoin blockchain structure
Source: Wikipedia
A node, or called block too, contains also information:
A nonce which is manipulated in the step of mining
There is a bit more information in the Bitcoin blocks, but these are the essentials.
Before looking at the consensus protocol, I think it's important to understand the transaction model used in Bitcoin and the concept of UTXO.
Example of transactions
Source: Medium
The consensus protocol is made for securing the funds on Bitcoin, and validate what is the source of truth.
In order to do that, Bitcoin nodes are part of a peer-to-peer network and the rights of appending a block in the blockchain are somehow randomized.
The consensus happens on two steps:
When signaling a transaction to the network
The direct peers of the bitcoin node broadcasting a transaction will evaluate the transaction and if it is valid will add it into their mempool and continue the propagation of the information.
When submitting a new block to the network
The direct peers of the bitcoin node submitting the block will evaluate the block and if it is valid will append it into their blockchain and broadcast it to their other peers.
The mempool is a buffer of a fixed size where pending transactions are recorded. And every bitcoin node hosts its own instance of mempool that is synchronized through the peer-to-peer network. Note that if the mempool is full, the transactions with the lowest fees will start to be canceled.
Since it is a limited size buffer, you are in competition with the other people doing transactions. Hence transactions include a fee. You can modulate that fee up to increase your chance to get recorded in a block quickly. The higher the fees you pay, the more interest the miners will have to include in your transaction to maximize their gains.
To earn the right to append a block to the blockchain, the bitcoin nodes use the concept of proof of work.
On the bitcoin network, there is a variable synchronized on every node called the difficulty. That variable corresponds to the mining difficulty. And to earn the right to append a block, the goal is to try different nonces in a block template provided by the bitcoin daemon until the value of the hash is lower than a certain target hash extrapolated from the difficulty.
It is visible by looking at the hashes of the blocks, for example:
The higher the difficulty, the lesser chance to find a block, the smaller the target number will be, thus the higher number of leading zeroes there will be.
In the first blocks, the difficulty was 1. And every 2016 block (about two weeks), the difficulty is adjusted up or down in order to make sure we get blocks every 10 minutes on average. Because this operation of trying nonce and hashing is actually predictable (on average).
One last interesting note is how the network reacts to network splits. Imagine a malicious country cutting the internet at its borders. Also stopped all their Bitcoin miners (or almost). At first, the difficulty would be too high for them to find blocks every 10 minutes or even any blocks at all. Then the network would adjust the difficulty to a low value that would match the last two weeks' hashrate. At that moment, maybe the difficulty is back to 1. If they turned back on all their Bitcoin miners then they would validate blocks not only every 10 minutes but potentially every second or less. Soon enough their domestic Bitcoin blockchain could surpass the height of the blockchain of the rest of the world. What will happen if they open back their internet borders? Well, all their effort to erase the blockchain will be crushed. Because Bitcoin will agree that the one true blockchain is the longest chain with the most work. So the domestic with X amount of blocks in advance but Y amount of less work would be quickly replaced by the rest of the world chain having fewer blocks, but a lot more work committed.
Bitcoin uses something called Hierarchical Deterministic Wallets.
BIP32 Hierarchical Deterministic Wallet
Source: Medium
Wallets can be also shared among multiple owners, and we call those multisignature wallets.
Justin, Vittie, and Craig each hold one of the three keys needed to unlock the multisig wallet.
Source: Coindesk
There are two major use cases for the multisignature wallets:
Share funds across multiple people. Imagine a company owning a bunch of Bitcoins like a crypto exchange, having a multisignature wallet will make sure that if someone is not available other people on the team can access the funds.
Real-life example:
Bitcoin offers a simple stack-based programming language, non-Turing complete (on purpose). Below find the page about it from the Bitcoin Wiki.
Source: Bitcoin Wiki
OP_RETURN can be used to leave a message on the blockchain. To interact with the bitcoin daemon, it exposes an RPC interface. This is what the bitcoin CLI tool uses, but you could also use curl or any HTTP client to communicate with a bitcoin node.
Source: Bitcoin.org
bitcoin-cli sendtoaddress 1 ${YOUR_FRIEND_ADDRESS}bitcoin-cli getbalancebitcoin-cli submitblock ${HEX_BLOCK}bitcon-cli getblockchaininfo Major updates and changes in Bitcoin are always first discussed and properly specified in a document labeled BIP. All the BIPs and work-in-progress BIP are stored there:
https://github.com/bitcoin/bips
As of 2022 and several years from now, if you understand all the concepts cited in that article, you are good enough to start with your development using Bitcoin.
Depending on what part of Bitcoin you will be working with or using, here are some hints on where you should start digging:
SPOILER In a future article, I will introduce Liquid, so stay tuned.