Block query method and device, equipment and storage medium

By acquiring and converting time information into a target timestamp, and combining a search algorithm and an RPC interface, efficient querying of a specified block in the blockchain is achieved, solving the problem of low query efficiency in existing technologies and improving user experience and analysis efficiency.

CN115422235BActive Publication Date: 2026-04-07THE PEOPLES BANK OF CHINA DIGITAL CURRENCY INST
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-22
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

How to efficiently query a specific block in a blockchain for statistical analysis? Current technologies suffer from low query efficiency and poor user experience.

Method used

The query request for obtaining time information is converted into a target timestamp. A preset search algorithm is used to determine the current block to be queried, and the block number is output on the blockchain. The timestamp of the block is obtained through the RPC interface to achieve precise positioning of the block number.

Benefits of technology

It improves the efficiency and accuracy of block queries, simplifies the subsequent statistical analysis process, and enhances the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115422235B_ABST
    Figure CN115422235B_ABST
Patent Text Reader

Abstract

This application relates to a block query method, apparatus, device, and storage medium, and pertains to the field of blockchain technology, for querying a specified block from a blockchain. The method includes: obtaining a query request; the query request includes time information; the query request is used to query the block number corresponding to the time information; converting the time information into a target timestamp; determining the current block to be queried from a specified block range on the blockchain, inputting the block number of the current block to be queried into a preset remote procedure call (RPC) interface, and outputting the timestamp of the current block to be queried; if the timestamp of the current block to be queried is the same as the target timestamp, determining the block number corresponding to the time information as the block number of the current block to be queried.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of blockchain technology, and in particular to a block query method, apparatus, device and storage medium. Background Technology

[0002] Blockchain is a decentralized shared ledger that combines blocks into a specific data structure in chronological order and is cryptographically guaranteed to be immutable and unforgeable.

[0003] In practical applications, it is often necessary to retrieve specific blocks for statistical analysis. Therefore, how to query specific blocks from the blockchain has become a pressing technical problem that needs to be solved. Summary of the Invention

[0004] This application provides a block query method, apparatus, device, and storage medium for retrieving a specified block from a blockchain. The technical solution of this application is as follows:

[0005] According to a first aspect of the embodiments of this application, a block query method is provided. The method includes: obtaining a query request; the query request includes time information; the query request is used to query the block number corresponding to the time information; converting the time information into a target timestamp; determining the current block to be queried from a specified block range on the blockchain, and inputting the block number of the current block to be queried into a preset remote procedure call (RPC) interface, and outputting the timestamp of the current block to be queried; if the timestamp of the current block to be queried is the same as the target timestamp, determining the block number corresponding to the time information as the block number of the current block to be queried.

[0006] In one possible implementation, determining the current block to be queried includes: obtaining the block number of the starting block and the block number of the ending block; the starting block is the first block in the blockchain; the ending block is the last block in the blockchain; the block numbers of each block in the blockchain are incremented according to the connection order of the blocks, and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value; the current block to be queried is determined according to a preset search algorithm, the block number of the starting block, and the block number of the ending block.

[0007] In one possible implementation, the preset search algorithm is a binary search algorithm. The current block to be queried is determined according to the preset search algorithm, the block number of the starting block and the block number of the ending block. This includes: determining the intermediate block number according to the preset search algorithm, the block number of the starting block and the block number of the ending block, and determining the block corresponding to the intermediate block number as the current block to be queried.

[0008] In one possible implementation, the method further includes: if the timestamp of the current block to be queried is different from the target timestamp, determining the next block to be queried and obtaining the timestamp of the next block to be queried; if the timestamp of the next block to be queried is the same as the target timestamp, outputting the block number of the next block to be queried.

[0009] In one possible implementation, when the timestamp of the current block to be queried is different from the target timestamp, determining the next block to be queried includes: if the timestamp of the current block to be queried is greater than the target timestamp, taking the current block to be queried as the end block, and determining the next block to be queried based on the block number of the end block, the block number of the start block, and a preset search algorithm; if the timestamp of the current block to be queried is less than the target timestamp, taking the current block to be queried as the start block, and determining the next block to be queried based on the block number of the start block, the block number of the end block, and a preset search algorithm.

[0010] In one possible implementation, when the timestamp of the current block to be queried is different from the target timestamp, determining the next block to be queried includes: when the timestamp of the current block to be queried is greater than the target timestamp, taking the previous block of the current block to be queried as the ending block, and determining the next block to be queried based on the block number of the ending block, the block number of the starting block, and a preset search algorithm; when the timestamp of the current block to be queried is less than the target timestamp, taking the next block of the current block to be queried as the starting block, and determining the next block to be queried based on the block number of the starting block, the block number of the ending block, and a preset search algorithm.

[0011] In one possible implementation, obtaining a query request includes: obtaining a query request in response to a user's query operation; the query operation includes an input operation and a click operation; the input operation is used to input time information; the click operation is used to trigger the query request.

[0012] In one possible implementation, the time information includes start time information and end time information, and the target timestamp includes a first target timestamp and a second target timestamp, wherein the first target timestamp is converted from the start time information and the second timestamp is converted from the end time information; when the timestamp of the current block to be queried is the same as the target timestamp, determining the block number corresponding to the time information as the block number of the current block to be queried includes: when the timestamp of the current block to be queried is the same as the first target timestamp, determining the interval number corresponding to the start time information as the block number of the current block to be queried; or, when the timestamp of the current block to be queried is the same as the second target timestamp, determining the interval number corresponding to the end time information as the block number of the current block to be queried; after determining the block number corresponding to the time information as the block number of the current block to be queried, the method further includes: obtaining the target block number range based on the interval number corresponding to the start time information and the interval number corresponding to the end time information; the target block number range is the range of block numbers corresponding to the time period of the start time information and the end time information.

[0013] According to a second aspect of the embodiments of this application, a block query apparatus is provided. The apparatus includes an acquisition unit, a processing unit, and a determination unit. The acquisition unit is used to acquire a query request. The query request includes time information. The query request is used to query the block number corresponding to the time information. The processing unit is used to convert the time information into a target timestamp. The determination unit is used to determine the block to be queried from a specified block range on the blockchain, input the block number of the block to be queried into a preset remote procedure call (RPC) interface, and output the timestamp of the block to be queried. The determination unit is further used to determine the block number corresponding to the time information as the block number of the block to be queried if the timestamp of the block to be queried is the same as the target timestamp.

[0014] In one possible implementation, the determining unit is specifically used to: obtain the block number of the starting block and the block number of the ending block; the starting block is the first block in the blockchain; the ending block is the last block in the blockchain; the block numbers of each block in the blockchain are incremented according to the connection order of each block, and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value; and determine the current block to be queried according to a preset search algorithm, the block number of the starting block, and the block number of the ending block.

[0015] In one possible implementation, the preset search algorithm is a binary search algorithm, and the determining unit is specifically used to: determine the intermediate block number according to the preset search algorithm, the block number of the starting block and the block number of the ending block, and determine the block corresponding to the intermediate block number as the current block to be queried.

[0016] In one possible implementation, the determining unit is further configured to: determine the next block to be queried and obtain the timestamp of the next block to be queried when the timestamp of the current block to be queried is different from the target timestamp; the output unit is further configured to output the block number of the next block to be queried when the timestamp of the next block to be queried is the same as the target timestamp.

[0017] In one possible implementation, the determining unit is specifically configured to: if the timestamp of the current block to be queried is greater than the target timestamp, take the current block to be queried as the end block, and determine the next block to be queried based on the block number of the end block, the block number of the start block, and a preset search algorithm; if the timestamp of the current block to be queried is less than the target timestamp, take the current block to be queried as the start block, and determine the next block to be queried based on the block number of the start block, the block number of the end block, and a preset search algorithm.

[0018] In one possible implementation, the acquisition unit is specifically used to: acquire a query request in response to a user's query operation; the query operation includes an input operation and a click operation; the input operation is used to input time information; the click operation is used to trigger the query request.

[0019] In one possible implementation, the time information includes start time information and end time information, and the target timestamp includes a first target timestamp and a second target timestamp, wherein the first target timestamp is obtained by converting the start time information and the second timestamp is obtained by converting the end time information; the determining unit is specifically used to: determine the interval number corresponding to the start time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the first target timestamp; or, determine the interval number corresponding to the end time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the second target timestamp; the determining unit is further used to: after determining that the block number corresponding to the time information is the block number of the current block to be queried, obtain the target block number range according to the interval number corresponding to the start time information and the interval number corresponding to the end time information; the target block number range is the range of block numbers corresponding to the time period of the start time information and the end time information.

[0020] According to a third aspect of the embodiments of this application, an electronic device is provided, comprising: a processor; and a memory for storing processor-executable instructions; wherein the processor is configured to execute instructions to implement the method as described in the first aspect above.

[0021] According to a fourth aspect of the embodiments of this application, a computer-readable storage medium is provided, which enables an electronic device to perform the method as described in the first aspect when computer-executable instructions stored in the computer-readable storage medium are executed by a processor of an electronic device.

[0022] According to a fifth aspect of the embodiments of this application, a computer program product is provided, the computer program product including computer instructions, which, when executed on an electronic device, cause the electronic device to perform the method as described in the first aspect above.

[0023] The technical solution of the first aspect provided by the embodiments of this application brings at least the following beneficial effects: The block query device obtains a query request including time information, and further converts the time information into a target timestamp to clarify the time specified in the query request. The block query device determines the current block to be queried and obtains the timestamp of the current block to be queried; if the timestamp of the current block to be queried is the same as the target timestamp, the block query device outputs the block number of the current block to be queried. In this way, blocks at a specified time can be queried, which greatly facilitates subsequent statistical analysis.

[0024] It should be noted that the technical effects of any of the implementation methods in aspects two through five can be found in the technical effects of the corresponding implementation methods in aspect one, and will not be repeated here.

[0025] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and do not limit this application. Attached Figure Description

[0026] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application, and do not constitute an undue limitation of this application.

[0027] Figure 1 This is a schematic diagram of a blockchain structure according to an exemplary embodiment;

[0028] Figure 2 This is a schematic diagram illustrating the structure of a blockchain query system according to an exemplary embodiment;

[0029] Figure 3 This is one of the flowcharts illustrating another block query method according to an exemplary embodiment;

[0030] Figure 4 This is a schematic diagram illustrating the process of obtaining a timestamp via an RPC interface according to an exemplary embodiment;

[0031] Figure 5 This is a second flowchart illustrating another block query method according to an exemplary embodiment;

[0032] Figure 6 This illustrates the connection relationship between blocks in a blockchain according to an exemplary embodiment;

[0033] Figure 7 This is a flowchart of another block query method illustrated according to an exemplary embodiment;

[0034] Figure 8 This is a flowchart of another block query method according to an exemplary embodiment;

[0035] Figure 9 This is a flowchart illustrating, according to an exemplary embodiment, how to use a binary search algorithm to query the block number corresponding to a start date;

[0036] Figure 10 This is a schematic diagram illustrating the structure of a block query device according to an exemplary embodiment;

[0037] Figure 11 This is a schematic diagram of the structure of an electronic device according to an exemplary embodiment. Detailed Implementation

[0038] To enable those skilled in the art to better understand the technical solutions of this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings.

[0039] It should be noted that the terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0040] Furthermore, in the description of the embodiments of this disclosure, unless otherwise stated, " / " means "or," for example, A / B can mean A or B. The term "and / or" in this document is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, in the description of the embodiments of this disclosure, "multiple" refers to two or more.

[0041] Before providing a detailed explanation of the embodiments of this application, some relevant technical terms and technologies involved in the embodiments of this application will be introduced first.

[0042] Blockchain: A blockchain is a chain-like data structure composed of blocks. Each block stores specific information, and these blocks are linked together in chronological order of their creation. This chain is stored on all servers, and as long as one server in the entire system is operational, the entire blockchain is secure. These servers are called nodes in the blockchain system, and they provide storage space and computing power for the entire system. To modify information in the blockchain, the consent of more than half of the nodes must be obtained, and the information in all nodes must be modified. Since these nodes are usually controlled by different entities, tampering with information in the blockchain is extremely difficult. Compared to traditional networks, blockchain has two core characteristics: first, the data is difficult to tamper with; second, it is decentralized. Based on these two characteristics, the information recorded by the blockchain is more authentic and reliable, helping to solve the problem of mutual distrust.

[0043] A timestamp is a complete and verifiable piece of data that indicates it existed before a specific time. It is typically a sequence of characters that uniquely identifies a particular moment in time. In a blockchain, the timestamp is located in the header of each block and is used to record when the block was created.

[0044] Block Number: Also known as a block identifier, the block number is used to identify different blocks. In a blockchain, each block has a unique block number, and the block numbers of each block increase sequentially according to the connection order of the blocks.

[0045] For ease of understanding, such as Figure 1 The diagram illustrates the structure of a blockchain, including blocks with block number n-1, block number n, and block number n+1. Block n-1 is concatenated with block n, and block n is concatenated with block n+1. Each block consists of a block header and a block body. The block header stores the block's verification information, including the version number, timestamp, target hash, random number, parent block hash, and Merkle root. The functions of each field are shown in Table 1. The block header condenses the blockchain's data information and uniquely identifies the block's position in the chain, facilitating tracing and retrieval.

[0046] Table 1

[0047] Field Name Function Description size Version number Describe the software version number and tracking agreement. 4B Timestamp Block generation time 4B Target hash Difficulty Target 4B random numbers Solution to the problem 4B Parent block hash Referencing the hash value of the parent block 32B Merkle root The set of transaction hashes in a block 32B

[0048] Blockchain technology is often used in the financial field. For example, it can package and record multiple transactions generated at the same time point in a block. That is, each block records transaction-related information at different time points, and each block has its own timestamp and block number.

[0049] In practical applications, transaction information often requires statistical analysis, which necessitates retrieving blocks from a specific date from the blockchain to obtain the necessary transaction information. However, blockchain networks contain a massive amount of transaction information stored across various blocks. For users to find the specific transaction information they need, they must traverse from the genesis block to the last block of the blockchain, resulting in long query times, low efficiency, a poor user experience, and limited blockchain applications. Therefore, finding a specific block from the blockchain has become a pressing technical problem that needs to be solved.

[0050] To address the aforementioned issues, this application provides a block query method. The block query device acquires a query request including time information, and further converts the time information into a target timestamp to clarify the time specified in the query request. The block query device determines the block to be queried and acquires the timestamp of the block to be queried. If the timestamp of the block to be queried is the same as the target timestamp, the block query device outputs the block number of the block to be queried. This allows for the querying of blocks at a specified time, greatly facilitating subsequent statistical analysis.

[0051] The block query method provided in this application can be applied to blockchain query systems. Figure 2 A schematic diagram of one structure of this blockchain query system is shown. For example... Figure 2 As shown, the blockchain query system 10 includes a block query device 11 and a server 12. The block query device 11 is connected to the server 12. The block query device 11 and the server 12 can be connected via a wired or wireless connection; this embodiment of the invention does not limit this. In other embodiments, the block query device 11 may also be integrated into the server 12, and the block query method is executed by the server 12; this application does not limit this as well.

[0052] The block query device 11 is used to obtain query requests and convert time information into a target timestamp. The block query device 11 is also used to determine the current block to be queried and obtain the timestamp of the current block to be queried. The block query device 11 is also used to output the block number of the current block to be queried if the timestamp of the current block to be queried is the same as the target timestamp.

[0053] Server 12 is used to store the blockchain, which consists of multiple blocks. The structure of the blockchain can be referenced. Figure 1 Server 12 can be any server in the financial field, such as a bank server, or server 12 can be a server cluster composed of multiple servers, which is not limited in this application.

[0054] Furthermore, it should be noted that the blockchain information involved in this application (including but not limited to block numbers, timestamps, and other blockchain information) is all information authorized by the user or fully authorized by all parties.

[0055] Figure 3 This is a flowchart illustrating a block query method according to some exemplary embodiments. In some embodiments, the above-described block query method can be applied to, for example... Figure 1 The block query device shown can also be applied to other similar devices.

[0056] like Figure 3 As shown, the block query method provided in this embodiment includes the following steps S201-S205.

[0057] S201, The block query device obtains a query request.

[0058] The query request includes time information; the query request is used to query the block number corresponding to the time information.

[0059] As one possible implementation, the block query device responds to the user's query operation and obtains the query request.

[0060] Specifically, the query operation includes an input operation and a click operation; the input operation is used to input time information; the click operation is used to trigger a query request. That is, the user inputs time information through the input operation, and the click operation prompts the block query device to generate a query request that includes the input time information. Furthermore, the block query device obtains the query request.

[0061] It should be noted that the time information is set by the user according to their actual needs. The time information can be a single point in time or a period of time. For example, if a user needs to query blocks within a certain date range, the time information includes the start date and the end date.

[0062] For example, a user enters a start date and an end date in the block query device and clicks to confirm the query. Further, the block query device obtains a query request including the start date and the end date.

[0063] S202, The block query device converts time information into a target timestamp.

[0064] As one possible implementation, the block query device converts time information into a target timestamp according to a preset time conversion function.

[0065] It should be noted that the time conversion function is pre-configured by operations and maintenance personnel in the block query device. For example, the time conversion function can be a PostgreSQL function.

[0066] For example, the block query device converts a date to a timestamp using a PostgreSQL function. The block query device inputs the date, 2021-12-02 08:00:00, into the PostgreSQL function `postgresql{select EXTRACT(epoch FROM CAST('2021-12-02 08:00:00'ASTIMESTAMP))}`. Further, PostgreSQL outputs the timestamp 1638403200.

[0067] Understandably, since blockchain uses timestamps to reflect the time when blocks are generated, in order to successfully query blocks for a specified date, the block query device needs to convert the start and end dates entered by the user into corresponding target timestamps.

[0068] S203, The block query device determines the block to be queried from a specified range of blocks on the blockchain.

[0069] It should be noted that the specified block range is set in advance by the operation and maintenance personnel in the block query device. This range can be all the blockchains stored in the server or a portion of the blockchains. This application does not limit this.

[0070] As one possible implementation, the block query device identifies a preset block in the server as the block to be queried.

[0071] For example, the block query device identifies the block at the top of the blockchain in the server as the block to be queried.

[0072] For another example, the block query device identifies the block located in the middle of the blockchain in the server as the block to be queried.

[0073] As another possible implementation, the block query device obtains the block number of the starting block and the block number of the ending block from the server, and determines the current block to be queried based on the preset search algorithm, the block number of the starting block and the block number of the ending block.

[0074] The specific implementation of this step can be found in the subsequent descriptions in the embodiments of this invention, and will not be repeated here.

[0075] S204. The block query device obtains the timestamp of the block to be queried.

[0076] As one possible implementation, after determining the block to be queried, the block query device obtains the block number of the block to be queried from the server. Further, the block query device inputs the block number of the block to be queried into a preset remote procedure call (RPC) interface and outputs the timestamp of the block to be queried.

[0077] It should be noted that the RPC interface is pre-configured by the operations and maintenance personnel in the block query device. For example, the RPC interface includes getblockhashblock_id (i.e., the first RPC interface) and getblock hash (i.e., the second RPC interface). Among them, getblockhashblock_id is used to obtain the block hash based on the block number, and getblock hash is used to obtain the block timestamp based on the block hash.

[0078] For example, such as Figure 4 The diagram illustrates the process of obtaining a timestamp via an RPC interface. The block query device inputs the block number of the block to be queried into the first RPC interface, such as `getblockhashblock_id`, to obtain the block hash of the block to be queried. Further, the block query device inputs the block hash of the block to be queried into the second RPC interface, such as `getblockhash`, to obtain the timestamp of the block to be queried.

[0079] S205. If the timestamp of the current block to be queried is the same as the target timestamp, the block query device determines the block number corresponding to the time information as the block number of the current block to be queried.

[0080] As one possible implementation, the block query device compares the timestamp of the current block to be queried with the target timestamp to determine if they are the same. If they are the same, the block query device outputs the block number of the current block to be queried.

[0081] In some embodiments, the target timestamp includes a timestamp corresponding to the start date and a timestamp corresponding to the end date. The block query device may first compare the timestamp of the current block to be queried with the timestamp corresponding to the start date, and then compare the timestamp of the current block to be queried with the timestamp corresponding to the end date; alternatively, the block query device may first compare the timestamp of the current block to be queried with the timestamp corresponding to the end date, and then compare the timestamp of the current block to be queried with the timestamp corresponding to the start date; or the block query device may simultaneously compare the timestamp of the current block to be queried with both the timestamp corresponding to the start date and the timestamp corresponding to the end date. This application embodiment does not limit this approach.

[0082] Understandably, if the timestamp of the block to be queried is the same as the timestamp corresponding to the start date, the block query device will identify the block to be queried as the block corresponding to the start date and output its block number. If the timestamp of the block to be queried is the same as the timestamp corresponding to the end date, the block query device will identify the block to be queried as the block corresponding to the end date and output its block number. In this way, the block query device can retrieve the block number corresponding to both the start and end dates. Due to the continuity of block numbers in the blockchain, it can then obtain all block numbers between the start and end dates. For example, if the block query device finds that the block number corresponding to the start date is 15 and the block number corresponding to the end date is 35, then block numbers 15-35 represent all block numbers between the start and end dates.

[0083] In a design, such as Figure 5 As shown, in order to determine the current block to be queried, the above-mentioned S203 provided in this application embodiment specifically includes the following S2031-S2032:

[0084] S2031, The block query device obtains the block number of the starting block and the block number of the ending block.

[0085] The starting block is the first block in the blockchain; the ending block is the last block in the blockchain; the block number of each block in the blockchain increases in the order of their connection; and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value.

[0086] For example, such as Figure 6The diagram illustrates the connection relationships between blocks in a blockchain. This blockchain consists of n+1 blocks, which are connected sequentially according to their generation time. The first block is block 0, the second block is block 1, ..., and the last block is block n. The block query device uses the first block in the blockchain as the starting block and obtains its block number (i.e., block 0); the block query device uses the last block in the blockchain as the ending block and obtains its block number (i.e., block n).

[0087] S2032. The block query device determines the current block to be queried based on the preset search algorithm, the block number of the starting block, and the block number of the ending block.

[0088] As one possible implementation, the block query device substitutes the block number of the starting block and the block number of the ending block into a preset search algorithm to calculate the block number of the current block to be queried, thereby determining the current block to be queried.

[0089] In some embodiments, the preset search algorithm is a binary search algorithm. The block query device calculates the intermediate block number according to the binary search algorithm and determines the block corresponding to the intermediate block number as the current block to be queried.

[0090] For example, if the starting block number is 0 and the ending block number is n, the block query device calculates the intermediate block number as int(0+n) / 2 according to the binary search algorithm. Int represents the integer operator, that is, when the denominator is odd, the binary search algorithm will round to the left or right according to the calculation result.

[0091] In a design, such as Figure 7 As shown, the block query method provided in this application embodiment also includes the following S206-S207.

[0092] S206. If the timestamp of the current block to be queried is different from the target timestamp, the block query device determines the next block to be queried and obtains the timestamp of the next block to be queried.

[0093] As one possible implementation, the block query device compares the timestamp of the current block to be queried with the target timestamp to determine whether the timestamps are the same. If the timestamps are different, the block query device determines the next block to be queried and obtains its timestamp.

[0094] S207. If the timestamp of the next block to be queried is the same as the target timestamp, the block query device outputs the block number of the next block to be queried.

[0095] The specific implementation method for this step can be referred to in S205 above, and will not be repeated here. The difference is that the current block to be queried in S205 is replaced with the next block to be queried.

[0096] Understandably, if the timestamp of the current block to be queried differs from the target timestamp, the block query device will determine the next block to be queried and check if its timestamp matches the target timestamp. If they match, the block query device will output the block number of the next block to be queried. If they differ, the block query device will determine a new block to be queried again, repeating the above process until a block with the same timestamp as the target is found. If no block with the same timestamp as the target does not exist in the blockchain, the block query device will ultimately output a preset value (e.g., error code -1).

[0097] In a design, such as Figure 8 As shown, in order to determine the next block to be queried, the above-mentioned S206 provided in this application embodiment specifically includes the following S2061-S2062:

[0098] S2061. If the timestamp of the current block to be queried is greater than the target timestamp, the block query device takes the current block to be queried as the end block and determines the next block to be queried based on the block number of the end block, the block number of the start block and the preset search algorithm.

[0099] As one possible implementation, the block query device compares the timestamp of the next block to be queried with the target timestamp. If the timestamp of the current block to be queried is greater than the target timestamp (i.e., the time represented by the timestamp of the current block to be queried is later than the time represented by the target timestamp), the block query device takes the current block to be queried as the end block and determines the next block to be queried based on the block number of the end block, the block number of the start block, and the preset search algorithm.

[0100] The specific implementation method for this step can be found in S2032 above, and will not be repeated here. The difference is that the ending block in S2032 is replaced with the current block to be queried.

[0101] S2062. If the timestamp of the current block to be queried is less than the target timestamp, the block query device takes the current block to be queried as the starting block and determines the next block to be queried based on the block number of the starting block, the block number of the ending block and the preset search algorithm.

[0102] As one possible implementation, the block query device compares the timestamp of the next block to be queried with the target timestamp. If the timestamp of the current block to be queried is less than the target timestamp, the block query device takes the current block to be queried as the starting block and determines the next block to be queried based on the block number of the starting block, the block number of the ending block, and the preset search algorithm.

[0103] The specific implementation method for this step can be found in S2032 above, and will not be repeated here. The difference is that the starting block in S2032 is replaced with the current block to be queried.

[0104] Optionally, if the timestamp of the current block to be queried is greater than the target timestamp, the block query device can also use the block preceding the current block to be queried as the ending block, and determine the next block to be queried based on the block number of the ending block, the block number of the starting block, and a preset search algorithm. If the timestamp of the current block to be queried is less than the target timestamp, the block query device can use the block following the current block to be queried as the starting block, and determine the next block to be queried based on the block number of the starting block, the block number of the ending block, and a preset search algorithm.

[0105] For example, such as Figure 9As shown, a flowchart of querying the block number corresponding to the start date using the binary search algorithm in an embodiment of the present application is shown. Specifically, the block query device determines the start block number start_block_id and the end block number end_block_id of the binary search algorithm, as well as the timestamp need_find_ts to be searched (i.e., the timestamp corresponding to the start date). The block query device sets the lower limit block number low of the binary search algorithm to start_block_id, sets the upper limit block number high of the binary search algorithm to end_block_id, and the loop condition is whether low is less than or equal to high. If the loop condition is satisfied, the block query device calculates the block number mid of the middle block as mid = (low + high) / 2, and then calculates the corresponding block timestamp mid_ts = get_block_ts(mid), where the input of the function get_block_ts is the block number and the output is the corresponding timestamp. If mid_ts is the same as need_find_ts, then the entire binary search algorithm ends and the block number mid is returned. If mid_ts is greater than need_find_ts, the block query device modifies high to high = mid - 1 and re-executes the loop body; if mid_ts is less than need_find_ts, the block query device modifies low to low = mid + 1 and re-executes the loop body; repeating the above process until the loop body condition is not satisfied, if the block query device determines that low > end_block_id or high < low_block_id, it returns an error code -1, otherwise it returns low, that is, the block query device uses low as the block number corresponding to the start date.

[0106] The above mainly introduces the solution provided by the embodiment of the present application from the perspective of the method. To implement the above functions, the cross-border payment device or electronic device includes the corresponding hardware structure and / or software module for executing each function. Those skilled in the art should easily realize that, combining the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or the combination of hardware and computer software. Whether a certain function is executed in the way of hardware or computer software driving hardware depends on the specific application and design constraint conditions of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present application.

[0107] This application embodiment can, based on the above method, exemplarily divide a cross-border payment device or electronic device into functional modules. For example, the cross-border payment device or electronic device may include functional modules corresponding to each functional division, or two or more functions may be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this application embodiment is illustrative and only represents one logical functional division; in actual implementation, there may be other division methods.

[0108] For example, embodiments of this application also provide a block query device.

[0109] Figure 10 This is a block diagram illustrating a block query apparatus according to an exemplary embodiment. (Refer to...) Figure 10 The block query device 30 includes an acquisition unit 301, a processing unit 302, and a determination unit 303.

[0110] The acquisition unit 301 is used to acquire a query request; the query request includes time information; the query request is used to query the block number corresponding to the time information; the processing unit 302 is used to convert the time information into a target timestamp; the determination unit 303 is used to determine the current block to be queried from a specified block range on the blockchain, input the block number of the current block to be queried into a preset remote procedure call (RPC) interface, and output the timestamp of the current block to be queried; the determination unit 303 is also used to determine the block number corresponding to the time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the target timestamp.

[0111] In one possible implementation, the determining unit 303 is specifically used to: obtain the block number of the starting block and the block number of the ending block; the starting block is the first block in the blockchain; the ending block is the last block in the blockchain; the block numbers of each block in the blockchain are incremented according to the connection order of each block, and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value; and determine the current block to be queried according to the preset search algorithm, the block number of the starting block, and the block number of the ending block.

[0112] In one possible implementation, the preset search algorithm is a binary search algorithm, and the determining unit 303 is specifically used to: determine the intermediate block number according to the preset search algorithm, the block number of the starting block and the block number of the ending block, and determine the block corresponding to the intermediate block number as the current block to be queried.

[0113] In one possible implementation, the determining unit 303 is further configured to: determine the next block to be queried and obtain the timestamp of the next block to be queried when the timestamp of the current block to be queried is different from the target timestamp; the output unit is further configured to output the block number of the next block to be queried when the timestamp of the next block to be queried is the same as the target timestamp.

[0114] In one possible implementation, the determining unit 303 is specifically configured to: if the timestamp of the current block to be queried is greater than the target timestamp, take the current block to be queried as the end block, and determine the next block to be queried based on the block number of the end block, the block number of the start block, and a preset search algorithm; if the timestamp of the current block to be queried is less than the target timestamp, take the current block to be queried as the start block, and determine the next block to be queried based on the block number of the start block, the block number of the end block, and a preset search algorithm.

[0115] In one possible implementation, the acquisition unit 301 is specifically used to: acquire a query request in response to a user's query operation; the query operation includes an input operation and a click operation; the input operation is used to input time information; the click operation is used to trigger the query request.

[0116] In one possible implementation, the time information includes start time information and end time information, and the target timestamp includes a first target timestamp and a second target timestamp, wherein the first target timestamp is obtained by converting the start time information and the second timestamp is obtained by converting the end time information; the determining unit 303 is specifically used to: determine the interval number corresponding to the start time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the first target timestamp; or, determine the interval number corresponding to the end time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the second target timestamp; the determining unit 303 is further used to: after determining that the block number corresponding to the time information is the block number of the current block to be queried, obtain the target block number range according to the interval number corresponding to the start time information and the interval number corresponding to the end time information; the target block number range is the range of block numbers corresponding to the time period of the start time information and the end time information.

[0117] Regarding the apparatus in the above embodiments, the specific manner in which each module performs its operation has been described in detail in the embodiments related to the method, and will not be elaborated upon here.

[0118] Figure 11 This is a block diagram illustrating an electronic device according to an exemplary embodiment. Figure 11 As shown, the electronic device 40 includes, but is not limited to, a processor 401 and a memory 402.

[0119] The memory 402 described above is used to store the executable instructions of the processor 401. It is understood that the processor 401 is configured to execute instructions to implement the block query method in the above embodiments.

[0120] It should be noted that those skilled in the art will understand that Figure 11 The electronic device structure shown does not constitute a limitation on the electronic device; the electronic device may include, but is not limited to, other electronic devices. Figure 11 This may indicate more or fewer components, or combinations of certain components, or different component arrangements.

[0121] Processor 401 is the control center of the electronic device. It connects various parts of the electronic device via various interfaces and lines. By running or executing software programs and / or modules stored in memory 402, and by calling data stored in memory 402, it performs various functions and processes data, thereby providing overall monitoring of the electronic device. Processor 401 may include one or more processing units; optionally, processor 401 may integrate an application processor and a modem processor. The application processor mainly handles the operating system, user interface, and applications, while the modem processor mainly handles wireless communication. It is understood that the aforementioned modem processor can also be integrated into processor 401.

[0122] The memory 402 can be used to store software programs and various data. The memory 402 may primarily include a program storage area and a data storage area, wherein the program storage area may store the operating system, application programs required by at least one functional module, etc. Furthermore, the memory 402 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.

[0123] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory 402 including instructions, which can be executed by a processor 401 of an electronic device 40 to implement the block query method in the above embodiments.

[0124] In actual implementation, the processing functions of the acquisition unit 301, processing unit 302, and determination unit 303 can all be handled by... Figure 11 The processor 401 calls the computer program stored in the memory 402 to implement the process. The specific execution process can be found in the description of the block query method section in the previous embodiment, and will not be repeated here.

[0125] Optionally, the computer-readable storage medium may be a non-transitory computer-readable storage medium, such as a read-only memory (ROM), random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device.

[0126] In an exemplary embodiment, this application also provides a computer program product including one or more instructions, which can be executed by the processor 401 of the electronic device 40 to complete the block query method in the above embodiment.

[0127] It should be noted that when one or more instructions in the computer-readable storage medium or computer program product are executed by the processor 401 of the electronic device 40, they implement the various processes of the above-described block query method embodiments and achieve the same technical effect as the above-described block query method. To avoid repetition, they will not be described again here.

[0128] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0129] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another apparatus, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0130] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the classified units can be selected to achieve the purpose of this embodiment, depending on actual needs.

[0131] Furthermore, the functional units in the various embodiments of this application can be integrated into a single processor, or each unit can exist physically separately, or two or more units can be integrated into a single unit. The integrated units described above can be implemented in hardware or as software functional units.

[0132] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiments of this application, essentially, or the part that contributes to the prior art, or a complete or partial classification of the technical solution, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.

[0133] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A block query method, characterized in that, The method includes: Obtain a query request; the query request includes time information; the query request is used to query the block number corresponding to the time information; Convert the time information into a target timestamp; Obtain the block number of the starting block and the block number of the ending block; the starting block is the first block in a specified range on the blockchain; the ending block is the last block in the specified range; the block numbers of each block in the blockchain are incremented according to the connection order of each block, and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value. Based on the preset search algorithm, the block number of the starting block and the block number of the ending block, the current block to be queried is determined, and the block number of the current block to be queried is input into the preset remote procedure call (RPC) interface, and the timestamp of the current block to be queried is output. If the timestamp of the current block to be queried is the same as the target timestamp, the block number corresponding to the time information is determined as the block number of the current block to be queried.

2. The block query method according to claim 1, characterized in that, The preset search algorithm is a binary search algorithm. Determining the current block to be queried based on the preset search algorithm, the block number of the starting block, and the block number of the ending block includes: According to the preset search algorithm, the intermediate block number between the block number of the starting block and the block number of the ending block is determined, and the block corresponding to the intermediate block number is determined as the current block to be queried.

3. The block query method according to claim 1, characterized in that, The method further includes: If the timestamp of the current block to be queried is different from the target timestamp, determine the next block to be queried and obtain the timestamp of the next block to be queried; If the timestamp of the next block to be queried is the same as the target timestamp, output the block number of the next block to be queried.

4. The block query method according to claim 3, characterized in that, The step of determining the next block to be queried when the timestamp of the current block to be queried is different from the target timestamp includes: If the timestamp of the current block to be queried is greater than the target timestamp, the current block to be queried is taken as the end block, and the next block to be queried is determined according to the block number of the end block, the block number of the starting block and the preset search algorithm. If the timestamp of the current block to be queried is less than the target timestamp, the current block to be queried is taken as the starting block, and the next block to be queried is determined according to the block number of the starting block, the block number of the ending block, and a preset search algorithm.

5. The block query method according to claim 3, characterized in that, The step of determining the next block to be queried when the timestamp of the current block to be queried is different from the target timestamp includes: If the timestamp of the current block to be queried is greater than the target timestamp, the block preceding the current block to be queried is taken as the end block, and the next block to be queried is determined according to the block number of the end block, the block number of the starting block and the preset search algorithm. If the timestamp of the current block to be queried is less than the target timestamp, the next block after the current block to be queried is taken as the starting block, and the next block to be queried is determined according to the block number of the starting block, the block number of the ending block and the preset search algorithm.

6. The block query method according to any one of claims 1-5, characterized in that, The process of obtaining the query request includes: In response to a user's query operation, a query request is obtained; the query operation includes an input operation and a click operation; the input operation is used to input the time information; the click operation is used to trigger the query request.

7. The block query method according to any one of claims 1-5, characterized in that, The RPC interface includes a first RPC interface and a second RPC interface. The step of inputting the block number of the currently queried block into a preset Remote Procedure Call (RPC) interface and outputting the timestamp of the currently queried block includes: Input the block number of the current block to be queried into the first RPC interface to obtain the block hash of the current block to be queried; Input the block hash of the current block to be queried into the second RPC interface to obtain the timestamp of the current block to be queried.

8. The block query method according to any one of claims 1-5, characterized in that, The time information includes start time information and end time information, and the target timestamp includes a first target timestamp and a second target timestamp. The first target timestamp is obtained by converting the start time information, and the second target timestamp is obtained by converting the end time information. If the timestamp of the currently queried block is the same as the target timestamp, the block number corresponding to the time information is determined as the block number of the currently queried block, including: If the timestamp of the current block to be queried is the same as the first target timestamp, the interval number corresponding to the start time information is determined as the block number of the current block to be queried; or, If the timestamp of the current block to be queried is the same as the second target timestamp, the interval number corresponding to the end time information is determined as the block number of the current block to be queried; After determining that the block number corresponding to the time information is the block number of the currently queried block, the method further includes: The target block number range is obtained based on the interval number corresponding to the start time information and the interval number corresponding to the end time information; the target block number range is the block number range corresponding to the time interval of the start time information and the end time information.

9. A block query device, characterized in that, The device includes an acquisition unit, a processing unit, and a determination unit; The acquisition unit is used to acquire a query request; the query request includes time information; the query request is used to query the block number corresponding to the time information; The processing unit is used to convert the time information into a target timestamp; The acquisition unit is further configured to acquire the block number of the starting block and the block number of the ending block; the starting block is the first block in a specified range on the blockchain; the ending block is the last block in the specified range; the block numbers of each block in the blockchain are incremented according to the connection order of each block, and the difference between the block numbers of any two adjacent blocks in the blockchain is a preset value. The determining unit is used to determine the current block to be queried based on a preset search algorithm, the block number of the starting block and the block number of the ending block, and input the block number of the current block to be queried into a preset remote procedure call (RPC) interface, and output the timestamp of the current block to be queried. The determining unit is further configured to determine the block number corresponding to the time information as the block number of the current block to be queried when the timestamp of the current block to be queried is the same as the target timestamp.

10. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the method as described in any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that, When the computer-executable instructions stored in the computer-readable storage medium are executed by the processor of the electronic device, the electronic device is capable of performing the method as described in any one of claims 1 to 8.

12. A computer program product, characterized in that, The computer program product includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Information query request response method and device based on block chain network

    CN110597836A

  • Block chain transaction index storage method and device, computer equipment and medium

    CN112765271A