如何将数据写入区块链中,如何将数据写入区块链里
如何将数据写入区块链中,这是一个非常重要的问题。在这里,我们将简要介绍三个与之相关的关键词:智能合约、交易和节点。
智能合约是一种用于管理区块链网络上的交易的代码。它可以定义交易的规则,并自动执行。智能合约可以用来存储和传输数据,以及确保数据的安全性和完整性。它可以用来实现复杂的业务逻辑,如购买和出售,转让资产等。
交易是区块链网络中的基本操作,它是一种用于在区块链上记录数据的方法。交易可以用来在区块链上传输数据,传输资产,或者执行智能合约。交易是由发送者和接收者组成的,它们之间通过一个网络进行通信。
节点是区块链网络中的一种特殊计算机,它可以执行交易,验证数据,并存储区块链中的数据。节点有助于维护区块链的完整性,因为它们可以验证新的交易,并将其写入区块链中。节点也可以用来存储数据,因此它们可以帮助将数据写入区块链中。
总而言之,智能合约、交易和节点是将数据写入区块链的三个关键组件。智能合约可以用来定义交易的规则,交易可以用来传输数据,而节点可以用来验证交易,并将数据写入区块链中。通过使用这三个关键组件,可以将数据安全地写入区块链中。
请查看相关英文文档
⑴ What is the storage method of data in blockchain technology?
Simply speaking, the data storage of blockchain is through the block through the formula algorithm process. Officially included in the blockchain for storage, all nodes in the network have expressed acceptance of the block, and the way to express acceptance is to use the random hash value of the block as the latest block hash value. Extended based on blockchain.
⑵ Tutorials for getting started with blockchain
However, there are very few simple and easy-to-understand introductory articles. What exactly blockchain is and what makes it special is rarely explained.
Next, I will try to write a best-understood blockchain tutorial. After all, it is not difficult. The core concept is very simple and can be explained clearly in a few sentences. I hope that after reading this article, you will not only understand the blockchain, but also understand what mining is, why mining is getting more and more difficult, and other issues.
It should be noted that I am not an expert in this area. Although I have been paying attention to it for a long time, my detailed understanding of blockchain started at the beginning of this year. You are welcome to correct any errors or inaccuracies in the article.
1. The essence of blockchain
What is blockchain? In a word, it is a special distributed database.
First of all, the main function of blockchain is to store information. Any information that needs to be saved can be written to the blockchain and read from it, so it is a database.
Secondly, anyone can set up a server, join the blockchain network, and become a node. In the world of blockchain, there is no central node. Every node is equal and stores the entire database. You can write/read data to any node, because all nodes will eventually be synchronized to ensure that the blockchain is consistent.
2. The biggest features of blockchain
Distributed databases are not a new invention, and there have been such products on the market for a long time. However, blockchain has a revolutionary feature.
Blockchain has no administrator, it is completely centerless. Other databases have administrators, but blockchain does not. If one wanted to add auditing to the blockchain, it would not be possible because it is designed to prevent the emergence of a central authority.
It is precisely because it is unmanageable that blockchain can be uncontrollable. Otherwise, once big companies and large groups control the management, they will control the entire platform, and other users will have to take orders from them.
However, without the administrator, everyone can write data into it. How can we ensure that the data is trustworthy? What should I do if it is modified by bad people? Please read on, this is the wonderful thing about blockchain place.
3. Block
Blockchain is composed of blocks. Blocks are much like database records. Every time data is written, a block is created.
Each block contains two parts.
Head: records the characteristic values of the current block
Body: actual data
The block header contains multiple characteristic values of the current block.
Generation time
Hash of the actual data (i.e. block body)
Hash of the previous block
...
Here, you need to understand what a hash is , which is necessary to understand blockchain.
The so-called hashing means that the computer can calculate a characteristic value of the same length for any content. The hash length of the blockchain is 256 bits, which means that no matter what the original content is, a 256-bit binary number will be calculated in the end. And it can be guaranteed that as long as the original content is different, the corresponding hash must be different.
For example, the hash of the string 123 is (hexadecimal), which is 256 bits when converted to binary, and only 123 can get this hash. (Theoretically, it is possible for other strings to get this hash, but the probability is extremely low and can be approximated as impossible.)
Therefore, there are two important inferences.
Corollary 1: The hash of each block is different, and the block can be identified by the hash.
Corollary 2: If the content of the block changes, its hash will definitely change.
4. The non-modifiable nature of Hash
Blocks and hashes have a one-to-one correspondence, and the hash of each block is calculated based on the block header (Head). That is to say, the characteristic values of the block header are connected together in order to form a very long string, and then the hash is calculated on this string.
Hash = SHA256 (block header)
The above is the calculation formula of block hash. SHA256 is the hash algorithm of the blockchain. Note that this formula only contains the block header and not the block body. In other words, the hash is uniquely determined by the block header.
As mentioned before, the block header contains a lot of content, including the hash of the current block body. , and the hash of the previous block. This means that if the content of the current block body changes, or the hash of the previous block changes, it will definitely cause the hash of the current block to change.
This point has great significance for blockchain. If someone modifies a block, the hash of the block changes. In order for subsequent blocks to still be connected to it (because the next block contains the hash of the previous block), the person must modify all subsequent blocks in sequence, otherwise the modified block will be removed from the blockchain . Due to the reasons mentioned later, hash calculation is very time-consuming, and it is almost impossible to modify multiple blocks in a short period of time, unless someone controls more than 51% of the computing power of the entire network.
It is through this linkage mechanism that the blockchain ensures its own reliability. Once the data is written, it cannot be tampered with. This is just like history, what happened happened, and it can’t be changed from now on.
Each block is connected to the previous block, which is where the name blockchain comes from.
5. Mining
Since synchronization between nodes must be ensured, the adding speed of new blocks cannot be too fast. Just imagine, you have just synchronized a block and are preparing to generate the next block based on it, but at this time other nodes haveWhen a new block is generated, you have to give up half of the calculations and synchronize again. Because each block can only be followed by one block, you can only generate the next block after the latest block. So, you have no choice but to sync as soon as you hear the signal.
So, the inventor of the blockchain, Satoshi Nakamoto (this is a pseudonym, and his true identity is still unknown) deliberately made it difficult to add new blocks. His design is that on average, the entire network can generate a new block every 10 minutes, which is only six per hour.
This output speed is not achieved through commands, but by deliberately setting up massive calculations. In other words, only through an extremely large amount of calculations can the effective hash of the current block be obtained and the new block added to the blockchain. Because the amount of calculation is too large, it cannot be done quickly.
This process is called mining, because the difficulty of calculating a valid hash is like finding a grain of sand that meets the conditions among the sand in the world. The machine that calculates hashes is called a mining machine, and the person who operates the mining machine is called a miner.
6. Difficulty coefficient
After reading this, you may have a question. People say that mining is difficult, but isn’t mining just about using a computer to calculate a hash? This is the strength of computers. How could it be? It becomes very difficult, why can’t it be calculated?
It turns out that not just any hash can be used, only hashes that meet the conditions will be accepted by the blockchain. This condition is particularly harsh, causing most hashes to fail to meet the requirements and must be recalculated.
It turns out that the block header contains a difficulty coefficient (difficulty), which determines the difficulty of calculating the hash. For example, the difficulty coefficient of the 100,000th block is 14484.16236122.
The blockchain protocol stipulates that the target value (target) can be obtained by dividing the difficulty coefficient by a constant. Obviously, the greater the difficulty coefficient, the smaller the target value.
The validity of the hash is closely related to the target value. Only hashes smaller than the target value are valid, otherwise the hash is invalid and must be recalculated. Since the target value is very small, the chance that the hash is smaller than this value is extremely slim, and it may be calculated 1 billion times before it is considered a hit. This is the fundamental reason why mining is so slow.
As mentioned earlier, the hash of the current block is uniquely determined by the block header. If the hash of the same block needs to be calculated repeatedly, it means that the block header must keep changing, otherwise it is impossible to calculate different hashes. All feature values in the block header are fixed. In order to make the block header change, Satoshi Nakamoto deliberately added a random item called Nonce.
Nonce is a random value. The role of the miner is actually to guess the value of Nonce so that the hash of the block header can be smaller than the target value so that it can be written to the blockchain. Nonce is very difficult to guess. At present, we can only use trial and error one by one through exhaustive methods. According to the protocol, Nonce is a 32-bit binary value, which can reach a maximum of 2.147 billion. N for the 100000th blockThe once value is 274148111. It can be understood that the miner started from 0 and calculated 274 million times before obtaining a valid Nonce value, so that the calculated hash can meet the conditions.
If you are lucky, you may find Nonce in a while. If you are unlucky, you may have calculated it 2.147 billion times without finding the Nonce, that is, it is impossible to calculate a hash that meets the conditions for the current block body. At this time, the protocol allows miners to change the block body and start a new calculation.
7. Dynamic adjustment of difficulty coefficient
As mentioned in the previous section, mining is random, and there is no guarantee that a block will be produced in exactly ten minutes. Sometimes it can be calculated in one minute, and sometimes it may take several hours. No result. Overall, with the improvement of hardware equipment and the increase in the number of mining machines, the computing speed will definitely become faster and faster.
In order to keep the output rate constant at ten minutes, Satoshi Nakamoto also designed a dynamic adjustment mechanism for the difficulty coefficient. He stipulated that the difficulty factor should be adjusted every two weeks (2016 blocks). If the average block generation speed in these two weeks is 9 minutes, it means that it is 10% faster than the legal speed, so the next difficulty factor will be increased by 10%; if the average block generation speed is 11 minutes, it means It is 10% slower than the legal speed, so the difficulty factor of the next step must be lowered by 10%.
The difficulty coefficient is adjusted higher and higher (the target value is getting smaller and smaller), which makes mining more and more difficult.
8. Forks of the blockchain
Even if the blockchain is reliable, there is still an unresolved problem: if two people write data to the blockchain at the same time, that is to say, two people write data to the blockchain at the same time. Blocks join because they are connected to the previous block, forming a fork. Which block should be adopted at this time?
The current rule is that new nodes always adopt the longest blockchain. If there is a fork in the blockchain, it will look at which branch is behind the fork to reach 6 new blocks first (called six confirmations). Based on a block calculation of 10 minutes, it can be confirmed in one hour.
Since the generation speed of new blocks is determined by computing power, this rule means that the branch with the most computing power is the authentic blockchain.
9. Summary
Blockchain, as an unmanaged distributed database, has been running for 8 years since 2009 without major problems. This proves it works.
However, in order to ensure the reliability of data, blockchain also has its own price. The first is efficiency. You have to wait at least ten minutes to write data to the blockchain. All nodes synchronize the data, which requires more time. The second is energy consumption. The generation of blocks requires miners to perform countless meaningless calculations. This is Very energy consuming.
Therefore, the applicable scenarios of blockchain are actually very limited.
There is no management authority that all members trust
The written data does not require real-time use
The benefits of mining can make up for its own costs
If the above conditions cannot be met, then the traditional database is betters solution.
Currently, the largest application scenario (and possibly the only application scenario) of blockchain is the cryptocurrency represented by Bitcoin.
⑶ What technology does the blockchain apply to achieve this function?
The blockchain applies the following technologies to achieve it
The first is the consensus mechanism, which is commonly used Consensus mechanisms mainly include PoW, PoS, DPoS, PBFT, PAXOS, etc. Since there is no center in the blockchain system, there needs to be a preset rule to guide the nodes of all parties to reach an agreement on data processing. All data interactions must be carried out in accordance with strict rules and consensus;
The second It is cryptography technology. Cryptography technology is one of the core technologies of blockchain. Many classic algorithms of modern cryptography are used in current blockchain applications, mainly including: hash algorithm, symmetric encryption, asymmetric encryption, digital Signature etc.
The third type is distributed storage. Blockchain is a distributed ledger on a peer-to-peer network. Each participating node will independently and completely store and write block data information. The advantages of distributed storage compared with traditional centralized storage are mainly reflected in two aspects: data information is backed up on each node to avoid data loss due to single point failure; data on each node is stored independently, effectively avoiding Malicious tampering with historical data.
Smart contracts: Smart contracts allow trusted transactions without a third party. As long as one party reaches the pre-set goals of the agreement, the contract will automatically execute the transaction. These transactions are trackable and irreversible. It has the advantages of transparency, credibility, automatic execution, and mandatory performance. Blockchain technology has many unique features that make it a unique invention and give it unlimited horizons to explore.
⑷ How a piece of data is uploaded to the chain
The blockchain processing process is roughly divided into three stages:
They are the pre-chain processing stage, the on-chain processing phase and smart contract processing phase.
Processing stage before uploading to the chain
Before the business data is uploaded to the chain, the business data needs to be processed and the information must be signed. These processes can be completed through corresponding tools, such as serialization tools and various elliptic curve signature tools, but more often it is completed through SDKs that integrate various tools. Ethereum's web3 is a typical on-chain Pre-processing development tools.
On-chain processing stage
After the processed data is sent to the blockchain node, a blockchain transaction is formed and enters the on-chain processing stage. On-chain processing It can be roughly divided into transaction broadcast and block consensus processes.
Smart contract processing stage
After the on-chain processing is completed, the business data has been recorded on the chain. For businesses that simply store certificates, writing business information into the block has been completed. After completing this business processing, you only need to record the transaction hash of the certificate deposit business and query it through the transaction hash when withdrawing it. But most business scenarios require certain logicProcessing, therefore processing via smart contracts is a must. Smart contract processing includes processes such as contract logic processing and modifying the state Merkel tree.
The Xueshuo Innovation Blockchain Technology Workstation under Lianqiao Education Online is the only approved "Smart Learning Factory 2020- Xueshuo Innovation Workstation" launched by the School Planning and Construction Development Center of the Ministry of Education of China. "Blockchain Technology Professional" pilot workstation. The professional position is based on providing students with diversified growth paths, promoting the reform of the training model integrating professional degree research, production, and research, and building an applied and compound talent training system.
⑸ How to apply blockchain technology to actual scenarios
Blockchains are divided into public chains and private chains, but in fact, using public chains and permissioned chains It is more appropriate to distinguish between concepts.
Everyone can join the public chain. In order to attract more people to join, it also has a certain incentive mechanism. This incentive mechanism is based on the consensus algorithm of the blockchain and uses Bitcoin as the unit of account. In order to make incentives more effective, methods such as anchoring to legal currency are usually adopted to make the accounting unit valuable. As time goes by, a certain degree of credibility has been accumulated, and businesses such as certificate storage and payment can be performed on the public chain.
Permissioned chains include what are commonly known as private chains and consortium chains.
It is more appropriate to use "proprietary chain" to refer to "private chain". Proprietary chains are usually used within a large company or group. In many-to-many reporting systems and reconciliation systems, management costs are very high. Using blockchain technology to achieve information sharing and more effective supervision can greatly reduce costs and improve efficiency.
A consortium chain is often a blockchain jointly constructed by several enterprises. These enterprises are already related, either upstream and downstream, Party A and Party B, or horizontal interconnection cooperation. In order to reduce costs and improve efficiency, using blockchain technology.
To a certain extent, the consortium chain and the proprietary chain are essentially the same. Both require participants to obtain technical permission to join, and all parties are also restricted, so they are called Permission chain. Because there is a correlation in the original economic activities, there is no need for an incentive mechanism on the blockchain, and tokens may not necessarily be generated in actual applications.
2. How to protect privacy on the blockchain where information is disclosed?
In the application of blockchain, information is transparent and shared, so how to solve the problem of privacy protection? How to deal with the relationship between supervision, checks and balances and privacy protection?
In fact, the sharing of information on the blockchain is selective sharing, and transparency is also limited transparency. Not all data will be written to the blockchain, only those that require supervision and sharing. In addition, not all data that has been written to the blockchain is transparent. Those opaque data may require authorization to see. These are achieved through encryption means.
3. How to ensure processing efficiency while data is growing rapidly?
With the growth of data, the database is getting larger and larger, which not only increases the storage burden, but also affects the processing effect at any time. How to deal with the contradiction between efficiency and data growth is a matter of great concern to people. .
And because there are many parties involved in the blockchain, some people may think that its data processing efficiency must be lower than that of a centralized network.
In fact, this depends on actual needs. In most cases, blockchain can meet efficiency needs.
The two major links that affect processing efficiency are the verification mechanism, which verifies the authenticity of each packaged block; and the second is the consensus algorithm. This second step takes the most time.
For permissioned chains, there are many ways to improve efficiency. For example, the POW algorithm is not used in the verification mechanism, but the verification pool algorithm is used. Which data is written to the blockchain, which data is transparent, and which is supervised can all be determined based on the actual situation.
4. Real application requirements of blockchain.
In various current discussions, people have imagined various blockchain application needs. But many of them are not real needs.
Whether a blockchain application demand is real or not, it is easy to judge whether the blockchain application solves actual problems - whether it can reduce costs and improve efficiency, rather than for the sake of application. Blockchain and application of blockchain.
According to customer feedback, in the actual application of blockchain, the most important thing is security and controllability - consensus algorithm, hierarchical authorization, and joint signature can only be applied under the premise of controllability. A further requirement is to be able to perform high-performance processing, including transaction processing efficiency and read efficiency of storage structures. The third is private key and privacy management. It is of course necessary to have both transparent supervision and privacy protection. The fourth is the built-in simple contract. The reason why it is a simple contract rather than a smart contract is also due to controllability considerations. As intelligence increases, controllability will inevitably decrease. Finally, scalability means supporting multiple types of transactions and certificate deposits, supporting massive data and large-scale users. For a better user experience, there must also be fast development and visualization tools, which is essential for the popularization of blockchain applications. is very critical.
⑹ What is the concept of blockchain
Literally understood, blockchain includes two concepts: block and chain. The blockchain itself is composed of blocks, and the network built by linking different nodes together is the blockchain. The main function of the blockchain is to store information. Any information that needs to be saved can be written to the blockchain or read from it.
Each block stores: some valid records or transactions; information involving the block; links to the previous block and the next block through the hash of each block -A unique code that can be thought of as a fingerprint of a block.
Each block therefore has a specific and immovable position within the chain, as each block contains information from the hash of the previous block. The entire chain is stored in every network node that makes up the blockchain, so an exact copy of the chain is stored among all network participants.
Uses
Essentially, blockchain can be used to store any type of information that must remain intact and be more secure than going through a middleman. , decentralized and cheaper ways remain available. Additionally, since the stored information is encrypted, its confidentiality is guaranteed as only those with the encryption key can access it.
Using blockchain in healthcare. For example, health records can be consolidated and stored on the blockchain. This means that every patient's medical history is secure and, at the same time, available to every authorized doctor, regardless of the health center where the patient received treatment. Even the pharmaceutical industry could use this technology to authenticate medicines and prevent counterfeiting.
Blockchain is also very useful for managing digital assets and documents. The problem with digitization so far has been that everything is easily copied, but Blockchain allows you to record purchases, deeds, documents or any other type of online asset without it being counterfeited.