Search

US-12619590-B2 - Method and apparatus for publishing smart contract, computer-readable medium,and electronic device

US12619590B2US 12619590 B2US12619590 B2US 12619590B2US-12619590-B2

Abstract

A method for publishing a smart contract includes: generating a version number index value according to a contract name of the smart contract, and searching for an associated target contract version number from a blockchain network according to the version number index value; generating a contract index value according to a contract version number of the smart contract and the contract name, in response to determining that the contract version number of the smart contract is greater than the target contract version number; associating the contract index value with the smart contract and associating the version number index value with the contract version number of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the smart contract to the blockchain network.

Inventors

  • Bo Fu
  • Maocai LI
  • Wei Shi
  • LI KONG
  • Yanxue CHONG
  • Yehui HUANG
  • Jiangwei TI
  • Jiahui CUI

Assignees

  • TENCENT TECHNOLOGY (SHENZHEN) COMPANY LIMITED

Dates

Publication Date
20260505
Application Date
20220914
Priority Date
20200910

Claims (20)

  1. 1 . A method for publishing a smart contract, performed by a client, the method comprising: generating a version number index value according to a contract name of the smart contract, and searching for an associated target contract version number from a blockchain network according to the version number index value, the target contract version number being indicative of a largest-numbered version of the smart contract already recorded in the blockchain network; and in response to determining that the contract version number of the first version of the smart contract to be published is greater than the target contract version number; generating a contract index value according to a contract version number of a first version of the smart contract and the contract name; associating the contract index value with executable contract code of the first version of the smart contract and associating the version number index value with the contract version number of the first version of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the first version of the smart contract to the blockchain network, the publishing the first version of the smart contract to the blockchain network comprising causing the executable contract code of the first version of the smart contract to be recorded in the blockchain network, wherein subsequent to the publishing of the first version of the smart contract to the blockchain network, the blockchain network records a first mapping relationship between the contract index value and the executable contract code of the first version of the smart contract, and a second mapping relationship between the version number index value and the contract version number of the first version of the smart contract, the second mapping relationship comprises a key-value pair entry stored in the blockchain network, the version number index value being a key of the key-value pair entry, the contract version number of the first version of the smart contract being a value of the key-value pair entry.
  2. 2 . The method according to claim 1 , wherein generating the version number index value comprises: performing a hash calculation according to the contract name of the smart contract to obtain a first hash value, and using the calculated first hash value as the version number index value.
  3. 3 . The method according to claim 1 , wherein generating the contract index value comprises: performing a hash calculation according to the contract version number of the first version of the smart contract and the contract name to obtain a second hash value, and using the calculated second hash value as the contract index value.
  4. 4 . The method according to claim 1 , wherein searching for the associated target contract version number comprises: searching for at least one associated contract version number from the blockchain network according to the version number index value; and using a largest contract version number in the at least one contract version number as the target contract version number.
  5. 5 . The method according to claim 1 , further comprising: generating the contract index value according to the contract version number of the first version of the smart contract and the contract name, in response to determining that no associated target contract version number is found from the blockchain network according to the version number index value; associating the contract index value with the executable contract code of the first version of the smart contract and associating the version number index value with the contract version number of the first version of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the first version of the smart contract to the blockchain network.
  6. 6 . The method according to claim 1 , further comprising: terminating publishing of the first version of the smart contract to the blockchain network, in response to determining that the contract version number of the first version of the smart contract is less than or equal to the target contract version number.
  7. 7 . The method according to claim 1 , further comprising: verifying the executable contract code of the first version of the smart contract; and publishing the transaction information to the blockchain network comprises: publishing the transaction information to the blockchain network in response to determining that verification of the executable contract code of the first version of the smart contract succeeds.
  8. 8 . The method according to claim 7 , wherein verifying the executable contract code of the first version of the smart contract comprises: creating a corresponding virtual machine according to a contract type of the smart contract; executing the executable contract code of the first version of the smart contract by the virtual machine; and determining that the verification of the executable contract code succeeds in response to determining that the virtual machine returns a result that the first version of the smart contract is successfully executed after executing the executable contract code of the first version of the smart contract.
  9. 9 . The method according to claim 1 , further comprising: obtaining owner information of the smart contract corresponding to the target contract version number; and performing the process of publishing the transaction information to the blockchain network in response to determining that the owner information matches publisher information of the first version of the smart contract.
  10. 10 . A method for calling a first smart contract, performed by a blockchain node, the method comprising: receiving a contract call request sent by a call requestor, the contract call request including a name of the first smart contract; generating a first contract index value according to the name of the first smart contract and a contract version number corresponding to the contract call request; querying a target smart contract associated with the first contract index value from published smart contracts in a blockchain network according to the first contract index value, wherein the blockchain network records, for each published version of each published smart contract, executable contract code of the published version of the published smart contract and a first mapping relationship between a contract index value and the executable contract code of the published version of the published smart contract, and, for each published smart contract, a second mapping relationship between a version number index value and a contract version number of a published largest-numbered version of the published smart contract already recorded in the blockchain network, the second mapping relationship comprises a key-value pair entry stored in the blockchain network, the version number index value being a key of the key-value pair entry, the contract version number of the published largest-numbered version of the published smart contract being a value of the key-value pair entry; and calling the target smart contract to perform a corresponding contract operation according to a call interface of the target smart contract.
  11. 11 . The method according to claim 10 , further comprising: using a first version number of the first smart contract as the contract version number corresponding to the contract call request, in response to determining that the contract call request includes the first version number of the first smart contract.
  12. 12 . The method according to claim 10 , comprising: generating a first version number index value according to the name of the first smart contract, in response to determining that the contract call request does not include any version number of the first smart contract; and searching for a target contract version number associated with the first version number index value from the blockchain network according to the first version number index value, and using the target contract version number as the contract version number corresponding to the contract call request, the target contract version number being indicative of a largest-numbered version of the first smart contract already recorded in the blockchain network.
  13. 13 . The method according to claim 10 , wherein calling the target smart contract comprises: executing, through the call interface of the target smart contract, executable target contract code corresponding to the call interface in the target smart contract, to perform the contract operation corresponding to the contract call request.
  14. 14 . An apparatus for publishing a smart contract, comprising: a memory storing computer program instructions; and a processor coupled to the memory and configured to execute the computer program instructions and perform: generating a version number index value according to a contract name of the smart contract, and searching for an associated target contract version number from a blockchain network according to the version number index value, the target contract version number being indicative of a largest-numbered version of the smart contract already recorded in the blockchain network; in response to determining that the contract version number of the first version of the smart contract to be published is greater than the target contract version number: generating a contract index value according to a contract version number of a first version of the smart contract and the contract name; associating the contract index value with executable contract code of the first version of the smart contract and associating the version number index value with the contract version number of the first version of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the first version of the smart contract to the blockchain network, the publishing the first version of the smart contract to the blockchain network comprising causing the executable contract code of the first version of the smart contract to be recorded in the blockchain network, wherein subsequent to the publishing of the first version of the smart contract to the blockchain network, the blockchain network records a first mapping relationship between the contract index value and the executable contract code of the first version of the smart contract, and a second mapping relationship between the version number index value and the contract version number of the first version of the smart contract, the second mapping relationship comprises a key-value pair entry stored in the blockchain network, the version number index value being a key of the key-value pair entry, the contract version number of the first version of the smart contract being a value of the key-value pair entry.
  15. 15 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: searching for at least one associated contract version number from the blockchain network according to the version number index value; and using a largest contract version number in the at least one contract version number as the target contract version number.
  16. 16 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: verifying the executable contract code of the first version of the smart contract to be published; and publishing the transaction information to the blockchain network in response to determining that verification of the executable contract code of the first version of the smart contract succeeds.
  17. 17 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: performing a hash calculation according to the contract name of the smart contract to obtain a first hash value, and using the calculated first hash value as the version number index value.
  18. 18 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: performing a hash calculation according to the contract version number of the first version of the smart contract and the contract name to obtain a second hash value, and using the calculated second hash value as the contract index value.
  19. 19 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: terminating publishing of the first version of the smart contract to the blockchain network, in response to determining that the contract version number of the first version of the smart contract is less than or equal to the target contract version number.
  20. 20 . The apparatus according to claim 14 , wherein the processor is further configured to execute the computer program instructions and perform: obtaining owner information of the smart contract corresponding to the target contract version number; and performing the process of publishing the transaction information to the blockchain network in response to determining that the owner information matches publisher information of the first version of the smart contract.

Description

RELATED APPLICATION(S) This application is a continuation application of PCT Patent Application No. PCT/CN2021/107240 filed on Jul. 20, 2021, which claims priority to Chinese Patent Application No. 2020109447660, entitled “METHOD AND APPARATUS FOR PUBLISHING SMART CONTRACT, COMPUTER-READABLE MEDIUM, AND ELECTRONIC DEVICE” and filed with the National Intellectual Property Administration, PRC on Sep. 10, 2020, all of which are incorporated herein by reference in entirety. FIELD OF THE TECHNOLOGY The present disclosure relates to the field of Internet technologies, and particularly, to a method and apparatus for publishing a smart contract, a computer-readable medium, and an electronic device. BACKGROUND A smart contract is executable code stored in a blockchain node, and the code specifies an execution process and processing logic of the smart contract, such as input, output, intermediate transition logic, and the like. To improve on the processing logic of the smart contract, it may be desirable to upgrade the smart contract in use. SUMMARY In one aspect, the present disclosure provides a method for publishing a smart contract, performed by a client, the method including: generating a version number index value according to a contract name of the smart contract, and searching for an associated target contract version number from a blockchain network according to the version number index value; generating a contract index value according to a contract version number of the smart contract and the contract name, when or in response to determining that the contract version number of the smart contract is greater than the target contract version number; associating the contract index value with the smart contract and associating the version number index value with the contract version number of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the smart contract to the blockchain network. In another aspect, the present disclosure provides a method for calling a smart contract, performed by a blockchain node, the method including: receiving a contract call request sent by a call requestor, the contract call request including a name of the smart contract; generating a contract index value according to the name of the smart contract and a contract version number corresponding to the contract call request; querying a target smart contract associated with the contract index value from published smart contracts in a blockchain network according to the contract index value; and calling the target smart contract to perform a corresponding contract operation according to a call interface of the target smart contract. In yet another aspect, the present disclosure provides an apparatus for publishing a smart contract, the apparatus including: a memory storing computer program instructions; and a processor coupled to the memory and configured to execute the computer program instructions and perform: generating a version number index value according to a contract name of the smart contract, and searching for an associated target contract version number from a blockchain network according to the version number index value; generating a contract index value according to a contract version number of the smart contract and the contract name, in response to determining that the contract version number of the smart contract to be published is greater than the target contract version number; associating the contract index value with the smart contract and associating the version number index value with the contract version number of the smart contract, to generate transaction information; and publishing the transaction information to the blockchain network, to publish the smart contract to the blockchain network. In yet another aspect, the present disclosure provides an apparatus for calling a smart contract, the apparatus including: a memory storing computer program instructions; and a processor coupled to the memory and configured to execute the computer program instructions and perform: receiving a contract call request sent by a call requestor, the contract call request including a name of the smart contract; generating a contract index value according to the name of the smart contract and a contract version number corresponding to the contract call request; querying a target smart contract associated with the contract index value from published smart contracts in a blockchain network according to the contract index value; and calling the target smart contract to perform a corresponding contract operation according to a call interface of the target smart contract. In yet another aspect, the present disclosure provides a computer-readable medium, storing a computer-readable instruction. The computer-readable instruction, when executed by a processor, implements the method for publishing a smart contract or the method for calling a smart contra