A method for creating a consortium chain smart contract template

By defining entity class base classes and encapsulating native smart contract operation objects, providing smart contract template creation methods, solving the problems of complexity and supervision difficulty of smart contract development on the alliance chain platform, and simplifying development and improving maintainability.

CN114218221BActive Publication Date: 2025-08-22CHINA ELECTRONICS TECH CYBER SECURITY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111559375.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-20
Publication Date
2025-08-22
Estimated Expiration
2041-12-20

AI Technical Summary

Technical Problem

The existing alliance chain platform has a high threshold for writing smart contracts. Contract developers need to understand the internal data format and increase the difficulty of supervision. The platform cannot effectively count and process data, which reduces the credibility of the platform.

Method used

By defining entity class base classes, encapsulating native smart contract operation objects, mapping fields to table structures, and encapsulating external KV smart contract interfaces, providing smart contract template creation methods, simplifying contract development processes, and supporting operations such as adding, deleting, modifying, and checking.

Benefits of technology

It reduces the cost of smart contract development, reduces human programming errors, optimizes the project structure, and improves the clarity and maintainability of the project.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114218221B_ABST
    Figure CN114218221B_ABST
Patent Text Reader

Abstract

This invention discloses a method for creating a consortium chain smart contract template, comprising the following steps: S1. defining an entity class base class; S2. encapsulating native smart contract operation objects; S3. encapsulating the operation class as a table object through entity class definition; S4. mapping fields to a table structure through reflection; S5. encapsulating an external KV smart contract interface; and S6. encapsulating the KV smart contract startup and debugging methods to complete the creation of the consortium chain smart contract template. By encapsulating native smart contracts, this invention allows smart contract developers to implement simple entity definitions and automatically implement contract methods that can add, delete, modify, and query world states, significantly reducing the development cost of such simple contracts.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of alliance chains, and specifically relates to a method for creating an alliance chain smart contract template. Background Art

[0002] Smart contracts, as logical units running on consortium blockchain platforms, possess data processing, storage, and verification capabilities. Currently, consortium blockchain platforms often incorporate concepts such as blockchain ledgers and contract proxies, creating a barrier to entry for first-time users or those seeking simple services.

[0003] In addition, smart contract callers often need to know the status of ledger data so they can verify the correctness of business data by obtaining the status. Currently, consortium chain platforms support synchronizing data from peer ledgers. However, due to the differences in smart contracts, the data stored in the ledgers is diverse. Only the contract developers know the internal data format, and the platform cannot use the on-chain data for statistics and processing, which increases the difficulty of supervision and reduces the platform's credibility. Summary of the Invention

[0004] The purpose of the present invention is to provide a method for creating a smart contract template for a consortium chain in order to overcome the shortcomings of the prior art.

[0005] The purpose of the present invention is achieved through the following technical solution: A method for creating a consortium chain smart contract template, comprising the following steps:

[0006] S1. Define the entity class base class;

[0007] S2. Encapsulate native smart contract operation objects;

[0008] S3. Define the entity class and encapsulate the operation class as a table object;

[0009] S4. Map fields to table structures through reflection;

[0010] S5. Encapsulate the external KV smart contract interface;

[0011] S6. Encapsulate the KV smart contract startup and debugging methods to complete the creation of the alliance chain smart contract template.

[0012] Furthermore: the step S1 specifically includes: defining the field name Id as the entity primary key, and the value is set by the business party.

[0013] Furthermore, step S2 is specifically as follows: the output parameter of the encapsulation function GetChainCodeType is the smart contract type; the input parameter of the encapsulation function GetEntryTypeByTableName is the taleName table name string, and the output parameter is the pointer type of the entity class corresponding to the table name; the input parameter of the encapsulation function Entry is the function method name and the parameters passed in by args, and the output parameter is whether it is successful; the input parameter of the encapsulation function Init is the function method name and the parameters passed in by args, and the output parameter is whether it is successful; the output parameter of the encapsulation function GetBusinessId is the business ID, and the output parameter of the encapsulation function Debug is a Bool value.

[0014] Further: the step S3 is specifically as follows: the input parameter of the encapsulation function set is the key, which uses BuildKey to create a string and a value Entity object, and the output parameter is the error object; the input parameter of the encapsulation function Get is the key of Set, and the output parameter is Value and the error object; the input parameter of the encapsulation function GetFieldValue is the key of Set and the fieldNames field name, and the output parameter is the value returned successfully; the input parameter of the encapsulation function Update is the key, which uses BuildKey to create a string and a value Entity object, and the output parameter is the error object; the input parameter of the encapsulation function UpdateFieldValue is the string, entry Entity object and pair map object created by key using BuildKey, and the output parameter is whether Error is successful; the input parameter of the encapsulation function Delete is the string created by key using BuildKey, and the output parameter is whether Error is successful.

[0015] Furthermore: the step S4 specifically includes: implementing the mapping of the Entity field and the Table field through the Golang Reflect package.

[0016] Further: the step S5 is specifically: encapsulating the interface Get as [{single table name}, {primary key ID}; encapsulating the interface GetFieldValue as [{single table name}, {primary key ID}, {field name 1, field name 2…}]; encapsulating the interface Set as [{single table name}, {primary key ID}, {json string corresponding to entity}]; encapsulating the interface Update as [{single table name}, {primary key ID}, {json string corresponding to entity}]; encapsulating the interface UpdateFieldValue as [{single table name}, {primary key ID}, {corresponding json string to be modified}]; encapsulating the interface Delete as [{single table name}, {primary key ID}]; and defining the custom method as [{table name 1 (, table name 2…)}, {business data}].

[0017] Furthermore, step S6 is specifically as follows: the input parameter of the encapsulation function MockInit is the parameter passed in by args, and the output parameter is Msg, payload, and status; the input parameter of the encapsulation function MockInvoke is the function method name and the parameter passed in by args, and the output parameter is Msg, payload, and status; the input parameter of the encapsulation function Start is the smart contract object.

[0018] The aforementioned main solution of the present invention and its various further options can be freely combined to form multiple solutions, all of which can be adopted and protected by the present invention. After understanding the solutions of the present invention, those skilled in the art will understand that there are many combinations based on existing technology and common knowledge, all of which are technical solutions to be protected by the present invention, and these are not exhaustive here.

[0019] Beneficial effects of the present invention: By encapsulating native smart contracts, the present invention allows smart contract developers to define simple entities and automatically implement contract methods that can add, delete, modify, and query world states, greatly reducing the development cost of such simple contracts.

[0020] For contracts with complex business logic, this invention provides table-based operations through data abstraction, allowing data to be added, deleted, modified, and queried using primary keys. By obtaining entity names and determining the key value of the internal world state, this approach avoids human programming errors during operation, reduces contract maintenance costs, optimizes the overall project structure, and improves project clarity and maintainability. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION

[0022] The following describes the embodiments of the present invention through specific examples. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments. The details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other unless they conflict.

[0023] It should be noted that in order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention are clearly and completely described below. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments.

[0024] In the description of the present invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer" indicate positions or relationships typically used when the product of the invention is in use. These terms are intended solely to facilitate and simplify the description of the present invention and are not intended to indicate or imply that the devices or components referred to must have a specific orientation, be constructed, or operate in a specific orientation. Therefore, they should not be construed as limitations on the present invention. Furthermore, the terms "first," "second," and "third," etc., are used solely for distinction and should not be construed as indicating or implying relative importance.

[0025] Furthermore, terms such as "horizontal," "vertical," and "overhanging" do not necessarily imply that a component must be absolutely horizontal or overhanging, but rather that it can be slightly tilted. For example, "horizontal" simply means that its direction is more horizontal than "vertical," and does not mean that the structure must be completely horizontal, but rather that it can be slightly tilted.

[0026] In the description of the present invention, it should also be noted that, unless otherwise expressly specified or limited, the terms "disposed," "installed," "connected," and "connected" should be understood in a broad sense. For example, they may refer to fixed connections, detachable connections, or integral connections; they may refer to mechanical connections or electrical connections; they may refer to direct connections or indirect connections through an intermediate medium; and they may refer to internal communication between two components. Those skilled in the art will understand the specific meanings of the above terms in the present invention based on the specific circumstances.

[0027] In addition, the present invention would like to point out that, in the present invention, unless the specific structure, connection relationship, positional relationship, power source relationship, etc. are specifically written out, the structure, connection relationship, positional relationship, power source relationship, etc. involved in the present invention are all known to those skilled in the art based on the existing technology without creative work.

[0028] Example 1:

[0029] The smart contract template is based on the Golang language, encapsulating the internal logic of the blockchain and providing default contract methods such as Get, Set, Update, Delete, GetFieldValue, and UpdateFieldValue, similar to KV database operations. It provides smart contract developers with a variety of configuration modes and operation interfaces, allowing them to quickly develop smart contracts based on their own business.

[0030] The present invention encapsulates the smart contract operation class and provides two external methods: built-in contract functions and custom functions. The operations on the blockchain world state are encapsulated as operations on the KV database table.

[0031] refer to Figure 1 As shown, the present invention discloses a method for creating a consortium chain smart contract template, comprising the following steps:

[0032] S1. Define the entity class base class; as shown in Table 1:

[0033] Table 1 defines the entity class base class

[0034]

[0035] Business entities can inherit Entity.

[0036] Example:

[0037]

[0038]

[0039] Multiple can be defined.

[0040] Note: If structure nesting is used, the UpdateFieldValue and GetFieldValue interfaces are not currently supported. It is recommended to use primary key association to avoid structure nesting.

[0041] S2. Encapsulate the native smart contract operation object; as shown in Table 2:

[0042] Implement this interface to realize the smart contract logic. When implementing, you need to inherit SimpleXLChainCode

[0043] Table 2 Encapsulates native smart contract operation objects

[0044]

[0045] S3. Define the entity class and encapsulate the operation class as a table object, as shown in Table 3:

[0046] Table 3 encapsulates the operation class as a table object

[0047]

[0048] S4. Map fields to table structures through reflection;

[0049] The mapping between Entity fields and Table fields is implemented through the Golang Reflect package.

[0050] S5. Encapsulate the external KV smart contract interface; as shown in Table 4:

[0051] Table 4 Encapsulates the external KV smart contract interface

[0052]

[0053]

[0054] S6. Encapsulate the KV smart contract startup and debugging methods to complete the creation of the alliance chain smart contract template. As shown in Table 5:

[0055] Table 5 Encapsulated KV smart contract startup and debugging methods

[0056]

[0057] By encapsulating native smart contracts, smart contract developers can use simple entity definitions and automatically implement contract methods that can add, delete, modify, and query world states, greatly reducing the development cost of such simple contracts.

[0058] For contracts with complex business logic, this method provides table-based operations through data abstraction, allowing data to be added, deleted, modified, and queried using primary keys. By obtaining entity names and determining the key value of the internal world state, this method avoids human programming errors during operation, reduces contract maintenance costs, optimizes the overall project structure, and improves project clarity and maintainability.

[0059] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for creating a consortium chain smart contract template, characterized in that: The following steps are involved: S1. Define the entity class base class; S2. Encapsulate native smart contract operation objects; S3. Define the entity class and encapsulate the operation class as a table object; S4. Map fields to table structures through reflection; S5. Encapsulate the external KV smart contract interface; S6. Encapsulate the KV smart contract startup and debugging methods to complete the creation of the alliance chain smart contract template; The step S2 is specifically as follows: the output parameter of the encapsulation function GetChainCodeType is the smart contract type; the input parameter of the encapsulation function GetEntryTypeByTableName is the taleName table name string, and the output parameter is the pointer type of the entity class corresponding to the table name; the input parameter of the encapsulation function Entry is the function method name and the parameters passed in by args, and the output parameter is whether it is successful; the input parameter of the encapsulation function Init is the function method name and the parameters passed in by args, and the output parameter is whether it is successful; the output parameter of the encapsulation function GetBusinessId is the business ID, and the output parameter of the encapsulation function Debug is a Bool value; The step S3 is specifically as follows: the input parameter of the encapsulation function set is key, and BuildKey is used to create a string and a valueEntity object, and the output parameter is an error object; the input parameter of the encapsulation function Get is the key of Set, and the output parameter is Value and an error object; the input parameter of the encapsulation function GetFieldValue is the key of Set and the fieldNames field name, and the output parameter is the value returned successfully; the input parameter of the encapsulation function Update is key, and BuildKey is used to create a string and a valueEntity object, and the output parameter is an error object; the input parameter of the encapsulation function UpdateFieldValue is key, and BuildKey is used to create a string, an entry Entity object, and a pair map object, and the output parameter is whether Error is successful; the input parameter of the encapsulation function Delete is key, and BuildKey is used to create a string, and the output parameter is whether Error is successful; The step S5 is specifically as follows: encapsulating the Get interface as [{single table name}, {primary key ID}; encapsulating the GetFieldValue interface as [{single table name}, {primary key ID}, {field name 1, field name 2...}]; encapsulating the Set interface as [{single table name}, {primary key ID}, {json string corresponding to entity}]; encapsulating the Update interface as [{single table name}, {primary key ID}, {json string corresponding to entity}]; encapsulating the UpdateFieldValue interface as [{single table name}, {primary key ID}, {corresponding json string to be modified}]; encapsulating the Delete interface as [{single table name}, {primary key ID}]; defining the custom method as [{table name 1 (, table name 2...)}, {business data}]; The specific steps of step S6 are as follows: the input parameters of the encapsulation function MockInit are the parameters passed in by args, and the output parameters are Msg, payload, and status; the input parameters of the encapsulation function MockInvoke are the function method name and the parameters passed in by args, and the output parameters are Msg, payload, and status; the input parameter of the encapsulation function Start is the smart contract object.

2. The method for creating a consortium chain smart contract template according to claim 1, characterized in that: The step S1 specifically includes: defining the field name Id as the entity primary key, and setting the value by the business party.

3. The method for creating a consortium chain smart contract template according to claim 1, characterized in that: The step S4 is specifically: implementing the mapping of the Entity field and the Table field through the Golang Reflect package.