Method and device for generating contract interface class of smart contract file
By generating contract interface classes, including decoding, calling and encoding codes, the problem of low efficiency in building smart contract files is solved, efficient interaction between smart contracts and external data is achieved, and the development process is simplified.
Patent Information
- Application Number
- CN202110957073.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-19
- Publication Date
- 2024-12-24
- Estimated Expiration
- 2041-08-19
AI Technical Summary
The existing technology is inefficient when building contract files for smart contracts, and requires complete writing of source files. It is difficult for smart contracts to interact with external applications with complex types of data, and requires different encoding and decoding codes.
By obtaining the contract code and contract description information written by the user, a contract interface class is generated, including decoding, calling and encoding codes, and is used to build a contract file for smart contracts. Users only need to develop contract implementation classes without the need to fully develop source files.
It realizes automatic contract interface generation, improves the efficiency of smart contract file construction, and does not need to fully develop source files, simplifying the process of interacting with smart contracts and external data.
Smart Images

Figure CN113672216B_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of this specification relate to the field of blockchain technology, and in particular, to a method and device for generating a contract interface class of a smart contract file. Background Art
[0002] Blockchain is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Due to the characteristics of blockchain such as decentralization, information immutability, and autonomy, blockchain has also received more and more attention and application. Among them, smart contracts appeared in the blockchain 2.0 era, which has raised the application scope of blockchain to a new level. With smart contracts, blockchain can no longer do a single transfer transaction, but can also implement the predetermined transactions that users expect to achieve by calling user-defined codes.
[0003] I hope there will be a new solution to build smart contract documents more efficiently. Summary of the invention
[0004] One or more embodiments of this specification provide a method and device for generating a contract interface class of a smart contract file.
[0005] In a first aspect, a method for generating a contract interface class of a smart contract file is provided, comprising: obtaining a contract code written by a user as a contract implementation class of the smart contract; obtaining contract description information, wherein the contract description information at least describes input parameters and output parameters defined by the user; generating an interface code according to the contract description information and the contract implementation class, and generating a contract interface class using the interface code, wherein the interface code comprises at least a first decoding code, a contract calling code, and a first encoding code, wherein the first decoding code is used to decode input data from outside the smart contract to obtain input parameter values corresponding to the input parameters, the contract calling code is used to call the contract implementation class to obtain output parameter values corresponding to the output parameters, and the first encoding code is used to encode the output parameter values to obtain output data provided to the outside of the smart contract; the contract interface class and the contract implementation class are used to construct the contract file of the smart contract.
[0006] In a possible implementation, the contract description information also defines a structure corresponding to the input data, which is used to indicate several field names and data types corresponding to the input data; the input parameter value includes several first business data obtained from the input data based on the structure.
[0007] In a possible implementation manner, the contract description information further defines a data exchange format; and the generating of the interface code according to the contract description information and the contract implementation class specifically includes:
[0008] Generate a contract call code according to the input parameters and the contract implementation class; and,
[0009] Determine an encoding code template and a decoding code template from a preset code template set according to the data exchange format;
[0010] The decoding code template is instantiated according to the input parameters to obtain the first decoding code, and the encoding code template is instantiated according to the output parameters to obtain the first encoding code.
[0011] In a possible implementation, the method further includes: determining a target contract engineering template from a preset contract engineering template set according to the contract description information;
[0012] The generating the contract interface class by using the interface code specifically includes: instantiating the target contract engineering template by using the first encoding code, the contract calling code and the first decoding code to obtain the contract interface class.
[0013] In a possible implementation, the contract description information further defines an interface permission, which is used to indicate a first account that is allowed or prohibited from calling the smart contract; the first decoding code is further used to decode the input data to obtain a second account that requests to call the smart contract; the interface code also includes a permission verification code generated based on the first account, which is used to determine whether the second account has permission to call the smart contract;
[0014] The generating a contract interface class by using the interface code specifically includes: generating a contract interface class by using the permission verification code, the first decoding code, the contract calling code and the first encoding code.
[0015] In a possible implementation manner, the contract description information further defines a data structure of an event log; the first decoding code is further used to decode the input data to obtain a plurality of second business data; the interface code further includes a log code generated based on the data structure, and is used to generate an event log according to the plurality of second business data;
[0016] The generating a contract interface class by using the interface code specifically includes: generating a contract interface class by using the log code, the first decoding code, the contract calling code and the first encoding code.
[0017] In a possible implementation, the contract description information also defines the interface name of the contract interface class; the method also includes: generating an access code based on the contract description information, and using the access code to generate a contract access class, the access code including a second encoding code, an interface calling code and a second decoding code, the second encoding code is used to encode the input parameter value of the contract access class input from the outside into the input data, the interface calling code is used to call the contract interface class based on the interface name, and the second decoding code is used to decode the output data to obtain the output parameter value.
[0018] In a second aspect, a device for generating a contract decomposition interface class of a smart contract file is provided, comprising: a code acquisition unit, configured to acquire a contract code written by a user as a contract implementation class of the smart contract; an information acquisition unit, configured to acquire contract description information, wherein the contract description information describes input parameters and output parameters defined by the user; an interface generation unit, configured to generate an interface code according to the contract description information and the contract implementation class, and generate a contract interface class using the interface code, wherein the interface code comprises at least a first decoding code, a contract calling code and a first encoding code, wherein the first decoding code is used to decode input data from outside the smart contract to obtain input parameter values corresponding to the input parameters, the contract calling code is used to call the contract implementation class to obtain output parameter values corresponding to the output parameters, and the first encoding code is used to encode the output parameter values to obtain output data provided to the outside of the smart contract; the contract interface class and the contract implementation class are used to construct the contract file of the smart contract.
[0019] In a possible implementation, the contract description information also defines a structure corresponding to the input data, which is used to indicate several field names and data types corresponding to the input data; the input parameter value includes several first business data obtained from the input data based on the structure.
[0020] In a possible implementation manner, the contract description information further defines a data exchange format;
[0021] The interface generation unit is specifically configured to generate a contract call code according to the input parameters and the contract implementation class; and, according to the data exchange format, determine an encoding code template and a decoding code template from a preset code template set; instantiate the decoding code template according to the input parameters to obtain the first decoding code, and instantiate the encoding code template according to the output parameters to obtain the first encoding code.
[0022] In a possible implementation, the method further includes: a template selection unit configured to determine a target contract engineering template from a preset contract engineering template set according to the contract description information;
[0023] The interface generation unit is specifically configured to instantiate the target contract engineering template using the first encoding code, the contract calling code and the first decoding code to obtain the contract interface class.
[0024] In a possible implementation, the contract description information further defines an interface permission, which is used to indicate a first account that is allowed or prohibited from calling the smart contract; the first decoding code is further used to decode the input data to obtain a second account that requests to call the smart contract; the interface code also includes generating a permission verification code based on the first account, which is used to determine whether the second account has permission to call the smart contract;
[0025] The interface generation unit is specifically configured to generate a contract interface class using the permission verification code, the first decoding code, the contract calling code and the first encoding code.
[0026] In a possible implementation manner, the contract description information further defines a data structure of an event log; the first decoding code is further used to decode the input data to obtain a plurality of second business data; the interface code further includes a log code generated based on the data structure, and is used to generate an event log according to the plurality of second business data;
[0027] The interface generation unit is specifically configured to generate a contract interface class using the log code, the first decoding code, the contract calling code and the first encoding code.
[0028] In a possible implementation, the contract description information also defines the interface name of the contract interface class; the interface generation unit is further configured to generate an access code based on the contract description information, and use the access code to generate a contract access class, the access code including a second encoding code, an interface calling code, and a second decoding code, the second encoding code being used to encode the input parameter value of the contract access class input from the outside into the input data, the interface calling code being used to call the contract interface class based on the interface name, and the second decoding code being used to decode the output data to obtain the output parameter value.
[0029] In a third aspect, a computer-readable storage medium is provided, on which a computer program / instruction is stored. When the computer program / instruction is executed in a computing device, the computing device executes the method as described in any one of the first aspects.
[0030] In a fourth aspect, a computing device is provided, comprising a memory and a processor, wherein a computer program / instructions are stored in the memory, and the processor executes the computer program / instructions to implement a method as described in any one of the first aspects.
[0031] Through the method and device provided in one or more embodiments of this specification, a contract interface class can be automatically generated based on the contract implementation class written by the user and the contract description information defined by the user. The contract interface class already contains the contract call code for calling the contract implementation class, and the encoding code and decoding code for encoding and decoding the data for the smart contract to interact with the outside world. Therefore, the construction of the contract file can be completed based on the contract implementation class and the contract interface class. In this process, the user only needs to develop the contract implementation class used to implement the predetermined transaction in the smart contract without the need to fully develop the source file of the smart contract, which can complete the construction of the contract file of the smart contract more efficiently. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0033] Figure 1 A schematic diagram of the process of deploying smart contracts in a blockchain network. ;
[0034] Figure 2 A flowchart of a method for generating a contract interface class of a smart contract file provided in an embodiment of this specification;
[0035] Figure 3 A schematic diagram of a process for generating an interface code provided in an embodiment of this specification;
[0036] Figure 4 A schematic diagram of a device for generating a contract interface class of a smart contract file provided in an embodiment of this specification. DETAILED DESCRIPTION
[0037] Each non-limiting embodiment provided in this specification is described in detail below in conjunction with the accompanying drawings.
[0038] In the blockchain network, users can initiate transactions through their accounts registered in the blockchain network. Transactions can be ordinary transfer transactions or used to deploy or call smart contracts. If the contract developer Bob wants to deploy a smart contract in the blockchain network, he can use the following method: Figure 1 The deployment process shown.
[0039] First, Bob can write the source code of the smart contract in the form of a high-level language and form a corresponding source file, where the source file may include one or more interfaces for other computer programs outside the smart contract to call.
[0040] Next, the source file can be compiled into a byte code file through a compiler. Byte code is an intermediate code format between high-level language and machine code, which includes a series of binary character codes composed of opcodes and data pairs. Byte code is not a machine code that can be executed directly, and needs to be parsed by the corresponding virtual machine before it can be executed.
[0041] Then, Bob can publish the contract to the blockchain network in the form of a transaction. Specifically, Bob can initiate a transaction, in which the initiator field from the transaction content is Bob (more specifically, the account / account address registered by Bob in the blockchain network), and the recipient field to is set to an empty value; the transaction also includes a Data field, and the aforementioned source file or bytecode file can be included in the Data field as the contract file of the smart contract.
[0042] Finally, the blockchain network can complete the deployment of the smart contract in the blockchain network by executing the transaction initiated by Bob. Correspondingly, other accounts registered on the blockchain network can call the smart contract by initiating transactions in the blockchain network, so that the blockchain network executes the smart contract based on the contract file of the smart contract.
[0043] The inventors have found that the contract file not only includes codes / instructions for implementing the predetermined transactions that developers expect to implement through smart contracts, but may also include codes for implementing other functions. For example, smart contracts usually cannot directly interact with external applications for complex types of data. This requires configuring the contract file of the smart contract to decode the input data from outside the smart contract and to encode the output data provided to the outside of the smart contract. Different smart contracts actually use different encoding codes and decoding codes. Correspondingly, in the process of building the contract file of the smart contract, that is, in the process of obtaining the source file or bytecode file of the smart contract, the developer needs to completely write the source file of the smart contract, which takes a long time to complete the construction of the contract file of the smart contract, and the efficiency is extremely low.
[0044] In view of the above problems, at least one method and device for generating a contract interface class of a smart contract file is provided in the embodiments of this specification, in order to more efficiently complete the construction of the contract file of the smart contract.
[0045] Figure 2This is a flowchart of a method for generating a contract interface class of a smart contract file provided in an embodiment of this specification. The method can be executed by any device, apparatus, platform or cluster with computing / processing capabilities, such as a contract development platform that integrates smart contract development and testing functions. The method may include the following steps 201 to 205.
[0046] First, in step 201, the contract code written by the user is obtained as the contract implementation class of the smart contract.
[0047] The contract implementation class is specifically used to implement the scheduled transaction that the user expects to implement through the smart contract, and the user actively writes / provides the contract code contained therein. In order to facilitate a clear and accurate description of the technical solution provided in the embodiments of this specification, the scheduled transaction will be mainly described as an example of a smart contract that allocates digital resources (such as a certain type of Token) to an account registered on the blockchain. However, it can be understood that the scheduled transaction can also be other transactions, such as the transfer of digital resources between accounts registered on the blockchain, etc., and this application embodiment does not impose any restrictions on this.
[0048] Next, in step 203, the contract description information is obtained.
[0049] The user can store the contract description information through a dedicated description file, and complete the acquisition of the contract description information by reading the contract description information from the description file. The contract description information reflects the design scheme of the smart contract, which at least describes the input parameters and output parameters of the aforementioned contract implementation class defined by the user. For example, for a smart contract used to implement the allocation of digital resources to an account registered on the blockchain network, the contract description information can specifically define the input parameters of the contract implementation class as an array params, including a receiver of a string data type (which corresponds to the account to which the digital resources are allocated and registered on the blockchain network) and an amount of a unit32 data type (which corresponds to the amount of digital resources allocated); the output parameter returns of the contract implementation class can also be defined, whose data type is bool, and whose value is true representing a successful call or false representing a failed call.
[0050] The contract description information may also describe at least one of the following information defined by the user:
[0051] The contract name of the smart contract. For example, for a smart contract used to allocate digital resources to accounts registered on a blockchain network, its contract name can be defined as Token Contract.
[0052] Contract type. For example, the contract type can be used to indicate that the Token Contract is deployed on MYCHAIN, and the contract file type of the Token Contract is WASM.
[0053] Contract version (version), such as the version number of the Token Contract;
[0054] The structure (types) corresponding to the input data is used to indicate the names and data types of several fields corresponding to the input data. For example, for the aforementioned Token Contract, the name of the structure corresponding to the input data can be defined as Transfer Request, that is, the input data can be defined as Transfer Request, and the field names corresponding to Transfer Request can include send (which corresponds to the request to call the smart contract and the account registered on the blockchain network), receiver, and amount, and the data types corresponding to send and receiver are defined as string, and the data type corresponding to amount is defined as uint32;
[0055] Data exchange format, which is used to indicate the data format of the input data provided to the smart contract by the computer program outside the smart contract and the output data returned by the smart contract to the computer program. For example, for the aforementioned TokenContract, its corresponding data exchange format can be defined as JSON (JavaScript Object Notation), XML (Extensible Markup Language) or other data exchange formats;
[0056] The interface name of the contract interface class. For example, define the interface name of the aforementioned Token Contract as Transfer.
[0057] Interface permissions, which are used to indicate the first account that is allowed or prohibited to call the smart contract. For example, it indicates that the first account allowed to call the aforementioned Token Contract is minter;
[0058] The data structure of the event log is used to indicate several fields and data types corresponding to the event log. For example, for the aforementioned Token Contract, the data structure can indicate that the fields corresponding to the event log include the amount field with a data type of unit32, the from field with a data type of string (the field value is the account requesting to call the Token Contract) and the to field (the field value is the account to which the digital resource is allocated). The name of the event log can also be defined as Transfer logs.
[0059] Next, in step 205, an interface code is generated according to the contract description information and the contract implementation class, and the contract interface class is generated using the interface code. The interface code at least includes: a first decoding code for decoding input data from outside the smart contract to obtain input parameter values corresponding to the input parameters; a contract calling code for calling the contract implementation class to obtain output parameter values corresponding to the output parameters; and a first encoding code for encoding the output parameter values to obtain output data provided to the outside of the smart contract.
[0060] In a more specific example, a set of code templates can be pre-set. For example, for input data / output data in two different formats, JSON and XML, different encoding codes / decoding codes are required to implement encoding / decoding of the input data / output data. Therefore, a decoding code template A1 for supporting decoding of input data in JSON format and an encoding code template A2 for supporting encoding of output data in JSON format can be pre-set, as well as a decoding code template A3 for supporting decoding of input data in XML format and an encoding code template A4 for supporting encoding of output data in XML format can be pre-set. Accordingly, when the data exchange format is defined in the contract description information, at least the aforementioned first decoding code template and first encoding code can be generated based on the data exchange format in step 205. Specifically, please refer to Figure 3 In step 205, at least part or all of the code in the interface code can be obtained through the following steps 2051 to 2055.
[0061] Step 2051, generating a contract call code according to the input parameters and the contract implementation class. That is, when the class name of the contract implementation class and the input parameters of the contract implementation class are known, the contract call code for calling the contract implementation class is generated based on the class name of the contract implementation class and the input parameters, for example, the contract call code Transfer-RE(params) is generated, where Transfer-RE represents the class name of the contract implementation class, and params represents the input parameters of the contract implementation class.
[0062] Step 2053, determine the encoding code template and the decoding code template from the preset code template set according to the data exchange format. For example, if the contract description information defines the data exchange format JSON, the aforementioned decoding code template A1 and encoding code template A2 corresponding to the data exchange format JSON can be determined from the code template set.
[0063] Step 2055, instantiate the decoding code template according to the input parameters to obtain the first decoding code, and instantiate the encoding code template according to the output parameters to obtain the first encoding code. That is, render the decoding code template based on various information including the input parameters; for example, the input parameter defined in the contract description information is the array params including receiver and amount, and the structure defined in the contract description information is Transfer Request, then the TransferRequest and receiver, amount and other parameters can be filled into the decoding code template accordingly to obtain the first decoding code that can be used to decode the input data and obtain the input parameter value corresponding to the input parameter. In addition, the process of obtaining the first encoding code is similar to the process of obtaining the first decoding code described above, and will not be repeated.
[0064] When the interface authority is also defined in the contract description information, the first decoding code can also obtain the second account requesting to call the smart contract when decoding the input data, for example, the input data is decoded into the aforementioned structure TransferRequest, and the account under the field name send in the Transfer Request is the second account requesting to call the smart contract. Correspondingly, in addition to generating the first decoding code, the contract call code and the first encoding code in the aforementioned step 205, a permission verification code can also be generated based on the aforementioned first account (for example, the first account minter that allows calling the smart contract), and the permission verification code is used to determine whether the second account has the authority to call the smart contract, for example, when the account under send is minter, it is determined that minter has the authority to call the smart contract; in addition, when the contract interface class is generated according to the interface code in step 205, the contract interface class can be specifically generated according to the permission verification code, the first decoding code, the contract call code and the first encoding code.
[0065] When the data structure of the event log is also defined in the contract description information, the first decoding code can obtain several second business data when decoding the input data, for example, the input data is decoded into the structure Transfer Request, and the account under the field name send, the account under the field name receive, and the field value under the field name amount in the Transfer Request can be used as several second business data for generating the event log. Correspondingly, in addition to generating the first decoding code, the contract call code, and the first encoding code in the aforementioned step 205, a log code can also be generated according to the data structure of the event log, and the log code is used to generate the event log according to the aforementioned several second business data; in addition, when the contract interface class is generated according to the interface code in step 205, the contract interface class can be specifically generated according to the log code, the first decoding code, the contract call code, and the first encoding code. For example, the fields corresponding to the event log indicated in the data structure of the event log include an amount field of data type unit32, from and to fields of data type string, and the name of the event log is Transfer log. Then, an event log can be generated in which the value of the from field is the account under send, the value of the to field is the account under receive, the value of the amount field is the field value under the amount field in the structure TransferRequest, and the name is Transfer log.
[0066] In a possible implementation, one or more method classes may be pre-configured to support decoding of input data, encoding of output data, and generation of event logs. For example, method class B1 may be pre-configured for the data exchange format JSON, and the first decoding code may complete the decoding of input data by calling this method class B1. For another example, method class B2 may be pre-configured for event logs, and the log code may complete the generation of event logs by calling this method class B2.
[0067] It should be noted that the log code can be located outside the contract implementation class as a member of the interface code, or can be inserted into the contract implementation class obtained in step 201 as a member of the contract implementation class to form a new contract implementation class.
[0068] In a possible implementation, a contract process template set can also be pre-configured; for a single contract engineering template in the contract engineering template set, it is specifically used to indicate the rules for generating a contract interface class using an interface code. For example, for a smart contract that does not require permission verification and does not require event log recording, its corresponding contract engineering template C1 can be configured, and C1 can indicate the rules for generating a contract interface class using a first decoding code, a contract call code, and a first encoding code. For another example, for a smart contract that requires permission verification and does not require event log recording, its corresponding contract engineering template C2 can be configured, and C2 can indicate the rules for generating a contract interface class using a first decoding code, a contract call code, a first encoding code, and a permission verification code for permission verification. For another example, for a smart contract that requires permission verification and requires event log recording, its corresponding contract engineering template C3 can be configured, and C3 can indicate the rules for generating a contract interface class using a first decoding code, a contract call code, a first encoding code, and the aforementioned permission verification code.
[0069] Corresponding to the above-mentioned method, before executing step 205, the target contract engineering template can also be determined from the preset contract engineering template set according to the contract description information. For example, when the data structure of the interface authority and the event log is not defined in the contract description information, it means that the smart contract does not need to perform authority and does not need to record the event log, and the aforementioned contract engineering template C1 can be determined as the target contract engineering template; when the interface authority is defined in the contract description information and the data structure of the event log is not defined, it means that the smart contract needs to perform authority verification and does not need to generate a record event log, and the aforementioned contract engineering module C2 can be determined as the target contract engineering template; when the interface authority is defined in the contract description information and the data structure of the event log is defined, it means that the smart contract needs to perform authority verification and needs to generate a record event log, and the aforementioned contract engineering module C3 can be determined as the target contract engineering template. In step 205, the interface code can be used to render / instantiate the target contract engineering template to obtain the contract interface class; for example, when it is determined that the target contract engineering template is the aforementioned contract engineering template C3, the permission verification code, the first decoding code, the contract call code, the first encoding code and the log code can also be obtained in step 205 by the aforementioned various methods. At this time, the contract interface class can be generated using the interface code according to the rules indicated by the contract engineering template C3, and the first decoding code, permission verification code, contract call code, first encoding code and log code can be filled into the contract engineering template C3 accordingly, so as to obtain the corresponding contract interface class. In addition, it should be understood that the class name of the contract interface class can be named as the interface name defined in the contract description information, so that the application outside the smart contract can call the contract interface class.
[0070] In some embodiments, the contract file of the smart contract can also be constructed based on the contract interface class and the contract implementation class. Specifically, the source file of the smart contract can be composed of the contract interface class, the contract implementation class and other method classes that are called by the contract interface class, and the source file can be used as the contract file of the smart contract; or, the source file of the smart contract can be compiled by a corresponding compiler to obtain a corresponding bytecode file and use it as the contract file of the smart contract.
[0071] Through the technical solutions provided in the above embodiments, a contract interface class can be automatically generated based on the contract implementation class written by the user and the contract description information defined by the user. The contract interface class already contains the contract call code for calling the contract implementation class, and the encoding code and decoding code for encoding and decoding the data for the smart contract to interact with the outside world. Therefore, the construction of the contract file can be completed based on the contract implementation class and the contract interface class. In this process, the user only needs to develop the contract implementation class used to implement the predetermined transaction in the smart contract without the need to fully develop the source file of the smart contract, which can complete the construction of the contract file of the smart contract more efficiently.
[0072] In some embodiments, an access code can also be generated according to the contract description information, and a contract access class can be generated using the access code. The access code includes a second encoding code, an interface call code, and a second decoding code. The second encoding code is used to encode the input parameter value of the external input contract access class as input data, the interface call code is used to call the contract interface class based on the interface name, and the second decoding code is used to decode the output data to obtain the output parameter value. The contract access class can also be expressed as a blockchain access (Blockchain Access Object, BAO) class, which is essentially a code that can access smart contracts in the form of remote procedure call (Remote Procedure Call, RPC) function calls, and is dedicated to processing data interactions between external computer programs and smart contracts. Correspondingly, a container (such as docker) for testing smart contracts can be built locally, and the contract interface class in the contract file is called through the contract access class in the container to complete the automated testing of the smart contract.
[0073] Based on the same concept as the above method embodiment, this specification embodiment also provides a device for generating a contract interface class of a smart contract file. Figure 4As shown, the device at least includes: a code acquisition unit 401, configured to acquire a contract code written by a user as a contract implementation class of a smart contract; an information acquisition unit 403, configured to acquire contract description information, wherein the contract description information describes input parameters and output parameters defined by the user; an interface generation unit 405, configured to generate an interface code according to the contract description information and the contract implementation class, and generate a contract interface class using the interface code, wherein the interface code at least includes a first decoding code, a contract calling code, and a first encoding code, wherein the first decoding code is used to decode input data from outside the smart contract to obtain input parameter values corresponding to the input parameters, the contract calling code is used to call the contract implementation class to obtain output parameter values corresponding to the output parameters, and the first encoding code is used to encode the output parameter values to obtain output data provided to the outside of the smart contract; the contract interface class and the contract implementation class are used to construct a contract file of the smart contract.
[0074] In a possible implementation, the contract description information also defines a structure corresponding to the input data, which is used to indicate several field names and data types corresponding to the input data; the input parameter value includes several first business data obtained from the input data based on the structure.
[0075] In a possible implementation, the contract description information also defines a data exchange format; the interface generation unit 405 is specifically configured to generate a contract call code based on the input parameters and the contract implementation class; and, based on the data exchange format, determine an encoding code template and a decoding code template from a preset code template set; instantiate the decoding code template based on the input parameters to obtain the first decoding code, and instantiate the encoding code template based on the output parameters to obtain the first encoding code.
[0076] In a possible implementation, it also includes: a template selection unit 407, configured to determine a target contract engineering template from a preset contract engineering template set according to the contract description information; and the interface generation unit 405, specifically configured to instantiate the target contract engineering template using the first encoding code, the contract calling code and the first decoding code to obtain the contract interface class.
[0077] In a possible implementation, the contract description information also defines interface permissions, which are used to indicate whether the first account is allowed or prohibited to call the smart contract; the first decoding code is also used to decode the input data to obtain the second account requesting to call the smart contract; the interface code also includes a permission verification code generated based on the first account, which is used to determine whether the second account has permission to call the smart contract. The interface generation unit 405 is specifically configured to generate a contract interface class using the permission verification code, the first decoding code, the contract calling code, and the first encoding code.
[0078] In a possible implementation, the contract description information further defines a data structure of an event log; the first decoding code is further used to decode the input data to obtain a plurality of second business data; the interface code further includes a log code generated based on the data structure, which is used to generate an event log according to the plurality of second business data. The interface generation unit 405 is specifically configured to generate a contract interface class using the log code, the first decoding code, the contract call code and the first encoding code.
[0079] In a possible implementation, the contract description information also defines the interface name of the contract interface class; the interface generation unit 405 is also configured to generate an access code based on the contract description information, and use the access code to generate a contract access class, the access code including a second encoding code, an interface calling code and a second decoding code, the second encoding code is used to encode the input parameter value of the contract access class input from the outside into the input data, the interface calling code is used to call the contract interface class based on the interface name, and the second decoding code is used to decode the output data to obtain the output parameter value.
[0080] Those skilled in the art should be aware that in one or more of the above examples, the functions described in this specification can be implemented by hardware, software, firmware, or any combination thereof. When implemented by software, the computer programs corresponding to these functions can be stored in a computer-readable medium or transmitted as one or more instructions / codes on a computer-readable medium, so that when the computer programs corresponding to these functions are executed by a computer, the method described in any embodiment of this specification is implemented by the computer.
[0081] A computer-readable storage medium is also provided in an embodiment of this specification, on which a computer program / instruction is stored. When the computer program / instruction is executed in a computing device, the computing device executes the method of generating a contract interface class of a smart contract file provided in any embodiment of this specification.
[0082] A computing device is also provided in an embodiment of the present specification, including a memory and a processor, wherein the memory stores a computer program / instruction, and when the processor executes the computer program / instruction, a method for generating a contract interface class of a smart contract file provided in any embodiment of the present specification is implemented.
[0083] Each embodiment in this specification is described in a progressive manner, and the same or similar parts of each embodiment can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0084] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0085] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solution of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for generating a contract interface class of a smart contract file, comprising: Get the contract code written by the user as the contract implementation class of the smart contract; Acquire contract description information, wherein the contract description information at least describes input parameters and output parameters defined by a user; An interface code is generated according to the contract description information and the contract implementation class, and a contract interface class is generated using the interface code, wherein the interface code at least includes a first decoding code, a contract calling code and a first encoding code, wherein the first decoding code is used to decode input data from outside the smart contract to obtain input parameter values corresponding to the input parameters, the contract calling code is used to call the contract implementation class to obtain output parameter values corresponding to the output parameters, and the first encoding code is used to encode the output parameter values to obtain output data provided to the outside of the smart contract; the contract interface class and the contract implementation class are used to construct a contract file of the smart contract.
2. The method according to claim 1, wherein: The contract description information also defines a structure corresponding to the input data, which is used to indicate several field names and data types corresponding to the input data; the input parameter value includes several first business data obtained from the input data based on the structure.
3. The method according to claim 1, wherein: The contract description information also defines the data exchange format; The generating of the interface code according to the contract description information and the contract implementation class specifically includes: Generate a contract call code according to the input parameters and the contract implementation class; and, Determine an encoding code template and a decoding code template from a preset code template set according to the data exchange format; The decoding code template is instantiated according to the input parameters to obtain the first decoding code, and the encoding code template is instantiated according to the output parameters to obtain the first encoding code.
4. The method according to claim 1, wherein: Also includes: Determining a target contract engineering template from a preset contract engineering template set according to the contract description information; The generating the contract interface class by using the interface code specifically includes: instantiating the target contract engineering template by using the first encoding code, the contract calling code and the first decoding code to obtain the contract interface class.
5. The method according to claim 1, wherein: The contract description information also defines an interface permission, which is used to indicate whether the first account is allowed or prohibited to call the smart contract; the first decoding code is also used to decode the input data to obtain the second account requesting to call the smart contract; the interface code also includes a permission verification code generated based on the first account, which is used to determine whether the second account has the permission to call the smart contract; The generating a contract interface class by using the interface code specifically includes: generating a contract interface class by using the permission verification code, the first decoding code, the contract calling code and the first encoding code.
6. The method according to claim 1, wherein: The contract description information also defines a data structure of an event log; the first decoding code is also used to decode the input data to obtain a plurality of second business data; the interface code also includes a log code generated based on the data structure, which is used to generate an event log according to the plurality of second business data; The generating a contract interface class by using the interface code specifically includes: generating a contract interface class by using the log code, the first decoding code, the contract calling code and the first encoding code.
7. The method according to any one of claims 1 to 6, wherein: The contract description information also defines the interface name of the contract interface class; the method also includes: generating an access code according to the contract description information, and generating a contract access class using the access code, the access code including a second encoding code, an interface calling code and a second decoding code, the second encoding code is used to encode the input parameter value of the contract access class input from the outside into the input data, the interface calling code is used to call the contract interface class based on the interface name, and the second decoding code is used to decode the output data to obtain the output parameter value.
8. A device for generating a contract interface class of a smart contract file, comprising: A code acquisition unit, configured to acquire a contract code written by a user as a contract implementation class of a smart contract; An information acquisition unit, configured to acquire contract description information, wherein the contract description information describes input parameters and output parameters defined by a user; An interface generation unit is configured to generate an interface code according to the contract description information and the contract implementation class, and generate a contract interface class using the interface code, wherein the interface code at least includes a first decoding code, a contract calling code and a first encoding code, the first decoding code is used to decode input data from outside the smart contract to obtain input parameter values corresponding to the input parameters, the contract calling code is used to call the contract implementation class to obtain output parameter values corresponding to the output parameters, and the first encoding code is used to encode the output parameter values to obtain output data provided to the outside of the smart contract; the contract interface class and the contract implementation class are used to construct a contract file of the smart contract.
9. The device according to claim 8, wherein: The contract description information also defines a structure corresponding to the input data, which is used to indicate several field names and data types corresponding to the input data; the input parameter value includes several first business data obtained from the input data based on the structure.
10. The device according to claim 8, wherein: The contract description information also defines the data exchange format; The interface generation unit is specifically configured to generate a contract call code according to the input parameters and the contract implementation class; and, determining an encoding code template and a decoding code template from a preset code template set according to the data exchange format; The decoding code template is instantiated according to the input parameters to obtain the first decoding code, and the encoding code template is instantiated according to the output parameters to obtain the first encoding code.
11. The device according to claim 8, wherein: It also includes: a template selection unit configured to determine a target contract engineering template from a preset contract engineering template set according to the contract description information; The interface generation unit is specifically configured to instantiate the target contract engineering template using the first encoding code, the contract calling code and the first decoding code to obtain the contract interface class.
12. The device according to claim 8, wherein: The contract description information also defines an interface permission, which is used to indicate whether the first account is allowed or prohibited to call the smart contract; the first decoding code is also used to decode the input data to obtain the second account requesting to call the smart contract; the interface code also includes a permission verification code generated based on the first account, which is used to determine whether the second account has the permission to call the smart contract; The interface generation unit is specifically configured to generate a contract interface class using the permission verification code, the first decoding code, the contract calling code and the first encoding code.
13. The device according to claim 8, wherein: The contract description information also defines a data structure of an event log; the first decoding code is also used to decode the input data to obtain a plurality of second business data; the interface code also includes a log code generated based on the data structure, which is used to generate an event log according to the plurality of second business data; The interface generation unit is specifically configured to generate a contract interface class using the log code, the first decoding code, the contract calling code and the first encoding code.
14. The device according to any one of claims 8 to 13, wherein: The contract description information also defines the interface name of the contract interface class; the interface generation unit is also configured to generate an access code based on the contract description information, and use the access code to generate a contract access class, the access code includes a second encoding code, an interface calling code and a second decoding code, the second encoding code is used to encode the input parameter value of the contract access class input from the outside into the input data, the interface calling code is used to call the contract interface class based on the interface name, and the second decoding code is used to decode the output data to obtain the output parameter value.
15. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed in a computing device, the computing device executes the method according to any one of claims 1 to 7.
16. A computing device, comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Interface document generation method and device and readable storage medium
CN111240653A
DSL-based intelligent contract code generation method and device, equipment and storage medium
CN112015396A
Computer system enabling flexible creation of and participation in blockchain-based smart contracts for transactions in tokenized digital assets
US20200193541A1
Systems, methods, and storage media for interfacing a user device with a decentralized architecture
US20210103582A1
Receipt storage method combining code labelling with user and event type, and node
WO2020233610A1
