Disclosure of Invention
In view of the above technical problems, the present invention is directed to provide an internal loosely-coupled consensus method, system, electronic device and computer-readable storage medium, for a blockchain consensus system to avoid a problem of loop call between modules through cooperative processing between modules when receiving a message, so as to implement loose coupling inside the consensus system.
In order to solve the technical problems, the invention adopts the following technical scheme:
in a first aspect, an embodiment of the present application provides an internal loosely-coupled consensus system, which includes a state storage module, a state machine module, a timer module, and a pre-write log module,
the state storage module is used for receiving the message, verifying the signature, verifying and storing the message, then sending the message to the state machine module for state change, monitoring an event thrown by the state machine module, and making corresponding logic according to the thrown event;
the state machine module is used for changing the state and controlling the lock, when the message trigger is received, corresponding state change is made according to the received message, and the changed state is thrown out as an event;
the timer module is used for setting a corresponding timer according to an event thrown by the state machine, and calling a state machine interface to wake up the state machine when the timer reaches the overtime;
the pre-write log module is used for being called by the state storage module to read or write the pre-write log.
In one possible design of the first aspect, the consensus process is as follows:
after the consensus is started, the state machine module is initialized firstly, then the pre-written log module is initialized, and finally the state storage module and the timer are initialized;
the state storage module calls the pre-written log module to read the previously written log and inputs the read log to the state machine module to change the state;
the loop then parallels the following actions:
when the state storage module receives the message, the message is signed and verified, the signature is verified, the message passes the verification, the message is input to the state machine module for state change, the state storage module and the timer module monitor the event thrown by the state machine module, the state storage module makes corresponding logic according to the thrown event, and the timer module sets a corresponding timer;
when the time of the timer reaches the overtime, the timer module inputs the state machine module, then continues to monitor the event, the state storage module and the timer module monitor the event thrown by the state machine, the state storage module performs corresponding operation according to the event thrown by the state machine, and the timer module sets the corresponding timer.
In one possible design of the first aspect, the state machine module throwing event comprises entering a new round event, including a new round, an optional locked proposal,
after receiving the event entering the new round, the state storage module updates the round of the state storage module, judges whether the state storage module is the proposed node of the round, and if not, does not perform any processing and waits for the next message; if the node is the proposal node of the round, judging whether locking exists, if so, proposing the locking proposal, and if not, proposing a new proposal;
the timer module updates its turn after receiving the event of entering a new turn, and then sets a proposed timer.
In one possible design of the first aspect, the state machine module throws the event includes a pre-voting event, including a first hash,
after receiving the pre-voting event, the state storage module casts a pre-vote to the first hash;
the timer module sets a pre-vote timer after receiving the pre-vote event.
In one possible design of the first aspect, the state machine throw event comprises a pre-commit event, including the second hash,
after receiving the event, the state storage module puts a pre-submission on the second hash;
the timer module sets a pre-commit timer after receiving the event.
In one possible design of the first aspect, the state machine throwing the event comprises a commit event, including the third hash,
and the state storage module submits the block corresponding to the third hash after receiving the event.
In one possible design of the first aspect, the state machine throw event comprises a stop event, and the entire consensus system stops working.
In a second aspect, an embodiment of the present application provides an internal loosely-coupled consensus method, including:
after the consensus is started, the state machine module is initialized firstly, then the pre-written log module is initialized, and finally the state storage module and the timer are initialized;
the state storage module calls the pre-written log module to read the previously written log and inputs the read log to the state machine module to change the state;
the loop then parallels the following actions:
when the state storage module receives the message, the message is subjected to signature verification and verification, and then the message is input to the state machine module for state change, the state storage module and the timer module monitor the event thrown by the state machine module, the state storage module makes corresponding logic according to the thrown event, and the timer module sets a corresponding timer;
when the time of the timer reaches the overtime, the timer module inputs the state machine module, then continues to monitor the event, the state storage module and the timer module monitor the event thrown by the state machine, the state storage module performs corresponding operation according to the event thrown by the state machine, and the timer module sets the corresponding timer.
In a third aspect, an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor, where the computer program is executed by the processor to perform the above-mentioned internal loosely-coupled consensus method.
In a fourth aspect, the present application provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, where the computer program is executed by a processor to implement the above-mentioned internal loosely-coupled consensus method.
The invention has the following beneficial effects: according to the internal loose coupling consensus method and system, the state machine module does not call any other module, the state storage module calls the pre-written log module and the state machine module, the timer module only calls the state machine module, the modules are not cyclically called, and are decoupled from each other, so that loose coupling in the consensus system is realized.
Detailed Description
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, 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.
Referring to fig. 1, the invention discloses an internal loosely-coupled consensus system, which comprises a state storage module, a state machine module, a timer module and a pre-written log module, wherein the state storage module is used for receiving a message, verifying a signature, storing the message, sending the message to the state machine module for state change, monitoring an event thrown by the state machine module, and making corresponding logic according to the thrown event; the state machine module is used for changing the state and controlling the lock, when the message trigger is received, corresponding state change is made according to the received message, and the changed state is thrown out as an event; the timer module is used for setting a corresponding timer according to an event thrown by the state machine, and calling a state machine interface to wake up the state machine when the timer reaches the overtime; the pre-write log module is used for being called by the state storage module to read or write the pre-write log.
The consensus process of the internal loosely-coupled consensus system composed above is as follows: after the consensus is started, the state machine module is initialized firstly, then the pre-written log module is initialized, and finally the state storage module and the timer are initialized; the state storage module calls the pre-written log module to read the previously written log and inputs the read log to the state machine module to change the state; then, circularly paralleling the following actions, inputting the information to the state machine module for state change after the state storage module receives the information, checking the signature, verifying the signature and passing the verification, monitoring the event thrown by the state machine module by the state storage module and the timer module, making corresponding logic by the state storage module according to the thrown event, and setting a corresponding timer by the timer module; when the time of the timer reaches the overtime, the timer module inputs the state machine module, then continues to monitor the event, the state storage module and the timer module monitor the event thrown by the state machine, the state storage module performs corresponding operation according to the event thrown by the state machine, and the timer module sets the corresponding timer.
Further, the state machine module may throw the following events: enter a new round event, a pre-vote event, a pre-commit event, a commit event, and a stop event. In order to make the events thrown by the state machine module more clear to those skilled in the art, the events are further described below.
In a specific application example, the state machine module throws out an event entering a new round, including a new round, a round of optional locking and an optional proposal of locking, the state storage module updates the round of the state machine module after receiving the event entering the new round, judges whether the state storage module is a proposal node of the round, and if not, does not perform any processing and waits for the next message; if the node is the proposal node of the round, judging whether locking exists, if so, proposing the locking proposal, and if not, proposing a new proposal; the timer module updates its turn after receiving the event of entering a new turn, and then sets a proposed timer.
In a specific application example, the state machine module throws a pre-voting event, which includes a first hash, and the state storage module throws a pre-vote to the first hash after receiving the pre-voting event; the timer module sets a pre-vote timer after receiving the pre-vote event.
In a specific application example, the state machine module throws out a pre-submission event, including a second hash, and the state storage module throws out a pre-submission to the second hash after receiving the event; the timer module sets a pre-commit timer after receiving the event.
In a specific application example, the event thrown by the state machine includes a commit event including a third hash, and the state storage module commits a block corresponding to the third hash after receiving the event.
In a specific application example, the state machine module throws a stop event, and the whole consensus system stops working.
The internal loosely-coupled consensus system implemented by the above embodiments places control of a lock into a state machine module, places active control into a timer module, and the state storage module only needs to check a received message, stores the message, and then sends the message to the state machine module to change the state. The logic of the state storage module becomes simpler and clearer. Meanwhile, the state machine module does not need to store information, only needs to maintain the state of the lock, and is convenient for unit testing.
The invention provides an internal loose coupling consensus method, which is applied to a consensus system comprising a state storage module, a state machine module, a timer module and a pre-written log module, wherein the state storage module is used for receiving messages, verifying, signing, verifying and storing the messages, sending the messages to the state machine module for state change, monitoring events thrown by the state machine module, and making corresponding logic according to the thrown events; the state machine module is used for changing the state and controlling the lock, when the message trigger is received, corresponding state change is made according to the received message, and the changed state is thrown out as an event; the timer module is used for setting a corresponding timer according to an event thrown by the state machine, and calling a state machine interface to wake up the state machine when the timer reaches the overtime; the pre-write log module is used for being called by the state storage module to read or write the pre-write log. The consensus process comprises the following steps:
after the consensus is started, the state machine module is initialized firstly, then the pre-written log module is initialized, and finally the state storage module and the timer are initialized; the state storage module calls the pre-written log module to read the previously written log and inputs the read log to the state machine module to change the state;
the loop then parallels the following actions: when the state storage module receives the message, the message is subjected to signature verification and verification, and then the message is input to the state machine module for state change, the state storage module and the timer module monitor the event thrown by the state machine module, the state storage module makes corresponding logic according to the thrown event, and the timer module sets a corresponding timer; when the time of the timer reaches the overtime, the timer module inputs the state machine module, then continues to monitor the event, the state storage module and the timer module monitor the event thrown by the state machine, the state storage module performs corresponding operation according to the event thrown by the state machine, and the timer module sets the corresponding timer.
Further, the state machine module may throw the following events: enter a new round event, a pre-vote event, a pre-commit event, a commit event, and a stop event. In order to make the events thrown by the state machine module more clear to those skilled in the art, the events are further described below.
In a specific application example, the state machine module throws out an event entering a new round, including a new round, a round of optional locking and an optional proposal of locking, the state storage module updates the round of the state machine module after receiving the event entering the new round, judges whether the state storage module is a proposal node of the round, and if not, does not perform any processing and waits for the next message; if the node is the proposal node of the round, judging whether locking exists, if so, proposing the locking proposal, and if not, proposing a new proposal; the timer module updates its turn after receiving the event of entering a new turn, and then sets a proposed timer.
In a specific application example, the state machine module throws a pre-voting event, which includes a first hash, and the state storage module throws a pre-vote to the first hash after receiving the pre-voting event; the timer module sets a pre-vote timer after receiving the pre-vote event.
In a specific application example, the state machine module throws out a pre-submission event, including a second hash, and the state storage module throws out a pre-submission to the second hash after receiving the event; the timer module sets a pre-commit timer after receiving the event.
In a specific application example, the event thrown by the state machine includes a commit event including a third hash, and the state storage module commits a block corresponding to the third hash after receiving the event.
In a specific application example, the state machine module throws a stop event, and the whole consensus system stops working.
The internal loose coupling consensus method implemented by the above embodiments places the control of the lock into the state machine module, places the active control into the timer module, and the state storage module only needs to check the received message, store the message, and then send the message to the state machine module to change the state. The logic of the state storage module becomes simpler and clearer. Meanwhile, the state machine module does not need to store information, only needs to maintain the state of the lock, and is convenient for unit testing.
The electronic device of the embodiment of the present invention is described in detail below with reference to fig. 2. On a hardware level, the electronic device includes a processor, and optionally an internal bus, a network interface, and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a Non-Volatile Memory (NVM), such as at least one disk Memory. Of course, the electronic device may also include hardware required for other services.
The processor, the network interface, and the memory may be interconnected by an internal bus, which may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an extended EISA (EISA) bus, or the like. Wherein the bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one double-headed arrow is shown in FIG. 2, but this does not represent only one bus or one type of bus.
The memory for storing the program, in particular, the program may include program code including computer operating instructions, the memory may include memory and non-volatile storage, and provide instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory into the memory and then runs the computer program to form the content recommendation device on a logic level. And the processor is used for executing the program stored in the memory and is specifically used for executing the method operation executed when the server is taken as an execution main body.
The processing method of the block chain intelligent contract platform supporting the stateful contract and the stateless contract according to the embodiment of the invention can be applied to a processor or implemented by the processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or instructions in the form of software. The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc., and may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, or a discrete hardware component. The various methods, steps and logic blocks disclosed in embodiments of the present invention may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present invention may be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in a decoder. The software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory, and a processor reads information in the memory and completes a consensus process of the steps of the above-mentioned method in combination with hardware thereof.
Of course, the electronic device in the embodiment of the present specification does not exclude other implementations, such as a logic device or a combination of software and hardware, besides a software implementation, that is, the execution subject of the following processing flow is not limited to each logic unit, and may be hardware or a logic device.
Embodiments of the present invention also provide a computer-readable storage medium storing one or more programs, which when executed by an electronic device including a plurality of application programs, cause the electronic device to perform the following operations, so that the electronic device may perform the consensus process in the above method embodiments. All the steps and the effects of the implementation are the same as those of the embodiment of the method, and the details are not repeated herein.
The computer-readable storage medium may be a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk.
It is to be understood that the exemplary embodiments described herein are illustrative and not restrictive. Although one or more embodiments of the present invention have been described with reference to the accompanying drawings, it will be understood by those of ordinary skill in the art that various changes in form and details may be made therein without departing from the spirit and scope of the present invention as defined by the following claims.