Disclosure of Invention
The application provides a data storage method and device applied to a blockchain, which solve the problem that the storage read-write performance of the whole blockchain network is poor due to the fact that the blockchain network operates for too long in the prior art.
In a first aspect, the present application provides a data storage method applied to a blockchain, comprising: after the block data consensus is successful, if the target node is determined not to be a node for storing block data in the block data, storing block header data in the block data but not storing block volume data in the block data; the target node is any node in a blockchain network; the block data includes the block volume data and the block header data; the nodes in the blockchain network for storing the blockvolume data are part of nodes in the blockchain network.
In the above method, after the block data consensus succeeds, the target node does not directly store the block body data and the block header data in the target node, but determines whether the target node is the block data in the block data storage area, if not, only stores the block header data in the block data, so that all data of the block data do not need 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 volume 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 storing the block data in the block data.
In the method, the target node takes the data identifier as the input of the node selection strategy, the node set is determined according to the node selection strategy, and when the target node is determined not to be in the node set, the target node is determined not to be the node for storing the block data in the block data, so that the method for determining the node for storing the block data in the block chain network is provided under the condition that data interaction with the outside is not needed.
In an alternative embodiment, the node selection policy includes a number of nodes in the blockchain network that store the blockvolume data; the number range of the number of nodes is determined by a consensus algorithm of the blockchain network; if the consensus algorithm is a practical Bayesian fault tolerant PBFT consensus algorithm, the number range isWherein M is the total number of nodes in the blockchain network, < >>Representing a rounding operation; alternatively, if the consensus algorithm is a proof of work POW consensus algorithm, the number range is +.>
In the method, under different consensus algorithms, the blockchain is used forThe block body data is stored according to the nodes above a certain node number in the network, so that the block body data stored by at least one node in the node set of the target node is effective data, under the PBFT consensus algorithm,the above is->Nodes within the range store the block volume data, ensure that the block volume data stored in at least one node is valid data, and under the POW consensus algorithm, the block volume data is stored in the at least one node>The above is-> Nodes in the range store the block volume data, and the block volume data stored in at least one node is ensured to be effective data, so that the block volume data is stored in the nodes in the range under different consensus algorithms, the correctness of data storage can be ensured, and the storage space of the data can be saved.
In an alternative embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes isAlternatively, if the consensus algorithm is a POW consensus algorithm, the number of nodes is +.>
Under the mode, the data storage space is saved to the greatest extent on the basis of ensuring the correctness of 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 uses the data identification of the block data as the input of the node selection strategy, and determines a node set storing the block volume data in a block chain network according to the node selection strategy, wherein the node set comprises: the target node takes the data identification as input of the consistency node selection algorithm, and takes the nodes of the node number selected in the blockchain network according to the consistency node selection algorithm as a node set of the target node; the consistency node selection algorithm is executed by 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; because the consistency node selection algorithm is executed by each node in the blockchain network, node sets determined by each node in the blockchain network according to the node selection strategy are consistent, and therefore the unification of storage information of block storage data in the blockchain network in each node can be ensured.
In a second aspect, the present application provides a data acquisition method applied to a blockchain, comprising: the target node obtains a query request; the query request is used for acquiring a block to be queried; the block data to be queried comprises block body data and block head data; if the target node determines that the block volume data is not stored in the target node, determining a node storing the block volume data in the block chain network; wherein the nodes in the blockchain network storing the blockvolume data are part of the nodes in the blockchain network; the target node is any node in a blockchain network; the target node sends an acquisition request to a node storing the block volume data in the blockchain network, thereby acquiring 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 volume data is not stored in the target node, determining a node storing the block volume data in the blockchain network, and transmitting an acquisition request to acquire the block volume data returned from the node, so that the block volume data acquisition method is provided when only part of nodes in the blockchain network store the block volume data.
In an alternative embodiment, after the target node sends the acquisition request to the node storing the block volume data in the blockchain network, the method further includes: the target node acquires pending block volume data returned by the node; the target node determines a hash value of the block head data to be localized according to the block data to be localized; and if the target node determines that the hash value of the block header to be determined is consistent with the hash value of the block header data, determining that the block data to be determined, which is returned from the node, is the block body data, and thus acquiring the returned block body data from the node.
In a third aspect, the present application provides a data storage device for use in a blockchain, comprising: the determining module is used for determining whether the target node is a node for storing block data in the block data after the block data consensus is successful; the storage module is used for storing the block head data in the block data but not the block volume data in the block data if the determination module determines that the target node is not the node for storing the block data in the block data; the target node is any node in a blockchain network; the block data includes the block volume data and the block header data; the nodes in the blockchain network for storing the blockvolume data are part of nodes in the blockchain network.
In an alternative embodiment, the determining module is specifically configured to: the data identification of the block data is used as the input of the node selection strategy, and the node set for storing the block volume data in the block chain network is determined 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 blockvolume data; the number range of the number of nodes is determined by a consensus algorithm of the blockchain network; if the consensus algorithm is a practical Bayesian fault tolerant PBFT consensus algorithm, the number range isWherein M is the total number of nodes in the blockchain network, < >>Representing a rounding operation; alternatively, if the consensus algorithm is a proof of work POW consensus algorithm, the number range is +.>
In an alternative embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes isAlternatively, if the consensus algorithm is a POW consensus algorithm, the number of 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 input of the consistency node selection algorithm, and takes the nodes of the node number selected in the blockchain network according to the consistency node selection algorithm as a node set of the target node; the consistency node selection algorithm is executed by 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 advantages of the foregoing third aspect and the respective embodiments of the third aspect may be referred to the advantages of the foregoing first aspect and the respective embodiments of the first aspect, and are not described herein again.
In a fourth aspect, the present application provides a data storage device for use in a blockchain, comprising: 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 queried 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 the block volume data is not stored in the target node; wherein the nodes in the blockchain network storing the blockvolume data are part of the nodes in the blockchain network; the target node is any node in a blockchain 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 alternative embodiment, the obtaining module is further configured to: acquiring pending block volume data returned by the node; the processing module is further configured to: determining a hash value of the block head data to be fixed according to the block data to be fixed; and if the hash value of the block header to be determined to be consistent with the hash value of the block header data, determining that the block data to be determined, which is returned from the node, is the block body data, and thus obtaining the returned block body data from the node.
The advantages of the foregoing fourth aspect and the embodiments of the fourth aspect may be referred to for the advantages of the foregoing second aspect and the embodiments of the second aspect, and will not be described herein.
In a fifth aspect, the present application provides a computer device comprising a program or instructions which, when executed, is adapted to carry out the method of the first or second aspect and the respective embodiments described above.
In a sixth aspect, the present application provides a storage medium comprising a program or instructions which, when executed, is operable to perform the method of each embodiment of the first or second aspects described above.
Detailed Description
In order to better understand the above technical solutions, the following detailed description will be made with reference to the accompanying drawings and specific embodiments of the present application, and it should be understood that specific features in the embodiments and examples of the present application are detailed descriptions of the technical solutions of the present application, and not limiting 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.
Blockchain: as shown in fig. 1, a blockchain is a chain of blocks, each block being recorded with a Hash value of the previous block in addition to the data of the block, in such a way that the chain is formed. The block chain has two core concepts, one is a cryptography technology, the other is a decentralization concept, and the history information on the block chain cannot be tampered based on the two concepts.
And (3) node: each participant in the network is a node that participates in network construction and data exchange. In a blockchain network, a node refers to a participant with a unique identity, the node having a complete ledger copy with the ability to participate in blockchain network consensus and ledger maintenance.
Intelligent contract: an intelligent contract is a computer protocol that aims to propagate, verify, or execute contracts in an informative manner. Smart contracts allow trusted transactions to be made without third parties, which transactions are traceable and irreversible. The smart contracts are in the form of code deployed on a blockchain to perform a specific function. For example, solubility is a mainstream smart contract programming language, and smart contracts written in the solubility language are called solubility contracts. When an intelligent contract is deployed onto a blockchain, a contract address is generated through which a user may invoke the intelligent contract. A function defined in an intelligent contract, called a contract interface, calls to an intelligent contract, are calls to a certain contract interface in the contract through a contract address.
Transaction: a transaction is a request by a user for an operation of an intelligent contract interface deployed on a blockchain. The transaction is initiated by a user, the transaction is sent to a blockchain node from a client of the user, and after the blockchain node receives the transaction, the corresponding intelligent contract is called according to a contract address and an interface appointed by the transaction. In the blockchain, any operation (deploying contracts, invoking contract interfaces, etc.) is performed by way of a send transaction. The transaction is initiated by the user and sent to the blockchain node through the client. After receiving the transaction, the blockchain node packages 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 results of the execution of the transaction, and typically the transaction receipt contains important information such as a transaction hash, status bits identifying whether the transaction was successfully executed, and logs generated during execution.
As shown in FIG. 2, a data storage method for a blockchain is provided in an embodiment of the present application.
Step 201: after the block data consensus is successful, the target node determines whether the target node is a node storing 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 volume data in the block data.
In step 201 to step 202, the target node is any node in a blockchain network; the block data includes the block volume data and the block header data; the nodes in the blockchain network for storing the blockvolume data are part of nodes in the blockchain network. Thus, the storage of the target node may be: a block head and a block body of the block I; a block header of the second block; a block header of a block three; block header and block body of block four.
In an alternative embodiment of step 201, determining that the target node is not storing the block 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 volume 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 storing the block data in the block data.
It should be noted that, the data identifier of the block data may be various, so long as the block 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, as long as the finally selected node is a part of nodes in the blockchain network and can be obtained according to the node selection policy.
The above process includes, for example, node one, node two, node three, and node four in the blockchain network. And when the target node is the first node, after the first node successfully shares the block data, taking the transaction hash of the first transaction of the block data as the input of a node selection strategy, obtaining a node set which is the second node and the third node, and determining that the first node is not the block data in the block data when the first node is not in the node set.
In an alternative embodiment, the node selection policy is a consistent node selection algorithm based on the number of nodes; the specific process of the target node using the data identifier of the block data as the input of the node selection policy and determining the node set storing the block volume data in the blockchain network according to the node selection policy may be:
the target node takes the data identification as input of the consistency node selection algorithm, and takes the nodes of the node number selected in the blockchain network according to the consistency node selection algorithm as a node set of the target node; the consistency node selection algorithm is executed by 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, second, third and fourth common block data succeed, the transaction hash of the first transaction of the block data is used as the input of the node selection strategy, and the first, second, third and fourth obtained node sets are the second and third nodes.
The consistency node selection algorithm may be any one of the following: a node polling algorithm; a node weight polling algorithm; a node identification ordering algorithm; the random function VRF algorithm may be validated.
It should be noted that, the node selection policy may include the number of nodes storing the block volume data in the blockchain network; the number range of the number of nodes is determined by a consensus algorithm of the blockchain network, specifically:
if the consensus algorithm is a practical Bayesian fault tolerant PBFT consensus algorithm, the number range isWherein M is the total number of nodes in the blockchain network, < >>Representing a rounding operation; alternatively, if the consensus algorithm is a proof of work POW consensus algorithm, the number range is +.>
For example, when M is 10, for the PBFT consensus algorithm, then 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 according to 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 can be valid data. That is, the number range may be determined according to the consensus algorithm; any of the number ranges is such that at least one node in the blockchain network stores a value for which the blockvolume data is real data. Under the PBFT consensus algorithm,the above is-> Nodes within the range store the block volume data, ensure that the block volume data stored in at least one node is valid data, and under the POW consensus algorithm, the block volume data is stored in the at least one node>The above isNodes in the range store the block volume data, and the block volume data stored in at least one node is ensured to be effective data, so that the block volume data is stored in the nodes in the range under different consensus algorithms, the correctness of data storage can be ensured, and the storage space of the data can be saved.
In a more specific alternative embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes isAlternatively, if the consensus algorithm is a POW consensus algorithm, the number of nodes is +.>
For example, when M is 20, for the PBFT consensus algorithm, then the number of nodes is 7; for the POW consensus algorithm, the number of nodes is 11.
Under the mode, the data storage space is saved to the greatest extent on the basis of ensuring the correctness of data storage under different consensus algorithms.
In the method of step 201 to step 202, after the block data consensus is successful, the target node does not directly store the block body data and the block header data in the target node, but determines whether the target node is the block data in the block data, if not, only stores the block header data in the block data, so that all data of the block data is not required to be stored.
After the data storage methods of steps 201 to 202 are performed, since each block data is stored in only a part of nodes in the blockchain network, when a node is requested for a block data, the node may not have all the block data and only the block body data.
To this end, the present application provides a data acquisition method applied to a blockchain, specifically:
the target node obtains the query request.
The query request is used for acquiring a block to be queried. The block data to be queried comprises block body data and block head data.
And if the target node determines that the block volume data is not stored in the target node, determining a node storing the block volume data in the block chain network. Wherein the nodes in the blockchain network storing the blockvolume data are part of the nodes in the blockchain network; the target node is any node in a blockchain network.
The target node sends an acquisition request to a node storing the block volume data in the blockchain network, thereby acquiring the block volume data returned from the node.
When the target node stores the block data, a node selection policy is used to select the node storing the block data, and the node selection policy includes the number of nodes storing the block data in the blockchain network, where the number range of the number of nodes is determined by a consensus algorithm of the blockchain network. The more nodes storing block data, the smaller the space for node collusion, and when the number of nodes is larger than a certain storage threshold, the block data stored by at least one node can be ensured to be effectively available. The number range of specific node numbers may thus be determined according to a consensus algorithm of the blockchain network such that blockvolume data stored by at least one node is effectively available. If the consensus algorithm is a practical Bayesian fault tolerant PBFT consensus algorithm, the number range isWherein M is the total number of nodes in the blockchain network, < >>Representing a rounding operation; alternatively, if the consensus algorithm is a proof of work POW consensus algorithm, the number range is +.>The target node has at least one node feeding back pending block data available effectively when querying the block data.
In the method, after the target node acquires the query request, if it is determined that the block volume data is not stored in the target node, determining a node storing the block volume data in the blockchain network, and transmitting an acquisition request to acquire the block volume data returned from the node, so that the block volume data acquisition method is provided when only part of nodes in the blockchain network store the block volume data.
In an alternative embodiment, after the target node sends the acquisition request to the node storing the block volume data in the blockchain network, the method further includes: the target node acquires pending block volume data returned by the node; the target node determines a hash value of the block head data to be localized according to the block data to be localized; and if the target node determines that the hash value of the block header to be determined is consistent with the hash value of the block header data, determining that the block data to be determined, which is returned from the node, is the block body data, and thus acquiring the returned block body data from the node.
When the target node stores the node of the block data of the storage area by adopting a node selection strategy, and the node selection strategy comprises the number of nodes storing the block volume data in the block chain network, as at least one node returns the block volume data to be determined, the target node can obtain at least one block volume data to be determined under the method, so that the hash value of the block head to be determined is consistent with the hash value of the block head data, and the returned block volume data is acquired from the node.
The following describes in detail a data storage method applied to a blockchain in conjunction with fig. 3 and 4. After the nodes receive the block data which are commonly recognized, the nodes are commonly recognized through an interface module, a transaction management module, an intelligent contract execution module and a multi-node module, and each node stores the block by an account book module. The nodes execute the transactions in the blocks to complete the updating of the state data, and the account book 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 area block head data is stored. And judging whether the current node stores block data or not by the account book module according to the node selection strategy. 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 body data is located is obtained through a consistency node selection algorithm according to the queried block number, and the query is performed to the node containing the block.
Specifically, the node selection policy includes two steps: node number selection and specific node selection. Wherein the number of nodes varies according to the consensus algorithm. For the PBFT type 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 each newly generated block needs at least f+1 nodes to store the block data. f represents the number of Bayesian nodes, and if the number of the nodes is 2f+1 (f is a positive integer greater than or equal to 1), the POW type consensus algorithm needs at least f+1 node storage area block data for each newly generated block. Other consensus algorithms also satisfy that the number of storage nodes is at least greater than the number of bayer pattern nodes.
As shown in fig. 4, the complete flow includes the following steps:
step 401: the target node identifies the block data.
After the success of the target node formula block data, step 402 is performed.
Step 402: the target node stores state data in the block data.
Step 403: the target node splits the block header data and the block body data in the block data and stores the block header data.
Step 404: and the target node obtains a node set of the storage block according to the consistency algorithm.
Step 405: it is determined whether the target node is in the node set.
If yes, go to step 406, otherwise go to step 407.
Step 406: block body data in the block data is stored.
Step 407: the block volume data in the block data is discarded.
The data storage method shown in fig. 1 is described in detail below in conjunction with the scenario shown in fig. 5, using a PBFT consensus algorithm, for example, by one blockchain system, where the total number of nodes is 4, and the nodes are a, B, C, D, respectively. Wherein the D node is a bayer node.
The node B initiates a transaction and the consensus is completed.
For each of the A, B, C and D nodes, the following steps are performed:
the ledger module stores block header data and status data. And the account book module determines to select at least F+1 nodes according to a consensus algorithm for data storage, wherein F=1, namely two nodes store the full data. And the account book module selects specific A nodes and C nodes to store block data according to the current block height and HASH of the block head through a VRF random selection algorithm.
And entering the next round of consensus.
When a certain node needs to inquire the data in the block body of the area, the node where the block is located is determined by a VRF verifiable algorithm preferentially according to the number of the block, if the node is the current node, the local database is inquired, and if the node is not the current node, the block data is requested to the corresponding node. After the data is acquired, the hash of the block and the hash of the local block header are verified. If the data are consistent, the data are correct, otherwise, the data are wrong, and the next node is requested to acquire the data. Until consistent data is obtained, if the data are inconsistent, a data error is returned.
As shown in FIG. 6, the present application provides a data storage device for a blockchain, comprising: a determining module 601, configured to determine whether a target node is a node storing block data in the block data after block data consensus is successful; a storage module 602, configured to store block header data in the block data but not block volume data in the block data if the determination module 601 determines that the target node is not a node storing block data in the block data; the target node is any node in a blockchain network; the block data includes the block volume data and the block header data; the nodes in the blockchain network for storing the blockvolume data are part of nodes in the blockchain network.
In an alternative embodiment, the determining module 601 is specifically configured to: the data identification of the block data is used as the input of the node selection strategy, and the node set for storing the block volume data in the block chain network is determined 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 blockvolume data; the number range of the number of nodes is determined by a consensus algorithm of the blockchain network; if the consensus algorithm is a practical Bayesian fault tolerant PBFT consensus algorithm, the number range isWherein M is the total number of nodes in the blockchain network, < >>Representing a rounding operation; alternatively, if the consensus algorithm is a proof of work POW consensus algorithm, the number range is +.>
In an alternative embodiment, if the consensus algorithm is a PBFT consensus algorithm, the number of nodes isAlternatively, if the consensus algorithm is a POW consensus algorithm, the number of 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 input of the consistency node selection algorithm, and takes the nodes of the node number selected in the blockchain network according to the consistency node selection algorithm as a node set of the target node; the consistency node selection algorithm is executed by 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 for a blockchain, comprising: 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 queried comprises block body data and block head data; a processing module 702, configured to determine a node in the blockchain network that stores the blockvolume data if it is determined that the blockvolume data is not stored in the target node; wherein the nodes in the blockchain network storing the blockvolume data are part of the nodes in the blockchain network; the target node is any node in a blockchain network; an acquisition request is sent to a node in the blockchain network that stores the blockvolume data, so that the acquisition module 701 acquires the blockvolume data returned from the node.
In an alternative embodiment, the obtaining module 701 is further configured to: acquiring pending block volume data returned by the node; the processing module 702 is further configured to: determining a hash value of the block head data to be fixed according to the block data to be fixed; and if the hash value of the block header to be determined to be consistent with the hash value of the block header data, determining that the block data to be determined, which is returned from the node, is the block body data, and thus obtaining the returned block body data from the node.
The embodiment of the application provides a computer device, which comprises a program or an instruction, and when the program or the instruction is executed, the program or the instruction is used for executing the data storage method and any optional method applied to a blockchain.
The embodiment of the application provides a storage medium comprising a program or an instruction, which when executed, is used for executing the data storage method and any optional method applied to a blockchain.
Finally, it should be noted that: it will be appreciated by those skilled in the art that 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 flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations 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 modifications and variations can be made to the present application without departing from the scope of the application. Thus, it is intended that the present application also include such modifications and alterations insofar as they come within the scope of the appended claims or the equivalents thereof.