We're going to carve away a high-performance service from a very simple node app. We'll do load testing to verify our bottleneck, create a protobuf file to clearly communicate the service boundary, and then integrate a golang server implemented with an RPC library called Twirp.

Throughout October 2017, we ran the first season of Productivity Quest — our Slack-based series of challenges tailored to improve the way we work. It turned out to be a huge success and kept the community wanting more. This year, we decided to take a different spin.

Reddcoin was designed to be a social currency to credit creators and others that might be losing to the ever-changing advertisement industry. We're going to see how to work with RDD coins in the sense of sending, managing, and maintaining these coins with Node.js and JavaScript.

Many articles have been written about refactoring. What I'm trying to do here is to bring to light real life's example of how together with my team we approached the problem and how do we plan to deal with it.

Introduction to Bitcoin for Developers

Interest in the Bitcoin cryptocurrency is greater than ever. The value has just reached 6500 USD and it shows no signs of decreasing in the near future. Moreover, the whole cryptocurrency market is growing – every day brings new applications, ICOs, exchange systems, and ideas to be developed or maintained by programmers. Implementing new features or business logic requires deep knowledge about the cryptocurrency, in this specific example, about Bitcoin.

All resources that I have found on the internet or books about Bitcoin or Ethereum describe the basic concepts, investment process or highly technical stuff in the blockchain. Therefore, for any programmer who would like to integrate with the Bitcoin network, there is a huge gap between and lack of information. This article is a part of series where I would like to give you an introduction to the Bitcoin cryptocurrency, to its historical background, and explain its underlying mechanisms and show how to integrate or communicate with its API.

What is Bitcoin?

Bitcoin is a form of digital currency, created and held electronically. Like your own money in a local bank. You can access, manage, or transfer the funds from your account on the bank’s website on the internet. But, the main difference between your bank and Bitcoin is that no one owns the Bitcoin network – it is decentralized. This means that no large institution can control the money and all responsibilities are put on the money owner – you.

There are some pros and cons for this solution. From one point of view, when you store your money in Bitcoin, you are the only holder and have a full freedom. Nobody can block your account or transfer the funds by mistake. On the other hand, if you make a mistake and transfer money to the wrong address, it is gone. There is no helpdesk that you can call to and ask to cancel the transaction.

What is Bitcoin for?

Bitcoin works or will work as a typical currency. At the time of writing this article, it is possible to order a pizza and pay with Bitcoin, so I find Bitcoin to be a solid and stable medium of exchange. Moreover, Bitcoin solves a couple of issues created by traditional banking:

It is decentralized

This means no single entity, company, or institution can manage the Bitcoin network. Transactions, management, or any repository of information are stored on many computers or systems that store the information about the state of the network.

No agreement is required to open an account.

Everybody is allowed to open a new account or accounts without filling any forms or presenting an ID or any other document. Opening a new account where you can store Bitcoin is easy like creating a new email address (exchanging traditional currency for Bitcoin is a different story, though).

Owners are free to do with their funds and accounts as they please.

Nobody can freeze your account, steal your funds, or make a transfer from your account without your knowledge. The network is not limited by country or region. No arbitrary limits can be set on the account.

Fewer risks for merchants

All transfers go directly from person to person via the internet without going through a bank. Because no central institution owns the blockchain, the fee for every transaction is set by the sender. In the result, the fee can be low.

Transparent and neutral

Every node has a full copy of the blockchain containing all information about blocks, addresses, and transactions.

Bitcoin History

Bitcoin is the first implementation of an idea for a cryptocurrency created in 1998 by Wei Dan. He proposed a new form of money that uses cryptography rather than central authority to control the transactions. In October 2008, an unknown software developer or an organization who goes by the name Satoshi Nakamoto published a paper on the Cryptography Mailing list describing the idea of the Bitcoin digital currency. It was titled Bitcoin: A Peer-to-Peer Electronic Cash System. On January 2009, a first version of the Bitcoin software was published on Sourceforge. Satoshi Nakamoto claimed that the code had been in development since 2007.

The genesis block has a timestamp of 18:15:05 GMT on 3 January 2009. Because all blocks in the blockchain refer to the previous block, the first block required a special, custom code that allows to mine it. First blocks were not mined only by Nakamoto and each of the miners was rewarded with Bitcoin for 10 days afterward. Except for the test transactions, these remain unspent since mid-January 2009.

How Does Bitcoin work?

The Bitcoin is a kind of a spreadsheet or a ledger that stores information about the transactions. Those pieces of information are held in the blockchain (see The Blockchain fundamentals for developers) in cryptographically secured blocks.

Because the system is decentralized, all of the users and parties can install their own instance on their computers. If you would like to join the network, you can either register on an existing website, such as the blockchain.info, which will create a new account for you, or you can download the Bitcoin software and join the network.

Registering to the website does not require any additional work from your site like downloading the blockchain to your computer. Somebody has already done this work and wants to share their resources with you – freely or not. This is a fast and smooth process. However, history shows us that some of the services were not secured enough in the past and, in the end, some of them were hacked and the money stolen. Because Bitcoin provides anonymity, it is impossible to track the funds or the attackers.

I am not saying that keeping Bitcoin at an external service is a bad thing, but I strongly suggest to choose this service reasonably and use only proven and verified websites. I keep my own Bitcoin at one of the websites and I feel comfortable with this.

Conversely, you can download the Bitcoin software (such as the Bitcoin Core) and download the full blockchain information to your computer. The software will download approximately 140 GB of data. However, this solution gives you full freedom of managing your funds and resources, possibility to mine new blocks and additional features like cold wallets.

Hot and Cold Wallets

In the blockchain world it is said that the security is as good as the encryption is or where you store your private key to the wallet. If you print it, anybody who has seen the paper can steal it, if you send it by email, anybody who hacks your email can steal it and if you keep it in password protection software, you depend on the software’s security. But, there is a way to double secure your assets and save them even if somebody will be able to steal the private keys – using the Cold Wallet or the Cold Storage.

The Hot Wallet is a usual address that you can use every day. The main difference between that and the cold wallet is, it can be encrypted from every point of the internet by using one of the authentication methods, e.g. with the private key. This private key works like a password to the bank account.

The Cold Wallet is a store available only from one point of access, which should be always offline, such as a pen drive or a computer disconnected from the Internet. The transaction can be encrypted only from this computer which means, even if somebody has the private key, they cannot transfer the funds from your account.

My professional experience as a developer says that there will always be at least one individual that wants to steal your money. And it does not matter if you create a Facebook’s contest app with a $50 prize for the petrol or a bigger application which will hold millions of dollars. A good practice is to keep a higher volume of money in cold wallets.

Transfers

If you want to transfer your money, you need to create a message that says “I want to transfer some amount of coins from my address to your address”. Moreover, this message should have a signature – like a check in the real world has got a signature or your credit card has a pin number (or signature). This signature is cryptographically generated based on your private key. This secures the transaction from thieving. Unlike the real-life signature, the private key can be used only once for the specific transaction.

Your coins are kept in the Bitcoin Wallet. Whenever you make a transfer, some coins are transferred from your address to the another. Because the blockchain in the Bitcoin network is public and transparent, it stores only the addresses, not names or any additional information. This applies to the accounts and to the transfers. So, the transfer has no title like in a traditional bank. At this point, this may be a little bit confusing – so, how do I know who sent me money? For every transfer, or for every person, you are able to generate a new address or addresses assigned to your wallet. One wallet can have many addresses.

I have opened an account in one of the available services on the Internet and I keep a couple of Bitcoins there. If I would buy dinner for my friends Kamil and Nick, and they want to return the money in Bitcoins, I should generate a new address for Nick, and new address for Kamil. When they transfer coins to the addresses, the funds will be automatically assigned to my wallet, and I will be able to check which transfer comes from which person without asking for their addresses.

Bitcoin Value

Let’s stick to the dinner topic and assume that I like pizza. The value of a single Bitcoin grows so fast that if I have 0.04 BTC (approximately 250 USD), I would be able to order one free pizza every week (assuming that a pizza costs $10) because of the value change. This can happen because of a couple of factors, such as the limited amount of Bitcoin, the miner mechanism, more people becoming aware, and others.

I hear a lot of voices that say buying Bitcoin or investing into Bitcoin is the perfect choice. Well, yes and no. As with any kind of investments, there are some significant pros and cons. I have to admit that the Bitcoin rate rises quickly, but buying 1 Bitcoin for $5000 is almost impossible. Usually, the spread between selling and buying currency less than $0.25. You can buy 1 EUR for 1.17 USD, but if you would like to sell it, you will receive 1.14 USD. The spread with Bitcoin is much bigger – you can buy 1 Bitcoin for 7000 USD and sell it for 3000 USD (depends on the exchange).

Limited Amount of Bitcoin

Unlike traditional currency, the Bitcoin amount is limited to 21 million BTC. The inflation happens at a stable rate and halves every four years. Because no one owns the Bitcoin network, this rule cannot be changed and no extra cash can be generated or printed. Thus, if more people want to buy Bitcoin the price will rise because of the limited supply.

The Miner Mechanism

The mining process was pretty easy at the beginning when the Bitcoin network was pretty young. Everybody was able to mine on local computers. Nowadays, more nodes are connected to the network and try to mine new blocks. Only the fastest miner is awarded which means that the competition is huge and requires more resources to win. More resources mean more investments, money, and costs. Because the mining cost is higher, the final rate of the Bitcoins rises too.

People Become Aware

With the limited supply, if more people are aware of Bitcoin and its possibilities, more people want to buy it. Which means the price will grow.

The Mining

The mining process can be described as calculating the cryptographical hash from the latest block. This operation is hard to do and consumes a lot of time and resources, but it is easy to check for other nodes and confirm that the mined block is legit and can be joined to the blockchain.

Every new block is mined in approximately 10 minutes. This does not mean that the next block will appear in the blockchain after 10 minutes. Sometimes you need to wait for the next 60 minutes and then 6 new blocks can be published.

A newly sent transaction might be added to the latest block, or it might wait to be mined a little bit later. Everything depends on the number of transactions that were sent since the latest block or are pending because they were not published in the latest block. This happens because the block size was limited to 1MB before 1 August 2017. Since then, it can be bigger than 2MB (even 8MB) as the new SegWit2.x was introduced.

If the limit is exceeded, every pending transaction should be mined in a new block. Moreover, the transactions mining order depends on the fee that you set with the transaction – bigger fee means that the transaction will be confirmed faster.

Confirmations

The security is one of the most important topics when you need to deal with money. The number of confirmations for a single transaction is present to show that the block where the transaction was mined is confirmed by other miners and nodes. Usually, the transaction can be marked as secure when it has at least 6 confirmations.

However, waiting for 6 confirmations may take an hour or two, which is quite a lot of time for micro-transactions. I have found that the following number of confirmations in specific situations are good enough to accept the payments:

1 confirmation for small transactions or for transactions from trusted sources (as my friends Kamil and Nick),

3 confirmations for usual transactions like paying for stuff from an Internet store or ebay,

6 and more confirmations for bigger transactions like buying a car, house, or investments.

Final word

I explained the basic concept of Bitcoin that gives a solid fundamental knowledge to start playing with its API. I described what is the blockchain, the block, and the transaction.

On the next article, I will show you how to setup own Bitcoin client as a test environment and start to get the first information from the blockchain to your code.