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

By including upgrade information and decoding operations of state variables in the contract code of the smart contract, the compatibility problem caused by the modification of the state variable data structure in the smart contract is solved, and the accurate decoding of the old state variable values ​​by the new contract code is achieved, improving the Upgrade efficiency.

CN119248345APending Publication Date: 2025-01-03ANT BLOCKCHAIN TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411391918.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-30
Publication Date
2025-01-03

AI Technical Summary

Technical Problem

During the upgrade process of smart contracts in blockchain systems, modifying the data structure of existing state variables may lead to incompatibility in the contract status, resulting in the new contract code being unable to effectively decode the value of the original state variable, and even writing incorrect data.

Method used

通过在智能合约的合约代码中包含状态变量的升级信息,使用解码操作从旧版本的状态变量值中提取历史版本,并根据升级信息执行相应的解码操作,确保新版合约代码可以正确解码已有状态 The value of the variable.

Benefits of technology

During the smart contract upgrade process, there is no need to track and update the values ​​of existing state variables, ensuring that the new contract code can accurately decode the values ​​of existing state variables, improving the upgrade efficiency and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119248345A_ABST
    Figure CN119248345A_ABST
Patent Text Reader

Abstract

According to the method for executing the smart contract in the block chain system and the block chain node, a contract code of the smart contract comprises a first version of a first structure body corresponding to a first state variable and respective upgrade information of a plurality of sub-variables related to the first state variable. The upgrade information is used for indicating a modification type and a modification version of a corresponding sub-variable, the modification version is not greater than a first version, the method is realized by executing a contract code, and the method comprises the following steps: reading a value of a first state variable from a contract state of the smart contract; decoding operation is carried out on the value of the first state variable, the decoding operation comprises the steps that a second version is decoded from the value of the first state variable, the second version is a historical version of a second structural body used when the value of the first state variable is obtained through coding, and the first version is not smaller than the second version; and for any sub-variable in the plurality of sub-variables, executing a corresponding decoding sub-operation on the sub-variable according to the upgrading information corresponding to the sub-variable and the second version.
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 blockchain 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 deployed in blockchain systems may need to be upgraded for purposes such as fixing defects, improving functions, and optimizing business implementation processes. When upgrading smart contracts, the state variables declared in the contract code may be modified, such as adding state variables, deleting state variables, or modifying the data structure of existing state variables. Modifying the data structure of existing state variables may cause incompatibility issues in the contract state of the smart contract. For example, the upgraded contract code cannot effectively decode the value of the modified state variable in the original contract state, and may even cause incorrect state data to be written into the contract state. Summary of the invention

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

[0005] In a first aspect, a method for executing a smart contract in a blockchain system is provided, wherein the contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of multiple sub-variables involved in the first state variable, the upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modification version is not greater than the first version. The method is implemented by executing the contract code, and the method includes: reading the value of the first state variable from the contract state of the smart contract; performing a decoding operation on the value of the first state variable, wherein the decoding operation includes: decoding a second version from the value of the first state variable, the second version being a historical version of a second structure used when encoding the value of the first state variable, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information and / or the second version corresponding to the sub-variable.

[0006] 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 contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of multiple sub-variables involved in the first state variable, wherein the upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modification version is not greater than the first version. The blockchain node is used to execute the contract code to implement: reading the value of the first state variable from the contract state of the smart contract; performing a decoding operation on the value of the first state variable, wherein the decoding operation includes: decoding a second version from the value of the first state variable, wherein the second version is a historical version of the second structure used when encoding the value of the first state variable, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information and / or the second version corresponding to the sub-variable.

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

[0008] In the technical solution provided by the embodiment of this specification, the contract code of the smart contract includes a first version of the first structure corresponding to the first state variable, and upgrade information corresponding to each of the multiple sub-variables involved in the first state variable, the upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modification version is not greater than the first version; when the value of the first state variable needs to be decoded during the execution of the contract code, the value of the first state variable can be first read from the contract state of the smart contract, and the value of the first state variable is decoded, wherein the decoding operation includes: decoding the second version from the value of the first state variable, the second version is the historical version of the second structure used when the value of the first state variable is obtained by encoding, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, according to the upgrade information and / or the second version corresponding to the sub-variable, the corresponding decoding sub-operation is performed on the sub-variable. In this way, when the data structure of the existing state variables in the contract state of the smart contract is modified by the new contract code during the upgrade of the smart contract, it can be achieved without tracking and updating the value of the existing state variables in the contract state of the smart contract, ensuring that the new contract code can accurately decode the value of the existing state variable. BRIEF DESCRIPTION OF THE DRAWINGS

[0009] 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.

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

[0011] Figure 2 A schematic diagram of the relationship between the structure and the state variables provided as an example in the embodiments of this specification;

[0012] Figure 3 A schematic diagram of a storage structure corresponding to the value of a state variable provided exemplarily in an embodiment of this specification;

[0013] Figure 4 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

[0014] 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.

[0015] 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.

[0016] 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.

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

[0018] 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.

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

[0020] 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.

[0021] 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 terms defined in digital form in the blockchain environment. When an event triggers the terms 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 address of a state variable is stored from the root node of the storage trie tree to a leaf node, and the value of the state variable is stored in the leaf node.

[0022] Smart contracts deployed in blockchain systems may need to be upgraded for purposes such as fixing defects, improving functions, and optimizing business implementation processes. When upgrading smart contracts, the state variables declared in the contract code may be modified, such as adding state variables, deleting state variables, or modifying the data structure of existing state variables. Modifying the data structure of existing state variables may cause incompatibility issues in the contract state of the smart contract. For example, the upgraded contract code cannot effectively decode the value of the modified state variable in the original contract state, and may even cause incorrect state data to be written into the contract state.

[0023] In one possible implementation, during the process of upgrading a smart contract, if the data structure of existing state variables is modified in the new version of the contract code of the smart contract, it is possible to track through manual intervention which data structures of existing state variables need to be modified, and update the values ​​of these existing state variables accordingly in the contract state of the smart contract. When the new contract code needs to decode the values ​​of these existing state variables, because the values ​​of the updated state variables already conform to the data structure required by the new contract code, the new contract code can correctly decode the values ​​of these existing state variables.

[0024] In this implementation, manual intervention is required to track existing state variables whose data structures have been modified, and the values ​​of related state variables need to be updated in the contract state of the smart contract. This is relatively inefficient and the values ​​of state variables are prone to erroneous updates.

[0025] In the embodiment of this specification, a method and a blockchain node for executing a smart contract in a blockchain system are provided. The contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of the multiple sub-variables involved in the first state variable, the upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modification version is not greater than the first version; when the value of the first state variable needs to be decoded in the process of executing the contract code, the value of the first state variable can be first read from the contract state of the smart contract, and the value of the first state variable is decoded, wherein the decoding operation includes: decoding the second version from the value of the first state variable, the second version is the historical version of the second structure used when the value of the first state variable is obtained by encoding, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, according to the upgrade information and / or the second version corresponding to the sub-variable, the corresponding decoding sub-operation is performed on the sub-variable. In this way, when the data structure of the existing state variables in the contract state of the smart contract is modified by the new contract code in the process of upgrading the smart contract, it can be achieved without tracking and updating the value of the existing state variables in the contract state of the smart contract, ensuring that the new contract code can accurately decode the value of the existing state variable.

[0026] The following is an illustrative description of the process of obtaining the contract code for deploying or upgrading a smart contract.

[0027] In the process of creating the contract code for deploying the smart contract and the process of creating the contract code for upgrading the smart contract, the developer of the smart contract may expect to realize the aggregate storage of the values ​​of multiple sub-variables (or fields) involved in the state variable through the key-value pairs corresponding to a single state variable; the contract code mentioned here refers to the bytecode that can be executed by the blockchain system rather than the source code. In this case, when the developer is writing the source code corresponding to the contract code, the data structure of the state variable can be defined through the structure corresponding to a state variable, such as defining the names of the multiple sub-variables involved in the state variable, the arrangement order of the multiple sub-variables, and the variable types corresponding to each of them.

[0028] When compiling the source code corresponding to the contract code, the compiler can automatically generate the encoding function and decoding function corresponding to the structure according to the structure in the source code. The encoding function contains the encoding logic of the state variable corresponding to the structure, which is used to encode the data to be encoded according to the arrangement order and variable type of multiple sub-variables involved in the corresponding state variable to obtain the value of the state variable; the decoding function contains the decoding logic of the state variable corresponding to the structure, which is used to decode the value of the state variable obtained by the corresponding encoding logic according to the arrangement order and variable type of multiple sub-variables involved in the corresponding state variable, and obtain a decoding result that matches the data structure defined by the structure.

[0029] In the process of writing the source code corresponding to the contract code, not only can the data structure of the state variable be defined through the structure in the source code, but also the version of the relevant structure can be declared through the corresponding structure annotation in the source code, and the upgrade information corresponding to the multiple sub-variables involved in the corresponding state variable can be declared through the corresponding field annotation in the structure; it should be noted that the upgrade information may include but is not limited to the modification type and modification version of the corresponding sub-variable.

[0030] The following is an exemplary description of the process of obtaining the contract code for deploying the smart contract C1, such as the contract code of version 1.

[0031] Reference Figure 2As shown, it is assumed that the state variable k1 needs to be declared in the contract code of version 1 of the smart contract C1. In the source code corresponding to the contract code of version 1, the developer can define the data structure of the state variable k1 through a structure such as Struct Person. For example, the multiple sub-variables involved in defining the state variable k1 include "name" and "memo" arranged in sequence, where the variable type of "name" is a string "str", and the variable type of "memo" is also a string "str". Correspondingly, in the source code corresponding to the contract code of version 1, the structure annotation "@version 1" corresponding to Struct Person can also be used to declare that the version of Struct Person is 1; since Struct Person is not used to modify the data structure of other state variables already in the smart contract C1, there is no need to set the corresponding field annotation in StructPerson, or it can be considered that the field annotation included in Struct Person of version 1 is empty (Null).

[0032] When the source code corresponding to the contract code of version 1 is compiled, the decoding function and encoding function corresponding to the StructPerson of version 1 will be obtained. The decoding function may include the decoding method corresponding to the preset sub-variable such as "version", and may also include the decoding methods corresponding to the multiple sub-variables involved in k1, such as the decoding method Decode name corresponding to "name" and the decoding method Decode memo corresponding to "memo". Among them, the contract code of version 1 should include the decoding function, and in addition, the contract code also includes the corresponding encoding function.

[0033] It should be noted that, when the encoding function corresponding to Struct Person in the contract code with version 1 is used to encode the data to be encoded, and the value of k1 corresponding to Struct Person is obtained, the value of k1 will contain the version "1" of Struct Person. Figure 2 As shown, the encoding function can obtain the value of the subvariable name Alice and the value of the subvariable memo memo1 from a certain data to be encoded, and then organize the version "1", Alice and momo1 according to a certain encoding rule, obtain the value of k1, and associate k1 and the value of k1 with the contract state of the smart contract C1 through the corresponding method function.

[0034] The following is an exemplary description of the process of obtaining the contract code for upgrading the smart contract C1, such as the contract code of version 2.

[0035] Reference Figure 2 As shown, assuming that the contract code used by the smart contract C1 deployed to the blockchain system is the aforementioned contract code of version 1, and the user expects to use the contract code of version 2 to upgrade the smart contract C1, the developer may need to write the source code corresponding to the contract code of version 2. Based on the actual needs of a certain business purpose, the user may expect to modify the data structure of the existing state variable k1 in the smart contract C1, such as adding or deleting sub-variables involved in the state variable k1, and may need to redefine the structure Struct Person corresponding to the state variable k1 in the source code corresponding to the contract code of version 2. For example, a structure Struct Person corresponding to the state variable k1 and having version 2 may be redefined in the source code. In this case, in the source code corresponding to the contract code with version 2, the structure annotation such as "@version 2" can be used to declare that the version of the structure Struct Person corresponding to the state variable k1 is 2; if it is necessary to add a sub-variable such as "age" to the state variable k1, a sub-variable "age" with a variable type such as a 32-bit unsigned integer "u32" can be added on the basis of the structure with version 1, and the field annotation corresponding to the sub-variable "age" can be set such as "@version(since=2)", so as to declare the modification type of "age" as a new sub-variable in the structure Struct Person with version 2, and declare the modification version of "age" as 2; it can be understood that the modification version of the sub-variable "age" is 2, which essentially indicates that the sub-variable "age" is added to the structure Struct Person with version 2.

[0036] When the source code corresponding to the contract code of version 2 is compiled, the decoding function and encoding function corresponding to the StructPerson of version 2 will be obtained. The decoding function may include the decoding method corresponding to the preset sub-variable such as version, and may also include the decoding methods corresponding to the multiple sub-variables involved in k1, such as the decoding method Decode name corresponding to "name", the decoding method Decode memo corresponding to "memo", and the decoding method Decode age corresponding to "age". It can be understood that the contract code of version 2 of the smart contract C1 should include the decoding function, and in addition, the contract code of version 2 should also include the corresponding encoding function.

[0037] It should be noted that a single structure can correspond to multiple state variables. For example, StructPerson with version 2 not only corresponds to the existing state variable k1, but may also correspond to the newly added state variable k2 in the contract code with version 2. When the encoding function corresponding to Struct Person in the contract code with version 2 is used to encode the data to be encoded, the value of k1 or k2 corresponding to Struct Person will be included in the value of k1 or k2, which will contain the version "2" of Struct Person. For example, please continue to refer to Figure 2 As shown, the encoding function can obtain the value of the subvariable name Bob, the value of the subvariable age 23, and the value of the subvariable memo memo2 from a certain data to be encoded, and then organize the version "2", Bob and momo2 according to a certain encoding rule, obtain the value of k2, and associate k2 and the value of k2 through the corresponding method function to store in the contract state of the smart contract C1.

[0038] Similar, please continue to refer to Figure 2 As shown, assuming that the contract code used by the smart contract C1 deployed to the blockchain system is the aforementioned contract code of version 2, and the user expects to use the contract code of version 3 to upgrade the smart contract C1, the developer may need to write the source code corresponding to the contract code of version 3. Based on the actual needs of a certain business purpose, the user may expect to modify the data structure of the existing state variables k1 and k2 in the smart contract C1, such as adding or deleting sub-variables involved in the state variable k1, and then it may be necessary to redefine the structure Struct Person corresponding to the state variables k1 and k2 in the source code corresponding to the contract code of version 3. For example, a structure Struct Person corresponding to the state variables k1 and k2 and with version 3 may be redefined in the source code. In this case, in the source code corresponding to the contract code with version 3, the version of the structure Struct Person corresponding to the state variables k1 and k2 can be declared as 3 through a structure annotation such as "@version 3"; if it is necessary to delete sub-variables such as "memo" for the state variables k1 and k2, then on the basis of the structure Struct Person with version 2, the field annotation corresponding to the sub-variable "memo" can be set, such as "@deprecated(since=3)", to implement the declaration of the modification type corresponding to "memo" in the structure Struct Person with version 3 as sub-variable deletion, and the declaration of the modified version of "memo" as 3; it can be understood that the modified version of the sub-variable "memo" is 3, which essentially indicates that the sub-variable "memo" is deleted in the structure Struct Person with version 3.

[0039] Similar to the process of compiling the source code corresponding to the contract code with version 2, when compiling the source code corresponding to the contract code with version 3, the decoding function and encoding function corresponding to the Struct Person with version 3 will be obtained. The decoding function may include the decoding method corresponding to the preset sub-variable version, and may also include the decoding methods corresponding to the multiple sub-variables involved in k1 and k2, such as the decoding method Decode name corresponding to "name", the decoding method Decode memo corresponding to "memo", and the decoding method Decode age corresponding to "age". It can be understood that the contract code with version 2 should include the decoding function, and in addition, the corresponding encoding function should also be included.

[0040] It should be noted that the version of the contract code and the version of the structure defined in its corresponding source code may not be the same. For example, in the contract code of smart contract C1 with version 4, it may not be expected to modify the data structure of the existing state variables k1, k2 and k3. In the source code corresponding to the contract code with version 4, the structure Struct Person with version 3 is still used. That is, in the source code corresponding to the contract code with version 4, the structure annotation of the structure Struct Person corresponding to the state variables k1, k2 and k3 still declares that the version of Struct Person is 3, which is different from the version 4 of the contract code.

[0041] It should be noted that when compiling the source code into contract code, the field annotations in the structure will be converted into corresponding upgrade information. Corresponding to the function of the field annotation, the upgrade information is used to indicate the modification type and modification version of the corresponding subvariable.

[0042] For the values ​​of state variables, SimpleSerialize (SSZ) encoding can usually be used to aggregate and store them in the storage of related computing devices. Figure 3As shown, in the SSZ encoding scheme, all encoded sub-variables are arranged in the order defined by the structure. For example, for the state variable k2 corresponding to the structure Struct Person with version 2 in the above example, four positions arranged in sequence can be determined first, where position 1 corresponds to the version of Struct Person, that is, the preset sub-variable version, position 2 corresponds to the sub-variable name, position 3 corresponds to the sub-variable age, and position 4 corresponds to the sub-variable memo, wherein the above positions 1 to 4 can all have the same predetermined length. For sub-variables with fixed lengths, such as version and age, the values ​​can be directly stored in their respective corresponding positions, such as position 1 and position 3. For sub-variables with variable lengths, such as name, the corresponding position, such as position 2, can store an address offset, which is used to indicate the address where the value of name is actually stored, such as the address of position 5 after position 4; based on similar rules, for sub-variables with variable lengths, such as memo, the corresponding position, such as position 4, can store an address offset, which is used to indicate the address where the value of memo is actually stored, such as the address of position 6 after position 5.

[0043] The values ​​of state variables are aggregated and stored through SSZ encoding. Adding new sub-variables to the structure corresponding to the state variable does not affect the decoding logic of the existing sub-variables, which is conducive to quickly completing the decoding of the values ​​of the state variables. It can be understood that the values ​​of state variables can also be aggregated and stored through other serialized storage methods. For example, for any variable-length sub-variable such as the value of name, the length of the value of name can be stored in its corresponding position with a predetermined length, such as position 2. The value of name can be directly stored in position 3 after position 2 and with a corresponding length, and the fixed-length value of age is sequentially stored in position 4 with a predetermined length after position 3.

[0044] The following is an illustrative description of the process of executing the contract code of a smart contract in a blockchain system.

[0045] Figure 3 It is a flowchart of a method for executing a smart contract in a blockchain system provided in an embodiment of this specification. The method exemplarily describes the process of executing a smart contract C1 in a blockchain system. The contract code of the smart contract C1 includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of a plurality of sub-variables involved in the first state variable. The upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modification version is not greater than the first version. The method can be implemented by executing the contract code of the smart contract C1 by a blockchain node.

[0046] For smart contract C1 that has been deployed to the blockchain system, the user may need to upgrade the deployed smart contract C1 using a new version of the contract code, such as the aforementioned version 2 or version 3, in order to fix defects, improve functions, optimize the business implementation process, etc.; after completing the upgrade of smart contract C1 in the blockchain system, the blockchain system will complete the execution of smart contract C1 by executing the new version of the contract code.

[0047] During the execution of the new version of the contract code, the value of the state variable that needs to be accessed may be stored in the contract state by executing the new version of the contract code or the old version of the contract code. For example, in the process of executing the aforementioned version 3 of the contract code on the smart contract C1, the blockchain system may need to access the value of the existing state variables k1, k2 or k3. The value of k1 may be stored in the contract state by executing the contract code of version 1, the value of k2 may be stored in the contract state by executing the contract code of version 2, and the value of k3 may be stored in the contract state by executing the contract code of version 2. This means that part or all of the following steps S301 to S303 may be executed in the blockchain system.

[0048] In step S301, the value of the first state variable is read from the contract state of the smart contract C1.

[0049] The value of the first state variable is obtained by encoding the corresponding data to be encoded using the encoding function corresponding to a second structure in the contract code of a certain version during the execution of the contract code of the version; the second structure is used to define the data structure of the first state variable in the source code corresponding to the contract code of the version, and the value of the first state variable shall include the second version of the second structure in addition to the values ​​of some or all sub-variables involved in the first state variable. The first version corresponding to the first structure used to define the data structure of the first state variable in the source code corresponding to the contract code of the smart contract C1 executed by the blockchain system at the current moment may be greater than or equal to the second version.

[0050] Continuing from the previous article Figure 2 Corresponding examples, when the contract code with version 1 is executed, the first state variable can be the state variable k1 declared in the contract code with version 1; when the contract code with version 2 is executed, the first state variable can be the state variable k1 or k2 declared in the contract code with version 2; when the contract code with version 3 is executed, the first state variable can be the state variable k1, k2 or k3 declared in the contract code with version 3.

[0051] In step S303, a decoding operation is performed on the value of the first state variable, wherein the decoding operation includes: decoding a second version from the value of the first state variable, the second version being a historical version of the second structure used when encoding the value of the first state variable, and the first version being not less than the second version; for any sub-variable among the multiple sub-variables, performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information and / or the second version corresponding to the sub-variable.

[0052] The decoding operation can be implemented by executing a decoding function in the contract code, where the decoding function is generated according to a first structure for defining a data structure of a first state variable during the process of compiling a source code corresponding to the contract code.

[0053] Continuing from the previous article Figure 2 Corresponding examples, when the first state variable is k1, for example, the decoding method Decode version corresponding to the preset sub-variable "version" in the relevant decoding function can be used to decode the second version "1" from the value of k1; when the first state variable is k2, for example, the decoding method Decode version corresponding to the preset sub-variable "version" in the relevant decoding function can be used to decode the second version "2" from the value of k2; when the first state variable is k3, for example, the decoding method Decode version corresponding to the preset sub-variable "version" in the relevant decoding function can be used to decode the second version "3" from the value of k3.

[0054] In the process of upgrading smart contracts, the data structure of existing state variables may be modified through new contract codes. Therefore, in different versions of contract codes, multiple sub-variables involved in the same state variable may be different. For example, please continue to refer to the above Figure 2 Corresponding example, in the contract code with version 1, the structure used to define the data structure of the state variable k1 is Struct Person with version 1, so in the contract code with version 1, the multiple sub-variables involved in the first state variable specifically include name and memo; in the contract code with version 2, the structure used to define the data structure of the state variable k1 is Struct Person with version 2, so in the contract code with version 2, the multiple sub-variables involved in the first state variable specifically include name, age and memo; similarly, in the contract code with version 3, the structure used to define the data structure of the state variable k1 is Struct Person with version 3, so in the contract code with version 3, the multiple sub-variables involved in the first state variable specifically include name, age and memo.

[0055] During the decoding operation of the value of the first state variable, when the second version is successfully decoded from the value of the first state variable, the contract code can perform sub-decoding operations on the multiple sub-variables according to the arrangement order of the multiple sub-variables involved in the first state variable defined by the first structure. Figure 2 For example, for the contract code with version 1, the decoding sub-operations will be performed on name and memo in sequence; for the contract code with version 2, the decoding sub-operations will be performed on name, age, and memo in sequence; for the contract code with version 3, the decoding sub-operations will be performed on name, age, and memo in sequence.

[0056] The following describes in detail the specific implementation process of various sub-decoding operations that may be performed on the sub-variables.

[0057] In the first case, when the modification type of the subvariable indicates that the subvariable is an unmodified subvariable, the value of the subvariable is decoded from the value of the first state variable, and the value of the subvariable is added to the decoding result of the first state variable.

[0058] Continuing from the above Figure 2 Corresponding examples, for the subvariable name involved in the state variables k1, k2 or k3, the upgrade information corresponding to name may be empty. It can be considered that the modification type in the upgrade information corresponding to name indicates that name is a subvariable that has not been modified. In this case, the value of name can be decoded from the value of k1, k2 or k3 and added to the corresponding decoding result. For example, the value of name Alice can be decoded from the value of k1 through the decoding method corresponding to name in the relevant decoding function, such as Decode name, and the value of name Alice can be added to the decoding result of k1; similarly, the value of name Bob can be decoded from the value of k2, and the value of name Bob can be added to the decoding result of k2; similarly, the value of name Cindy can be decoded from the value of k3, and the value of name Cindy can be added to the decoding result of k3.

[0059] In the second case, when the modification type of the subvariable indicates that the subvariable is a newly added subvariable, it can be first determined whether the second version is smaller than the modified version of the subvariable. When the second version is not smaller than the modified version of the subvariable, the value of the subvariable is decoded from the value of the first state variable, and the value of the subvariable is added to the decoding result; when the second version is smaller than the modified version of the subvariable, the value of the subvariable is set to a preset value in the decoding result.

[0060] Continuing from the above Figure 2Corresponding example, for the sub-variable age involved in the state variable k2 or k3, the modification type included in the upgrade information corresponding to age indicates that age is a newly added sub-variable. If the first state variable is k1, the second version decoded from k1 is 1, and the modified version included in the upgrade information of age is 2, that is, the second version is less than the modified version of age, the value of age can be set to a preset value, such as 0, in the decoding result of k1 through the data filling method corresponding to age in the relevant decoding function, such as fillage. If the first state variable is k2 or k3, the second version decoded from k2 or k3 is 2 or 3, and the modified version included in the upgrade information of age is 2, that is, the second version is not less than the modified version of age, so the value of age 32 can be decoded from the value of k2 through the decoding method corresponding to age in the relevant decoding function, such as Decode age, and the value of age 32 is added to the decoding result of k2. Similarly, the value of age 23 can also be decoded from the value of k3, and the value of age 23 is added to the decoding result of k3.

[0061] In the third case, when the modification type of the subvariable indicates that the subvariable is a deleted subvariable, it is possible to first determine whether the second version is greater than the modified version of the subvariable. If so, the decoding suboperation on the subvariable is directly terminated; otherwise, the value of the subvariable is decoded from the value of the first state variable and discarded.

[0062] Continuing from the above Figure 2 Corresponding example, if the version of the currently executed contract code is 3, that is, the first structure is Struct Person with version 3, Struct Person with version 3 can correspond to state variables k1, k2 and k3, that is, the first state variable can be k1, k2 or k3, and the modification type included in the upgrade information of the sub-variable memo involved in k1, k2 or k3 indicates that memo is a deleted sub-variable, so the value of memo will be decoded from the values ​​of k1 and k2 and discarded, while k3 will directly terminate the decoding sub-operation of memo. More specifically, when memo is decoded by the decoding method corresponding to memo in the relevant decoding function, such as Decode memo, the value of memo will be decoded from the values ​​of k1 and k2 but will not be added to the corresponding decoding result. It is only necessary to determine the position of memo and / or the value of memo corresponding to the values ​​of k1 and k2, so as to continue to perform decoding sub-operations on other sub-variables located after memo in the subsequent process.

[0063] In some embodiments, during the process of executing the contract code of the smart contract, the blockchain system may also encode the target data to be encoded according to the first version, obtain the value of the second state variable corresponding to the first structure, the value of the second state variable includes the first version, and store the value of the second state variable in the contract state of the smart contract.

[0064] The second state variable may be a state variable newly added in the executed new version of the contract code, or may be an existing state variable defined in the old version of the contract code, that is, the second state variable may be the same as or different from the aforementioned first state variable. If the second state variable is an existing state variable defined in the old version of the contract code, then storing the value of the second state variable in the contract state of the smart contract essentially completes the update of the value of the existing state variable defined in the old version of the contract code.

[0065] Continuing from the above Figure 2 Corresponding example, here we continue to assume that the first structure is StructPerson with version 3, and the second state variable is the state variable k3 corresponding to the first structure. Then, in the process of executing the contract code with version 3, the blockchain system may encode the value of k3 by using the encoding function corresponding to the contract code with version 3 and Struct Person with version 3, the value of the preset sub-variable version 3, the value of the sub-variable name Cindy, and the value of the sub-variable age 23.

[0066] In summary, when the data structure of an existing state variable is modified due to upgrading a smart contract, the new version of the contract code can decode the value of the existing state variable to obtain a decoding result that meets the requirements of the new version of the contract code, so that the new version of the contract code can use the decoding result. Moreover, the upper-level business code that actually uses the decoding result in the new version of the contract code does not need to perceive the version corresponding to the value of the existing state variable and the data structure it satisfies, thereby better supporting the new version of the contract code to directly use the value of the existing state variable encoded by the old version of the contract code during the process of upgrading the smart contract.

[0067] 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 contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of multiple sub-variables involved in the first state variable, wherein the upgrade information is used to indicate the modification type and modification version of the corresponding sub-variable, and the modified version is not greater than the first version. The method is implemented by executing the contract code, and the method includes: reading the value of the first state variable from the contract state of the smart contract; performing a decoding operation on the value of the first state variable, wherein the decoding operation includes: decoding a second version from the value of the first state variable, wherein the second version is a historical version of the second structure used when encoding the value of the first state variable, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, performing a corresponding decoding sub-operation on the sub-variable at least according to the upgrade information and / or the second version corresponding to the sub-variable.

[0068] The embodiments of the present specification also provide a computer-readable storage medium on which a computer program / instruction is stored. When the computer program / instruction is executed in a computer, the computer is caused to execute the method described in any of the aforementioned embodiments.

[0069] 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 / instructions, and when the processor executes the computer program / instructions, the method in any of the foregoing embodiments is implemented.

[0070] 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.

[0071] 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.

[0072] 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.

[0073] Although one or more embodiments of the present specification provide method transaction steps as described in the embodiments or flow charts, more or less transaction 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.

[0074] 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.

[0075] 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.

[0076] 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.

[0077] These computer program instructions may also be loaded onto a computer or other programmable data processing device so that a series of transaction steps are executed on the computer or other programmable device to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable device provide 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.

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

[0079] 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.

[0080] 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.

[0081] 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.

[0082] 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.

[0083] 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.

[0084] 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 contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of a plurality of sub-variables involved in the first state variable, wherein the upgrade information is used to indicate a modification type and a modification version of the corresponding sub-variable, wherein the modification version is not greater than the first version, and the method is implemented by executing the contract code, and the method includes: Reading the value of the first state variable from the contract state of the smart contract; Performing a decoding operation on the value of the first state variable, wherein the decoding operation comprises: Decoding a second version from the value of the first state variable, where the second version is a historical version of a second structure used when encoding the value of the first state variable, and the first version is not less than the second version; For any sub-variable among the multiple sub-variables, a corresponding decoding sub-operation is performed on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version.

2. According to the method of claim 1, the contract code also includes a decoding function generated based on the first structure, and the decoding operation is implemented by executing the decoding function.

3. The method according to claim 1, wherein the step of performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version comprises: When the modification type of the subvariable indicates that the subvariable is a newly added subvariable, determining whether the second version is smaller than the modified version of the subvariable; When the second version is not less than the modified version of the sub-variable, the value of the sub-variable is decoded from the value of the first state variable, and the value of the sub-variable is added to the decoding result of the first state variable.

4. The method according to claim 3, wherein the step of performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version further comprises: When the second version is smaller than the modified version of the sub-variable, the value of the sub-variable is set to a preset value in the decoding result of the first state variable.

5. The method according to claim 1, wherein performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version comprises: When the modification type of the subvariable indicates that the subvariable is a deleted subvariable, determining whether the second version is greater than the modified version of the subvariable; When the second version is greater than the modified version of the subvariable, the decoding sub-operation on the subvariable is terminated.

6. The method according to claim 5, further comprising: When the second version is not greater than the modified version of the sub-variable, the value of the sub-variable is decoded from the value of the first state variable and discarded.

7. The method according to claim 1, wherein the step of performing a corresponding decoding sub-operation on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version comprises: When the modification type of the subvariable indicates that the subvariable is an unmodified subvariable, the value of the subvariable is decoded from the value of the first state variable, and the value of the subvariable is added to the decoding result of the first state variable.

8. The method according to claim 1, further comprising: Encode the target data to be encoded according to the first version to obtain a value of a second state variable corresponding to the first structure, wherein the value of the second state variable includes the first version; The value of the second state variable is stored in the contract state of the smart contract.

9. According to the method of claim 1, the contract code is obtained by compiling the source code of the smart contract through a compiler, the contract source code defines the first structure and its corresponding structure annotation, the first structure includes a field annotation, the structure annotation is used to indicate that the version of the first structure is the first version, and the field annotation is used to indicate the upgrade information corresponding to each of the multiple sub-variables.

10. The method according to any one of claims 1-9, wherein the value of the first state variable is SSZ-coded.

11. A blockchain node in a blockchain system, wherein a smart contract is deployed in the blockchain system, wherein the contract code of the smart contract includes a first version of a first structure corresponding to a first state variable, and upgrade information corresponding to each of a plurality of sub-variables involved in the first state variable, wherein the upgrade information is used to indicate a modification type and a modification version of the corresponding sub-variable, wherein the modification version is not greater than the first version, and wherein the blockchain node is used to execute the contract code to implement: reading the value of the first state variable from the contract state of the smart contract; and performing a decoding operation on the value of the first state variable, wherein the decoding operation includes: A second version is decoded from the value of the first state variable, where the second version is a historical version of a second structure used when encoding the value of the first state variable, and the first version is not less than the second version; for any sub-variable among the multiple sub-variables, a corresponding decoding sub-operation is performed on the sub-variable according to the upgrade information corresponding to the sub-variable and / or the second version.

12. 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 10.