Disclosure of Invention
The application provides a data storage method and device applied to a block chain, and solves the problem that in the prior art, the storage read-write performance of the whole block chain network is poor due to the fact that the block chain network runs for a long time.
In a first aspect, the present application provides a data storage method applied to a block chain, including: after the block data consensus succeeds, if the target node is determined not to be a node for storing the block data in the block data, storing block head data in the block data but not storing the block data in the block data; the target node is any node in the block chain network; the block data comprises the block volume data and the block header data; and the nodes for storing the block data in the block chain network are partial nodes in the block chain network.
In the method, after the block data is successfully identified, the target node does not directly store the block data and the block header data in the target node, but determines whether the target node is the block data in the block data, and only stores the block header data in the block data if the target node is not the node for storing the block data, so that all data of the block data is not required to be stored.
In an alternative embodiment, the determining that the target node is not storing block data in the block data includes: the target node takes the data identification of the block data as the input of the node selection strategy, and determines a node set storing the block data in a block chain network according to the node selection strategy; and if the target node determines that the target node is not in the node set, determining that the target node is not the region block data in the storage of the region block data.
In the method, the target node uses the data identifier as an input of the node selection policy, determines a node set according to the node selection policy, and further determines that the target node is not a node for storing block data in the block data when the target node is determined not to be in the node set, so that a method for determining the node for storing the block data in the block chain network is provided without data interaction with the outside.
In an alternative embodiment, the node selection policy includes a number of nodes in the blockchain network that store the blocky data; the number range of the nodes is determined by a consensus algorithm of the block chain network; if the consensus algorithm is a practical Byzantine Fault tolerant PBFT consensus algorithm, then the number range is
Wherein M is the blockchain networkThe total number of the middle nodes is,
represents a lower rounding operation; or, if the consensus algorithm is the working evidence POW consensus algorithm, the number range is
In the method, under different consensus algorithms, the block volume data is stored by nodes with a certain number of nodes in the block chain network, so that the block volume data stored by at least one node in the node set of the target node is valid data,
the above are that
The nodes in the range store the block data, the block data stored in at least one node is guaranteed to be valid data, under the POW consensus algorithm,
the above are that
The nodes in the range store the block volume data, and the block volume data stored in at least one node is guaranteed to be valid data, so that under different consensus algorithms, the block volume data is stored in the nodes in the range, the correctness of data storage can be guaranteed, and the storage space of the data can be saved.
In an optional embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes is
Or, if the consensus algorithm is a POW consensus algorithm, the number of the nodes is
In the mode, the storage space of the data is saved to the greatest extent on the basis of ensuring the correctness of the data storage under different consensus algorithms.
In an alternative embodiment, the node selection policy is a consistent node selection algorithm based on the number of nodes; the target node takes the data identifier of the block data as the input of the node selection policy, and determines the node set storing the block data in the block chain network according to the node selection policy, including: the target node takes the data identification as the input of the consistency node selection algorithm, and takes the nodes with the number of the nodes selected in the block chain network according to the consistency node selection algorithm as the node set of the target node; the consistent node selection algorithm is executed at each node in the blockchain network, so that node sets determined by each node in the blockchain network according to the node selection strategy are consistent.
In the above manner, the node selection policy is a consistent node selection algorithm based on the number of nodes; the consistency node selection algorithm is executed at each node in the block chain network, so that node sets determined by each node in the block chain network according to the node selection strategy are consistent, and therefore, the storage information of block storage data in each node in the block chain network can be guaranteed to be uniform.
In a second aspect, the present application provides a data acquisition method applied to a blockchain, including: a target node acquires a query request; the query request is used for acquiring a block to be queried; the block data to be inquired comprises block body data and block head data; if the target node determines that the block data is not stored in the target node, determining a node storing the block data in the block chain network; wherein, the nodes for storing the block data in the block chain network are partial nodes in the block chain network; the target node is any node in the block chain network; and the target node sends an acquisition request to a node storing the block volume data in the block chain network, so as to acquire the block volume data returned from the node.
In the method, after the target node acquires the query request, if it is determined that the block data is not stored in the target node, the node storing the block data in the block chain network is determined, and the acquisition request is sent, so that the block data returned from the node is acquired.
In an optional implementation manner, after the target node sends an acquisition request to a node in the blockchain network that stores the blockchain data, the method further includes: the target node acquires the data of the undetermined block volume returned by the node; the target node determines a hash value of head data of the undetermined block according to the undetermined block data; and if the target node determines that the hash value of the undetermined block head is consistent with the hash value of the block head data, determining that the undetermined block data acquired and returned from the node is the block data, and acquiring the returned block data from the node.
In a third aspect, the present application provides a data storage device applied to a blockchain, including: the determining module is used for determining whether the target node is a node for storing the block data in the block data after the block data is successfully identified; a storage module, configured to store block header data in the block data but not store block body data in the block data if the determination module determines that the target node is not a node for storing block body data in the block data; the target node is any node in the block chain network; the block data comprises the block volume data and the block header data; and the nodes for storing the block data in the block chain network are partial nodes in the block chain network.
In an optional implementation manner, the determining module is specifically configured to: taking the data identification of the block data as the input of the node selection strategy, and determining a node set storing the block data in a block chain network according to the node selection strategy; and if the target node is determined not to be in the node set, determining that the target node is not a node for storing block data in the block data.
In an alternative embodiment, the node selection policy includes a number of nodes in the blockchain network that store the blocky data; the number range of the nodes is determined by a consensus algorithm of the block chain network; if the consensus algorithm is a practical Byzantine Fault tolerant PBFT consensus algorithm, then the number range is
Wherein M is the total number of nodes in the blockchain network,
represents a lower rounding operation; or, if the consensus algorithm is the working evidence POW consensus algorithm, the number range is
In an optional embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes is
Or, if the consensus algorithm is a POW consensus algorithm, the number of the nodes is
In an alternative embodiment, the node selection policy is a consistent node selection algorithm based on the number of nodes; the determining module is specifically configured to: the target node takes the data identification as the input of the consistency node selection algorithm, and takes the nodes with the number of the nodes selected in the block chain network according to the consistency node selection algorithm as the node set of the target node; the consistent node selection algorithm is executed at each node in the blockchain network, so that node sets determined by each node in the blockchain network according to the node selection strategy are consistent.
For the advantages of the third aspect and the embodiments of the third aspect, reference may be made to the advantages of the first aspect and the embodiments of the first aspect, which are not described herein again.
In a fourth aspect, the present application provides a data storage device applied to a block chain, including: the acquisition module is used for acquiring the query request; the query request is used for acquiring a block to be queried; the block data to be inquired comprises block body data and block head data; the processing module is used for determining a node storing the block volume data in the block chain network if it is determined that the block volume data is not stored in the target node; wherein, the nodes for storing the block data in the block chain network are partial nodes in the block chain network; the target node is any node in the block chain network; and sending an acquisition request to a node storing the block volume data in the block chain network, so that the acquisition module acquires the block volume data returned from the node.
In an optional implementation, the obtaining module is further configured to: acquiring data of the undetermined block volume returned by the node; the processing module is further configured to: determining a hash value of head data of the undetermined block according to the undetermined block volume data; and if the Hash value of the undetermined block head is consistent with the Hash value of the block head data, determining that the undetermined block data acquired and returned from the node is the block data, and acquiring the returned block data from the node.
The advantageous effects of the embodiments of the fourth aspect and the fourth aspect may refer to the advantageous effects of the embodiments of the second aspect and the second aspect, and are not described herein again.
In a fifth aspect, the present application provides a computer device comprising a program or instructions for performing the method of the first or second aspect and embodiments described above when the program or instructions are executed.
In a sixth aspect, the present application provides a storage medium comprising a program or instructions which, when executed, is adapted to perform the method of the embodiments of the first or second aspect.
Detailed Description
In order to better understand the technical solutions, the technical solutions will be described in detail below with reference to the drawings and the specific embodiments of the specification, and it should be understood that the specific features in the embodiments and examples of the present application are detailed descriptions of the technical solutions of the present application, but not limitations of the technical solutions of the present application, and the technical features in the embodiments and examples of the present application may be combined with each other without conflict.
The terms appearing in the embodiments of the present application are explained first below.
Block chains: as shown in fig. 1, a block chain is a chain consisting of a series of blocks, and each block records a Hash value of the block in addition to the data of the block, so that a chain is formed. The block chain has two core ideas, one is a cryptography technology, the other is a decentralization idea, and based on the two ideas, historical information on the block chain cannot be tampered.
And (3) node: each participant in the network is a node that participates in network set-up and data exchange. In a blockchain network, a node refers to a participant with a unique identity, and the node has a complete copy of the ledger and has the capability of participating in the consensus and ledger maintenance of the blockchain network.
Intelligent contract: an intelligent contract is a computer protocol intended to propagate, validate or execute contracts in an informational manner. Smart contracts allow trusted transactions to be conducted without third parties, which transactions are traceable and irreversible. The specific form of the intelligent contract is a code which is deployed on a block chain and completes a specific function. For example, identity is a mainstream intelligent contract programming language, and an intelligent contract written in the identity language is called an identity contract. When an intelligent contract is deployed on a blockchain, a contract address is generated, and a user can call the intelligent contract through the contract address. The function defined in the intelligent contract is called a contract interface, and the calling of the intelligent contract is to call a certain contract interface in the contract through a contract address.
Trading: a transaction is a user request for an operation of an intelligent contract interface deployed on a blockchain. The transaction is initiated by the user, sent from the client of the user to the block chain node, and after receiving the transaction, the block chain node calls the corresponding intelligent contract according to the contract address and the interface specified by the transaction. In blockchains, any operation (deploying a contract, invoking a contract interface, etc.) is done by sending a transaction. The transaction is initiated by the user and sent to the blockchain node through the client. After receiving the transaction, the blockchain node packs the transaction into blocks and executes the blocks.
Each transaction in the block is executed to generate a corresponding transaction receipt. The transaction receipt stores the execution result of the transaction, and typically contains important information such as a transaction hash, a status bit identifying whether the transaction was executed successfully, and a log generated during the execution.
As shown in fig. 2, a data storage method applied to a block chain is provided for the embodiment of the present application.
Step 201: and after the block data consensus is successful, the target node determines whether the target node is a node for storing the block data in the block data.
Step 202: and if the target node is determined not to be the node for storing the block data in the block data, storing the block head data in the block data but not storing the block data in the block data.
In step 201 to step 202, the target node is any node in the blockchain network; the block data comprises the block volume data and the block header data; and the nodes for storing the block data in the block chain network are partial nodes in the block chain network. Thus, the storage of the target node may be: a block head and a block body of the first block; a block head of the second block; a block head of block three; block head and block body of block four.
In an optional implementation manner of step 201, determining that the target node is not storing chunk data in the chunk data may be as follows:
the target node takes the data identification of the block data as the input of the node selection strategy, and determines a node set storing the block data in a block chain network according to the node selection strategy; and if the target node determines that the target node is not in the node set, determining that the target node is not the region block data in the storage of the region block data.
It should be noted that the data identifier of the block data may be various, as long as the block data can be uniquely identified. For example, the data identification of the chunk data is a transaction hash of the first transaction in the chunk data. The node selection policy is not limited, and any node that is finally selected can be obtained according to the node selection policy as long as the node is part of nodes in the blockchain network.
The above process includes, for example, node one, node two, node three, and node four in the blockchain network. And when the first node is not in the node set, determining that the first node is not the block data in the block data.
In an alternative embodiment, the node selection policy is a consistent node selection algorithm based on the number of nodes; the target node uses the data identifier of the block data as an input of the node selection policy, and a specific process of determining a node set storing the block data in the block chain network according to the node selection policy may be:
the target node takes the data identification as the input of the consistency node selection algorithm, and takes the nodes with the number of the nodes selected in the block chain network according to the consistency node selection algorithm as the node set of the target node; the consistent node selection algorithm is executed at each node in the blockchain network, so that node sets determined by each node in the blockchain network according to the node selection strategy are consistent.
The above process includes, for example, node one, node two, node three, and node four in the blockchain network. After the first node, the second node, the third node and the fourth node know the block data successfully, the transaction hash of the first transaction of the block data is used as the input of a node selection strategy, and the node sets obtained by the first node, the second node, the third node and the fourth node are the second node and the third node.
The consistency node selection algorithm may be any one of the following algorithms: a node polling algorithm; a node weight polling algorithm; a node identifier sorting algorithm; the random function VRF algorithm may be verified.
It should be noted that the node selection policy may include the number of nodes storing the block data in the block chain network; the number range of the nodes is determined by a consensus algorithm of the blockchain network, in particular:
if the consensus algorithm is a practical Byzantine Fault tolerant PBFT consensus algorithm, then the number range is
Wherein M is the total number of nodes in the blockchain network,
represents a lower rounding operation; or, if the consensus algorithm is the working evidence POW consensus algorithm, the number range is
For example, when M is 10, then for the PBFT consensus algorithm, the number range is [4, 10); for the POW consensus algorithm, the number range is [6,10 ].
Under different consensus algorithms, the block volume data is stored by nodes with more than a certain number of nodes in the block chain network, so that the block volume data stored by at least one node in the node set of the target node is valid data. That is, the number range may be determined according to the consensus algorithm; any number in the range of numbers is such that at least one node in the blockchain network stores a value that the blockvolume data is real data. Under the PBFT consensus algorithm,
the above are that
The nodes in the range store the block data, the block data stored in at least one node is guaranteed to be valid data, under the POW consensus algorithm,
the above are that
The nodes in the range store the block volume data, and the block volume data stored in at least one node is guaranteed to be valid data, so that under different consensus algorithms, the block volume data is stored in the nodes in the range, the correctness of data storage can be guaranteed, and the storage space of the data can be saved.
In a more specific optional implementation manner, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes is
Or, if the consensus algorithm is a POW consensus algorithm, the number of the nodes is
For example, when M is 20, for the PBFT consensus algorithm, the number of nodes is 7; for the POW consensus algorithm, the number of nodes is 11.
In the mode, the storage space of the data is saved to the greatest extent on the basis of ensuring the correctness of the data storage under different consensus algorithms.
In the method of step 201 to step 202, after the block data is successfully identified, the target node does not directly store the block data and the block header data in the target node, but first determines whether the target node is a node for storing the block data in the block data, and if not, only stores the block header data in the block data, so that all data of the block data is not needed to be stored.
After the data storage method of steps 201 to 202 is performed, since each block data is only stored in a part of nodes in the blockchain network, when a node is requested for a block data, the node may not have all block data, but only block volume data.
Therefore, the application provides a data acquisition method applied to a block chain, and specifically comprises the following steps:
the target node obtains the query request.
The query request is used for acquiring a block to be queried. The block data to be inquired comprises block volume data and block head data.
And if the target node determines that the block data is not stored in the target node, determining a node storing the block data in the block chain network. Wherein, the nodes for storing the block data in the block chain network are partial nodes in the block chain network; the target node is any node in the blockchain network.
And the target node sends an acquisition request to a node storing the block volume data in the block chain network, so as to acquire the block volume data returned from the node.
It should be noted that, when the target node selects the node of the storage block data by using a node selection policy during storage, and the node selection policy includes the number of nodes storing the block data in the block chain network, where the number range of the nodes is determined by a consensus algorithm of the block chain network. When the number of nodes of the block data in the storage area is more, the space for the nodes to communicate with each other is smaller, and when the number of the nodes is more than a certain storage threshold value, at least one node can be ensuredThe stored block volume data is available efficiently. The number range of the specific number of nodes can be determined according to the consensus algorithm of the blockchain network, so that the block volume data stored by at least one node is effectively available. If the consensus algorithm is a practical Byzantine Fault tolerant PBFT consensus algorithm, then the number range is
![Figure BDA0002388889040000121](https://patentimages.storage.***apis.com/c9/b7/d8/e2334b94d8ec30/BDA0002388889040000121.png)
Wherein M is the total number of nodes in the blockchain network,
represents a lower rounding operation; or, if the consensus algorithm is the working evidence POW consensus algorithm, the number range is
When the target node queries the block data, the data of the pending block fed back by at least one node is available.
In the method, after the target node acquires the query request, if it is determined that the block data is not stored in the target node, the node storing the block data in the block chain network is determined, and the acquisition request is sent, so that the block data returned from the node is acquired.
In an optional implementation manner, after the target node sends an acquisition request to a node in the blockchain network that stores the blockchain data, the method further includes: the target node acquires the data of the undetermined block volume returned by the node; the target node determines a hash value of head data of the undetermined block according to the undetermined block data; and if the target node determines that the hash value of the undetermined block head is consistent with the hash value of the block head data, determining that the undetermined block data acquired and returned from the node is the block data, and acquiring the returned block data from the node.
When a target node selects a node storing block data by adopting a node selection strategy during storage, and the node selection strategy comprises the number of nodes storing the block data in the block chain network, as the undetermined block data returned by at least one node is correct and effective, under the method, the target node can obtain at least one undetermined block data, so that the hash value of the head of the undetermined block is consistent with the hash value of the head data of the block, and the returned block data is obtained from the node.
A data storage method applied to a blockchain according to the present application is described in detail below with reference to fig. 3 and 4. After the nodes receive the block data which are identified in common, the common identification is achieved through the interface module, the transaction management module, the execution of the intelligent contract and the multiple nodes, and the accounting book module stores the blocks of each node. The node executes the transaction in the block, completes the update of the state data, and the ledger module stores the state data. After the account book module receives the block, the block data is split into block body data and block head data, and the storage block head data is stored. And judging whether the current node stores the block data according to the node selection strategy by the account book module. If so, the block data is stored. Otherwise, the block data is not stored. After the query module receives the query request, the node where the block data is located is obtained through a consistent node selection algorithm according to the queried block number, and the node containing the block is queried.
Specifically, the node selection strategy comprises two steps: node number selection and specific node selection. Wherein the number of nodes is different according to different consensus algorithms. For the PBFT-like consensus algorithm, if the number of nodes is 3f +1 (f is a positive integer greater than or equal to 1), the key field of the block data, then at least f +1 nodes are required for storing the block data in each newly generated block. f represents the number of Byzantine nodes, and the POW type consensus algorithm is adopted, if the number of the nodes is 2f +1 (f is a positive integer greater than or equal to 1), at least f +1 nodes are required for storing block data in each newly generated block. Other consensus algorithms also require that the number of stored nodes is at least greater than the number of byzantine nodes.
As shown in fig. 4, the complete flow includes the following steps:
step 401: the target node shares the block data.
After the target node formula block data is successful, step 402 is performed.
Step 402: the target node stores state data in the chunk data.
Step 403: and splitting the block head data and the block body data in the block data by the target node, and storing the block head data.
Step 404: and the target node obtains a node set of the storage block according to a consistency algorithm.
Step 405: it is determined whether the target node is in the set of nodes.
If yes, go to step 406, otherwise go to step 407.
Step 406: block volume data in the block data is stored.
Step 407: and discarding the block data in the block data.
The following describes the data storage method shown in fig. 1 in detail with reference to the scenario shown in fig. 5, and a PBFT consensus algorithm is used through a block chain system, where the total number of nodes is 4, and the nodes are a, B, C, and D, respectively. Wherein the D node is a byzantine node.
The node B initiates a transaction to be completed.
For each of A, B, C and D nodes, performing the steps of:
the ledger module stores block header data and status data. The account book module determines and selects at least F +1 nodes according to a consensus algorithm to store data, wherein F is 1, namely two nodes store full data. And the account book module selects specific A nodes and C nodes to store block data through a VRF random selection algorithm according to the current block height and the HASH of the block head.
Entering the next round of consensus.
When a certain node needs to inquire data in a block body, the node where the block is located is preferentially determined through a VRF verifiable algorithm according to the block number, if the node is the current node, a local database is inquired, and if the node is not the current node, block data is requested from the corresponding node. And after the data is acquired, verifying the hash of the block and the hash of the head of the local block. If the data are consistent, the data are correct, otherwise, the data are wrong, and the next node is requested to acquire the data. And returning data errors if the data are inconsistent until the consistent data are acquired.
As shown in fig. 6, the present application provides a data storage device applied to a block chain, including: a determining module 601, configured to determine whether a target node is a node for storing chunk data in the chunk data after the chunk data consensus is successful; a storage module 602, configured to store block header data in the block data but not store block volume data in the block data if the determining module 601 determines that the target node is not a node for storing block volume data in the block data; the target node is any node in the block chain network; the block data comprises the block volume data and the block header data; and the nodes for storing the block data in the block chain network are partial nodes in the block chain network.
In an optional implementation manner, the determining module 601 is specifically configured to: taking the data identification of the block data as the input of the node selection strategy, and determining a node set storing the block data in a block chain network according to the node selection strategy; and if the target node is determined not to be in the node set, determining that the target node is not a node for storing block data in the block data.
In an alternative embodiment, the node selection policy includes a number of nodes in the blockchain network that store the blocky data; the number range of the nodes is determined by a consensus algorithm of the block chain network; if the consensus algorithm is a practical Byzantine Fault tolerant PBFT consensus algorithm, then the number range is
Wherein M is as defined aboveThe total number of nodes in the blockchain network,
represents a lower rounding operation; or, if the consensus algorithm is the working evidence POW consensus algorithm, the number range is
In an optional embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes is
Or, if the consensus algorithm is a POW consensus algorithm, the number of the nodes is
In an alternative embodiment, the node selection policy is a consistent node selection algorithm based on the number of nodes; the determining module 601 is specifically configured to: the target node takes the data identification as the input of the consistency node selection algorithm, and takes the nodes with the number of the nodes selected in the block chain network according to the consistency node selection algorithm as the node set of the target node; the consistent node selection algorithm is executed at each node in the blockchain network, so that node sets determined by each node in the blockchain network according to the node selection strategy are consistent.
As shown in fig. 7, the present application provides a data storage device applied to a block chain, including: an obtaining module 701, configured to obtain a query request; the query request is used for acquiring a block to be queried; the block data to be inquired comprises block body data and block head data; a processing module 702, configured to determine a node in the blockchain network that stores the chunk data if it is determined that the chunk data is not stored in the target node; wherein, the nodes for storing the block data in the block chain network are partial nodes in the block chain network; the target node is any node in the block chain network; an acquisition request is sent to a node storing the block volume data in the blockchain network, so that the acquisition module 701 acquires the block volume data returned from the node.
In an optional implementation manner, the obtaining module 701 is further configured to: acquiring data of the undetermined block volume returned by the node; the processing module 702 is further configured to: determining a hash value of head data of the undetermined block according to the undetermined block volume data; and if the Hash value of the undetermined block head is consistent with the Hash value of the block head data, determining that the undetermined block data acquired and returned from the node is the block data, and acquiring the returned block data from the node.
The embodiment of the present application provides a computer device, which includes a program or an instruction, and when the program or the instruction is executed, the program or the instruction is used to execute a data storage method applied to a block chain and any optional method provided by the embodiment of the present application.
The embodiment of the present application provides a storage medium, which includes a program or an instruction, and when the program or the instruction is executed, the program or the instruction is used to execute a data storage method applied to a block chain and any optional method provided by the embodiment of the present application.
Finally, it should be noted that: as will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.