Blockchain off-chain execution method and electronic device with state presetting capability
By presetting and updating the blockchain world state and simulating different execution environments, the problem of insufficient flexibility and accuracy of transaction execution results prediction in the existing technology is solved, and more flexible and accurate transaction execution results prediction is achieved.
Patent Information
- Application Number
- CN202411359755.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-09-27
AI Technical Summary
When predicting transaction execution results, the prior art is limited by a fixed block environment and cannot meet the needs of different transaction scenarios, resulting in insufficient prediction flexibility and accuracy.
By obtaining the current world status of the blockchain, obtaining the corresponding modification information of the account, updating the status information, obtaining the simulated world status, and executing the target transaction based on this status, so as to achieve flexible and accurate prediction of the transaction execution results.
It improves the flexibility and accuracy of forecasting transaction execution results, meets the needs of different transaction scenarios, provides different execution environments, and is suitable for different application scenarios.
Smart Images

Figure CN118916428B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a blockchain off-chain execution method and electronic device with state presetting capability. Background Art
[0002] Currently, when predicting the on-chain execution results of historical transactions or transactions that have just entered the broadcast but have not yet been put on the chain, it is possible to predict the transaction execution results by running a full blockchain node with a complete world state and running a virtual machine separately outside the consensus algorithm to execute the transaction.
[0003] However, when predicting the results of transaction execution, transactions can only be executed based on the world state of a certain fixed block in the future as the environment; due to the limitations of the fixed block environment, the prediction of transaction execution results has certain limitations and cannot meet the needs of different transaction scenarios. Summary of the invention
[0004] According to various embodiments of the present application, a blockchain off-chain execution method and electronic device with state presetting capability are provided, which can improve the flexibility and accuracy of predicting transaction execution results and meet the needs of different transaction scenarios.
[0005] In a first aspect, the present application provides a blockchain off-chain execution method with state presetting capability, the method comprising:
[0006] Get the current world state of the blockchain, which includes the status information of the account; get the modification information corresponding to the account, and based on the modification information, update the status information to get the simulated world state of the blockchain; based on the simulated world state, execute the target transaction to get the prediction result under the simulated world state.
[0007] Through the above method, the world state of the blockchain can be modified based on the needs of different actual application scenarios, and the world state can be preset before the transaction execution prediction is performed; and based on the updated simulated world state, the target transaction is executed to obtain the prediction results under different world states, thereby improving the flexibility and accuracy of the prediction of transaction execution results; based on the modification of the world state, different world states are simulated and different execution environments are provided, which can be suitable for the needs of different application scenarios; it has strong ease of use and practicality.
[0008] In a possible implementation manner of the first aspect, obtaining modification information corresponding to an account includes:
[0009] Traverse the address information of the account and the state variables corresponding to the address information; extract the variable name and change content of the state variable to obtain the modification information.
[0010] In a possible implementation of the first aspect, based on the modification information, the state information is updated to obtain the simulated world state of the blockchain, including: based on the address information, determining the target smart contract to be modified in the world state; based on the variable name and the changed content, updating the state information of the target smart contract to obtain the simulated world state of the blockchain.
[0011] In a possible implementation manner of the first aspect, address information and state variables are stored in a key-value pair structure, and variable names and change contents are stored in a key-value pair structure.
[0012] In a possible implementation manner of the first aspect, updating the status information based on the modification information includes:
[0013] Based on the variable name and the changed content of the state variable, the content of at least one state variable in the state information is updated.
[0014] In a possible implementation of the first aspect, executing a target transaction based on a simulated world state to obtain a prediction result in the simulated world state includes:
[0015] Based on the simulated world state, the future transaction execution method is adopted to execute the target transaction off-chain to obtain the predicted results under the simulated world state.
[0016] In a possible implementation of the first aspect, executing a target transaction based on a simulated world state to obtain a prediction result in the simulated world state includes:
[0017] Based on the simulated world state, the contract call operation is executed to obtain the prediction result under the simulated world state; wherein, the process of executing the target transaction includes executing the contract call operation.
[0018] In a second aspect, the present application provides a blockchain off-chain execution device with state presetting capability, including:
[0019] An acquisition unit, used to acquire the current world state of the blockchain, where the world state includes state information of all accounts;
[0020] A processing unit, configured to obtain modification information corresponding to the account, and based on the modification information, update the state information to obtain a simulated world state of the blockchain;
[0021] An execution unit is used to execute a target transaction based on the simulated world state to obtain a prediction result under the simulated world state.
[0022] In a third aspect, the present application provides an electronic device, comprising at least one processor and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executed by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute any one of the methods described in the first aspect.
[0023] In a fourth aspect, the present application provides a chip system, which is applied to an electronic device, and the chip system includes one or more processors, and the one or more processors are used to call computer instructions so that the electronic device executes the method described in the first aspect.
[0024] In a fifth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method described in the first aspect is implemented.
[0025] In a sixth aspect, the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device executes the method described in the first aspect above.
[0026] It can be understood that the beneficial effects of the second to sixth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0028] Figure 1 A schematic diagram of an application scenario architecture provided for an embodiment of the present application;
[0029] Figure 2 A flowchart of a blockchain off-chain execution method with state presetting capability provided in an embodiment of the present application;
[0030] Figure 3 A schematic diagram of a data storage architecture provided in an embodiment of the present application;
[0031] Figure 4 A schematic diagram of the structure of a blockchain off-chain execution device with state presetting capability provided in an embodiment of the present application;
[0032] Figure 5 A structural schematic diagram of an electronic device is provided for an embodiment of the present application. DETAILED DESCRIPTION
[0033] The following embodiments of the technical solution of the present application are described in detail in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present application, and are therefore only used as examples, and cannot be used to limit the scope of protection of the present application.
[0034] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by technicians in the technical field to which this application belongs; the terms used herein are only for the purpose of describing specific embodiments and are not intended to limit this application; the terms "including" and "having" in the specification and claims of this application and the above-mentioned figure descriptions and any variations thereof are intended to cover non-exclusive inclusions.
[0035] In the description of the embodiments of the present application, the technical terms "first", "second", etc. are only used to distinguish different objects, and cannot be understood as indicating or implying relative importance or implicitly indicating the number, specific order or primary and secondary relationship of the indicated technical features. In the description of the embodiments of the present application, the meaning of "multiple" is more than two, unless otherwise clearly and specifically defined.
[0036] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0037] In the description of the embodiments of the present application, the term "and / or" is only a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship.
[0038] In blockchain technology, since the latest status of all accounts on the blockchain is based on the cumulative results of all previous transactions, the status data cannot be modified before executing a new transaction. When predicting the on-chain execution results of transactions that have not yet been put on the chain, the transaction is executed by running a full blockchain node with a data set of the latest status of the blockchain and running a virtual machine separately outside the consensus algorithm.
[0039] However, when predicting the results of transaction execution, it may be necessary to consider a variety of different contextual situations, such as different account balances, different smart contract states, etc. Due to the immutability of blockchain state data and the characteristics of distributed ledgers, transactions can only be executed based on the world state of a certain future block, which makes it difficult to predict the results of transaction execution.
[0040] In response to the above-mentioned problems, the embodiments of the present application provide a blockchain off-chain execution method with state presetting capability, which can preset the world state (i.e., the state data of the blockchain) before executing the blockchain transaction off-chain, thereby obtaining accurate predictions of the execution results of the blockchain transaction in multiple scenarios and meeting the needs of different application scenarios. In particular, when developing and testing smart contracts on the blockchain, the behavior of the smart contract under different state data can be predicted and verified.
[0041] Before introducing the blockchain off-chain execution method with state presetting capability provided by this application, a brief introduction to the technical terms related to this technical solution is first given to facilitate the subsequent understanding of the solution implementation process.
[0042] 1. The world state is a collection of all accounts and their related data in the Ethereum blockchain. It is a tree structure, usually stored in the form of a Merkle Tree. The world state contains information such as the balance of each account, storage (state variables of the smart contract), code (bytecode of the smart contract), etc. It has the characteristic of dynamic change, that is, the world state will change with the execution of transactions. Each block contains a snapshot of the world state to represent the state of the blockchain when the block is packaged. Among them, Ethereum uses the Merkle Patricia Trie to efficiently store and verify the data of the world state to ensure the integrity and consistency of the data.
[0043] 2. A full node in the blockchain refers to a node that can save the entire blockchain data and participate in the network consensus. It can download and verify all blocks and transactions to ensure the security and decentralization of the network. A full node stores the historical data of the entire blockchain, including all transactions and status changes, to ensure data integrity; a full node has a verification function and will verify the validity of each new block and transaction to ensure that they comply with the consensus rules. A full node can broadcast transactions or participate in consensus (under the proof of stake mechanism). By running a full node, users can independently verify the status of the blockchain without relying on third-party services.
[0044] The following is an introduction to the implementation process of the blockchain off-chain execution method with state presetting capability provided by this application through an embodiment.
[0045] like Figure 1 As shown, the application scenario architecture diagram provided by the embodiment of the present application, the blockchain network includes each node participating in the consensus, such as node 1, node 2 and node M, where M is an integer greater than or equal to 1. Each node stores each block data on the chain, such as block 1, block 2 and block N, where N is an integer greater than or equal to 1; each block data constitutes the world state of the blockchain.
[0046] Among them, node 1 can be a full node, which has a complete world state, that is, a collection of the latest state data in the blockchain network. Based on the above architecture, the embodiment of the present application modifies the world state of the blockchain based on the modification data configured in the node by running node 1, and realizes accurate prediction of the execution results of blockchain transactions in various contextual scenarios.
[0047] like Figure 1 As shown in the figure, the off-chain execution process of the blockchain with state presetting capability is the process of the node executing the transaction off-chain based on the modified world state (i.e. the world state pre-configured before the transaction is executed); based on different execution environments, the prediction of transaction execution results in different application scenarios is realized. By modifying the world state, simulating different world states, and realizing state presetting before transaction execution, the flexibility, accuracy and diversity of the prediction of transaction execution results are improved.
[0048] The following further introduces the implementation process of the blockchain off-chain execution method with state presetting capability through an embodiment. Figure 2 As shown, a flowchart of a blockchain off-chain execution method with state presetting capability provided in an embodiment of the present application is provided. The execution subject of the method may be Figure 1 The blockchain node 1 shown in , the method may include the following steps:
[0049] S201, obtain the current world state of the blockchain, which includes the status information of all accounts.
[0050] In some embodiments, the blockchain node can read the world state of the blockchain network participating in the consensus locally to determine the current world state of the blockchain network.
[0051] Among them, the blockchain network is based on the state database (stateDB), which manages and stores the status information of all accounts (or addresses); the accounts of the blockchain network include ordinary accounts and contract accounts, and each contract account stores the status information of a smart contract. The status information of ordinary accounts can include account balance, transaction count (nonce) and other information, and the status information of contract accounts can include contract balance, transaction count (nonce), contract code, storage value and other information generated during the contract execution process. The state database is a snapshot or summary of the above state information at a certain moment, which constitutes the world state of the blockchain network.
[0052] Exemplarily, the functional components of the state database (stateDB) include account model, transaction counting unit (nonce), statistical balance unit (balance), storage root unit (storage Root), contract code recording unit (codeHash), state and storage access interface and snapshot mechanism. Among them, the blockchain network is based on the account model, recording and maintaining the state information of each account. Each account can be an externally owned account (EOA), that is, a normal account, or a contract account, that is, a smart contract.
[0053] Exemplarily, the transaction counting unit (nonce) is used by the nodes of the blockchain network to increase the count each time a transaction is sent to prevent transaction replay. Taking the Ethereum blockchain as an example, the statistical balance unit (balance) is used to count the balance of Ethereum. The storage root unit (storage Root) points to the root of the Merkle Patricia tree of the storage structure and is used to store the state information of the smart contract. The contract code recording unit (code Hash) is used to record the hash value of the contract account. The state and storage access interface is used to access and modify the storage of the account; the storage is a collection of key-value pairs, and the key and value can be a 256-bit hash value. During the execution process, the smart contract can maintain the state through storage, that is, through the state information stored under the contract address (i.e., the contract account), the data in the contract account that changes with the execution of the transaction is saved. The above-mentioned state database (stateDB) supports snapshots and rollbacks of state information. Through the snapshot mechanism, the state information can be temporarily modified during the execution of transactions or the invocation of smart contracts, and the modification of the state information can be revoked according to needs.
[0054] S202, obtaining the modification information corresponding to the account, and based on the modification information, updating the status information to obtain the simulated world status of the blockchain.
[0055] In some embodiments, the nodes in the blockchain network manage and store the world state based on the state database stateDB; based on the state database, after obtaining the current world state of the blockchain network, the modification information stored based on the state difference data structure can be read; for example, the state difference data structure can be a stateDifference data structure, which is used to represent the difference between the state database (stateDB) before and after a specific operation or transaction, and can store the modification information set by the user based on actual needs.
[0056] Exemplarily, the modification information can be obtained by calling a state difference function (such as calculateStateDifference function), and the modification information can be used to update the world state in the state database. The update process can be performed in a local environment or a specific test network, without affecting the actual world state in the blockchain consensus network and the consistency of the blockchain ledger. For example, the update of the state information can be a temporary modification operation off-chain to perform off-chain transaction prediction. After the modification, the simulated world state of the blockchain network is obtained, not the actual world state of the blockchain network.
[0057] Exemplarily, the process of updating status information may include adding or subtracting the numerical values corresponding to each status variable in the status information (such as increasing or decreasing the balance), modifying the contract code, or changing the data structure (such as updating the mapping table stored in the contract). The specific content that needs to be updated can be set based on the actual needs of the user.
[0058] In some embodiments, obtaining modification information corresponding to an account includes: traversing address information of the account and state variables corresponding to the address information; extracting variable names and change contents of the state variables to obtain modification information.
[0059] Exemplarily, the state difference (stateDifference) data structure is added to the configuration parameters of the off-chain transaction execution (such as the trace call parameter TraceCallArgs) to indicate the modification required to the world state before the off-chain transaction execution. Among them, the trace call parameter TraceCallArgs represents the configuration parameters when executing the transaction off-chain.
[0060] For example, the above configuration parameters may include the initiator of the transaction (From field, i.e. the address that initiated the transaction, the contract can perform permission check based on this address when executing), the target address (To field, i.e. the address of the target contract, indicating which contract function to call), resource limit (gas field, i.e. the optional resource gas limit, used to specify the maximum amount of gas that can be used for this call), transaction value (GasPrice field, i.e. the optional gas price, which affects the priority of the transaction), amount (Value field, i.e. the optional ETH transfer amount, indicating the amount of ETH that can be transferred when calling the contract), data (Data field, i.e. the encoded function call data, including function selectors and parameters, which can be generated by the method library used to interact with the blockchain (Web3.js) etc.), and the tracing strategy during execution (Tracer field, which specifies the tracer to be used, allowing users to select different tracing strategies to obtain different types of execution information).
[0061] Among them, in the state difference (stateDifference) data structure, each account can correspond to an address information, for example, the address information can include an account address or a contract address (common.Address); each address information corresponds to a state variable that needs to be modified, and the state variable includes a variable name and the change content corresponding to the variable name.
[0062] Correspondingly, the contract address is the unique identifier of the smart contract on the blockchain. For example, a smart contract is written in a high-level language, converted into bytecode by a compiler, and deployed on the blockchain to create a new contract account. The address of this account is the contract address. The contract address can be a unique string generated by a hash function. The account address is the unique identifier of a user account on the blockchain, which is used to store the user's ether and tokens, and to call smart contracts to initiate transactions; for example, when a user creates an external account on the blockchain, a unique account address is generated. The user can use this account address to receive ether or tokens sent by others, and can also use this address to initiate transactions, call smart contracts, etc.
[0063] Exemplarily, when a node updates status information off-chain, it parses the parameters of the state difference (stateDifference) data structure in the above configuration parameters to parse the status information that needs to be modified from the parameters configured by the user; that is, by traversing the address information of the account in the state difference (stateDifference) data structure, determining the state variables that need to be modified, extracting the variable names contained in the state variables and the corresponding changes, and obtaining specific modification information; for example, the account balance, transaction count, storage value, etc.
[0064] Among them, the address information and state variables are stored in a key-value pair structure, and the variable name and change content are stored in a key-value pair structure.
[0065] Exemplarily, the stateDifference data structure is a mapping structure, which represents the change of the world state through a modification set stored in the form of key-value pairs kv; the key of the mapping structure is the address information, and the value is the specific data of the world state change, that is, the state variable; wherein the value contains another internal mapping, that is, the key of the internal mapping is the name or identifier of the state variable, and the value is the corresponding change content (which can be any type, such as a JSON serializable data type).
[0066] like Figure 3 As shown, the address information of the account to be modified and the state variable corresponding to the address information are stored in the form of a key-value pair, that is, the address information is the key and the state variable is the value. Accordingly, the state variable may include multiple variable names, each variable name corresponds to the change content to be modified; wherein each variable name and the corresponding change name are stored in the form of another key-value pair, that is, the variable name is the key and the change content is the value.
[0067] In some embodiments, based on the modification information, the state information is updated to obtain the simulated world state of the blockchain, including: based on the address information, determining the target smart contract to be modified in the world state; based on the variable name and the changed content, updating the state information of the target smart contract to obtain the simulated world state of the blockchain.
[0068] Exemplarily, the modification information stored based on the stateDifference data structure includes a key-value mapping pair of address information and state variables; wherein the address information is the unique identifier of the user account or contract account, so that the target smart contract (i.e., the target contract account) to be modified in the current world state can be determined based on the address information; based on the extracted variable name, the state variable to be modified in the target smart contract (i.e., the target object to be modified, such as the contract code) is determined, and based on the extracted change content, the value of the state variable in the target smart contract is updated. For example, if the variable name of the state variable is contract balance, the value of the contract balance of the target smart contract in the current world state is updated based on the value of the corresponding change content.
[0069] For example, the node calls the stateDifference data structure to check whether a state difference is provided; if so, it traverses each address information and the corresponding state variable (for address, changes), extracts the corresponding variable name and change content (for key, value) for each state variable; by calling the current state modification function currentState.SetState(address, key, value), the state information of the current world state is updated based on the SetState method, that is, the state of the target smart contract in the current execution context is modified.
[0070] In some embodiments, the state information includes modification information of one or more state variables; updating the state information based on the modification information includes: updating the content of at least one state variable in the state information based on the variable name and changed content of the state variable.
[0071] Exemplarily, the modification information stored based on the stateDifference data structure can be modification information corresponding to a state variable or a collection of modification information of multiple state variables; accordingly, the collection of modification information of multiple state variables can be modification information of multiple state variables corresponding to one address information (such as one contract account), or modification information of state variables corresponding to multiple address information (such as multiple contract accounts). Thus, by traversing each state variable corresponding to each address information, before predicting the transaction execution result, the modification information is obtained by parsing the stateDifference data structure, and the state information of the current world state is modified based on the modification information.
[0072] S203, based on the simulated world state, executing the target transaction to obtain the prediction result under the simulated world state.
[0073] In some embodiments, the target transaction can be a transaction whose execution result the user, developer or system wants to predict. For example, the target transaction can be a new transaction that has just entered the broadcast but has not yet been actually executed on the block; or, it can be a historical transaction that already exists on the blockchain, but the user wants to know its execution results in different world states. In addition, the target transaction can also be a call to a function or method defined in the contract, that is, the user or system predicts the result of calling this function or method in the simulated world state, such as the change of state variables, the content of the return value, and other transactions or events that may be triggered.
[0074] Exemplarily, by adding the state difference (stateDifference) data structure to the configuration parameters of the off-chain transaction execution (such as the trace call parameter TraceCallArgs), the state information of the current world state is modified before the transaction is executed to obtain the simulated world state. Based on the simulated world state, according to the selected target transaction or contract function, the configuration parameters that need to be input are determined, such as the address of the initiating transaction, the address of the target contract, etc.; so that the above parameters are called when executing transactions or functions in the simulated world state.
[0075] For example, the node simulates the execution of transactions or contract functions based on the modified simulated world state by using off-chain simulation tools or libraries; wherein the transaction execution process is only carried out off-chain, and the transaction execution results are not actually written into the blockchain ledger. Therefore, the consistency and immutability of the blockchain ledger will not be affected.
[0076] Accordingly, after the transaction is executed, the expected transaction result or the return value of the contract function call will be returned as the prediction result for subsequent analysis of the impact of the transaction or contract function. For example, users can adjust the contract logic and investment strategy based on the prediction results.
[0077] In some embodiments, based on the simulated world state, executing the target transaction to obtain the prediction result under the simulated world state includes:
[0078] Based on the simulated world state, the future transaction execution method is adopted to execute the target transaction off-chain to obtain the predicted results under the simulated world state.
[0079] Exemplarily, the future transaction execution method (trace_call) is a calling function for executing the target transaction off-chain, which allows developers to simulate transaction execution without actually sending the transaction execution result, and obtain detailed tracking information of all events and states that occurred during the transaction execution process, so as to obtain the predicted result of the transaction execution by executing the transaction in the simulated world state, so as to facilitate further debugging, auditing, etc. of the smart contract based on the predicted result.
[0080] Among them, the implementation of future transaction execution methods (trace_call) is concentrated in the Remote Procedure Call (RPC) package and the tracer (tracers) package; the RPC package is responsible for processing requests received from clients (such as JSON-RPC clients), such as parsing request parameters (such as To, Data, Value, etc.), and forwarding the parsed request parameters to the processing function. The tracers package provides the function of tracing the transaction execution process on the blockchain. When the RPC package forwards the trace_call request parameters to the processing function, the processing function uses the tracer in the tracers package to execute the target transaction and collect detailed tracking information.
[0081] For example, without changing the state of the blockchain, the transaction code is executed and all related state changes and events are recorded; the tracker collects information during the transaction execution process, converts the information into a response format (such as JSON format), and returns it to the client through the RPC interface to obtain the prediction result of the target transaction in the simulated world state.
[0082] In some embodiments, based on the simulated world state, executing the target transaction to obtain the prediction result under the simulated world state includes:
[0083] Based on the simulated world state, the contract call operation is executed to obtain the prediction result under the simulated world state; wherein, the process of executing the target transaction includes executing the contract call operation.
[0084] For example, contract call refers to triggering a function or method in a smart contract during the execution of a transaction in order to execute preset logic and rules. For example, by sending a transaction request (such as the name of the function to be called, parameters, etc.) to the contract address of the smart contract, calling the function in the smart contract, executing the bytecode of the smart contract, and simulating the execution process of the contract function.
[0085] For example, by calling the executeCall(ctx, args, currentState, block) function, the operation of the contract call is executed (such as result, err := s.executeCall(ctx, args, currentState, block)), and after applying the modification information of the state difference data structure, the contract call is executed and the prediction result is returned.
[0086] Among them, the object of type ctxcontext.Context can be used to control the execution time of the smart contract call, or cancel the execution when necessary; the parameter args (Arguments) contains the data structure of the parameters required when calling the smart contract. These parameters are the values passed to it when calling the contract function or method. For example, they can be simple data types (such as integers, strings) or more complex structures (such as arrays, mappings, etc.); currentState represents the blockchain state when the contract call is executed, that is, the modified simulation world state; block is used to represent the metadata or information of the block currently being processed, such as the block height, timestamp, etc.
[0087] In some embodiments, the snapshot mechanism of the state database stateDB supports state snapshot and rollback. During the process of executing a transaction or calling a smart contract, the world state can be modified. After completing the prediction of the transaction execution results, the modification to the world state can also be undone, and the modified simulated world state can be restored to the original world state.
[0088] Through the embodiments of the present application, based on the state difference stateDifference data structure, developers can simulate and test different world states and configuration parameters without permanently changing the on-chain state. When developing and testing smart contracts, the behavior of smart contracts in different world states can be predicted and verified. Based on the state database stateDB, the necessary state operation interface is provided to ensure the consistency and security of the blockchain world state. The world state of the execution environment can be temporarily modified according to all requirements for off-chain execution of blockchain transactions, and the prediction of transaction execution can be completed on this basis to meet the needs of different transaction scenarios.
[0089] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0090] Corresponding to the blockchain off-chain execution method with state presetting capability provided in the above embodiment, Figure 4 As shown, an embodiment of the present application provides a blockchain off-chain execution device with state presetting capability. For ease of explanation, only the parts related to the embodiment of the present application are shown.
[0091] The device includes:
[0092] An acquisition unit 41 is used to acquire the current world state of the blockchain, where the world state includes state information of all accounts;
[0093] The processing unit 42 is used to obtain the modification information corresponding to the account, and based on the modification information, update the state information to obtain the simulated world state of the blockchain;
[0094] The execution unit 43 is used to execute the target transaction based on the simulated world state to obtain the prediction result under the simulated world state.
[0095] In a possible implementation, the acquisition unit 41 is further used to traverse the address information of the account and the state variables corresponding to the address information; extract the variable name and change content of the state variable to obtain the modification information.
[0096] In one possible implementation, the processing unit 42 is used to determine the target smart contract to be modified in the world state based on the address information; based on the variable name and the changed content, update the state information of the target smart contract to obtain the simulated world state of the blockchain.
[0097] In a possible implementation, address information and state variables are stored in a key-value pair structure, and variable names and change contents are stored in a key-value pair structure.
[0098] In a possible implementation, the processing unit 42 is further configured to update the content of at least one state variable in the state information based on the variable name and the changed content of the state variable.
[0099] In a possible implementation, the execution unit 43 is also used to execute the target transaction off-chain based on the simulated world state and adopt a future transaction execution method to obtain a predicted result under the simulated world state.
[0100] In a possible implementation, the execution unit 43 is also used to execute the contract call operation based on the simulated world state to obtain the prediction result under the simulated world state; wherein, the process of executing the target transaction includes executing the contract call operation.
[0101] Through the embodiments of the present application, the world state of the blockchain can be modified based on the needs of different actual application scenarios, and based on the updated simulated world state, the target transaction can be executed, and the prediction results under different world states can be obtained, thereby improving the flexibility and accuracy of the prediction of transaction execution results; based on the modification of the world state, different world states are simulated and different execution environments are provided, which can be suitable for the needs of different application scenarios.
[0102] Figure 5 A schematic diagram of the hardware structure of the electronic device 5 is shown.
[0103] like Figure 5 As shown, the electronic device 5 of this embodiment includes: at least one processor 50 ( Figure 5Only one is shown in the figure), a memory 51, in which a computer program 52 that can be run on the processor 50 is stored. When the processor 50 executes the computer program 52, the steps in the above method embodiment are implemented, such as Figure 2 Alternatively, when the processor 50 executes the computer program 52, the functions of the modules / units in the above-mentioned device embodiments are realized.
[0104] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 5. In other embodiments of the present application, the electronic device 5 may include more or fewer components than shown in the figure, or combine certain components, or separate certain components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0105] The electronic device 5 may include, but is not limited to, a processor 50 and a memory 51. Those skilled in the art will appreciate that Figure 5 It is only an example of the electronic device 5 and does not constitute a limitation of the electronic device 5. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the electronic device may also include an input sending device, a network access device, a bus, etc.
[0106] The processor 50 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0107] The processor 50 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 50 is a cache memory. The memory may store instructions or data that the processor 50 has just used or cyclically used. If the processor 50 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 50, and thus improves the efficiency of the system.
[0108] In some embodiments, the memory 51 may be an internal storage unit of the electronic device 5, such as a hard disk or memory of the electronic device 5. The memory 51 may also be an external storage device of the electronic device 5, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 5. Further, the memory 51 may include both an internal storage unit and an external storage device of the electronic device 5. The memory 51 is used to store an operating system, an application program, a boot loader (BootLoader), data, and other programs, such as program codes of a computer program. The memory 51 may also be used to temporarily store data that has been sent or is to be sent.
[0109] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0110] It should be noted that the structure of the above-mentioned electronic device is only illustrative, and based on different application scenarios, it may also include other physical structures, and the physical structure of the electronic device is not limited here.
[0111] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0112] An embodiment of the present application also provides a chip system, which is applied to an electronic device. The chip system includes one or more processors, and the one or more processors are used to call computer instructions so that the electronic device implements the steps in the above-mentioned method embodiments.
[0113] An embodiment of the present application further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.
[0114] An embodiment of the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device can implement the steps in the above-mentioned method embodiments when executing the computer program product.
[0115] If the integrated module / 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 this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. Computer-readable media may include: any entity or device capable of carrying computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal and software distribution medium, etc.
[0116] The devices, electronic devices, chip systems, computer storage media, and computer program products provided in the above-mentioned embodiments of the present application are all used to execute the methods provided above. Therefore, the beneficial effects that can be achieved can refer to the corresponding beneficial effects of the methods provided above, and will not be repeated here.
[0117] It should be understood that the above is only to help those skilled in the art to better understand the embodiments of the present application, rather than to limit the scope of the embodiments of the present application. According to the above examples given, those skilled in the art can obviously make various equivalent modifications or changes. For example, some steps in each embodiment of the above detection method may be unnecessary, or some steps may be newly added. Or a combination of any two or any multiple embodiments of the above. Such modifications, changes or combined schemes also fall within the scope of the embodiments of the present application.
[0118] It should also be understood that the division of the methods, situations, categories and embodiments in the embodiments of the present application is only for the convenience of description and should not constitute a special limitation. The features of various methods, categories, situations and embodiments can be combined without contradiction.
[0119] It should also be understood that in the various embodiments of the present application, unless otherwise specified or there is a logical conflict, the terms and / or descriptions between different embodiments are consistent and can be referenced to each other, and the technical features in different embodiments can be combined to form new embodiments according to their internal logical relationships.
[0120] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0121] In the embodiments provided in the present application, it should be understood that the disclosed devices / network equipment and methods can be implemented in other ways. For example, the device / network equipment embodiments described above are merely schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as 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 mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0122] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0123] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
[0124] Finally, it should be noted that the above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A blockchain off-chain execution method with state presetting capability, characterized in that: The method comprises: Obtain the current world state of the blockchain, which includes the status information of all accounts; Obtaining modification information corresponding to the account, and updating the state information based on the modification information to obtain a simulated world state of the blockchain; Based on the simulated world state, executing a target transaction to obtain a prediction result under the simulated world state; Among them, obtaining the modification information corresponding to the account includes: reading the modification information stored based on the state difference data structure; the state difference data structure is used to store the modification information set by the user; modifying the world state of the blockchain based on the needs of different actual application scenarios, and presetting the world state before performing transaction execution prediction.
2. The method according to claim 1, characterized in that: The obtaining modification information corresponding to the account includes: Traversing the address information of the account and the state variables corresponding to the address information; The variable name and the change content of the state variable are extracted to obtain the modification information.
3. The method according to claim 2, characterized in that The updating of the state information based on the modification information to obtain the simulated world state of the blockchain includes: Based on the address information, determining a target smart contract to be modified in the world state; Based on the variable name and the changed content, the state information of the target smart contract is updated to obtain the simulated world state of the blockchain.
4. The method according to claim 2, characterized in that: The address information and the state variable are stored in a key-value pair structure, and the variable name and the change content are stored in a key-value pair structure.
5. The method according to claim 2, characterized in that: The updating of the state information based on the modification information includes: Based on the variable name and the changed content of the state variable, the content of at least one state variable in the state information is updated.
6. The method according to claim 1, characterized in that The step of executing a target transaction based on the simulated world state to obtain a prediction result under the simulated world state includes: Based on the simulated world state, the future transaction execution method is adopted to execute the target transaction off-chain to obtain the prediction result under the simulated world state.
7. The method according to any one of claims 1 to 6, characterized in that: The step of executing a target transaction based on the simulated world state to obtain a prediction result under the simulated world state includes: Based on the simulated world state, executing the contract call operation to obtain the prediction result under the simulated world state; The process of executing the target transaction includes the operation of executing the contract call.
8. An electronic device, characterized in that: The electronic device includes at least one processor and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executed by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the method described in any one of claims 1 to 7.
9. A chip system, characterized in that: The chip system is applied to an electronic device, and the chip system includes one or more processors, and the one or more processors are used to call computer instructions so that the electronic device executes the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Read-write set generation method and device, storage medium and electronic equipment
CN114610812A