Price query method and device based on oracle machine, electronic equipment and storage medium
By using the combination of dichotomous iterative computing and oracle native interfaces in the blockchain, the historical price data is quickly positioned and queryed, and the problem of inefficiency in the existing technology is solved, and efficient and low-cost price data query is achieved.
Patent Information
- Application Number
- CN202510360100.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-25
- Publication Date
- 2025-07-18
AI Technical Summary
In the blockchain, the method of querying prices by traversing data flows in oracles is inefficient and difficult to meet the needs of real-time and efficientness.
The dichotomy method is used to perform iterative calculations within the oracle's data cycle range, narrow the query range until the historical price data corresponding to the target timestamp is obtained, and data traversal is performed through the oracle's native interface.
It improves the efficiency of blockchain data query, reduces on-chain resource consumption and gas costs, and meets the efficiency and lightweight needs of historical price data in decentralized financial applications.
Smart Images

Figure CN120336377A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology or other related technical fields. Specifically, it relates to a price query method and device based on an oracle, an electronic device, and a storage medium. Background Art
[0002] In recent years, with the rapid development of blockchain technology, its applications in multiple fields such as finance, supply chain, and Internet of Things have become increasingly widespread. Blockchain, with its characteristics of decentralization, distributed ledger, and encryption security, provides strong support for the immutability and transparency of data. Especially in the field of decentralized finance (DeFi), the application of blockchain technology has given rise to new financial service models, such as decentralized exchanges, lending platforms, insurance products, and complex derivatives markets. These applications all rely on accurate historical price data for various financial activities and calculations, such as asset valuation, transaction pricing, clearing, and risk management.
[0003] However, querying historical prices in a blockchain environment faces a series of technical challenges. Due to the characteristics of the blockchain network, its data storage is in block order, and the transaction and data information contained in each block is fixed. This means that retrieving historical price data requires searching for transaction records of specific assets or token pairs in each block of the blockchain and extracting the corresponding price information from them. This process not only has a high computational complexity but also consumes a large amount of on-chain resources. Especially in the case of a large amount of data, directly querying historical price data in the blockchain will become very inefficient and expensive.
[0004] Based on the problem of high cost and low efficiency of on-chain queries, relying on oracle services to obtain off-chain asset price data has emerged. The method of querying data through off-chain services solves the problem of low cost, but in related technologies, querying historical data at a specific moment by traversing time series data has the problem of low query efficiency and it is difficult to ensure the real-time nature of the query.
[0005] In response to the above problems, no effective solution has been proposed yet. Summary of the Invention
[0006] Embodiments of the present invention provide a price query method and device based on an oracle, an electronic device, and a storage medium, so as to at least solve the technical problem of low efficiency in the related technology of querying prices by traversing data streams in an oracle in a blockchain.
[0007] According to one aspect of the embodiments of the present invention, a price query method based on an oracle is provided, including: receiving a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parsing the financial transaction request to determine a target object to be queried and a target timestamp, wherein the transaction request includes a historical price query task; calling a data interface of the oracle based on the target object to be queried to determine an initial data period range to be queried, wherein the data stream in the oracle is stored in units of data periods; using the binary search method to perform iterative calculations on the basis of the initial data period range to narrow down the data period range to be queried until the target data period where the historical price data corresponding to the target timestamp is obtained is acquired; traversing the historical price data within the target data period to query the target historical price data corresponding to the target timestamp, and sending the target historical price data to the blockchain interaction interface.
[0008] Further, the step of calling a data interface of the oracle based on the target object to be queried to determine an initial data period range to be queried includes: calling a native data interface of the oracle based on the target object to be queried, and accessing the data stream in the oracle through the native data interface; determining the earliest updated data period and the latest updated data period in the oracle; determining the initial data period range to be queried based on the earliest updated data period and the latest updated data period.
[0009] Further, the step of using the binary search method to perform iterative calculations on the basis of the initial data period range to narrow down the data period range to be queried includes: Step 1, selecting the midpoint of the initial data period range, and dividing the initial data period range through the midpoint; Step 2, determining the historical timestamp corresponding to the midpoint, and comparing the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; Step 3, narrowing down the data period range to be queried based on the comparison result to obtain a new initial data period range; repeating the above steps 1 to 3 for iterative calculations until the data period range is narrowed down to the data period where the target timestamp is located, and then stopping the iteration.
[0010] Further, the step of determining the historical timestamp corresponding to the midpoint and comparing the historical timestamp corresponding to the midpoint with the target timestamp includes: obtaining the timestamp field of the data period corresponding to the midpoint, and reading the historical timestamp corresponding to the midpoint based on the timestamp field; comparing the value of the historical timestamp with the value of the target timestamp.
[0011] Further, the step of narrowing down the data cycle range to be queried based on the comparison result includes: using the data cycle corresponding to the intermediate point as the boundary of the data cycle range to be queried according to the comparison result; narrowing down the data cycle range to be queried based on the boundary of the data cycle range to be queried.
[0012] Further, the step of using the data cycle corresponding to the intermediate point as the boundary of the data cycle range to be queried according to the comparison result includes: when the comparison result indicates that the historical timestamp corresponding to the intermediate point is earlier than the target timestamp, using the data cycle corresponding to the intermediate point as the left boundary of the data cycle range to be queried; or, when the comparison result indicates that the historical timestamp corresponding to the intermediate point is later than the target timestamp, using the data cycle corresponding to the intermediate point as the right boundary of the data cycle range to be queried.
[0013] Further, the step of narrowing down the data cycle range to be queried based on the boundary of the data cycle range to be queried includes: when using the data cycle corresponding to the intermediate point as the left boundary of the data cycle range to be queried, obtaining the right boundary of the currently queried data cycle range, and determining a new data cycle range to be queried based on the left boundary and the right boundary to obtain a narrowed data cycle range; or, when using the data cycle corresponding to the intermediate point as the right boundary of the data cycle range to be queried, obtaining the left boundary of the currently queried data cycle range, and determining a new data cycle range to be queried based on the left boundary and the right boundary to obtain a narrowed data cycle range.
[0014] According to another aspect of the embodiments of the present invention, there is also provided an oracle-based price query device, including: a receiving unit, configured to receive a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parse the financial transaction request to determine a target object and a target timestamp to be queried, where the transaction request includes a historical price query task; a determining unit, configured to call a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried, where the data stream in the oracle is stored in units of data cycles; a calculation unit, configured to perform iterative calculations on the basis of the initial data cycle range by using the dichotomy method to narrow down the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained; a query unit, configured to traverse the historical price data within the target data cycle, query the target historical price data corresponding to the target timestamp, and send the target historical price data to the blockchain interaction interface.
[0015] Further, the determining unit includes: a first calling subunit, configured to call the native data interface of the oracle based on the target object to be queried, and access the data stream in the oracle through the native data interface; a first determining subunit, configured to determine the earliest updated data period and the latest updated data period in the oracle; and a second determining subunit, configured to determine the initial data period range to be queried based on the earliest updated data period and the latest updated data period.
[0016] Further, the calculating unit includes: a first selecting subunit, configured to perform step one, select the midpoint of the initial data period range, and divide the initial data period range by the midpoint; a first comparing subunit, configured to perform step two, determine the historical timestamp corresponding to the midpoint, and compare the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; a first narrowing subunit, configured to perform step three, narrow the data period range to be queried based on the comparison result to obtain a new initial data period range; and a first repeating subunit, configured to repeat the above steps one to three for iterative calculation until the data period range is narrowed down to the data period where the target timestamp is located, and then stop the iteration.
[0017] Further, the first comparing subunit includes: a first reading module, configured to obtain the timestamp field of the data period corresponding to the midpoint, and read the historical timestamp corresponding to the midpoint based on the timestamp field; and a first comparing module, configured to compare the value of the historical timestamp with the value of the target timestamp.
[0018] Further, the first narrowing subunit includes: a first acting module, configured to use the data period corresponding to the midpoint as the boundary of the data period range to be queried according to the comparison result; and a first narrowing module, configured to narrow the data period range to be queried based on the boundary of the data period range to be queried.
[0019] Further, the first acting module includes: a first acting sub-module, configured to use the data period corresponding to the midpoint as the left boundary of the data period range to be queried when the comparison result indicates that the historical timestamp corresponding to the midpoint is earlier than the target timestamp; and a second acting sub-module, configured to use the data period corresponding to the midpoint as the right boundary of the data period range to be queried when the comparison result indicates that the historical timestamp corresponding to the midpoint is later than the target timestamp.
[0020] Further, the first shrinking module includes: a first determination sub-module, configured to obtain the right boundary of the currently to-be-query data cycle range when using the data cycle corresponding to the midpoint as the left boundary of the to-be-query data cycle range, and determine a new to-be-query data cycle range based on the left boundary and the right boundary to obtain a shrunk data cycle range; a second determination sub-module, configured to obtain the left boundary of the currently to-be-query data cycle range when using the data cycle corresponding to the midpoint as the right boundary of the to-be-query data cycle range, and determine a new to-be-query data cycle range based on the left boundary and the right boundary to obtain a shrunk data cycle range.
[0021] According to another aspect of the embodiments of the present invention, there is also provided a computer-readable storage medium, which includes a stored computer program, wherein when the computer program runs, it controls the device where the computer-readable storage medium is located to perform any one of the above oracle-based price query methods.
[0022] According to another aspect of the embodiments of the present invention, there is also provided an electronic device, including one or more processors and a memory, where the memory is used to store one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are caused to implement any one of the above oracle-based price query methods.
[0023] In this application, through the following steps: first, receive a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parse the financial transaction request to determine a target object and a target timestamp to be queried, where the transaction request includes a historical price query task, and call the data interface of the oracle based on the target object to be queried to determine an initial data cycle range to be queried, where the data stream in the oracle is stored in units of data cycles, then use the dichotomy method to perform iterative calculations on the basis of the initial data cycle range to shrink the data cycle range to be queried until obtaining the target data cycle where the historical price data corresponding to the target timestamp is located, and finally traverse the historical price data within the target data cycle to query the target historical price data corresponding to the target timestamp, and send the target historical price data to the blockchain interaction interface.
[0024] In this application, for a financial transaction request involving price data query in a blockchain environment, the oracle native interface is called to determine the initial data query range through the data stream stored in the oracle. On this basis, the dichotomy method is used to continuously iterate and narrow the data query range until the target data period where the data to be queried is located is found. Then, the data stream in the target data period is traversed to determine the historical price data corresponding to the target timestamp, achieving the purpose of quickly querying historical price data, obtaining the technical effect of improving the blockchain data query efficiency, and further solving the technical problem of low efficiency in the related technology of querying prices by traversing the data stream in the oracle in the blockchain. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The schematic embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0026] Figure 1 is a flowchart of an optional oracle-based price query method according to an embodiment of the present invention;
[0027] Figure 2 is a schematic diagram of an optional oracle-based price query process according to an embodiment of the present invention;
[0028] Figure 3 is a schematic diagram of an optional oracle-based price query device according to an embodiment of the present invention;
[0029] Figure 4 is a hardware structure block diagram of an electronic device (or mobile device) for executing the oracle-based price query method according to an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0030] In order to enable those skilled in the art of this technology to better understand the solution of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0031] It should be noted that the terms "first", "second", etc. in the specification, claims, and the above-mentioned drawings of the present invention are used to distinguish similar objects, and do not necessarily have to be used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or are inherent to these processes, methods, products, or devices.
[0032] For the convenience of those skilled in the art to understand the present invention, the following explanations are made for some terms or nouns involved in each embodiment of the present invention:
[0033] Chainlink oracle. Chainlink oracle is a decentralized oracle network. As a middleware solution, it connects smart contracts on the blockchain with off-chain data sources, enabling smart contracts to securely and reliably obtain and verify external data.
[0034] Gas fee. Gas is a unit of measurement for the computing resources required to execute smart contract operations or conduct transactions. Gas fees are used to measure and pay the costs of smart contract execution or transaction processing.
[0035] k-nearest neighbor algorithm, k-Nearest Neighbors, abbreviated as kNN, is an instance-based learning method widely used in classification and regression problems.
[0036] It should be noted that the oracle-based price query method and its device in the present application can be used in the field of blockchain technology when quickly querying price data based on an oracle, and can also be used in any field other than the field of blockchain technology when quickly querying price data based on an oracle. The application field of the oracle-based price query method and its device in the present application is not limited.
[0037] It should be noted that the relevant information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are information and data that have been authorized by the user or fully authorized by all parties. Moreover, the processing of the relevant data, such as collection, storage, use, processing, transmission, provision, disclosure, and application, complies with the relevant laws, regulations, and standards in the relevant regions, adopts necessary confidentiality measures, does not violate public order and good customs, and provides corresponding operation entrances for users to choose to authorize or refuse. For example, an interface is set between this system and relevant users or institutions. Before obtaining relevant information, a request for obtaining information needs to be sent to the aforementioned users or institutions through the interface, and after receiving the consent information feedback from the aforementioned users or institutions, the relevant information is obtained.
[0038] It should be noted that when collecting and analyzing customer information in this application, corresponding operation entrances are provided for users to choose to agree or refuse the results of automated decision-making; if the user chooses to refuse, the expert decision-making process will be entered.
[0039] The following embodiments of the present invention can be applied to various oracle-based price query systems / applications / devices. The present invention proposes a lightweight price retrieval method based on an oracle, aiming to achieve efficient query of historical price data at any time and solve the problems of data integrity and real-time performance in existing methods. The present invention adopts the method of integrating native interfaces with an efficient binary algorithm, and only relies on on-chain contracts to achieve accurate data positioning, which can effectively solve the problems of high consumption of on-chain resources and high Gas fees. While significantly optimizing the utilization of on-chain resources, it also adapts to low-power devices and high-frequency interaction scenarios, meeting the requirements of high efficiency and lightweight for querying historical price data in decentralized finance applications.
[0040] The present invention will be described in detail below in conjunction with each embodiment.
[0041] Embodiment 1
[0042] According to an embodiment of the present invention, an embodiment of a price query method based on an oracle is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order from here.
[0043] Figure 1 is a flowchart of an optional price query method based on an oracle according to an embodiment of the present invention. As Figure 1 shown, the method includes the following steps:
[0044] Step S101: Receive a financial transaction request initiated by the client based on the blockchain interface, and parse the financial transaction request to determine the target object and target timestamp to be queried.
[0045] Blockchain technology is a distributed ledger technology that allows multiple parties to verify and record transactions through a consensus mechanism without a central authority. Its core features include decentralization, data transparency, immutability, and security. Blockchain achieves a highly reliable and tamper-resistant recording method by storing data in "blocks" arranged in chronological order and linking these blocks using cryptographic methods. It is widely used in fields such as finance, supply chain management, Internet of Things, and digital identity verification. With the continuous growth of financial data and the increasing demand for financial security from users, applying blockchain technology to the financial field to achieve decentralized finance. As blockchain technology is increasingly applied in decentralized finance (DeFi, full name Decentralized Finance), the demand for the security, reliability, and real-time nature of on-chain and off-chain data interaction is growing. Traditional smart contracts are limited to the on-chain environment and are difficult to directly access off-chain data sources, thus affecting their function expansion and commercial value in complex application scenarios.
[0046] In the above step S101, the client refers to any entity that interacts with the blockchain, including but not limited to individual users, enterprises, smart contracts, or other decentralized applications. In the DeFi field, the client can be the terminal where a person or enterprise that needs to execute a financial transaction is located. The client submits a financial transaction request through the blockchain interface. The financial transaction request contains a historical price data query task, and a certain function in the smart contract will be triggered through the financial transaction request, such as a request for liquidation, lending, or transaction execution.
[0047] Specifically, after receiving the request, the smart contract will execute the corresponding logic according to the parameters in the request, such as the target object, target timestamp, etc. These requests may involve financial transactions such as lending, trading, and liquidation. These financial transactions involve the query of historical price data, and the price data of the historical time needs to be queried to execute the above financial transaction operations. After receiving the request, the smart contract will parse the request to determine the target object and target timestamp that the client needs to query. The target object refers to the specific asset or token pair specified in the financial transaction request, which is the historical price data that the client hopes to query. The smart contract executes the risk assessment, pricing, or other complex financial calculations requested by the client by calling the historical price data.
[0048] Step S102: Call the data interface of the oracle based on the target object to be queried to determine the initial data cycle range to be queried.
[0049] In the embodiments of the present invention, an oracle connects the blockchain system with data and events in the external world, enabling smart contracts to access external data and providing reliable data input required for smart contracts. Further, the oracle in the embodiments of the present invention may select the Chainlink oracle. With the development of oracle technology, Chainlink, as a decentralized oracle network, is being widely applied in multiple fields. In the financial field, Chainlink technology supports the booming development of the decentralized financial ecosystem by providing trusted data source input and output. The Chainlink oracle can not only securely obtain external information such as off-chain market prices and weather data for on-chain smart contracts, but also enhance the data collaboration ability between different blockchain networks through cross-chain interoperability. In addition, the encryption protection mechanism and anti-tampering characteristics of Chainlink provide important support for the transmission of highly sensitive data, laying a solid foundation for the execution reliability and accuracy of smart contracts. Applying the Chainlink oracle technology to fields such as payment verification, clearing calculation, and risk assessment can achieve the credibility, automation, and efficiency of data interaction, further promoting the innovation and development of the blockchain ecosystem.
[0050] In the embodiments of the present invention, the native interface of the Chainlink oracle is integrated to access the data stream stored in the oracle, without the need to interact with off-chain services or databases, realizing lightweight price retrieval.
[0051] It should be noted that the data stream in the oracle is stored in units of data cycles. Each data cycle represents a data report within a specific time point or time period, and these reports contain external data collected by the oracle, such as asset prices, weather conditions, event results, etc.
[0052] Through the above steps, it is possible to call the data interface of the oracle based on the target object and determine a data cycle range containing the target timestamp. The determination of this range is the basis of the lightweight price retrieval method, which provides precise query boundaries for the subsequent binary search algorithm, enabling the data query process to be completed within a limited time with low resource consumption.
[0053] Further, the steps of calling the data interface of the oracle based on the target object to be queried and determining the initial data cycle range to be queried include: calling the native data interface of the oracle based on the target object to be queried, and accessing the data stream in the oracle through the native data interface; determining the earliest updated data cycle and the latest updated data cycle in the oracle; and determining the initial data cycle range to be queried based on the earliest updated data cycle and the latest updated data cycle.
[0054] Specifically, when querying historical prices in real time, it is first necessary to determine an initial data query range. To determine the upper limit of the initial data cycle range, the smart contract will call the latestRoundData() interface of the Chainlink oracle. The oracle will obtain the earliest updated and currently queryable data cycle and the current latest data cycle. Based on the earliest and latest updated data cycles, an initial data cycle range to be queried is determined, providing a starting point for subsequent binary search.
[0055] Step S103: Use the binary method to perform iterative calculations based on the initial data cycle range, narrowing down the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained.
[0056] It should be noted that after determining the initial data cycle range, since this data range is large, traversing all the data within the range will result in a long query time and low efficiency. Therefore, based on the initial data query range, the next task of the smart contract is to find the data cycle closest to the target timestamp. To narrow down the query range, the data query range is divided by determining the midpoint of the range, and the data query range for the next division is determined based on the divided range. Then, through continuous iteration using the binary method, the data query range is narrowed down to ensure that the data cycle where the target timestamp is located can be quickly located, avoiding linear search from the very beginning of the historical data stream and significantly improving the query efficiency.
[0057] Furthermore, the steps of using the binary method to perform iterative calculations based on the initial data cycle range to narrow down the data cycle range to be queried include: Step 1, select the midpoint of the initial data cycle range and divide the initial data cycle range by the midpoint; Step 2, determine the historical timestamp corresponding to the midpoint and compare the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; Step 3, narrow down the data cycle range to be queried based on the comparison result to obtain a new initial data cycle range; repeat the above Steps 1 to 3 for iterative calculations until the data cycle range is narrowed down to the data cycle where the target timestamp is located, and then stop the iteration.
[0058] Specifically, when performing iterative calculations to narrow down the data range to be queried, it specifically includes: Step 1, according to the pre-determined initial data cycle range of the data to be queried, select the midpoint within this range, that is, the data cycle in the very middle of the cycle range. Based on the midpoint, the range can be divided into two parts: the data cycles before the midpoint RoundID and the data cycles after it. By selecting the midpoint, the query time for the data cycle range can be halved, and subsequently, the query range can be narrowed down according to the division result, thereby significantly improving the data retrieval efficiency.
[0059] Step 2: After determining the midpoint, obtain the historical timestamp of the data cycle corresponding to the midpoint, that is, update the time field of the data cycle corresponding to the midpoint. Subsequently, compare this historical timestamp with the target timestamp input by the client. If the historical timestamp is earlier than the target timestamp, then the target data is on the right side of the time cycle corresponding to the midpoint (i.e., a later data cycle); conversely, if the historical timestamp is later than the target timestamp, the target data is on the left side of the current data cycle (i.e., an earlier data cycle). This comparison result will be used for adjusting the data cycle range in the next step.
[0060] Step 3: According to the comparison result in Step 2, the data cycle range to be queried will be updated. If the historical timestamp corresponding to the midpoint is earlier than the target timestamp, select the range on the right side of the midpoint as the new data query cycle; conversely, if the corresponding historical timestamp is later than the target timestamp, then use the range on the left side of the midpoint as the new data query range. Through multiple divisions and iterations, gradually narrow the data cycle range to be queried until the range is small enough to be accurate to the data cycle where the target timestamp is located.
[0061] The above Steps 1 to 3 are an iterative process. In each iteration, these three steps are repeated until the data cycle range is narrowed down to only include the data cycle where the target timestamp is located. At this time, the position where the historical price data to be queried is located can be accurately located, and the iteration can be stopped and the data can be obtained without traversing and comparing each data in the data stream, saving query time and improving query efficiency.
[0062] Further, the steps of determining the historical timestamp corresponding to the midpoint and comparing the historical timestamp corresponding to the midpoint with the target timestamp include: obtaining the timestamp field of the data cycle corresponding to the midpoint, and reading the historical timestamp corresponding to the midpoint based on the timestamp field; comparing the value of the historical timestamp with the value of the target timestamp.
[0063] In some embodiments, when narrowing the data cycle range, it is necessary to rely on the StartAt field of the data cycle in the Chainlink oracle. The StartAt field is the moment when the data cycle is updated. By comparing the target timestamp t with the value of the StartAt field of the data cycle corresponding to the midpoint, judge the relative position of the target data to be queried and the midpoint, so as to iteratively update the left and right boundaries, and finally accurately lock the historical price data to be queried.
[0064] Further, the steps of narrowing the data cycle range to be queried based on the comparison result include: using the data cycle corresponding to the midpoint as the boundary of the data cycle range to be queried according to the comparison result; narrowing the data cycle range to be queried based on the boundary of the data cycle range to be queried.
[0065] It should be noted that the timestamp comparison result is the basis for updating the boundary of the data cycle range. By continuously updating the boundary, the data cycle range to be queried is gradually narrowed until the data cycle in which the data corresponding to the target timestamp is located is determined. By using the binary search algorithm within the initial data cycle range, the number of on-chain operations and Gas fees can be significantly reduced. Compared with the traditional linear search, the time complexity of this method is reduced from O(N) to O(logN), greatly improving the efficiency and speed of data retrieval. At the same time, the accuracy of the binary search method ensures that the price data closest to the target timestamp can be quickly located, meeting the real-time and accuracy requirements for historical price data queries in decentralized finance (DeFi) applications.
[0066] Further, the step of using the data cycle corresponding to the midpoint as the boundary of the data cycle range to be queried according to the comparison result includes: when the comparison result indicates that the historical timestamp corresponding to the midpoint is earlier than the target timestamp, using the data cycle corresponding to the midpoint as the left boundary of the data cycle range to be queried; or, when the comparison result indicates that the historical timestamp corresponding to the midpoint is later than the target timestamp, using the data cycle corresponding to the midpoint as the right boundary of the data cycle range to be queried.
[0067] In some embodiments, the comparison result between the historical timestamp corresponding to the midpoint and the target timestamp determines the data cycle range for the next iterative calculation. Specifically, if the timestamp corresponding to the midpoint is earlier than the target timestamp, according to the increasing characteristic of the data stream in the oracle, it indicates that the data cycle corresponding to the target timestamp is within the range on the right side of the midpoint. Thus, the data cycle corresponding to the midpoint is used as the left boundary of the data cycle range to be queried, and the updated data cycle range is half of the data cycle range in the previous iteration. On the contrary, if the timestamp corresponding to the midpoint is later than the target timestamp, it indicates that the data cycle corresponding to the target timestamp is within the range on the left side of the midpoint. Thus, the data cycle corresponding to the midpoint is used as the right boundary of the data cycle range to be queried, and the data cycle range to be queried is updated.
[0068] Further, the step of narrowing the data cycle range to be queried based on the boundary of the data cycle range to be queried includes: when using the data cycle corresponding to the midpoint as the left boundary of the data cycle range to be queried, obtaining the right boundary of the current data cycle range to be queried, and determining the new data cycle range to be queried based on the left boundary and the right boundary to obtain the narrowed data cycle range; or, when using the data cycle corresponding to the midpoint as the right boundary of the data cycle range to be queried, obtaining the left boundary of the current data cycle range to be queried, and determining the new data cycle range to be queried based on the left boundary and the right boundary to obtain the narrowed data cycle range.
[0069] Finally, based on the updated left and right boundaries, the narrowed data cycle range can be determined. After updating the left boundary of the data cycle range to be queried, a new data cycle range can be obtained according to the updated left boundary and the right boundary of the current data cycle range to be queried. Conversely, after updating the right boundary of the data cycle range to be queried, a new data cycle range can be obtained according to the updated right boundary and the left boundary of the current data cycle range to be queried. Through multiple iterations, the data cycle range is continuously narrowed, thereby locating the data cycle where the data corresponding to the target timestamp is located.
[0070] Step S104: Traverse the historical price data within the target data cycle, query the target historical price data corresponding to the target timestamp, and send the target historical price data to the blockchain interaction interface.
[0071] In the above step S104, after determining the target data cycle where the data corresponding to the target timestamp is located, traverse the historical price data within the target data cycle to determine the target historical price data corresponding to the target timestamp, thereby obtaining the data required by the user side. According to the queried target historical price data, the smart contract will send the queried target historical price data to the blockchain interaction interface to achieve transparent visibility of the data and automatically execute the corresponding financial transaction calculations.
[0072] By integrating the native interface of the oracle in the embodiment of the present invention and using the dichotomy search to query the historical price data of the target timestamp, the native interface of the Chainlink oracle is fully utilized, avoiding additional custom development work, and improving the versatility and portability of the solution. Through this integration, this solution can support all tokens and assets based on the Chainlink oracle, ensuring efficient retrieval of historical price data in different application scenarios. At the same time, the smart contract does not need to traverse each piece of data in the oracle. By using the dichotomy to locate the specific data position, it realizes the traversal of a small amount of data and the comparison of field values, reducing the calculation amount and improving the query efficiency.
[0073] Through the above steps, first receive the financial transaction request initiated by the client based on the blockchain interaction interface, and parse the financial transaction request to determine the target object and target timestamp to be queried. Among them, the transaction request includes a historical price query task, and call the data interface of the oracle based on the target object to be queried to determine the initial data cycle range to be queried. Among them, the data stream in the oracle is stored in units of data cycles. Then, use the binary method to perform iterative calculations on the basis of the initial data cycle range to narrow the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained. Finally, traverse the historical price data within the target data cycle, query the target historical price data corresponding to the target timestamp, and send the target historical price data to the blockchain interaction interface.
[0074] In this embodiment, for the financial transaction request involving price data query in the blockchain environment, call the native interface of the oracle, determine the initial data query range through the data stream stored in the oracle, and on this basis, use the binary method to continuously iterate and narrow the data query range until the target data cycle where the data to be queried is located is found. Traverse the data stream in the target data cycle to determine the historical price data corresponding to the target timestamp, achieving the purpose of quickly querying historical price data, obtaining the technical effect of improving the blockchain data query efficiency, and thus solving the technical problem of low efficiency in the related technology of querying prices by traversing the data stream in the oracle in the blockchain.
[0075] The following will be described in detail in combination with another optional specific implementation manner.
[0076] Blockchain technology is a distributed ledger technology that allows multiple parties to verify and record transactions through a consensus mechanism. Its core features include decentralization, data transparency, immutability, and security. Blockchain stores data in "blocks" arranged in chronological order and links these blocks using cryptographic methods, achieving a highly reliable and tamper-resistant recording method. It is widely used in fields such as finance, supply chain management, the Internet of Things, and digital identity verification.
[0077] An oracle is a bridging technology used to connect the blockchain network with data and events in the external world. Since smart contracts run within the blockchain and cannot directly access off-chain data, the oracle verifies and transmits external data (such as prices, weather, sensor data, etc.) to the chain to provide reliable data input required by smart contracts. An oracle system usually includes data sources, data verification mechanisms, and security measures to ensure the authenticity and immutability of data.
[0078] A smart contract is self-executing code running on a blockchain, used to automatically trigger and execute transactions according to predefined conditions, and is the execution entity in the embodiments of the present invention. Its key feature is "trusted neutrality", that is, once deployed, the execution of the contract requires no third-party intervention and the result cannot be tampered with. Smart contracts can implement automated and transparent processes and are widely used in scenarios such as finance, logistics, law, and the Internet of Things, such as automatic payment, supply chain tracking, and decentralized autonomous organizations.
[0079] Price retrieval refers to the process of obtaining the current market price of a certain asset, commodity or service from off-chain data sources and using this data in blockchain applications. Lightweight price retrieval methods adapt to resource-constrained blockchain environments by optimizing the data acquisition process, reducing computational overhead, and improving efficiency. Combined with oracle technology, price retrieval is usually applied in decentralized finance scenarios, such as pricing smart contracts, decentralized exchanges, and dynamic staking mechanisms.
[0080] Most existing solutions mainly rely on off-chain services or independent databases for querying historical price data, resulting in a significant increase in system complexity and operating costs. At the same time, this off-chain dependence introduces problems of data integrity and real-time performance, making it difficult to ensure the consistency of on-chain data. In addition, directly querying historical price data on-chain usually faces problems of high power consumption and high Gas fees. In resource-constrained devices or high-frequency interaction scenarios, existing methods are difficult to achieve high efficiency and lightweight. How to meet the accuracy and real-time requirements of historical price data queries while reducing on-chain resource consumption remains a technical problem to be solved urgently.
[0081] As a middleware solution integrating technologies such as blockchain, distributed computing, and encryption algorithms, the oracle is committed to achieving a trusted connection between on-chain smart contracts and off-chain data sources. Currently, oracle technology has become an important part of promoting the implementation of blockchain technology. With the continuous in-depth application of blockchain technology in decentralized finance (DeFi), the demand for the security, reliability, and real-time performance of on-chain and off-chain data interaction is increasing. Traditional smart contracts are limited to the on-chain environment and are difficult to directly access off-chain data sources, thus affecting their function expansion and commercial value in complex application scenarios.
[0082] Meanwhile, with the development of oracle technology, Chainlink, as a decentralized oracle network, is being widely applied in multiple fields. In the financial field, Chainlink technology supports the booming development of the decentralized finance ecosystem by providing trusted data source input and output. Chainlink oracles can not only securely obtain external information such as off-chain market prices and weather data for on-chain smart contracts but also enhance the data collaboration ability between different blockchain networks through cross-chain interoperability. In addition, Chainlink's encryption protection mechanism and anti-tampering features provide important support for the transmission of highly sensitive data, laying a solid foundation for the execution reliability and accuracy of smart contracts. Therefore, existing research actively explores applying Chainlink oracle technology to fields such as payment verification, clearing calculations, and risk assessment to achieve trustworthy, automated, and efficient data interaction, further promoting the innovation and development of the blockchain ecosystem.
[0083] In the existing technology, focusing on the application of Chainlink oracle technology in decentralized finance (DeFi), especially how to achieve a reliable connection between on-chain smart contracts and off-chain data sources, there is a solution that proposes a lightweight distributed index framework to support approximate kNN queries for terabyte-scale time series data and realizes efficient query processing and a compact index structure through a three-step index construction method. There is also a solution that proposes an oracle-based price manipulation prevention framework to identify on-chain price manipulation behaviors through off-chain prices and intercept price manipulation transactions with proxy contracts. Additionally, there is a solution that proposes an oracle design pattern based on selective storage to achieve data reuse through off-chain classification models and on-chain smart contracts, as well as dynamically adjust weights to ensure the security and efficiency of data aggregation, improving the performance of the oracle system in blockchain applications.
[0084] Furthermore, regarding the application of Chainlink oracles in cross-chain data interaction, there is a solution that proposes an oracle-based cross-chain data migration architecture and designs a corresponding data migration mechanism to ensure secure data migration between heterogeneous blockchains while ensuring data confidentiality, integrity, and security. There is also a solution that explores how Chainlink oracles support the collaborative execution of multi-chain smart contracts by enabling inter-chain data transmission, improving the adaptability of smart contracts in complex and changing business scenarios.
[0085] However, existing methods often rely on off-chain services or independent databases for historical price data queries, increasing the complexity and operating costs of the system. At the same time, issues of data integrity and real-time performance are introduced, making it difficult to ensure the consistency of on-chain data. Additionally, directly querying historical price data on-chain usually faces challenges of high power consumption and high Gas fees. In resource-constrained devices or high-frequency interaction scenarios, existing methods are difficult to achieve high efficiency and light weight. Finally, existing methods fail to effectively balance the on-chain resource consumption with the accuracy and real-time requirements of historical price data queries, presenting a problem of how to ensure query accuracy and real-time performance while reducing resource consumption.
[0086] The present invention proposes a lightweight price retrieval method based on Chainlink oracle, aiming to achieve efficient query of historical price data at any time and solve the data integrity and real-time problems in existing methods. To address the problems of high on-chain resource consumption and high Gas fees, the present invention adopts a method of integrating native interfaces with an efficient binary algorithm, relying only on on-chain contracts to achieve accurate data positioning. While significantly optimizing the utilization of on-chain resources, it adapts to low-power devices and high-frequency interaction scenarios, improves data query efficiency, and meets the high-efficiency and lightweight requirements for historical price data queries in DeFi applications.
[0087] Figure 2 FIG. is a schematic diagram of an optional oracle-based price query process according to an embodiment of the present invention. The embodiment of the present invention innovatively uses the native interface provided by Chainlink oracle and an efficient binary search algorithm to achieve efficient and accurate retrieval of historical price data in a blockchain environment, which is particularly suitable for low-power devices and high-frequency interaction scenarios. As Figure 2 shown, the oracle-based price query process specifically includes:
[0088] The core objective of the embodiment of the present invention is to quickly and accurately retrieve historical price data in resource-constrained devices and environments through the native interface provided by Chainlink oracle and the optimized binary search algorithm.
[0089] In the preparation stage, the smart contract receives a financial transaction request from the user end, determines the target timestamp t to be queried, calls the oracle native interface, and accesses the data stream stored on the oracle, thereby extracting the historical price data at the target timestamp t.
[0090] In the query stage based on the binary method, locate the earliest queryable cycle number in the oracle, determine the query range, and iteratively narrow the query range based on the binary method until the data cycle where the historical price data corresponding to the target timestamp t is located is determined.
[0091] This solution effectively utilizes the increasing property of RoundID (the identifier of the data cycle) in the Chainlink oracle to design a find_earliest_round function. This function uses the binary search method to locate the data cycle closest to the target timestamp within a specified range. During this process, the function iteratively narrows the query range and finally determines the earliest queryable RoundID to ensure the efficiency and accuracy of data retrieval.
[0092] Specifically, by continuously iteratively updating the left and right boundaries (left and right) of the data cycle range and determining whether the middle point (mid) meets the query conditions, the earliest queryable RoundID is finally located. The time complexity of this algorithm is O(logN), which greatly improves the query efficiency compared to traditional linear search methods.
[0093] When locating the data cycle where the target timestamp is located, it depends on the StartAt field in the Chainlink RoundData. By comparing the target timestamp t with the value of the StartAt field, the position of the data to be queried is determined, and then the left and right boundaries are iteratively updated to finally accurately lock the target price data.
[0094] In the output stage, by traversing the price data within the located data cycle, the price data at time t is output.
[0095] In the embodiments of the present invention, in order to reduce the query cost and reduce the consumption of on-chain resources, the number of interface calls is minimized during the binary search process. Only when necessary, data is obtained through the contract interfaces (latestRoundData() and getRoundData()) of the Chainlink oracle to avoid frequent on-chain interactions. This design idea not only reduces the cost of on-chain operations but also improves the overall efficiency of the system.
[0096] In the embodiments of the present invention, only the left and right boundary data (S.RoundID and E.RoundID) is stored during the binary search process, reducing the complexity of memory storage. Without adding additional storage burden, it can ensure the good operation of the system in low-power devices and resource-constrained scenarios.
[0097] The embodiments of the present invention make full use of the native interfaces of Chainlink, avoiding additional custom development work and enhancing the versatility and portability of the solution. Through this integration, this solution can support all tokens and assets based on the Chainlink oracle, ensuring the efficient retrieval of historical price data in different application scenarios.
[0098] The following is a detailed description in combination with another embodiment.
[0099] Example 2
[0100] A price query device based on an oracle provided in this embodiment includes multiple implementation units. Each implementation unit corresponds to each implementation step in the first embodiment above. The specific implementation manner and beneficial effects can refer to the foregoing method embodiment and will not be elaborated here.
[0101] Figure 3 is a schematic diagram of an optional price query device based on an oracle according to an embodiment of the present invention. As Figure 3 shown, the price query device based on an oracle may include: a receiving unit 31, a determining unit 32, a calculating unit 33, and a querying unit 34. Among them,
[0102] The receiving unit 31 is configured to receive a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parse the financial transaction request to determine a target object to be queried and a target timestamp. Among them, the transaction request includes a historical price query task;
[0103] The determining unit 32 is configured to call a data interface of an oracle based on the target object to be queried, and determine an initial data cycle range to be queried. Among them, the data stream in the oracle is stored in units of data cycles;
[0104] The calculating unit 33 is configured to perform iterative calculations on the basis of the initial data cycle range by using the dichotomy method to narrow the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained;
[0105] The querying unit 34 is configured to traverse the historical price data within the target data cycle, query the target historical price data corresponding to the target timestamp, and send the target historical price data to the blockchain interaction interface.
[0106] For the above price query device based on an oracle, the receiving unit 31 receives a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parses the financial transaction request to determine a target object to be queried and a target timestamp. Among them, the transaction request includes a historical price query task; the determining unit 32 calls a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried. Among them, the data stream in the oracle is stored in units of data cycles; the calculating unit 33 performs iterative calculations on the basis of the initial data cycle range by using the dichotomy method to narrow the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained; the querying unit 34 traverses the historical price data within the target data cycle, queries the target historical price data corresponding to the target timestamp, and sends the target historical price data to the blockchain interaction interface.
[0107] In this embodiment, for a financial transaction request involving price data query in a blockchain environment, the native oracle interface is called to determine the initial data query range based on the data stream stored in the oracle. On this basis, the dichotomy method is used to continuously iterate and narrow down the data query range until the target data period where the data to be queried is located is found. Then, the data stream in the target data period is traversed to determine the historical price data corresponding to the target timestamp, achieving the purpose of quickly querying historical price data, obtaining the technical effect of improving the efficiency of blockchain data query, and further solving the technical problem of low efficiency in the related art of querying prices by traversing the data stream in the oracle in the blockchain.
[0108] Further, the determination unit 32 includes: a first call subunit, configured to call the native data interface of the oracle based on the target object to be queried and access the data stream in the oracle through the native data interface; a first determination subunit, configured to determine the earliest updated data period and the latest updated data period in the oracle; and a second determination subunit, configured to determine the initial data period range to be queried based on the earliest updated data period and the latest updated data period.
[0109] Further, the calculation unit 33 includes: a first selection subunit, configured to perform step one, select the midpoint of the initial data period range, and divide the initial data period range by the midpoint; a first comparison subunit, configured to perform step two, determine the historical timestamp corresponding to the midpoint, and compare the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; a first narrowing subunit, configured to perform step three, narrow down the data period range to be queried based on the comparison result to obtain a new initial data period range; and a first repetition subunit, configured to repeat the above steps one to three for iterative calculation until the data period range is narrowed down to the data period where the target timestamp is located and then stop the iteration.
[0110] Further, the first comparison subunit includes: a first reading module, configured to obtain the timestamp field of the data period corresponding to the midpoint and read the historical timestamp corresponding to the midpoint based on the timestamp field; and a first comparison module, configured to compare the value of the historical timestamp with the value of the target timestamp.
[0111] Further, the first narrowing subunit includes: a first serving as module, configured to use the data period corresponding to the midpoint as the boundary of the data period range to be queried according to the comparison result; and a first narrowing module, configured to narrow down the data period range to be queried based on the boundary of the data period range to be queried.
[0112] Further, the first acting module includes: a first acting sub-module, configured to use the data period corresponding to the intermediate point as the left boundary of the data period range to be queried when the comparison result indicates that the historical timestamp corresponding to the intermediate point is earlier than the target timestamp; and a second acting sub-module, configured to use the data period corresponding to the intermediate point as the right boundary of the data period range to be queried when the comparison result indicates that the historical timestamp corresponding to the intermediate point is later than the target timestamp.
[0113] Further, the first shrinking module includes: a first determining sub-module, configured to obtain the right boundary of the currently to-be-query data period range and determine a new to-be-query data period range based on the left boundary and the right boundary to obtain a shrunk data period range when using the data period corresponding to the intermediate point as the left boundary of the data period range to be queried; and a second determining sub-module, configured to obtain the left boundary of the currently to-be-query data period range and determine a new to-be-query data period range based on the left boundary and the right boundary to obtain a shrunk data period range when using the data period corresponding to the intermediate point as the right boundary of the data period range to be queried.
[0114] The above oracle-based price query device may further include a processor and a memory. The above receiving unit 31, determining unit 32, calculating unit 33, querying unit 34, etc. are all stored in the memory as program units, and the processor executes the above program units stored in the memory to implement corresponding functions.
[0115] The above processor includes a kernel, and the kernel retrieves the corresponding program units from the memory. One or more kernels can be set, and by adjusting the kernel parameters, price data can be queried quickly and efficiently.
[0116] The above memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in forms such as read-only memory (ROM) or flash memory (flash RAM), and the memory includes at least one storage chip.
[0117] On the other hand, according to an embodiment of the present invention, there is also provided a computer-readable storage medium, which includes a stored computer program. When the computer program runs, it controls the device where the computer-readable storage medium is located to execute any one of the above oracle-based price query methods.
[0118] On the other hand, according to an embodiment of the present invention, there is also provided an electronic device, including one or more processors and a memory. The memory is used to store one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors are caused to implement any one of the above oracle-based price query methods.
[0119] According to another aspect of the embodiments of the present invention, there is also provided a computer program product, which includes a computer program. When the computer program is executed by a processor, the above-mentioned oracle-based price query method is implemented.
[0120] The present application also provides a computer program product, which, when executed on a data processing device, is adapted to execute a program initialized with the following method steps: receiving a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parsing the financial transaction request to determine a target object and a target timestamp to be queried, where the transaction request includes a historical price query task; calling a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried, where the data stream in the oracle is stored in units of data cycles; using the dichotomy method to perform iterative calculations on the basis of the initial data cycle range to narrow the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained; traversing the historical price data within the target data cycle to query the target historical price data corresponding to the target timestamp, and sending the target historical price data to the blockchain interaction interface.
[0121] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: the step of calling a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried includes: calling a native data interface of the oracle based on the target object to be queried, and accessing the data stream in the oracle through the native data interface; determining the earliest updated data cycle and the latest updated data cycle in the oracle; determining the initial data cycle range to be queried based on the earliest updated data cycle and the latest updated data cycle.
[0122] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: the step of using the dichotomy method to perform iterative calculations on the basis of the initial data cycle range to narrow the data cycle range to be queried includes: Step 1, selecting the midpoint of the initial data cycle range and dividing the initial data cycle range through the midpoint; Step 2, determining the historical timestamp corresponding to the midpoint, and comparing the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; Step 3, narrowing the data cycle range to be queried based on the comparison result to obtain a new initial data cycle range; repeating the above steps 1 to 3 for iterative calculations until the data cycle range is narrowed to the data cycle where the target timestamp is located, and then stopping the iteration.
[0123] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: determining the historical timestamp corresponding to the intermediate point, and the step of comparing the historical timestamp corresponding to the intermediate point with the target timestamp includes: obtaining the timestamp field of the data period corresponding to the intermediate point, and reading the historical timestamp corresponding to the intermediate point based on the timestamp field; comparing the value of the historical timestamp with the value of the target timestamp.
[0124] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: the step of narrowing down the data period range to be queried based on the comparison result includes: taking the data period corresponding to the intermediate point as the boundary of the data period range to be queried according to the comparison result; narrowing down the data period range to be queried based on the boundary of the data period range to be queried.
[0125] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: the step of taking the data period corresponding to the intermediate point as the boundary of the data period range to be queried according to the comparison result includes: when the comparison result indicates that the historical timestamp corresponding to the intermediate point is earlier than the target timestamp, taking the data period corresponding to the intermediate point as the left boundary of the data period range to be queried; or, when the comparison result indicates that the historical timestamp corresponding to the intermediate point is later than the target timestamp, taking the data period corresponding to the intermediate point as the right boundary of the data period range to be queried.
[0126] The present application also provides a computer program product, which, when executed on a data processing device, is also adapted to execute a program initialized with the following method steps: the step of narrowing down the data period range to be queried based on the boundary of the data period range to be queried includes: when taking the data period corresponding to the intermediate point as the left boundary of the data period range to be queried, obtaining the right boundary of the current data period range to be queried, and determining a new data period range to be queried based on the left boundary and the right boundary to obtain a narrowed data period range; or, when taking the data period corresponding to the intermediate point as the right boundary of the data period range to be queried, obtaining the left boundary of the current data period range to be queried, and determining a new data period range to be queried based on the left boundary and the right boundary to obtain a narrowed data period range.
[0127] Figure 4 is a hardware structural block diagram of an electronic device (or mobile device) for executing an oracle-based price query method according to an embodiment of the present invention. As Figure 4 shown, the electronic device may include one or more processors ( Figure 4In the figure, 402a, 402b, ……, 402n are used to show that the processor may include, but is not limited to, a processing device such as a microprocessor MCU or a programmable logic device FPGA), and a memory 404 for storing data. In addition, it may further include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply, and / or a camera. Those of ordinary skill in the art can understand that Figure 4 The structure shown is only illustrative and does not limit the structure of the above-mentioned electronic device. For example, the electronic device may further include more or fewer components than those shown Figure 4 in the figure, or have a different configuration from that Figure 4 shown in the figure.
[0128] The serial numbers of the above-mentioned embodiments of the present invention are only for description and do not represent the superiority or inferiority of the embodiments.
[0129] In the above-mentioned embodiments of the present invention, the descriptions of the respective embodiments have their own emphases. For the parts not detailed in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0130] In the several embodiments provided by the present application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of the units can be a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of units or modules can be in electrical or other forms.
[0131] The units described as separate components may or may not be physically separated. The components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0132] In addition, in each embodiment of the present invention, the functional units can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above-mentioned integrated units can be implemented in the form of hardware or in the form of software functional units.
[0133] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The foregoing storage medium includes: various media that can store program codes, such as USB flash drives, read-only memories (ROMs), random access memories (RAMs), mobile hard disks, magnetic disks, or optical discs.
[0134] The foregoing are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.
Claims
1. A price query method based on an oracle, characterized in that, Including: Receiving a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parsing the financial transaction request to determine a target object and a target timestamp to be queried, where the transaction request includes a historical price query task; Invoking a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried, where the data stream in the oracle is stored in units of data cycles; Performing iterative calculation on the basis of the initial data cycle range by using the dichotomy method to narrow down the data cycle range to be queried until the target data cycle where the historical price data corresponding to the target timestamp is obtained; Traversing the historical price data within the target data cycle, querying the target historical price data corresponding to the target timestamp, and sending the target historical price data to the blockchain interaction interface.
2. The method according to claim 1, wherein The step of invoking a data interface of an oracle based on the target object to be queried to determine an initial data cycle range to be queried includes: Invoking a native data interface of an oracle based on the target object to be queried, and accessing the data stream in the oracle through the native data interface; Determining the earliest updated data cycle and the latest updated data cycle in the oracle; Determining an initial data cycle range to be queried based on the earliest updated data cycle and the latest updated data cycle.
3. The method according to claim 1, characterized in that, The step of performing iterative calculation on the basis of the initial data cycle range by using the dichotomy method to narrow down the data cycle range to be queried includes: Step 1, selecting the midpoint of the initial data cycle range, and dividing the initial data cycle range by the midpoint; Step 2, determining the historical timestamp corresponding to the midpoint, and comparing the historical timestamp corresponding to the midpoint with the target timestamp to obtain a comparison result; Step 3, narrowing down the data cycle range to be queried based on the comparison result to obtain a new initial data cycle range; Repeating the above steps 1 to 3 for iterative calculation until the data cycle range is narrowed down to the data cycle where the target timestamp is located, and then stopping the iteration.
4. The method according to claim 3, characterized in that, The step of determining the historical timestamp corresponding to the midpoint and comparing the historical timestamp corresponding to the midpoint with the target timestamp includes: Obtaining the timestamp field of the data cycle corresponding to the midpoint, and reading the historical timestamp corresponding to the midpoint based on the timestamp field; Comparing the value of the historical timestamp with the value of the target timestamp.
5. The method according to claim 3, characterized in that, The step of narrowing down the data cycle range to be queried based on the comparison result includes: Taking the data cycle corresponding to the midpoint as the boundary of the data cycle range to be queried according to the comparison result; Narrowing down the data cycle range to be queried based on the boundary of the data cycle range to be queried.
6. The method according to claim 5, wherein The step of taking the data cycle corresponding to the midpoint as the boundary of the data cycle range to be queried according to the comparison result includes: In the case where the comparison result indicates that the historical timestamp corresponding to the midpoint is earlier than the target timestamp, taking the data cycle corresponding to the midpoint as the left boundary of the data cycle range to be queried; or, In the case that the historical timestamp corresponding to the intermediate point indicated by the comparison result is later than the target timestamp, use the data period corresponding to the intermediate point as the right boundary of the data period range to be queried.
7. The method according to claim 6, wherein The step of narrowing the data period range to be queried based on the boundaries of the data period range to be queried includes: In the case of using the data period corresponding to the intermediate point as the left boundary of the data period range to be queried, obtain the right boundary of the currently queried data period range, and determine a new data period range to be queried based on the left boundary and the right boundary to obtain a narrowed data period range; or, In the case of using the data period corresponding to the intermediate point as the right boundary of the data period range to be queried, obtain the left boundary of the currently queried data period range, and determine a new data period range to be queried based on the left boundary and the right boundary to obtain a narrowed data period range.
8. A price query device based on an oracle, characterized in that, Includes: A receiving unit for receiving a financial transaction request initiated by a user terminal based on a blockchain interaction interface, and parsing the financial transaction request to determine a target object and a target timestamp to be queried, wherein the transaction request includes a historical price query task; A determining unit for calling a data interface of an oracle based on the target object to be queried to determine an initial data period range to be queried, wherein the data stream in the oracle is stored in units of data periods; A calculation unit for performing iterative calculations on the basis of the initial data period range by using the binary method to narrow the data period range to be queried until the target data period where the historical price data corresponding to the target timestamp is obtained; A query unit for traversing the historical price data within the target data period, querying the target historical price data corresponding to the target timestamp, and sending the target historical price data to the blockchain interaction interface.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein when the computer program runs, it controls the device where the computer-readable storage medium is located to execute the oracle-based price query method according to any one of claims 1 to 7.
10. An electronic device, characterized in that, Includes one or more processors and a memory, the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the oracle-based price query method according to any one of claims 1 to 7.