Computing, updating, reading methods and devices, and electronic devices based on smart contracts
By introducing a hierarchical sampling mechanism into blockchain smart contracts, the problem of time-consuming calculation of big data sets is solved, and the efficiency of approximate calculation is achieved.
Patent Information
- Application Number
- CN202210334257.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-30
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2042-03-30
AI Technical Summary
When performing data calculations on blockchain, it is time-consuming to accurately calculate the big data set, and in some business scenarios, it does not require accurate results, but can tolerate certain calculation accuracy losses.
A hierarchical sampling mechanism is introduced in smart contracts, and data samples are obtained through hierarchical sampling for approximate calculations, reducing the calculation time.
Without sacrificing the accuracy of the approximate calculation results, the calculation efficiency of the data set is significantly improved.
Smart Images

Figure CN114708096B_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a computing device and an electronic device based on a smart contract. Background Art
[0002] Blockchain is a new application mode of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithms. In a blockchain system, data blocks are combined into a chained data structure in a sequential connection manner according to the time sequence, and a distributed ledger that is immutable and unforgeable is guaranteed by cryptographic means. Due to the characteristics of decentralization, information immutability, and autonomy of the blockchain, the blockchain has received more and more attention and applications. Summary of the Invention
[0003] This specification proposes a computing method based on a smart contract, which is applied to a node device in a blockchain. A smart contract for performing approximate calculation is deployed on the blockchain. The method includes:
[0004] Receiving a smart contract call transaction initiated by a calculation initiator for the smart contract; wherein, the smart contract call transaction includes calculation parameters corresponding to the approximate calculation; the calculation parameters include data identifiers of a data set participating in the approximate calculation;
[0005] In response to the smart contract call transaction, calling the sampling logic included in the smart contract call transaction to perform stratified sampling on data samples in the data set corresponding to the data identifier;
[0006] Further calling the approximate calculation logic included in the smart contract call transaction to perform approximate calculation based on the data samples stratified and sampled from the data set, so as to obtain an approximate calculation result for the data set.
[0007] This specification also proposes a computing device based on a smart contract, which is applied to a node device in a blockchain. A smart contract for performing approximate calculation is deployed on the blockchain. The device includes:
[0008] A receiving module, which receives a smart contract call transaction initiated by a calculation initiator for the smart contract; wherein, the smart contract call transaction includes calculation parameters corresponding to the approximate calculation; the calculation parameters include data identifiers of a data set participating in the approximate calculation;
[0009] A sampling module, which, in response to the smart contract call transaction, calls the sampling logic included in the smart contract call transaction to perform stratified sampling on data samples in the data set corresponding to the data identifier;
[0010] The calculation module further calls the approximate calculation logic included in the smart contract call transaction, and performs approximate calculation based on the data samples obtained by hierarchical sampling from the data set, so as to obtain an approximate calculation result for the data set.
[0011] In the above technical solution, in the scenario of performing approximate calculation on a data set by calling a smart contract, by introducing a hierarchical sampling mechanism for the data set in the smart contract, it is possible to reduce the time consumption when performing approximate calculation on the data set without sacrificing the accuracy of the approximate calculation result, and improve the calculation efficiency when performing approximate calculation on the data set. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 is a flowchart of a calculation method based on a smart contract provided by an exemplary embodiment;
[0013] Figure 2 is a flowchart of an optimization solution method provided by an exemplary embodiment;
[0014] Figure 3 is a schematic structural diagram of an electronic device provided by an exemplary embodiment;
[0015] Figure 4 is a block diagram of a calculation device based on a smart contract provided by an exemplary embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0016] Here, the exemplary embodiments will be described in detail, and the examples are shown in the drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with one or more embodiments of this specification. On the contrary, they are merely examples of devices and methods consistent with some aspects of one or more embodiments of this specification as detailed in the appended claims.
[0017] It should be noted that: in other embodiments, the steps of the corresponding method are not necessarily executed in the order shown and described in this specification. In some other embodiments, the steps included in the method may be more or less than those described in this specification. In addition, a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may also be combined into a single step for description in other embodiments.
[0018] With the continuous development of smart contract technology, when using smart contracts to interface with a business, smart contracts have gradually begun to undertake a part of the computing power related to the business.
[0019] For example, in practical applications, in the smart contract deployed on the blockchain for docking with a business, in addition to including business logic related to the business, it can also include logic for calculating business data related to the business, so that users can complete calculations related to the business on the blockchain by calling the smart contract.
[0020] When using a smart contract to calculate a data set related to a business, the total calculation time usually depends on the time-consuming for performing I / O operations on each piece of data separately and the time-consuming for batch calculating the above set of data.
[0021] For example, in practical applications, taking the case where a data set related to a business is pre-archived on the blockchain as an example, at this time, the total time-consuming when the smart contract calculates the data set related to the business can usually be expressed by the following formula:
[0022]
[0023] Among them, in the above formula, i represents the i-th piece of data in the above data set; IO i represents the time-consuming for performing I / O operation processing on the i-th piece of data; Operation i represents the time-consuming for batch calculating i pieces of data in the data set.
[0024] It should be noted that since the data set is usually archived on the blockchain in the form of key-Value key-value pairs, and is stored one by one in the storage medium carried by the blockchain node device, for the above data set stored on the blockchain, usually only according to the key value of the data, the data can be read one by one from the storage medium carried by the blockchain node device.
[0025] In some application scenarios with high requirements for the privacy and security of data calculation, the above smart contract can also be deployed in the TEE (Trusted Execution Environment) carried by the blockchain node device.
[0026] In this case, the data in the above data set usually needs to be encrypted and stored. At this time, when using a smart contract to calculate a data set related to a business, the total calculation time usually depends on the time-consuming for performing I / O operations on each piece of data separately, the time-consuming for decrypting each piece of data separately, and the time-consuming for batch calculating the above set of data.
[0027] For example, in practical applications, taking the example that a data set related to a business is pre-deposited on a blockchain, the total time consumed when a smart contract calculates the data set related to the business can usually be expressed by the following formula:
[0028]
[0029] Among them, in the above formula, Operation i represents the time consumed for decrypting the i-th piece of data in the data set.
[0030] It is not difficult to see from the above introduction that in the scenario of using a smart contract to calculate a data set related to a business, if the amount of data contained in the data set is relatively large, it is very time-consuming to calculate the data set accurately through the smart contract.
[0031] In practical applications, in some business scenarios, it may not be necessary to obtain the accurate calculation result of the data related to the business, but some loss in calculation accuracy can be tolerated.
[0032] For example, in the calculation scenario of calculating the average age of users, in most cases, an accurate calculation result is not required, and usually only an approximate calculation to obtain an interval of the average age is needed.
[0033] Based on this, this specification proposes a technical solution to introduce an approximate calculation and stratified sampling mechanism in a smart contract to improve the calculation efficiency of calculating data related to a business.
[0034] When implementing, a smart contract for data calculation can be deployed on the blockchain. The smart contract can include an approximate calculation logic for approximate calculation and a sampling logic for stratified sampling. The calculation initiator can call the smart contract to perform approximate calculation on the data set participating in the calculation by initiating a smart contract call transaction. Among them, the smart contract call transaction can include calculation parameters corresponding to the approximate calculation; the calculation parameters can include the data identifier of the data set participating in the approximate calculation;
[0035] When the node device in the blockchain receives the smart contract call transaction initiated by the calculation initiator, it can, in response to the smart contract call transaction, call the sampling logic included in the smart contract call transaction to perform stratified sampling on the data samples in the data set corresponding to the above data identifier. After the stratified sampling is completed, it can further call the approximate calculation logic included in the smart contract to perform approximate calculation based on the data samples obtained by stratified sampling from the above data set to obtain an approximate calculation result for the data set.
[0036] In the above technical solution, in the scenario of invoking a smart contract to perform approximate calculations on a data set, by introducing a hierarchical sampling mechanism for the data set in the smart contract, it is possible to reduce the time consumed for performing approximate calculations on the data set and improve the calculation efficiency when performing approximate calculations on the data set, without sacrificing the accuracy of the approximate calculation results.
[0037] For example, still taking the case where a data set related to the business is pre-stored on the blockchain as an example, after introducing the hierarchical sampling mechanism in the smart contract, the total time consumed when the smart contract calculates the data set related to the business can usually be expressed by the following formula:
[0038]
[0039] Among them, in the above formula, n g represents the number of data samples obtained by hierarchical sampling from the data set. N g represents the total number of data samples in the data set. Since the value of n g is usually of an order of magnitude difference compared to the value of N g , after introducing the hierarchical sampling mechanism in the smart contract, the time consumed for calculating the data set through this smart contract will also be reduced by an order of magnitude.
[0040] It can be seen that introducing the hierarchical sampling mechanism in the smart contract can significantly improve the calculation efficiency when performing approximate calculations on the data set.
[0041] Please refer to Figure 1 , Figure 1 which is a flowchart of a calculation method based on a smart contract provided by an exemplary embodiment. The method is applied to a node device in a blockchain; wherein, a smart contract for performing approximate calculations is deployed on the blockchain, and the method includes the following steps:
[0042] Step 102, receiving a smart contract call transaction for the smart contract initiated by a calculation initiator; wherein, the smart contract call transaction includes calculation parameters corresponding to the approximate calculation; the calculation parameters include data identifiers of a data set participating in the approximate calculation;
[0043] The above calculation initiator may specifically be a party with data calculation requirements. For example, in one example, the above calculation initiator may be a user with data calculation requirements. In another example, in the scenario of docking with the business based on the smart contract, the calculation initiator may specifically also be an off-chain business system with data calculation requirements.
[0044] On the blockchain, smart contracts for data calculation can be deployed. The execution logic corresponding to the contract code included in the smart contract can specifically include approximate calculation logic for approximate calculation and sampling logic for data sampling. In this way, the logic of approximate calculation and data sampling for data can be introduced into the smart contract.
[0045] It should be noted that the sampling method used for the above data sampling is not specifically limited in this specification; for example, random sampling, stratified sampling, etc. can be used.
[0046] In the following embodiments, the above data sampling is stratified sampling, and the above sampling logic is stratified sampling logic as an example for illustration.
[0047] The above calculation initiator can call the above smart contract to perform approximate calculation on the data set participating in the calculation by initiating a smart contract call transaction.
[0048] For example, taking the above calculation initiator as a user and the above blockchain as a blockchain adopting an account model as an example, in this case, the above smart contract can be understood as a contract account anchored with contract code on the blockchain, and the user can register an external account on the blockchain and initiate a smart contract call transaction through the external account, and submit the smart contract call transaction to the connected blockchain node device to call the smart contract.
[0049] It should be noted that in the above smart contract call transaction, it can specifically include calculation parameters corresponding to approximate calculation; the calculation parameters can include data identifiers of the data set participating in the approximate calculation.
[0050] When the above calculation initiator initiates the above smart contract call transaction, if the calculation initiator directly docks with the blockchain node, a smart contract transaction can be packaged and directly submitted point-to-point to the connected blockchain node device. If the calculation initiator accesses the blockchain through a blockchain access service provided by, for example, a Baas (Blockchain as a Service) platform, a call request for the above smart contract can be generated and submitted to the Baas platform, and then the Baas platform packages a smart contract call transaction based on the call parameters carried in the call request and submits it to the blockchain node device.
[0051] The blockchain node device can receive the above-mentioned smart contract call transaction initiated by the above-mentioned calculation initiator, and when receiving the above-mentioned smart contract call transaction, it can respond to the smart contract call transaction, call the above-mentioned smart contract on the blockchain, and perform approximate calculation on the above-mentioned data set.
[0052] Step 104, in response to the smart contract call transaction, call the sampling logic included in the smart contract to perform stratified sampling on the data samples in the data set corresponding to the data identifier.
[0053] After the blockchain node device receives the above-mentioned smart contract call transaction initiated by the above-mentioned calculation initiator, it can respond to the smart contract call transaction, call the sampling logic included in the smart contract, and perform stratified sampling on the data samples in the data set corresponding to the data identifier.
[0054] It should be noted that after the blockchain node device receives the above-mentioned smart contract call transaction initiated by the above-mentioned calculation initiator, it usually also needs to, based on the consensus algorithm supported by the blockchain, together with other blockchain nodes participating in the consensus, perform consensus processing on the smart contract call transaction and the execution result of the smart contract call transaction. Since this specification does not involve improving the consensus process of the blockchain, the process of performing consensus processing on the smart contract call transaction and the execution result of the smart contract call transaction will not be elaborated in this specification.
[0055] In an illustrated implementation manner, before the blockchain node device calls the sampling logic included in the smart contract to perform stratified sampling on the data samples in the data set corresponding to the data identifier, it can first obtain the above-mentioned data identifier included in the above-mentioned smart contract call transaction, and read the data set participating in the approximate calculation based on this data identifier.
[0056] When reading the data set participating in the approximate calculation based on this data identifier, it can specifically be read from the blockchain or read from off-chain, which is not particularly limited in this specification.
[0057] In one implementation manner, this data set can specifically be pre-certified on the above-mentioned blockchain.
[0058] For example, a certification contract for data certification can also be deployed on the blockchain. Before the calculation initiator calls the above-mentioned smart contract for calculation, it can publish the data set that needs to participate in the calculation to the certification contract for certification by packing a certification transaction.
[0059] For another example, the execution logic corresponding to the contract code included in the above intelligent contract may, in addition to including the above approximate calculation logic and the above sampling logic, further include data deposition logic. That is to say, in addition to being used for approximate calculation, the intelligent contract itself also has a data deposition function for data. At this time, before invoking the intelligent contract for calculation, the calculation initiator may also first pre-publish the data set that needs to participate in the calculation to the intelligent contract for deposition by packing a deposition transaction, and then the intelligent contract can read the above-mentioned data set that has been deposited from its own contract storage space for approximate calculation.
[0060] In this case, the blockchain node device may obtain, based on the above data identifier, the data set deposited on the blockchain corresponding to the data identifier. For example, in this case, the data identifier may specifically be the deposition hash returned by the blockchain node after the above data set is successfully deposited on the blockchain.
[0061] In another implementation manner, the data set may specifically also be pre-deposited in an off-chain database docked with the above blockchain. In this case, the intelligent contract may obtain, through its corresponding oracle machine, the data set corresponding to the data identifier from the above off-chain database.
[0062] Among them, the above oracle machine may specifically be a centralized oracle machine or a decentralized oracle machine. When the oracle machine is a centralized oracle machine, the oracle machine may specifically be an oracle service program deployed on an off-chain service device. When the oracle machine is a decentralized oracle machine, the oracle machine may specifically be an oracle contract deployed on the blockchain and docked with the above intelligent contract. It should be noted that since this specification does not involve improvements related to the oracle machine, the specific implementation process of the above intelligent contract obtaining the data set corresponding to the data identifier from the above off-chain database through its corresponding oracle machine will not be elaborated in this specification.
[0063] For the calculation parameters included in the above intelligent contract call transaction, in addition to including the data identifiers of the above-mentioned data sets mentioned above, in practical applications, other forms of parameters related to approximate calculation may also be included.
[0064] In an illustrated implementation manner, the above calculation parameters may specifically include various parameters shown in the following table:
[0065] Parameter type Parameter meaning Dataset ID Indicates the data set participating in the approximate calculation Calculation type ID Indicates the calculation type of the approximate calculation to be performed Error value Indicates the calculation error of the tolerable approximate calculation Confidence probability Indicates the accuracy of the expected approximate calculation Sampling algorithm ID Indicates the specified sampling algorithm type
[0066] Among them, it should be noted that in the above table, except for the dataset ID, other parameters are all optional parameters.
[0067] For example, if the calculation parameters in the above smart contract call transaction do not include the calculation type ID, it means that the above smart contract is allowed to perform approximate calculations on the above data set using the default calculation type. If the calculation parameters in the above smart contract call transaction do not include the error value, it means that the tolerable calculation error is 0. If the calculation parameters in the above smart contract call transaction do not include the confidence probability, it means that the confidence probability is 100%, and the expected accuracy of the approximate calculation is 100%. In this case, the above smart contract will perform an exact calculation on the above data set and will no longer perform an approximate calculation.
[0068] In an illustrated embodiment, when the blockchain node device calls the sampling logic included in the above smart contract to perform stratified sampling on the data set obtained corresponding to the above data identifier, it can specifically first calculate the sampling quantity for stratified sampling, and then perform stratified sampling according to the calculated sampling quantity.
[0069] In an illustrated embodiment, Hoeffding's Inequality is generally used to describe the upper bound of the probability of the deviation of the sum of random variables from its expected value. In the scenario of approximate calculation, the above sampling quantity can be used as a random variable, the error value of the above approximate calculation can be used as the deviation of the expected value, and the confidence probability of the above approximate calculation can be used as the above probability upper bound. Therefore, in this specification, Hoeffding's Inequality can be used to describe the mathematical relationship between the above sampling quantity, the error value of the above approximate calculation, and the confidence probability of the above approximate calculation. In other words, in the scenario of approximate calculation, Hoeffding's Inequality can be used to derive the mathematical relationship between the above sampling quantity, the error value of the above approximate calculation, and the confidence probability of the above approximate calculation.
[0070] Among them, when using Hoeffding's Inequality to describe the mathematical relationship between the above sampling quantity, the error value of the above approximate calculation, and the confidence probability of the above approximate calculation, Hoeffding's Inequality is expressed as the following formula:
[0071]
[0072] In the above formula, H represents the mathematical identifier of Hoeffding's Inequality. n g represents the sampling quantity. b g , a g respectively represent the maximum value and the minimum value of the data samples in the data set. δ represents the confidence probability; ε g represents the error value corresponding to the above approximate calculation; N gRepresents the total number of data samples in the said data set.
[0073] The mathematical relationship among the above sampling quantity, the error value of the above approximate calculation, and the confidence probability of the above approximate calculation derived based on the above formula can be expressed by the following formula:
[0074]
[0075] In the above smart contract, the above mathematical relationship can be pre-maintained. When the blockchain node device calls the above smart contract to calculate the sampling quantity required for stratified sampling, it can obtain the confidence probability δ corresponding to the approximate calculation and the error value ε corresponding to the approximate calculation in the calculation parameters of the above smart contract call transaction. g , and then input the obtained confidence probability δ and error value ε g into the above mathematical relationship maintained for calculation to obtain the sampling quantity corresponding to the above data set.
[0076] Among them, when using stratified sampling, it is usually necessary to divide the above data set into several buckets (i.e., the above data subsets), and then perform data sampling from these buckets respectively. Therefore, when the blockchain node device calls the above smart contract to calculate the sampling quantity required for stratified sampling, it can obtain the confidence probability δ corresponding to the approximate calculation and the error value ε corresponding to each bucket included in the calculation parameters carried by the above smart contract call transaction. k , and then input the obtained confidence probability δ and the error value ε of each bucket k into the above mathematical relationship maintained for calculation respectively to obtain the sampling quantity corresponding to each bucket divided from the above data set.
[0077] It should be noted that when performing stratified sampling on the above data set, the number K of buckets to be divided and the error value ε corresponding to each bucket k , can be specified by the calculation initiator and carried as calculation parameters in the above smart contract call transaction. For example, in this case, in addition to carrying a total error ε specified by the calculation initiator for the approximate calculation of the above data set in the above calculation parameters g , it is also necessary to carry K error values ε corresponding to each bucket k .
[0078] In addition, when performing stratified sampling on the above data set, the number K of buckets to be divided and the error value ε corresponding to each bucket k , can specifically also be the optimal values calculated autonomously by the above smart contract on the chain.
[0079] In an illustrated implementation, the number K of buckets to be partitioned, and the error value ε corresponding to each bucket k can be the optimal values solved by the above smart contract using an optimization solution method.
[0080] In this case, when the blockchain node device invokes the sampling logic included in the above smart contract to perform stratified sampling on the data set obtained corresponding to the above data identifier, it can first use the optimization solution method to solve the optimal number K of buckets required for stratified sampling of the above data set, and the optimal error value ε corresponding to each bucket k and then compare the confidence probability δ corresponding to approximate calculation in the calculation parameters of the above smart contract call transaction with the optimal error value ε corresponding to each solved bucket k and input them into the maintained above mathematical relationship for calculation respectively to obtain the optimal sampling number corresponding to each bucket partitioned from the above data set. Then, based on the calculated above optimal number K and the above optimal sampling number, stratified sampling can be performed on the data samples in the above data set.
[0081] It should be noted that the specific type of the optimization solution method adopted by the above smart contract is not particularly limited in this specification. In practical applications, those skilled in the art can flexibly adopt different optimization solution algorithms based on actual needs. For example, in practical applications, commonly used optimal solution algorithms such as the Gradient Descent method can be specifically adopted.
[0082] Among them, for the optimization solution method, a clear constraint condition usually needs to be set. And in practical applications, the above constraint condition can usually be set based on specific solution objectives.
[0083] In the scenario of performing stratified sampling on the above data set, the optimization solution objectives can include solving the optimal number of buckets partitioned, solving the optimal error value corresponding to each bucket, and so on. Then, in practical applications, the above constraint condition can be set for the above optimization solution method based on the above optimization objectives.
[0084] In an illustrated implementation, based on the above solution objectives, setting the constraint condition for the above optimization solution method can specifically be:
[0085] Perform weighted average calculation on the error values corresponding to each bucket, and the obtained weighted average error value is the smallest and not greater than the total error value corresponding to approximate calculation of the above data set.
[0086] For example, the above constraints can be expressed as the following formula:
[0087]
[0088] In the above formula, ε g represents the total error value corresponding to the approximate calculation for the above data set. N k represents the number of samples sampled from the k-th bucket. N represents the total number of samples sampled from the above data set.
[0089] The following describes the specific algorithm flow for solving the optimal number of buckets required for hierarchical sampling and the optimal error value of each bucket by using the above constraints through the accompanying drawings and specific embodiments.
[0090] Please refer to Figure 2 , Figure 2 which is a flowchart of an optimization solution method shown in this specification, including the following execution steps:
[0091] Step 201, initialize the value of i; wherein, the value of i represents the number of samples included in each initialized bucket. Except for step 201, the following steps are iterative steps:
[0092] Step 202, adjust the value corresponding to the initialized value of i;
[0093] Among them, the adjustment range of the value of i can be set flexibly and is not specifically limited in this specification.
[0094] Step 203, divide the data set into several buckets each containing i samples;
[0095] Step 204, use the above confidence probability δ (i.e., the confidence probability δ included in the calculation parameters carried by the smart contract call transaction) and the adjusted value of i (i.e., the number of samples corresponding to each bucket) as calculation parameters, input them into the mathematical relationship for calculation, obtain the error values corresponding to each bucket respectively, and perform a weighted average calculation on the error values corresponding to each bucket to obtain a weighted average error value;
[0096] It should be noted that, if it is the first round of iteration, after step 204 is executed, steps 202 - 204 will be executed again to perform the second round of iteration.
[0097] Step 205: Determine whether the weighted average error value is not greater than the total error value (i.e., the error value corresponding to the approximate calculation in the calculation parameters carried by the smart contract call transaction), and is less than the weighted average error value calculated in the previous iteration (i.e., the weighted average error value calculated based on the i value before the adjustment in this iteration); if not, re-execute the above steps 202 - 205, continue with the next iteration, and repeat the above iterative process until the optimization algorithm converges and stops iterating when the weighted average error value that meets the above constraint conditions is calculated.
[0098] Step 206: After stopping the iteration, obtain the optimal i value when the calculated weighted average error value meets the above constraint conditions.
[0099] Step 207: Based on the optimal i value, determine the optimal number of buckets required for stratified sampling of the data set, and input the above confidence probability and the optimal i value into the mathematical relationship for calculation again to obtain the optimal error value corresponding to each bucket. It should be noted that in the above embodiments, this is a specific implementation manner of setting constraint conditions for the above optimization method based on the above-described solution objective. In practical applications, obviously, other forms of constraint conditions can also be set for the above optimization method based on the above solution objective. In this specification, after the smart contract uses the optimization method to solve the optimal number of buckets to be divided and the optimal sampling number corresponding to each bucket, stratified sampling of the data set can be performed based on the optimal number and the optimal error value.
[0100] In an illustrated embodiment, when the smart contract performs stratified sampling of the data set based on the optimal number and the optimal error value, first, the data set can be divided into several buckets according to the optimal number; for example, assuming the optimal number is K, the data set can be divided into K buckets. Then, from each of the divided buckets, the data samples in each bucket can be sampled according to the optimal sampling number.
[0101] Among them, the specific sampling method used to sample the data samples in each bucket according to the optimal sampling number is not particularly limited in this specification.
[0102] In an illustrated embodiment, the specific sampling method used to sample the data samples in each bucket according to the optimal sampling number can specifically adopt the random sampling method.
[0103] If the random sampling method is adopted and the data samples in each bucket are sampled according to the above optimal sampling quantity, specifically, the random numbers for random sampling can be obtained first, and then the data samples in each bucket can be randomly sampled based on the obtained random numbers to obtain the data samples corresponding to the calculated above optimal sampling quantity.
[0104] Among them, the above random numbers are specifically used to control the randomness of the data samples sampled from each bucket. In practical applications, the data samples to be sampled from each bucket can be determined according to the obtained random numbers. For example, in one example, the random numbers can be used to represent the sample identifiers of the data samples to be sampled. During the random sampling process, the data samples with the value of the random number as the sample identifier can be randomly selected from the bucket to complete the data sampling.
[0105] It should be noted that regarding the specific method for obtaining the above random numbers, they can be generated on the blockchain or obtained from off-chain. This is not specifically limited in this specification.
[0106] The following are several specific methods for obtaining random numbers shown in this specification:
[0107] In one shown method, a random function for generating random numbers can be pre-deployed on the blockchain. For example, in practical applications, the above random function can be specifically deployed on the blockchain as an independent smart contract, or deployed in the smart contract as the execution logic included in the above smart contract for approximate calculation. In this case, random numbers can be generated on the blockchain by calling the above random function;
[0108] In another shown method, a Trusted Execution Environment can be installed on the above blockchain node device. In this Trusted Execution Environment, a random number seed for generating random numbers can be pre-maintained. In this case, random numbers can be generated based on the random seed in this Trusted Execution Environment.
[0109] In the third shown method, the target data parameter that can be used as a random number seed can also be obtained from the data parameters related to the data maintained by the above smart contract for approximate calculation, and then random numbers can be generated in the above smart contract based on the obtained target data parameter. For example, the hash value of the historical block and the generation timestamp of the historical block maintained by the above smart contract, which are unique parameters, can also be used as the random number seed to calculate random numbers in this smart contract.
[0110] In the fourth way shown above, the above-mentioned random number can be generated off-chain. In this case, the above-mentioned smart contract can also obtain the random number generated off-chain through the oracle program corresponding to the smart contract.
[0111] In the fifth way shown above, a random number seed for further generating the above-mentioned random number can be generated off-chain. In this case, the above-mentioned smart contract can also obtain the random number seed generated off-chain through the oracle program corresponding to the smart contract, and then generate a random number in the smart contract based on the obtained random number seed.
[0112] In the sixth way shown above, the above-mentioned random number seed generated off-chain can specifically be carried as a calculation parameter in the above-mentioned smart contract call transaction. In this case, the random number seed generated off-chain included in the smart contract call transaction can be obtained, and then a random number can be generated in the smart contract based on the obtained random number seed.
[0113] The above lists several common implementation ways for obtaining random numbers. It should be emphasized that in practical applications, obviously, ways other than the implementation ways listed above can also be used to obtain random numbers, which will not be listed one by one in this specification.
[0114] It should be emphasized that in the above embodiments, the description is made by taking the example of randomly sampling the data samples in each bucket according to the above optimal sampling quantity. In practical applications, the specific sampling method for sampling the data samples in each bucket according to the above optimal sampling quantity is not limited to random sampling, and other forms of sampling methods other than random sampling can also be used, which will not be listed one by one in this specification.
[0115] Step 106, further call the approximate calculation logic included in the above-mentioned smart contract call transaction, and perform approximate calculation based on the data samples obtained by stratified sampling from the above-mentioned data set to obtain an approximate calculation result for the above-mentioned data set.
[0116] In this specification, when performing approximate calculation on the sampled data samples, the approximate calculation can be performed using the calculation type specified by the calculation initiator, or the default calculation type supported by the above-mentioned smart contract. No special limitation is made in this specification.
[0117] For example, in an implementation way shown, in the above-mentioned smart contract call transaction, a sampling algorithm ID can also be included. The sampling algorithm ID can specifically be used to indicate the calculation type for performing approximate calculation on the above-mentioned data set specified by the calculation initiator.
[0118] In this case, when performing approximate calculations on the sampled data samples, the sampling algorithm ID included in the smart contract call transaction can be obtained, and then approximate calculations can be performed on the collected data samples according to the calculation type indicated by the sampling algorithm ID.
[0119] Of course, if the above smart contract call transaction does not include the above sampling algorithm ID, that is, the calculation initiator does not specify a calculation type for performing approximate calculations on the above data set, approximate calculations can also be performed on the sampled sample data based on the default calculation type supported by the above smart contract.
[0120] It should be noted that the calculation type corresponding to the above approximate calculation is not specifically limited in this specification. For example, it can include summation, average calculation, etc., which will not be listed one by one in this specification.
[0121] In an illustrated embodiment, the smart contract for performing approximate calculations can specifically be a privacy smart contract deployed in a trusted execution environment carried by a blockchain node device.
[0122] In this scenario, the calculation parameters in the above smart contract call transaction and the data samples in the obtained above data set are usually pre-encrypted. Before the blockchain node device calls the sampling logic included in the above smart contract to randomly sample the obtained data set corresponding to the above data identifier, the above calculation parameters and the data samples in the obtained above data set can also be decrypted respectively in the trusted execution environment, and after decryption, the data samples in the above data set can be randomly sampled according to the random sampling method described above. The specific process will not be elaborated.
[0123] For example, in an example, a pair of asymmetric key pairs for encrypting and decrypting data can be assigned to the above trusted execution environment, and the private key of the above asymmetric key can be stored in the above trusted execution environment, and the public key of the above asymmetric key can be published to the above calculation initiator. The calculation parameters in the above smart contract call transaction and the data samples in the obtained above data set can both be pre-encrypted based on the above public key. Before the blockchain node device calls the sampling logic included in the above smart contract to randomly sample the obtained data set corresponding to the above data identifier, the above calculation parameters and the data samples in the obtained above data set can also be decrypted respectively in the trusted execution environment using the maintained private key.
[0124] In the above technical solution, in the scenario of calling a smart contract to perform approximate calculations on a data set, by introducing a hierarchical sampling mechanism for the data set in the smart contract, it is possible to reduce the time consumed for performing approximate calculations on the data set and improve the calculation efficiency when performing approximate calculations on the data set without sacrificing the accuracy of the approximate calculation results.
[0125] For example, still taking the example where the data set related to the business is pre-archived on the blockchain, after introducing the hierarchical sampling mechanism in the smart contract, the total time consumed when the smart contract calculates the data set related to the business can usually be expressed by the following formula:
[0126]
[0127] Among them, in the above formula, n g represents the number of data samples obtained by hierarchical sampling from the data set. N g represents the total number of data samples in the data set. Since the value of n g is usually of an order of magnitude difference compared to the value of N g , after introducing the hierarchical sampling mechanism in the smart contract, the time consumed for calculating the data set through the smart contract will also be reduced by an order of magnitude.
[0128] It can be seen that introducing the hierarchical sampling mechanism in the smart contract can significantly shorten the time consumed for calculating the data set and improve the calculation efficiency when performing approximate calculations on the data set.
[0129] Corresponding to the above method embodiment, the present application also provides an embodiment of a device.
[0130] The embodiment of the device in this specification can be applied to an electronic device. The embodiment of the device can be implemented through software, or through hardware or a combination of software and hardware. Taking software implementation as an example, as a logically meaningful device, it is formed by the processor of the electronic device where it is located reading the corresponding computer program instructions in the non-volatile memory into the memory for operation.
[0131] From the hardware level, as Figure 3 shown, it is a hardware structure diagram of the electronic device where the device in this specification is located. In addition to Figure 3 the shown processor, memory, network interface, and non-volatile memory, the electronic device where the device is located in the embodiment usually includes other hardware according to the actual functions of the electronic device, which will not be elaborated here.
[0132] Figure 4 is a block diagram of a computing device based on a smart contract shown in an exemplary embodiment of this specification.
[0133] Please refer to Figure 4 , the computing device 40 based on the smart contract can be applied in the foregoing Figure 3 shown electronic device, where a smart contract for performing approximate calculation is deployed on the blockchain, and the device 40 includes:
[0134] A receiving module 401 that receives a smart contract call transaction initiated by a calculation initiator for the smart contract; wherein, the smart contract call transaction includes calculation parameters corresponding to the approximate calculation; the calculation parameters include data identifiers of a data set participating in the approximate calculation;
[0135] A sampling module 402 that, in response to the smart contract call transaction, invokes the sampling logic included in the smart contract call transaction to perform stratified sampling on data samples in the data set corresponding to the data identifier;
[0136] A calculation module 403 that further invokes the approximate calculation logic included in the smart contract call transaction to perform approximate calculation based on the data samples obtained by stratified sampling from the data set, so as to obtain an approximate calculation result for the data set.
[0137] In this embodiment, the device 40 further includes:
[0138] An obtaining module 404( Figure 4 not shown in the figure), before the sampling module performs stratified sampling on data samples in the data set corresponding to the data identifier, obtains the data set corresponding to the data identifier stored on the blockchain; or, obtains the data set corresponding to the data identifier from an off-chain database docked with the blockchain through an oracle program corresponding to the smart contract.
[0139] In this embodiment, the calculation parameters include a confidence probability corresponding to the approximate calculation; and a total error value corresponding to the approximate calculation; the confidence probability represents the accuracy of the approximate calculation; the smart contract maintains a mathematical relationship among the confidence probability corresponding to the approximate calculation, the error value corresponding to the approximate calculation, and the sampling quantity corresponding to the data set participating in the approximate calculation, which is derived based on the Hoeffding inequality;
[0140] The sampling module 402:
[0141] Adopts an optimization solution method to solve the optimal number of data subsets to be divided when performing stratified sampling on the data set, and the optimal error value corresponding to each divided data subset;
[0142] Input the confidence probability corresponding to the approximate calculation and the optimal error value corresponding to each data subset into the mathematical relationship for calculation to obtain the optimal sampling quantity corresponding to each data subset respectively;
[0143] Based on the calculated optimal quantity and the optimal sampling quantity, perform stratified sampling on the data samples in the data set corresponding to the data identifier.
[0144] In this embodiment, the constraint conditions adopted by the optimization solution method include: the weighted average error value obtained by performing weighted average calculation on the error values corresponding to each data subset is the smallest and not greater than the total error value;
[0145] The sampling module 402 further performs the following steps:
[0146] Step A, adjust the value corresponding to the initialized i value;
[0147] Step B, divide the data set into several data subsets each containing i sample quantities;
[0148] Step C, input the confidence probability and the sample quantity corresponding to each data subset as calculation parameters into the mathematical relationship for calculation to obtain the error values corresponding to each data subset respectively, and perform weighted average calculation on the error values corresponding to each data subset to obtain the weighted average error value;
[0149] Step D, determine whether the weighted average error value is not greater than the total error value and less than the weighted average error value calculated based on the i value before this adjustment; if not, re - execute the above steps A - D until the calculated weighted average error value satisfies the constraint conditions and stop the iteration, and obtain the optimal i value when the weighted average error value satisfies the constraint conditions;
[0150] Step E, based on the optimal i value, determine the optimal quantity of data subsets required for stratified sampling of the data set, and input the confidence probability and the optimal i value into the mathematical relationship for calculation to obtain the optimal error values corresponding to each data subset respectively.
[0151] In this embodiment, the mathematical relationship is represented by the following formula:
[0152]
[0153] Among them, in the above formula, n g represents the sampling quantity; b g 、ag respectively represent the maximum and minimum values of the data samples in the data set; δ represents the confidence probability; ε g represents the error value; N g represents the total number of data samples in the data set.
[0154] In this embodiment, the sampling module 402 further:
[0155] divide the data set into a plurality of data subsets according to the optimal quantity;
[0156] sample the data samples in each data subset according to the optimal sampling data.
[0157] In this embodiment, the manner of sampling the data samples in each data subset includes random sampling;
[0158] The sampling module 402 further:
[0159] obtain a random number for random sampling;
[0160] randomly sample the data samples in each data subset based on the random number to obtain data samples corresponding to the optimal sampling quantity.
[0161] In this embodiment, the sampling module 402 further executes any one of the following shown:
[0162] invoke a random function deployed on the blockchain to generate a random tree for random sampling;
[0163] generate a random number in the trusted execution environment based on the random number seed maintained in the trusted execution environment carried by the node device;
[0164] obtain a target data parameter as the random number seed from the data-related data parameters maintained by the smart contract, and generate a random number for random sampling in the smart contract based on the obtained target data parameter;
[0165] obtain a random number for random sampling generated off-chain through an oracle program corresponding to the smart contract;
[0166] Obtain a random number seed generated off-chain for generating a random number through an oracle program corresponding to the smart contract, and generate a random number for random sampling in the smart contract based on the obtained target data parameters; obtain the random number seed generated off-chain included in the calculation parameters, and generate a random number for random sampling in the smart contract based on the random number seed. In this embodiment, the calculation parameters further include an algorithm identifier indicating the calculation type corresponding to the approximate calculation; the calculation module 403:
[0167] Perform approximate calculation according to the calculation type indicated by the algorithm identifier based on the data samples obtained by stratified sampling from the data set.
[0168] In this embodiment, the smart contract is deployed in the trusted execution environment carried by the node device; the calculation parameters and the data samples in the data set have been encrypted in advance;
[0169] The sampling module 402 further:
[0170] Before performing stratified sampling on the data samples in the data set corresponding to the data identifier, decrypt the calculation parameters and the obtained data samples in the data set respectively in the trusted execution environment.
[0171] The systems, devices, modules, or units illustrated in the above embodiments may be specifically implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer, and the specific form of the computer may be a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver device, a game console, a tablet computer, a wearable device, or a combination of any several of these devices.
[0172] In a typical configuration, a computer includes one or more processors (CPUs), an input / output interface, a network interface, and a memory.
[0173] The memory may include non-permanent memory in the computer-readable medium, random access memory (RAM), and / or non-volatile memory in the form of, for example, read-only memory (ROM) or flash memory (flash RAM). The memory is an example of a computer-readable medium.
[0174] A computer-readable medium includes both permanent and non-permanent, removable and non-removable media and can implement information storage by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory, or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD), or other optical storage, magnetic cassette tapes, disk storage, quantum memory, graphene-based storage media, or other magnetic storage devices, or any other non-transitory medium that can be used to store information accessible by a computing device. As defined herein, a computer-readable medium does not include transitory computer-readable media such as modulated data signals and carrier waves.
[0175] It should also be noted that the term "comprising," "including," or any other variation thereof is intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but also other elements not expressly listed, or elements that are inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the statement "comprising a..." does not exclude the presence of additional identical elements in the process, method, article, or apparatus that comprises the element.
[0176] The specific embodiments of the present specification have been described above. Other embodiments are within the scope of the appended claims. In some cases, the acts or steps recited in the claims may be performed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0177] The terms used in one or more embodiments of this specification are for the purpose of describing particular embodiments only and are not intended to limit one or more embodiments of this specification. The singular forms "a," "the," and "that" used in one or more embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly dictates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0178] It should be understood that although the terms first, second, third, etc. may be used in one or more embodiments of this specification to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of one or more embodiments of this specification, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to determining".
[0179] The above are only the preferred embodiments of one or more embodiments of this specification, and are not intended to limit one or more embodiments of this specification. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of one or more embodiments of this specification shall be included within the scope protected by one or more embodiments of this specification.
Claims
1. A computing method based on smart contracts, applied to node devices in a blockchain, where a smart contract for performing approximate computing is deployed on the blockchain. The method includes: Receiving a smart contract call transaction initiated by a computing initiator for the smart contract; wherein, the smart contract call transaction includes computing parameters corresponding to the approximate computing; the computing parameters include data identifiers of a data set participating in the approximate computing, a confidence probability corresponding to the approximate computing, and a total error value corresponding to the approximate computing; the confidence probability characterizes the accuracy of the approximate computing; the smart contract maintains a mathematical relationship derived from the Hoeffding inequality, which describes the relationship among the confidence probability corresponding to the approximate computing, the error value corresponding to the approximate computing, and the sampling quantity corresponding to the data set participating in the approximate computing. In response to the smart contract call transaction, invoking the sampling logic included in the smart contract call transaction, using an optimization solution method to solve for the optimal number of data subsets to be divided when performing stratified sampling on the data set, and the optimal error value corresponding to each divided data subset, and inputting the confidence probability corresponding to the approximate computing and the optimal error value corresponding to each data subset into the mathematical relationship for calculation to obtain the optimal sampling quantity corresponding to each data subset respectively, so as to perform stratified sampling on the data samples in the data set corresponding to the data identifier based on the calculated optimal number and the optimal sampling quantity. Further invoking the approximate computing logic included in the smart contract call transaction to perform approximate computing based on the data samples obtained by stratified sampling from the data set, so as to obtain an approximate computing result for the data set.
2. The method according to claim 1, before performing stratified sampling on the data samples in the data set corresponding to the data identifier, further including: Obtaining the data set corresponding to the data identifier stored on the blockchain; Or, Obtaining the data set corresponding to the data identifier from an off-chain database docked with the blockchain through an oracle program corresponding to the smart contract.
3. The method according to claim 1, wherein The constraint conditions adopted by the optimization solution method include: the weighted average error value obtained by performing weighted average calculation on the error values corresponding to each data subset is the smallest and does not exceed the total error value. Using an optimization solution method to solve for the optimal number of data subsets to be divided when performing stratified sampling on the data set, and the optimal error value corresponding to each divided data subset, including: Step A, adjusting the value corresponding to the initialized i value; Step B, dividing the data set into several data subsets each containing i sample quantities; Step C: Use the confidence probability and the adjusted value of i as calculation parameters, input them into the mathematical relationship for calculation, obtain the error values corresponding to each data subset respectively, and perform a weighted average calculation on the error values corresponding to each data subset to obtain a weighted average error value; Step D: Determine whether the weighted average error value is not greater than the total error value and less than the weighted average error value calculated based on the value of i before this adjustment; if not, re-execute the above Steps A - D until the calculated weighted average error value meets the constraint condition, then stop the iteration and obtain the optimal value of i when the weighted average error value meets the constraint condition; Step E: Based on the optimal value of i, determine the optimal number of data subsets required for stratified sampling of the data set, and input the confidence probability and the optimal value of i into the mathematical relationship for calculation to obtain the optimal error values corresponding to each data subset respectively.
4. The method according to claim 3, wherein the mathematical relationship is represented by the following formula: Among them, In the above formula, n g represents the number of samplings; b g , a g respectively represent the maximum value and the minimum value of the data samples in the data set; δ represents the confidence probability; ε g represents the error value; N g represents the total number of data samples in the data set.
5. The method according to claim 1, wherein the stratified sampling of the data samples in the data set corresponding to the data identifier includes: Dividing the data set into several data subsets according to the optimal number; Sampling the data samples in each data subset according to the optimal sampling number respectively.
6. The method according to claim 5, wherein the way of sampling the data samples in each data subset respectively includes random sampling; Sampling the data samples in each data subset according to the optimal sampling number respectively includes: Obtaining a random number for random sampling; Based on the random number, randomly sampling the data samples in each data subset respectively to obtain data samples corresponding to the optimal sampling number.
7. The method according to claim 6, wherein the obtaining of the random number for random sampling includes any of the following: Invoking the random function deployed on the blockchain to generate a random number for random sampling; Generating a random number in the trusted execution environment based on the random number seed maintained in the trusted execution environment carried by the node device; Obtaining a target data parameter serving as the random number seed from the data-related data parameters maintained by the smart contract, and generating a random number for random sampling in the smart contract based on the obtained target data parameter; Obtaining a random number for random sampling generated off-chain through the oracle program corresponding to the smart contract; Obtaining a random number seed for generating a random number generated off-chain through the oracle program corresponding to the smart contract, and generating a random number for random sampling in the smart contract based on the obtained target data parameter; Obtaining the random number seed generated off-chain included in the calculation parameters, and generating a random number for random sampling in the smart contract based on the random number seed.
8. The method according to claim 1, wherein the calculation parameter further includes an algorithm identifier indicating the calculation type corresponding to the approximate calculation; Performing approximate calculation based on data samples obtained by stratified sampling from the data set includes: Performing approximate calculation based on the data samples obtained by stratified sampling from the data set according to the calculation type indicated by the algorithm identifier.
9. The method according to claim 1, wherein the smart contract is deployed in a trusted execution environment carried by the node device; the calculation parameter and the data samples in the data set have been encrypted in advance; Before performing stratified sampling on the data samples in the data set corresponding to the data identifier, it further includes: Decrypting the calculation parameter and the data samples in the obtained data set respectively in the trusted execution environment.
10. A computing device based on a smart contract, applied to a node device in a blockchain, where a smart contract for performing approximate calculation is deployed on the blockchain, and the device includes: A receiving module, which receives a smart contract call transaction for the smart contract initiated by a calculation initiator; wherein, the smart contract call transaction includes calculation parameters corresponding to the approximate calculation; the calculation parameters include a data identifier of a data set participating in the approximate calculation, a confidence probability corresponding to the approximate calculation, and a total error value corresponding to the approximate calculation; the confidence probability characterizes the accuracy of the approximate calculation; the smart contract maintains a mathematical relationship derived based on the Hoeffding inequality, which describes the relationship among the confidence probability corresponding to the approximate calculation, the error value corresponding to the approximate calculation, and the sampling quantity corresponding to the data set participating in the approximate calculation; A sampling module, in response to the smart contract call transaction, calls the sampling logic included in the smart contract call transaction, uses an optimization solution method to solve for the optimal quantity of data subsets to be divided when performing stratified sampling on the data set, and the optimal error value corresponding to each divided data subset, and inputs the confidence probability corresponding to the approximate calculation and the optimal error value corresponding to each data subset into the mathematical relationship for calculation to obtain the optimal sampling quantity corresponding to each data subset respectively, so as to perform stratified sampling on the data samples in the data set corresponding to the data identifier based on the calculated optimal quantity and the optimal sampling quantity; A calculation module, further calls the approximate calculation logic included in the smart contract call transaction, and performs approximate calculation based on the data samples obtained by stratified sampling from the data set to obtain an approximate calculation result for the data set.
11. An electronic device, including: A processor; A memory for storing instructions executable by the processor; Wherein, the processor realizes the steps of the method according to any one of claims 1-9 by running the executable instructions.
12. A computer-readable storage medium having computer instructions stored thereon, which when executed by a processor, implement the steps of the method according to any one of claims 1-9.
Citation Information
Patent Citations
Block chain technology-based shipping index calculation method
CN108734524A
Product sampling detection method and device based on block chain, and medium
CN111008849A