If you are going to work on software using Bitcoin or
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.
Source:
A node, or called block too, contains also information:
A
A timestamp to state at what time the block was added to the blockchain
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.
Source:
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 right of appending a block in the blockchain is 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 blocks (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.
Source:
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:
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: https://www.cnn.com/2019/02/05/tech/quadriga-gerald-cotten-cryptocurrency/index.html
Have a backup of your wallet. Imagine you could have a signature on your computer, on your phone, and the third one on an old device hidden. If you lose any one of your devices, then you still have a chance to access your funds and move them to a new wallet.
Bitcoin offers a simple stack-based programming language, non-Turing complete (on purpose). Below find an example from the page about it from the Bitcoin Wiki.
Source:
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-cli sendtoaddress 1 ${YOUR_FRIEND_ADDRESS}
bitcoin-cli getbalance
bitcoin-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:
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:
Also published on: https://sonny.alvesdi.as/introduction-to-bitcoin-for-developers