During my journey with the cryptocurrency, I realized this is a common problem to find a highly qualified programmer who used to work with the cryptocurrency network. This is like finding a big panda in China – they are super rare. So, finally I was called a big panda and because I like to have some big panda friends, I decided to share the knowledge and my experience.
I will create a set of articles about the blockchain, the idea and general view, cryptocurrency – the Bitcoin, the Ethereum (including smart contracts), EOS (new fancy cryptocurrency that may be a trendsetter next year) and all troubles and problems that we faced.
From my personal experience, it came that the blockchain idea is both – technical and intricate. Coding without a strong knowledge about the background and idea of the blockchain brings to a lot of confusion and misunderstanding. Let’s start with the idea.
What is the blockchain?
When most people think about the blockchain the first thing that comes to mind is cryptocurrency, like the Bitcoin or the Ethereum. But, Melanie Swan, founder of Institute for Blockchain Studies, claims that
We should think about the blockchain as another thing like the Internet – a comprehensive information technology with tiered technical levels and multiple classes of applications for any form of asset registry, inventory, and exchange, including every area of finance, economics, and money; hard assets (physical property); and intangible assets (votes, ideas, reputation, intention, health data, information etc.).
From a more technical point of view, the blockchain is a non-centralized system purposed to share things through the Internet. Something like a torrent, but with few differences.
When I send you a file by torrent, we will end with a situation where me and you will have the same file on our disks. But if we both remove this file from our computers, it will not be available on the network anymore. In the blockchain, when I send you a file, everybody connected to the blockchain will have a copy of this file and we cannot remove this file from the network. The difference comes from the purpose of both systems – the torrent system is all about sharing information, if you do not need a copy of the information, it can be removed. The blockchain is all about preventing the information lost. So, all the seeders have to get a copy of the information from the blockchain.
The blockchain as an idea is based on the following concepts:
Decentralization – the peer-to-peer network does not need to have any central point of access to server or DNS. All nodes share information between each other and if one of them does not work, every other can do the job.
Security – cryptography ensures that the stored information is safe and immutable. Every transaction is cryptographically connected with the block and the block is cryptographically secured.
Anonymity - (for cryptocurrency blockchains as the Bitcoin or the Ethereum) because all information is shared across the nodes the data saved in blockchain becomes public. To avoid a situation where everybody can follow your transactions, all addresses, wallets or transactions are just numbers without any additional comment or name that may suggest the owner or purpose.
Game theory – to persuade seeders to keep the nodes working, an award system should be created. The blockchain support two system of rewarding seeders – mining and fee. When the peer calculated all hashes for the newest block as the very first he is rewarded with some amount of prize, this process is called mining.
The question is – what kind of information do we store in the blockchain?
Transactions
When we think about the blockchain we mostly have the cryptocurrency in the mind, which was invented as a competition to banks. The bank account is mostly dedicated for storing the funds and transferring them from one account to the another. In the cryptocurrency, this works in the same way.
An investor or a user can open new wallet – which works like a login to your bank account. A private key to this wallet can be generated – which is used as a password (there are more ways to access the wallet, but I will describe them in an article dedicated for specific cryptocurrency).
When the wallet is opened, a new address is automatically bound to this wallet. All funds are stored or assigned to the specific address. The funds can be transferred between addresses and because the transfer has not got any title, name or receiver, except the final destination address, a new address for each transaction should be generated to recognize the purpose of the transaction. In an example, if you want to ask your friend to send you money for the last dinner you put him last day, new address should be generated for this one, specific transaction. Otherwise, the transaction sender can be recognized only by the amount of funds that are sent or if your friend sends you an address from which she sent money.
Each transaction has few fields with an information about its state – the sender’s address, receiver’s address, amount of funds, time, block number, number of confirmations and fee.
Blocks
The blockchain is a distributed ledger of immutable digital records saved in a chain of units called blocks. When the transaction is sent it is not added to the blockchain yet. Every transaction needs to be mined, which means that one of the nodes needs to collect newly published transactions, calculate the hash of the latest block and spread an information about the latest block in the blockchain. When this information is published, each transaction in the latest block will have first confirmation and the funds can be primarily treated as transferred. Each block can be identified by a block number or block hash. A new block contains cryptographically hashed data and is built upon the previous block in the chain, ensuring that the data in the blockchain cannot be compromised.
A block can be easily described as a set of transactions. The blockchain can be described as set of blocks.
Block hash calculations
Because the system is decentralized, securing it is a hard task to do. As with every system where money is involved, somebody will try to break it sooner or later. The cryptocurrency blockchain, the Bitcoin as a pioneer in this matter, solves a couple of really specific problems in the blockchain:
Cracking funds from wallets
Because the information about wallets, funds, and transactions are public it is really easy to find a wallet which stores a lot of money and try to break it or make a transaction (fake or not) from this wallet to your own one. To secure users wallets, the cryptocurrency networks require signing all transactions by a private key. The private key is kind of a ticket that allows you to manage your financial resources.
Every key is 256 bytes long, which solves a force attack problem.
Because the blockchain ensures the anonymity, everybody who has knows the address and has got the private key to this address can make a transfer.
Double spending and publishing incorrect blocks
Double spending is a situation where a user has some amount of cryptocurrency on his wallet and sends two different transactions and spend the same money twice. It’s like going to the store with only 10 $ in a wallet and buying stuff for 20 $. You go to the cash desk and pay 10 $ and then… you go to the cash desk and pay the same 10 $ and somehow – everybody is happy. The transactional system which operates with a huge amount of money should prevent double spending.
The Bitcoin solves that problem with a mining process which is described as calculating the cryptographical hash for the latest block in the chain. That calculation needs to be based on the hash generated from the previous block. The funds from the wallet are spent only when the transaction is mined, which means confirmed, but not once or twice, but at least 3-6 times in the blockchain. Because the mining process consumes a lot (and I mean – a lot) of resources, it is hard to mine 3 or even 6 rows faster than your competitors in the network. Moreover, the mining is time and resource consuming process, but verifying hash for the mined block is a fast and easy operation. So, every node can easily verify if the block mined by you is correct or incorrect and should be rejected from the blockchain.
Blocking the blockchain
Let’s focus on the Bitcoin network for a moment longer. The block size is limited to 1MB and if there are more transactions pending to be mined or confirmed, they need to wait for the next block. Usually, the full block stores an information about 2000-3000 transactions. Imagine that you want to block the blockchain for a moment or two. It is easy to calculate that publishing 288 000 transactions will block all other users for next 24 hours and probably make some bad noise about the Bitcoins and decrease the value of BTC.
288 000 may sound like a huge amount, but the Bitcoin was invented mostly for micropayments, so let’s multiply that by 0,003 & – it is only 1000 $. The Bitcoin implements the Dust Threshold, which is different for each node (you can change the value in a configuration file). By default, it is set to 0.00005 BTC – around 0.1 $ at the time.
51% percent attack
51% percent attack is a potential attack where one organization is able to control more than a half of available nodes in the network mining power. The Bitcoin network operates on the verification process – every block is not only mined by one miner but also verified by the other nodes. Bitcoin nodes look to each other to verify what they’re working on is the valid blockchain. If the majority of miners is controlled by a single entity, they can have a power, or at least try to, take a control on the confirmation process. This does not give a power over the full network and still, some blocks can be invalidated and the attacker would be able to manipulate the transactions only over few last blocks, but enough to double spend the money.
The idea of controlling more than a half of the network is theoretical because the cryptocurrency market is growing every day. So far, 10 000 nodes are working instantly in the network, so another 10 000 should be opened by the attacker, and the smallest one should be bigger than the most powerful existing node (https://bitnodes.21.co/).
Confirmations
The number of confirmations is really important in the cryptocurrency because the blockchain does not provide you a status done or verified for transactions. Just the number. The decision, how many confirmations do you need to trust that the transaction is correctly added to the blockchain depends on your confidentiality.
I usually set 1 confirmation for really small transactions, under 5-10 $, 3 for bigger ones, under 1000$ and more than 6 (ex. 15) for transactions bigger than 1000 $. The reason to manipulate the number of confirmations until the transaction is approved is to confirm the money spent faster or more confidently. Each block in the Bitcoin network is mined in approximately 10 minutes. That means you need to wait 30 minutes for 3 confirmations, or 3 hours for 15.
A fee
The transaction fee is an amount of funds that the sender specifies with the transaction. All fees are collected by the miner. The miner decided which transactions will be included in the block and mostly chooses transactions with the highest fee. The fee is paid in addition to the sent amount of funds – if you would like to send 10 $, you need to have also about 0,1 $ on your wallet to pay the fee.
Conclusion
Working with the blockchains can be a fun, especially with the cryptocurrency blockchains, but without knowledge solid foundations it can wipe awake at night. Integrating with the BTC or ETH RPC API or sending transactions may be a difficult task to do and requires deep knowledge of not only the API endpoints but also the blockchain and transaction’s structure. Understanding the blockchain can take a while, but it is worth the effort.
Next article will be dedicated for the Bitcoin cryptocurrency. See you soon.
P.S. If you would like to ask about something related (or not) to the cryptocurrency, or read a new article about some specific topic, please feel free to leave a comment below. I will be happy to help.