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-written log module is used for being called by the state storage module to read or write the pre-written 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, 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 upon receiving the event.
In one possible design of the first aspect, the state machine throwing the event comprises committing the 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 a pre-written log module to read the previously written log, and inputs the log to the state machine module according to the read information 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, where the program is executed by a processor to implement the above-mentioned internal loose coupling 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, the modules are decoupled, and loose coupling in the consensus system is achieved.
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, but not all, embodiments of the present invention. All other embodiments, which can be obtained by a person skilled in the art without making any creative effort based on the embodiments in the present invention, belong to 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 timeout time; 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 a pre-written log module to read the previously written log, and inputs the log to the state machine module according to the read information 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, a state machine module throws an event entering a new round, including a new round, an optional locking round and an optional locking proposal, and a state storage module updates the round after receiving the event entering the new round, judges whether the state storage module is a proposal node of the round or not, and waits for the next message if the state storage module is not the proposal node of the round, and does not perform any processing if the state storage module is not the proposal node of the round; 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 receives the event of entering the new round, updates the round and sets a proposal 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, checking the messages to verify signatures, checking and storing the messages, then sending the messages to the state machine module to change states, monitoring events thrown by the state machine module, and making corresponding logics 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 timeout time; 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, after the message is verified, signed and verified, 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. 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, a state machine module throws an event entering a new round, including a new round, an optional locking round and an optional locking proposal, and a state storage module updates the round after receiving the event entering the new round, judges whether the state storage module is a proposal node of the round or not, and waits for the next message if the state storage module is not the proposal node of the round, and does not perform any processing if the state storage module is not the proposal node of the round; if the node is the proposal node of the round, judging whether locking exists, if so, proposing the locking, and if not, proposing a new proposal; the timer module receives the event of entering the new round, updates the round and sets a proposal 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 which comprises 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 out 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 only needs to maintain the state of the lock without storing information, and unit testing is convenient to carry out.
The electronic device according to an embodiment of the present invention will be described in detail with reference to fig. 2. At the hardware level, the electronic device includes a processor, and optionally also includes 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 ISA (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.
A memory for storing a program, in particular, the program may comprise program code comprising computer operating instructions, the memory may comprise memory and non-volatile storage, and provide the 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 combines hardware thereof to complete a consensus process of the steps of the above-described method.
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 implementation effects are the same as those of the embodiment of the method, and are not described herein again.
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 should 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.