Detailed Description
The following detailed description of specific embodiments of the present disclosure is provided in connection with the accompanying drawings. It should be understood that the detailed description and specific examples, while indicating the present disclosure, are given by way of illustration and explanation only, not limitation.
First, terms related to the embodiments of the present disclosure will be described.
All nodes: nodes with all data in the blockchain system.
New block servicing: a node in a blockchain system generates a service function for a new block. When providing a new block service in the related art, all the blockchain data of the node is required, and therefore, all the nodes can provide the new block service.
Broadcast service: nodes in the blockchain system broadcast data to the service functions of the whole blockchain system.
And (3) query service: the method is used for inquiring the business functions of the data such as the zone data, the contract code result and the like in the block chain system.
Forward synchronization: and synchronizing the data from front to back according to the arrangement sequence of the blocks.
Backward synchronization: and synchronizing the data from back to front according to the arrangement sequence of the blocks.
In the related art, when a new node joins a blockchain system, the new node needs to synchronize all blocks in the blockchain system to start various service functions such as broadcasting. For example, in the block chain system including a plurality of blocks from block 1 to block N, according to the scheme in the related art, the new node needs to start various service functions by sequentially synchronizing to block N from block 1 according to the block arrangement order.
However, as the number of blocks increases with the increase of transactions and data in the blockchain system, a new node needs to spend more time synchronizing all blocks in the blockchain system, so that the process from adding the blockchain system to starting a service function by the new node is long, and the expansion of the blockchain system is affected.
In order to solve the foregoing technical problem, embodiments of the present disclosure provide a method and an apparatus for controlling a blockchain node, a storage medium, and an electronic device, so that a new node can quickly provide a service function after joining a blockchain system, and the blockchain system can be quickly expanded.
Fig. 1 is a flowchart illustrating a control method of a blockchain node according to an exemplary embodiment of the present disclosure. Referring to fig. 1, the control method may be applied to a new node joining a blockchain system, including:
step S101, querying a target block with the largest block number in the blockchain system from the target full node in the blockchain system, wherein the new node is connected to the target full node.
For example, the target full node may be one or multiple, and the embodiment of the present disclosure does not limit this. In the case where there is one target full node, the process of querying the target block may be to use the block with the largest block number in the target node as the target block. In the case that the target node is plural, the process of querying the target block may be to query the blocks with the largest block numbers from the plural target nodes, and then determine the block with the largest block number as the target block among the queried blocks.
It should be understood that the target block with the largest block number in the embodiments of the present disclosure refers to the block with the latest data in the blockchain system, and therefore, in order to ensure that the queried target block is the block with the latest data in the blockchain system, i.e. to ensure the accuracy of the query result, the target full node may preferably be multiple.
Step S102, the target block is synchronized from the target full node to the new node.
For example, synchronizing the target block from the target full node to the new node may be implemented by traversing branches of the hash tree root of the block header of the target block, and the specific implementation manner is similar to that in the related art and is not described herein again. Alternatively, the target block may be synchronized from the target full node to the new node in other manners, which is not limited in the embodiment of the present disclosure.
Step S103, after the target block is synchronized to the new node, the service function that can be executed in the blockchain system and that is already configured by the new node is started.
For example, the synchronization of the target block to the new node may be the synchronization of all data of the target block to the new node, or may be the synchronization of part of the data of the target block to the new node. In the former case, after the data traversed by the root of the merkel tree in the target block is synchronized to the new node, the new node is started to configure the business function which can be executed in the blockchain system. For the latter case, when the target block is synchronized with the new node, the configured query service function of the new node is started first, and then, as the synchronization process continues, after the partial data of the target block is synchronized with the new node, the configured broadcast service function and the configured new block service function of the new node are started.
Illustratively, the service functions that the new node has configured to be able to perform in the blockchain system include a broadcast service function, a new-block service function for generating a new block, and a query service function. That is to say, in the embodiment of the present disclosure, after the target block is synchronized to the new node, the broadcast service function, the new block service function, and the query service function provided by the new node may be started.
In a possible manner, the starting of the service function that can be executed in the blockchain system and is configured by the new node may be to determine whether a new block with a block number greater than that of the target block exists in the blockchain system, if a new block with a block number greater than that of the target block exists in the blockchain system, take the block with the largest block number in the new block as the new target block, and start the service function that can be executed in the blockchain system and is configured by the new node after the new target block is synchronized to the new node.
In the blockchain system, as the transaction data is continuously generated, the blocks are also continuously generated, so that in order to ensure the reliability of the service function provided by the new node, that is, to ensure that the new node is synchronized with the block with the latest data when the service function is started, it can be further determined whether the target block is the latest block in the current blockchain system before the service function of the new node is started.
For example, it may be determined whether there is a new block in the blockchain system with a block number greater than the target block. If a new block with a block better than the target block exists in the blockchain system, the target block is not the latest block in the current blockchain system. In this case, the new block may be used as a new target block, and then after the new target block is synchronized to the new node, the service function that can be executed in the blockchain system and that has been configured by the new node is started, so as to ensure that the new node is synchronized with the latest block in the blockchain system.
In other cases, if there is no new block with a block number greater than that of the target block in the blockchain system, it indicates that the target block is the latest block in the current blockchain system, and then the service function that can be executed in the blockchain system and is configured for the new node can be directly started.
After initiating the service function that the new node has configured to be able to execute in the blockchain system, it may be determined whether the data required to verify the transaction is included in the currently synchronized block data of the new node when the transaction is received, and if the currently synchronized block data of the new node includes the data required to verify the transaction, the broadcast service function and the new block service function may be executed for the transaction.
In the embodiment of the present disclosure, when the service function is started, the new node synchronizes all or part of the data of the target block in the blockchain system, but not all the data in the blockchain system, so that the new node performs a limited service function on the received transaction, that is, the new node can check the received transaction, and perform a corresponding service function on the transaction when the check is passed.
For example, the target block includes balance data for account X and account Y, and the balance for account X is 10 yen and the balance for account Y is 20 yen. After the new node synchronizes the target block to the new node, that is, under the condition that the currently synchronized block data of the new node includes balance data of an account X and an account Y, when the new node receives a transfer transaction of 5-bit account X transfer to the account Y, the new node firstly determines whether the currently synchronized block data can inquire the balance data respectively corresponding to the account X and the account Y, that is, the new node firstly determines whether the currently synchronized block data includes data for verifying the transfer transaction.
In this case, since the block data currently synchronized by the new node includes the balances of account X and account Y, i.e., the data currently synchronized by the new node includes data required to verify the transaction, the new node may perform the broadcast service function and the new block service function on the transfer transaction. Specifically, the new node may broadcast the transfer transaction to the entire blockchain system. Also, the new node may package the transfer transaction as a new block, where the new block includes data for account X balance of 5 and account Y balance of 25.
Through the mode, when the new node receives the transaction, the transaction can be verified, the corresponding business function is executed on the transaction when the verification is passed, and the business function is not executed for the transaction which is not verified, so that the data security in the block chain system can be ensured.
Further, in order to enhance the capability of the new node to perform the service function, in the embodiment of the present disclosure, in the process of starting the service function of the new node, other blocks in the blockchain system except for the target block may be synchronized to the new node, so that the new node may eventually synchronize all blocks in the blockchain system.
In one possible approach, a block with a block number larger than the target block in the block chain system may be synchronized to the new node by the longest chain mechanism; and if the block head of the blocks with the block numbers smaller than that of the target block in the block chain system passes the validity verification, synchronizing the blocks with the block heads passing the validity verification to the new node.
That is, synchronizing other blocks in the blockchain system except the target block to the new node may be a bidirectional synchronization process, i.e., performing forward synchronization and backward synchronization simultaneously. The forward synchronization process (for a block with a block number greater than the target block) can be implemented by a longest chain mechanism, and the specific implementation process is similar to that in the related art and is not described herein again.
In the backward synchronization process (for a block with a block number smaller than the target block), validity verification may be performed on the block headers of a plurality of blocks with block numbers smaller than the target block in the block chain system, and then the plurality of blocks with block headers that pass the validity verification may be synchronized to the new node.
For example, the new node may first obtain a plurality of consecutive block headers from a block with a block number smaller than the target block, then obtain a plurality of consecutive block headers with the same identifier (e.g., the same block number) as the plurality of consecutive block headers from any whole node in the block chain system, and compare the plurality of consecutive block headers obtained from the whole node with the plurality of consecutive block headers obtained from a block with a block number smaller than the target block. If the comparison is consistent, the validity verification of the block head is passed, so that the block corresponding to the block head can be synchronized to the new node.
In another case, if the comparison is inconsistent, it indicates that the validity verification of the block header fails, so to ensure data security, the block corresponding to the block header is not synchronized to the new node, and the block whose validity verification of the block header fails may be discarded at the next data synchronization, and the synchronization process may be continued from the last block whose validity verification of the block header passed last time. For example, a plurality of consecutive block headers are acquired as block 5, block 4, and block 3, respectively, and when the validity verification of the block header is performed, the validity verification of the block header of block 4 fails, and then the validity verification of block 3, block 2, and block 1 may be continuously acquired in the next synchronization process.
By the method, other blocks except the target block in the blockchain system can be synchronized to the new node through bidirectional synchronization, so that the new node can more quickly synchronize all the blocks in the blockchain system, and the capability of the new node for executing service functions is enhanced.
In another possible way, when synchronizing other blocks in the blockchain system except the target block to the new node, if the new node is in the connectible state, a synchronized block with the smallest block number and a target block of the current blockchain system may be determined among the blocks synchronized to the new node, and then when the new node is in the connectible state, if a new block with a block number larger than the target block exists in the blockchain system, all blocks between the block with the largest block number in the new block and the target block are synchronized to the new node, and all blocks with a block number smaller than the synchronized block are synchronized to the new node.
In the process of synchronizing blocks other than the target block to a new node in the blockchain system, a new node may be dropped (in an unconnected state). For this case, when the new node comes online again (in a connectable state), the data that was not synchronized before the new node dropped may be synchronized continuously, and the data that was synchronized before the new node dropped may not need to repeat the data synchronization process. And, if a new block is generated during the drop of the new node, i.e. there is a new block with a block number greater than the target block in the blockchain system, all blocks between the block with the largest block number in the new block and the target block can be synchronized to the new node.
For example, the new node drops after synchronizing the data of blocks 10, 9, 8, 7, 6, in which case the synchronized block with the smallest block number in the blocks synchronized to the new node is block 6, the target block of the current blockchain system is block 10, and new blocks 11, 12, 13 are generated during the dropping of the new node. Thus, when the new node comes back on line, all blocks between block 13 (the block with the largest block number in the new block) and block 10 (the target block) can be synchronized to the new node, and all blocks with block numbers smaller than block 6 (the synchronized block) can continue to be synchronized to the new node.
By the method, when the new node is recovered to a connectable state from a non-connectable state, the unsynchronized data in the blockchain system can be continuously synchronized, and the synchronized data does not need to repeat the synchronization process again, so that the data synchronization process of the new node is accelerated, all data in the blockchain system can be synchronized more quickly by the new node, and the capability of the new node in executing the service function is enhanced.
The following describes a method for controlling a blockchain node according to the present disclosure by using a complete embodiment.
Referring to fig. 2, nodes a, B, and C are all nodes including all block data (block 1 to block N) of the current blockchain system, node E is a new node added to the blockchain system, and block N is a target block having a largest block number in the current blockchain system. After the new node E joins the blockchain system, the target block with the largest block number in the current blockchain system can be queried from the whole node a. In this case, the new node may query the target block as block N. Then, the new node E synchronizes the block N from the full node a to the new node, and after the synchronization of the block N to the new node E is completed, the service function that can be executed in the blockchain system and that has been configured by the new node may be started.
At this point, if a transaction F is made which is "Account X transfers 5 Meta to New Account Y" and is sent to the New node E. Since node E has synchronized the data in block N for "account X balance 10", new node E can verify the transaction F and can perform the business function on the transaction F. For example, the new node E may broadcast the transaction F to all neighboring nodes a and C. Alternatively, new node E may package transaction F as block N +1 and broadcast to the entire blockchain system, with data for "account X balance 5" and "account Y balance 5" at block N + 1.
Through the technical scheme, after the new node synchronizes the target block, the service function which can be executed in the block chain system and is configured by the new node can be started, namely, the service provided by the new node is started.
Based on the same inventive concept, the embodiment of the present disclosure further provides a management apparatus 300 for a blockchain node, which can be applied to a new node added to a blockchain system. Referring to fig. 3, the apparatus 300 may include:
a query module 301, configured to query a target block with a largest block number in the blockchain system from a target full node in the blockchain system, where the new node is connected to the target full node;
a synchronization module 302 for synchronizing the target block from the target full node to the new node;
a starting module 303, configured to start a service function that can be executed in the blockchain system and that is configured by the new node after the target block is synchronized to the new node.
Optionally, the starting module 303 is configured to determine whether a new block with a block number greater than that of the target block exists in the blockchain system, when the new block with the block number greater than that of the target block exists in the blockchain system, take the block with the largest block number in the new block as a new target block, and start a service function that can be executed in the blockchain system and is configured by the new node after the new target block is synchronized to the new node.
Optionally, the apparatus 300 further comprises:
a forward synchronization module, configured to synchronize, to the new node, a block in the block chain system whose block number is greater than the target block through a longest chain mechanism; and
and the backward synchronization module is used for synchronizing the blocks with the block heads passing the validity verification to the new node when the block heads of the blocks with the block numbers smaller than the target block in the block chain system pass the validity verification.
Optionally, the apparatus 300 further comprises:
a determining module, configured to determine, when other blocks in the blockchain system except the target block are synchronized to the new node and the new node is in an unconnected state, a synchronized block with a smallest block number and the target block of the current blockchain system from among the blocks synchronized to the new node;
and a synchronization execution module, configured to synchronize, to the new node, all blocks between a block with a largest block number in the new block and the target block, and synchronize, to the new node, all blocks with a block number smaller than the synchronized block, when the new node is in a connectable state and a new block with a block number larger than the target block exists in the blockchain system.
Optionally, the service functions that the new node has configured to be executable in the blockchain system include a broadcast service function, a new block service function, and an inquiry service function, where the new block service function is used to generate a new block.
Optionally, the apparatus 300 further comprises:
a second determining module, configured to determine, when a transaction is received after a service function that can be executed in the blockchain system and that is configured by the new node is started, whether data required for verifying the transaction is included in currently synchronized blockdata of the new node;
and the execution module is used for executing a broadcasting service function and a new block service function on the transaction when the block data synchronized currently by the new node comprises the data required for verifying the transaction, and the new block service function is used for generating a new block.
The specific manner in which each module performs operations has been described in detail in relation to the embodiments of the method with respect to any of the apparatuses in the above embodiments, and will not be elaborated upon here.
Through any one of the control devices, after the new node synchronizes the target block, the service function which can be executed in the blockchain system and is configured by the new node can be started, that is, the service provided by the new node is started.
Based on the same inventive concept, an embodiment of the present disclosure further provides an electronic device, including:
a memory having a computer program stored thereon;
a processor for executing the computer program in the memory to implement the steps of the control method of any blockchain node described above.
In one possible approach, a block diagram of the electronic device may be as shown in fig. 4. Referring to fig. 4, the electronic device 400 may include: a processor 401 and a memory 402. The electronic device 400 may also include one or more of a multimedia component 403, an input/output (I/O) interface 404, and a communications component 405.
The processor 401 is configured to control the overall operation of the electronic device 400, so as to complete all or part of the steps in the above method for controlling a blockchain node. The memory 402 is used to store various types of data to support operations at the electronic device 400, such as instructions for any application or method operating on the electronic device 400 and application-related data, such as block numbers, block headers, and so forth.
The Memory 402 may be implemented by any type of volatile or non-volatile Memory device or combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read-Only Memory (PROM), Read-Only Memory (ROM), magnetic Memory, flash Memory, magnetic disk or optical disk.
The multimedia components 403 may include a screen and an audio component. Wherein the screen may be, for example, a touch screen and the audio component is used for outputting and/or inputting audio signals. For example, the audio component may include a microphone for receiving external audio signals. The received audio signal may further be stored in the memory 402 or transmitted through the communication component 405. The audio assembly also includes at least one speaker for outputting audio signals. The I/O interface 404 provides an interface between the processor 401 and other interface modules, such as a keyboard, mouse, buttons, etc. These buttons may be virtual buttons or physical buttons.
The communication component 405 is used for wired or wireless communication between the electronic device 400 and other devices. Wireless Communication, such as Wi-Fi, bluetooth, Near Field Communication (NFC), 2G, 3G, 4G, NB-IOT, eMTC, or other 5G, etc., or a combination of one or more of them, which is not limited herein. The corresponding communication component 407 may therefore include: Wi-Fi module, Bluetooth module, NFC module, etc.
In an exemplary embodiment, the electronic Device 400 may be implemented by one or more Application Specific Integrated Circuits (ASICs), Digital Signal Processors (DSPs), Digital Signal Processing Devices (DSPDs), Programmable Logic Devices (PLDs), Field Programmable Gate Arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components, and is used for executing the control method of the above-mentioned block chain node.
In another exemplary embodiment, there is also provided a computer readable storage medium comprising program instructions which, when executed by a processor, implement the steps of the above-described method of controlling a blockchain node. For example, the computer readable storage medium may be the memory 402 comprising program instructions executable by the processor 401 of the electronic device 400 to perform the above-described control method of the blockchain node.
Through the electronic device or the storage medium, after the new node synchronizes the target block, the service function which is configured by the new node and can be executed in the block chain system can be started, that is, the service provided by the new node is started.
The preferred embodiments of the present disclosure are described in detail with reference to the accompanying drawings, however, the present disclosure is not limited to the specific details of the above embodiments, and various simple modifications may be made to the technical solution of the present disclosure within the technical idea of the present disclosure, and these simple modifications all belong to the protection scope of the present disclosure.
It should be noted that, in the foregoing embodiments, various features described in the above embodiments may be combined in any suitable manner, and in order to avoid unnecessary repetition, various combinations that are possible in the present disclosure are not described again.
In addition, any combination of various embodiments of the present disclosure may be made, and the same should be considered as the disclosure of the present disclosure, as long as it does not depart from the spirit of the present disclosure.