Method for determining main chain of blockchain, device, and storage medium
View Patent ↗A method for determining a main chain of a blockchain, a device, and a storage medium. The method comprises: determining, on the basis of a detection result that it is detected that a blockchain has at least two fork chains, weights of blocks in the fork chains ( 110 ); determining, on the basis of the weights of the blocks comprised in the fork chains, the weights of the fork chains ( 120 ); and determining a main chain from the fork chains on the basis of the weights of the fork chains, and rolling back the fork chains except for the main chain ( 130 ).
1. A method for determining a main chain of a blockchain, comprising:
determining a weight of each block in each fork chain of the blockchain based on a block generation node of each block in each fork chain in response to detecting that at least two fork chains exist in the blockchain;
determining a weight of each fork chain according to the weight of each block included in the fork chain;
determining a main chain from the fork chains according to the weight of each fork chain, and rolling back the fork chains other than the main chain;
wherein determining the weight of each block in each fork chain comprises, for any block in the fork chain,
setting a weight of the block as a first value in response to determining that a block generation node of the block is different from a block generation node of a previous block of the block;
setting the weight of the block as the first value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is a fork block; and
setting the weight of the block as a second value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is not a fork block;
wherein the first value is greater than the second value.
2. The method of claim 1 , wherein determining the weight of each block in each fork chain comprises:
determining a target attack type according to block information included in the fork chain;
determining a target weight setting rule associated with the target attack type according to an association relationship between preset attack types and weight setting rules; and
determining the weight of each block in the fork chain according to the target weight setting rule.
3. The method of claim 1 , wherein determining the weight of each fork chain according to the weight of each block included in the fork chain comprises:
summing the weight of each block included in the fork chain to obtain the weight of the fork chain.
4. The method of claim 1 , wherein determining the main chain from the fork chains according to the weight of each fork chain comprises:
determine the fork chain with a largest weight as the main chain.
5. A device, comprising:
one or more processors;
a memory configured to store one or more programs;
when the one or more programs are executed by the one or more processors, the one or more processors are caused to carry out a method for determining a main chain of a blockchain comprising:
determining a weight of each block in each fork chain of the blockchain based on a block generation node of each block in each fork chain in response to detecting that at least two fork chains exist in the blockchain;
determining a weight of each fork chain according to the weight of each block included in the fork chain;
determining a main chain from the fork chains according to the weight of each fork chain, and rolling back the fork chains other than the main chain;
wherein determining the weight of each block in each fork chain comprises, for any block in the fork chain,
setting a weight of the block as a first value in response to determining that the block generation node of the block is different from a block generation node of a previous block of the block;
setting the weight of the block as the first value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is a fork block; and
setting the weight of the block as a second value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is not a fork block;
wherein the first value is greater than the second value.
6. The device of claim 5 , wherein determining the weight of each block in each fork chain comprises:
determining a target attack type according to block information included in the fork chain;
determining a target weight setting rule associated with the target attack type according to an association relationship between preset attack types and weight setting rules; and
determining the weight of each block in the fork chain according to the target weight setting rule.
7. The device of claim 5 , wherein determining the weight of each fork chain according to the weight of each block included in the fork chain comprises:
summing the weight of each block included in the fork chain to obtain the weight of the fork chain.
8. The device of claim 5 , wherein determining the main chain from the fork chains according to the weight of each fork chain comprises:
determine the fork chain with a largest weight as the main chain.
9. A non-transitory computer-readable storage medium having a computer program stored thereon which, when is executed by a processor, the processor is caused to carry out a method for determining a main chain of a blockchain comprising:
determining a weight of each block in each fork chain of the blockchain based on a block generation node of each block in each fork chain in response to detecting that at least two fork chains exist in the blockchain;
determining a weight of each fork chain according to the weight of each block included in the fork chain;
determining a main chain from the fork chains according to the weight of each fork chain, and rolling back the fork chains other than the main chain;
wherein determining the weight of each block in each fork chain comprises, for any block in the fork chain,
setting a weight of the block as a first value in response to determining that the block generation node of the block is different from a block generation node of a previous block of the block;
setting the weight of the block as the first value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is a fork block; and
setting the weight of the block as a second value in response to determining that the block generation node of the block is the same as the block generation node of the previous block of the block and that the previous block is not a fork block;
wherein the first value is greater than the second value.
10. The non-transitory computer-readable storage medium of claim 9 , wherein determining the weight of each block in each fork chain comprises:
determining a target attack type according to block information included in the fork chain;
determining a target weight setting rule associated with the target attack type according to an association relationship between preset attack types and weight setting rules; and
determining the weight of each block in the fork chain according to the target weight setting rule.
11. The non-transitory computer-readable storage medium of claim 9 , wherein determining the weight of each fork chain according to the weight of each block included in the fork chain comprises:
summing the weight of each block included in the fork chain to obtain the weight of the fork chain.
12. The non-transitory computer-readable storage medium of claim 9 , wherein determining the main chain from the fork chains according to the weight of each fork chain comprises:
determine the fork chain with a largest weight as the main chain.