A method and apparatus for selecting an optimal node
By selecting the optimal node through chain coordination service, the latency problem caused by unreliable nodes in existing technologies is solved, and the timeliness and accuracy of data acquisition are achieved.
Patent Information
- Application Number
- CN202311165778.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-08
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2043-09-08
AI Technical Summary
Existing technologies cannot guarantee that the optimal node will be accessed, leading to block generation delays and data processing delays.
By initiating the chain coordination service and establishing connections with nodes, a new set of blocks is obtained. Their weight and height are compared to determine the set of the heaviest and largest blocks. The corresponding nodes are then placed into the optimal node set, and the target optimal node is selected based on the client request.
Ensure that the selected node has the latest on-chain information among the currently connected nodes, reducing data latency and improving the timeliness of data acquisition.
Smart Images

Figure CN117119065B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computers, and more particularly to a method and apparatus for selecting the optimal node. Background Technology
[0002] Blockchain projects often use libp2p to discover other nodes in the network, maintain node states, adjust network links based on node states, build a stable network topology, and handle data flow between nodes. In practice, to ensure stable access to node interfaces, multiple nodes are often deployed, and then Nginx is used as a proxy. When a downstream request is made to Nginx, Nginx selects a node from its proxied valid nodes and forwards the request. The selected node is guaranteed to be available, but it is not necessarily the optimal node, which can lead to block production delays and processing delays for some data. Nodes receive and transmit block data through libp2p. The time it takes for each node to receive block data is different, which can cause inconsistencies in the latest blocks of different nodes, especially when there are multiple blocks at a single height. Summary of the Invention
[0003] One objective of this application is to provide a method and apparatus for selecting the optimal node, thereby solving the problem in the prior art that the optimal node cannot be guaranteed to be accessed, resulting in block production delays and processing delays for some data.
[0004] According to one aspect of this application, a method for selecting an optimal node is provided, the method comprising:
[0005] Initiate the chain coordination service and establish a connection with at least one node;
[0006] The chain coordination service calls the block change interface of each node to obtain at least one new block set;
[0007] The weight and height of each new block set are compared with the existing chain to determine the heaviest and largest block set;
[0008] The node corresponding to the heaviest and largest block set is placed into the optimal node set, and the target optimal node is selected from the optimal node set based on the client's request.
[0009] Optionally, before the chain coordination service calls the block change interface of each node, it includes:
[0010] The node continuously synchronizes the blockchain height and provides a block change interface, through which it listens for block changes.
[0011] Optionally, the chain coordination service calls the block change interface of each node to obtain at least one new block set, including:
[0012] The chain coordination service calls the block change interface of each node to listen for block changes. When a change occurs, it aggregates all blocks at the height of the new block to obtain at least one new block set.
[0013] Optionally, the weight and height of each new block set are compared with the existing chain to determine the heaviest and largest block set, including:
[0014] The weight of each new block set is compared with the weight of the existing chain. If the weight of the new block set is greater than the weight of the existing chain, the new block set is taken as the heaviest and largest block set. If the weights are equal, the number of blocks at the current height is compared, and the block set with the more blocks is taken as the heaviest and largest block set.
[0015] Optionally, the node corresponding to the set of heaviest and largest blocks is placed into the optimal node set, including:
[0016] If the chain weight of the new block set is equal to the weight of the existing chain, then the node corresponding to the new block set is marked as high priority and placed in the optimal node set, while keeping the priorities of other nodes unchanged.
[0017] If the chain weight of the new block set is greater than the weight of the existing chain, then the node corresponding to the new block set is marked as high priority and placed into the optimal node set, and the other nodes are marked as low priority.
[0018] If the chain weight of the new block set is less than the weight of the existing chain, then the priority of all nodes in the optimal node set will remain low.
[0019] Optionally, selecting a target optimal node from the optimal node set based on the client's request includes:
[0020] Based on the client's request, the system polls the nodes marked as high priority from the optimal node set to select the target optimal node.
[0021] If no high-priority node is marked, then select from the low-priority nodes marked in a round-robin fashion.
[0022] Optionally, after selecting the target optimal node from the optimal node set based on the client's request, the process includes:
[0023] The client's request is forwarded to the target optimal node, which then returns node information, including block information, message information, storage provider information, and order information.
[0024] Optionally, before comparing the weight and height of each new block set with the existing chain, the following steps are included:
[0025] The weight of the new block set is calculated based on the number of blocks contained at the current block height and the weight of the parent block.
[0026] According to another aspect of this application, a device for selecting the optimal node is also provided, the device comprising:
[0027] One or more processors; and
[0028] A memory storing computer-readable instructions, which, when executed, cause the processor to perform operations as described above.
[0029] According to another aspect of this application, a computer-readable medium is also provided, having stored thereon computer-readable instructions that can be executed by a processor to implement the method as described above.
[0030] Compared to existing technologies, this application initiates a chain coordination service and establishes a connection with at least one node. The chain coordination service calls the block change interface of each node to obtain at least one new block set. The weight and height of each new block set are compared with the existing chain to determine the heaviest and largest block set. The node corresponding to the heaviest and largest block set is placed in the optimal node set, and a target optimal node is selected from the optimal node set based on the client's request. This ensures that the selected node has the latest on-chain information among the currently connected nodes, allowing downstream services to obtain the latest chain state information in a timely manner and reducing data latency. Attached Figure Description
[0031] Other features, objects, and advantages of this application will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings:
[0032] Figure 1 A schematic flowchart of a method for selecting the optimal node according to one aspect of this application is shown.
[0033] Figure 2 This diagram illustrates an interaction framework for selecting the optimal node in one embodiment of this application.
[0034] The same or similar reference numerals in the accompanying drawings represent the same or similar parts. Detailed Implementation
[0035] The present application will now be described in further detail with reference to the accompanying drawings.
[0036] In a typical configuration of this application, the terminal, the device of the service network, and the trusted party all include one or more processors (e.g., a central processing unit (CPU)), input / output interfaces, network interfaces, and memory.
[0037] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0038] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change RAM (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device. As defined in this article, computer-readable media do not include non-transitory computer-readable media, such as modulated data signals and carrier waves.
[0039] Figure 1 This diagram illustrates a method for selecting an optimal node according to one aspect of this application. The method includes steps S11 to S14, wherein...
[0040] Step S11: Start the chain coordination service and establish a connection with at least one node; here, the chain coordination service uses a chain coordinator (chain-co for short), which interacts with the nodes to establish a connection with at least one node, thereby obtaining on-chain information.
[0041] In step S12, the chain coordination service calls the block change interface of each node to obtain at least one new block set. Here, chain-co calls the block change interface of each node connected to it, listens for changes in node blocks in a loop, aggregates new blocks, and obtains a new block set. For example, when chain-co interacts with three nodes A, B, and C, it listens for the block change interfaces of nodes A, B, and C. If it detects changes in the blocks of nodes B and C, it aggregates the changed blocks of node B into the original blocks of node B, and aggregates the changed blocks of node C into the original blocks of node C, resulting in two new block sets, namely the new block set of node B and the new block set of node C.
[0042] Step S13: Compare the weight and height of each new block set with the existing chains to determine the largest and heaviest block set. Here, the node with the longest and heaviest chain is selected for comparison. The weight and height of the new block set obtained by listening are compared with the existing chains, where the longest and heaviest chain is called the existing chain. If the new block set is longer and heavier than the existing chain, then the new block set is the largest and heaviest block set, and the chain containing the new block set becomes the existing chain. The next new block set is compared with the new existing chain (the chain containing the current new block set). If no existing chain is longer and heavier, then the existing chain remains the largest and heaviest block set.
[0043] Step S14: The node corresponding to the largest and heaviest block set is placed into the optimal node set. Based on the client's request, a target optimal node is selected from the optimal node set. Here, the node containing the selected largest and heaviest block set is placed into the optimal node set, so that when chain-co receives a request from the client, it selects a node from the optimal node set and calls the selected node's interface. This ensures that the client's request retrieves data from the node with the longest and heaviest chain, thereby reducing the latency of retrieving on-chain data.
[0044] In one embodiment of this application, before the chain coordination service calls the block change interface of each node, the nodes continuously synchronize the blockchain height and provide a block change interface to monitor block changes. Here, as... Figure 2As shown, nodes are used to synchronize the blockchain network height and obtain the latest blockchain ledger information, i.e., to synchronize on-chain information. Network height refers to the height of the blockchain. Nodes also provide RPC interfaces for external service calls and to verify the validity of tokens carried in requests. In this embodiment, a node block change event is added. The node provides an interface for block change events, which external systems can notify to detect changes in node blocks. When a requester requests the block change interface, and the node obtains a new block, it returns all blocks at the same height to the requester through this interface. Each time, the block change interface returns a set of all blocks at the same height.
[0045] In one embodiment of this application, in step S12, the chain coordination service calls the block change interface of each node to listen for block changes of each node. When a change occurs, it aggregates all blocks at the height of the new block to obtain at least one new block set. Here, the Chain Coordination Service (chain-co) can act as a proxy for multiple nodes, interact with them, obtain on-chain information, and continuously monitor node block changes. When a node block change event occurs, it aggregates blocks, combining all blocks at the new block height to obtain a new block set. It then calculates the chain weight of the blockchain within this new block set, compares and selects the node with the longest and heaviest chain, and places that node into the optimal node set. Blocks aggregated at the same height must have the same parent block. A parent block is a block that contains a block from the previous height. For example, if height 3 has blocks A, B, and C, and height 4 has blocks D and E, and D's parent block is A, then D and E at the same height can be aggregated. Chain-co also verifies the validity of the token carried in the request, interacts with the client, provides an RPC interface, and returns the node request results.
[0046] In one embodiment of this application, the weight of the new block set is calculated based on the number of blocks contained at the current block height and the parent block weight. Here, the chain weight is calculated after aggregating blocks at a certain height, and is obtained based on the parent block weight and the number of blocks contained at the current block height. The parent block weight is the weight of the chain after aggregating blocks at the previous height, and the weight of the chain after aggregating blocks = parent block weight + number of blocks contained at the current block height multiplied by a coefficient.
[0047] In one embodiment of this application, in step S13, the weight of each new block set is compared with the existing chain. If the weight of the new block set is greater than the weight of the existing chain, the new block set is taken as the heaviest and largest block set. If the weights are equal, the number of currently highly aggregated blocks is compared, and the block set with the more blocks is taken as the heaviest and largest block set. (Continuing to refer to...) Figure 2Assuming there are three nodes, one chain-co, and multiple clients, when chain-co starts, it establishes connections with all three nodes and then calls the block change interface of each node. When each node obtains a new block, it returns all blocks at the height of the new block to chain-co, determining the new block set. chain-co calculates the weight of the received new block set and compares it with the existing longest and heaviest chain. First, it compares the weights; if the weight is greater, it updates and uses the new block set as the heaviest and largest block set. If the weights are equal, it compares the number of blocks aggregated at the current height; if the number of blocks is greater, it updates. If both the weight and the number of aggregated blocks are equal, it also updates. Specifically, if the new block set has a greater weight, it is considered the heaviest block set. If two block sets have the same weight, the number of blocks they contain is compared, and the one with the larger number is considered the heaviest block set. If both the weight and the number of blocks in the new block set are the same as the heaviest block set, the new block set is also considered the heaviest block set. The node corresponding to the heaviest block set is then the node of the longest and heaviest chain and is placed in the optimal node set. When a client requests chain-co, chain-co prioritizes selecting the optimal node and forwards the request to it. If there are multiple optimal nodes, it uses a round-robin approach to select one of them.
[0048] In one embodiment of this application, in step S14, if the chain weight of the new block set is equal to the weight of the existing chain, the node corresponding to the new block set is marked as high priority and placed in the optimal node set, while the priorities of other nodes remain unchanged; if the chain weight of the new block set is greater than the weight of the existing chain, the node corresponding to the new block set is marked as high priority and placed in the optimal node set, while the priorities of other nodes are marked as low priority; if the chain weight of the new block set is less than the weight of the existing chain, the priorities of all nodes remain unchanged as low priority. Here, nodes are divided into two priorities: high priority and low priority, with the default priority of nodes being low priority; the new block set is compared with the existing longest and heaviest chain. If the weights are equal, the node corresponding to the new block set is set as high priority, while the priorities of the other nodes remain unchanged; if the weight of the new block set is greater than the weight of the existing longest and heaviest chain, the node corresponding to the new block set is marked as high priority, while the priorities of the other nodes are set as low priority; if the weight of the new block set is less than the weight of the existing longest and heaviest chain, the priorities of all nodes remain unchanged.
[0049] Following the above embodiment, in step S14, based on the client's request, nodes marked as high priority are selected from the optimal node set through a round-robin process to choose the target optimal node; if no high-priority node is marked, then nodes are selected from the low-priority node set through a round-robin process. Here, nodes are divided into a high-priority set and a low-priority set according to their priority. First, nodes are selected from the high-priority set through a round-robin process. If there are no nodes in the high-priority set, then nodes are selected from the low-priority set through a round-robin process. Since the node priority is low when the program starts, there may be a situation where there are no nodes in the high-priority set.
[0050] In one embodiment of this application, the client's request is forwarded to the target optimal node, and the target optimal node returns request result information, wherein the request result information includes block information, message information, storage provider information, and order information. Here, when a client requests chain-co, chain-co will prioritize selecting the optimal node, that is, select the target optimal node and forward the request to the target optimal node. If there are multiple optimal nodes, it will round-robin select one of them as the target optimal node, call the interface of the selected target optimal node, and the node will return the request result data. The request result data includes any one or a combination of block information, message information, storage provider information, and order information. Block information includes block proof, parent block set, parent block weight, block height, block message, message signature set, block time, and block signature, etc. Message information includes message version, message recipient, message sender, transfer amount, and message parameters, etc. Storage provider information includes the computing power of the storage provider, the total computing power of the entire chain, sector information, working address, and sector size, etc. Order information includes order size, verification information, sending order address, receiving order address, and order status, etc.
[0051] By using the above method, chain-co can ensure that the selected node has the latest on-chain information among the currently connected nodes, and that downstream services can obtain the latest chain status information in a timely manner, reducing data latency.
[0052] Furthermore, embodiments of this application also provide a computer-readable medium storing computer-readable instructions that can be executed by a processor to implement the aforementioned method for selecting an optimal node.
[0053] Corresponding to the methods described above, this application also provides a terminal, which includes components capable of performing the above-described... Figure 1 or Figure 2The modules or units described in the various embodiments may be implemented in hardware, software, or a combination of both, and this application is not limited thereto. For example, in one embodiment of this application, a device for selecting the optimal node is also provided, the device comprising:
[0054] One or more processors; and
[0055] A memory storing computer-readable instructions, which, when executed, cause the processor to perform operations as described above.
[0056] For example, computer-readable instructions, when executed, cause the one or more processors to:
[0057] Initiate the chain coordination service and establish a connection with at least one node;
[0058] The chain coordination service calls the block change interface of each node to obtain at least one new block set;
[0059] The weight and height of each new block set are compared with the existing chain to determine the heaviest and largest block set;
[0060] The node corresponding to the heaviest and largest block set is placed into the optimal node set, and the target optimal node is selected from the optimal node set based on the client's request.
[0061] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.
[0062] It should be noted that this application can be implemented in software and / or a combination of software and hardware, for example, using an application-specific integrated circuit (ASIC), a general-purpose computer, or any other similar hardware device. In one embodiment, the software program of this application can be executed by a processor to implement the steps or functions described above. Similarly, the software program of this application (including related data structures) can be stored in a computer-readable recording medium, such as RAM memory, magnetic or optical drives, floppy disks, and similar devices. Furthermore, some steps or functions of this application can be implemented in hardware, for example, as circuitry that cooperates with a processor to perform the various steps or functions.
[0063] Furthermore, a portion of this application can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide the methods and / or technical solutions according to this application through the operation of the computer. The program instructions invoking the methods of this application may be stored in a fixed or removable recording medium, and / or transmitted via data streams in broadcast or other signal carrying media, and / or stored in the working memory of a computer device operating according to the program instructions. Here, one embodiment of this application includes an apparatus comprising a memory for storing computer program instructions and a processor for executing the program instructions, wherein, when the computer program instructions are executed by the processor, the apparatus is triggered to operate the methods and / or technical solutions based on the foregoing embodiments of this application.
[0064] It will be apparent to those skilled in the art that this application is not limited to the details of the exemplary embodiments described above, and that it can be implemented in other specific forms without departing from the spirit or essential characteristics of this application. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of this application is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be embraced within this application. No reference numerals in the claims should be construed as limiting the scope of the claims. Furthermore, it is clear that the word "comprising" does not exclude other elements or steps, and the singular does not exclude the plural. Terms such as "first," "second," etc., are used to denote names and do not indicate any particular order.
Claims
1. A method for selecting the optimal node, characterized in that, The method includes: Initiate the chain coordination service and establish a connection with at least one node; The chain coordination service calls the block change interface of each node to obtain at least one new block set; The weight and height of each new block set are compared with the existing chain to determine the heaviest and largest block set; Place the node corresponding to the heaviest and largest block set into the optimal node set, and select the target optimal node from the optimal node set based on the client's request; The step of comparing the weight and height of each new block set with the existing longest and heaviest chain to determine the heaviest and largest block set includes: comparing the weight of each new block set with the weight of the existing chain; if the weight of the new block set is greater than the weight of the existing chain, then the new block set is taken as the heaviest and largest block set; if the weights are equal, then the number of blocks aggregated at the current height is compared, and the block set with the more blocks is taken as the heaviest and largest block set. The step of placing the node corresponding to the heaviest and largest block set into the optimal node set includes: if the chain weight of the new block set is equal to the weight of the existing chain, then the node corresponding to the new block set is marked as high priority and placed into the optimal node set, while keeping the priorities of other nodes unchanged; if the chain weight of the new block set is greater than the weight of the existing chain, then the node corresponding to the new block set is marked as high priority and placed into the optimal node set, while the priorities of other nodes are marked as low priority; if the chain weight of the new block set is less than the weight of the existing chain, then the priorities of all nodes in the optimal node set remain low priority. The process of selecting the target optimal node from the optimal node set based on the client request includes: polling the nodes marked as high priority from the optimal node set based on the client request to select the target optimal node; if there are no marked high priority nodes, then polling the marked low priority nodes.
2. The method according to claim 1, characterized in that, Before the chain coordination service calls the block change interface of each node, it includes: The node continuously synchronizes the blockchain height and provides a block change interface, through which it listens for block changes.
3. The method according to claim 2, characterized in that, The chain coordination service calls the block change interface of each node to obtain at least one new block set, including: The chain coordination service calls the block change interface of each node to listen for block changes. When a change occurs, it aggregates all blocks at the height of the new block to obtain at least one new block set.
4. The method according to claim 1, characterized in that, After selecting the target optimal node from the optimal node set based on the client's request, the process includes: The client's request is forwarded to the target optimal node, which then returns node information, including block information, message information, storage provider information, and order information.
5. The method according to claim 1, characterized in that, Before comparing the weight and height of each new block set with the existing chain, the following steps are included: The weight of the new block set is calculated based on the number of blocks contained at the current block height and the weight of the parent block.
6. A device for selecting the optimal node, characterized in that, The device includes: One or more processors; and A memory storing computer-readable instructions, which, when executed, cause the processor to perform the operations of the method as described in any one of claims 1 to 5.
7. A computer-readable medium having stored thereon computer-readable instructions that can be executed by a processor to implement the method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Block chain node synchronization method and device
CN113037852A
Method, device and equipment for selecting credible coordinator for federal learning
CN114721789A
