Detailed Description
in order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
in order to make the technical solutions provided by the embodiments of the present invention easier to understand for those skilled in the art, first, related technologies will be briefly described below.
The block chain is a decentralized distributed database system which is jointly maintained by all nodes in a block chain network and consists of a series of data blocks generated based on a cryptography method, wherein each data block is one block in the block chain. The blocks are linked together in order according to the chronological order of the generation times, forming a chain of data, which is referred to visually as a chain of blocks. The blockchain has the security characteristics of being unchangeable, unforgeable and completely traceable due to the special blockchain and the transaction generation and verification protocol.
each tile in the chain of tiles includes some recent transactions, references to previous tiles, and other data, as well as an answer to the computational difficulty that is unique to each tile. The new block cannot be sent to the network without a correct answer. That is, the process of "mining" essentially "resolves" the current block in competition. The mathematical problem in each block is difficult to solve, but once a valid solution is found, it is easy for other network nodes to verify the correctness of this solution. Particularly, in the bit currency, "digging mine" is actually a bookkeeping process, a mechanism called "Proof of Work (PoW)" is adopted for the operation of the bit currency, a system can generate a mathematical problem every time in order to find out who has stronger computing power, and only a computer which can solve the problem at the fastest speed can perform bookkeeping.
in addition, in the blockchain network, not all nodes participate in mining, and some nodes may only have functions of querying and issuing transactions. The nodes involved in mining are referred to as miner nodes, and all the nodes described below are miner nodes.
the block chain and the excavation process thereof will be specifically described below based on fig. 1. As shown in fig. 1, node devices of certain data are connected with each other through a p2p network to form a basic network of block chains. When digging a mine, first, a target value corresponding to a current block needs to be calculated according to a protocol established by a block chain, that is, the target value is F, where F is a target value in a previous block and a time difference between the previous block and the current block. Each node device in the network then generates a very large random number file by seeding with the current block number. Each node device continuously traverses the random number in the random number file and performs hash (random number) operation, when the node device finds a random number v with a hash (v) < target from the file, the round of mining is finished, and the node device which finds the v value first in all the node devices obtains the block writing authority.
Based on the above process, in the case that all the node devices participate in each ore excavation, the probability that the node devices with higher calculation force excavate the ore production block is higher. If an attacker has control over most of the computational power in the blockchain network, resulting in a "51% attack," the data of the blockchain network may be corrupted.
An embodiment of the present invention provides a method for mining a block chain, where the method is applied to a node device in a block chain network, and as shown in fig. 2, the method includes:
s201, acquiring the current block number of the block chain.
The blockchain is synchronized locally at the node device, so that the node device can obtain the current number of blocks by querying the locally stored blockchain.
s202, judging whether the hash groove corresponding to the block number is consistent with the hash groove corresponding to the node equipment.
Each node device in the blockchain network is distributed in N hash grooves through a hash algorithm, wherein N is a positive integer greater than or equal to 2.
And S203, determining whether the node equipment digs the mine at this time according to the judgment result.
That is, before each excavation, the node apparatus needs to go through step S202 to determine whether to perform the excavation by itself this time.
By adopting the method, all the node equipment in the block chain network are uniformly distributed in different hash grooves, and the node equipment for mining changes along with the increase of the number of blocks of the current block chain every time, so that the condition that the same node equipment cannot continuously generate the blocks is ensured. Therefore, the probability of digging the ore by the node equipment with strong calculation capacity is reduced, the probability of digging the ore by the node equipment with weak calculation capacity is increased, and the ore digging machine of each node equipment in the block chain network can be fairer.
in order to make those skilled in the art understand the technical solutions provided by the embodiments of the present invention, the following detailed descriptions are provided for the above steps.
First, how to realize the uniform distribution of each node device in the blockchain network in N hash slots is illustrated.
Performing MD5(Message Digest MD5, the name of Chinese is the fifth edition of Message Digest Algorithm) hash operation on the account information of each node device, and performing modular operation on N (namely, calculating the remainder of N) by using the characteristic that hash results are randomly and uniformly distributed, so that each account corresponds to a hash slot with the number between 0 and N-1.
It should be noted that there may be a case where multiple accounts can log in the same node device, and the account information of the node device described herein refers to current account information of the node device, that is, an account in a blockchain network corresponding to one node device.
As shown in fig. 3, when N is equal to 4, after the account information of the node device is hashed by using the MD5 algorithm, all the node devices may be uniformly distributed in hash slot 0, hash slot 1, hash slot 2, and hash slot 3 after the remainder is found by using the hash value to N, because the hash value has the characteristic of random uniform distribution. That is, each hash slot corresponds to approximately one-fourth of the node devices in the blockchain network. For example, eight node devices 1 to 8 exist in the block chain network, and hash values obtained by sequentially using the MD5 algorithm for the account information of each node device are respectively: 63. 17, 46, 27, 36, 82, 24, 41, the values obtained modulo 4 are: 3. 1, 2, 3, 0, 2, 0, 1. The available node device 5 and the node device 7 correspond to the hash slot 0, the node device 2 and the node device 8 correspond to the hash slot 1, the node device 3 and the node device 6 correspond to the hash slot 2, and the node device 1 and the node device 4 correspond to the hash slot 3.
It is worth to be noted that, by adopting the above method, when a new node device is added to the blockchain network, only the hash slot corresponding to the newly added node device needs to be calculated, and the hash slot of the existing node device does not need to be adjusted, which is convenient for the extension of the blockchain network.
In addition, the foregoing is only an example, and in the embodiment of the present invention, other hash algorithms may also be adopted to uniformly distribute all node devices in each hash slot, which is not limited in the present invention.
In this way, after obtaining the current block number of the blockchain, the calculation of the hash slot corresponding to the block number may be: and performing modular operation on the N by using the current block number to obtain a value which is the number of the corresponding hash groove.
Still referring to fig. 3, if the current number of blocks of the block chain is 8728, and the remainder is performed on 4 to obtain 0, the hash slot corresponding to the current block is hash slot 0.
Further, in a possible implementation manner of the embodiment of the present invention, the step S203 may specifically include: when the hash grooves corresponding to the block number are consistent with the hash grooves corresponding to the node equipment, determining that the node equipment does not dig the ore at this time; and when the hash grooves corresponding to the block numbers are not consistent with the hash grooves corresponding to the node equipment, determining that the node equipment digs the mine at this time.
For example, as shown in fig. 3, if the current block number of the block chain is 1 and the remainder of the block number to 4 is equal to 1, the node devices 2 and 8 corresponding to the hash slot 1 do not participate in mining of the next block. After the next block is generated by mining of other node devices, the number of blocks of the block chain is 2, and the remainder of 4 is equal to 2, so that the node device 3 and the node device 6 corresponding to the hash groove 2 do not participate in mining of the 3 rd block. Go on in proper order according to above-mentioned flow, because the constantly increase of block number, the hash groove that corresponds also constantly changes to the node equipment who participates in at every turn and dig the ore also changes, and then same node equipment can't produce the block for several times in succession, has promoted node equipment and has dug the fairness in ore.
It should be noted that, considering that a node device may fail to dig a mine, there may be a situation in the block chain network where no node device participates in a certain mine digging. For example, in fig. 3, when the number of the front blocks is 3, and the remainder of the number of the blocks to 4 is equal to 3, the node devices 1 and 4 corresponding to the hash slot 3 do not participate in ore excavation of the next block, and if all other node devices fail to dig an ore, the case of no node device ore excavation occurs at this time, and the number of blocks of the block chain is not increased.
Therefore, the embodiment of the present invention may further include: and when the hash grooves corresponding to the number of the blocks obtained for M times are consistent with the hash grooves corresponding to the node equipment, determining that the node equipment digs the mine at this time, wherein M is a positive integer greater than or equal to 2. That is to say, if the number of blocks acquired by a certain node device for M consecutive times is not changed, and the node device does not participate in mine excavation for M-1 consecutive times, the node device can perform mine excavation for the mth time, and normal operation of the block chain network is ensured.
The specific value of M can be preset according to actual conditions. Still taking the example of fig. 3 as an example, if the current block number of the block chain acquired by the node device 1 is 7, since the hash slot corresponding to the block number 7 is the hash slot 3, the node device 1 does not participate in mining the 8 th block at this time. And when the next mine excavation is carried out, if the current block number of the block chain acquired by the node equipment 1 is still 7, indicating that other node equipment does not participate in the previous mine excavation. Thus, in the case where M is equal to 2, the node apparatus 1 determines that excavation is performed this time. Under the condition that M is equal to 4, if the node equipment 1 does not dig the mine at this time, if the number of blocks increases for the next time, it is indicated that other node equipment recovers to dig the mine normally, and if the number of blocks is continuously unchanged for 4 times, the node equipment 1 determines to dig the mine, so that the normal operation of the block chain network is ensured.
Furthermore, after the block is generated, whether the block generated by the node device at this time is legal or not can be verified according to the judgment of the hash slot. Specifically, the method for mining provided by the embodiment of the present invention further includes: receiving a block sent by another node device in the blockchain network, wherein the block comprises account information of the other node device; and when the hash groove corresponding to the block number is determined to be inconsistent with the hash groove corresponding to the other node device, carrying out workload certification on the block.
For example, as shown in fig. 3, after the node device 3 successfully excavates the mine this time, the block is broadcast to other node devices, after the node device 1 receives the block broadcast by the node device 3, MD5 hash operation is performed on account information of the node device 3 to obtain a hash value, a remainder is obtained by using the hash value to 4, so as to determine the hash slot 2 corresponding to the node device 3, and then a remainder is obtained by using the hash value to 4 according to the current block number of the block chain, so as to determine the hash slot corresponding to the current block number, if the hash slot corresponding to the current block number is not consistent with the hash slot corresponding to the node device 3, it is indicated that the node device 3 is legal to excavate the mine this time, and a workload certification can be performed on the block. If the hash slot corresponding to the current block number is consistent with the hash slot corresponding to the node device 3, indicating that the node device 3 is not legal to mine at this time, the node device 1 may discard the block to refuse to add the block to the block chain. Alternatively, when the hash slot corresponding to the current block number is consistent with the hash slot corresponding to the node device 3 and the block number of the current block chain has not changed for M times, the node device 1 may consider that the node device 3 is legal to mine at this time, and may further perform workload certification on the block. In specific implementation, the node 1 may comprehensively determine whether the current ore excavation of the node 3 is legal by combining the hash slot and the time difference between the block and the previous block.
In another possible implementation manner of the embodiment of the present invention, step S203 may further include: when the hash grooves corresponding to the block numbers are consistent with the hash grooves corresponding to the node equipment, determining that the node equipment digs the mine at this time; and when the hash grooves corresponding to the block numbers are not consistent with the hash grooves corresponding to the node equipment, determining that the node equipment does not dig the mine at this time.
For example, as shown in fig. 3, if the current block number of the block chain is 1 and the remainder of the block number to 4 is equal to 1, node devices 2 and 8 corresponding to the hash slot 1 participate in mining of the next block, and all other node devices do not participate in mining of the next block. After the node device 2 or the node device 8 excavates the mine to generate the next block, the number of blocks of the block chain is 2, and the remainder of 4 is equal to 2, then the node device 3 and the node device 6 corresponding to the hash slot 2 participate in the mine excavation of the 3 rd block, and other node devices do not participate in the mine excavation of the 3 rd block. Go on in proper order according to above-mentioned flow, because the constantly increase of block number, the hash groove that corresponds also constantly changes to the node equipment who participates in at every turn and dig the ore also changes, and then same node equipment can't produce the block for several times in succession, has promoted node equipment and has dug the fairness in ore.
Similarly, considering that a node device may fail to dig a mine, there may be a situation in the blockchain network where no node device participates in a certain excavation. The embodiment of the present invention may further include determining that the node device performs ore excavation this time when all hash grooves corresponding to the number of blocks obtained for L consecutive times are inconsistent with the hash groove corresponding to the node device, where L is a positive integer greater than or equal to N.
That is to say, for N hash grooves, under normal conditions, one node device may participate in mining at least once every N times, and if a certain node device does not participate in mining for L-1 times continuously, it indicates that other node devices may fail, so that the node device may dig mining when the hash groove corresponding to the L-th determined block number continuously is inconsistent with the hash groove corresponding to the node device, thereby ensuring normal operation of the block chain network.
The two possible implementation manners are only examples, and in specific implementation, whether to perform ore excavation each time may be determined according to a specific service configuration node device in what manner.
the effect that can be achieved by the ore excavation method provided by the embodiment of the invention is analyzed in the following manner aiming at the first possible implementation manner.
As illustrated in fig. 3, all node devices on the blockchain are assigned 4 hash slots by using the account MD5 hash value. Modulo 4 by the number of blocks, this hits the slot in sequence from hash slot 0 to hash slot 3. Each node device in the slot is prohibited to participate in ore excavation periodically, so that the purpose of prohibiting strong computing power node devices from excavating ores is achieved, monopoly of the strong computing power node devices on block chains is avoided, and the difficulty of double-flower attack of attackers is improved. The existing workload proving mechanism can realize the double-flower attack only by one strong-computing node device, and after the embodiment of the invention is adopted, at least 4 strong-computing nodes are needed to realize the double-flower attack, thereby improving the crime cost of an attacker.
the fault tolerance and attack resistance of the embodiments of the present invention are analyzed as follows. If the probability of the fault of each node device is 2.5%, the probability of each node device being attacked maliciously is 25%, the number of hash slots is 4, and the total number of the node devices is X, when other node devices fail, the total number of available node devices is less than X/4, and all the available node devices are in the same slot, freezing occurs, that is, there is no node device participating in ore excavation, the probability distribution of the situation conforms to the cumulative binomial distribution, the experimental result shows that when X is 5, the probability of freezing occurs is 1.914E-6, that is, the probability of parts per million, and the probability of freezing gradually approaches 0 with the increase of X, which indicates that the block chain network adopting the ore excavation method provided by the embodiment of the present invention has a high fault tolerance capability.
for the anti-attack capability, when the total number of available node devices is smaller than X/4 and all are in the same slot position due to the attack, the situation that the write of the whole block is completely mastered by an intruder occurs. This probability also follows a cumulative binomial distribution, and experimental results show that the probability of occurrence being controlled is 3.9482E-6 when X is 18, after which the probability will gradually approach 0 as X increases. The block chain network adopting the mining method provided by the embodiment of the invention has higher anti-attack capability.
Therefore, by adopting the method provided by the embodiment of the invention, the mining opportunities of each node device in the block chain network are fairer on the premise of ensuring that the block chain network has enough fault tolerance capability and attack resistance capability.
an embodiment of the present invention further provides a mining device 40, where the mining device 40 is used for a node device in a block chain network, and is used to implement the block chain mining method provided in the foregoing method embodiment, as shown in fig. 4A, the mining device 40 includes:
an obtaining module 401, configured to obtain a current block number of the block chain;
a determining module 402, configured to determine whether a hash slot corresponding to the block number is consistent with a hash slot corresponding to the node device, where each node device in the block chain network is distributed in N hash slots through a hash algorithm, where N is a positive integer greater than or equal to 2;
And a determining module 403, configured to determine whether the node device performs ore excavation this time according to the determination result.
by adopting the ore digging device, all node equipment in the block chain network are uniformly distributed in different hash grooves, and the node equipment for digging the ore also changes along with the increase of the number of blocks of the current block chain every time, so that the situation that the same node equipment cannot continuously generate the blocks is ensured. Therefore, the probability of digging the ore by the node equipment with strong calculation capacity is reduced, the probability of digging the ore by the node equipment with weak calculation capacity is increased, and the ore digging machine of each node equipment in the block chain network can be fairer.
optionally, the determining module 403 is configured to determine that the node device does not perform ore excavation this time when the hash slot corresponding to the block number is consistent with the hash slot corresponding to the node device; and when the hash grooves corresponding to the block numbers are not consistent with the hash grooves corresponding to the node equipment, determining that the node equipment digs the mine at this time.
Optionally, the determining unit 403 is configured to determine that the node device performs ore excavation this time when all hash slots corresponding to the number of blocks obtained M consecutive times are consistent with a hash slot corresponding to the node device, where M is a positive integer greater than or equal to 2.
Optionally, as shown in fig. 4B, the ore excavating device 40 further includes: a calculating unit 404, configured to perform a modulus operation on N by using the block number to obtain a first modulus value; carrying out modular operation on N according to an MD5 algorithm by using the account information of the node equipment to obtain a second modular value; the determining module 402 is configured to determine whether the first modulus value is consistent with the second modulus value, where a consistency of the first modulus value with the second modulus value indicates that the hash slot corresponding to the block number is consistent with the hash slot corresponding to the node device, and a inconsistency of the first modulus value with the second modulus value indicates that the hash slot corresponding to the block number is inconsistent with the hash slot corresponding to the node device.
Optionally, as shown in fig. 4B, the ore excavating device 40 further includes: a receiving module 405, configured to receive a block sent by another node device in the blockchain network, where the block includes account information of the another node device; a workload certification module 406, configured to perform workload certification on the block when it is determined that the hash slot corresponding to the number of blocks is inconsistent with the hash slot corresponding to the another node device.
It is obvious to those skilled in the art that, for convenience and simplicity of description, the foregoing division of the functional modules is merely used as an example, and in practical applications, the above function distribution may be performed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to perform all or part of the above described functions. For the specific working process of the functional module, reference may be made to the corresponding process in the foregoing method embodiment, which is not described herein again.
An embodiment of the present invention further provides a node device 50, configured to implement the method for block chain mining provided in the foregoing method embodiment, as shown in fig. 5, where the node device 50 includes:
A processor unit 501, a communication interface 502, a memory 503, and a communication bus 504; the processor unit 501, the communication interface 502 and the memory 503 are communicated with each other through the communication bus 504.
the processor unit 501 may be a multi-core central processing unit CPU, or an ASIC (Application Specific Integrated Circuit), or one or more Integrated circuits configured to implement embodiments of the present invention.
The memory 503 is used to store program code, including computer operating instructions and network flow diagrams. The memory 503 may carry high-speed RAM memory, and may also include non-volatile memory (non-volatile memory), such as at least one disk memory.
the communication interface 502 is used for implementing connection communication between the node devices.
The processor unit 501 executes a program code, which is configured to implement the method for mining a block chain according to the above method embodiment when running, and the method includes:
Acquiring the current block number of the block chain;
Judging whether the hash grooves corresponding to the block numbers are consistent with the hash grooves corresponding to the node devices, wherein each node device in the block chain network is distributed in N hash grooves through a hash algorithm, and N is a positive integer greater than or equal to 2;
And determining whether the node equipment digs the mine or not according to the judgment result.
Optionally, the determining, according to the determination result, whether the node device performs ore excavation at this time includes:
when the hash grooves corresponding to the block number are consistent with the hash grooves corresponding to the node equipment, determining that the node equipment does not dig the ore at this time;
And when the hash grooves corresponding to the block numbers are not consistent with the hash grooves corresponding to the node equipment, determining that the node equipment digs the mine at this time.
Optionally, the determining, according to the determination result, whether the node device performs ore excavation at this time includes:
and if the hash grooves corresponding to the number of the blocks obtained for M times are consistent with the hash grooves corresponding to the node equipment, determining that the node equipment excavates the mine at this time, wherein M is a positive integer greater than or equal to 2.
Optionally, the determining whether the hash slot corresponding to the block number is consistent with the hash slot corresponding to the node device includes:
Performing modular operation on N by using the block number to obtain a first modular value;
carrying out modular operation on N according to an MD5 algorithm by using the account information of the node equipment to obtain a second modular value;
and judging whether the first modulus value is consistent with the second modulus value, wherein the consistency of the first modulus value and the second modulus value indicates that the hash slot corresponding to the block number is consistent with the hash slot corresponding to the node device, and the inconsistency of the first modulus value and the second modulus value indicates that the hash slot corresponding to the block number is inconsistent with the hash slot corresponding to the node device.
optionally, the method further comprises:
Receiving a block sent by another node device in the blockchain network, wherein the block comprises account information of the other node device;
And when the hash groove corresponding to the block number is determined to be inconsistent with the hash groove corresponding to the other node device, carrying out workload certification on the block.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the functional blocks is only one logical functional division, and there may be other divisions when the actual implementation is performed.
The modules described as separate parts may or may not be physically separate, and parts displayed as modules may or may not be physical modules, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment.
In addition, functional modules in the embodiments of the present invention may be integrated into one processing unit, or each module may exist alone physically, or two or more modules are integrated into one module. The integrated module can be realized in a hardware form, and can also be realized in a form of hardware and a software functional module.
The integrated module implemented in the form of a software functional module may be stored in a computer-readable storage medium. The software functional module is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute some steps of the methods according to the embodiments of the present invention. And the aforementioned storage medium includes: a usb disk, a removable hard disk, a Random Access Memory (RAM), a magnetic disk, an optical disk, or other various media capable of storing data. That is, an embodiment of the present invention also provides a computer-readable storage medium for storing a computer program including instructions for executing the method for mining a block chain as described above.
The above description is only for the specific embodiment of the present invention, but the scope of the present invention is not limited thereto, and any changes or substitutions that can be easily conceived by those skilled in the art within the technical scope of the present invention are included in the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.