Method for executing smart contract in block chain system and block chain node

By introducing dedicated loading functions and position variables into smart contracts, it solves the problem that smart contract developers can find it difficult to intuitively feel the operation of reading state variables from the data storage system, and achieves the effect of optimizing the execution efficiency of smart contracts.

CN119903103APending Publication Date: 2025-04-29ANT BLOCKCHAIN TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411970389.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-27
Publication Date
2025-04-29

AI Technical Summary

Technical Problem

In the blockchain system, it is difficult for smart contract developers to intuitively feel the operation of reading state variables from the data storage system, and it is easy to ignore the nested relationship between state variables, resulting in high cost of execution of smart contracts.

Method used

Provides a method to obtain the value of the state variable by introducing a dedicated load function and position variable in a smart contract, and call the load function at the storage location in the data storage system according to the value of the state variable, thereby obtaining the value of the state variable. This method is suitable for the case where the storage type of state variables belongs to aggregate storage.

Benefits of technology

Through this method, developers can intuitively feel the operation of obtaining the value of state variables from the data storage system, avoid unnecessary reading, optimize the source code of the smart contract, and thus improve the execution efficiency of the smart contract.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119903103A_ABST
    Figure CN119903103A_ABST
Patent Text Reader

Abstract

A method for executing a smart contract in a block chain system and a block chain node, the smart contract comprising a load function and a first position variable corresponding to a first state variable, the first position variable being used for representing a storage position of a value of the first state variable in a data storage system. A first storage type of a first state variable in a data storage system belongs to aggregation storage, the method is realized by executing a contract code of an intelligent contract, and the method comprises the following steps: calling a loading function according to a storage position of a value of the first state variable in the data storage system; and in response to calling of the loading function, executing the loading function, and obtaining the value of the first state variable from the data storage system according to the storage position of the value of the first state variable in the data storage system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification belong to the field of computer technology, and more particularly to a method for executing a smart contract in a blockchain system and a blockchain node. Background Art

[0002] The blockchain system is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithm. In the blockchain system, data blocks are combined into a chain data structure in a sequential manner according to time order, and a distributed ledger that cannot be tampered with or forged is guaranteed by cryptography. Due to the characteristics of decentralization, information cannot be tampered with, and autonomy, the blockchain system has received more and more attention and application.

[0003] Smart contracts in blockchain systems usually include multiple state variables, and the values ​​of state variables are usually stored in the data storage system in the form of key-value pairs. Some state variables in smart contracts may have nested relationships. In addition, in the process of executing smart contracts, it is often necessary to read the values ​​of state variables from the data storage system to complete the implementation of related transactions.

[0004] Accessing variables in a data storage system is relatively time-consuming compared to directly accessing variables in the memory of a computing device. When developers write the source code of a smart contract, it is difficult to intuitively feel the operation of reading state variables from a data storage system from a grammatical perspective, and it is also easy to ignore the nested relationship between state variables. This can easily lead developers to develop smart contracts with relatively high execution costs. For example, during the execution of the developed execution contract, it is necessary to obtain the value of a state variable that is not needed in the subsequent execution process from the data storage system, and to obtain the value of the same state variable from the data storage system multiple times in a row to implement multiple transactions using the value of the state variable, etc. Summary of the invention

[0005] The object of the present invention is to provide a method for executing a smart contract in a blockchain system and a blockchain node.

[0006] In a first aspect, a method for executing a smart contract in a blockchain system is provided, wherein the smart contract includes a loading function and a first position variable corresponding to a first state variable, the first position variable is used to represent the storage position of the value of the first state variable in the data storage system, and the first storage type of the first state variable in the data storage system belongs to aggregate storage. The method is implemented by executing the contract code of the smart contract, and the method includes: calling the loading function according to the storage position of the value of the first state variable in the data storage system; in response to the call to the loading function, executing the loading function to obtain the value of the first state variable from the data storage system according to the storage position of the value of the first state variable in the data storage system.

[0007] In a possible implementation, the contract code is compiled from source code, and the source code defines a first storage type of the first state variable in the data storage system; the first storage type of the first state variable in the data storage system is determined based on a first data type of the first state variable in the memory.

[0008] In a possible implementation, the first data type includes a basic type; or, the first data type includes a first composite type, and the source code also includes annotation information corresponding to the first state variable, and the annotation information is used to indicate that the first storage type of the first state variable in the data storage system belongs to aggregate storage.

[0009] In a possible implementation, the first complex type includes array, map, vector or struct.

[0010] In a possible implementation manner, the first position variable is defined in the source code, the variable name of the first position variable is the variable name of the first state variable, and the type of the first position variable is the first storage type.

[0011] In a possible implementation, the method further includes: assigning the value of the first state variable to a target temporary variable; and executing a target transaction using the value of the target temporary variable.

[0012] In a possible implementation, the smart contract also includes a storage function; the method also includes: calling the storage function according to the value of the first state variable and the storage location of the value of the first state variable in the data storage system; in response to the call to the storage function, executing the storage function to store the value of the first state variable into the data storage system according to the storage location of the value of the first state variable in the data storage system.

[0013] In a possible implementation, the smart contract also includes a second position variable corresponding to the second state variable, the second position variable is used to represent the storage position of the value of the second state variable in the data storage system, and the second storage type of the second state variable in the data storage system belongs to separate storage; the second state variable includes at least one first member variable, and the first state variable belongs to the at least one first member variable; wherein the method further includes: obtaining the storage position of the value of the second state variable in the data storage system, including the storage position of the value of the first state variable in the data storage system.

[0014] In a possible implementation, the contract code is compiled from source code, the second position variable is defined in the source code, the variable name of the second position variable is the variable name of the second state variable, and the type of the second position variable is the second storage type.

[0015] In a possible implementation, the smart contract also includes a third position variable corresponding to a third state variable, the second position variable is used to represent the storage position of the value of the second state variable in the data storage system, the third state variable includes at least one second member variable, the storage type of the third state variable in the data storage system belongs to separate storage, and the second state variable belongs to the at least one second member variable; wherein, the method also includes: obtaining the storage position of the value of the third state variable in the data storage system, including the storage position of the value of the second state variable in the data storage system.

[0016] In a second aspect, a blockchain node in a blockchain system is provided, wherein a smart contract is deployed in the blockchain system, wherein the smart contract includes a loading function and a first position variable corresponding to a first state variable, wherein the first position variable is used to represent the storage position of the value of the first state variable in the data storage system, and the first storage type of the first state variable in the data storage system belongs to aggregate storage, and the blockchain node is used to execute the contract code of the smart contract to implement: calling the loading function according to the storage position of the value of the first state variable in the data storage system; and executing the loading function in response to the call to the loading function to obtain the value of the first state variable from the data storage system according to the storage position of the value of the first state variable in the data storage system.

[0017] In a third aspect, a computer-readable storage medium is provided, on which a computer program / instruction is stored. When the computer program / instruction is executed in a computing device, the computing device executes the method described in the first aspect.

[0018] In the technical solution provided by the embodiment of this specification, a dedicated loading function and a location variable for representing the storage location of the value of the state variable in the data storage system are provided in the smart contract. When it is necessary to obtain a certain state variable value from the data storage system, and the storage type of the state variable in the data storage system belongs to aggregate storage, the loading function can be called according to the storage location of the value of the state variable in the data storage system, and the loading function is used to obtain the value of the state variable from the data storage system according to the storage location of the value of the state variable in the data storage system. In this way, by setting a loading function for obtaining the value of the state variable from the data storage system, and the input parameter of the loading function needs to be the storage location of the value of the state variable whose storage type belongs to aggregate storage in the data storage system: on the one hand, it can avoid the unnecessary reading of the value of certain state variables during the execution of the smart contract; on the other hand, in the process of developing the smart contract, the developer can observe the call of the loading function and intuitively feel the operation of obtaining the value of the state variable whose storage type belongs to aggregate storage from the data storage system, which is conducive to the developer to optimize the source code of the smart contract, so as to improve the execution efficiency of the smart contract. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0020] Figure 1 This is an architectural diagram of a blockchain system exemplarily provided in the embodiments of this specification;

[0021] Figure 2 This is a flowchart of a method for executing a smart contract in a blockchain system provided in an embodiment of this specification. DETAILED DESCRIPTION

[0022] In order to enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this specification.

[0023] Figure 1 This is an architectural diagram of a blockchain system provided exemplarily in the embodiments of this specification. The blockchain system may include N blockchain nodes, where Figure 1 8 blockchain nodes, i.e., node 1 to node 8, are shown as examples. The lines between the nodes schematically represent the connection between the nodes, and the aforementioned connection is used to support data transmission between different nodes.

[0024] The blockchain system can provide the function of smart contracts. Smart contracts in the blockchain system are contracts that can be triggered and executed by transactions. Smart contracts can be defined in the form of contract code. Calling a smart contract in the blockchain system is to initiate a transaction pointing to the contract address of the smart contract, so that each node in the blockchain system can run the corresponding contract code in a distributed manner.

[0025] In various blockchain systems that introduce smart contracts, accounts can generally be divided into two types:

[0026] Contract account (CA): mainly used to store the contract code of the corresponding smart contract and the value of the state variable in the smart contract. It can usually only be activated by calling an external account.

[0027] Externally owned account (EOA): An account registered by an external user in the blockchain system.

[0028] The design of external accounts and contract accounts is actually a mapping from account addresses to account states. The account state of any account usually includes fields such as nonce, balance, storageRoot, code, and / or codeHash. Nonce and balance exist in both external accounts and contract accounts, while code, codeHash, and storageRoot attributes are generally only valid on contract accounts.

[0029] The contract account is used to store the contract status of the smart contract. After the smart contract is deployed in the blockchain system, a corresponding contract account will be generated. This contract account generally has some states, which are defined by the state variables in the smart contract and generate new values ​​when the smart contract is created and executed. The smart contract generally refers to a contract that can automatically execute clauses in a digital form in a blockchain environment. When an event triggers the clauses in the contract (meets the execution conditions), the code can be automatically executed. The contract status of the smart contract is saved in the storage trie, and the hash value of the storagetrie root node is stored in the above storageroot, so that all the contract states of the smart contract are locked to the contract account through hash. The storage trie can be a tree structure that stores the key-value mapping from the state address to the state value. The key of a state variable is stored from the root node of the storage trie tree to a leaf node, and the leaf node can store the value of the state variable, or the storage location of the value of the state variable in the data storage system. In specific technical scenarios, the values ​​of state variables are usually stored in the data storage system in the form of key-value pairs. The key of any state variable can be determined based on the declaration position of the state variable in the contract code during the compilation or execution of the smart contract, or based on the variable name of the state variable and / or several preceding state variables in the smart contract that have a nested relationship with the state variable.

[0030] The data type of the state variable in the smart contract, that is, the data type of the state variable in the memory, can usually be divided into two types: basic type and composite type. Basic types include, but are not limited to, int, string, and bool; composite types include, but are not limited to, array, map, vector, enum, struct, and asset. When the data type of a state variable in memory is a composite type, it means that the state variable may include / nest multiple member variables that are also state variables, and the data types of any two member variables in memory may be the same or different.

[0031] In the process of executing smart contracts, it is often necessary to obtain the value of state variables from the data storage system to complete the execution of related transactions. Among them, obtaining the value of state variables in the data storage system has relatively large computational overhead and time overhead compared to directly reading the value of state variables in the memory of the computing device. However, in the process of writing the source code of smart contracts, developers usually find it difficult to intuitively feel the operation of obtaining the value of state variables in the data storage system from a grammatical perspective; at the same time, it is easy to ignore the nested relationship between state variables. In this way, it is very easy for developers to develop smart contracts with relatively high execution costs. For example, during the execution of the developed smart contract, it may be necessary to obtain the value of state variables that are not needed in the subsequent process from the data storage system, and obtain the value of the same state variable from the data storage system for multiple consecutive times to implement multiple transactions using the value of the state variable.

[0032] For example, please refer to the pseudo code R11 of the smart contract as follows:

[0033]

[0034] In the pseudocode R11 of the above example, each use of the state variable state_str1 requires obtaining the value of state_str1 from the data storage system. However, the above pseudocode R11 can actually be optimized so that after obtaining the value of state_str1 from the data storage system for the first time, the value of state_str1 can be stored in the memory of the computing device as the value of a temporary variable; and each subsequent acquisition of the value of state_str1 from the data storage system can actually be optimized to directly read the value of the temporary state variable from the memory of the computing device. However, for the pseudocode R11 of the above example, it is difficult for developers to intuitively feel the operation of obtaining the value of state_str1 from the data storage system from a grammatical perspective, and the optimization of the above example is not easy to implement.

[0035] For example, refer to the pseudo code R21 of the smart contract in the following example:

[0036]

[0037] In the pseudocode R21 of the above example, there are multiple state variables in a nested relationship, such as a state variable at the first level with a data type of map, a state variable at the second level with a data type of Person, and state variables name, age, and tokens at the third level with data types of str, int32, and [int32], respectively. In the process of executing the pseudocode R21 of the above example, although only the value of the member variable age in a state variable with the key name and the data type Person needs to be used, the value of the entire state variable with the key name and the data type Person needs to be read first; that is, the values ​​of the member variables name and tokens in the state variable with the key name and the data type Person are read unnecessarily, but the values ​​of the member variables name and tokens are not needed in the subsequent execution of the smart contract, which will obviously have a negative impact on the execution efficiency of the smart contract.

[0038] In view of this, at least one method and blockchain node for executing a smart contract in a blockchain system is provided in the embodiments of this specification. A dedicated loading function and a location variable for characterizing the storage location of the value of a state variable in a data storage system are provided in the smart contract. When a state variable value needs to be obtained from the data storage system, and the storage type of the state variable in the data storage system belongs to aggregate storage, the loading function can be called according to the storage location of the value of the state variable in the data storage system, and the value of the state variable is obtained from the data storage system according to the storage location of the value of the state variable in the data storage system through the loading function. In this way, by setting a loading function for obtaining the value of the state variable from the data storage system, and the input parameter of the loading function needs to be the storage location of the value of the state variable whose storage type belongs to aggregate storage in the data storage system: on the one hand, it can avoid unnecessary reading of the values ​​of certain state variables during the execution of the smart contract; on the other hand, in the process of developing smart contracts, developers can observe the call of the loading function and intuitively feel the operation of obtaining the value of the state variable whose storage type belongs to aggregate storage from the data storage system, which is beneficial for developers to optimize the source code of the smart contract, so as to improve the execution efficiency of the smart contract.

[0039] The storage type of state variables, that is, the storage type of state variables in the data storage system, can be divided into two types: aggregate storage and separate storage. If the storage type of a state variable belongs to separate storage, it means that the value of the state variable is stored in multiple storage locations in the data storage system in a dispersed manner; correspondingly, if the storage type of a state variable belongs to aggregate storage, it means that the value of the state variable is stored as a whole in a storage location in the data storage system.

[0040] There is a mapping relationship between the storage type of the state variable in the data storage system and the data type of the state variable in the memory. For example, a storage type storage T corresponding to data type T can be defined in advance for any data type T; that is, for any state variable with data type T in the smart contract, the storage type storage T of the state variable in the data storage system is determined according to the data type T of the state variable in the memory.

[0041] For state variables whose data types belong to basic types, the storage type of the state variable in the data storage system usually belongs to aggregate storage. For state variables whose data types belong to composite types, if the storage type of the state variable is expected to belong to aggregate storage, annotation information corresponding to the state variable can be set in the source code to indicate that the storage type of the state variable belongs to aggregate storage; if the storage type of the state variable is expected to belong to separate storage, there is no need to set annotation information corresponding to the state variable in the source code, and the storage type of the state variable belongs to separate storage by default.

[0042] For state variables whose data types are composite types, it is possible to estimate the amount of data corresponding to the encoding result obtained when a specific encoding algorithm is used to encode the values ​​of multiple member variables included in the state variable. If the estimated amount of data is not greater than a predetermined value, such as not greater than 32 bytes, annotation information can be set for the state variable in the source code to indicate that the storage type of the state variable in the data storage system belongs to aggregate storage. On the contrary, if the estimated amount of data is greater than a predetermined value, such as greater than 32 bytes, there is no need to set the aforementioned annotation information for the state variable in the source code; in this case, it means that the storage type of the state variable in the data storage system belongs to separate storage.

[0043] For a state variable whose data type is a structure (struct), the annotation information of the state variable may be a structure annotation set for the structure corresponding to the state variable; for a state variable whose data type is an array (array), a vector (vector) or other composite type, the annotation information of the state variable may be a variable annotation set for the state variable. More specifically, the structure annotation may be a predetermined mark such as "@pack" set before the declaration position of the structure; similarly, the variable annotation may be a predetermined mark such as "@pack" set before the declaration position of the relevant state variable.

[0044] For state variables whose data types are composite types, if the aforementioned annotation information is set for the state variable in the source code, then during the compilation of the source code, or during the execution of the smart contract, it is no longer necessary to allocate state addresses to the member variables included in the state variable; in other words, it is no longer necessary to allocate keys for storing the values ​​of the multiple member variables in the corresponding data storage system to the multiple member variables included in the state variable.

[0045] Based on the mapping relationship between data type T and storage type storage T introduced above, when the developer needs to define a state variable X in the smart contract and the data type of the state variable X is T, the developer may not directly define the state variable X with data type T. For example, the state variable X may not be defined by the statement "X: T" in the source code, but the state variable X with storage type storage T may be defined. For example, the statement "X: storage T" may be used to define the position variable corresponding to the state variable X. Different from the related art, the "X: storage T" defined in the source code is used as the position variable corresponding to the state variable "X: T", which represents the storage position of the state variable X in the data storage system, and is not used to specifically represent the value of the state variable "X: T".

[0046] For example, refer to the pseudo code R31 of the smart contract in the following example:

[0047] / / ***R31*** / /

[0048] contract demo{

[0049] @pack

[0050] struct Person{

[0051] name:str

[0052] friends:[str]

[0053] }

[0054] a:storage Person

[0055] }.

[0056] / / ***R31*** / /

[0057] In the pseudo code R13 of the above example, a structure named Person is defined through struct Person, and the structure annotation "@pack" indicates that the storage type of the state variable corresponding to struct Person belongs to aggregate storage, that is, it indicates that the storage type storage Person of the state variable a of the data type Person in the data storage system belongs to aggregate storage. At the same time, it should be noted that the position variable "a: storage Person" is used to represent the storage location of the value of the state variable a of the data type Person in the data storage system, and is not used to directly represent the value of the state variable a of the data type Person.

[0058] Based on the pseudocode R31 of the above example, it is assumed here that the structure annotation "@pack" is not set for struct Person in pseudocode R13, which means that the storage type storagePerson of the state variable a in the data storage system belongs to separate storage. In order to facilitate developers to intuitively distinguish the storage type of state variables whose data types belong to composite types in the data storage system, whether they belong to aggregate storage or separate storage, when the storage type of the state variable belongs to aggregate storage, the storage type of the state variable can also be marked by a predetermined mark such as "packed". For example, in the pseudocode R31 of the above example, if there is no structure annotation "@pack" set for struct Person, the position variable corresponding to the state variable a is defined as "a:storage Person"; if there is a structure annotation "@pack" set for struct Person, the position variable "a:storage Person" is modified to "a:packed storagePerson".

[0059] Depending on how the variables are defined in the source code of the smart contract, the input parameter of the dedicated load function needs to be the storage location of the value of the state variable in the data storage system. For example, in the load function "load(storage_value:storage T)", storage_value:storage T represents the storage location of the value of the state variable storage_value of data type T in the data storage system, and the return value of the load function is the value of the state variable storage_value of data type T.

[0060] When a state variable y1 is a member variable of another state variable y2, and the storage type of the other state variable y2 in the data storage system belongs to aggregate storage, since the data storage system does not allocate a separate storage location for the value of the state variable y1, there is no need to define the storage type of the state variable y1 in the data storage system in the source code of the smart contract.

[0061] The dedicated loading function can be pre-configured in the relevant software development kit (SDK) provided by the service provider; the source code of the smart contract can directly call the loading function in the SDK, and compile the loading function into the contract code of the smart contract during the compilation stage. Alternatively, the dedicated loading function can also be a private function independently developed by the developer in the process of developing the source code of the smart contract and allowed to be called in the smart contract.

[0062] By setting a dedicated loading function, developers can intuitively view the call location of the loading function in the source code when writing the source code of the smart contract. Therefore, they can intuitively feel from a grammatical perspective that each operation in the source code obtains the value of the state variable from the data storage system. This helps developers optimize the source code of the smart contract and develop smart contracts with relatively low execution costs. For example, it can avoid the smart contract from obtaining the value of the same state variable from the data storage system multiple times in a row during the execution process to implement multiple transactions using the value of the state variable.

[0063] For example, based on the above-described loading function and the definition of the storage type of the state variable, the function to be implemented by the pseudo code R11 of the above example can be correspondingly rewritten into the following pseudo code R12:

[0064] / / ***R12*** / /

[0065] contract demo{

[0066] / / Describes the state variable state_str1 of data type string, where state_str1:storagestring represents the storage location of the value of state_str1 in the data storage system

[0067] state_str1: storage string;

[0068] function fromHex(data:string)->bytes{

[0069] return bytes(data); / / Here the hexadecimal string conversion of data is omitted

[0070] }

[0071] event TestEvent(string data);

[0072] function func1()->string{

[0073] / / Read the value of state_str1 as a hexadecimal string decoded into bytes; by observing the call to the load() function, it can be found that the value of the state variable state_str1 is read from the data storage system

[0074] let data1:bytes=fromHex(state_str1.load());

[0075] / / Read the value of state_str1 as the parameter of the TestEvent event; by observing the call to the load() function, we can find that the value of the state variable state_str1 is read from the data storage system

[0076] emit TestEvent(state_str1.load());

[0077] / / Read the value of state_str1 and concatenate it with the value of state_str1 and return it; by observing the call to the load() function, we can find that the value of the state variable state_str1 is read twice from the data storage system.

[0078] return string.concat(state_str1.load(), state_str1.load());

[0079] }

[0080] }.

[0081] / / ***R12*** / /

[0082] In the pseudo code R12 of the above example, it is very easy to observe multiple calls to the load function load(), and further observe that the multiple calls to the load function load() are all for obtaining the value of state_str1 from the data storage system, which prompts the developer to optimize the pseudo code R12 of the above example and obtain the pseudo code R13 of the following example:

[0083] / / ***R13*** / /

[0084] contract demo{

[0085] / / Describes the state variable state_str1 of data type string, where state_str1:storagestring represents the storage location of the value of state_str1 in the data storage system

[0086] state_str1: storage string;

[0087] function fromHex(data:string)->bytes{

[0088] return bytes(data); / / Here the hexadecimal string conversion of data is omitted

[0089] }

[0090] event TestEvent(string data);

[0091] function func1()->string{

[0092]

[0093] In the pseudocode R13 of the above example, compared with the pseudocode R11 used in the related art, the number of times the value of the state variable state_str1 is obtained from the data storage system is reduced from 4 times to 1 time, which is conducive to improving the execution efficiency of the smart contract.

[0094] The dedicated loading function requires that the storage type of the input parameter belongs to aggregate storage. Therefore, when the storage type of a state variable belongs to separate storage, it is impossible to obtain the values ​​of multiple member variables included in the state variable from the data storage system by calling the loading function once. In this way, it is possible to avoid the situation where the value of any state variable whose storage type belongs to separate storage is completely obtained from the data storage system by calling the loading function once, which is helpful to avoid the unnecessary acquisition of the value of state variables that are not needed from the data storage system during the execution of smart contracts.

[0095] For example, based on the loading function described above and the definition of the storage type of the state variable, the function to be implemented by the pseudo code R21 of the above example can be correspondingly rewritten into the pseudo code R22 of the following example:

[0096]

[0097]

[0098] In the pseudo code R22 of the smart contract in the above example, the value of the state variable with the key name and the data type Person is not obtained from the data storage system. Only the value of the member variable age included in the state variable is obtained. That is, there is no need to read the values ​​of the member variables name and tokens in the state variable with the key name corresponding to struct Person. In fact, only the value of age with the size of 4 bytes int32 is read, and decoding the read data into int32 will also be faster.

[0099] Similar to the dedicated loading function, a dedicated storage function can also be set. The storage function takes the value of the state variable of the storage type belonging to the aggregate storage and the storage location of the value of the state variable in the data storage system as input parameters, and stores the value of the state variable in the data storage system according to the storage location of the value of the state variable in the data storage system.

[0100] For state variables whose data types are composite types, the smart contract may need to perform certain specific operations on the state variables. For example, for state variables whose data types are arrays, a new element can be inserted into the state variable through the PUSH operation, and an element can be taken out of the state variable through the POP operation; for state variables whose data types are tables, a new data object can be added to the state variable through the SET operation, and a data object can be deleted through the DELETE operation.

[0101] If the storage type of such state variables in the data storage system is separated storage, the process of executing these operations will involve access to the data storage system. For example, it may be necessary to obtain the old length of the array from the data storage system, store the new length of the array into the data storage system, store the newly inserted elements in the array into the data storage system, etc. These accesses can be achieved by calling dedicated loading functions and storage functions to complete the execution of specific operations.

[0102] Based on the variable definition method, dedicated loading function and dedicated storage function of the aforementioned example, the developer completes the development of the source code of the smart contract, and after compiling the source code into a contract code that can be executed in the blockchain system through the corresponding compiler, the smart contract can be deployed in the blockchain system using the contract code. It should be noted that the source code of the smart contract uses an unconventional definition method to define variables. In order to ensure that the source code can be compiled correctly, it is also necessary to configure a dedicated adjustment compiler so that the configured compiler can correctly identify and process the state variables defined using the storage type. Exemplarily, the position variable "state_str1: storage string" contained in the pseudocode R12 of the aforementioned example is actually used to represent the storage location of the value of the state variable with a data type of string in the data storage system, and is not used to represent the value of the state variable with a data type of string. Conventional compilers may not be able to process "state_str1: storage string".

[0103] The foregoing exemplarily describes the process of obtaining the contract code of the smart contract provided in the embodiments of this specification. The following exemplarily describes the process of executing the smart contract in the blockchain system based on the contract code of the smart contract.

[0104] Figure 2 This is a flowchart of a method for executing a smart contract in a blockchain system provided in an embodiment of this specification.

[0105] The smart contract includes a dedicated loading function and a first position variable corresponding to a first state variable, and the first storage type of the first state variable in the data storage system belongs to aggregate storage; the method exemplarily describes a process of obtaining the value of the first state variable from the data storage system and using the value of the first state variable to execute one or more target transactions; the method can be implemented by executing the contract code of the smart contract through the blockchain nodes included in the blockchain system.

[0106] Reference Figure 2 As shown, the method may include but is not limited to part or all of the following steps S201 to S209.

[0107] Referring to the description of data types and storage types in the previous text, when the first storage type of the first state variable belongs to aggregate storage, the first state variable may be a member variable of other state variables, or may not be a member variable of other state variables. When the first state variable is a member variable of a second state variable in a smart contract, the second state variable may contain at least one first member variable, and the first state variable belongs to the at least one first member variable. Since the storage type of the first state variable in the data storage system is the first storage type, it means that the storage type of the second state variable in the data storage system is separated, otherwise the first state variable will not have a corresponding storage type.

[0108] Similarly, the second state variable may be a member variable of other state variables, or may not be a member variable of other state variables. When the second state variable is a member variable of a third state variable included in the smart contract, the third state variable may contain at least one second member variable, the second state variable belongs to the at least one second member variable, and the storage type of the third state variable in the data storage system belongs to separate storage.

[0109] Combined with the nested relationship between the state variables analyzed above, when the first state variable to be used is a member variable of a second state variable, it may be necessary to execute the following step S201 to obtain the storage location of the value of the second state variable in the data storage system, the second storage type of the second state variable in the data storage system belongs to separate storage, the second state variable includes at least one first member variable, the first state variable to be used belongs to the at least one first member variable, and the first storage type of the first state variable in the data storage system belongs to aggregate storage.

[0110] Exemplarily, referring to the pseudocode R22 of the smart contract in the above example, in pseudocode R22: a structure named Person is defined through structPerson, that is, a data type Person is defined, and the state variables of the data type Person include member variables name, age, and tokens of data types str, int32, and [int32], respectively; in addition, a state variable map_demo1 of the data type table is defined, and the state variable map_demo1 includes one or more member variables of the data type Person. It is assumed here that the first state variable to be used is the member variable age in the state variable with the key name and the data type Person, then the state variable with the key name and the data type Peson is the second state variable, and the state variable map_demo1 of the data type table is the third state variable. By executing the instruction sequence corresponding to the statements "let person:storage Person=map_demo1[name]" and "return getAgeFromPerson(person)" in the smart contract in sequence, the storage location of the state variable with the key name and the data type Peson in the data storage system can be obtained. The value of "person:storagePerson" defined in pseudo code R22 is the storage location of the value of the second state variable in the data storage system. Since the data type of the second state variable is the structure Person, the value of "person:storage Person" actually includes the storage location of each of the first member variables name, age, and tokens included in the second state variable in the data storage system. Therefore, the storage location of the value of the member variable age in the state variable with the key name and the data type Person in the data storage system can be obtained from the value of "person:storage Person".

[0111] The aforementioned step S201 is exemplary. For example, when the first state variable to be used is not a member variable of other state variables, the first state variable of the first storage type, that is, the first position variable corresponding to the first state variable, is used to represent the storage position of the first state variable in the data storage system; the storage position of the first state variable in the data storage system can be obtained by accessing the value of the first position variable. For example, please continue to refer to the pseudo code R13 of the smart contract in the aforementioned example. The first state variable can be the state variable state_str1. The smart contract defines the corresponding position variable "state_str1: storage string" to represent the storage position of the value of state_str1 in the data storage system. By accessing the value of "state_str1: storage string", the storage position of the value of state_str1 in the data storage system can be obtained.

[0112] After obtaining the storage location of the value of the first state variable to be used in the data storage system, the following step S203 may be executed to call a loading function according to the storage location of the value of the first state variable in the data storage system.

[0113] For the dedicated load function, the input parameter needs to be the storage location of the value of the state variable in the data storage system. For example, referring to the pseudo code R22 of the above example, in the statement return person.age.load(), the input parameter of the load function load() needs to be the storage location of the value of the member variable age in the state variable with the key name and the data type Peson in the data storage system. For example, referring to the pseudo code R13 of the above example, in the statement let str1: string = state_str1.load(), the input parameter of the load function load() needs to be the storage location of the value of state_str1 in the data storage system, that is, the input parameter of the dedicated load function load() needs to be the value of the location variable state_str1: storage string.

[0114] Step S205 , in response to calling the loading function, executing the loading function to obtain the value of the first state variable from the data storage system according to the storage location of the value of the first state variable in the data storage system.

[0115] Step S207, assigning the value of the first state variable to the target temporary variable.

[0116] For example, please continue to refer to the pseudocode R13 of the aforementioned example. By executing the instruction sequence corresponding to the statement letstr1: string = state_str1.load() in the smart contract, the aforementioned steps S203 to S207 can be implemented; finally, the value of the state variable state_str1: string is assigned to the target temporary variable str1: string.

[0117] Step S209, executing the target transaction using the value of the target temporary variable.

[0118] Exemplarily, referring to the pseudocode R13 of the previous example, after assigning the value of state_str1 to the target temporary variable str1: string, you can execute transactions such as decoding the value of str1: string stored in the memory as a hexadecimal string into bytes, using the value of str1: string stored in the memory as the input parameter of the TestEvent event, and concatenating the value of str1: string and returning it.

[0119] When it is necessary to store the value of the first state variable in the storage system, after obtaining the storage location of the value of the first state variable in the data storage system, a dedicated storage function can be called according to the value of the first state variable and the storage location of the value of the first state variable in the data storage system; then, in response to the call to the storage function, the storage function is executed to store the value of the first state variable in the data storage system according to the storage location of the value of the first state variable in the data storage system.

[0120] Based on the same concept as the aforementioned method embodiment, the embodiment of this specification also provides a blockchain node in a blockchain system, wherein a smart contract is deployed in the blockchain system, and the smart contract includes a loading function and a first position variable corresponding to a first state variable, wherein the first position variable is used to represent the storage position of the value of the first state variable in the data storage system, and the first storage type of the first state variable in the data storage system belongs to aggregate storage, and the blockchain node is used to execute the contract code of the smart contract to implement: calling the loading function according to the storage position of the value of the first state variable in the data storage system; and executing the loading function in response to the call to the loading function to obtain the value of the first state variable from the data storage system according to the storage position of the value of the first state variable in the data storage system.

[0121] A computer-readable storage medium is also provided in an embodiment of the present specification, on which a computer program / instruction is stored. When the computer program / instruction is executed in a computer, the computer executes the method for executing a smart contract in a blockchain system provided in the aforementioned embodiments.

[0122] A computing device is also provided in an embodiment of the present specification, including a memory and a processor, wherein the memory stores a computer program / instruction, and when the processor executes the computer program / instruction, the method for executing a smart contract in a blockchain system provided in the aforementioned embodiments is implemented.

[0123] In the 1990s, improvements to a technology could be clearly distinguished as hardware improvements (for example, improvements to the circuit structure of diodes, transistors, switches, etc.) or software improvements (improvements to the method flow). However, with the development of technology, many improvements to the method flow today can be regarded as direct improvements to the hardware circuit structure. Designers almost always obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement in a method flow cannot be implemented using a hardware entity module. For example, a programmable logic device (PLD) (such as a field programmable gate array (FPGA)) is such an integrated circuit whose logical function is determined by the user's programming of the device. Designers can "integrate" a digital system on a PLD by programming it themselves, without having to ask a chip manufacturer to design and produce a dedicated integrated circuit chip. Moreover, nowadays, instead of manually making integrated circuit chips, this kind of programming is mostly implemented by "logic compiler" software, which is similar to the software compiler used when developing and writing programs, and the original code before compilation must also be written in a specific programming language, which is called hardware description language (HDL). There is not only one HDL, but many kinds, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. The most commonly used ones are VHDL (Very-High-Speed ​​Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also know that it is only necessary to program the method flow slightly in the above-mentioned hardware description languages ​​and program it into the integrated circuit, and then it is easy to obtain the hardware circuit that implements the logic method flow.

[0124] The controller can be implemented in any appropriate manner, for example, the controller can take the form of a microprocessor or processor and a computer-readable medium storing a computer-readable program code (such as software or firmware) that can be executed by the (micro)processor, a logic gate, a switch, an application-specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320. The memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art also know that in addition to implementing the controller in a purely computer-readable program code manner, the controller can be implemented in the form of a logic gate, a switch, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, this controller can be considered as a hardware component, and the devices included therein for implementing various functions can also be regarded as structures within the hardware component. Or even, the devices for implementing various functions can be regarded as both software modules for implementing the method and structures within the hardware component.

[0125] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a server system. Of course, the present application does not exclude that with the development of computer technology in the future, the computer that implements the functions of the above embodiments may be, for example, a personal computer, a laptop computer, a vehicle-mounted human-computer interaction device, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.

[0126] Although one or more embodiments of the present specification provide method operation steps as described in the embodiments or flow charts, more or less operation steps may be included based on conventional or non-creative means. The order of steps listed in the embodiments is only one way of executing the order of many steps, and does not represent the only execution order. When the device or terminal product in practice is executed, it can be executed in sequence or in parallel according to the method shown in the embodiments or the drawings (for example, a parallel processor or a multi-threaded processing environment, or even a distributed data processing environment). The term "include", "include" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, product or equipment including a series of elements includes not only those elements, but also includes other elements that are not explicitly listed, or also includes elements inherent to such a process, method, product or equipment. In the absence of more restrictions, it is not excluded that there are other identical or equivalent elements in the process, method, product or equipment including the elements. For example, if the words first, second, etc. are used to represent the name, they do not represent any specific order.

[0127] For the convenience of description, the above devices are described in various modules according to their functions. Of course, when implementing one or more of the present specification, the functions of each module can be implemented in the same or more software and / or hardware, or the module implementing the same function can be implemented by a combination of multiple sub-modules or sub-units, etc. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the 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.

[0128] The present invention is described with reference to flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0129] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0130] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0131] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0132] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0133] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. 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 technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage, graphene storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0134] It should be understood by those skilled in the art that one or more embodiments of the present specification may be provided as a method, system or computer program product. Therefore, one or more embodiments of the present specification may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware. Moreover, one or more embodiments of the present specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0135] One or more embodiments of the present specification may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. One or more embodiments of the present specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.

[0136] Each embodiment in this specification is described in a progressive manner, and the same and similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. In the description of this specification, the description of the reference terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic representation of the above terms does not necessarily target the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner. In addition, in the absence of mutual contradiction, a person skilled in the art can combine and combine the different embodiments or examples described in this specification and the features of different embodiments or examples.

[0137] The above description is only an example of one or more embodiments of the present specification and is not intended to limit one or more embodiments of the present specification. For those skilled in the art, one or more embodiments of the present specification may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present specification shall be included in the scope of the claims.

Claims

1. A method for executing a smart contract in a blockchain system, wherein the smart contract includes a loading function and a first position variable corresponding to a first state variable, wherein the first position variable is used to represent a storage position of a value of the first state variable in the data storage system, and a first storage type of the first state variable in the data storage system belongs to aggregate storage. The method is implemented by executing a contract code of the smart contract, and the method includes: Calling the loading function according to the storage location of the value of the first state variable in the data storage system; In response to calling the load function, the load function is executed to obtain the value of the first state variable from the data storage system according to the storage location of the value of the first state variable in the data storage system.

2. According to the method according to claim 1, the contract code is compiled from source code, and the source code defines a first storage type of the first state variable in the data storage system; the first storage type of the first state variable in the data storage system is determined based on a first data type of the first state variable in the memory.

3. According to the method according to claim 2, the first data type includes a basic type; or, the first data type includes a first composite type, and the source code also includes annotation information corresponding to the first state variable, and the annotation information is used to indicate that the first storage type of the first state variable in the data storage system belongs to aggregate storage.

4. According to the method of claim 2, the first position variable is defined in the source code, the variable name of the first position variable is the variable name of the first state variable, and the type of the first position variable is the first storage type.

5. The method according to claim 1, further comprising: Assign the value of the first state variable to the target temporary variable; The target transaction is executed using the value of the target temporary variable.

6. According to the method of claim 1, the smart contract further includes a storage function; the method further includes: Calling the storage function according to the value of the first state variable and the storage location of the value of the first state variable in the data storage system; In response to calling the storage function, the storage function is executed to store the value of the first state variable into the data storage system according to the storage location of the value of the first state variable in the data storage system.

7. According to the method of claim 1, the smart contract further includes a second position variable corresponding to the second state variable, the second position variable is used to represent the storage position of the value of the second state variable in the data storage system, and the second storage type of the second state variable in the data storage system belongs to separate storage; the second state variable includes at least one first member variable, and the first state variable belongs to the at least one first member variable; in, The method further includes: obtaining a storage location of the value of the second state variable in the data storage system, which includes a storage location of the value of the first state variable in the data storage system.

8. According to the method of claim 7, the contract code is obtained by compiling source code, the second position variable is defined in the source code, the variable name of the second position variable is the variable name of the second state variable, and the type of the second position variable is the second storage type.

9. A blockchain node in a blockchain system, wherein a smart contract is deployed in the blockchain system, wherein the smart contract includes a loading function and a first position variable corresponding to a first state variable, wherein the first position variable is used to represent a storage position of a value of the first state variable in the data storage system, wherein a first storage type of the first state variable in the data storage system belongs to aggregate storage, and wherein the blockchain node is used to execute the contract code of the smart contract to implement: Calling the loading function according to the storage location of the value of the first state variable in the data storage system; In response to calling the load function, the load function is executed to obtain the value of the first state variable from the data storage system according to the storage location of the value of the first state variable in the data storage system.

10. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed in a computing device, the computing device executes the method according to any one of claims 1 to 8.