WO2022077186A1 - Execution method and apparatus for smart contract in blockchain, and electronic device - Google Patents

Execution method and apparatus for smart contract in blockchain, and electronic device Download PDF

Info

Publication number
WO2022077186A1
WO2022077186A1 PCT/CN2020/120487 CN2020120487W WO2022077186A1 WO 2022077186 A1 WO2022077186 A1 WO 2022077186A1 CN 2020120487 W CN2020120487 W CN 2020120487W WO 2022077186 A1 WO2022077186 A1 WO 2022077186A1
Authority
WO
WIPO (PCT)
Prior art keywords
contract
data
logic
address
transaction
Prior art date
Application number
PCT/CN2020/120487
Other languages
French (fr)
Chinese (zh)
Inventor
刘海峰
郭三彬
陈复星
李腾
卓越
雷宇
Original Assignee
北京和联共识科技有限公司
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 北京和联共识科技有限公司 filed Critical 北京和联共识科技有限公司
Priority to CN202080002396.7A priority Critical patent/CN112400182A/en
Priority to PCT/CN2020/120487 priority patent/WO2022077186A1/en
Publication of WO2022077186A1 publication Critical patent/WO2022077186A1/en

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
    • G06Q20/00Payment architectures, schemes or protocols
    • G06Q20/38Payment protocols; Details thereof
    • G06Q20/382Payment protocols; Details thereof insuring higher security of transaction
    • G06Q20/3827Use of message hashing
    • 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

Definitions

  • One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a method and apparatus for executing a smart contract in a blockchain, and an electronic device.
  • Blockchain technology also known as distributed ledger technology, is an emerging technology in which several computing devices jointly participate in "bookkeeping" and jointly maintain a complete distributed database. Due to the characteristics of decentralization, openness and transparency of blockchain technology, each computing device can participate in database records, and data synchronization between computing devices can be performed quickly, blockchain technology has been widely used in many fields. to apply.
  • the embodiments of this specification provide a method, device, and electronic device for creating, upgrading, and executing a smart contract in a blockchain.
  • a method for executing a smart contract in a blockchain comprising:
  • the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
  • the state data is supplemented to the missing state data in the business logic to execute the business logic.
  • the method further includes:
  • the data contract corresponding to the second contract address is called to update the state data of the target field maintained in the contract account corresponding to the data contract, so that the The state data of the target field is modified to new state data.
  • the method further includes:
  • a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
  • the method further includes:
  • the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • a new logic contract is created, and the new business logic is written in the contract account corresponding to the new logic contract;
  • Invoke the routing contract in the blockchain update the mapping table maintained in the contract account corresponding to the routing contract, to modify the first contract address of the old logical contract in the mapping table to the new logical contract 's first contract address.
  • the method further includes:
  • mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table.
  • the business logic maintained in specifically includes:
  • the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
  • If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
  • If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
  • the logic contract corresponding to the first contract address is called, and the business logic maintained in the contract account corresponding to the logic contract is obtained,
  • the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
  • a method for creating a smart contract in a blockchain includes:
  • a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
  • a method for upgrading a smart contract in a blockchain includes:
  • the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • a new logic contract is created, and the new business logic is written in the contract account corresponding to the new logic contract;
  • Invoke the routing contract in the blockchain update the mapping table maintained in the contract account corresponding to the routing contract, to modify the first contract address of the old logical contract in the mapping table to the new logical contract 's first contract address.
  • the method further includes:
  • mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table.
  • a device for executing a smart contract in a blockchain includes:
  • the receiving unit receives the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
  • the response unit in response to the first transaction, invokes the logic contract corresponding to the first contract address, and obtains the business logic maintained in the contract account corresponding to the logic contract;
  • a determining unit determining the field name of the state data lacking in the execution of the business logic
  • a routing unit calling the routing contract of the blockchain, and querying the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
  • an obtaining unit calling the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
  • An execution unit supplementing the state data to the state data missing in the business logic, so as to execute the business logic.
  • the device further includes:
  • the updating unit after executing the business logic to generate the new state data of the target field, invokes the data contract corresponding to the second contract address to update the state data of the target field maintained in the contract account corresponding to the data contract , to modify the state data of the target field to new state data.
  • the device further includes:
  • the first receiving subunit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
  • the first response subunit in response to the second transaction, creates a logic contract, and writes the business logic in the contract account corresponding to the logic contract;
  • the first creation sub-unit creates a data contract, and writes the initial state data of each field in the business logic in the contract account corresponding to the data contract;
  • the first update sub-unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the data contract's address.
  • the second contract address is written into the mapping table.
  • the device further includes:
  • the second receiving subunit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • the second response subunit in response to the third transaction, creates a new logic contract, and writes the new business logic in the contract account corresponding to the new logic contract;
  • the second update subunit invokes the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to The first contract address of the new logical contract.
  • the device further includes:
  • the second creation sub-unit if the new business logic adds a new field compared to the old business logic, a new data contract is created, and the new field is written in the contract account corresponding to the new data contract. initial state data;
  • the third update subunit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the new data contract to the mapping table The mapping relationship of the second contract address.
  • the response unit includes:
  • the first transaction also carries business-related status data
  • call the routing contract of the blockchain and query the mapping table maintained in the contract account corresponding to the routing contract.
  • the second contract address mapped by the first contract address; further call the data contract corresponding to the second contract address to update the status data of the target field maintained in the contract account corresponding to the data contract to record the first Status data carried by the transaction; further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
  • the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
  • the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
  • If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
  • If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
  • the logic contract corresponding to the first contract address is invoked, and the business logic maintained in the contract account corresponding to the logic contract is obtained, including:
  • the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
  • an apparatus for creating a smart contract in a blockchain includes:
  • the receiving unit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
  • a response unit in response to the second transaction, creating a logic contract, and writing the business logic in a contract account corresponding to the logic contract;
  • the creation unit creates a data contract, and writes the initial state data of each field in the business logic into the contract account corresponding to the data contract;
  • An update unit calling the routing contract in the blockchain, and updating the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the second contract of the data contract address is written to the mapping table.
  • a device for upgrading a smart contract in a blockchain includes:
  • the receiving unit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • the response unit in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
  • the upgrading unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one The first contract address of the logical contract.
  • the device further includes:
  • the creation unit if the new business logic adds a new field compared to the old business logic, create a new data contract, and write the initial state data of the new field in the contract account corresponding to the new data contract ;
  • the updating unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the second address of the new data contract to the mapping table.
  • the mapping relationship of contract addresses is a mapping relationship of contract addresses.
  • a smart contract of a blockchain is composed of a logic contract, a routing contract, and a data contract;
  • mapping table is maintained in the contract account corresponding to the routing contract, and the mapping table stores the mapping relationship between the first contract address of the logic contract and the second contract address of the data contract;
  • the state data generated by the execution of the logic contract is maintained in the contract account corresponding to the data contract.
  • an electronic device including:
  • memory for storing processor-executable instructions
  • the processor is configured to execute, create, and upgrade the smart contracts in any of the above-mentioned blockchains.
  • the embodiments of this specification provide a solution for executing, creating, and upgrading smart contracts in the blockchain, by dividing smart contracts into logical contracts, routing contracts, and data contracts.
  • the logic contract that maintains business logic no longer maintains state data, so there are no restrictions on asset security and data migration, making business logic upgradeable and pluggable.
  • the logic contract that writes the new business logic is directly created, and the mapping relationship between the data contract that stores the state data and the old logic contract is modified to the new logic contract by updating the mapping table maintained by the routing contract.
  • FIG. 1 is a schematic diagram of a smart contract of a blockchain provided by an exemplary embodiment
  • FIG. 2 is a flow chart of a method for creating a smart contract in a blockchain provided by an exemplary embodiment
  • FIG. 3 is a flow chart of a method for upgrading a smart contract in a blockchain provided by an exemplary embodiment
  • FIG. 4 is a flow chart of a method for executing a smart contract in a blockchain provided by an exemplary embodiment
  • FIG. 5 is a schematic structural diagram of an electronic device provided by an exemplary embodiment
  • FIG. 6 is a block diagram of an apparatus for executing a smart contract in a blockchain provided by an exemplary embodiment.
  • the steps of the corresponding methods are not necessarily performed in the order shown and described in this specification.
  • the method may include more or fewer steps than described in this specification.
  • a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may also be combined into a single step in other embodiments. describe.
  • the first, the second, the third, etc. in this specification are for distinguishing the difference, rather than limiting the sequence.
  • Blockchains are generally divided into three types: Public Blockchain, Private Blockchain and Consortium Blockchain.
  • Public Blockchain Private Blockchain
  • Consortium Blockchain there can also be a combination of the above-mentioned types, such as private chain + alliance chain, alliance chain + public chain, etc.
  • the public chain is represented by Bitcoin and Ethereum. Participants who join the public chain (also called nodes in the blockchain) can read the data records on the chain, participate in transactions, and compete for the accounting rights of new blocks, etc. . Moreover, each node can freely join or leave the network and perform related operations.
  • the private chain is on the contrary, the write permission of the network is controlled by an organization or institution, and the data read permission is regulated by the organization.
  • a private chain can be a weakly centralized system with strict restrictions on nodes and a small number of nodes. This type of blockchain is more suitable for internal use within a specific institution.
  • the consortium chain is a blockchain between the public chain and the private chain, which can achieve "partial decentralization".
  • Each node in the alliance chain usually has a corresponding entity or organization; the node joins the network through authorization and forms a stakeholder alliance to jointly maintain the operation of the blockchain.
  • the blockchain is usually composed of several blocks. Timestamps corresponding to the creation time of the blocks are respectively recorded in these blocks, and all blocks form a time-ordered data chain strictly according to the timestamps recorded in the blocks.
  • the real data generated in the physical world it can be constructed into a standard transaction format supported by the blockchain, and then published to the blockchain, and the node devices in the blockchain will perform consensus processing on the received transactions , and after reaching a consensus, the node device in the blockchain as the accounting node will package the transaction into the block and store the certificate persistently in the blockchain.
  • Ethereum In the field of blockchain, an important concept is Account; taking Ethereum as an example, Ethereum usually divides accounts into external accounts and contract accounts; external accounts are accounts directly controlled by users, also known as It is a user account; and a contract account is an account (ie, a smart contract) that is created by the user through an external account and contains the contract code.
  • a structure is usually used to maintain the account state of the account.
  • the state of the account associated with that transaction in the blockchain usually changes as well.
  • the structure of an account usually includes fields such as Balance, Nonce, Code, and Storage. in:
  • the Balance field is used to maintain the current account balance of the account
  • the Nonce field is used to maintain the number of transactions of the account; it is a counter used to ensure that each transaction can be processed only once, effectively avoiding replay attacks;
  • the Code field is used to maintain the contract code of the account; in practical applications, the Code field usually only maintains the hash value of the contract code; therefore, the Code field is usually also called the Codehash field.
  • the Storage field is used to maintain the state data of the account (the default field value is empty); for the contract account, an independent storage space is usually allocated to store the state data of the contract account; the independent storage space is usually Call it the account storage for this contract account.
  • the state data of the contract account is usually constructed as a data structure of the MPT (Merkle Patricia Trie) tree and stored in the above-mentioned independent storage space; among them, the MPT tree constructed based on the state data of the contract account is usually also called the Storage tree. .
  • the Storage field usually only maintains the root node of the Storage tree; therefore, the Storage field is usually also called the StorageRoot field.
  • the field values of the Code field and the Storage field shown above are all null values.
  • a smart contract on the blockchain is a contract that can be triggered and executed by a transaction on the blockchain.
  • Smart contracts can be defined in the form of code.
  • EVM Ethereum Virtual Machine
  • bytecode virtual machine code
  • the smart contracts in the existing blockchain are designed to be immutable and non-upgradable.
  • the contract code of the business logic declared in the smart contract cannot be modified.
  • any business logic needs to be changed to adapt to different needs, that is to say, there is a need to modify or upgrade smart contracts in practice.
  • the business side needs to continuously release smart contracts with new business logic.
  • the contract account corresponding to the smart contract not only maintains the contract code of the business logic (Code field), but also maintains stateful data (Storage field).
  • the newly created smart contract can write the contract code of the new business logic, it cannot directly create the state data in the old smart contract. This requires additional operations to obtain, and the operation of synchronizing state data is more complicated.
  • the old smart contract can no longer be used, the maintained state data is no longer updated, but these state data are always recorded, which additionally occupies the resources of the blockchain. With the continuous iterative upgrade of smart contracts, the state data maintained in the old smart contracts will occupy more and more blockchain resources, thus continuously squeezing the available resources of the existing blockchain.
  • the smart contract can be divided into a logic contract, a routing contract and a data contract; wherein:
  • mapping table is maintained in the contract account corresponding to the routing contract, and the mapping table stores the mapping relationship between the first contract address of the logic contract and the second contract address of the data contract;
  • the state data generated by the execution of the logic contract is maintained in the contract account corresponding to the data contract.
  • the logic contract can be upgraded (an embodiment of the logic contract upgrade will be described in detail later), and the data contract can be expanded (an embodiment of the data contract extension will be described in detail later).
  • Users can initiate transactions to logic contracts or data contracts.
  • the logic contract can read and write data to the data contract. Routing contracts are used to provide routing services to logic contracts or data contracts.
  • the method includes:
  • Step 110 Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
  • Step 120 In response to the second transaction, create a logic contract, and write the business logic in a contract account corresponding to the logic contract;
  • Step 130 Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
  • Step 140 Invoke the routing contract in the blockchain, and update the mapping table maintained in the contract account corresponding to the routing contract, so as to link the first contract address corresponding to the logical contract with the second contract of the data contract. address is written to the mapping table.
  • the user can send a transaction including the creation of a smart contract to the blockchain, and the transaction carries the contract code of the business logic.
  • the smart contract can be created.
  • this embodiment not only creates a logic contract for storing business logic contract codes, but also creates a data contract for storing state data.
  • a contract account corresponding to the logical contract appears on the blockchain, and the contract account has a unique contract address (called the first contract address).
  • the contract code of the business logic carried in the transaction will be stored in the Code of the contract account corresponding to the logical contract (in some cases, the Code stores the hash pointer of the contract code, which is addressed to the contract code through the hash pointer).
  • the Storage of the contract account corresponding to the logic contract is empty, that is, the logic contract only maintains business logic and does not store state data.
  • a contract account corresponding to the data contract appears on the blockchain, and the data account has a unique contract address (called the second contract address).
  • the initial state data (usually 0) of each business field is written into the Storage of the contract account corresponding to the data contract.
  • the code of the contract account corresponding to the data contract is empty, that is, the data contract only maintains state data and does not store business logic.
  • the routing contract needs to update the mapping table maintained in its corresponding contract account to map the mapping relationship between the first contract address corresponding to the logical contract and the second contract address of the data contract Write the mapping table.
  • the following further introduces an embodiment of the method for upgrading the smart contract shown in FIG. 3 .
  • the method includes:
  • Step 210 Receive a third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • Step 220 In response to the third transaction, create a new logical contract, and write the new business logic in the contract account corresponding to the new logical contract;
  • Step 230 Invoke the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one.
  • the first contract address of the logical contract is
  • the user can send a transaction including the upgraded smart contract to the blockchain, and the transaction carries the first contract address of the old logic contract and the contract code of the new business logic to be upgraded.
  • the smart contract can be upgraded.
  • a contract account corresponding to the new logical contract appears on the blockchain, and the contract account has a unique contract address (called the first contract address of the new logical contract).
  • the contract code of the new business logic carried in the transaction will be saved in the Code of the contract account corresponding to the new logical contract (in some cases, the Code saves the hash pointer of the contract code, which is addressed to the contract code through the hash pointer) .
  • the Storage of the contract account corresponding to the new logical contract is also empty, that is, the new logical contract only maintains business logic and does not store state data.
  • the mapping table maintained by the routing contract needs to be updated. That is, the first contract address of the old logical contract in the mapping table is modified to the first contract address of the new logical contract, so as to invalidate the connection between the old logical contract and the data contract and establish a new logical contract and Links between data contracts.
  • the logic contract in this manual is modifiable and upgradeable, the data contract adheres to the concept of traditional smart contracts and cannot be modified or upgraded. Once the data contract is deployed on the chain, it cannot be modified, and the data structure (the current business field) will be locked in this data contract. However, in practical applications, if the business logic is iteratively upgraded, the corresponding data structure often also has iterations. For example, the business logic adds some business fields. But data contracts cannot be upgraded. Therefore, the smart contracts in the above-mentioned blockchain still have the problem of data expansion.
  • mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table.
  • the form of the mapping table maintained in the above routing contract may also include:
  • the smart contract is divided into a logical contract, a routing contract and a data contract; the contract account corresponding to the logical contract maintains business logic; the contract account corresponding to the routing contract maintains a mapping table, and the mapping table stores the logic The mapping relationship between the first contract address of the contract and the second contract address of the data contract; the contract account corresponding to the data contract maintains the state data generated by the execution of the logic contract; the method includes:
  • Step 310 Receive the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract to be invoked for business execution;
  • Step 320 In response to the first transaction, call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract;
  • Step 330 Determine the field name of the state data lacking in the execution of the business logic
  • Step 340 Invoke the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
  • Step 350 Invoke the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
  • Step 360 Supplement the state data to the missing state data in the business logic to execute the business logic.
  • the user sends a transaction containing the calling logic contract to the blockchain
  • the From field of the transaction is used to record the address of the user who initiated the calling logic contract (that is, the address of the user's user account on the blockchain)
  • the To field is used to record the contract address of the called logical contract (that is, the first contract address of the logical contract on the blockchain)
  • the Data field of the transaction is used to record the method and parameters for calling the logical contract.
  • the node device in the blockchain executes the transaction in the local EVM, calls the logic contract corresponding to the first contract address, and obtains the contract code of the business logic maintained in the contract account corresponding to the logic contract.
  • the logic contract does not store state data
  • after determining the field name of the state data lacking in the execution of the business logic by calling the routing contract of the blockchain, query the mapping table maintained in the contract account corresponding to the routing contract The second contract address mapped to the first contract address.
  • the data contract corresponding to the second contract address is further called to obtain the status data of the field name maintained in the contract account corresponding to the data contract.
  • the node device obtains the contract code of the business logic and the missing state data in the business logic, supplements the state data in the EVM to the missing state data in the business logic, and runs the business logic contract in the EVM code.
  • the execution of the smart contract obtained in this embodiment obtains the contract code of the business logic from the logic contract, and obtains the data corresponding to the business logic from the routing contract through the mutual cooperation between the logic contract, the routing contract and the data contract. contract, and obtain relevant state data from the data contract, and finally assemble the contract code and state data into a complete executable code to run in the EVM.
  • the data contract corresponding to the second contract address is called to update the state data of the target field maintained in the contract account corresponding to the data contract, so as to convert the The state data of the target field is modified to the new state data.
  • the execution of the logic contract since the second contract address of the corresponding data contract has been obtained before, the data contract corresponding to the second contract address is directly called, and the contract corresponding to the data contract is called directly.
  • the state data of the target field maintained in the account is modified to the new state data.
  • the invocation method of the smart contract shown in Figure 4 above is that the first transaction does not carry business-related status data, and the logical contract can be directly invoked.
  • step 320 specifically includes:
  • Step 321 in response to the first transaction, call the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
  • Step 322 further calling the data contract corresponding to the second contract address, and updating the state data of the target field maintained in the contract account corresponding to the data contract to record the state data carried by the first transaction;
  • Step 323 further calling the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
  • a user's direct transaction of state data such transaction is directly sent to its data contract, and after the state data is recorded by the data contract, the logic contract is invoked to process and record the business logic. Since the original state data (state data specified in the transaction) is not transferred by the logical contract, but is directly recorded by the data contract, and the data contract cannot be modified or upgraded, the original state data directly recorded by the data contract can be Trusted, so as to maximize data security.
  • this specification uses the separation of duties to divide traditional smart contracts into two separate contracts: logical contracts and data contracts.
  • the user assets in the data contract are determined by the mapping relationship maintained by the routing contract, but this mapping relationship can be modified, that is to say, the user assets do not directly correspond to users, which poses the problem of user asset security. For example, map user A's assets to user B.
  • the contract account corresponding to the data contract can also maintain an asset mapping table of the mapping relationship between user accounts and user assets.
  • the mapping table is inside the data contract, and it only provides two interfaces for the user's asset entry and exit, and no other interface has the right to write and update the asset mapping table.
  • the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
  • If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
  • If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
  • step 323 the logic contract corresponding to the first contract address is called to obtain the business logic maintained in the contract account corresponding to the logic contract;
  • the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
  • the above-mentioned user assets may refer to virtual tokens or virtual assets.
  • the virtual tokens may refer to converting real-world currency into virtual tokens that can be circulated on the chain.
  • virtual tokens For example, Bitcoin is a typical virtual token.
  • the virtual assets may refer to converting some non-monetary physical assets in the real world into virtual assets that can be circulated on the blockchain.
  • converting non-monetary physical assets in the real world into virtual assets on the blockchain usually refers to “anchoring" the physical assets with the virtual assets on the blockchain, as The value support of these virtual assets, and then the process of generating virtual assets (hereinafter referred to as user assets) that match the value of physical assets on the blockchain and can be circulated between blockchain accounts on the blockchain.
  • the state data of user assets can be maintained in the contract account corresponding to the data contract, so that non-monetary physical assets in the real world can be used as value support.
  • the field value of the Code field can be null (or not); the field value of the Storage field can no longer be null; the Storage field can be used to maintain and user accounts.
  • the asset status of the corresponding user asset can be flexibly designed based on requirements, such as an asset mapping table of the mapping relationship between the user account and the asset state of the user's assets.
  • the specific form of the above-mentioned user assets can be non-monetary physical assets such as real estate, stocks, loan contracts, bills, accounts receivable, etc., and such physical assets are converted into user assets with matching value and circulated on the blockchain.
  • the user when the user puts in the asset, he directly calls the account entry interface on the data contract. After the user's assets are locked into the data contract, the corresponding relationship between the address of the user's account and the user's assets is recorded in the asset mapping table.
  • the data contract will verify whether the address of the user account exists in the asset mapping table, call the logic contract to perform the next steps if it exists, and finally transfer the user's assets directly to the user's request address.
  • the outbound interface will not respond to its operation request for user assets. This ensures that any user assets that are issued belong to the address of the original user account that was originally entered, which ensures the user's binding of asset ownership and improves the security of user assets.
  • the technical solutions provided in this manual divide smart contracts into logical contracts, routing contracts and data contracts.
  • the logical contracts that maintain business logic no longer maintain state data, so there are no restrictions on asset security and data migration, and smart contracts can be upgraded.
  • the data contract is set to be non-upgradable, but extensible to meet the needs of business logic upgrades.
  • the user account to which each user asset belongs is managed through the asset mapping table maintained within the data contract, and the user assets are managed based on the asset mapping table, so as to completely protect the user assets and not be affected by the contract. Asset security risks due to upgrades.
  • the present specification also provides embodiments of an apparatus for executing, creating and upgrading smart contracts in a blockchain.
  • the embodiments of the apparatus for executing, creating and upgrading smart contracts in the blockchain of this specification can be applied to electronic devices.
  • the apparatus embodiment may be implemented by software, or may be implemented by hardware or a combination of software and hardware. Taking software implementation as an example, a device in a logical sense is formed by reading the corresponding computer program instructions in the non-volatile memory into the memory for operation by the processor of the electronic device where the device is located.
  • FIG. 5 a hardware structure diagram of the electronic equipment where the execution device of the smart contract in the blockchain of this specification (the creation and upgrading device of the smart contract in the blockchain is not shown) is located , in addition to the processor, memory, network interface, and non-volatile memory shown in FIG. 5 , the electronic device in which the device is located in the embodiment may also include other hardware, usually according to the actual function of the electronic device. Repeat.
  • Fig. 6 is a block diagram of a smart contract execution device in a blockchain according to an exemplary embodiment of the present specification.
  • the apparatus can be applied to the aforementioned electronic equipment shown in FIG. 5 , and the apparatus includes:
  • the receiving unit 410 receives the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract to be invoked for service execution;
  • the response unit 420 in response to the first transaction, invokes the logic contract corresponding to the first contract address, and obtains the business logic maintained in the contract account corresponding to the logic contract;
  • Determining unit 430 determining the field name of the state data lacking in the execution of the business logic
  • the routing unit 440 invokes the routing contract of the blockchain to query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
  • the obtaining unit 450 calls the data contract corresponding to the second contract address, and obtains the status data of the field name maintained in the contract account corresponding to the data contract;
  • the execution unit 460 supplements the state data to the missing state data in the business logic, so as to execute the business logic.
  • the device further includes:
  • the updating unit after executing the business logic to generate the new state data of the target field, invokes the data contract corresponding to the second contract address to update the state data of the target field maintained in the contract account corresponding to the data contract , to modify the state data of the target field to new state data.
  • the device further includes:
  • the first receiving subunit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
  • the first response subunit in response to the second transaction, creates a logic contract, and writes the business logic in the contract account corresponding to the logic contract;
  • the first creation sub-unit creates a data contract, and writes the initial state data of each field in the business logic in the contract account corresponding to the data contract;
  • the first update sub-unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the data contract's address.
  • the second contract address is written into the mapping table.
  • the device further includes:
  • the second receiving subunit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • the second response subunit in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
  • the second update subunit invokes the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to The first contract address of the new logical contract.
  • the device further includes:
  • the second creation sub-unit if the new business logic adds a new field compared to the old business logic, a new data contract is created, and the new field is written in the contract account corresponding to the new data contract. initial state data;
  • the third update subunit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the new data contract to the mapping table The mapping relationship of the second contract address.
  • the response unit 420 includes:
  • the first transaction also carries business-related status data
  • call the routing contract of the blockchain and query the mapping table maintained in the contract account corresponding to the routing contract.
  • the second contract address mapped by the first contract address; further call the data contract corresponding to the second contract address to update the status data of the target field maintained in the contract account corresponding to the data contract to record the first Status data carried by the transaction; further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
  • the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
  • the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
  • If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
  • If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
  • the logic contract corresponding to the first contract address is invoked, and the business logic maintained in the contract account corresponding to the logic contract is obtained, including:
  • the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
  • the following describes an apparatus for creating a smart contract in a blockchain according to an exemplary embodiment of this specification.
  • the device can also be applied to the aforementioned electronic equipment shown in FIG. 5 , and the device includes:
  • the receiving unit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
  • a response unit in response to the second transaction, creating a logic contract, and writing the business logic in a contract account corresponding to the logic contract;
  • the creation unit creates a data contract, and writes the initial state data of each field in the business logic into the contract account corresponding to the data contract;
  • An update unit calling the routing contract in the blockchain, and updating the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the second contract of the data contract address is written to the mapping table.
  • the device can also be applied to the aforementioned electronic equipment shown in FIG. 5 , and the device includes:
  • the receiving unit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
  • the response unit in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
  • the upgrading unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one The first contract address of the logical contract.
  • the device further includes:
  • the creation unit if the new business logic adds a new field compared to the old business logic, create a new data contract, and write the initial state data of the new field in the contract account corresponding to the new data contract ;
  • the updating unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the second address of the new data contract to the mapping table.
  • the mapping relationship of contract addresses is a mapping relationship of contract addresses.
  • a typical implementing device is a computer, which may be in the form of a personal computer, laptop computer, cellular phone, camera phone, smart phone, personal digital assistant, media player, navigation device, email sending and receiving device, game control desktop, tablet, wearable device, or a combination of any of these devices.
  • a computer includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
  • processors CPUs
  • input/output interfaces network interfaces
  • memory volatile and non-volatile memory
  • Memory may include forms of non-persistent memory, random access memory (RAM) and/or non-volatile memory in computer readable media, such as read only memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
  • RAM random access memory
  • ROM read only memory
  • flash RAM flash memory
  • Computer-readable media includes both persistent and non-permanent, removable and non-removable media, and storage of information may be implemented by any method or technology.
  • Information may be computer readable instructions, data structures, modules of programs, 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 Disc Read Only Memory (CD-ROM), Digital Versatile Disc (DVD) or other optical storage, Magnetic tape cartridges, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices or any other non-transmission media can be used to store information that can be accessed by computing devices.
  • computer-readable media does not include transitory computer-readable media, such as modulated data signals and carrier waves.
  • first, second, third, etc. may be used in this specification to describe various information, such information should not be limited by these terms. These terms are only used to distinguish the same type of information from each other.
  • first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information without departing from the scope of one or more embodiments of the present specification.
  • word "if” as used herein can be interpreted as "at the time of" or "when” or "in response to determining.”

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Business, Economics & Management (AREA)
  • Accounting & Taxation (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Finance (AREA)
  • Strategic Management (AREA)
  • General Business, Economics & Management (AREA)
  • Computer Security & Cryptography (AREA)
  • Computing Systems (AREA)
  • Data Mining & Analysis (AREA)
  • General Engineering & Computer Science (AREA)
  • Management, Administration, Business Operations System, And Electronic Commerce (AREA)

Abstract

An execution method and apparatus for a smart contract in a blockchain, and an electronic device. The method comprises: receiving a first transaction submitted by a user, wherein the first transaction carries a first contract address of a logic contract needing to be called for service execution (310); in response to the first transaction, calling the logic contract corresponding to the first contract address, and acquiring a service logic maintained in a contract account corresponding to the logic contract (320); determining a field name of status data which is lacking in execution of the service logic (330); calling a routing contract of a blockchain, and querying a second contract address mapped from the first contract address in a mapping table maintained in a contract account corresponding to the routing contract (340); calling a data contract corresponding to the second contract address, and acquiring status data of the field name maintained in a contract account corresponding to the data contract (350); and supplementing the status data, which is lacking in the service logic, with the status data, so as to execute the service logic (360).

Description

区块链中智能合约的执行方法及装置和电子设备Execution method and device and electronic device of smart contract in blockchain 技术领域technical field
本说明书一个或多个实施例涉及区块链技术领域,尤其涉及一种区块链中智能合约的执行方法及装置和电子设备。One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a method and apparatus for executing a smart contract in a blockchain, and an electronic device.
背景技术Background technique
区块链技术,也被称之为分布式账本技术,是一种由若干台计算设备共同参与“记账”,共同维护一份完整的分布式数据库的新兴技术。由于区块链技术具有去中心化、公开透明、每台计算设备可以参与数据库记录、并且各计算设备之间可以快速的进行数据同步的特性,使得区块链技术已在众多的领域中广泛的进行应用。Blockchain technology, also known as distributed ledger technology, is an emerging technology in which several computing devices jointly participate in "bookkeeping" and jointly maintain a complete distributed database. Due to the characteristics of decentralization, openness and transparency of blockchain technology, each computing device can participate in database records, and data synchronization between computing devices can be performed quickly, blockchain technology has been widely used in many fields. to apply.
发明内容SUMMARY OF THE INVENTION
本说明书实施例提供的一种区块链中智能合约的创建、升级、执行方法及装置和电子设备。The embodiments of this specification provide a method, device, and electronic device for creating, upgrading, and executing a smart contract in a blockchain.
根据本说明书实施例的第一方面,提供一种区块链中智能合约的执行方法,所述方法包括:According to a first aspect of the embodiments of this specification, a method for executing a smart contract in a blockchain is provided, the method comprising:
接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;Receive the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;In response to the first transaction, call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract;
确定执行所述业务逻辑缺少的状态数据的字段名称;determining the field name of the state data missing from the execution of the business logic;
调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;Invoke the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;calling the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。The state data is supplemented to the missing state data in the business logic to execute the business logic.
可选的,所述方法还包括:Optionally, the method further includes:
当执行所述业务逻辑产生了目标字段的新状态数据后,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以将所述目标字段的状态数据修改为新状态数据。After executing the business logic to generate the new state data of the target field, the data contract corresponding to the second contract address is called to update the state data of the target field maintained in the contract account corresponding to the data contract, so that the The state data of the target field is modified to new state data.
可选的,所述方法还包括:Optionally, the method further includes:
接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;In response to the second transaction, a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, and write the first contract address corresponding to the logic contract and the second contract address of the data contract the mapping table.
可选的,所述方法还包括:Optionally, the method further includes:
接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一 合约地址和需升级的新业务逻辑;Receive the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;In response to the third transaction, a new logic contract is created, and the new business logic is written in the contract account corresponding to the new logic contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。Invoke the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to modify the first contract address of the old logical contract in the mapping table to the new logical contract 's first contract address.
可选的,所述方法还包括:Optionally, the method further includes:
如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;If the new business logic adds a new field compared to the old business logic, a new data contract is created, and the initial state data of the new field is written in the contract account corresponding to the new data contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table. Mapping relations.
可选的,如果所述第一交易还携带有业务相关的状态数据,所述响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,具体包括:Optionally, if the first transaction also carries business-related status data, in response to the first transaction, call the logical contract corresponding to the first contract address to obtain the contract account corresponding to the logical contract The business logic maintained in , specifically includes:
响应于所述第一交易,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;In response to the first transaction, call the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
进一步调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以记录所述第一交易携带的状态数据;Further call the data contract corresponding to the second contract address, and update the state data of the target field maintained in the contract account corresponding to the data contract, so as to record the state data carried by the first transaction;
进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。Further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
可选的,所述数据合约对应的合约账户维护的状态数据位于资产映射表,所述资产映射表存储了用户账户与用户资产的状态数据;Optionally, the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
所述调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,包括:The invoking the data contract corresponding to the second contract address to update the state data of the target field maintained in the contract account corresponding to the data contract includes:
如果所述第一交易为用户资产的入账交易,则调用所述第二合约地址对应的数据合约,在所述资产映射表中记录该用户账户和用户资产的状态数据;If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
如果所述第一交易为用户资产的出账交易,则调用所述第二合约地址对应的数据合约,校验所述第一交易指示的用户账户是否位于所述资产映射表;If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
所述调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,The logic contract corresponding to the first contract address is called, and the business logic maintained in the contract account corresponding to the logic contract is obtained,
在所述用户账户位于所述资产映射表中时,,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。When the user account is located in the asset mapping table, the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
根据本说明书实施例的第二方面,一种区块链中智能合约的创建方法,所述方法包括:According to a second aspect of the embodiments of this specification, a method for creating a smart contract in a blockchain, the method includes:
接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;In response to the second transaction, a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, and write the first contract address corresponding to the logic contract and the second contract address of the data contract the mapping table.
根据本说明书实施例的第三方面,一种区块链中智能合约的升级方法,所述方法包括:According to a third aspect of the embodiments of this specification, a method for upgrading a smart contract in a blockchain, the method includes:
接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;Receive the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;In response to the third transaction, a new logic contract is created, and the new business logic is written in the contract account corresponding to the new logic contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。Invoke the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to modify the first contract address of the old logical contract in the mapping table to the new logical contract 's first contract address.
可选的,所述方法还包括:Optionally, the method further includes:
如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;If the new business logic adds a new field compared to the old business logic, a new data contract is created, and the initial state data of the new field is written in the contract account corresponding to the new data contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table. Mapping relations.
根据本说明书实施例的第四方面,一种区块链中智能合约的执行装置,所述装置包括:According to a fourth aspect of the embodiments of this specification, a device for executing a smart contract in a blockchain, the device includes:
接收单元,接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;The receiving unit receives the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
响应单元,响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;The response unit, in response to the first transaction, invokes the logic contract corresponding to the first contract address, and obtains the business logic maintained in the contract account corresponding to the logic contract;
确定单元,确定执行所述业务逻辑缺少的状态数据的字段名称;A determining unit, determining the field name of the state data lacking in the execution of the business logic;
路由单元,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;A routing unit, calling the routing contract of the blockchain, and querying the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
获取单元,调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;an obtaining unit, calling the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
执行单元,将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。An execution unit, supplementing the state data to the state data missing in the business logic, so as to execute the business logic.
可选的,所述装置还包括:Optionally, the device further includes:
更新单元,当执行所述业务逻辑产生了目标字段的新状态数据后,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以将所述目标字段的状态数据修改为新状态数据。The updating unit, after executing the business logic to generate the new state data of the target field, invokes the data contract corresponding to the second contract address to update the state data of the target field maintained in the contract account corresponding to the data contract , to modify the state data of the target field to new state data.
可选的,所述装置还包括:Optionally, the device further includes:
第一接收子单元,接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;The first receiving subunit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
第一响应子单元,响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;The first response subunit, in response to the second transaction, creates a logic contract, and writes the business logic in the contract account corresponding to the logic contract;
第一创建子单元,创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;The first creation sub-unit, creates a data contract, and writes the initial state data of each field in the business logic in the contract account corresponding to the data contract;
第一更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。The first update sub-unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the data contract's address. The second contract address is written into the mapping table.
可选的,所述装置还包括:Optionally, the device further includes:
第二接收子单元,接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;The second receiving subunit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
第二响应子单元,响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;The second response subunit, in response to the third transaction, creates a new logic contract, and writes the new business logic in the contract account corresponding to the new logic contract;
第二更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。The second update subunit invokes the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to The first contract address of the new logical contract.
可选的,所述装置还包括:Optionally, the device further includes:
第二创建子单元,如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;The second creation sub-unit, if the new business logic adds a new field compared to the old business logic, a new data contract is created, and the new field is written in the contract account corresponding to the new data contract. initial state data;
第三更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。The third update subunit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the new data contract to the mapping table The mapping relationship of the second contract address.
可选的,所述响应单元,包括:Optionally, the response unit includes:
如果所述第一交易还携带有业务相关的状态数据,响应于所述第一交易,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;进一步调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以记录所述第一交易携带的状态数据;进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。If the first transaction also carries business-related status data, in response to the first transaction, call the routing contract of the blockchain, and query the mapping table maintained in the contract account corresponding to the routing contract. The second contract address mapped by the first contract address; further call the data contract corresponding to the second contract address to update the status data of the target field maintained in the contract account corresponding to the data contract to record the first Status data carried by the transaction; further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
可选的,所述数据合约对应的合约账户维护的状态数据位于资产映射表,所述资产映射表存储了用户账户与用户资产的状态数据;Optionally, the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
所述响应单元中,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,包括:In the response unit, the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
如果所述第一交易为用户资产的入账交易,则调用所述第二合约地址对应的数据合约,在所述资产映射表中记录该用户账户和用户资产的状态数据;If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
如果所述第一交易为用户资产的出账交易,则调用所述第二合约地址对应的数据合约,校验所述第一交易指示的用户账户是否位于所述资产映射表;If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
所述响应单元中,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,包括:In the response unit, the logic contract corresponding to the first contract address is invoked, and the business logic maintained in the contract account corresponding to the logic contract is obtained, including:
在所述用户账户位于所述资产映射表中时,,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。When the user account is located in the asset mapping table, the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
根据本说明书实施例的第五方面,一种区块链中智能合约的创建装置,所述装置包括:According to a fifth aspect of the embodiments of this specification, an apparatus for creating a smart contract in a blockchain, the apparatus includes:
接收单元,接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;The receiving unit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
响应单元,响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;a response unit, in response to the second transaction, creating a logic contract, and writing the business logic in a contract account corresponding to the logic contract;
创建单元,创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;The creation unit creates a data contract, and writes the initial state data of each field in the business logic into the contract account corresponding to the data contract;
更新单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。An update unit, calling the routing contract in the blockchain, and updating the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the second contract of the data contract address is written to the mapping table.
根据本说明书实施例的第六方面,一种区块链中智能合约的升级装置,所述装置包括:According to a sixth aspect of the embodiments of the present specification, a device for upgrading a smart contract in a blockchain, the device includes:
接收单元,接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;The receiving unit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
响应单元,响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;The response unit, in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
升级单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。The upgrading unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one The first contract address of the logical contract.
可选的,所述装置还包括:Optionally, the device further includes:
创建单元,如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;The creation unit, if the new business logic adds a new field compared to the old business logic, create a new data contract, and write the initial state data of the new field in the contract account corresponding to the new data contract ;
更新单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。The updating unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the second address of the new data contract to the mapping table. The mapping relationship of contract addresses.
根据本说明书实施例的第七方面,一种区块链的智能合约,所述智能合约由逻辑合约、路由合约和数据合约构成;According to a seventh aspect of the embodiments of this specification, a smart contract of a blockchain, the smart contract is composed of a logic contract, a routing contract, and a data contract;
所述逻辑合约对应的合约账户中维护有业务逻辑;Business logic is maintained in the contract account corresponding to the logic contract;
所述路由合约对应的合约账户中维护有映射表,所述映射表存储了逻辑合约的第一合约地址与数据合约的第二合约地址的映射关系;A mapping table is maintained in the contract account corresponding to the routing contract, and the mapping table stores the mapping relationship between the first contract address of the logic contract and the second contract address of the data contract;
所述数据合约对应的合约账户中维护有所述逻辑合约执行所产生的状态数据。The state data generated by the execution of the logic contract is maintained in the contract account corresponding to the data contract.
根据本说明书实施例的第八方面,提供一种电子设备,包括:According to an eighth aspect of the embodiments of the present specification, an electronic device is provided, including:
处理器;processor;
用于存储处理器可执行指令的存储器;memory for storing processor-executable instructions;
其中,所述处理器被配置为上述任一项区块链中智能合约的执行、创建、升级方法。Wherein, the processor is configured to execute, create, and upgrade the smart contracts in any of the above-mentioned blockchains.
本说明书实施例,提供了一种区块链中智能合约的执行、创建、升级方案,通过将智能合约分为逻辑合约、路由合约和数据合约。使得维护业务逻辑的逻辑合约不再维护状态数据,因此就不存在资产安全和数据迁移的限制,使得业务逻辑可升级和可插拔。在业务逻辑升级时,直接创建写入新业务逻辑的逻辑合约,并通过更新路由合约维护的映射表,将存储状态数据的数据合约与旧逻辑合约的映射关系修改为该新逻辑合约。The embodiments of this specification provide a solution for executing, creating, and upgrading smart contracts in the blockchain, by dividing smart contracts into logical contracts, routing contracts, and data contracts. The logic contract that maintains business logic no longer maintains state data, so there are no restrictions on asset security and data migration, making business logic upgradeable and pluggable. When the business logic is upgraded, the logic contract that writes the new business logic is directly created, and the mapping relationship between the data contract that stores the state data and the old logic contract is modified to the new logic contract by updating the mapping table maintained by the routing contract.
附图说明Description of drawings
图1是一示例性实施例提供的区块链的智能合约的示意图;1 is a schematic diagram of a smart contract of a blockchain provided by an exemplary embodiment;
图2是一示例性实施例提供的区块链中智能合约的创建方法的流程;2 is a flow chart of a method for creating a smart contract in a blockchain provided by an exemplary embodiment;
图3是一示例性实施例提供的区块链中智能合约的升级方法的流程;3 is a flow chart of a method for upgrading a smart contract in a blockchain provided by an exemplary embodiment;
图4是一示例性实施例提供的区块链中智能合约的执行方法的流程;4 is a flow chart of a method for executing a smart contract in a blockchain provided by an exemplary embodiment;
图5是一示例性实施例提供的电子设备的结构示意图;5 is a schematic structural diagram of an electronic device provided by an exemplary embodiment;
图6是一示例性实施例提供的区块链中智能合约的执行装置的框图。FIG. 6 is a block diagram of an apparatus for executing a smart contract in a blockchain provided by an exemplary embodiment.
具体实施方式Detailed ways
这里将详细地对示例性实施例进行说明,其示例表示在附图中。下面的描述涉及附图时,除非另有表示,不同附图中的相同数字表示相同或相似的要素。以下示例性实施例中所描述的实现方式并不代表与本说明书一个或多个实施例相一致的所有实现方式。相反,它们仅是与如所附权利要求书中所详述的、本说明书一个或多个实施例的一些方面相一致的装置和方法的例子。Exemplary embodiments will be described in detail herein, examples of which are illustrated in the accompanying drawings. Where the following description refers to the drawings, the same numerals in different drawings refer to the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments are not intended to represent all implementations consistent with one or more embodiments of this specification. Rather, they are merely examples of apparatus and methods consistent with some aspects of one or more embodiments of this specification, as recited in the appended claims.
需要说明的是:在其他实施例中并不一定按照本说明书示出和描述的顺序来执行相应方法的步骤。在一些其他实施例中,其方法所包括的步骤可以比本说明书所描述的更多或更少。此外,本说明书中所描述的单个步骤,在其他实施例中可能被分解为多个步骤进行描述;而本说明书中所描述的多个步骤,在其他实施例中也可能被合并为单个步骤进行描述。本说明书中的第一、第二、第三等是为了进行区分不同,而不是对先后顺序进行的限定。It should be noted that: in other embodiments, the steps of the corresponding methods are not necessarily performed in the order shown and described in this specification. In some other embodiments, the method may include more or fewer steps than described in this specification. In addition, a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may also be combined into a single step in other embodiments. describe. The first, the second, the third, etc. in this specification are for distinguishing the difference, rather than limiting the sequence.
为了方便理解,以下首先介绍区块链中的一些技术概念。For the convenience of understanding, the following first introduces some technical concepts in the blockchain.
区块链一般被划分为三种类型:公有链(Public Blockchain),私有链(Private Blockchain)和联盟链(Consortium Blockchain)。此外,还可以有上述多种类型的结合,比如私有链+联盟链、联盟链+公有链等。Blockchains are generally divided into three types: Public Blockchain, Private Blockchain and Consortium Blockchain. In addition, there can also be a combination of the above-mentioned types, such as private chain + alliance chain, alliance chain + public chain, etc.
其中,去中心化程度最高的是公有链。公有链以比特币、以太坊为代表,加入公有链的参与者(也可称为区块链中的节点)可以读取链上的数据记录、参与交易、以及竞争新区块的记账权等。而且,各节点可自由加入或者退出网络,并进行相关操作。Among them, the most decentralized is the public chain. The public chain is represented by Bitcoin and Ethereum. Participants who join the public chain (also called nodes in the blockchain) can read the data records on the chain, participate in transactions, and compete for the accounting rights of new blocks, etc. . Moreover, each node can freely join or leave the network and perform related operations.
私有链则相反,该网络的写入权限由某个组织或者机构控制,数据读取权限受组织规定。简单来说,私有链可以为一个弱中心化系统,其对节点具有严格限制且节点数量较少。这种类型的区块链更适合于特定机构内部使用。The private chain is on the contrary, the write permission of the network is controlled by an organization or institution, and the data read permission is regulated by the organization. In simple terms, a private chain can be a weakly centralized system with strict restrictions on nodes and a small number of nodes. This type of blockchain is more suitable for internal use within a specific institution.
联盟链则是介于公有链以及私有链之间的区块链,可实现“部分去中心化”。联盟链中各个节点通常有与之相对应的实体机构或者组织;节点通过授权加入网络并组成利益相关联盟,共同维护区块链运行。The consortium chain is a blockchain between the public chain and the private chain, which can achieve "partial decentralization". Each node in the alliance chain usually has a corresponding entity or organization; the node joins the network through authorization and forms a stakeholder alliance to jointly maintain the operation of the blockchain.
基于区块链的基本特性,区块链通常是由若干个区块构成。在这些区块中分别记录有与该区块的创建时刻对应的时间戳,所有的区块严格按照区块中记录的时间戳,构成一条在时间上有序的数据链条。Based on the basic characteristics of the blockchain, the blockchain is usually composed of several blocks. Timestamps corresponding to the creation time of the blocks are respectively recorded in these blocks, and all blocks form a time-ordered data chain strictly according to the timestamps recorded in the blocks.
对于物理世界产生的真实数据,可以将其构建成区块链所支持的标准的交易(transaction)格式,然后发布至区块链,由区块链中的节点设备对收到的交易进行共识处理,并在达成共识后,由区块链中作为记账节点的节点设备,将这笔交易打包进区块,在区块链中进行持久化存证。For the real data generated in the physical world, it can be constructed into a standard transaction format supported by the blockchain, and then published to the blockchain, and the node devices in the blockchain will perform consensus processing on the received transactions , and after reaching a consensus, the node device in the blockchain as the accounting node will package the transaction into the block and store the certificate persistently in the blockchain.
在区块链领域,有一个重要的概念就是账户(Account);以以太坊为例,以太坊通常将账户划分为外部账户和合约账户两类;外部账户就是由用户直接控制的账户, 也称之为用户账户;而合约账户则是由用户通过外部账户创建的,包含合约代码的账户(即智能合约)。In the field of blockchain, an important concept is Account; taking Ethereum as an example, Ethereum usually divides accounts into external accounts and contract accounts; external accounts are accounts directly controlled by users, also known as It is a user account; and a contract account is an account (ie, a smart contract) that is created by the user through an external account and contains the contract code.
对于区块链中的账户而言,通常会通过一个结构体,来维护账户的账户状态。当区块中的交易被执行后,区块链中与该交易相关的账户的状态通常也会发生变化。For an account in the blockchain, a structure is usually used to maintain the account state of the account. When a transaction in a block is executed, the state of the account associated with that transaction in the blockchain usually changes as well.
以以太坊为例,账户的结构体通常包括Balance,Nonce,Code和Storage等字段。其中:Taking Ethereum as an example, the structure of an account usually includes fields such as Balance, Nonce, Code, and Storage. in:
Balance字段,用于维护账户目前的账户余额;The Balance field is used to maintain the current account balance of the account;
Nonce字段,用于维护该账户的交易次数;它是用于保障每笔交易能且只能被处理一次的计数器,有效避免重放攻击;The Nonce field is used to maintain the number of transactions of the account; it is a counter used to ensure that each transaction can be processed only once, effectively avoiding replay attacks;
Code字段,用于维护该账户的合约代码;在实际应用中,Code字段中通常仅维护合约代码的hash值;因而,Code字段通常也称之为Codehash字段。The Code field is used to maintain the contract code of the account; in practical applications, the Code field usually only maintains the hash value of the contract code; therefore, the Code field is usually also called the Codehash field.
Storage字段,用于维护该账户的状态数据(默认字段值为空);对于合约账户而言,通常会分配一个独立的存储空间,用以存储该合约账户的状态数据;该独立的存储空间通常称之为该合约账户的账户存储。合约账户的状态数据通常会构建成MPT(Merkle Patricia Trie)树的数据结构存储在上述独立的存储空间之中;其中,基于合约账户的状态数据构建成的MPT树,通常也称之为Storage树。而Storage字段通常仅维护该Storage树的根节点;因此,Storage字段通常也称之为StorageRoot字段。The Storage field is used to maintain the state data of the account (the default field value is empty); for the contract account, an independent storage space is usually allocated to store the state data of the contract account; the independent storage space is usually Call it the account storage for this contract account. The state data of the contract account is usually constructed as a data structure of the MPT (Merkle Patricia Trie) tree and stored in the above-mentioned independent storage space; among them, the MPT tree constructed based on the state data of the contract account is usually also called the Storage tree. . The Storage field usually only maintains the root node of the Storage tree; therefore, the Storage field is usually also called the StorageRoot field.
其中,对于外部账户而言,以上示出的Code字段和Storage字段的字段值均为空值。Wherein, for the external account, the field values of the Code field and the Storage field shown above are all null values.
在实际应用中,不论是公有链、私有链还是联盟链,都可能提供智能合约(Smart contract)的功能。区块链上的智能合约是在区块链上可以被交易触发执行的合约。智能合约可以通过代码的形式定义。In practical applications, whether it is a public chain, a private chain or a consortium chain, it is possible to provide the function of smart contracts. A smart contract on the blockchain is a contract that can be triggered and executed by a transaction on the blockchain. Smart contracts can be defined in the form of code.
以以太坊为例,支持用户在以太坊网络中创建并调用一些复杂的逻辑。以太坊作为一个可编程区块链,其核心是以太坊虚拟机(EVM),每个以太坊节点都可以运行EVM。EVM是一个图灵完备的虚拟机,通过它可以实现各种复杂的逻辑。用户在以太坊中发布和调用智能合约就是在EVM上运行的。实际上,EVM直接运行的是虚拟机代码(虚拟机字节码,下简称“字节码”),所以部署在区块链上的智能合约可以是字节码。Taking Ethereum as an example, users are supported to create and invoke some complex logic in the Ethereum network. As a programmable blockchain, the core of Ethereum is the Ethereum Virtual Machine (EVM), and each Ethereum node can run the EVM. EVM is a Turing-complete virtual machine through which various complex logics can be implemented. Users publish and invoke smart contracts in Ethereum that run on the EVM. In fact, EVM directly runs virtual machine code (virtual machine bytecode, hereinafter referred to as "bytecode"), so smart contracts deployed on the blockchain can be bytecodes.
为了区块链的安全考虑,现有区块链中智能合约被设计为不可修改、不可升级。以以太坊为例,智能合约一旦在区块链中创建后,就无法修改这个智能合约中声明的业务逻辑的合约代码。但是任何业务逻辑都是需要改变以适应不同的需求,也就是说实际中智能合约存在修改或者升级的需求。为此,业务方需要不断发布新业务逻辑的智能合约。然而,这样做存在以下问题:For the security of the blockchain, the smart contracts in the existing blockchain are designed to be immutable and non-upgradable. Taking Ethereum as an example, once a smart contract is created in the blockchain, the contract code of the business logic declared in the smart contract cannot be modified. However, any business logic needs to be changed to adapt to different needs, that is to say, there is a need to modify or upgrade smart contracts in practice. To this end, the business side needs to continuously release smart contracts with new business logic. However, doing so has the following problems:
智能合约对应的合约账户中不仅维护了业务逻辑的合约代码(Code字段),还维护有状态数据(Storage字段)。新创建的智能合约虽然可以写入新业务逻辑的合约代码,但是无法直接创建旧智能合约中的状态数据。而这需要额外的操作来获取,同步状态数据的操作较为复杂。另外,由于旧智能合约虽然可以不再使用,因此维护的状态数据也不再更新,但这些状态数据始终是记录着的,从而额外占用了区块链的资源。随着智能合约的不断迭代升级,旧智能合约中维护的状态数据占用的区块链资源也会越来越多,从而不断挤压现有区块链的可用资源。The contract account corresponding to the smart contract not only maintains the contract code of the business logic (Code field), but also maintains stateful data (Storage field). Although the newly created smart contract can write the contract code of the new business logic, it cannot directly create the state data in the old smart contract. This requires additional operations to obtain, and the operation of synchronizing state data is more complicated. In addition, although the old smart contract can no longer be used, the maintained state data is no longer updated, but these state data are always recorded, which additionally occupies the resources of the blockchain. With the continuous iterative upgrade of smart contracts, the state data maintained in the old smart contracts will occupy more and more blockchain resources, thus continuously squeezing the available resources of the existing blockchain.
为此,本说明书主要是针对传统的智能合约做出改进。请参见图1一示例性实施例提供的区块链的智能合约,所述智能合约可以分为逻辑合约、路由合约和数据合约; 其中:For this reason, this manual is mainly aimed at improving traditional smart contracts. Please refer to the smart contract of the blockchain provided by an exemplary embodiment of FIG. 1, the smart contract can be divided into a logic contract, a routing contract and a data contract; wherein:
所述逻辑合约对应的合约账户中维护有业务逻辑;Business logic is maintained in the contract account corresponding to the logic contract;
所述路由合约对应的合约账户中维护有映射表,所述映射表存储了逻辑合约的第一合约地址与数据合约的第二合约地址的映射关系;A mapping table is maintained in the contract account corresponding to the routing contract, and the mapping table stores the mapping relationship between the first contract address of the logic contract and the second contract address of the data contract;
所述数据合约对应的合约账户中维护有所述逻辑合约执行所产生的状态数据。The state data generated by the execution of the logic contract is maintained in the contract account corresponding to the data contract.
如图1所示,逻辑合约可以升级(后面会详细介绍逻辑合约升级的实施例),数据合约可以扩展(后面会详细介绍数据合约扩展的实施例)。用户可以向逻辑合约或数据合约发起交易。逻辑合约可以向数据合约读写数据。路由合约用于向逻辑合约或数据合约提供路由服务。As shown in Fig. 1, the logic contract can be upgraded (an embodiment of the logic contract upgrade will be described in detail later), and the data contract can be expanded (an embodiment of the data contract extension will be described in detail later). Users can initiate transactions to logic contracts or data contracts. The logic contract can read and write data to the data contract. Routing contracts are used to provide routing services to logic contracts or data contracts.
通过将智能合约分为逻辑合约、路由合约和数据合约。使得维护业务逻辑的逻辑合约不再维护状态数据,因此就不存在资产安全和数据迁移的限制,使得业务逻辑可升级和可插拔。在业务逻辑升级时,直接创建写入新业务逻辑的逻辑合约,并通过更新路由合约维护的映射表,将存储状态数据的数据合约与旧逻辑合约的映射关系修改为该新逻辑合约。By dividing smart contracts into logical contracts, routing contracts and data contracts. The logic contract that maintains business logic no longer maintains state data, so there are no restrictions on asset security and data migration, making business logic upgradeable and pluggable. When the business logic is upgraded, the logic contract that writes the new business logic is directly created, and the mapping relationship between the data contract that stores the state data and the old logic contract is modified to the new logic contract by updating the mapping table maintained by the routing contract.
后续介绍的区块链中智能合约的创建、升级、执行等实施例均在该改进后智能合约架构基础上进行的。The subsequent examples of the creation, upgrade, and execution of smart contracts in the blockchain are all based on the improved smart contract architecture.
以下进一步介绍图2所示智能合约的创建方法的实施例。所述方法包括:The following further describes an embodiment of the method for creating the smart contract shown in FIG. 2 . The method includes:
步骤110:接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;Step 110: Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
步骤120:响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;Step 120: In response to the second transaction, create a logic contract, and write the business logic in a contract account corresponding to the logic contract;
步骤130:创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;Step 130: Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
步骤140:调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。Step 140: Invoke the routing contract in the blockchain, and update the mapping table maintained in the contract account corresponding to the routing contract, so as to link the first contract address corresponding to the logical contract with the second contract of the data contract. address is written to the mapping table.
该实施例中,用户可以将一笔包含创建智能合约的交易发送到区块链,该交易携带有业务逻辑的合约代码。当区块链中的节点间通过共识机制达成一致后,可以开始创建这个智能合约。In this embodiment, the user can send a transaction including the creation of a smart contract to the blockchain, and the transaction carries the contract code of the business logic. When the nodes in the blockchain reach an agreement through a consensus mechanism, the smart contract can be created.
与现有创建一个智能合约不同的是,该实施例除了创建用于存储业务逻辑的合约代码的逻辑合约,还会创建用于存储状态数据的数据合约。Different from the existing creation of a smart contract, this embodiment not only creates a logic contract for storing business logic contract codes, but also creates a data contract for storing state data.
逻辑合约创建后,区块链上出现一个与该逻辑合约对应的合约账户,该合约账户具有一个唯一的合约地址(称为第一合约地址)。交易中携带的业务逻辑的合约代码将保存在该逻辑合约对应的合约账户的Code(在有的情况下,Code保存的是合约代码的hash指针,通过hash指针寻址到合约代码)。在该逻辑合约对应的合约账户的Storage为空,即逻辑合约仅维护业务逻辑,不存储状态数据。After the logical contract is created, a contract account corresponding to the logical contract appears on the blockchain, and the contract account has a unique contract address (called the first contract address). The contract code of the business logic carried in the transaction will be stored in the Code of the contract account corresponding to the logical contract (in some cases, the Code stores the hash pointer of the contract code, which is addressed to the contract code through the hash pointer). The Storage of the contract account corresponding to the logic contract is empty, that is, the logic contract only maintains business logic and does not store state data.
数据合约创建后,区块链上出现一个与该数据合约对应的合约账户,该数据账户具有一个唯一的合约地址(称为第二合约地址)。该数据合约对应的合约账户的Storage中写入各个业务字段的初始状态数据(一般为0)。在该数据合约对应的合约账户的Code为空,即数据合约仅维护状态数据,不存储业务逻辑。After the data contract is created, a contract account corresponding to the data contract appears on the blockchain, and the data account has a unique contract address (called the second contract address). The initial state data (usually 0) of each business field is written into the Storage of the contract account corresponding to the data contract. The code of the contract account corresponding to the data contract is empty, that is, the data contract only maintains state data and does not store business logic.
在逻辑合约和数据合约都创建后,路由合约需要更新其对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址的映射关系写入所述映射表。After both the logical contract and the data contract are created, the routing contract needs to update the mapping table maintained in its corresponding contract account to map the mapping relationship between the first contract address corresponding to the logical contract and the second contract address of the data contract Write the mapping table.
以下进一步介绍图3所示智能合约的升级方法的实施例。所述方法包括:The following further introduces an embodiment of the method for upgrading the smart contract shown in FIG. 3 . The method includes:
步骤210:接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;Step 210: Receive a third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
步骤220:响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;Step 220: In response to the third transaction, create a new logical contract, and write the new business logic in the contract account corresponding to the new logical contract;
步骤230:调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。Step 230: Invoke the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one. The first contract address of the logical contract.
该实施例中,用户可以将一笔包含升级智能合约的交易发送到区块链,该交易携带有旧逻辑合约的第一合约地址和需升级的新业务逻辑的合约代码。当区块链中的节点间通过共识机制达成一致后,可以开始升级智能合约。In this embodiment, the user can send a transaction including the upgraded smart contract to the blockchain, and the transaction carries the first contract address of the old logic contract and the contract code of the new business logic to be upgraded. When the nodes in the blockchain reach an agreement through a consensus mechanism, the smart contract can be upgraded.
与现有升级智能合约不同的是,由于该实施例中逻辑合约不再维护状态数据,因此无需同步旧合约维护的状态数据。Different from the existing upgraded smart contract, since the logic contract in this embodiment no longer maintains state data, there is no need to synchronize the state data maintained by the old contract.
新的逻辑合约创建后,区块链上出现一个与该新的逻辑合约对应的合约账户,该合约账户具有一个唯一的合约地址(称为新的逻辑合约的第一合约地址)。交易中携带的新业务逻辑的合约代码将保存在该新的逻辑合约对应的合约账户的Code(在有的情况下,Code保存的是合约代码的hash指针,通过hash指针寻址到合约代码)。在该新的逻辑合约对应的合约账户的Storage同样为空,即新的逻辑合约仅维护业务逻辑,不存储状态数据。After a new logical contract is created, a contract account corresponding to the new logical contract appears on the blockchain, and the contract account has a unique contract address (called the first contract address of the new logical contract). The contract code of the new business logic carried in the transaction will be saved in the Code of the contract account corresponding to the new logical contract (in some cases, the Code saves the hash pointer of the contract code, which is addressed to the contract code through the hash pointer) . The Storage of the contract account corresponding to the new logical contract is also empty, that is, the new logical contract only maintains business logic and does not store state data.
由于状态数据是存储在合约数据中的,因此无需同步状态至新的逻辑合约。为了新的逻辑合约可以获取所需的状态数据,需要更新路由合约维护的映射表。即,将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址,以失效旧的逻辑合约与数据合约之间联系以及建立新的逻辑合约与数据合约之间联系。Since the state data is stored in the contract data, there is no need to synchronize the state to the new logical contract. In order for the new logic contract to obtain the required state data, the mapping table maintained by the routing contract needs to be updated. That is, the first contract address of the old logical contract in the mapping table is modified to the first contract address of the new logical contract, so as to invalidate the connection between the old logical contract and the data contract and establish a new logical contract and Links between data contracts.
本说明书中逻辑合约虽然是可修改和升级的,但数据合约却秉承传统智能合约的理念,是不可修改、不可升级的。这个数据合约一旦部署到链上即不可修改,数据结构(当前的业务字段)会锁定在这个数据合约中。然而,在实际应用中,如果业务逻辑迭代升级,对应的数据结构往往也存在迭代。例如业务逻辑新增了一些业务字段。但是数据合约是不可以升级的。因此,上述区块链中智能合约还存在数据扩展的问题。Although the logic contract in this manual is modifiable and upgradeable, the data contract adheres to the concept of traditional smart contracts and cannot be modified or upgraded. Once the data contract is deployed on the chain, it cannot be modified, and the data structure (the current business field) will be locked in this data contract. However, in practical applications, if the business logic is iteratively upgraded, the corresponding data structure often also has iterations. For example, the business logic adds some business fields. But data contracts cannot be upgraded. Therefore, the smart contracts in the above-mentioned blockchain still have the problem of data expansion.
针对这一问题,本说明书还提出了用于数据扩展的方案,在图3基础上,还可以包括:In response to this problem, this specification also proposes a solution for data expansion. On the basis of Figure 3, it can also include:
如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;If the new business logic adds a new field compared to the old business logic, a new data contract is created, and the initial state data of the new field is written in the contract account corresponding to the new data contract;
调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to add the first contract address and the second contract address of the new data contract in the mapping table. Mapping relations.
该实施例中,如果业务逻辑上需要添加新字段,这些字段会被存储到一个全新的数据合约中。同时,这个新的数据合约的第二合约地址和内含的字段名称,会被更新到路由合约的映射表中,逻辑合约通过查询映射表,获取新字段对应的数据合约的第二 合约地址,从而路由进行读写。In this embodiment, if business logic needs to add new fields, these fields will be stored in a brand new data contract. At the same time, the second contract address and the included field names of the new data contract will be updated to the mapping table of the routing contract. The logic contract obtains the second contract address of the data contract corresponding to the new field by querying the mapping table. Thereby routing reads and writes.
需要说明的是,对于逻辑合约映射多个数据合约的情况,为了提高查询效率,上述路由合约中维护的映射表的形式还可以包括:It should be noted that, for the case where the logical contract maps multiple data contracts, in order to improve the query efficiency, the form of the mapping table maintained in the above routing contract may also include:
逻辑合约的第一合约地址与各个字段的第一映射关系,以及,各个字段再与所属数据合约的第二合约地址的第二映射关系。The first mapping relationship between the first contract address of the logical contract and each field, and the second mapping relationship between each field and the second contract address of the data contract to which it belongs.
如此,通过两层映射关系,可以最终确定逻辑合约执行所需的每个字段对应的状态数据。In this way, through the two-layer mapping relationship, the state data corresponding to each field required for the execution of the logical contract can be finally determined.
以下进一步介绍图4所示智能合约的执行方法的实施例。所述智能合约分为逻辑合约、路由合约和数据合约;所述逻辑合约对应的合约账户中维护有业务逻辑;所述路由合约对应的合约账户中维护有映射表,所述映射表存储了逻辑合约的第一合约地址与数据合约的第二合约地址的映射关系;所述数据合约对应的合约账户中维护有所述逻辑合约执行所产生的状态数据;所述方法包括:The following further introduces an embodiment of the execution method of the smart contract shown in FIG. 4 . The smart contract is divided into a logical contract, a routing contract and a data contract; the contract account corresponding to the logical contract maintains business logic; the contract account corresponding to the routing contract maintains a mapping table, and the mapping table stores the logic The mapping relationship between the first contract address of the contract and the second contract address of the data contract; the contract account corresponding to the data contract maintains the state data generated by the execution of the logic contract; the method includes:
步骤310:接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;Step 310: Receive the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract to be invoked for business execution;
步骤320:响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;Step 320: In response to the first transaction, call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract;
步骤330:确定执行所述业务逻辑缺少的状态数据的字段名称;Step 330: Determine the field name of the state data lacking in the execution of the business logic;
步骤340:调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;Step 340: Invoke the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
步骤350:调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;Step 350: Invoke the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
步骤360:将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。Step 360: Supplement the state data to the missing state data in the business logic to execute the business logic.
该实施例中,用户将一笔包含调用逻辑合约的交易发送到以区块链,交易的From字段用于记录发起调用逻辑合约的用户地址(即用户在区块链上的用户账户的地址),To字段用于记录被调用的逻辑合约的合约地址(即逻辑合约在区块链上的第一合约地址),交易的Data字段用于记录调用逻辑合约的方法和参数。In this embodiment, the user sends a transaction containing the calling logic contract to the blockchain, and the From field of the transaction is used to record the address of the user who initiated the calling logic contract (that is, the address of the user's user account on the blockchain) , the To field is used to record the contract address of the called logical contract (that is, the first contract address of the logical contract on the blockchain), and the Data field of the transaction is used to record the method and parameters for calling the logical contract.
由区块链中的节点设备在本地的EVM中执行这笔交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑的合约代码。The node device in the blockchain executes the transaction in the local EVM, calls the logic contract corresponding to the first contract address, and obtains the contract code of the business logic maintained in the contract account corresponding to the logic contract.
因为逻辑合约不存储状态数据,在确定执行所述业务逻辑缺少的状态数据的字段名称后,通过调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址。Because the logic contract does not store state data, after determining the field name of the state data lacking in the execution of the business logic, by calling the routing contract of the blockchain, query the mapping table maintained in the contract account corresponding to the routing contract The second contract address mapped to the first contract address.
进一步调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据。The data contract corresponding to the second contract address is further called to obtain the status data of the field name maintained in the contract account corresponding to the data contract.
此时节点设备就获取到业务逻辑的合约代码和该业务逻辑中缺少的状态数据,在EVM中将所述状态数据补充到所述业务逻辑中缺少的状态数据,在EVM中运行业务逻辑的合约代码。At this time, the node device obtains the contract code of the business logic and the missing state data in the business logic, supplements the state data in the EVM to the missing state data in the business logic, and runs the business logic contract in the EVM code.
有上述过程可知,该实施例提供的智能合约的执行,通过逻辑合约、路由合约和数据合约之间的相互配合,从逻辑合约获取业务逻辑的合约代码,从路由合约获取该业务逻辑对应的数据合约,并从数据合约获取相关的状态数据,最后将合约代码与状态 数据组装为完整的可执行代码以在EVM中运行。As can be seen from the above process, the execution of the smart contract provided in this embodiment obtains the contract code of the business logic from the logic contract, and obtains the data corresponding to the business logic from the routing contract through the mutual cooperation between the logic contract, the routing contract and the data contract. contract, and obtain relevant state data from the data contract, and finally assemble the contract code and state data into a complete executable code to run in the EVM.
在实际应用中,调用智能合约后,相应的状态数据会改变,因此需要更新状态数据。因此,在上述图4基础上,还可以包括:In practical applications, after calling the smart contract, the corresponding state data will change, so the state data needs to be updated. Therefore, on the basis of the above Figure 4, it can also include:
当执行业务逻辑产生了目标字段的新状态数据后,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以将所述目标字段的状态数据修改为新状态数据。After the execution of the business logic generates new state data of the target field, the data contract corresponding to the second contract address is called to update the state data of the target field maintained in the contract account corresponding to the data contract, so as to convert the The state data of the target field is modified to the new state data.
该实施例中,当逻辑合约执行产生了新状态数据后,由于之前已经获取到对应的数据合约的第二合约地址,因此直接调用第二合约地址对应的数据合约,将该数据合约对应的合约账户中维护的目标字段的状态数据修改为新状态数据。In this embodiment, after the execution of the logic contract generates new state data, since the second contract address of the corresponding data contract has been obtained before, the data contract corresponding to the second contract address is directly called, and the contract corresponding to the data contract is called directly. The state data of the target field maintained in the account is modified to the new state data.
上述图4所示的智能合约的调用方式,是针对第一交易中未携带业务相关的状态数据,可以直接调用逻辑合约。The invocation method of the smart contract shown in Figure 4 above is that the first transaction does not carry business-related status data, and the logical contract can be directly invoked.
而如果所述第一交易还携带有业务相关的状态数据;则上述步骤320,具体包括:And if the first transaction also carries service-related status data; the above step 320 specifically includes:
步骤321,响应于所述第一交易,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;Step 321, in response to the first transaction, call the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
步骤322,进一步调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以记录所述第一交易携带的状态数据;Step 322, further calling the data contract corresponding to the second contract address, and updating the state data of the target field maintained in the contract account corresponding to the data contract to record the state data carried by the first transaction;
步骤323,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。Step 323, further calling the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
该实施例中,针对用户直接对状态数据的交易,这样的交易直接发往其数据合约,由数据合约记录状态数据后,再调用逻辑合约,处理和记录业务逻辑。由于原始的状态数据(交易中指定的状态数据)未经逻辑合约中转,而是直接由数据合约记录,而数据合约是不可修改不可升级的,因此由数据合约直接记录的原始的状态数据是可信的,从而最大可能保障数据安全。In this embodiment, for a user's direct transaction of state data, such transaction is directly sent to its data contract, and after the state data is recorded by the data contract, the logic contract is invoked to process and record the business logic. Since the original state data (state data specified in the transaction) is not transferred by the logical contract, but is directly recorded by the data contract, and the data contract cannot be modified or upgraded, the original state data directly recorded by the data contract can be Trusted, so as to maximize data security.
针对本说明书,如果逻辑合约可升级,数据合约可扩展,那么随之而来的是,用户的数据所有权和资产安全的问题。For this manual, if the logic contract can be upgraded and the data contract can be expanded, then there will be the issues of user data ownership and asset security.
对于传统智能合约,用户的所有资产都被锁定在合约里。智能合约提取资产或修改资产余额的功能,也通过代码公开的形式向用户保证,除了用户自己,没有其它任何人或程序可以获取用户锁定在合约中的资产。更进一步地,传统智能合约的不可修改性使得合约一旦部署就不会有代码的变动,从而可以确保智能合约中维护的用户资产是绝对安全的。For traditional smart contracts, all of the user's assets are locked in the contract. The function of the smart contract to extract assets or modify the asset balance also assures the user through code disclosure that no other person or program can obtain the user's assets locked in the contract except the user himself. Furthermore, the immutability of traditional smart contracts ensures that once the contract is deployed, there will be no code changes, thus ensuring that the user assets maintained in the smart contract are absolutely safe.
然而,本说明书采用职责分离的方式将传统智能合约分为逻辑合约和数据合约两个独立的合约。数据合约中的用户资产是由路由合约维护的映射关系确定所属的用户,但是这个映射关系是可以修改的,也就是说用户资产并非直接对应用户,这就存在用户资产安全的问题。例如,将用户A的资产映射给用户B。However, this specification uses the separation of duties to divide traditional smart contracts into two separate contracts: logical contracts and data contracts. The user assets in the data contract are determined by the mapping relationship maintained by the routing contract, but this mapping relationship can be modified, that is to say, the user assets do not directly correspond to users, which poses the problem of user asset security. For example, map user A's assets to user B.
为此,数据合约对应的合约账户还可以维护一个用户账户与用户资产之间映射关系的资产映射表。该映射表在数据合约内部,只提供给用户资产的入账和出账两个接口,其它任何接口都无权写入和更新该资产映射表。To this end, the contract account corresponding to the data contract can also maintain an asset mapping table of the mapping relationship between user accounts and user assets. The mapping table is inside the data contract, and it only provides two interfaces for the user's asset entry and exit, and no other interface has the right to write and update the asset mapping table.
上述步骤322中,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,包括:In the above step 322, the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
如果所述第一交易为用户资产的入账交易,则调用所述第二合约地址对应的数据合约,在所述资产映射表中记录该用户账户和用户资产的状态数据;If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
如果所述第一交易为用户资产的出账交易,则调用所述第二合约地址对应的数据合约,校验所述第一交易指示的用户账户是否位于所述资产映射表;If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
上述步骤323中,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;In the above step 323, the logic contract corresponding to the first contract address is called to obtain the business logic maintained in the contract account corresponding to the logic contract;
在所述用户账户位于所述资产映射表中时,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。When the user account is located in the asset mapping table, the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
其中,上述用户资产可以是指虚拟代币,或者虚拟资产。The above-mentioned user assets may refer to virtual tokens or virtual assets.
所述虚拟代币可以是指,将现实世界的货币转换为能够在链上流通的虚拟代币。例如比特币就是典型的虚拟代币。The virtual tokens may refer to converting real-world currency into virtual tokens that can be circulated on the chain. For example, Bitcoin is a typical virtual token.
所述虚拟资产可以是指,将现实世界中的一些非货币属性的实体资产,转化成为能够在区块链上流通的虚拟资产。The virtual assets may refer to converting some non-monetary physical assets in the real world into virtual assets that can be circulated on the blockchain.
其中,需要说明的是,将现实世界中的非货币属性的实体资产转化为区块链上的虚拟资产,通常是指将该实体资产与区块链上的虚拟资产进行“锚定”,作为这些虚拟资产的价值支撑,进而在区块链上产生与实体资产的价值匹配,且能够在区块链上的区块链账户之间进行流通的虚拟资产(以下称为用户资产)的过程。Among them, it should be noted that converting non-monetary physical assets in the real world into virtual assets on the blockchain usually refers to "anchoring" the physical assets with the virtual assets on the blockchain, as The value support of these virtual assets, and then the process of generating virtual assets (hereinafter referred to as user assets) that match the value of physical assets on the blockchain and can be circulated between blockchain accounts on the blockchain.
在实现时,可以在数据合约对应的合约账户中维护用户资产的状态数据,从而为可以将现实世界中的非货币属性的实体资产作为价值支撑。During implementation, the state data of user assets can be maintained in the contract account corresponding to the data contract, so that non-monetary physical assets in the real world can be used as value support.
具体地,数据合约的合约账户的Nonce,Code字段的字段值可以为空值(也可以不为空);而Storage字段的字段值可以不再是空值;Storage字段可以用于维护与用户账户对应的用户资产的资产状态。其中,Storage字段中维护与用户资产的资产状态的具体方式,可以基于需求灵活的进行设计,例如用户账户与用户资产的资产状态之间映射关系的资产映射表。Specifically, the Nonce of the contract account of the data contract, the field value of the Code field can be null (or not); the field value of the Storage field can no longer be null; the Storage field can be used to maintain and user accounts. The asset status of the corresponding user asset. Among them, the specific method of maintaining the asset state of the user's assets in the Storage field can be flexibly designed based on requirements, such as an asset mapping table of the mapping relationship between the user account and the asset state of the user's assets.
其中,上述用户资产的具体形式可以例如房产、股票、贷款合同、票据、应收账款等非货币属性的实体资产,将这样的实体资产转换为价值匹配的用户资产在区块链上流通。The specific form of the above-mentioned user assets can be non-monetary physical assets such as real estate, stocks, loan contracts, bills, accounts receivable, etc., and such physical assets are converted into user assets with matching value and circulated on the blockchain.
回到该实施例,用户在放入资产时,是直接调用数据合约上的入账接口。用户资产锁入数据合约后,在资产映射表中记录用户账户的地址和该用户资产的对应关系。Returning to this embodiment, when the user puts in the asset, he directly calls the account entry interface on the data contract. After the user's assets are locked into the data contract, the corresponding relationship between the address of the user's account and the user's assets is recorded in the asset mapping table.
用户在取回资产时,仍然是直接调用数据合约上的出账接口。数据合约将校验用户账户的地址是否存在于资产映射表中,在存在时调用逻辑合约执行后续步骤,最后把用户资产直接转账给用户的请求地址。When users retrieve assets, they still directly call the outgoing interface on the data contract. The data contract will verify whether the address of the user account exists in the asset mapping table, call the logic contract to perform the next steps if it exists, and finally transfer the user's assets directly to the user's request address.
任何不在资产映射表中的用户账户,出账接口将不响应其对用户资产的操作请求。如此保证了任何一份出账的用户资产,都属于当初入账的原始用户账户的地址,确保了用户对资产所有权的绑定,提升了用户资产的安全。For any user account that is not in the asset mapping table, the outbound interface will not respond to its operation request for user assets. This ensures that any user assets that are issued belong to the address of the original user account that was originally entered, which ensures the user's binding of asset ownership and improves the security of user assets.
综上所述,本说明书提供的技术方案,通过将智能合约分为逻辑合约、路由合约和数据合约。使得维护业务逻辑的逻辑合约不再维护状态数据,因此就不存在资产安全和数据迁移的限制,实现了智能合约可升级。另一方面,数据合约设为不可升级,但可扩展,从而满足业务逻辑升级的需求。再一方面,通过数据合约内部维护的资产映射表来管理每笔用户资产所属的用户账户,基于该资产映射表来做用户资产的出入账管理,从而将用户资产完整地保护起来,不因合约升级而产生资产安全风险。To sum up, the technical solutions provided in this manual divide smart contracts into logical contracts, routing contracts and data contracts. The logical contracts that maintain business logic no longer maintain state data, so there are no restrictions on asset security and data migration, and smart contracts can be upgraded. On the other hand, the data contract is set to be non-upgradable, but extensible to meet the needs of business logic upgrades. On the other hand, the user account to which each user asset belongs is managed through the asset mapping table maintained within the data contract, and the user assets are managed based on the asset mapping table, so as to completely protect the user assets and not be affected by the contract. Asset security risks due to upgrades.
与上述方法实施例相对应,本说明书还提供了区块链中智能合约的执行、创建和升级装置的实施例。Corresponding to the above method embodiments, the present specification also provides embodiments of an apparatus for executing, creating and upgrading smart contracts in a blockchain.
本说明书的区块链中智能合约的执行、创建和升级装置的实施例可以应用在电子设备上。装置实施例可以通过软件实现,也可以通过硬件或者软硬件结合的方式实现。以软件实现为例,作为一个逻辑意义上的装置,是通过其所在电子设备的处理器将非易失性存储器中对应的计算机程序指令读取到内存中运行形成的。The embodiments of the apparatus for executing, creating and upgrading smart contracts in the blockchain of this specification can be applied to electronic devices. The apparatus embodiment may be implemented by software, or may be implemented by hardware or a combination of software and hardware. Taking software implementation as an example, a device in a logical sense is formed by reading the corresponding computer program instructions in the non-volatile memory into the memory for operation by the processor of the electronic device where the device is located.
从硬件层面而言,如图5所示,为本说明书的区块链中智能合约的执行装置(区块链中智能合约的创建、升级装置未示出)所在电子设备的一种硬件结构图,除了图5所示的处理器、内存、网络接口、以及非易失性存储器之外,实施例中装置所在的电子设备通常根据该电子设备的实际功能,还可以包括其他硬件,对此不再赘述。From the perspective of hardware, as shown in Figure 5, a hardware structure diagram of the electronic equipment where the execution device of the smart contract in the blockchain of this specification (the creation and upgrading device of the smart contract in the blockchain is not shown) is located , in addition to the processor, memory, network interface, and non-volatile memory shown in FIG. 5 , the electronic device in which the device is located in the embodiment may also include other hardware, usually according to the actual function of the electronic device. Repeat.
图6是本说明书一示例性实施例示出的一种区块链中智能合约的执行装置的框图。该装置可以应用在前述图5所示的电子设备中,所述装置包括:Fig. 6 is a block diagram of a smart contract execution device in a blockchain according to an exemplary embodiment of the present specification. The apparatus can be applied to the aforementioned electronic equipment shown in FIG. 5 , and the apparatus includes:
接收单元410,接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;The receiving unit 410 receives the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract to be invoked for service execution;
响应单元420,响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;The response unit 420, in response to the first transaction, invokes the logic contract corresponding to the first contract address, and obtains the business logic maintained in the contract account corresponding to the logic contract;
确定单元430,确定执行所述业务逻辑缺少的状态数据的字段名称;Determining unit 430, determining the field name of the state data lacking in the execution of the business logic;
路由单元440,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;The routing unit 440 invokes the routing contract of the blockchain to query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
获取单元450,调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;The obtaining unit 450 calls the data contract corresponding to the second contract address, and obtains the status data of the field name maintained in the contract account corresponding to the data contract;
执行单元460,将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。The execution unit 460 supplements the state data to the missing state data in the business logic, so as to execute the business logic.
可选的,所述装置还包括:Optionally, the device further includes:
更新单元,当执行所述业务逻辑产生了目标字段的新状态数据后,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以将所述目标字段的状态数据修改为新状态数据。The updating unit, after executing the business logic to generate the new state data of the target field, invokes the data contract corresponding to the second contract address to update the state data of the target field maintained in the contract account corresponding to the data contract , to modify the state data of the target field to new state data.
可选的,所述装置还包括:Optionally, the device further includes:
第一接收子单元,接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;The first receiving subunit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
第一响应子单元,响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;The first response subunit, in response to the second transaction, creates a logic contract, and writes the business logic in the contract account corresponding to the logic contract;
第一创建子单元,创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;The first creation sub-unit, creates a data contract, and writes the initial state data of each field in the business logic in the contract account corresponding to the data contract;
第一更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。The first update sub-unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the data contract's address. The second contract address is written into the mapping table.
可选的,所述装置还包括:Optionally, the device further includes:
第二接收子单元,接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;The second receiving subunit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
第二响应子单元,响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑 合约对应的合约账户中写入所述新业务逻辑;The second response subunit, in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
第二更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。The second update subunit invokes the routing contract in the blockchain to update the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to The first contract address of the new logical contract.
可选的,所述装置还包括:Optionally, the device further includes:
第二创建子单元,如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;The second creation sub-unit, if the new business logic adds a new field compared to the old business logic, a new data contract is created, and the new field is written in the contract account corresponding to the new data contract. initial state data;
第三更新子单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。The third update subunit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the new data contract to the mapping table The mapping relationship of the second contract address.
可选的,所述响应单元420,包括:Optionally, the response unit 420 includes:
如果所述第一交易还携带有业务相关的状态数据,响应于所述第一交易,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;进一步调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以记录所述第一交易携带的状态数据;进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。If the first transaction also carries business-related status data, in response to the first transaction, call the routing contract of the blockchain, and query the mapping table maintained in the contract account corresponding to the routing contract. The second contract address mapped by the first contract address; further call the data contract corresponding to the second contract address to update the status data of the target field maintained in the contract account corresponding to the data contract to record the first Status data carried by the transaction; further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
可选的,所述数据合约对应的合约账户维护的状态数据位于资产映射表,所述资产映射表存储了用户账户与用户资产的状态数据;Optionally, the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
所述响应单元420中,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,包括:In the response unit 420, the data contract corresponding to the second contract address is called to update the status data of the target field maintained in the contract account corresponding to the data contract, including:
如果所述第一交易为用户资产的入账交易,则调用所述第二合约地址对应的数据合约,在所述资产映射表中记录该用户账户和用户资产的状态数据;If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
如果所述第一交易为用户资产的出账交易,则调用所述第二合约地址对应的数据合约,校验所述第一交易指示的用户账户是否位于所述资产映射表;If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
所述响应单元420中,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,包括:In the response unit 420, the logic contract corresponding to the first contract address is invoked, and the business logic maintained in the contract account corresponding to the logic contract is obtained, including:
在所述用户账户位于所述资产映射表中时,,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。When the user account is located in the asset mapping table, the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
以下介绍本说明书一示例性实施例示出的一种区块链中智能合约的创建装置。该装置同样可以应用在前述图5所示的电子设备中,所述装置包括:The following describes an apparatus for creating a smart contract in a blockchain according to an exemplary embodiment of this specification. The device can also be applied to the aforementioned electronic equipment shown in FIG. 5 , and the device includes:
接收单元,接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;The receiving unit receives the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
响应单元,响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;a response unit, in response to the second transaction, creating a logic contract, and writing the business logic in a contract account corresponding to the logic contract;
创建单元,创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;The creation unit creates a data contract, and writes the initial state data of each field in the business logic into the contract account corresponding to the data contract;
更新单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地 址写入所述映射表。An update unit, calling the routing contract in the blockchain, and updating the mapping table maintained in the contract account corresponding to the routing contract, so as to convert the first contract address corresponding to the logical contract to the second contract of the data contract address is written to the mapping table.
以下介绍本说明书一示例性实施例示出的一种区块链中智能合约的升级装置。该装置同样可以应用在前述图5所示的电子设备中,所述装置包括:An apparatus for upgrading a smart contract in a blockchain shown in an exemplary embodiment of this specification is described below. The device can also be applied to the aforementioned electronic equipment shown in FIG. 5 , and the device includes:
接收单元,接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;The receiving unit receives the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
响应单元,响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;The response unit, in response to the third transaction, creates a new logical contract, and writes the new business logic in the contract account corresponding to the new logical contract;
升级单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。The upgrading unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to modify the first contract address of the old logical contract in the mapping table to the new one The first contract address of the logical contract.
可选的,所述装置还包括:Optionally, the device further includes:
创建单元,如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;The creation unit, if the new business logic adds a new field compared to the old business logic, create a new data contract, and write the initial state data of the new field in the contract account corresponding to the new data contract ;
更新单元,调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。The updating unit calls the routing contract in the blockchain, and updates the mapping table maintained in the contract account corresponding to the routing contract, so as to add the first contract address and the second address of the new data contract to the mapping table. The mapping relationship of contract addresses.
上述实施例阐明的系统、装置、模块或单元,具体可以由计算机芯片或实体实现,或者由具有某种功能的产品来实现。一种典型的实现设备为计算机,计算机的具体形式可以是个人计算机、膝上型计算机、蜂窝电话、相机电话、智能电话、个人数字助理、媒体播放器、导航设备、电子邮件收发设备、游戏控制台、平板计算机、可穿戴设备或者这些设备中的任意几种设备的组合。The systems, devices, modules or units described in the above embodiments may be specifically implemented by computer chips or entities, or by products with certain functions. A typical implementing device is a computer, which may be in the form of a personal computer, laptop computer, cellular phone, camera phone, smart phone, personal digital assistant, media player, navigation device, email sending and receiving device, game control desktop, tablet, wearable device, or a combination of any of these devices.
在一个典型的配置中,计算机包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。In a typical configuration, a computer includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。内存是计算机可读介质的示例。Memory may include forms of non-persistent memory, random access memory (RAM) and/or non-volatile memory in computer readable media, such as read only memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带、磁盘存储、量子存储器、基于石墨烯的存储介质或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括暂存电脑可读媒体(transitory media),如调制的数据信号和载波。Computer-readable media includes both persistent and non-permanent, removable and non-removable media, and storage of information may be implemented by any method or technology. Information may be computer readable instructions, data structures, modules of programs, 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 Disc Read Only Memory (CD-ROM), Digital Versatile Disc (DVD) or other optical storage, Magnetic tape cartridges, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices or any other non-transmission media can be used to store information that can be accessed by computing devices. As defined herein, computer-readable media does not include transitory computer-readable media, such as modulated data signals and carrier waves.
还需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、商品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、商品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、商品或者设备中还存在另外的相同要素。It should also be noted that the terms "comprising", "comprising" or any other variation thereof are intended to encompass a non-exclusive inclusion such that a process, method, article or device comprising a series of elements includes not only those elements, but also Other elements not expressly listed, or which are inherent to such a process, method, article of manufacture, or apparatus are also included. Without further limitation, an element qualified by the phrase "comprising a..." does not preclude the presence of additional identical elements in the process, method, article of manufacture, or device that includes the element.
上述对本说明书特定实施例进行了描述。其它实施例在所附权利要求书的范围 内。在一些情况下,在权利要求书中记载的动作或步骤可以按照不同于实施例中的顺序来执行并且仍然可以实现期望的结果。另外,在附图中描绘的过程不一定要求示出的特定顺序或者连续顺序才能实现期望的结果。在某些实现方式中,多任务处理和并行处理也是可以的或者可能是有利的。The foregoing describes specific embodiments of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that in the embodiments and still achieve desirable results. Additionally, the processes depicted in the figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some implementations, multitasking and parallel processing are also possible or may be advantageous.
在本说明书一个或多个实施例使用的术语是仅仅出于描述特定实施例的目的,而非旨在限制本说明书一个或多个实施例。在本说明书一个或多个实施例和所附权利要求书中所使用的单数形式的“一种”、“所述”和“该”也旨在包括多数形式,除非上下文清楚地表示其他含义。还应当理解,本文中使用的术语“和/或”是指并包含一个或多个相关联的列出项目的任何或所有可能组合。The terminology used in one or more embodiments of this specification is for the purpose of describing a particular embodiment only and is not intended to limit the one or more embodiments of this specification. As used in the specification or embodiments and the appended claims, the singular forms "a," "the," and "the" are intended to include the plural forms as well, unless the context clearly dictates otherwise. It will also be understood that the term "and/or" as used herein refers to and includes any and all possible combinations of one or more of the associated listed items.
应当理解,尽管在本说明书一个或多个实施例可能采用术语第一、第二、第三等来描述各种信息,但这些信息不应限于这些术语。这些术语仅用来将同一类型的信息彼此区分开。例如,在不脱离本说明书一个或多个实施例范围的情况下,第一信息也可以被称为第二信息,类似地,第二信息也可以被称为第一信息。取决于语境,如在此所使用的词语“如果”可以被解释成为“在……时”或“当……时”或“响应于确定”。It will be understood that although the terms first, second, third, etc. may be used in this specification to describe various information, such information should not be limited by these terms. These terms are only used to distinguish the same type of information from each other. For example, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information without departing from the scope of one or more embodiments of the present specification. Depending on the context, the word "if" as used herein can be interpreted as "at the time of" or "when" or "in response to determining."
以上所述仅为本说明书一个或多个实施例的较佳实施例而已,并不用以限制本说明书一个或多个实施例,凡在本说明书一个或多个实施例的精神和原则之内,所做的任何修改、等同替换、改进等,均应包含在本说明书一个或多个实施例保护的范围之内。The above descriptions are only preferred embodiments of one or more embodiments of this specification, and are not intended to limit one or more embodiments of this specification. All within the spirit and principles of one or more embodiments of this specification, Any modifications, equivalent replacements, improvements, etc. made should be included within the protection scope of one or more embodiments of this specification.

Claims (10)

  1. 一种区块链中智能合约的执行方法,其特征在于,所述方法包括:A method for executing a smart contract in a blockchain, wherein the method comprises:
    接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;Receive the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
    响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;In response to the first transaction, call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract;
    确定执行所述业务逻辑缺少的状态数据的字段名称;determining the field name of the state data missing from the execution of the business logic;
    调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;Invoke the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
    调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;calling the data contract corresponding to the second contract address to obtain the status data of the field name maintained in the contract account corresponding to the data contract;
    将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。The state data is supplemented to the missing state data in the business logic to execute the business logic.
  2. 根据权利要求1所述的方法,其特征在于,所述方法还包括:The method according to claim 1, wherein the method further comprises:
    当执行所述业务逻辑产生了目标字段的新状态数据后,调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以将所述目标字段的状态数据修改为新状态数据。After executing the business logic to generate the new state data of the target field, the data contract corresponding to the second contract address is called to update the state data of the target field maintained in the contract account corresponding to the data contract, so that the The state data of the target field is modified to new state data.
  3. 根据权利要求1所述的方法,其特征在于,所述方法还包括:The method according to claim 1, wherein the method further comprises:
    接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
    响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;In response to the second transaction, a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
    创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
    调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, and write the first contract address corresponding to the logic contract and the second contract address of the data contract the mapping table.
  4. 根据权利要求1所述的方法,其特征在于,所述方法还包括:The method according to claim 1, wherein the method further comprises:
    接收用户提交的第三交易;其中,所述第三交易携带有待升级的旧逻辑合约的第一合约地址和需升级的新业务逻辑;Receive the third transaction submitted by the user; wherein, the third transaction carries the first contract address of the old logic contract to be upgraded and the new business logic to be upgraded;
    响应于所述第三交易,创建新的逻辑合约,在所述新的逻辑合约对应的合约账户中写入所述新业务逻辑;In response to the third transaction, a new logic contract is created, and the new business logic is written in the contract account corresponding to the new logic contract;
    调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述映射表中旧的逻辑合约的第一合约地址修改为所述新的逻辑合约的第一合约地址。Invoke the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, to modify the first contract address of the old logical contract in the mapping table to the new logical contract The first contract address of .
  5. 根据权利要求4所述的方法,其特征在于,所述方法还包括:The method according to claim 4, wherein the method further comprises:
    如果所述新业务逻辑相比旧业务逻辑增加了新的字段,则创建新的数据合约,在所述新的数据合约对应的合约账户中写入所述新的字段的初始状态数据;If the new business logic adds a new field compared to the old business logic, a new data contract is created, and the initial state data of the new field is written in the contract account corresponding to the new data contract;
    调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以在所述映射表中增加第一合约地址与该新的数据合约的第二合约地址的映射关系。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, and add the first contract address and the second contract address of the new data contract to the mapping table. Mapping relations.
  6. 根据权利要求1所述的方法,其特征在于,如果所述第一交易还携带有业务相关的状态数据,所述响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,具体包括:The method according to claim 1, wherein if the first transaction also carries business-related status data, the logic contract corresponding to the first contract address is invoked in response to the first transaction, Obtain the business logic maintained in the contract account corresponding to the logic contract, including:
    响应于所述第一交易,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;In response to the first transaction, call the routing contract of the blockchain, and query the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
    进一步调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,以记录所述第一交易携带的状态数据;Further call the data contract corresponding to the second contract address, and update the state data of the target field maintained in the contract account corresponding to the data contract, so as to record the state data carried by the first transaction;
    进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户 中维护的业务逻辑。Further call the logic contract corresponding to the first contract address to obtain the business logic maintained in the contract account corresponding to the logic contract.
  7. 根据权利要求6所述的方法,其特征在于,所述数据合约对应的合约账户维护的状态数据位于资产映射表,所述资产映射表存储了用户账户与用户资产的状态数据;The method according to claim 6, wherein the state data maintained by the contract account corresponding to the data contract is located in an asset mapping table, and the asset mapping table stores the state data of user accounts and user assets;
    所述调用所述第二合约地址对应的数据合约,更新所述数据合约对应的合约账户中维护的所述目标字段的状态数据,包括:The invoking the data contract corresponding to the second contract address and updating the state data of the target field maintained in the contract account corresponding to the data contract includes:
    如果所述第一交易为用户资产的入账交易,则调用所述第二合约地址对应的数据合约,在所述资产映射表中记录该用户账户和用户资产的状态数据;If the first transaction is a credit transaction of user assets, call the data contract corresponding to the second contract address, and record the status data of the user account and user assets in the asset mapping table;
    如果所述第一交易为用户资产的出账交易,则调用所述第二合约地址对应的数据合约,校验所述第一交易指示的用户账户是否位于所述资产映射表;If the first transaction is an outgoing transaction of user assets, call the data contract corresponding to the second contract address to verify whether the user account indicated by the first transaction is located in the asset mapping table;
    所述调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑,The logic contract corresponding to the first contract address is called, and the business logic maintained in the contract account corresponding to the logic contract is obtained,
    在所述用户账户位于所述资产映射表中时,,进一步调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑。When the user account is located in the asset mapping table, the logic contract corresponding to the first contract address is further invoked to obtain business logic maintained in the contract account corresponding to the logic contract.
  8. 一种区块链中智能合约的创建方法,其特征在于,所述方法包括:A method for creating a smart contract in a blockchain, the method comprising:
    接收用户提交的第二交易;其中,所述第二交易携带有待创建智能合约的业务逻辑;Receive the second transaction submitted by the user; wherein, the second transaction carries the business logic of the smart contract to be created;
    响应于所述第二交易,创建逻辑合约,在所述逻辑合约对应的合约账户中写入所述业务逻辑;In response to the second transaction, a logic contract is created, and the business logic is written in a contract account corresponding to the logic contract;
    创建数据合约,在所述数据合约对应的合约账户中写入所述业务逻辑中各字段的初始状态数据;Create a data contract, and write the initial state data of each field in the business logic into the contract account corresponding to the data contract;
    调用所述区块链中的路由合约,更新所述路由合约对应的合约账户中维护的映射表,以将所述逻辑合约对应的第一合约地址和所述数据合约的第二合约地址写入所述映射表。Call the routing contract in the blockchain, update the mapping table maintained in the contract account corresponding to the routing contract, and write the first contract address corresponding to the logic contract and the second contract address of the data contract the mapping table.
  9. 一种区块链中智能合约的执行装置,其特征在于,所述装置包括:A device for executing a smart contract in a blockchain, characterized in that the device comprises:
    接收单元,接收用户提交的第一交易;其中,所述第一交易携带有业务执行所需调用的逻辑合约的第一合约地址;The receiving unit receives the first transaction submitted by the user; wherein, the first transaction carries the first contract address of the logical contract that needs to be invoked for business execution;
    响应单元,响应于所述第一交易,调用所述第一合约地址对应的逻辑合约,获取所述逻辑合约对应的合约账户中维护的业务逻辑;The response unit, in response to the first transaction, invokes the logic contract corresponding to the first contract address, and obtains the business logic maintained in the contract account corresponding to the logic contract;
    确定单元,确定执行所述业务逻辑缺少的状态数据的字段名称;A determining unit, determining the field name of the state data lacking in the execution of the business logic;
    路由单元,调用所述区块链的路由合约,查询所述路由合约对应的合约账户中维护的映射表中所述第一合约地址映射的第二合约地址;a routing unit, calling the routing contract of the blockchain, and querying the second contract address mapped to the first contract address in the mapping table maintained in the contract account corresponding to the routing contract;
    获取单元,调用所述第二合约地址对应的数据合约,获取所述数据合约对应的合约账户中维护的所述字段名称的状态数据;an obtaining unit, calling the data contract corresponding to the second contract address, and obtaining the status data of the field name maintained in the contract account corresponding to the data contract;
    执行单元,将所述状态数据补充到所述业务逻辑中缺少的状态数据,以执行所述业务逻辑。An execution unit, supplementing the state data to the state data missing in the business logic, so as to execute the business logic.
  10. 一种电子设备,其特征在于,包括:An electronic device, comprising:
    处理器;processor;
    用于存储处理器可执行指令的存储器;memory for storing processor-executable instructions;
    其中,所述处理器通过运行所述可执行指令以实现如权利要求1-8中任一项所述的方法。Wherein, the processor implements the method of any one of claims 1-8 by executing the executable instructions.
PCT/CN2020/120487 2020-10-12 2020-10-12 Execution method and apparatus for smart contract in blockchain, and electronic device WO2022077186A1 (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN202080002396.7A CN112400182A (en) 2020-10-12 2020-10-12 Method and device for executing intelligent contracts in block chain and electronic equipment
PCT/CN2020/120487 WO2022077186A1 (en) 2020-10-12 2020-10-12 Execution method and apparatus for smart contract in blockchain, and electronic device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2020/120487 WO2022077186A1 (en) 2020-10-12 2020-10-12 Execution method and apparatus for smart contract in blockchain, and electronic device

Publications (1)

Publication Number Publication Date
WO2022077186A1 true WO2022077186A1 (en) 2022-04-21

Family

ID=74624922

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/120487 WO2022077186A1 (en) 2020-10-12 2020-10-12 Execution method and apparatus for smart contract in blockchain, and electronic device

Country Status (2)

Country Link
CN (1) CN112400182A (en)
WO (1) WO2022077186A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117726343A (en) * 2024-02-06 2024-03-19 中国信息通信研究院 Method, device, equipment and medium for supervising execution of intelligent contracts in block chain

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112819634B (en) * 2021-02-25 2023-06-02 成都质数斯达克科技有限公司 Transaction processing method, device, electronic equipment and readable storage medium
CN113220701B (en) * 2021-05-25 2023-05-02 成都佰纳瑞信息技术有限公司 Block chain-based data structure self-adaptive change upgrading method, device, equipment and product
CN113486114B (en) * 2021-06-30 2023-03-28 电子科技大学 Block chain intelligent contract calling management method
CN115203747A (en) * 2022-07-29 2022-10-18 蚂蚁区块链科技(上海)有限公司 Data account creation method and device

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109151031A (en) * 2018-08-28 2019-01-04 北京奇虎科技有限公司 Contract processing method and processing device based on block chain
CN109559229A (en) * 2018-11-30 2019-04-02 众安信息技术服务有限公司 The method of the intelligent contract group of update based on block chain
CN110933163A (en) * 2019-11-27 2020-03-27 腾讯科技(深圳)有限公司 Block chain contract deployment method, device, equipment and storage medium
US20200202355A1 (en) * 2019-04-19 2020-06-25 Alibaba Group Holding Limited Storage and execution of smart contracts in blockchains

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107145521B (en) * 2017-04-10 2019-05-21 杭州趣链科技有限公司 A kind of data migration method towards block chain multistage intelligent contract
CN109658238B (en) * 2018-10-26 2020-06-16 阿里巴巴集团控股有限公司 Data processing method and device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109151031A (en) * 2018-08-28 2019-01-04 北京奇虎科技有限公司 Contract processing method and processing device based on block chain
CN109559229A (en) * 2018-11-30 2019-04-02 众安信息技术服务有限公司 The method of the intelligent contract group of update based on block chain
US20200202355A1 (en) * 2019-04-19 2020-06-25 Alibaba Group Holding Limited Storage and execution of smart contracts in blockchains
CN110933163A (en) * 2019-11-27 2020-03-27 腾讯科技(深圳)有限公司 Block chain contract deployment method, device, equipment and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
YUAN LI XIAOM YU YU [THE FORCE LITTLE FISH]: "Technology Planning Framework", FOR TUBETECHNICAL ARTICLE SERIES, CN, pages 1 - 5, XP009536302 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117726343A (en) * 2024-02-06 2024-03-19 中国信息通信研究院 Method, device, equipment and medium for supervising execution of intelligent contracts in block chain

Also Published As

Publication number Publication date
CN112400182A (en) 2021-02-23

Similar Documents

Publication Publication Date Title
WO2022077186A1 (en) Execution method and apparatus for smart contract in blockchain, and electronic device
WO2021017436A1 (en) Blockchain state data synchronization method and apparatus, and electronic device
TWI737944B (en) Block chain-based transaction execution method and device, and electronic equipment
TWI735820B (en) Asset management method and device, electronic equipment
WO2021042809A1 (en) Asset purchase method and apparatus based on blockchain, and electronic device
CN111898139B (en) Data reading and writing method and device and electronic equipment
CN110458631B (en) Bill number distribution method and device based on block chain and electronic equipment
CN110032599B (en) Data structure reading and updating method and device, and electronic equipment
CN110032598B (en) Method and device for updating field and electronic equipment
US11361054B2 (en) Blockchain-based infringement detection method, apparatus, and electronic device
US11429983B2 (en) Blockchain-based bill write-off method, apparatus, electronic device, and storage medium
WO2021017439A1 (en) Block chain-based electronic bill number application method and apparatus, and electronic device
WO2021017437A1 (en) Blockchain-based note verification method and apparatus, electronic device, and storage medium
US20200169402A1 (en) Data storage on tree nodes
US11775507B2 (en) Methods and apparatuses for reading and updating data structures, and electronic devices
US10789628B2 (en) Blockchain-based bill number allocation method, apparatus and electronic device
US20200279309A1 (en) Blockchain-based electronic bill cancellation method, apparatus, and electronic device
WO2024021417A1 (en) Data account creation method and apparatus
WO2022206209A1 (en) Blockchain-based asset management
WO2021218778A1 (en) User recommendation based on blockchain
CN115203746A (en) Data account access authorization method and device
US20230073883A1 (en) Multi-blockchain token rebalancer
CN115221559A (en) Data account access authorization method and device
CN115098886A (en) Access authorization method and device of intelligent contract
CN115640321A (en) Data query method and device

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20956968

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20956968

Country of ref document: EP

Kind code of ref document: A1