How does blockchain work?


How does blockchain work?

Blockchain technology is probably the best invention since the advent of the Internet era. It allows value exchange without the need for trust and central authority. Come on, let’s imagine a bet. We’re betting $50 on the weather in San Francisco tomorrow. I bet it will be sunny and you choose to rain. Now, we have three options to manage the deal:

  1. We can choose each othertrust。 Rain or shine, the loser will give the winner $50. If we are friends, this may be a good way to deal with it. However, no matter friends or strangers, no one can easily pay anyone.
  2. We can choose to turn the bet intocontract。 Once a contract is signed, it is easier for both parties to fulfill their payment obligations. However, if the loser does not fulfill the payment obligation, the winner will have to take legal measures and spend extra money to pay the legal fees, and the litigation may also require a high time cost. This does not seem to be the best way to manage transactions, especially for small amounts of cash.
  3. We can choose to letNeutral third party intervention。 Each of us will give $50 to the third party, who will give the total to the winner. But maybe she’ll run away with all our money? At this point, we can only choose between the first two options:trustorcontract

But neither trust nor contract is the best solution: we cannot trust strangers, and it takes time and money to execute contracts. Blockchain technology is interesting because it provides us with a safe, fast and cheap third solution.

Blockchain allows us to write a few lines of code (a program running on the blockchain) so that both of us will send it $50. This program will guarantee the safety of $100 and automatically query the weather of tomorrow on several data sources. Whether it’s sunny or rainy tomorrow, it will automatically transfer the full amount to the winner. Each party can check its contract logic before the program runs, but once it runs on the blockchain, no one can change or stop it. That might be too much for a small $50 bet, but imagine selling a house or a company?

This article explains how blockchain works, but does not discuss the technical details in depth. Instead, through in-depth mining, you can get a general understanding of the underlying logic and mechanism.

Basic knowledge of bitcoin

How does blockchain work?
Image provided by the author

The most widely known and discussed application of blockchain technology is bitcoin, which is a digital currency that can be used to exchange products and services, just like the US dollar, euro, RMB and other national currencies. Let’s start with the first application of blockchain technology to understand how it works.

“Bitcoin allows us to transfer unique digital assets to Internet users for the first time, and this way is safe and reliable. Everyone knows that the transfer occurs, and no one will question its legitimacy. It’s a breakthrough that can’t be overemphasized. “
——Mark Anderson

“One bitcoin” is a single unit of bitcoin (BTC) digital currency. Like the dollar, bitcoin itself has no value; it is only valuable because we agree to trade goods and services to keep more currencies under our control.

To record the amount of bitcoin each of us owns, the blockchain uses a ledger, a digital file that tracks all bitcoin transactions.

How does blockchain work?
Figure 1 – bitcoin electronic ledger

Ledger files are not stored in a central entity server, such as a bank or a single data center. It is distributed around the world through a network of private computers that store data and perform calculations at the same time. Each computer represents a “node” of the blockchain network and has a copy of the general ledger file.

If David wants to send Sandra bitcoin one day, he will send a message to the network, saying that the number of bitcoins in his account should be reduced by 5 BTC, and the number of bitcoins in Sandra’s account should be increased by 5 BTC. Each node in the network receives the transaction message and applies the requested transaction to its ledger copy to update the account balance.

How does blockchain work?
Figure 2 – transaction request message

The fact that ledgers are managed by a set of interconnected computers, rather than by a centralized entity like a bank, has the following implications:

  • In our banking system, we only know our own transactions and account balances; but in the blockchain, everyone can see other people’s transactions.
  • Although you can usually trust your bank, the bitcoin network is distributed. If something goes wrong, there is no service desk to call and no one to sue.
  • The design of blockchain system does not need any trust; it realizes security and reliability through special mathematical functions and codes.

We can define blockchain as a system that allows a group of connected computers to maintain the update and security of a single ledger. Then, in order to trade on the blockchain, you need a “wallet,” a program that allows you to store and exchange bitcoin. Since only you can use your bitcoin, each wallet is protected by a special encryption method that uses a unique but interconnected pair of keys: a private key and a public key.

If the message is encrypted with a specific public key, only the corresponding private key can decrypt and read the message. The reverse is the same: if you encrypt a message with a private key, you can only decrypt it with a matching public key. So when David wants to execute a bitcoin transaction, he needs to broadcast a message encrypted with his wallet’s private key. Since David is the only one who knows the private key needed to open the wallet, he is the only one who can use the bitcoin. In addition, each node in the network can cross check whether the transaction request comes from David by decrypting the message using David wallet’s public key.

When you encrypt the transaction request with the private key of the wallet, a digital signature will be generated, which will be used by the blockchain computer to verify the source and authenticity of the transaction. A digital signature is a text string generated by your transaction request and your private key; therefore, it cannot be used for other transactions. Once you change a single character in the transaction request message, the digital signature will also change to ensure that no potential attacker can change your transaction request or the number of bitcoins you send.

How does blockchain work?
Figure 3 – simplified digital signature transaction encryption

To send bitcoin, you need to prove that you have the private key of a particular wallet, because you need the private key to encrypt your transaction request information. Since you can only broadcast messages after they are encrypted, you never need to disclose what your private key is.

Track wallet balance

Each node in the blockchain holds a copy of the ledger. So, how does a node know your account balance? The blockchain system does not record the account balance at all; it only records every verified and approved transaction. The ledger does not actually record the balance, it records only every transaction broadcast on the bitcoin network (Figure 4). Therefore, in order to determine your wallet balance, you need to analyze and verify all transactions that occur across the network connected to your wallet.

How does blockchain work?
Figure 4 – blockchain network ledger

This “balance” validation is based on links to previous transactions. That is, if you want to send 10 bitcoins to John, Mary must generate a transaction request that includes links to previously passed in transactions that add up to at least 10 bitcoins. These links are called inputs. Nodes in the network will verify the amount and make sure that the investment has not been spent. In fact, every time you reference input in a transaction, they are considered invalid. All of this is performed automatically in Mary’s wallet and double checked by the bitcoin network node; she just uses John’s public key to send a 10 BTC transaction to his wallet.

So, how can the system believe that input transactions are valid? Yes, it will check all previous transactions related to your wallet, which are executed by reference input. In order to speed up the verification process, network nodes keep a special record of unused transactions. Thanks to this security check, bitcoin cannot be reused.
How does blockchain work?
Figure 5 – blockchain transaction information structure

What does it mean to have bitcoin? That is to say, some transactions are recorded in the ledger, pointing to your wallet address, but have not been used as input yet. At the same time, all the codes that execute transactions on the bitcoin network are open source; that means anyone with a laptop and Internet connection can operate transactions. However, if there is an error in the code used to propagate the transaction request message, the associated bitcoin will be permanently lost.

Please note that because the network is distributed, there is no customer service support to call and no one to help you recover lost transactions or forgotten wallet passwords. Therefore, if you are interested in trading on the bitcoin network, it is best to use the open-source, official version of bitcoin wallet software (such as bitcoin core), and store your wallet password or private key in a very secure repository.

Why is blockchain called blockchain? Is it really safe?

Anyone can access the bitcoin network through an anonymous connection, such as tor network or VPN network, and submit or receive transactions that display only their public key. However, if someone repeatedly uses the same public key, all transactions can be connected to the same owner. Bitcoin networks allow you to generate any number of wallets, each with its own private and public key. In this way, payment can be received on different wallets. Unless you send all received bitcoins to a single wallet, no one can know which wallet’s private key you have.

The total number of bitcoin addresses is
2 ^ 1 or 146150163733090291820368483271628301659325932542976

The numbers in the above references mean that while everyone is allowed to own their wallets, they can also protect the network from some possible attacks.

But even through this setting, there is still a big security vulnerability, which can be used to recall bitcoin after spending it. Transactions are passed from one node to another in the network, so the order in which two transactions arrive at each node can be different. An attacker can send a transaction, wait for the other party to send the product, and then send the reverse transaction back to his account. In this case, some nodes can receive the second transaction before the first one, so the initial payment transaction is considered invalid, and the transaction input will be marked as spent. How can we know which deal to ask first? It is not safe to order transactions by time stamp because it is easy to be forged, which increases the security risk of bitcoin transactions.

If this happens, there will be differences in the order of transactions received by each node. Therefore, the blockchain system has been designed to use node protocol to order transactions and prevent the above-mentioned fraud.

Bitcoin’s online order transactions are grouped into blocks; each block contains a certain number of transactions and links to the previous block. That’s why you put one block after the other. Therefore, the block is organized into a time-dependent chain (Figure 6), which is named blockchain for the whole system.

How does blockchain work?
Figure 6 – Simplified blockchain sequence structure

Transactions in the same block are considered to occur simultaneously, while transactions not yet in the block are considered to be unacknowledged. Each node can group the transaction into one block, and then publish it to the network as the partition reference of the next block. Since any node can propose a new block, how does the system define how the next block should be divided?

To be added to a blockchain, each block must contain the answer to a complex mathematical problem created using an irreversible cryptographic hash function. The only way to solve this kind of mathematical problem is to guess the random number, which can be combined with the previous block content to generate the defined results. A typical computer may take about a year to guess the right number. However, because there are a large number of computers in the network that are guessing the numbers, one block is solved every 10 minutes on average. The node solving the mathematical problem gets the right to place the next block on the chain and push it to the network.

What if two nodes solve the problem and send blocks to the network at the same time? In this case, both blocks are pushed and each node is based on the block it first receives. However, the blockchain system requires that each node immediately build on the longest blockchain available. Therefore, if it is not clear which block is the last, each node will take the longest chain as the only choice when solving the next block.

How does blockchain work?
Figure 7 – end of chain fuzzy logic

Because the possibility of solving blocks at the same time is low, it is almost impossible to solve multiple blocks again and again, and build different “tails”, so the whole blockchain can quickly stabilize to a single string block agreed by each node.

The debate about which block represents the end of the chain opens up the discussion of blockchain security again. If a transaction happens to be in a block that belongs to the shorter tail (as block B in Figure 7), then after the next block is resolved, the transaction and all other transactions in the block will return to the unconfirmed transaction.

Transactions in bitcoin blockchain system are protected by Digital Competition: any attacker is competing with the whole network.

Let’s see how Mary uses this end of chain ambiguity to perform “double suspension attack”. Mary sends money to John, who sends the product to Mary. Since the node always uses a long chain as the confirmed transaction, if Mary can generate a long chain of reverse transactions with the same reverse reference, John’s funds and products will not be available.

How does blockchain work?
Figure 8 – Mary attack model

How does the system prevent such fraud? Each block contains a reference to the previous block (see Figure 6). In order to extend the following modules to the network, we need to solve a series of mathematical problems. Because it requires a lot of random guesswork to solve a block and put it on the blockchain, it is difficult to pre calculate a series of blocks. Mary is competing with the rest of the network to enable her to put the next block on the chain. Even if she solves the problem ahead of others, it is unlikely to solve two, three or more problems in a row, because every time she needs to compete with the computing of the whole network.

One might wonder, can Mary use super fast computers to generate enough random guesses to compete with the entire network to solve this problem? Yes, it is indeed possible. When she controls the whole retained 50% computing power, she will have a 50% chance to solve one block before other nodes. If she wants to solve two blocks, the probability will be reduced to 20% The more questions you have to answer, the less likely you are to succeed. Therefore, even if a very fast computer is used, due to the large number of users in the network, it is almost impossible to solve several blocks continuously at the exact time.

So, we can see that as time goes on, transactions become more and more secure. For example, a block that was confirmed an hour ago contains more security than a block that was confirmed in the last 10 minutes. Since a block is added to the chain every 10 minutes on average, transactions first included in the block an hour ago are likely to have been processed and the results are irreversible.

How does blockchain work?
Figure 9 – blockchain transaction security

Bitcoin mining

If you want to send bitcoin, you need to have bitcoin transferred to your own wallet so that the process can proceed. This applies to every transaction on the network. So where did bitcoin come from in the first place?

Due to software errors or lost wallet passwords, bitcoin may deflate, so every node that solves block math problems will be rewarded. The activity of running bitcoin blockchain software to get these bitcoin rewards is called “mining”, which is very similar to mining gold.

Rewards are the main driving force of private operation nodes, thus providing the necessary computing power for transaction processing and stabilizing the blockchain network.

Because a typical computer takes a long time to solve a block (about a year on average), the nodes solve the problem together by components, which can greatly improve the efficiency, and then the team members share the awarded bitcoin. These groups work together to solve the problem in the form of a mining pool.

Some of them are very large, accounting for more than 20% of the total computing capacity of the network. As Mary’s attack example above shows, this has a significant impact on network security. Even if one of the potential nodes in these pools gets 50% of the network computing power, the longer the time is, the more secure the transactions will be.

However, some of these mining pools with strong computing power have decided to restrict their members to maintain the overall network security. Due to technological innovation and the increase of the number of nodes, with the passage of time, the computing power of the whole network may increase. Therefore, the blockchain system recalibrates the mathematical difficulty of solving the next block, so that the average target time of the whole network is 10 minutes. This can ensure the stability and overall security of the network.

In addition, mining incentives for blocks are halved every four years, so interest in bitcoin mining (running the network) is reduced over time. Therefore, in order to avoid the node stop mining, the system allows each transaction information to be accompanied by a little reward, and the node can obtain additional benefits.

Because of this mechanism, transactions associated with higher awards are usually processed faster than transactions associated with lower awards. This means that when sending transactions, you can decide whether you want to process them faster (more expensive) or cheaper (more time). Compared with the fees charged by banks, the transaction fees in bitcoin network are very small at present, and have nothing to do with the transaction amount.

Benefits and challenges of blockchain

Based on a general understanding of the working principle of blockchain, let’s take a quick look at why so many people are paying attention to it?

Significant advantages of using blockchain Technology:

  • You have full control over your assets; no third party can hold your assets for you or restrict your access;
  • Whether you are trading from anywhere on the planet, the cost is very low, allowing small payments;
  • The property can be transferred safely within a few minutes;
  • With high transparency, anyone can verify every transaction on the blockchain at any time;
  • Blockchain technology can be used to build decentralized applications that can quickly and securely manage information and transfer value.

However, there are still some challenges to be solved in blockchain at present:

  • Transactions can be sent and received anonymously. This can not only protect the privacy of users, but also allow illegal activities on the network.
    Although there are many trading platforms and digital currency is becoming more and more popular, it is still not easy to trade goods and services with bitcoin;
  • Like many other cryptocurrencies, bitcoin is very volatile: there is not much bitcoin in the market and demand is changing rapidly. The price of bitcoin is unstable, which will be affected by major events or policy releases in the cryptocurrency industry.

    In general, blockchain technology is of innovative significance. There is potential to be explored in advertising, energy distribution and other industries. Its main thrust is decentralization and dependence, which has attracted the attention of many industries.

Original link:

At present, BDS of JD cloud blockchain project has been open-source, and developers are still being recruited to join! Click [read] to learn more

Welcome to “JD cloud” to learn more

How does blockchain work?

The above information is from the Internet, compiled by the official account of Jingdong cloud developer community.
It doesn’t represent JD cloud.
How does blockchain work?