Consensus algorithm switching method, device, electronic device and storage medium
By cached transactions pending in the blockchain and executing the consensus algorithm switching process, the problem of shutdown in the existing technology of consensus algorithm switching is solved, and the consensus algorithm is switched without shutting down is realized to ensure that transaction data is not lost and the normal operation and consistency of the blockchain.
Patent Information
- Application Number
- CN202011595108.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-29
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2040-12-29
AI Technical Summary
The existing technology requires downtime when switching consensus algorithms in blockchain to prevent transaction data from being lost, resulting in downtime in blockchain business system and affecting business operations.
When the switching conditions are met, the transactions to be processed are cached and the consensus algorithm switching process is executed, and the first consensus algorithm is switched to the target consensus algorithm. If all nodes complete the switching process, the target algorithm will be used to process the transaction. Otherwise, the original consensus algorithm will be used to ensure that the consensus algorithm is switched without shutting down.
It realizes switching consensus algorithms without shutting down to avoid loss of transaction data and ensures the normal operation and consistency of the blockchain.
Smart Images

Figure CN112650626B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of blockchain technology, and in particular relates to a method, device, electronic device, and storage medium for switching a consensus algorithm. Background Art
[0002] All consensus nodes in a blockchain use the same consensus algorithm when processing transactions, such as the Byzantine Fault Tolerance (BFT) algorithm or the Crash Fault Tolerance (CFT) algorithm. In existing technologies, if the underlying blockchain supports switching consensus algorithms, switching consensus algorithms for consensus nodes requires shutting down all nodes to prevent transaction data loss, switching the consensus algorithm configuration on the nodes, and then restarting the nodes. The nodes must wait for consensus to succeed before processing transactions. This process can cause the upper-layer blockchain business system to shut down, impacting business operations. Summary of the Invention
[0003] In view of this, embodiments of the present application provide a consensus algorithm switching method, device, electronic device, and storage medium, which can achieve consensus algorithm switching without downtime.
[0004] A first aspect of an embodiment of the present application provides a method for switching a consensus algorithm, the method being applied to a first consensus node in a blockchain, the method comprising:
[0005] When a consensus algorithm switching condition is met, pending transactions are cached and a consensus algorithm switching process is executed, wherein the consensus algorithm switching process is used to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node other than the first consensus node in the blockchain;
[0006] If both the first consensus node and the second consensus node complete the consensus algorithm switching process, the target consensus algorithm is used to process the pending transaction;
[0007] If the first consensus node or the second consensus node has not completed the switching process of the consensus algorithm, the first consensus algorithm is used to process the pending transaction.
[0008] In one possible implementation, before caching pending transactions and executing the consensus algorithm switching process when the consensus algorithm switching condition is met, the method further includes:
[0009] If the block height of the blockchain reaches the target block height, it is determined that the switching condition of the consensus algorithm is met.
[0010] In one possible implementation, the first consensus node is a master node, and before determining that a switching condition of the consensus algorithm is satisfied when the block height of the blockchain reaches a target block height, the method further includes:
[0011] Determining the target block height;
[0012] Generate first transaction data according to the target block height;
[0013] The first transaction data is sent to the second consensus node, and the second consensus node is used to determine the target block height according to the first transaction data.
[0014] In one possible implementation, generating first transaction data according to the target block height includes:
[0015] First transaction data is generated according to the target block height and the identifier of the target consensus algorithm.
[0016] In one possible implementation, the number of the second consensus nodes is N-1, where N represents an integer greater than or equal to 2. Before determining that a switching condition of the consensus algorithm is satisfied if the block height of the blockchain reaches a target block height, the method further includes:
[0017] Obtaining second transaction data sent by a participant in the blockchain, where the second transaction data is used to instruct the first consensus node and the second consensus node to execute a switching process of the consensus algorithm;
[0018] Broadcasting the second transaction data to the entire network;
[0019] If N-1 second transaction data broadcasted by the second consensus node throughout the entire network are received within the preset time, and it is determined that each of the second consensus nodes has received N-1 second transaction data within the preset time, it is determined whether the block height of the blockchain has reached the target block height.
[0020] In a possible implementation, the first consensus node is a master node, and after the switching process of executing the consensus algorithm, the method further includes:
[0021] If the switching process of the consensus algorithm is completed and the completion information sent by the second consensus node is received, a switching success information is sent to the second consensus node; the completion information is used to indicate that the second consensus node has completed the switching process of the consensus algorithm, and the second consensus node is used to determine whether the first consensus node and the second consensus node have both completed the switching process of the consensus algorithm based on the switching success information.
[0022] In a possible implementation, after executing the switching process of the consensus algorithm, the method further includes:
[0023] If the completion information sent by the second consensus node is not received, a switching failure information is sent to the second consensus node, and the second consensus node is used to determine, based on the switching failure information, that the second consensus node has not completed the switching process of the consensus algorithm.
[0024] In a possible implementation, after executing the switching process of the consensus algorithm, the method further includes:
[0025] If the switching process of the consensus algorithm is not completed, the first consensus algorithm is used to re-elect the master node.
[0026] A second aspect of an embodiment of the present application provides a consensus algorithm switching device, which is applied to a first consensus node in a blockchain, and includes:
[0027] a switching module, configured to cache pending transactions and execute a consensus algorithm switching process when a consensus algorithm switching condition is met, wherein the consensus algorithm switching process is configured to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node in the blockchain other than the first consensus node;
[0028] A first processing module, configured to process the pending transaction using the target consensus algorithm if both the first consensus node and the second consensus node complete the consensus algorithm switching process;
[0029] The second processing module is configured to process the pending transaction using the first consensus algorithm if the first consensus node or the second consensus node has not completed the switching process of the consensus algorithm.
[0030] In a possible implementation, the consensus algorithm switching device further includes:
[0031] A determination module is configured to determine whether a switching condition for the consensus algorithm is satisfied if the block height of the blockchain reaches a target block height.
[0032] In a possible implementation, the first consensus node is a master node, and the consensus algorithm switching device further includes:
[0033] A determination module is configured to determine the target block height; generate first transaction data based on the target block height; and send the first transaction data to the second consensus node, where the second consensus node is configured to determine the target block height based on the first transaction data.
[0034] In a possible implementation, the determining module is specifically configured to:
[0035] First transaction data is generated according to the target block height and the identifier of the target consensus algorithm.
[0036] In one possible implementation, the number of the second consensus nodes is N-1, where N represents an integer greater than or equal to 2, and the consensus algorithm switching device further includes:
[0037] An acquisition module is configured to acquire second transaction data sent by a participant in the blockchain, the second transaction data being used to instruct the first consensus node and the second consensus node to execute a switching process of the consensus algorithm; broadcast the second transaction data to the entire network; and if N-1 pieces of the second transaction data broadcasted by the second consensus node to the entire network are received within a preset time, and it is determined that each of the second consensus nodes has received N-1 pieces of the second transaction data within the preset time, then determine whether the block height of the blockchain has reached a target block height.
[0038] In a possible implementation, the first consensus node is a master node, and the first processing module is further configured to:
[0039] If the switching process of the consensus algorithm is completed and the completion information sent by the second consensus node is received, a switching success information is sent to the second consensus node; the completion information is used to indicate that the second consensus node has completed the switching process of the consensus algorithm, and the second consensus node is used to determine whether the first consensus node and the second consensus node have both completed the switching process of the consensus algorithm based on the switching success information.
[0040] In a possible implementation, the first consensus node is a master node, and the second processing module is further configured to:
[0041] If the completion information sent by the second consensus node is not received, a switching failure information is sent to the second consensus node, and the second consensus node is used to determine, based on the switching failure information, that the second consensus node has not completed the switching process of the consensus algorithm.
[0042] In a possible implementation, the first consensus node is a master node, and the second processing module is further configured to:
[0043] If the switching process of the consensus algorithm is not completed, the first consensus algorithm is used to re-elect the master node.
[0044] A third aspect of an embodiment of the present application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for switching the consensus algorithm as described in the first aspect above is implemented.
[0045] A fourth aspect of an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for switching the consensus algorithm as described in the first aspect above is implemented.
[0046] A fifth aspect of the embodiments of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the consensus algorithm switching method described in the first aspect above.
[0047] Compared with the prior art, the embodiments of the present application have the following beneficial effects: when the switching conditions of the consensus algorithm are met, pending transactions are cached, and the consensus algorithm switching process is executed. The consensus algorithm switching process is used to switch the first consensus algorithm to the target consensus algorithm. The first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node. If it is determined that both the first consensus node and the second consensus node have completed the consensus algorithm switching process, indicating that the consensus algorithm switch is successful, the target consensus algorithm is used to process the pending transactions; if it is determined that the first consensus node or the second consensus node has not completed the consensus algorithm switching process, indicating that the consensus algorithm switch has failed, the first consensus algorithm is used to process the pending transactions. That is, pending transactions are cached before the consensus algorithm switching process is executed. After the consensus algorithm switching process is executed, if the switch is successful, the target consensus algorithm is used to process the pending transactions. If the switch fails, the first consensus algorithm is used to process the pending transactions. Therefore, when the consensus algorithm is switched, transaction data will not be lost even if the system does not shut down, and the consensus algorithm can be switched without shutting down. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments or descriptions of the prior art.
[0049] Figure 1 This is a schematic diagram of the implementation flow of the consensus algorithm switching method provided in the embodiment of the present application;
[0050] Figure 2 Schematic diagram of a switching device for a consensus algorithm provided in an embodiment of the present application;
[0051] Figure 3 Schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0052] In the following description, specific details such as specific system structures and techniques are provided for purposes of illustration rather than limitation to facilitate a thorough understanding of the embodiments of the present application. However, it will be apparent to those skilled in the art that the present application may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid obscuring the description of the present application with unnecessary detail.
[0053] In order to illustrate the technical solution described in this application, specific embodiments are provided below.
[0054] It will be understood that when used in this specification and the appended claims, the term "comprising" indicates the presence of described features, integers, steps, operations, elements and / or components, but does not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.
[0055] It should also be understood that the terms used in this specification are for the purpose of describing specific embodiments only and are not intended to limit the present application. As used in this specification and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms unless the context clearly indicates otherwise.
[0056] It should be further understood that the term "and / or" used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
[0057] As used in this specification and the appended claims, the term "if" can be interpreted as "when" or "upon" or "in response to determining" or "in response to detecting," depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "upon determination" or "in response to determining" or "upon detection of [described condition or event]" or "in response to detecting [described condition or event]," depending on the context.
[0058] In addition, in the description of the present application, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0059] In the existing technology, when it is necessary to switch the consensus algorithm of the consensus nodes of the blockchain, in order to prevent the loss of transaction data, all consensus nodes need to be shut down and the transaction processing can be started again after the switch is successful. This will cause the upper-level blockchain business system to shut down, affecting business operations.
[0060] To this end, the present application provides a consensus algorithm switching method, which is executed on the first consensus node in the blockchain. When the consensus algorithm switching conditions are met, pending transactions are cached and a consensus algorithm switching process is executed. The consensus algorithm switching process is used to switch the first consensus algorithm to a target consensus algorithm. The first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node. If it is determined that both the first consensus node and the second consensus node have completed the consensus algorithm switching process, indicating that the consensus algorithm switch is successful, the target consensus algorithm is used to process the pending transactions. If it is determined that the first consensus node or the second consensus node has not completed the consensus algorithm switching process, indicating that the consensus algorithm switch has failed, the first consensus algorithm is used to process the pending transactions. That is, pending transactions are cached before executing the consensus algorithm switching process. After executing the consensus algorithm switching process, if the switch is successful, the target consensus algorithm is used to process the pending transactions. If the switch fails, the first consensus algorithm is used to process the pending transactions. Therefore, when the consensus algorithm is switched, transaction data will not be lost even if the system does not shut down, thus achieving switching of the consensus algorithm without downtime.
[0061] The following is an exemplary description of the switching method of the consensus algorithm provided in this application.
[0062] Please see the attached Figure 1 The consensus algorithm switching method provided in one embodiment of the present application is applied to a first consensus node in a blockchain. The first consensus node may be an electronic device connected to a blockchain network. The method includes:
[0063] S101: When a switching condition of a consensus algorithm is met, cache pending transactions and execute a switching process of the consensus algorithm, wherein the switching process of the consensus algorithm is used to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is a consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node other than the first consensus node in the blockchain.
[0064] The switching conditions for the consensus algorithm can be sent by the blockchain participants to the first consensus node, or determined by the first consensus node based on the consensus algorithm switching instructions sent by the blockchain participants. The switching conditions for the consensus algorithm can be set by the first consensus node, set by the blockchain, or set at the switching time.
[0065] In one possible implementation, if the blockchain's block height reaches a target block height, the consensus algorithm switching condition is determined to be satisfied. The target block height is determined by agreement between the first and second consensus nodes, and thus by agreement between all consensus nodes in the blockchain. By switching the consensus algorithm when the blockchain reaches the target block height, the first and second consensus nodes can be guaranteed to switch their consensus algorithms simultaneously, ensuring consistency across all consensus nodes in the blockchain and thus ensuring the proper operation of the blockchain.
[0066] When the first consensus node determines that the switching conditions of the consensus algorithm are met, it suspends transaction processing, caches the pending transactions in the transaction pool of the blockchain, and executes the consensus algorithm switching process at the same time.
[0067] In one possible implementation, the first consensus node determines the currently running consensus algorithm based on the identifier of the consensus algorithm stored in the memory, that is, the enumeration value of the consensus algorithm. For example, if the identifier of the first consensus algorithm is stored in the memory of the first consensus node, the first consensus node uses the first consensus algorithm to process transactions. When it is necessary to switch the consensus algorithm, the first consensus node replaces the identifier of the consensus algorithm stored in the memory to switch the consensus algorithm. In an embodiment of the present application, when it is necessary to switch the first consensus algorithm to the target consensus algorithm, the identifier of the first consensus algorithm stored in the memory is replaced with the identifier of the target consensus algorithm, and the consensus algorithm can be switched.
[0068] S102: If both the first consensus node and the second consensus node complete the consensus algorithm switching process, the target consensus algorithm is used to process the pending transaction.
[0069] Specifically, if both the first consensus node and the second consensus node complete the consensus algorithm switching process, indicating that the consensus algorithm switch is successful, then both the first consensus node and the second consensus node use the switched target consensus algorithm to process pending transactions. After completing the processing of pending transactions, they process new transactions, thereby ensuring the consistency of the consensus algorithm switch.
[0070] S103: If the first consensus node or the second consensus node has not completed the consensus algorithm switching process, the first consensus algorithm is used to process the pending transaction.
[0071] Specifically, if the first consensus node or the second consensus node fails to complete the consensus algorithm switching process, indicating that the consensus algorithm switch has failed, the first consensus node and the second consensus node will both roll back the stored consensus algorithm identifier to before the change, that is, change the enumeration variable of the consensus algorithm to the enumeration variable of the first consensus algorithm, and use the first consensus algorithm to process pending transactions, thereby ensuring the consistency of the consensus algorithm switch, and will not lose transaction data when the switch fails, thereby ensuring the normal operation of the blockchain.
[0072] In one possible implementation, each consensus node determines whether the consensus algorithm has been successfully switched based on a message sent by a master node in the blockchain. That is, each consensus node determines whether both the first consensus node and the second consensus node have completed the consensus algorithm switching process based on a message sent by the master node. The master node is one of the first consensus node and the second consensus node.
[0073] Specifically, if the first consensus node is the master node, the second consensus node sends a completion message to the first consensus node after completing the consensus algorithm switch process. If the first consensus node completes the consensus algorithm switch process within a preset time and receives the completion message from the second consensus node, it indicates that all consensus nodes in the blockchain have completed the consensus algorithm switch and the consensus algorithm switch is successful. The node then sends a switch success message to the second consensus node. Upon receiving the switch success message, the second consensus node determines that both the first and second consensus nodes have completed the consensus algorithm switch process.
[0074] If the first consensus node completes the consensus algorithm switching process within the preset time but does not receive completion information from all second consensus nodes, it indicates that there is a second consensus node that has not completed the consensus algorithm switching process. The blockchain consensus algorithm switching has failed, and a switch failure information is sent to the second consensus node. After receiving the switch failure information, the second consensus node can determine that the second consensus node has not completed the consensus algorithm switching process.
[0075] If the first consensus node fails to complete the consensus algorithm switch process within the preset time, it indicates that the first consensus node has failed. Correspondingly, if the second consensus node fails to receive the switch success message sent by the first consensus node within the preset time, it is determined that the blockchain consensus algorithm switch has failed. At the same time, due to the failure of the first consensus node (i.e., the master node), each consensus node changes the enumeration variable of the consensus algorithm to the enumeration variable of the first consensus algorithm, and then re-elects the master node using the first consensus algorithm. After the new master node is selected, business operations will be processed again, thereby ensuring the normal operation of the blockchain.
[0076] In the above embodiment, if the consensus algorithm switching conditions are met, pending transactions are cached and the consensus algorithm switching process is executed. The consensus algorithm switching process is used to switch the first consensus algorithm to the target consensus algorithm. The first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node. If it is determined that both the first consensus node and the second consensus node have completed the consensus algorithm switching process, indicating that the consensus algorithm switch is successful, the target consensus algorithm is used to process the pending transactions. If it is determined that the first consensus node or the second consensus node has not completed the consensus algorithm switching process, indicating that the consensus algorithm switch has failed, the first consensus algorithm is used to process the pending transactions. That is, pending transactions are cached before the consensus algorithm switching process is executed. After the consensus algorithm switching process is executed, if the switch is successful, the target consensus algorithm is used to process the pending transactions. If the switch fails, the first consensus algorithm is used to process the pending transactions. This allows transaction data to be lost even without downtime when the consensus algorithm is switched, thus achieving non-stop switching of consensus algorithms.
[0077] In one possible implementation, the target block height used to determine whether the consensus algorithm switching conditions are met is sent by the master node in the blockchain to the other consensus nodes. Specifically, if the first consensus node is the master node, upon determining the need to switch the consensus algorithm, it obtains the current block height of the blockchain and adds a set value (e.g., 10) to the current block height to obtain the target block height. First transaction data is generated based on the target block height and broadcasted network-wide, i.e., sent to the second consensus node. Upon receiving the first transaction data, the second consensus node determines the target block height based on the first transaction data. Until the target block height is reached, the first and second consensus nodes continue processing transactions. Upon reaching the target block height, the first and second consensus nodes cache pending transactions and execute the consensus algorithm switch process. Since transactions must be verified by each consensus node, sending the target block height to the second consensus node via a transaction ensures that each node receives consistent data, thereby ensuring consistency in the consensus algorithm switch across all consensus nodes in the blockchain.
[0078] In one possible implementation, the identifier of the target consensus algorithm is sent by the master node to the other consensus nodes. Specifically, if the first consensus node is the master node, it generates first transaction data based on the target block height and the identifier of the target consensus algorithm. The first transaction data is sent to the second consensus node, which then determines the target block height and the identifier of the target consensus algorithm based on the first transaction data.
[0079] In one possible implementation, after the first consensus node and the second consensus node obtain the consensus algorithm switching instruction sent by the blockchain participant, if it is determined that both the first consensus node and the second consensus node have received the consensus algorithm switching instruction, it is determined whether the consensus algorithm switching condition is met.
[0080] Specifically, the blockchain participants generate second transaction data based on the consensus algorithm switching instruction and send the second transaction data to the first and second consensus nodes via a software development kit (SDK). The consensus algorithm switching instruction is used to instruct the first and second consensus nodes to execute the consensus algorithm switching process. For example, if the blockchain has N consensus nodes, where N is an integer greater than or equal to 2, the second transaction data sent by the blockchain participants to each consensus node are Tx1, Tx2, Tx3, Tx4, ..., Txn.
[0081] In one possible implementation, the second transaction data includes an identifier for executing a consensus algorithm switch process, for example, the transaction header of the second transaction data includes "changeConsensus." After receiving the second transaction data, the first and second consensus nodes determine the consensus algorithm switch process to execute based on the identifier carried in the second transaction data and broadcast the second transaction data to the entire network.
[0082] If, within a preset time, every consensus node in the blockchain receives the second transaction data sent by all consensus nodes other than itself, then each consensus node is determined to have received the second transaction data. Specifically, if the blockchain has N consensus nodes, the number of second consensus nodes is N-1. After the first and second consensus nodes receive the second transaction data and broadcast it network-wide, if the first consensus node receives N-1 pieces of second transaction data broadcasted network-wide by the second consensus node within the preset time, and if it is determined that each second consensus node has received N-1 pieces of second transaction data within the preset time, then a determination is made as to whether the block height of the blockchain has reached the target block height. Based on the target block height, a determination is made as to whether the consensus algorithm switching condition is met. If any consensus node has not received N-1 pieces of second transaction data, the consensus algorithm switching is stopped. By receiving the first transaction data and broadcasting it network-wide, it is ensured that all consensus nodes in the blockchain receive the consensus algorithm switching instruction, thereby ensuring the consistency of the consensus algorithm transition across all consensus nodes.
[0083] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0084] Corresponding to the switching method of the consensus algorithm described in the above embodiment, Figure 2 A structural block diagram of a switching device for a consensus algorithm provided in an embodiment of the present application is shown. For ease of explanation, only the parts related to the embodiment of the present application are shown.
[0085] like Figure 2 As shown, the switching device of the consensus algorithm is applied to the first consensus node in the blockchain, including:
[0086] A switching module 10 is configured to cache pending transactions and execute a consensus algorithm switching process when a consensus algorithm switching condition is met, wherein the consensus algorithm switching process is configured to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node in the blockchain other than the first consensus node;
[0087] A first processing module 20 is configured to process the pending transaction using the target consensus algorithm if both the first consensus node and the second consensus node complete the consensus algorithm switching process;
[0088] The second processing module 30 is configured to process the pending transaction using the first consensus algorithm if the first consensus node or the second consensus node has not completed the switching process of the consensus algorithm.
[0089] In a possible implementation, the consensus algorithm switching device further includes:
[0090] A determination module is configured to determine whether a switching condition for the consensus algorithm is satisfied if the block height of the blockchain reaches a target block height.
[0091] In a possible implementation, the first consensus node is a master node, and the consensus algorithm switching device further includes:
[0092] A determination module is configured to determine the target block height; generate first transaction data based on the target block height; and send the first transaction data to the second consensus node, where the second consensus node is configured to determine the target block height based on the first transaction data.
[0093] In a possible implementation, the determining module is specifically configured to:
[0094] First transaction data is generated according to the target block height and the identifier of the target consensus algorithm.
[0095] In one possible implementation, the number of the second consensus nodes is N-1, where N represents an integer greater than or equal to 2, and the consensus algorithm switching device further includes:
[0096] An acquisition module is configured to acquire second transaction data sent by a participant in the blockchain, the second transaction data being used to instruct the first consensus node and the second consensus node to execute a switching process of the consensus algorithm; broadcast the second transaction data to the entire network; and if N-1 pieces of the second transaction data broadcasted by the second consensus node to the entire network are received within a preset time, and it is determined that each of the second consensus nodes has received N-1 pieces of the second transaction data within the preset time, then determine whether the block height of the blockchain has reached a target block height.
[0097] In a possible implementation, the first consensus node is a master node, and the first processing module 20 is further configured to:
[0098] If the switching process of the consensus algorithm is completed and the completion information sent by the second consensus node is received, a switching success information is sent to the second consensus node; the completion information is used to indicate that the second consensus node has completed the switching process of the consensus algorithm, and the second consensus node is used to determine whether the first consensus node and the second consensus node have both completed the switching process of the consensus algorithm based on the switching success information.
[0099] In a possible implementation, the first consensus node is a master node, and the second processing module 30 is further configured to:
[0100] If the completion information sent by the second consensus node is not received, a switching failure information is sent to the second consensus node, and the second consensus node is used to determine, based on the switching failure information, that the second consensus node has not completed the switching process of the consensus algorithm.
[0101] In a possible implementation, the first consensus node is a master node, and the second processing module 30 is further configured to:
[0102] If the switching process of the consensus algorithm is not completed, the first consensus algorithm is used to re-elect the master node.
[0103] It should be noted that the information interaction, execution process, etc. between the above-mentioned devices / units are based on the same concept as the method embodiment of this application. Their specific functions and technical effects can be found in the method embodiment section and will not be repeated here.
[0104] Figure 3 Schematic diagram of an electronic device provided in an embodiment of the present application. Figure 3As shown, the electronic device of this embodiment includes: a processor 11, a memory 12, and a computer program 13 stored in the memory 12 and executable on the processor 11. When the processor 11 executes the computer program 13, the steps in the switching method embodiment of the above consensus algorithm are implemented, such as Figure 1 Alternatively, when the processor 11 executes the computer program 13, the functions of the modules / units in the above-mentioned device embodiments are realized, for example, Figure 2 The function of the switching module 10 to the second processing module 30 is shown.
[0105] Exemplarily, the computer program 13 may be divided into one or more modules / units, which are stored in the memory 12 and executed by the processor 11 to implement the present application. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, and the instruction segments are used to describe the execution process of the computer program 13 in the electronic device.
[0106] The electronic device may be a computing device such as a desktop computer, a notebook, a palmtop computer, etc. It will be understood by those skilled in the art that Figure 3 These are merely examples of electronic devices and do not constitute a limitation of the electronic device. The electronic device may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0107] The processor 11 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0108] The memory 12 may be an internal storage unit of the electronic device, such as a hard disk or memory of the electronic device. The memory 12 may also be an external storage device of the electronic device, such as a plug-in hard disk, a smart memory card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device. Furthermore, the memory 12 may also include both an internal storage unit of the electronic device and an external storage device. The memory 12 is used to store the computer program and other programs and data required by the electronic device. The memory 12 may also be used to temporarily store data that has been output or is to be output.
[0109] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0110] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0111] In the embodiments provided in the present application, it should be understood that the disclosed devices / electronic devices and methods can be implemented in other ways. For example, the device / electronic device embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0112] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0113] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0114] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the process in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device that can carry the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc.
[0115] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0116] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present application, and should all be included in the scope of protection of the present application.
Claims
1. A method for switching a consensus algorithm, characterized in that: The method is applied to a first consensus node in a blockchain, and the method includes: When a consensus algorithm switching condition is met, pending transactions are cached and a consensus algorithm switching process is executed, wherein the consensus algorithm switching process is used to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node other than the first consensus node in the blockchain; If both the first consensus node and the second consensus node complete the consensus algorithm switching process, the target consensus algorithm is used to process the pending transaction; If the first consensus node or the second consensus node has not completed the consensus algorithm switching process, the first consensus algorithm is used to process the pending transaction; Each consensus node determines whether the consensus algorithm has been successfully switched based on the message sent by the master node in the blockchain; The first consensus node is a master node, and after the switching process of executing the consensus algorithm, the method further includes: If the switching process of the consensus algorithm is completed and the completion information sent by the second consensus node is received, a switching success information is sent to the second consensus node; the completion information is used to indicate that the second consensus node has completed the switching process of the consensus algorithm, and the second consensus node is used to determine whether the first consensus node and the second consensus node have both completed the switching process of the consensus algorithm according to the switching success information; After the switching process of executing the consensus algorithm, the method further includes: If the completion information sent by the second consensus node is not received, a switching failure information is sent to the second consensus node, and the second consensus node is used to determine, based on the switching failure information, that the second consensus node has not completed the switching process of the consensus algorithm.
2. The method for switching the consensus algorithm according to claim 1, characterized in that: Before caching pending transactions and executing the consensus algorithm switching process when the consensus algorithm switching condition is met, the method further includes: If the block height of the blockchain reaches the target block height, it is determined that the switching condition of the consensus algorithm is met.
3. The method for switching the consensus algorithm according to claim 2, characterized in that: The first consensus node is a master node, and before determining that a switching condition of the consensus algorithm is satisfied if the block height of the blockchain reaches a target block height, the method further includes: Determining the target block height; Generate first transaction data according to the target block height; The first transaction data is sent to the second consensus node, and the second consensus node is used to determine the target block height according to the first transaction data.
4. The method for switching the consensus algorithm according to claim 3, characterized in that: Generating first transaction data according to the target block height includes: First transaction data is generated according to the target block height and the identifier of the target consensus algorithm.
5. The method for switching the consensus algorithm according to claim 2, characterized in that: The number of the second consensus nodes is N-1, where N represents an integer greater than or equal to 2. Before determining that the switching condition of the consensus algorithm is satisfied if the block height of the blockchain reaches the target block height, the method further includes: Obtaining second transaction data sent by a participant in the blockchain, where the second transaction data is used to instruct the first consensus node and the second consensus node to execute a switching process of the consensus algorithm; Broadcasting the second transaction data to the entire network; If N-1 second transaction data broadcasted by the second consensus node throughout the entire network are received within the preset time, and it is determined that each of the second consensus nodes has received N-1 second transaction data within the preset time, it is determined whether the block height of the blockchain has reached the target block height.
6. The method for switching the consensus algorithm according to claim 1, characterized in that: After the switching process of executing the consensus algorithm, the method further includes: If the switching process of the consensus algorithm is not completed, the first consensus algorithm is used to re-elect the master node.
7. A switching device for a consensus algorithm, characterized in that: The device is applied to a first consensus node in a blockchain, and the device includes: a switching module, configured to cache pending transactions and execute a consensus algorithm switching process when a consensus algorithm switching condition is met, wherein the consensus algorithm switching process is configured to switch a first consensus algorithm to a target consensus algorithm, where the first consensus algorithm is the consensus algorithm currently running on the first consensus node and the second consensus node, and the second consensus node is a consensus node in the blockchain other than the first consensus node; A first processing module, configured to process the pending transaction using the target consensus algorithm if both the first consensus node and the second consensus node complete the consensus algorithm switching process; a second processing module, configured to process the pending transaction using the first consensus algorithm if the first consensus node or the second consensus node has not completed the consensus algorithm switching process; Each consensus node determines whether the consensus algorithm has been successfully switched based on the message sent by the master node in the blockchain; The first consensus node is a master node, and the first processing module is further configured to: If the switching process of the consensus algorithm is completed and the completion information sent by the second consensus node is received, a switching success information is sent to the second consensus node; the completion information is used to indicate that the second consensus node has completed the switching process of the consensus algorithm, and the second consensus node is used to determine whether the first consensus node and the second consensus node have both completed the switching process of the consensus algorithm according to the switching success information; The second processing module is further configured to: If the completion information sent by the second consensus node is not received, a switching failure information is sent to the second consensus node, and the second consensus node is used to determine, based on the switching failure information, that the second consensus node has not completed the switching process of the consensus algorithm.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the switching method of the consensus algorithm according to any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the switching method of the consensus algorithm according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Consensus algorithm switching method and device, node equipment and storage medium
CN111767347A