CN112001799B - Method and device for executing intelligent contract in block chain - Google Patents

Method and device for executing intelligent contract in block chain Download PDF

Info

Publication number
CN112001799B
CN112001799B CN202011173786.9A CN202011173786A CN112001799B CN 112001799 B CN112001799 B CN 112001799B CN 202011173786 A CN202011173786 A CN 202011173786A CN 112001799 B CN112001799 B CN 112001799B
Authority
CN
China
Prior art keywords
variable
value
subspace
contract
intelligent contract
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202011173786.9A
Other languages
Chinese (zh)
Other versions
CN112001799A (en
Inventor
谢桂鲁
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Alipay Hangzhou Information Technology Co Ltd
Original Assignee
Alipay Hangzhou Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Alipay Hangzhou Information Technology Co Ltd filed Critical Alipay Hangzhou Information Technology Co Ltd
Priority to CN202110334950.8A priority Critical patent/CN112950378B/en
Priority to CN202011173786.9A priority patent/CN112001799B/en
Publication of CN112001799A publication Critical patent/CN112001799A/en
Application granted granted Critical
Publication of CN112001799B publication Critical patent/CN112001799B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06QINFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
    • G06Q40/00Finance; Insurance; Tax strategies; Processing of corporate or income taxes
    • G06Q40/04Trading; Exchange, e.g. stocks, commodities, derivatives or currency exchange
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24552Database cache management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor

Abstract

The embodiment of the specification provides a method and a device for executing intelligent contracts in a blockchain, wherein the method comprises the following steps: after a first intelligent contract called in a transaction begins to be executed, allocating a first subspace corresponding to the first intelligent contract in a storage space corresponding to the transaction; reading a current state value of a first variable as a first value of the first variable before modifying the first variable according to the first intelligent contract; storing a first value of the first variable in the first subspace; modifying a state value of the first variable according to the first smart contract; after the execution of the first intelligent contract is finished, reading and deleting the first subspace to obtain a first value of the first variable; in the event of a failure to execute the first intelligent contract, restoring the state value of the first variable to the first value.

Description

Method and device for executing intelligent contract in block chain
Technical Field
The embodiment of the specification relates to the technical field of blockchain, in particular to a method and a device for executing intelligent contracts in blockchain.
Background
The blockchain technology is a brand-new distributed infrastructure and computing paradigm that is built on a peer-to-peer (P2P) network, utilizes a chained data structure to verify and store data, utilizes a distributed node consensus algorithm to generate and update data, utilizes cryptography to ensure the security of data transmission and access, and utilizes an intelligent contract composed of automated script codes to program and operate data. In the blockchain, consensus data in the blockchain is generated by sending a transaction and causing the transaction to be performed by a node. The transaction may include a call to a smart contract such that, when the transaction is executed, a change to a world state is made by executing the smart contract, the world state including, for example, an account balance, a storage variable of the smart contract, or the like. In addition, a contract a invoked in a transaction may invoke a contract B, which if executed fails, requires a rollback of the world state modifications made while executing the contract B, otherwise the world state correctness will be affected. In the prior art, in order to solve the above problem, when executing a contract B called in a contract a, a world state copy including key-value pairs of all storage variables appearing in the contract B is copied with respect to the contract B, the world state copy is updated into the world state only when the execution of the contract B is successful, and the world state copy is discarded if the execution of the contract B fails. In the method, the world state copy corresponding to the contract B needs to include key-value pairs of all storage variables appearing in the contract B, and the data volume is large.
Therefore, a more efficient scheme for executing smart contracts is needed.
Disclosure of Invention
The embodiments of the present specification aim to provide a more efficient scheme for executing intelligent contracts, so as to solve the deficiencies in the prior art.
To achieve the above object, one aspect of the present specification provides a method for executing an intelligent contract in a blockchain, the method including:
after a first intelligent contract called in a transaction begins to be executed, allocating a first subspace corresponding to the first intelligent contract in a storage space corresponding to the transaction;
reading a current state value of a first variable as a first value of the first variable before modifying the first variable according to the first intelligent contract;
storing a first value of the first variable in the first subspace;
modifying a state value of the first variable according to the first smart contract;
after the execution of the first intelligent contract is finished, reading and deleting the first subspace to obtain a first value of the first variable;
in the event of a failure to execute the first intelligent contract, restoring the state value of the first variable to the first value.
In one embodiment, the first subspace is used to indicate an order of invocation of the first smart contract in the transaction,
reading and deleting the first subspace comprises reading and deleting a currently latest allocated subspace based on the calling sequence of the corresponding intelligent contracts indicated by the subspaces in the storage space.
In one embodiment, storing the first value of the first variable in the first subspace includes storing a first key-value pair of the first variable in the first subspace, the first key-value pair including the first value.
In one embodiment, a second intelligent contract is invoked in the transaction, the second intelligent contract invoking the first intelligent contract, a second subspace corresponding to the second intelligent contract is further included in the memory space corresponding to the transaction, and the method further includes, in the event that execution of the first intelligent contract is successful, adding the first value of the first variable to the second subspace in a predetermined order.
In one embodiment, invoking a third intelligent contract in the first intelligent contract, the third intelligent contract including a modification to the first variable, reading and deleting the first subspace to obtain the first value of the first variable includes reading at least two values of the first variable from the first subspace, and obtaining an oldest stored value from the at least two values based on an order in which the at least two values are arranged in the first subspace.
In one embodiment, the third intelligent contract includes a modification of a second variable, and reading and deleting the first subspace further includes reading and deleting the first subspace to obtain a first value of the second variable, and the method further includes, in the event of a failure to execute the first intelligent contract, restoring the state value of the second variable to the first value of the second variable in the state database.
In one embodiment, the storage space is a sequential table, wherein allocating a first subspace corresponding to the first intelligent contract in the storage space corresponding to the transaction includes allocating a first table element in the sequential table that is ordered relative to other table elements,
reading and deleting the currently latest allocated subspace includes reading and deleting the currently latest allocated table element based on the arrangement order of the table elements in the order table.
In one embodiment, the storage space is a stack, wherein allocating a first subspace corresponding to the first smart contract in the storage space corresponding to the transaction includes, pushing a first element as a top-of-stack element in the stack,
reading and deleting the currently latest allocated subspace comprises reading and deleting a current top-of-stack element of the stack.
In one embodiment, the first variable is an account balance of the blockchain or a storage variable included in the first smart contract.
In one embodiment, reading the current state value of the first variable as the first value of the first variable includes reading the current state value of the first variable from a memory as the first value of the first variable, and modifying the state value of the first variable according to the first smart contract includes modifying the state value of the first variable in the memory according to the first smart contract.
Another aspect of the present specification provides an apparatus for executing an intelligent contract in a blockchain, the apparatus comprising:
the allocation unit is configured to allocate a first subspace corresponding to a first intelligent contract in a storage space corresponding to a transaction after the first intelligent contract called in the transaction starts to be executed;
the reading unit is configured to read a current state value of a first variable as a first value of the first variable before the first variable is modified according to the first intelligent contract;
a storing unit configured to store a first value of the first variable into the first subspace;
a modification unit configured to modify a state value of the first variable according to the first smart contract;
a reading and deleting unit configured to read and delete the first subspace to obtain a first value of the first variable after the execution of the first intelligent contract is finished;
a first restoring unit configured to restore the state value of the first variable to the first value in a case where execution of the first smart contract fails.
In one embodiment, the first subspace is used to indicate an order of invocation of the first smart contract in the transaction,
the reading and deleting unit is further configured to read and delete the currently latest allocated subspace based on the calling order of the corresponding smart contracts indicated by the subspaces in the storage space.
In one embodiment, the storing unit is further configured to store a first key-value pair of the first variable into the first subspace, the first key-value pair including the first value.
In one embodiment, a second intelligent contract is invoked in the transaction, the second intelligent contract invoking the first intelligent contract, a second subspace corresponding to the second intelligent contract is further included in the storage space corresponding to the transaction, and the apparatus further includes an adding unit configured to add the first value of the first variable to the second subspace in a predetermined order in a case where the execution of the first intelligent contract is successful.
In one embodiment, a third intelligent contract is invoked from the first intelligent contract, the third intelligent contract including a modification of the first variable, and the reading and deleting unit includes a reading subunit configured to read at least two values of the first variable from the first subspace, and an obtaining subunit configured to obtain an oldest stored value from the at least two values based on an order of arrangement of the at least two values in the first subspace.
In one embodiment, the third intelligent contract includes a modification of a second variable, the reading and deleting unit is further configured to read and delete the first subspace to obtain a first value of the second variable, and the apparatus further includes a second restoring unit configured to restore, in the case of a failure in executing the first intelligent contract, the state value of the second variable to the first value of the second variable in the state database.
In one embodiment, the storage space is a sequence table, wherein the allocation unit is further configured to allocate a first table element in the sequence table that is arranged sequentially with respect to other table elements,
the reading and deleting unit is further configured to read and delete a currently latest allocated table element based on an arrangement order of the respective table elements in the order table.
In one embodiment, the storage space is a stack, wherein the allocation unit is further configured to stack a first element as a top element in the stack,
the read and delete unit is further configured to read and delete a current top-of-stack element of the stack.
In one embodiment, the first variable is an account balance of the blockchain or a storage variable included in the first smart contract.
In an embodiment, the reading unit is further configured to read a current state value of a first variable from a memory as a first value of the first variable, and the modifying unit is further configured to modify the state value of the first variable in the memory according to the first smart contract.
Another aspect of the present specification provides a computer readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to perform any one of the above methods.
Another aspect of the present specification provides a computing device comprising a memory having stored therein executable code, and a processor that, when executing the executable code, implements any of the methods described above.
According to the scheme for executing the intelligent contract, a data structure such as a stack is used for indicating the calling level of the contract in the transaction, the stack element is used for storing the value of the variable before the contract modification variable is executed, and when the contract calling fails, the world state can be rolled back according to the stack top element, so that the efficiency of executing the contract is improved, and the performance of a block chain is improved.
Drawings
The embodiments of the present specification may be made more clear by describing the embodiments with reference to the attached drawings:
FIG. 1 is a schematic diagram of a process for executing intelligent contracts in a blockchain 100;
FIG. 2 illustrates a flow diagram of a method for executing intelligent contracts in blockchains in accordance with an embodiment of the present description;
FIG. 3 shows a schematic diagram of the process of executing contract A in transaction Tx1 in node 1;
fig. 4 shows a schematic diagram of the data stored in the memory 12 of the node 1 after execution of the contract B;
FIG. 5 shows a schematic of the data stored in the memory 12 of node 1 after execution of contract C;
FIG. 6 schematically shows a diagram of the stack 121 after this appending;
fig. 7 illustrates an apparatus 700 for executing an intelligent contract in a blockchain according to an embodiment of the present description.
Detailed Description
The embodiments of the present specification will be described below with reference to the accompanying drawings.
Fig. 1 is a schematic diagram of a process for executing intelligent contracts in a blockchain 100. As shown in fig. 1, the blockchain 100 includes a plurality of interconnected nodes, including, for example, node 1 and node 2. Assuming that Alice, corresponding to node 2, wants to invoke an intelligent contract (e.g., contract postdata in fig. 1) already deployed in blockchain 100, node 2 may initiate a transaction (Tx) to any node in blockchain 100 to make the contract invocation.
Specifically, in the transaction content, the from field may be an account address of the caller Alice, and the to field is a contract address 0x6f … of the aforementioned PostingData contract, which represents the intelligent contract invoked by the transaction. In the Data field, the method or function name (set) in the contract to be called and the incoming parameters ("green commons") are contained.
And after receiving the transaction, the accounting node in the block chain packs the transaction into the block and sends the block to other nodes in the block chain after recognizing the block. After receiving the block, the other nodes in the block chain 100 execute each transaction in the block, allocate a storage space corresponding to the block in the memory, and use the storage space to store the world states of the variables included in each transaction in the block, and after completing the execution of all transactions in the block, update the world states of the variables to the account state database 11, and delete the storage space allocated in the memory and corresponding to the block. Although it is described above that in executing a transaction of a block, the world state of a variable is cached in the memory, and the world state of the variable is updated to the account state database after all transactions in the block are executed, the embodiments of the present disclosure are not limited thereto. For example, in embodiments of the present description, the state values of the variables may also be updated into the account state database after each transaction is performed.
In fig. 1 is shown an account status database 11 in a node 1, which account status database 11 records the world status of each account in a blockchain, for example in the form of an mpt (media Patricia trie) status tree. The MPT state tree stores the corresponding relation between the accounts and the account data in a tree structure, and nodes in the tree are connected through Hash, namely, Hash values are generated based on child nodes and stored in father nodes. In the MPT tree, except for a root node, each node corresponds to at least one character, the continuous concatenation of characters on a path from the root node to a specific node is an account address of an account corresponding to the specific node, and the content stored in the specific node is an account state or account content (or a world state) of the account corresponding to the specific node. The accounts in the etherhouse blockchain may include external accounts and contract accounts, where the external accounts are accounts of the user. The account status of the external account includes, for example, an account balance, and the account status of the contract account includes, for example, data such as an account balance, a value of a storage variable in the contract, and the like, which are recorded in the MPT status tree in the form of a key-value pair.
For example, when executing the transaction, node 1 in the blockchain 100 executes the set function, so that the storage variable postData value in the account status of the intelligent contract postData is modified to "green commons" in the memory, that is, the world status of the storage variable postData recorded in the memory is modified. And the world state of the storage variable postData recorded in the internal memory will be updated to the account state database 11 after all transactions in the completion block are performed.
While in actual execution of an intelligent contract in a transaction, there may be instances where contract execution fails. For example, any of the following may cause a contract to fail to execute: insufficient fuel (GAS) balance for performing transactions, illegal instructions in contracts, failed reads and writes to storage units, virtual machine stack overflow, virtual machine memory overflow, transfer failures, failed reads to contracts, and the like. In the case of a contract execution failure, in order to ensure the correctness of the world state recorded in the memory, the world state in the memory modified in the course of executing the contract needs to be rolled back, i.e., restored to the value before modification.
To this end, the embodiments of the present specification propose a method for efficiently rolling back world states in the event of a contract execution failure, in which partial initial world states respectively corresponding to multiple layers of smart contracts invoked in a transaction are recorded by a plurality of subspaces arranged in an order in a storage space allocated to the transaction. The storage space is, for example, a stack, wherein the currently executing contract corresponds, for example, to a top-of-stack element in the stack. Therefore, when any layer of intelligent contracts fails to execute, the world state can be rolled back based on the initial world state recorded in the top element of the stack, and the performance of executing the intelligent contracts is improved.
A scheme of executing an intelligent contract according to an embodiment of the present specification will be described in detail below.
Fig. 2 is a flowchart illustrating a method for executing an intelligent contract in a blockchain according to an embodiment of the present specification, where the method includes:
in step S202, after starting to execute the smart contract called in the transaction, a new element is stacked in the stack corresponding to the transaction;
in step S204, before the variables are modified according to the intelligent contract, the current state values of the variables are read as first values of the variables;
at step S206, storing a first value of the variable in a top-of-stack element of the stack;
in step S208, modifying the state values of the variables according to the smart contract;
in step S210, after the execution of the intelligent contract is finished, reading and deleting a stack top element to obtain a first value of the variable;
in step S212, in the case of failure of execution of the smart contract, restoring the value of the variable to the first value of the variable;
in step S214, in the case of successful execution of the intelligent contract, if the intelligent contract is a contract called in another intelligent contract, the first value of the variable is added to the current top-of-stack element of the stack.
The method shown in FIG. 2 will be described below by way of an example of executing transaction Tx 1. Assume that contract A is invoked in transaction Tx1, contract B is invoked in contract A, and contract C is invoked in contract B, i.e., multiple levels of invocation of smart contracts are included in transaction Tx 1. Fig. 3 shows a schematic diagram of the process of executing contract a in transaction Tx1 in node 1. As shown in fig. 3, the node 1 includes a state database 11 and a memory 12, and the state database 11 is stored in a persistent medium in the node 1. As described above, the state database 11 stores therein key-value pairs of a plurality of variables in the blockchain, including, for example, (k 1: v 1), (k 2: v 2), (k 3: v 3), etc., after starting execution of the transaction Tx1, node 1 (i.e., the node device of node 1) allocates a stack 121 to the transaction Tx1 in the memory 12 for executing the method shown in FIG. 2, initially, as shown in the top box of FIG. 3, with an empty stack in the stack 121, i.e., without any stack elements being pushed therein.
When executing contract a into transaction Tx1, step S202 is performed, as shown in the 2 nd block in fig. 3, to stack a new element in stack 121. The newly popped element is currently the top-of-stack element of the stack 121, which corresponds to contract A.
The contract a includes, for example, a write instruction "set k1, va" for writing a value va to the variable k1 in the state database 11. After executing the write instruction "set k1, va" into the contract a, first, as shown in the 3 rd block in fig. 3, step S204 is executed to read the current state value of the variable k 1. Node 1 may first determine whether the memory space 122 corresponding to the block in which the transaction Tx1 is located in memory stores the state value of the variable k1, assuming that the change to the value of the variable k1 occurred for the first time in the contract a of the transaction Tx1 in the block in which the transaction Tx1 is located, and therefore, the memory space 122 in the memory of node 1 does not store the state value of the variable k 1. Thus, after determining the state value of the variable k1 that is not cached in the memory, the node 1 may read the key-value pair (k 1: v 1) of the variable k1 from the state database 11, thereby obtaining the current state value v1 of the variable k 1.
Then, step S206 is executed to store the key-value pair (k 1: v 1) of the variable k1 in the top element of the stack 121. That is, before modifying the state value of the variable k1, the key-value pair of the variable k1 before the modification is saved to the stack element (i.e., the current stack top element) corresponding to the contract a in the stack 121, so as to perform rollback of the variable value when the subsequent contract a execution fails.
Thereafter, as shown in the bottom block of fig. 3, step S208 is performed to store the state value of the variable k1 after being modified according to the contract a, that is, va, in the memory. The current state value of the variable k1 is temporarily stored in memory and updated into the state database 11 after all transactions in the block are completed.
After executing the above procedure in contract a, since contract B is called in contract a, execution of contract B is started in node 1, and after execution of contract B is started, step S202 is first executed similarly to the above, and a new element is stacked in stack 121, that is, the current top element corresponds to contract B. Assuming that the write instruction "set k1, vb" to the variable k1 and the write instruction "set k2, vc" to the variable k2 are sequentially included in the contract B, when the write instruction "set k1, vb" is executed, step S204 is performed, similarly to the above, to read the current state value of the variable k 1. The node 1 first determines whether the state value of the variable k1 is cached in the memory, and since the updated state value of the variable k1 (i.e., (k 1: va)) is cached in the memory during the execution of the contract a, the node 1 can read the state value va of the variable k1 from the memory. In step S206, the node 1 stores the key-value pair (k 1: va) of the variable k1 read from the memory into the top element of the stack 121, and then executes step S208 to modify the state value of the variable k1 in the memory into vb according to the write instruction "set k1, vb" in the contract B. When executing to the write instruction set k2, vc, similarly, the current state value of the variable k2 (i.e., v 2) is read, the key-value pair (k 2: v 2) of the variable k2 is stored in the top-of-stack element of the stack 121, before the updated state value of the variable k2 (i.e., vc) according to the instruction "set k2, vc" is stored in the memory space 122 in memory. Fig. 4 shows a schematic diagram of data stored in the memory 12 of the node 1 after executing the contract B. As shown in fig. 4, after executing the contract B, the value of the variable k1 is updated to vb and the value of the variable k2 is updated to vc in the storage space 122 of the memory 12, and at the same time, in the stack 121 in the memory 12, a stack top element corresponding to the contract B in which the key-value pairs (i.e., world states) of k1 and k2 before executing the contract B are stored is added as compared to fig. 3.
After executing the above procedure in contract B, since contract C is called in contract B, and execution of contract C is started, when execution of contract C is started, a new element is first stacked in stack 121 similarly to the above, that is, the current top element corresponds to contract C. Assuming that the contract C sequentially includes a write instruction "set k2, vd" for the variable k2 and a write instruction "set k3, ve" for the variable k3, when the write instruction "set k2, vd" is executed, the key-value pair of k2 (i.e., (k 2: vc)) is read from the storage space 122 in the memory 12 and stored in the top element of the stack 121, similarly to the above, before the state value of the variable k2 in the storage space 122 is modified to vd. When the write instruction "set k3, ve" is executed, similarly, the current state value of the variable k3 (i.e., v 3) is read, wherein, similarly to the above, the state value of the variable k3 is read from the storage space 122 if the state value of the variable k3 has been stored in the storage space 122 when the transaction in the block was previously executed, and the state value of the variable k3 is read from the state database 11 if the state value of the variable k3 is not stored in the storage space 122. Thereafter, the key-value pair (k 3: v 3) of the variable k3 is stored in the top element of the stack 121, and the state value ve modified by the variable k3 is stored in the storage space 122. Fig. 5 shows a schematic diagram of the data stored in the memory 12 of the node 1 after execution of the contract C. As shown in fig. 5, after executing the contract C, the state value of the variable k2 in the storage space 122 is updated to vd, and the state value of the variable k3 is stored to ve, while a stack top element corresponding to the contract C, in which the key-value pairs of k2 and k3 before executing the contract C are stored, is added in the stack 121 in the memory 12 compared to fig. 4.
After the above-described procedure of contract C is performed, since other contracts are not continuously called in contract C, the execution of contract C is ended, in which case step S210 in fig. 2 is performed, the top-stack element of stack 121 is read, and the top-stack element is deleted. In executing contract C, execution failure for contract C may be caused by either: insufficient fuel (GAS) balance for executing the transaction, presence of illegal instructions in the contract, failure to read and write to the storage unit, virtual machine stack overflow, virtual machine memory overflow, transfer failure, failure to read the contract, and the like, and in the case of failure to execute contract C, likewise, execution of contract C is ended, and the top stack element of stack 121 is read and deleted.
In case of a failure of execution of contract C, step S212 in fig. 2 is executed, and the state values of the variables modified by contract C (i.e., variables k2 and k 3) are rolled back in the storage space 122, specifically, the above-mentioned key-value pairs read from the previous stack top elements (i.e., the stack top elements in fig. 5) are restored into the storage space 122, i.e., the variable k2 is rolled back from vd to vc, and the variable k3 is rolled back from ve to v 1.
In the case where the execution of the contract C is successful, step S214 in fig. 2 is executed. In particular, in one embodiment, the data in the top-of-stack element in FIG. 5 may be appended to the current top-of-stack element. Fig. 6 schematically shows a diagram of the stack 121 after the appending. As shown in FIG. 6, the current stack top element of the stack 121 corresponds to contract B according to the contract invocation order in transaction Tx1, and the initial world state to which contract B corresponds includes the values of variables k1, k2, and k 3. That is, although the variable k3 is not directly written in the contract B, since the contract C called by the contract B writes the variable k3, if the contract B fails to execute, the variable k3 modified by the contract C needs to be restored to the value before modification (i.e., v 3) when the world state of the variable in the storage space 122 is rolled back, and therefore, the initial world state of the variable modified by the contract C needs to be merged into the initial world state corresponding to the contract B.
In addition, as shown in fig. 6, two key-value pairs of the variable k2 are included in the top element in the order that represents their corresponding chronological order, that is, the world state of the variable k2 is earlier in the top (k 2: v 2) and the world state of the variable k2 is later in the top (k 2: vc), so that if the execution of the contract B fails, the value of the variable k2 in the storage space 122 is restored to the earliest world state, that is, v 2.
In another embodiment, when the initial world state corresponding to contract C is appended to the initial world state corresponding to contract B, only variables that do not appear in the initial world state corresponding to contract B may be added, i.e., (k 3: v 3) is added only to the top-of-stack element, and (k 2: vc) is not required to be added to the top-of-stack element.
After execution of contract B is complete, processing of the top-of-stack elements and variable world states in storage 122 may proceed similarly as above. After execution of contract A is complete, execution of transaction Tx1 may end, and stack 121 may be deleted from memory 12.
It is to be appreciated that although the above describes a scheme according to an embodiment of the present specification in a stacked data structure, the embodiment of the present specification is not limited thereto. For example, in one embodiment, a data structure such as a sequential table (e.g., linked list, vector table) may be used in embodiments of the present specification to sequentially record the initial world state of each contract-modified variable that is sequentially invoked in a transaction. The sequence table includes, for example, a plurality of sequentially arranged table elements that correspond one-to-one to the contracts already executed in transaction Tx1, and the arrangement order of the plurality of table elements corresponds to the order in which the contracts are invoked in transaction Tx1, so that the contracts corresponding to the respective table elements can be determined according to the arrangement order of the plurality of table elements in the sequence table.
In another embodiment, after the transaction Tx1 is executed, the memory space corresponding to the transaction Tx1 may be allocated in the memory. In executing the contracts invoked sequentially in transaction Tx1, for example, contract A is executed first, subspace 1 is allocated to contract A, and subspace 1 is identified by a predetermined identification as corresponding to contract A. For example, the address or address hash value of the contract a is first stored in the subspace 1 to identify that the subspace 1 corresponds to the contract a, so that subsequently after the execution of the contract a is finished, the subspace corresponding to the contract a can be found according to the contract identification in each subspace, and corresponding operation is performed.
In another embodiment, the predetermined identifier is, for example, a number arranged in a size order, for example, the identifier of the subspace allocated to the contract a is 1, the identifier of the subspace allocated to the contract B is 2, and the identifier of the subspace allocated to the contract C is 3, so that the corresponding relationship between each subspace and each contract can be determined according to the size order of the number.
Fig. 7 illustrates an apparatus 700 for executing an intelligent contract in a blockchain according to an embodiment of the present specification, the apparatus 700 including:
an allocation unit 71 configured to, after starting execution of a first intelligent contract invoked in a transaction, allocate a first subspace corresponding to the first intelligent contract in a storage space corresponding to the transaction;
a reading unit 72 configured to read a current state value of a first variable as a first value of the first variable before modifying the first variable according to the first smart contract;
a storing unit 73 configured to store a first value of the first variable into the first subspace;
a modifying unit 74 configured to modify a state value of the first variable according to the first smart contract;
a reading and deleting unit 75 configured to read and delete the first subspace to obtain the first value of the first variable after the execution of the first smart contract is finished;
a first restoring unit 76 configured to restore the state value of the first variable to the first value in case of a failure of the execution of the first smart contract.
In one embodiment, the first subspace is used to indicate an order of invocation of the first smart contract in the transaction,
the reading and deleting unit 75 is further configured to read and delete the currently latest allocated subspace based on the invocation order of the corresponding smart contracts indicated by the respective subspaces in the storage space.
In an embodiment, the storing unit 73 is further configured to store a first key-value pair of the first variable into the first subspace, wherein the first key-value pair includes the first value.
In one embodiment, a second intelligent contract is invoked in the transaction, the second intelligent contract invoking the first intelligent contract, a second subspace corresponding to the second intelligent contract is further included in the storage space corresponding to the transaction, and the apparatus 700 further includes an adding unit 77 configured to add the first value of the first variable to the second subspace in a predetermined order in the case of successful execution of the first intelligent contract.
In one embodiment, a third intelligent contract is invoked from the first intelligent contract, the third intelligent contract including a modification of the first variable, and the reading and deleting unit 75 includes a reading subunit 751 configured to read at least two values of the first variable from the first subspace, and a retrieving subunit 752 configured to retrieve an oldest stored value from the at least two values based on an order in which the at least two values are arranged in the first subspace.
In one embodiment, the third intelligent contract includes a modification of a second variable, the reading and deleting unit 75 is further configured to read and delete the first subspace to obtain a first value of the second variable, and the apparatus 700 further includes a second restoring unit 78 configured to restore, in the case of a failure in executing the first intelligent contract, the state value of the second variable to the first value of the second variable in the state database.
In one embodiment, the storage space is a sequence table, wherein the allocating unit 71 is further configured to allocate a first table element arranged sequentially with respect to other table elements in the sequence table,
the reading and deleting unit 75 is further configured to read and delete the currently latest allocated table element based on the arrangement order of the respective table elements in the order table.
In one embodiment, the storage space is a stack, wherein the allocating unit 71 is further configured to, stack a first element as a top element in the stack,
the read and delete unit 75 is further configured to read and delete the current top-of-stack element of the stack.
In one embodiment, the reading unit 72 is further configured to read a current state value of a first variable from a memory as the first value of the first variable, and the modifying unit 74 is further configured to modify the state value of the first variable in the memory according to the first intelligent contract.
Another aspect of the present specification provides a computer readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to perform any one of the above methods.
Another aspect of the present specification provides a computing device comprising a memory having stored therein executable code, and a processor that, when executing the executable code, implements any of the methods described above.
According to the scheme for executing the intelligent contract, a data structure such as a stack is used for indicating the calling level of the contract in the transaction, the stack element is used for storing the value of the variable before the contract modification variable is executed, and when the contract calling fails, the world state can be rolled back according to the stack top element, so that the efficiency of executing the contract is improved, and the performance of a block chain is improved.
It is to be understood that the terms "first," "second," and the like, herein are used for descriptive purposes only and not for purposes of limitation, to distinguish between similar concepts.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The foregoing description has been directed to specific embodiments of this disclosure. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing may also be possible or may be advantageous.
It will be further appreciated by those of ordinary skill in the art that the elements and algorithm steps of the examples described in connection with the embodiments disclosed herein may be embodied in electronic hardware, computer software, or combinations of both, and that the components and steps of the examples have been described in a functional general in the foregoing description for the purpose of illustrating clearly the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application. The software modules may reside in Random Access Memory (RAM), memory, Read Only Memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
The above-mentioned embodiments are intended to illustrate the objects, technical solutions and advantages of the present invention in further detail, and it should be understood that the above-mentioned embodiments are merely exemplary embodiments of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (20)

1. A method of executing intelligent contracts in blockchains, the method comprising:
after a first intelligent contract called in a transaction begins to be executed, allocating a first subspace corresponding to the first intelligent contract in a storage space corresponding to the transaction;
reading a current state value of a first variable as a first value of the first variable before modifying the first variable according to the first intelligent contract;
storing a first value of the first variable in the first subspace;
modifying a state value of the first variable according to the first smart contract;
after the execution of the first intelligent contract is finished, reading and deleting the first subspace to obtain a first value of the first variable;
in the event of a failure to execute the first smart contract, restoring the state value of the first variable to the first value;
and the transaction also calls a second intelligent contract which calls the first intelligent contract, the storage space corresponding to the transaction also comprises a second subspace corresponding to the second intelligent contract, and the first value of the first variable is added into the second subspace in a preset sequence when the first intelligent contract is successfully executed.
2. The method of claim 1, wherein the first subspace is used to indicate an order of invocation of the first smart contract in the transaction,
reading and deleting the first subspace comprises reading and deleting a currently latest allocated subspace based on the calling sequence of the corresponding intelligent contracts indicated by the subspaces in the storage space.
3. The method of claim 1, wherein storing a first value of the first variable in the first subspace comprises storing a first key-value pair of the first variable in the first subspace, the first key-value pair comprising the first value.
4. The method of claim 1, wherein invoking a third intelligent contract in the first intelligent contract, the third intelligent contract comprising modifications to the first variable, and reading and deleting the first subspace to obtain the first value of the first variable comprises reading at least two values of the first variable from the first subspace and obtaining an oldest stored value from the at least two values based on an order in which the at least two values are arranged in the first subspace.
5. The method of claim 4, wherein the third intelligent contract includes a modification to a second variable, wherein reading and deleting the first subspace further includes reading and deleting the first subspace to obtain a first value of the second variable, and wherein the method further comprises, in the event of a failure to execute the first intelligent contract, restoring the state value of the second variable to the first value of the second variable.
6. The method of claim 2, wherein the storage space is a sequential table, wherein allocating a first subspace corresponding to the first intelligent contract in the storage space corresponding to the transaction comprises allocating a first table element in the sequential table that is ordered relative to other table elements,
reading and deleting the currently latest allocated subspace includes reading and deleting the currently latest allocated table element based on the arrangement order of the table elements in the order table.
7. The method of claim 2, wherein the storage space is a stack, wherein allocating a first subspace corresponding to the first smart contract in the storage space corresponding to the transaction comprises, stacking a first element as a top-of-stack element in the stack,
reading and deleting the currently latest allocated subspace comprises reading and deleting a current top-of-stack element of the stack.
8. The method of claim 1 or 2, wherein the first variable is an account balance of the blockchain or is a storage variable included in the first smart contract.
9. The method of claim 1, wherein reading the current state value of the first variable as the first value of the first variable comprises reading the current state value of the first variable from memory as the first value of the first variable, and modifying the state value of the first variable according to the first smart contract comprises modifying the state value of the first variable in memory according to the first smart contract.
10. An apparatus for executing intelligent contracts in blockchains, the apparatus comprising:
the allocation unit is configured to allocate a first subspace corresponding to a first intelligent contract in a storage space corresponding to a transaction after the first intelligent contract called in the transaction starts to be executed;
the reading unit is configured to read a current state value of a first variable as a first value of the first variable before the first variable is modified according to the first intelligent contract;
a storing unit configured to store a first value of the first variable into the first subspace;
a modification unit configured to modify a state value of the first variable according to the first smart contract;
a reading and deleting unit configured to read and delete the first subspace to obtain a first value of the first variable after the execution of the first intelligent contract is finished;
a first restoring unit configured to restore the state value of the first variable to the first value in the case of failure in execution of the first smart contract;
the device also comprises an adding unit, wherein the adding unit is configured to add the first value of the first variable into the second subspace according to a preset sequence in the case of successful execution of the first intelligent contract.
11. The apparatus of claim 10, wherein the first subspace is to indicate an order of invocation of the first smart contract in the transaction,
the reading and deleting unit is further configured to read and delete the currently latest allocated subspace based on the calling order of the corresponding smart contracts indicated by the subspaces in the storage space.
12. The apparatus of claim 10, wherein the logging unit is further configured to log a first key-value pair of the first variable into the first subspace, the first key-value pair including the first value.
13. The apparatus of claim 10, wherein a third intelligent contract is invoked in the first intelligent contract, the third intelligent contract including modifications to the first variable, the read and delete unit including, a read subunit configured to read at least two values of the first variable from the first subspace, an obtain subunit configured to obtain an oldest stored value from the at least two values based on an order in which the at least two values are arranged in the first subspace.
14. The apparatus of claim 13, wherein the third intelligent contract includes a modification of a second variable, the read and delete unit is further configured to read and delete the first subspace to obtain the first value of the second variable, the apparatus further comprising a second recovery unit configured to recover the state value of the second variable to the first value of the second variable in the event of a failure to execute the first intelligent contract.
15. The apparatus of claim 11, wherein the storage space is a sequence table, wherein the allocation unit is further configured to allocate a first table element in the sequence table that is ordered relative to other table elements,
the reading and deleting unit is further configured to read and delete a currently latest allocated table element based on an arrangement order of the respective table elements in the order table.
16. The apparatus of claim 10, wherein the storage space is a stack, wherein the allocation unit is further configured to stack a first element in the stack as a top-of-stack element,
the read and delete unit is further configured to read and delete a current top-of-stack element of the stack.
17. The apparatus of claim 10 or 11, wherein the first variable is an account balance of the blockchain or is a storage variable included in the first smart contract.
18. The apparatus according to claim 10, wherein the reading unit is further configured to read a current state value of a first variable from a memory as the first value of the first variable, and the modifying unit is further configured to modify the state value of the first variable in the memory according to the first smart contract.
19. A computer-readable storage medium, on which a computer program is stored which, when executed in a computer, causes the computer to carry out the method of any one of claims 1-9.
20. A computing device comprising a memory having executable code stored therein and a processor that, when executing the executable code, implements the method of any of claims 1-9.
CN202011173786.9A 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain Active CN112001799B (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN202110334950.8A CN112950378B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain
CN202011173786.9A CN112001799B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011173786.9A CN112001799B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain

Related Child Applications (1)

Application Number Title Priority Date Filing Date
CN202110334950.8A Division CN112950378B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain

Publications (2)

Publication Number Publication Date
CN112001799A CN112001799A (en) 2020-11-27
CN112001799B true CN112001799B (en) 2021-02-05

Family

ID=73475168

Family Applications (2)

Application Number Title Priority Date Filing Date
CN202110334950.8A Active CN112950378B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain
CN202011173786.9A Active CN112001799B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain

Family Applications Before (1)

Application Number Title Priority Date Filing Date
CN202110334950.8A Active CN112950378B (en) 2020-10-28 2020-10-28 Method and device for executing intelligent contract in block chain

Country Status (1)

Country Link
CN (2) CN112950378B (en)

Family Cites Families (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10560268B2 (en) * 2017-02-13 2020-02-11 International Business Machines Corporation Node characterization in a blockchain
CN109300036B (en) * 2018-09-14 2020-08-14 百度在线网络技术(北京)有限公司 Bifurcation regression method and device of block chain network
US11138597B2 (en) * 2018-11-27 2021-10-05 Advanced New Technologies Co., Ltd. System and method for improving security of smart contract on blockchain
CN110009362B (en) * 2019-04-02 2021-07-06 北京众享比特科技有限公司 Block chain intelligent contract modification method and device and computer readable storage medium
CN110266655B (en) * 2019-05-30 2021-11-12 中国工商银行股份有限公司 Cross-chain interconnection method, device and system based on block chain
CN110704490A (en) * 2019-10-15 2020-01-17 北京启迪区块链科技发展有限公司 Service data processing method and device based on intelligent contract
CN111080290B (en) * 2019-11-19 2023-05-26 杭州趣链科技有限公司 Block chain transaction execution and data storage method based on account classification
CN111045792A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for caching and executing intelligent contracts
CN111383031B (en) * 2020-05-29 2020-09-22 支付宝(杭州)信息技术有限公司 Intelligent contract execution method and system in block chain and electronic equipment

Also Published As

Publication number Publication date
CN112950378B (en) 2022-06-07
CN112001799A (en) 2020-11-27
CN112950378A (en) 2021-06-11

Similar Documents

Publication Publication Date Title
CN110602148B (en) Method and device for generating state tree of block and verifying data on chain
CN110471795B (en) Block chain state data recovery method and device and electronic equipment
CN109359222B (en) Data storage method and system, equipment and storage medium
US10949118B2 (en) Data storage method, apparatus, and device for multi-layer blockchain-type ledger
US11269902B2 (en) Time series data management method, device, and apparatus
EP3961431B1 (en) Method and apparatus for grouping transactions that need to be serially executed based on a sequence of transactions in one block of a blockchain
CN110597825B (en) Data processing method and device based on block chain and node equipment
EP4310689A1 (en) Data archiving method and apparatus, device, storage medium, and computer program product
CN112037061A (en) Processing method and device for transactions in block chain, electronic equipment and storage medium
CN114780285A (en) Block chain data recovery method and device and electronic equipment
CN111459948B (en) Transaction integrity verification method based on centralized block chain type account book
CN113421073A (en) Method and apparatus for concurrently executing transactions in a blockchain
CN112860412B (en) Service data processing method and device, electronic equipment and storage medium
CN112001799B (en) Method and device for executing intelligent contract in block chain
CN109446258A (en) A kind of distributed data storage method and system
CN103503388A (en) Distributed queue message reading method, device and system
CN103812719B (en) The failure prediction method and device of group system
CN113254467B (en) Method and blockchain node for performing transactions in blockchain system
CN116977067A (en) Block chain-based data processing method, device, equipment and readable storage medium
CN115421856A (en) Data recovery method and device
CN111679902A (en) Intelligent contract calling method and device based on block chain and electronic equipment
CN113190567B (en) Transaction detection method and device
CN117811900A (en) Inference resource recovery method, device, equipment and storage medium of inference platform
CN114064802A (en) Tree block chain generation method and equipment for improving block chain transaction throughput
CN114185896A (en) Data processing method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
REG Reference to a national code

Ref country code: HK

Ref legal event code: DE

Ref document number: 40041124

Country of ref document: HK