How Bitcoin Works
- Luda Kukuetu
- 2024-03-25
- 12 min
- 7
- 7 830
There is a lot of talk about crypto and blockchain because it is a good investment, but few people go into detail.
Many are satisfied with the knowledge, which is enough to have a wallet, to receive, to send cryptocurrency to another or to sell it. To use cryptocurrencies comfortably, it is very important to understand what is bitcoin and how it works. This information can remove a lot of questions and avoid problems with usage.
Once, we have already reviewed what bitcoin is, but we didn't go into details. In this article, we give an accessible and detailed description of how bitcoin works.
The network: how bitcoin works
Bitcoin is a peer-to-peer payment system (network) where the unit of account is the cryptocurrency of the same name. We will explain how bitcoin works.
A peer-to-peer network is a computer network, the principle of which is equality of all participants. Each peer or node, as they are called, acts as both a client and a server.This organization allows the network to remain functional for any number or combination of available nodes. Participants in the network are all peers, so the main feature of the system is complete decentralization: there is no central administrator or server.Instead of the term "peer-to-peer", definitions such as decentralized, peer-to-peer or p2p (peer-to-peer) network can be used.
Blockchain
The core of bitcoin and the "ledger" of transactions is an ordered chain of blocks, or simply, the blockchain. Each block of the bitcoin network holds up to 1 MB of data, takes an average of 10 minutes to form, contains information about all the transactions placed in it and information that links it to the previous block.
Each block in the chain is cryptographically interconnected with the previous and subsequent blocks. This interconnection protects the network from future substitution or modification of existing blocks, making the blockchain immutable and transactions irreversible.
How bitcoin transaction works
To better understand the basics of bitcoin network, let's introduce a hypothetical crypto-enthusiast Tony, who has a bitcoin wallet with 2 BTC in his balance. We discussed what a bitcoin wallet is in the previous article.
Our Tony wants to present 1 BTC to his friend Cindy, and for that he needs her unique bitcoin wallet address (the analogue of a traditional bank account number). Tony enters this address into the appropriate entry field of his wallet, and in the other field he specifies the amount of the transfer - 1 BTC. Then Tony moves on to setting the amount of fees for the transaction, here we will stop in a little more detail.
Transactions and network load
Like any other payment system, bitcoin has transactions, that is, the movement of assets from one customer to another. Bitcoin transactions charge a fee, which goes as a reward to the miners who confirm those transactions. The higher the fee and the lower the network load, the faster the transactions are confirmed.
Essentially, a transaction is a set of information that tells network participants by what value the sender's balance has decreased, and by what value the recipient's balance has increased.
Miners
For a transaction to be successful, it must be validated by miners, so it is important to understand how bitcoin mining works. Now, these are special organisations that use powerful computing equipment (ASIC-farms) to check transactions for "correctness", assemble these transactions into blocks, and sequentially join the blocks to each other. It takes the network algorithm 10 minutes to form 1 block, during which the miners perform the same type of computational work every second in search of one single correct solution. It's not hard, it's not a math problem, but it's a very large amount of work that's hard to do with small computing power quickly and efficiently.
Miners are rewarded for their work in the form of BTC. This reward consists of the fees that users paid for the transactions and the new bitcoins that the network itself generated when a new block was collected and confirmed from the transactions. We will talk more about miners and the generation of new bitcoins in a separate article.
Fees
Back to our crypto-enthusiast... Setting the transaction fee, Tony is guided by the fact that for each byte of information he has to pay a certain amount of satoshi - a fraction of a bitcoin. Since Tony doesn't know how much data his transaction will take, he decides to save money and set the price at 1 satoshi per byte.
We will tell you how the weight and cost of Tony's transaction is formed.
Those 2 BTC that he has in his balance appeared in his wallet for a reason. Tony earned them. 1 BTC in 2017, 1 BTC in 2018. Now he wants to send half of it to his girlfriend. His transaction, in addition to information about the number of coins, will include information about the addresses from which the coins came to him (called inputs), information about the recipients' addresses (called outputs), and information about his own address.
- The weight of each input from which coins were received is ~148 bytes;
- Each output to which the coins go have a weight ~34 bytes;
- The weight of the transaction itself is ~10 bytes, regardless of the number of addresses or transfer amount.
We do the math: 148*2+34*2+10=374 bytes. Now we know that our "greedy" Tony will pay 374 satoshi for the transaction
Change
You may ask, why do we multiply 34 (the weight of the recipient address) by 2 when there is only one recipient? The thing is that when you send a part of your balance to someone, it's not a part but the whole amount that goes from your balance, and after validation, i.e. including the transaction in the block, the balance (minus the network fee) goes back to your wallet. Therefore, in addition to the information about the recipient's address, the transaction also includes information about the sender's address, that is, your address. This return is called change. In browsers you can see this moment in the following form.
Mempool
Setting a small fee of 374 satoshi, Tony confirms the sending of 1 BTC and waits for the miners to confirm the transaction.
But the miners are in no hurry to confirm it, the transaction goes to mempool and waits for confirmation. Why? Because miners want to make money, not just operate their equipment, because they will only get 374 satoshi for confirming a transaction. Especially for such very cheap transactions and there is a mempool with its sorting system.
First of all, miners "take" from the mempool lighter transactions and with higher fees, because it is more profitable to confirm them and there is a lot more of them in the block.
But don't think that once in the mempool, the transaction is assigned a number. No. This queue is dynamic, with many transactions with different fees and weights every second, so our Tony's transaction can wait a long time for its confirmation. And, most importantly, while it's in the mempool, Tony can't dispose of the change.
In fact, in this situation, the sender's balances are sort of "frozen". It is this mechanism that protects the network from spam transactions - situations where a spammer creates a lot of cheap sends, thereby overloading the network.
While Tony's transaction is in the mempool, it is not considered confirmed and is not considered pending. At the same time, it can stay in the queue for quite a long time - until it is confirmed, or until it is deleted by validating nodes from the mempool. According to statistics, the maximum waiting period is ~3 days, but in moments of high network activity this period can last for a week or longer.
Nowadays the speed of transfers is one of the priority characteristics when it comes to the use of payment systems and such queues are not very pleasant.
There are a lot of ways to push the transaction through a mempool. All of them are quite complicated to understand and implement for the average user, and some of them border on such a fraudulent way as "double-spending". Nevertheless, there is one great feature that anyone can use, so let's talk about it. It's called Replace-by-Fee (RBF).
Replace-by-fee (RBF) in bitcoin: how it works
At the moment, not every bitcoin wallet has such an advanced functionality. RBF is an editing of an existing transaction that is in a mempool.
Editing means that the new transaction will use the same inputs as the old one, but the fee will be changed. Such an edited transaction will not be considered "double-spending" since using the Replace-by-Fee option will avoid this. The bitcoin network nodes treat the edited transaction as the old one, and it is moved in the mempool queue to a more attractive position for miners.
Thus, if the fee is too low, increasing it is a normal practice in case you need to speed up the transaction. Do not disregard the RBF feature and turn it on before making transactions, it allows you to secure yourself in case the transfer is delayed in the queue.
Confirmation: how bitcoin transaction works
Tony figured out in time that because of his greed Cindy might not get the gift at all, so he used the RBF feature in time and increased the fee. The transaction, as expected, became more attractive to the miners, and they included it in the next block - the transaction received 1 confirmation. With each subsequent block found, this transaction will receive 1 additional confirmation. When there are 6 or more such confirmations, the transaction is considered confirmed.
Transaction speed
It is impossible to determine the exact transaction speed, but let's calculate it based on Tony's transaction and the data we have.
Condition:
- One block size is ~1 MB (1'048'576 bytes);
- each block filled with transactions is generated once every ~10 minutes (600 seconds);
- our Tony's transaction has a weight of 374 bytes.
Solution:
- 1'048'576 bytes : 374 bytes = 2803.6 transactions in one ten-minute block.
- 2803.6 transactions : 600 seconds = 4.6 transactions per second.
Answer: Every second, miners confirm ~5 transactions, similar to Tony's transaction.
Of course, these are all approximate figures, as the processing speed varies every second and depends on many factors. The main thing is to have your transaction taken from the mempool as soon as possible, and if that happens, your transfer will reach the recipient within 1-600 seconds.
In fact, once the transaction is included in the block, the recipient can not only see the funds on their balance, but also, under certain conditions, dispose of them. Fast, isn't it?
Yes, but what about the 6 confirmation rule? 6 blocks, 10 minutes for each block, total of 1 hour...
Bitcoin clients
It's all about the software that cryptocurrency holders use. Classic Bitcoin wallets will display a transaction as "unconfirmed" until it receives 6 confirmations (blocks).
Services that accept BTC as payment can put their limits on the number of blocks needed for confirmation (7, 10, 45, 214 ...).
BTC, which are generated by the network as a reward for finding a block, can only be used after 100 confirmations, but classic bitcoin wallets will not display coins received for finding a block until 120 confirmations have been received
The number "6" was not chosen by accident. With each new block, the transaction becomes less and less vulnerable to spoofing or modification. To do something with an already confirmed transaction in a block, it would be necessary to "rewrite" all subsequent blocks, and this requires very huge computing power and millions of dollars of investment. And the more confirmations a transaction receives, the more secure it will be.
But in a blockchain as powerful and stable as bitcoin, this is more a requirement of the recipient of the transaction than a protocol rule.
This is why our Cindy and Tony use a very advanced and functional bitcoin wallet, rather than the classic one. Because of the unconfirmed transactions feature activated in it, Tony's outgoing transaction, once included in the block, will immediately show up in Cindy's wallet. In doing so, Tony will be able to dispose of the change immediately, and Cindy will be able to dispose of the gift received.
"SegWit + RBF + use of unconfirmed transactions" allows to squeeze the maximum speed out of BTC blockchain, which is important for non-castodial services.
Anticipating your question what kind of bitcoin wallet do Tony and Cindy use, we will answer at once - Trustee Wallet.
Summary
Bitcoin is very complex. It is impossible to describe all the intricacies and nuances of its work in one article, but we did tell you about its basics. Now you know what complex journeys your coins make within just one single transaction between two wallets and at what speed. How and in what way the network protects itself from hacks, spam attacks, scams or rash actions of newcomers.
Sustainability of bitcoin depends on miners as much as it does on regular users, and the more participants there are in the network, the more secure it will.
In the next article we will explain in detail how to use RBF features of Trustee Wallet and show you by example how to speed up not only outgoing transactions, but incoming ones as well! Stay tuned!