Blockchain smart contract generation method and device, equipment and storage medium

By automatically generating smart contracts through anti-object relational mapping technology, the problem of low smart contract development efficiency is solved, and a highly efficient smart contract generation process is achieved.

CN115391330BActive Publication Date: 2026-06-19TENCENT TECHNOLOGY (SHENZHEN) CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
TENCENT TECHNOLOGY (SHENZHEN) CO LTD
Filing Date
2021-05-25
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

The development efficiency of smart contracts in existing technologies is low, with a large number of repetitive operations, resulting in high development costs.

Method used

By employing the reverse Object Relational Mapping (ORM) method, smart contracts are automatically generated. This is achieved by obtaining data layer description information defined by business requirements, writing template files, and performing rendering processing.

Benefits of technology

It simplifies the smart contract development process, improves development efficiency, reduces repetitive operations, and increases the speed of smart contract generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115391330B_ABST
    Figure CN115391330B_ABST
Patent Text Reader

Abstract

This application proposes a method, apparatus, device, and storage medium for generating smart contracts on a blockchain. The method includes: obtaining business requirements of a target business; defining data layer description information for the smart contract corresponding to the target business according to the business requirements; writing a template file for the smart contract based on the data layer description information; and rendering the data layer description information and the template file to generate the smart contract. This method can automatically generate smart contracts, thereby improving the development efficiency of smart contracts.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of blockchain technology, and in particular to a method for generating smart contracts for blockchain, a device for generating smart contracts for blockchain, a computer device, and a computer-readable storage medium. Background Technology

[0002] Blockchain is a ledger technology that is jointly maintained by multiple parties, uses cryptography to ensure secure transmission and access, and enables consistent data storage, tamper-proofing, and non-repudiation. Smart contracts can run on a blockchain. Currently, smart contracts are typically written manually. Developers need to understand the business logic first, and then manually write the registration and operation logic of the smart contract. This makes smart contract development costly in terms of time and effort, resulting in low development efficiency. Summary of the Invention

[0003] This application provides a method, apparatus, device, and storage medium for generating smart contracts on a blockchain, which can automatically generate smart contracts and improve the development efficiency of smart contracts.

[0004] On one hand, embodiments of this application provide a method for generating smart contracts for blockchain, the method comprising:

[0005] Obtain the business requirements of the target business;

[0006] Define data layer description information for smart contracts corresponding to the target business according to business requirements;

[0007] Write a template file for the smart contract based on the data layer description information;

[0008] The data layer description information and template files are rendered to generate smart contracts.

[0009] On one hand, embodiments of this application provide a smart contract generation apparatus for blockchain, the apparatus comprising:

[0010] The acquisition module is used to acquire the business requirements of the target business.

[0011] The processing module is used to define data layer description information for smart contracts corresponding to the target business according to business requirements;

[0012] The processing module is also used to write template files for smart contracts based on the data layer description information;

[0013] The processing module is also used to render the data layer description information and template files to generate smart contracts.

[0014] On one hand, embodiments of this application provide a computer device, including:

[0015] A processor, adapted to execute computer programs;

[0016] A computer-readable storage medium storing a computer program that, when executed by a processor, implements the aforementioned method for generating smart contracts for a blockchain.

[0017] On one hand, embodiments of this application provide a computer-readable storage medium storing a computer program adapted to be loaded by a processor and executed by the above-described method for generating smart contracts for a blockchain.

[0018] On one hand, embodiments of this application provide a computer program product or computer program that includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the aforementioned method for generating smart contracts for blockchain.

[0019] In this embodiment, data layer description information can be defined for the smart contract corresponding to the target business according to the business requirements of the target business, and a template file for the smart contract can be written based on the data layer description information. The smart contract can be automatically generated by rendering the data layer description information and the template file for the smart contract. The above-mentioned automated generation process of the smart contract is based on the method of inverse object relation mapping. This method can convert the original data layer description information into the corresponding code of the smart contract. This method conforms to the idea of ​​data-driven approach, can reduce repetitive operations, simplify the development process of smart contracts, and effectively improve the development efficiency of smart contracts. Attached Figure Description

[0020] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0021] Figure 1A This is a schematic diagram of the architecture of a blockchain system provided in an exemplary embodiment of this application;

[0022] Figure 1B This is a schematic diagram of a blockchain structure provided in an exemplary embodiment of this application;

[0023] Figure 1C This is a flowchart illustrating a block generation process provided in an exemplary embodiment of this application;

[0024] Figure 1D This is a schematic diagram illustrating the lifecycle of a smart contract provided in an exemplary embodiment of this application;

[0025] Figure 2 A flowchart illustrating a method for generating a smart contract on a blockchain, provided as an exemplary embodiment of this application;

[0026] Figure 3 A flowchart illustrating a method for generating a smart contract on a blockchain, provided as another exemplary embodiment of this application;

[0027] Figure 4 A flowchart illustrating a method for generating a smart contract on a blockchain, provided as another exemplary embodiment of this application;

[0028] Figure 5 A schematic diagram of the structure of a blockchain smart contract generation device provided as an exemplary embodiment of this application;

[0029] Figure 6 This is a schematic diagram of the structure of a computer device provided for an exemplary embodiment of this application. Detailed Implementation

[0030] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0031] This application relates to blockchain, a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. It is primarily used to organize data chronologically and encrypt it into a ledger, making it tamper-proof and forgery-proof, while also enabling data verification, storage, and updating. The relevant technologies of blockchain will be described in detail below with reference to the accompanying drawings.

[0032] Figure 1A This is a schematic diagram of the structure of a blockchain system provided in an exemplary embodiment of this application. For example... Figure 1AAs shown, a blockchain system is a data-sharing system that can include multiple nodes, specifically node 1, node 2, node 3, ..., node N. Each node, during normal operation, can receive data sent from the outside world and process it into blocks on the blockchain, and can also send data to the outside world. To ensure data interoperability between nodes, information connections can exist between them, allowing for information transmission. For example, when any node in the blockchain system receives input information, other nodes in the system obtain this input information according to the consensus algorithm and store it as data in the shared data, ensuring consistency of data stored on all nodes in the blockchain system.

[0033] It is understood that the above-mentioned information connection is not limited to the connection method. It can be connected directly or indirectly through wired communication, or directly or indirectly through wireless communication, or through other connection methods. This application does not impose any restrictions on this.

[0034] It is understandable that each node in a blockchain system has a corresponding node identifier, and each node can store the node identifiers of other nodes in the blockchain system. This allows the generated blocks to be broadcast to other nodes in the blockchain system based on their node identifiers. Each node can maintain a node identifier list as shown in the table below, storing the node name and node identifier in this list. The node identifier can be an IP (Internet Protocol) address or any other information that can be used to identify the node. Table 1 uses IP addresses as an example only.

[0035] Table 1

[0036] Node Name Node identifier Node 1 117.114.151.174 Node 2 117.116.189.145 Node 3 117.113.181.124 … … Node N xx.xx.xx.xx

[0037] Each node in a blockchain system stores the same blockchain entry. A blockchain consists of multiple blocks; see [link to blockchain documentation]. Figure 1B A blockchain consists of multiple blocks. The genesis block includes a block header and a block body. The block header stores input information feature values, version number, timestamp, and difficulty value, while the block body stores the input information. The next block after the genesis block takes the genesis block as its parent block. The next block also includes a block header and a block body. The block header stores the input information feature values ​​of the current block, the block header feature values ​​of the parent block, version number, timestamp, and difficulty value, and so on. This ensures that the block data stored in each block is related to the block data stored in the parent block, guaranteeing the security of the input information in the blocks.

[0038] When generating each block in the blockchain, refer to Figure 1C , when the node where the blockchain is located receives the input information, it verifies the input information. After the verification is completed, it stores the input information in the memory pool and updates the hash tree used to record the input information; then, it updates the update timestamp to the time when the input information is received and tries different random numbers, and performs eigenvalue calculations multiple times so that the calculated eigenvalue can satisfy the following formula:

[0039] SHA256(SHA256(version + prev_hash + merkle_root + ntime + nbits + x)) < TARGET where SHA256 is the eigenvalue algorithm used to calculate the eigenvalue; version (version number) is the version information of the relevant block protocol in the blockchain; prev_hash is the block header eigenvalue of the parent block of the current block; merkle_root is the eigenvalue of the input information; ntime is the update time of the update timestamp; nbits is the current difficulty, which is a fixed value within a period of time and is determined again after exceeding the fixed time period; x is a random number; TARGET is the eigenvalue threshold, and this eigenvalue threshold can be determined according to nbits.

[0040] In this way, when a random number that satisfies the above formula is calculated, the information can be stored correspondingly, the block header and the block body are generated, and the current block is obtained. Subsequently, the node where the blockchain is located sends the newly generated block to other nodes in its blockchain system according to the node identifiers of other nodes in the blockchain system, and other nodes verify the newly generated block and add the newly generated block to the blockchain they store after the verification is completed.

[0041] The blockchain system may include smart contracts. A smart contract is a computer protocol designed to spread, verify or execute contracts in an information-based manner, which can allow trusted transactions without a third party, and these transactions are traceable and tamper-proof. A smart contract may refer to a piece of code that can be understood and executed by each node in a blockchain system, and can execute any logic and obtain results. It should be understood that the blockchain system may include one or more smart contracts, and these smart contracts can be distinguished by an identification number (Identity document, ID) or name, and the identification number or name of the smart contract may be carried in the business request to specify the smart contract that the blockchain system needs to run.

[0042] Figure 1D is a schematic diagram of the life cycle of a smart contract provided by an exemplary embodiment of the present application. As Figure 1DAs shown, a smart contract's lifecycle typically goes through the following stages: (1) Writing stage. Developers can use Object Relational Mapping (ORM) to write smart contracts. ORM is a programming technique used to convert data between different types of systems in object-oriented programming languages. It can convert program objects into SQL-based operation statements. In other words, ORM is a method to convert code written in an object-oriented programming language into an SQL database table structure. The process of this method is as follows: Based on business requirements, the contract logic (including registration logic and operation logic) of the smart contract is defined using a certain programming language (such as a contract-based programming language (Solidity), a general-purpose programming language (Vyper), a just-in-time compiled programming language (JavaScript), etc.), and the smart contract is written in the forward direction. Among them, Solidity is a high-level language for smart contracts. Smart contracts written in this language can generate bytecode that can be executed on the EVM (Ethereum Virtual Machine, the environment used to execute smart contracts). (2) Compilation stage. The compilation phase can perform lexical and syntactic analysis on the contract logic of the defined smart contract. After confirming that the contract logic of the smart contract conforms to the syntactic rules, the smart contract is output. The output smart contract is a binary language that the machine can recognize and run; (3) Deployment phase. Developers can use virtual machines (such as Ethereum's EVM, Fabric's Docker, etc.) to process the consensus of smart contracts. After the smart contract passes the consensus, it is deployed to various nodes in the blockchain system. (4) Invocation phase. When a node in the blockchain system detects a contract event (such as a user sending a transaction), it can invoke the corresponding smart contract and execute the contract logic included in the smart contract to process the contract event. (5) Listening phase. This phase refers to listening to the contract events of the smart contract to obtain complete information about the triggering of the contract event. (6) Destruction phase. When the smart contract becomes invalid or is no longer used, it needs to be destroyed on the blockchain system.

[0043] Currently, smart contracts are typically written using ORM methods, which suffers from low development efficiency and numerous repetitive operations. Therefore, this application provides a method for generating blockchain smart contracts, which can automatically generate smart contracts based on a reverse ORM method. In this embodiment, reverse ORM refers to a processing technique that reverses or reverses the ORM process; specifically, it can be a method that converts SQL database table structures into code files based on an object-oriented programming language. Specifically: Data layer description information is defined for the smart contract corresponding to the target business according to the business requirements of the target business. A template file for the smart contract is written based on the data layer description information. Rendering the data layer description information and the smart contract template file automatically generates the smart contract. In the process of automatically generating smart contracts using the above-mentioned reverse ORM method, the original data layer description information can be converted into the corresponding code of the smart contract. This method conforms to the data-driven approach, reduces repetitive operations, simplifies the smart contract development process, and effectively improves the development efficiency of smart contracts.

[0044] After the smart contract is automatically generated, it can be compiled, tested, and deployed to the blockchain system after passing the test. This allows nodes in the blockchain system to use the smart contract to process and implement target business.

[0045] The method for generating a blockchain smart contract provided in this application embodiment can be executed by a computer device, which can be a terminal or a server. The server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, etc., but is not limited to these. The computer device can be any device outside the blockchain system; of course, the computer device can also be a node in the blockchain system.

[0046] The following details a method for generating smart contracts for blockchain based on this application.

[0047] Figure 2 This is a flowchart illustrating a method for generating a smart contract on a blockchain, provided in an exemplary embodiment of this application. The method may include the following steps: S201-S204:

[0048] S201. Obtain the business requirements of the target business.

[0049] The target business can be any business that needs to be executed through a smart contract. For example, the target business could be a data uploading business, a payment business, or an auditing business, etc. Business requirements describe the goals or functions that the target business needs to achieve; for example, if the target business is a data uploading business, its business requirements could be described as creating a new block on the blockchain for the transmitted data. In this embodiment, the computer device can output a configuration interface, where the smart contract developer can configure the business requirements of the target business. The computer device obtains the business requirements configured by the developer and, based on these requirements, automatically generates the corresponding smart contract for the target business according to subsequent processes.

[0050] S202. Define data layer description information for the smart contract corresponding to the target business according to business requirements.

[0051] The data layer description information of a smart contract refers to the descriptive statements used to describe the data layer of the smart contract; these descriptive statements can include, but are not limited to, SQL (Structured Query Language) statements. In one implementation, the data layer description information can refer to the DDL (Data Definition Language) description statements of the data tables corresponding to the target business. DDL is a language in SQL that is responsible for defining data structures and database objects, used to create various objects in the database (tables, views, indexes, synonyms, clusters, etc.).

[0052] After obtaining business requirements, the computer equipment retrieves the business parameters of the target business based on those requirements. For example, if the target business is access control, and the business requirement is to control user access, then the business parameters can include user information, including but not limited to user ID, user name, user age, etc. Furthermore, the business parameters of the target business can be used to create a data table in a relational database. For example, this data table can be a two-dimensional table, where the business parameters can be used as field names (attribute names) to obtain the data table corresponding to the target business. Based on this data table, a DDL description statement is generated as the data layer description information, which then manifests as a data table in the relational database.

[0053] A relational database is a database that uses a relational model to organize data. It stores data in rows and columns for easy user understanding. These rows and columns are called tables, and a set of tables makes up the database. The relational model can be simply understood as a two-dimensional table model, and a relational database is a data organization composed of two-dimensional tables and the relationships between them.

[0054] S203. Write a template file for the smart contract based on the data layer description information.

[0055] After determining the data layer description information, the computer device can write a template file for a smart contract based on the data layer description information. In one implementation, the data layer description information is the DDL description statement of the data table corresponding to the target business. Then, the template file can include a function that matches the table structure of the data table. The function can be used to generate a data table that nodes in the blockchain system can manipulate.

[0056] S204. Render the data layer description information and template file to generate a smart contract.

[0057] The business parameters of the target business can be obtained from the data layer description information (such as the DDL description statement of the data table corresponding to the target business). The business parameters of the target business can be filled into the template file of the smart contract to obtain the smart contract of the target business.

[0058] In this embodiment, the computer device defines data layer description information for the smart contract corresponding to the target business according to the business requirements of the target business, and writes a template file for the smart contract based on the data layer description information. By rendering the data layer description information and the template file of the smart contract, the smart contract for the target business can be automatically generated. The automated generation process of the smart contract is implemented based on the inverse object relational mapping method. This inverse object relational mapping method can convert the original data layer description information into the corresponding code of the smart contract. This method conforms to the data-driven idea, can reduce repetitive operations, simplify the development process of smart contracts, and effectively improve the development efficiency of smart contracts.

[0059] Figure 3 This is a flowchart illustrating a method for generating a smart contract on a blockchain, provided by another exemplary embodiment of this application. The method may include the following steps: S301-S307:

[0060] S301. Obtain the business requirements of the target business.

[0061] The business requirements of the target business can include business parameters. For example, developers can configure the following business parameters: message identifier (MsgID), integer parameter (KeyInt), string parameter (KeyString), variable parameter (KeyVariable), boolean parameter (KeyBool), result parameter (Results), index object parameter (KeyObjectIndex), string parameter (KeyStringNew), string parameter (KeyStringFor), string parameter (KeyStringWhile), and string parameter (KeyStringDo).

[0062] The business requirements of the target business can also carry the data type of the business parameters. For example, the business parameters of the above target business can be configured with the following data types: MsgID is a variable-length string with a maximum length of 255 (varchar) that is not a null pointer (NULL); KeyInt and Results are precise numeric data types with a maximum data length of 20 (bigint); KeyString, KeyVariable, KeyObjectIndex, KeyStringNew, KeyStringFor, KeyStringWhile, and KeyStringDo are variable-length strings with a maximum length of 255 (varchar); and KeyBool is an integer data type with a maximum length of 4 (tinyint).

[0063] S302. Create the data table required for the target business in the relational database. The data table contains fields.

[0064] When a computer device creates a data table required for a target business in a relational database, the data table can be a two-dimensional table. The first row of the two-dimensional table contains field names (attribute names), and these field names are unique. The number of parameters can be determined based on the business parameters of the target business, and multiple fields consistent with the number of parameters can be created in the first row of the two-dimensional table.

[0065] S303. Input the business parameters carried in the business requirements as field values ​​into the data table to obtain the data layer description information.

[0066] Specifically, after obtaining the business parameters carried in the business requirements, the computer equipment inputs the business parameters as field values ​​corresponding to multiple fields in the data table, thus obtaining the data table corresponding to the target business. The computer equipment can also add the data type of the business parameters as the data type of the field values ​​to the data table. Furthermore, it can use the data table corresponding to the target business to generate a DDL description statement (that is, the data table corresponding to the target business can be generated based on the DDL description statement), thereby obtaining the data layer description information.

[0067] The following is a specific example illustrating how the data layer description information of a smart contract can be represented:

[0068] SET FOREIGN_KEY_CHECKS = 0 / / This allows you to update or delete data.

[0069] DROP TABLE IF EXISTS't_sahehllo_table_JsNormal'; / / Delete the table if it exists.

[0070] CREATE TABLE 't_sahehllo_table_JsNormal' ( / / Create the data table)

[0071] 'F_MsgID' varchar(255) NOT NULL, / / MsgID is a variable-length string (varchar) with a maximum length of 255 that is not NULL.

[0072] 'F_KeyInt' bigint(20) DEFAULT NULL, / / KeyInt is a precise numeric data type with a maximum data length of 20 (bigint).

[0073] 'F_KeyString'varchar(255)DEFAULT NULL, / / KeyString is a varchar with a maximum length of 255.

[0074] 'F_KeyVariable' varchar(255) DEFAULT NULL, / / KeyVariable is a varchar with a maximum length of 255.

[0075] 'F_KeyBool'tinyint(4)DEFAULT NULL, / / KeyBool is an integer data type (tinyint) with a maximum length of 4.

[0076] 'F_Results' bigint(20) DEFAULT NULL, / / Results is a bigint with a maximum data length of 20.

[0077] 'F_KeyObjectIndex'varchar(255)DEFAULT NULL, / / KeyObjectIndex is a varchar with a maximum length of 255.

[0078] 'F_KeyStringNew' varchar(255) DEFAULT NULL, / / KeyStringNew is a varchar with a maximum length of 255.

[0079] 'F_KeyStringFor' varchar(255) DEFAULT NULL, / / KeyStringFor is a varchar with a maximum length of 255.

[0080] 'F_KeyStringWhile'varchar(255)DEFAULT NULL, / / KeyStringWhile is a varchar with a maximum length of 255.

[0081] 'F_KeyStringDo'varchar(255)DEFAULT NULL, / / KeyStringDo is a varchar with a maximum length of 255.

[0082] PRIMARY KEY(F_MsgID) / / MsgID is the primary key

[0083] )ENGING = InnoDB DEFAULY CHARSET = UTF8; / / Inserting Chinese characters into the data table will not result in garbled characters.

[0084] Among them, t_sahehllo_table_JsNormal is the data table corresponding to the target business. The fields in the data layer description information are determined based on the business parameters carried in the business requirements.

[0085] S304. Determine the functional functions that match the table structure of the data table, and write the template file of the smart contract based on the matching functional functions.

[0086] Smart contracts are executable code within a blockchain system. For example, Solidity is a high-level language for smart contracts, and smart contracts written in Solidity can generate bytecode that executes on the blockchain system. Therefore, data tables based on DDL (Data Definition Language) statements need to be converted into code executable by the blockchain system. The computer obtains functionalities that match the table structure of the data tables; these functionalities are the code that can run on the blockchain system.

[0087] In one embodiment, the computer device can obtain functional functions from a template library that match the table structure of the data table. This template library may include various functional functions, including but not limited to functions for testing registered objects, functions for reading and writing data objects (test registered objects), etc. The template library here may include, but is not limited to, Python template libraries. After obtaining the functional functions that match the table structure, a template file for the smart contract can be written based on these functional functions.

[0088] For example, if the function matching the table structure of the data table t_sahehllo_table_JsNormal is OnDeplay, then the template file for the smart contract written based on this function would be:

[0089]

[0090]

[0091]

[0092] S305. Render the data layer description information and template file to generate a smart contract.

[0093] In one embodiment, the process of rendering data layer description information and template files to generate a smart contract may include the following steps:

[0094] (1) Call the contract template engine to parse the fields in the data table and obtain the field values. Since the field values ​​in the data table required by the target business are based on the business parameters of the target business, the field values ​​obtained by calling the contract template engine can reflect the business parameters of the target business.

[0095] (2) Call the contract template engine to parse the template file and determine the location of variables in the template file.

[0096] (3) Fill the variable positions in the template file with the parsed field values ​​as rule variables to obtain the smart contract.

[0097] In the steps described above, the contract template engine is a tool that can parse and render the template file and database table structure of a smart contract, and automatically generate the smart contract code. This contract template engine can include, but is not limited to, the Jinja2 engine. The Jinja2 engine is a template system developed under the Python web framework. Originally a template engine modeled after Django templates, it provides template support for Flask and has been widely used due to its flexibility, speed, and security.

[0098] In one embodiment, when the contract template engine is called to parse the fields in the data table corresponding to the target business, information such as the data type of the field value can also be obtained. The contract template engine will convert the data type information defined by the DDL language into the language format of the template file's development language, such as converting varchar(255) into string type and tinyint(4) into boolean type.

[0099] Furthermore, by continuing to call the contract template engine to parse the template file, the positions of variables in the template file can be determined. Taking the template file of the smart contract written in the OnDeplay function above as an example, the contract template engine can determine the position of variables based on {{variable}}. After determining the position of the variables, the obtained field values ​​are used as rule variables to fill the variable positions in the template file, thus obtaining the smart contract corresponding to the template business.

[0100] For example, by populating the field values ​​(business parameters of the target business) from the data table into the template file of the smart contract written in the OnDeplay function above as rule variables, we get:

[0101]

[0102]

[0103] S306. Obtain the operation logic of the target business in the business requirements, and obtain the operation function that matches the operation logic.

[0104] The operational logic of the target business can be determined based on the actual application scenario of the target business. For example, if the business requirement indicates that read operations are allowed on the data layer, then the operational logic is read operation logic. The triggering conditions for the read operation logic can be configured. For example, the triggering condition can be that the computer device can only execute the read operation logic based on the primary key when the business transaction request carries the primary key, thereby obtaining the information of the business parameters corresponding to the primary key. In addition, the business requirement can also indicate which target business parameters have read permissions, etc. This application does not limit this.

[0105] In one embodiment, the business requirement may also indicate that write operations are allowed on the data layer, in which case the operation logic may include write operation logic and the operation function may include write operation function; or, the business requirement may also indicate that read operations are allowed on the data layer, in which case the operation logic may include read operation logic and the operation function may include read operation function; or, the business requirement may also indicate that query operations are allowed on the data layer, in which case the operation logic may include query operation logic and the operation function may include query operation function.

[0106] Specifically, the computer device needs to determine the operation function that executes the operation logic, and generate an operation function that matches the operation logic based on the operation function.

[0107] In one embodiment, after determining the operation function for executing the operation logic, the computer device can generate a corresponding template file based on the operation function, and use the template file to generate an operation function that matches the operation logic of the target business. For example, if the read operation function for executing the read operation logic is GetJsNormal, the computer device can write the corresponding template file as follows:

[0108]

[0109] In one embodiment, the computer device can also directly generate an operation function that matches the operation logic, for example, directly obtaining a write (query) operation function that matches the write (query) operation logic, as shown below:

[0110]

[0111]

[0112] S307. Add matching operation functions to the smart contract.

[0113] Specifically, computer devices add operation functions that match the operation logic to smart contracts, so that when a business request is initiated for a target business, the blockchain system can directly call the corresponding operation functions in the smart contract to perform data operations on the data table corresponding to the target business.

[0114] Compared to the method where business parties manually write smart contracts based on the business requirements of the target business, which is prone to a lot of repetitive work due to changes in business requirements, the embodiments of this application can use a method based on reverse object relational mapping to directly create a data table corresponding to the target business based on the business requirements of the target business, and automatically convert the data layer description information of the data table into the corresponding code of the smart contract. This can reduce repetitive operations, simplify the development process of smart contracts, and effectively improve the development efficiency of smart contracts.

[0115] Figure 4This is a flowchart illustrating a method for generating smart contracts on a blockchain, provided in another exemplary embodiment of this application. This method can be combined with the above... Figure 1D The example illustrates the lifecycle of a smart contract; this method may include the following stages:

[0116] (1) Development phase.

[0117] During the development phase of a smart contract, the computer device needs to define data layer description information based on the business requirements of the target business, and generate a smart contract based on the data layer description information. The detailed implementation process can be found in the descriptions in the aforementioned embodiments, and will not be repeated here. After generating the smart contract, customization is also possible. For example, developers can generate modification logic for the smart contract (such as modifying the trigger conditions of operation functions), and send the modification information including the modification logic to the computer device. After receiving the modification information including the modification logic, the computer device corrects the smart contract according to the modification information, and outputs the smart contract after the correction is completed.

[0118] (2) Testing phase.

[0119] Computer equipment can be used to deploy smart contract testing environments to test smart contracts and obtain test results. This testing can be used to test the contract logic of the smart contract, verifying whether it can function correctly under certain conditions. For example, when testing write operation logic, it checks whether a value is written to the database when a certain business parameter is written. This testing can also be used for reliability testing of the smart contract, checking for security vulnerabilities. For instance, when testing the reliability of write operation logic, it checks whether the data type of the value written to a certain business parameter matches the data type of the business parameter. If the data type does not match the business parameter, yet the value is still written to the database, the smart contract is considered unreliable and needs to be revised.

[0120] Furthermore, when the test results of the smart contract indicate that the smart contract can meet the test requirements of the target business (test passed), the smart contract file is published to the blockchain system.

[0121] (3) Release stage.

[0122] In one embodiment, when the computer device is a node in the blockchain system, it can use a hash algorithm to generate data blocks for smart contracts and broadcast these data blocks within the blockchain system. This allows the blockchain system to reach a consensus on the smart contract data blocks. Once consensus is reached, the smart contract data blocks are published to the nodes in the blockchain system. At this point, the blockchain system can invoke the smart contract to process contract events related to the target business and listen for these contract events.

[0123] In one embodiment, when the computer device is any device outside the blockchain system, the tested smart contract can be sent to any node in the blockchain system, and the smart contract can be published using that node.

[0124] Through the embodiments of this application, computer devices can customize, test, and publish smart contracts after they are generated, so that the entire life cycle of smart contracts can be automated, thereby improving the deployment efficiency of smart contracts.

[0125] The methods of the embodiments of this application have been described in detail above. In order to facilitate better implementation of the above solutions of the embodiments of this application, the apparatus of the embodiments of this application is provided below. Figure 5 This is a schematic diagram of the structure of a blockchain-based smart contract generation device provided in an exemplary embodiment of this application. Figure 5 As shown, the smart contract generation device 50 of the blockchain can be installed in a computer device. This device 50 can be used to execute the corresponding steps in the aforementioned smart contract generation method of the blockchain. The device 50 may include:

[0126] Module 501 is used to obtain the business requirements of the target business.

[0127] Processing module 502 is used to define data layer description information for smart contracts corresponding to target businesses according to business requirements;

[0128] The processing module 502 is also used to write template files for smart contracts based on the data layer description information;

[0129] The processing module 502 is also used to render the data layer description information and template files to generate smart contracts.

[0130] In one embodiment, the processing module 502 is further configured to:

[0131] Create the data tables required for the target business in a relational database, with the data tables containing fields;

[0132] The business parameters carried in the business requirements are input as field values ​​into the data table to obtain the data layer description information.

[0133] In one embodiment, the processing module 502 is further configured to:

[0134] Identify the functionalities that match the table structure of the data table;

[0135] Write a template file for the smart contract based on the matching functionalities.

[0136] In one embodiment, the processing module 502 is further configured to:

[0137] The contract template engine is invoked to parse the fields in the data table and obtain the field values;

[0138] The contract template engine is invoked to parse the template file and determine the location of variables in the template file;

[0139] The parsed field values ​​are used as rule variables to fill the variable positions in the template file, thus obtaining the smart contract.

[0140] In one embodiment, the processing module 502 is further configured to:

[0141] Retrieve the operation function that matches the operation logic;

[0142] Add matching operation functions to the smart contract.

[0143] In one embodiment, the processing module 502 is further configured to:

[0144] When modification information for a smart contract is received, the smart contract is corrected according to the modification information.

[0145] In one embodiment, the processing module 502 is further configured to:

[0146] A test environment for deploying smart contracts;

[0147] The smart contract is tested in a test environment to obtain the test results.

[0148] In one embodiment, the processing module 502 is further configured to:

[0149] If the test results of the smart contract indicate that the smart contract can meet the business needs of the target business, then the smart contract file will be published to the blockchain.

[0150] In this embodiment, data layer description information can be defined for the smart contract corresponding to the target business according to the business requirements of the target business, and a template file for the smart contract can be written based on the data layer description information. By rendering the data layer description information and the template file of the smart contract, the smart contract for the target business can be automatically generated. The automated generation process of the smart contract is implemented based on the inverse object relational mapping method. This inverse object relational mapping method can convert the original data layer description information into the corresponding code of the smart contract. This method conforms to the data-driven idea, can reduce repetitive operations, simplify the development process of smart contracts, and effectively improve the development efficiency of smart contracts.

[0151] Figure 6 This is a schematic diagram of the structure of a computer device 60 provided in an exemplary embodiment of this application. The computer device 60 includes at least a processor 601, a memory 602, and a communication interface 603. The processor 601, memory 602, and communication interface 603 can be connected via a bus or other means. The device 60 may be, but is not limited to, a node, and the communication interface 603 can be used to receive or send data. The memory 602 stores a computer-readable storage medium for storing computer programs, including computer instructions. The processor 601 is used to execute computer instructions. The processor 601 (or CPU (Central Processing Unit)) is the computing and control core of the computer device 60, and is adapted to implement one or more computer instructions, specifically adapted to load and execute one or more computer instructions to achieve corresponding method flows or corresponding functions.

[0152] The memory 602 stores a computer program, which includes one or more computer instructions. The processor 601 loads and executes one or more computer instructions to implement the corresponding steps in the above-described embodiment of the blockchain smart contract generation method. Specifically, the computer instructions in the memory 602 are loaded and executed by the processor 601 in the following steps:

[0153] Obtain the business requirements of the target business;

[0154] Define data layer description information for smart contracts corresponding to the target business according to business requirements;

[0155] Write a template file for the smart contract based on the data layer description information;

[0156] The data layer description information and template files are rendered to generate smart contracts.

[0157] In one embodiment, the processor 601 is further configured to:

[0158] Create the data tables required for the target business in a relational database, with the data tables containing fields;

[0159] The business parameters carried in the business requirements are input as field values ​​into the data table to obtain the data layer description information.

[0160] In one embodiment, the processor 601 is further configured to:

[0161] Identify the functionalities that match the table structure of the data table;

[0162] Write a template file for the smart contract based on the matching functionalities.

[0163] In one embodiment, the processor 601 is further configured to:

[0164] The contract template engine is invoked to parse the fields in the data table and obtain the field values;

[0165] The contract template engine is invoked to parse the template file and determine the location of variables in the template file;

[0166] The parsed field values ​​are used as rule variables to fill the variable positions in the template file, thus obtaining the smart contract.

[0167] In one embodiment, the processor 601 is further configured to:

[0168] Retrieve the operation function that matches the operation logic;

[0169] Add matching operation functions to the smart contract.

[0170] In one embodiment, the processor 601 is further configured to:

[0171] When modification information for a smart contract is received, the smart contract is corrected according to the modification information.

[0172] In one embodiment, the processor 601 is further configured to:

[0173] A test environment for deploying smart contracts;

[0174] The smart contract is tested in a test environment to obtain the test results.

[0175] In one embodiment, the processor 601 is further configured to:

[0176] If the test results of the smart contract indicate that the smart contract can meet the business needs of the target business, then the smart contract file will be published to the blockchain.

[0177] In this embodiment, data layer description information can be defined for the smart contract corresponding to the target business according to the business requirements of the target business, and a template file for the smart contract can be written based on the data layer description information. By rendering the data layer description information and the template file of the smart contract, the smart contract for the target business can be automatically generated. The automated generation process of the smart contract is implemented based on the inverse object relational mapping method. This inverse object relational mapping method can convert the original data layer description information into the corresponding code of the smart contract. This method conforms to the data-driven idea, can reduce repetitive operations, simplify the development process of smart contracts, and effectively improve the development efficiency of smart contracts.

[0178] This application embodiment also provides a computer-readable storage medium (Memory), which is a memory device in the computer device 60 for storing programs and data. It is understood that the computer-readable storage medium here may include the built-in storage medium in the computer device 60, or it may include extended storage media supported by the computer device 60. The computer-readable storage medium provides storage space that stores the operating system of the computer device 60. Furthermore, the storage space also stores one or more computer instructions suitable for loading and execution by the processor 601. These computer instructions may be one or more computer programs (including program code). It should be noted that the computer-readable storage medium here may be high-speed RAM memory, or non-volatile memory, such as at least one disk storage device; optionally, it may also be at least one computer-readable storage medium located remotely from the aforementioned processor 601.

[0179] One or more embodiments of this application also provide a computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the steps performed in the embodiments of the methods described above.

[0180] The above embodiments merely illustrate several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.

Claims

1. A method for generating smart contracts on a blockchain, characterized in that, The method includes: Obtain the business requirements of the target service configured in the output configuration interface; the business requirements carry the business parameters of the target service; Create the data table required for the target business in a relational database. The data table is a two-dimensional table. The first row of the two-dimensional table is the field name. When creating the data table, the number of parameters is determined according to the business parameters of the target business. Multiple fields with the same number of parameters are created in the first row of the two-dimensional table. The business parameters carried in the business requirements are input as field values ​​into the data table to obtain data layer description information; Retrieve functionalities from the template library that match the table structure of the data table; Write the template file for the smart contract based on the matching function; The contract template engine is invoked to parse and render the data layer description information and the template file to generate the smart contract. Obtain operation functions that match the operation logic contained in the business requirements; wherein, if the business requirements indicate that write operations are allowed on the data layer, then the operation logic includes write operation logic and the operation function includes write operation functions; or, if the business requirements indicate that read operations are allowed on the data layer, then the operation logic includes read operation logic and the operation function includes read operation functions; or, if the business requirements indicate that query operations are allowed on the data layer, then the operation logic may also include query operation logic and the operation function includes query operation functions. Add the matching operation function to the smart contract. The smart contract with the added operation function enables the blockchain system to call the corresponding operation function in the smart contract to perform data operations on the data table corresponding to the target business when it receives a business request for the target business.

2. The method of claim 1, wherein, The process of calling the contract template engine to parse and render the data layer description information and the template file to generate the smart contract includes: The contract template engine is invoked to parse the fields in the data table and obtain the field values; The contract template engine is invoked to parse the template file and determine the location of variables in the template file; The parsed field values ​​are used as rule variables to fill the variable positions in the template file to obtain the smart contract.

3. The method of claim 1, wherein, If the operation logic includes write operation logic, then the operation function includes a write operation function; or... If the operation logic includes read operation logic, then the operation function includes a read operation function; or, If the operation logic includes query operation logic, then the operation function includes a query operation function.

4. The method according to any one of claims 1 to 3, characterized in that, The method further includes: outputting the smart contract; When modification information for the smart contract is received, the smart contract is corrected according to the modification information.

5. The method of claim 4, wherein, The method further includes: Deploy the test environment for the smart contract; The smart contract is tested in the test environment to obtain the test results.

6. The method of claim 5, wherein, The method further includes: If the test results of the smart contract indicate that the smart contract can meet the business requirements of the target business, then the smart contract file will be published to the blockchain. 7.A device for generating a smart contract of a blockchain, characterized by, The device includes: The acquisition module is used to acquire the business requirements of the target business; the business requirements carry the business parameters of the target business; The processing module is used to define data layer description information for the smart contract corresponding to the target business according to the business requirements; The processing module is also used to write a template file for the smart contract based on the data layer description information; The processing module is also used to call the contract template engine to parse and render the data layer description information and the template file to generate the smart contract; The processing module is further configured to obtain an operation function that matches the operation logic contained in the business requirement; add the matching operation function to the smart contract, and the smart contract with the added operation function enables the blockchain system to call the corresponding operation function in the smart contract to perform data operations on the data table corresponding to the target business when it receives a business request initiated for the target business; The processing module is used to create a data table required by the target business in a relational database. The data table is a two-dimensional table, and the first row of the two-dimensional table is the field name. When creating the data table, the number of parameters is determined according to the business parameters of the target business. Multiple fields with the same number of parameters are created in the first row of the two-dimensional table. The business parameters carried in the business requirements are input as field values ​​into the data table to obtain the data layer description information. The processing module is also used to obtain functional functions from the template library that match the table structure of the data table; and to write the template file of the smart contract based on the matching functional functions.

8. A computer device, comprising: include: A processor, adapted to execute computer programs; A computer-readable storage medium storing a computer program, which, when executed by the processor, implements the method for generating a smart contract for a blockchain as described in any one of claims 1-6.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program adapted to be loaded by a processor and executed by the method for generating a smart contract for a blockchain according to any one of claims 1 to 6.

10. A computer program product comprising computer instructions, characterized in that, When the computer instructions are executed by the processor, they implement the method for generating smart contracts for a blockchain as described in any one of claims 1 to 6.