A smart contract access method and device
By reading the ABI field from the JSON file of a smart contract, and generating header and source code files in C language, the problem of cumbersome C language calls to smart contracts in existing technologies is solved, improving coding efficiency and reducing costs.
Patent Information
- Application Number
- CN202211216210.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-30
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2042-09-30
AI Technical Summary
In existing technologies, when calling smart contracts using C language, ABI encoding needs to be performed manually, which results in tedious coding and low development efficiency.
A method for accessing smart contracts is provided, which generates a C language access method by reading ABI fields. The method includes: reading the application binary interface ABI field from the JSON file generated after the smart contract to be accessed is compiled; generating the corresponding C language type in the header file and source program file according to the type of the read ABI field; and accessing the smart contract through the header file and source program file.
It enables the automatic generation of C language type files, improving coding efficiency and reducing coding costs.
Smart Images

Figure CN115495092B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of computers, and particularly to a smart contract access method and device. BACKGROUND
[0002] A smart contract is a core component of a blockchain, is an event-driven computer program with a state, and runs on a replicable shared blockchain data account book. The smart contract can actively or passively process data, accept, store and send values, and control and manage various types of on-chain smart assets. Taking the Ethereum series of blockchains as an example, a smart contract needs to be called through an ABI (Application Binary Interface). Usually, a blockchain developer only provides a JAVA or GO language version calling tool. If a C language is used to call a smart contract, ABI coding needs to be manually performed. The disadvantage of manually performing ABI coding is that the coding needs to be converted for each function input of the smart contract, which is very tedious. The conversion content needs to be changed with the change of the smart contract input, and the coding personnel need to master the ABI coding rules, which increases the labor cost. SUMMARY
[0003] The present application provides a smart contract access method and device, which can access a smart contract in C language.
[0004] To solve the above technical problems, the present application provides a smart contract access method, comprising:
[0005] reading an application binary interface (ABI) field from a JSON file generated after a smart contract to be accessed is compiled;
[0006] generating a corresponding C language type in a header file and a source program file according to the type of the read ABI field;
[0007] accessing the smart contract through the header file and the source program file;
[0008] The generation of the corresponding C language type in the header file and the source program file according to the type of the read ABI field comprises:
[0009] for each contract function in the ABI field, generating a code for converting the contract function to a C language function according to the input parameter type of the contract function and the type of the blockchain corresponding to the contract function, wherein the type of the blockchain includes modifying a blockchain state and not modifying the blockchain state;
[0010] encapsulating the generated C language function code of each contract function in the header file and the source program file.
[0011] In an example, the generating code for converting the contract function into a C language function according to the input parameter type of the contract function and the category of the blockchain corresponding to the contract function comprises:
[0012] According to the input parameter type of the contract function, generating code for counting the total length after encoding, code for filling in the function selector, and code for filling in data;
[0013] Judging the category of the blockchain corresponding to the contract function, when the category of the blockchain is modifying the state of the blockchain, generating interface code for calling the function of changing the state of the blockchain and generating return code for storing the evidence on the chain; when the category of the blockchain is not modifying the state of the blockchain, generating interface code for calling the function of not changing the state of the blockchain and generating code for returning the data obtained.
[0014] In an example, the generating code for counting the total length after encoding comprises:
[0015] Judging whether the data corresponding to the input parameter type is fixed-length data and judging whether the number of data corresponding to the input parameter type is fixed;
[0016] When the data is not fixed-length data or the number of data is not fixed, dynamically calculating the space size occupied by the data after encoding after the function is executed;
[0017] When the data is fixed-length data and the number of data is fixed, calculating the space size occupied by the data after encoding before the function is executed.
[0018] In an example, the input parameter type comprises: fixed-length data fixed-length, fixed-length data variable-length, variable-length data fixed-length, variable-length data variable-length, and tuple.
[0019] Fixed-length data refers to the length of the data being fixed, variable-length data refers to the length of the data being variable, fixed-length refers to the number of data being fixed, and variable-length refers to the number of data being variable.
[0020] The tuple is data composed of one or more of fixed-length data fixed-length, fixed-length data variable-length, variable-length data fixed-length, variable-length data variable-length, and tuple.
[0021] In an example, the generating code for counting the total length after encoding further comprises:
[0022] When the input parameter type is fixed-length data fixed-length, modifying the data corresponding to the type into a 32-byte array;
[0023] When the input parameter type is the following type, the length of the array is added in the input of the function: fixed-length data indefinite-length, indefinite-length data fixed-length, indefinite-length data indefinite-length, tuple containing fixed-length data indefinite-length, indefinite-length data fixed-length, or indefinite-length data indefinite-length data.
[0024] In an example, the JSON file generated after the smart contract to be accessed is compiled includes an application binary interface (ABI) field.
[0025] The JSON file generated after the smart contract is compiled by the contract compiler is read by a pre-written PYTHON script.
[0026] The ABI field is read from the JSON file.
[0027] In an example, when the type of the blockchain is to modify the state of the blockchain, interface code for calling the change of the state of the blockchain is generated, and code for returning the on-chain storage is generated; when the type of the blockchain is not to modify the state of the blockchain, interface code for calling the change of the state of the blockchain is generated, and code for returning the obtained data is generated.
[0028] When the type of the blockchain is to modify the state of the blockchain, the transaction hash value of the blockchain is returned by a C language function.
[0029] When the type of the blockchain is not to modify the state of the blockchain, the return value of the blockchain is returned by a C language function.
[0030] The application also provides a smart contract access device, comprising a memory and a processor.
[0031] The memory is configured to store a program for smart contract access.
[0032] The processor is configured to read the program for smart contract access and execute any of the above methods.
[0033] The application also provides a computer storage medium, wherein the storage medium stores a computer program, and the computer program is configured to execute any of the above methods when running.
[0034] The application provides a smart contract access method, comprising: reading an application binary interface (ABI) field from a JSON file generated after a smart contract to be accessed is compiled; generating a corresponding C language type in a header file and a source program file according to a type of the read ABI field; accessing the smart contract through the header file and the source program file; wherein the generating the corresponding C language type in the header file and the source program file according to the type of the read ABI field comprises: for each contract function in the ABI field, generating code for converting the contract function into a C language function according to an input parameter type of the contract function and a type of a blockchain corresponding to the contract function, wherein the type of the blockchain comprises modifying a blockchain state and not modifying the blockchain state; and encapsulating the code of the C language function corresponding to each contract function in the header file and the source program file. Through the above technical solution, the C language type file can be automatically generated, and the smart contract can be accessed through the file, thereby improving the coding efficiency and reducing the coding cost. BRIEF DESCRIPTION OF DRAWINGS
[0035] The accompanying drawings are included to provide a further understanding of the present application, and constitute a part of this specification, illustrate embodiments of the present application, and are used to explain the technical solutions of the present application, and do not constitute a limitation on the technical solutions of the present application.
[0036] Figure 1 is a flowchart of the smart contract access method of the first embodiment of the present application;
[0037] Figure 2 is a flowchart of the generation method of the header file and the source program file of the first embodiment of the present application;
[0038] Figure 3 is a schematic diagram of the smart contract access device of the first embodiment of the present application. DETAILED DESCRIPTION
[0039] The present application describes a plurality of embodiments, but the description is exemplary rather than limiting, and it is obvious to those skilled in the art that there can be more embodiments and implementation schemes within the scope of the embodiments described in the present application. Although many possible combinations of features are shown in the drawings and discussed in the specific embodiments, many other combinations of the disclosed features are also possible. Unless specifically limited, any feature or element of any embodiment can be used in combination with any other feature or element of any other embodiment, or can replace any other feature or element of any other embodiment.
[0040] The present application includes and contemplates combinations of features and elements known to those of ordinary skill in the art. The embodiments, features, and elements disclosed herein can also be combined with any conventional feature or element to form a unique and inventive solution that is defined by the claims. Any feature or element of any embodiment can also be combined with features or elements from other inventive solutions to form another unique and inventive solution that is defined by the claims. Therefore, it is to be understood that any feature shown and / or discussed in the present application can be implemented alone or in any appropriate combination. Embodiments are, therefore, not to be limited to any one specific combination of features. Rather, the specific combination of features made in the claims is to be determined by the language of the claims, and it is the claims, therefore, that establish the limitations of the application. Moreover, various modifications and changes can be made within the scope of the claims.
[0041] Furthermore, in describing representative embodiments, the specification can have presented the method and / or process as a particular sequence of steps. However, to the extent that the method or process depends on more than one step, the method or process should not be limited to the particular sequence of steps described. Other sequences of steps can be possible, and are within the scope of the application. Therefore, the particular sequence of steps set forth in the specification is not a limitation of the application. Moreover, the intervals between steps in the disclosed methods and / or processes can be eliminated, and the steps can be performed concurrently or in an order other than that described.
[0042] Embodiment one
[0043] As shown in the embodiment, the method for accessing a smart contract comprises the following steps: Figure 1
[0044] Step S101, reading an application binary interface (ABI) field from a JSON file generated after a smart contract to be accessed is compiled;
[0045] Step S102, generating a corresponding C language type in a header file and a source program file according to a type of the read ABI field;
[0046] Step S103, accessing the smart contract through the header file and the source program file.
[0047] In the embodiment, the step of generating a corresponding C language type in a header file and a source program file according to a type of the read ABI field comprises:
[0048] For each contract function in the ABI field, a code for converting the contract function into a C language function is generated according to an input parameter type of the contract function and a type of a blockchain corresponding to the contract function, wherein the type of the blockchain comprises modifying a blockchain state and not modifying the blockchain state.
[0049] Encapsulate the code of the C language function corresponding to each contract function in the header file and the source program file.
[0050] By the above technical solution, the C language type file can be automatically generated, and the smart contract can be accessed through the file, so that the coding efficiency is improved and the coding cost is reduced.
[0051] In an exemplary embodiment, the code for converting the contract function into a C language function according to the input parameter type of the contract function and the type of the blockchain corresponding to the contract function includes:
[0052] According to the input parameter type of the contract function, the code for counting the total length after coding, the code for filling in the function selector, and the code for filling in the data are generated;
[0053] Determine the type of the blockchain corresponding to the contract function. When the type of the blockchain is to modify the state of the blockchain, generate the interface code for calling the change of the state of the blockchain and generate the return storage code for chaining; when the type of the blockchain is not to modify the state of the blockchain, generate the interface code for calling the change of the state of the blockchain and generate the return data code.
[0054] For each contract function in the smart contract, the code of the corresponding C language function is generated. After the generation is completed, all the codes are saved in the header file and the source file, and a file for calling the blockchain smart contract by the C language is generated.
[0055] In an exemplary embodiment, the code for counting the total length after coding includes:
[0056] Determine whether the data corresponding to the input parameter type is fixed-length data and whether the number of data corresponding to the input parameter type is fixed;
[0057] When the data is not fixed-length data or the number of data is not fixed, the space size occupied by the data after coding is dynamically calculated after the function is executed;
[0058] When the data is fixed-length data and the number of data is fixed, the space size occupied by the data after coding is calculated before the function is executed.
[0059] In an exemplary embodiment, the input parameter type includes: fixed-length data fixed-length, fixed-length data variable-length, variable-length data fixed-length, variable-length data variable-length, and tuple;
[0060] The fixed-length data refers to the length of the data being determined, the indefinite-length data refers to the length of the data being not determined, the fixed-length length refers to the number of data being determined, and the indefinite-length length refers to the number of data being not determined;
[0061] The tuple is data composed of one or more of the fixed-length data fixed-length length, the fixed-length data indefinite-length length, the indefinite-length data fixed-length length, the indefinite-length data indefinite-length length, and the tuple.
[0062] In an example embodiment, the code for generating the total length after statistical encoding further comprises:
[0063] When the input parameter type is the fixed-length data fixed-length length, the data corresponding to the type is modified into an array of 32 bytes;
[0064] When the input parameter type is the following type, the length of the array is added to the input of the function: the fixed-length data indefinite-length length, the indefinite-length data fixed-length length, the indefinite-length data indefinite-length length, the tuple containing the fixed-length data indefinite-length length, the indefinite-length data fixed-length length, or the indefinite-length data indefinite-length length data.
[0065] In this embodiment, the input parameter type can be divided into the following types according to the encoding mode:
[0066] The first type is the fixed-length data fixed-length length, for example, address or int
[10] , which is a single data or a data of a fixed-length array. The fixed-length data refers to the length of the data being determined, the indefinite-length data refers to the length of the data being not determined, the fixed-length length refers to the number of data being determined, and the indefinite-length length refers to the number of data being not determined.
[0067] The second type is the fixed-length data indefinite-length length, for example, int[], the actual number of the array to be transmitted is not determined, and is determined only when the program is executed.
[0068] The third type is the indefinite-length data fixed-length length, for example, string or string
[10] , the length of the data is not determined, but the number of data is determined.
[0069] The fourth type is the indefinite-length data indefinite-length length, for example, string[], the length of the data and the number of data are not determined.
[0070] The fifth type is the tuple, which is composed of any order of the five types, is a data type defined by Solidity (a high-level language for writing smart contracts), and is a data structure in which at least one input parameter type is placed in a pair of parentheses and separated by a comma, such as (address, int[], string, string
[10] , tuple) being a tuple.
[0071] ABI supports many types, but not every type has a corresponding C language type, so the user needs to be provided with the corresponding C language type to implement various types in ABI. For example, the int type of Solidity is a 256-bit integer, while most C languages only support up to 64-bit integers, so the int of Solidity can be changed to an 8-bit array of 32 bytes, and the length of 8 times 32 is 256 bits, which is consistent with the int type of Solidity. In addition, for negative numbers of int type, additional transcoding is required.
[0072] Solidity also has uint8[] format of fixed-length data with variable-length arrays, which is not supported in C language, so the C language function needs to additionally pass in the length of the uint8 pointer. The variable-length data with variable-length (such as string[]) contains a length in each data, and the length of the array is also included in the input function.
[0073] In the process of generating C language functions, the space after encoding needs to be applied according to the input parameter type to store the generated encoded data. Since some data can only be determined at runtime, after the C language execution enters the function, all data needs to be traversed first to calculate the occupied space.
[0074] For variable-length data or variable-length data, the length of the data or the length of the array will appear as an input value in the C language function, so the space size occupied by the encoded data will be dynamically calculated. For fixed-length data, since the data length is fixed, the PYTHON function will calculate the length, and when generating, the length calculated will be statically added to the total length in the C language function.
[0075] For example, the input format of string is variable-length data with fixed-length, and the specific length can only be calculated after the function is executed. The length will occupy the number of characters in the string divided by 32, rounded up, and then multiplied by 32. When using the string "Hello" as input to send to the block link interface, it will first occupy 32 bytes of space jump, and then occupy 32 bytes to store the content of Hello.
[0076] For tuples, since tuples can be nested tuples, the length of each data in all nested tuples and the placeholder for jump need to be calculated, and all data is added up. After completing the memory allocation, fill in the four-byte function selector, and then fill in the remaining memory part according to the content of the ABI.
[0077] Furthermore, different blockchains provide different interfaces, which are also encapsulated as parameters in header and source files. Therefore, it is necessary to distinguish this state in the header and source files and add different function call code. For each contract function, the input parameters are converted into corresponding C language type input parameters according to the input parameter type of the contract function, thus obtaining the converted function header. Then, the function's internal code is ABI encoded according to the input parameter type and the number of data corresponding to the input parameter type.
[0078] In one exemplary embodiment, reading the Application Binary Interface (ABI) field from the JSON file generated after the smart contract to be accessed is compiled includes:
[0079] The JSON file generated by the contract compiler after compiling the smart contract is read through a pre-written PYTHON script;
[0080] The ABI field is read from the JSON file.
[0081] In one exemplary embodiment, when the blockchain type is "modifying blockchain state," generating interface code to retrieve the changed blockchain state and generating code to return on-chain evidence storage; when the blockchain type is "not modifying blockchain state," generating interface code to retrieve the unmodified blockchain state and generating code to return the obtained data includes:
[0082] When the type of blockchain is modifying the blockchain state, the state query return value of the blockchain is returned through a C language function;
[0083] When the blockchain type is one that does not modify the blockchain state, the transaction hash value of the blockchain is returned through a C language function.
[0084] Different processes need to be executed for different blockchain interfaces. Calls that do not change the blockchain state typically query the state, so the returned value of the generated C function should be the blockchain's return value sent to the user. Calls that change the blockchain state generate transactions, in which case the blockchain returns a transaction hash, so the returned value of the generated C function should be the transaction hash returned by the blockchain sent to the user.
[0085] like Figure 2 The methods for generating header files and source files shown include:
[0086] Step S201: Read the ABI field;
[0087] Step S202: Determine whether the contract function has been read. If the contract function has been read, proceed to step S203. If the contract function has not been read, proceed to step S211.
[0088] Step S203, generating code of total length after statistical encoding;
[0089] Step S204, generating code of filling function selector;
[0090] Step S205, generating code of filling data;
[0091] Step S206, judging whether the type of the blockchain corresponding to the contract function is modifying the blockchain state, if the type of the blockchain corresponding to the contract function is not modifying the blockchain state, turning to step S207, if the type of the blockchain corresponding to the contract function is modifying the blockchain state, turning to step S209;
[0092] Step S207, generating code of calling the interface of not changing the blockchain state;
[0093] Step S208, generating code of returning the obtained data; turning to step S202;
[0094] Step S209, generating code of calling the interface of changing the blockchain state;
[0095] Step S210, generating code of returning the on-chain storage; turning to step S202;
[0096] Step S211, saving the header file and the source program file;
[0097] Step S212, ending.
[0098] Through the above technical solution, the header file and the source program file of C language type can be automatically generated, so that the smart contract can be accessed through C language.
[0099] As shown in Figure 3 , the embodiment further provides a smart contract access device, comprising a memory 10 and a processor 11;
[0100] The memory 10 is used to save a program for smart contract access;
[0101] The processor 11 is used to read the program for smart contract access, and execute the smart contract access method.
[0102] The embodiment further provides a computer storage medium, and the storage medium stores a computer program, wherein the computer program is set to execute the smart contract access method when running.
[0103] Those of ordinary skill in the art will realize and understand that all or some of the steps in the methods disclosed above and the functional modules / units in the systems and devices can be implemented as software, firmware, hardware, and appropriate combinations thereof. In hardware implementation, the division between the functional modules / units mentioned in the above description does not necessarily correspond to the division of physical components; for example, one physical component can have multiple functions, or one function or step can be performed by several physical components in cooperation. Some or all of the components can be implemented as software executed by a processor, such as a digital signal processor or a microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software can be distributed on computer-readable media, which can include computer storage media (or non-transitory media) and communication media (or transitory media). As is well known to those of ordinary skill in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storage of information such as computer readable instructions, data structures, program modules or other data. Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tapes, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to store the desired information and which can be accessed by a computer. Furthermore, it is common and well understood by those of ordinary skill in the art that communication media typically embodies computer readable instructions, data structures, program modules or other data in a modulated data signal such as a carrier wave or other transport mechanism and can include any information delivery media.
Claims
1. A method for accessing a smart contract, comprising: Read the application binary interface ABI field from the JSON file generated after the smart contract to be accessed is compiled; Based on the type of the ABI field read, generate the corresponding C language type in the header file and source program file; Access the smart contract through the header file and source file; The step of generating corresponding C language types in the header file and source program file based on the type of the read ABI field includes: For each contract function in the ABI field, code is generated to convert the contract function into a C language function based on the type of input parameters of the contract function and the type of blockchain corresponding to the contract function. The type of blockchain includes those that modify the blockchain state and those that do not modify the blockchain state. The code of the C language function corresponding to each contract function is encapsulated in the header file and the source program file. 2.The smart contract access method of claim 1, wherein, The step of generating code to convert the contract function into a C language function based on the input parameter types of the contract function and the type of blockchain corresponding to the contract function includes: Based on the input parameter type of the contract function, generate code for calculating the total length after encoding, code for filling in the function selector, and code for filling in the data; Determine the type of blockchain corresponding to the contract function. If the blockchain type is to modify the blockchain state, generate the interface code to call the change of the blockchain state and generate the code to return the on-chain evidence. If the blockchain type is not to modify the blockchain state, generate the interface code to call the non-change of the blockchain state and generate the code to return the obtained data. 3.The smart contract access method of claim 2, wherein, The code generated for calculating the total length after encoding includes: Determine whether the data corresponding to the input parameter type is fixed-length data and whether the number of data corresponding to the input parameter type is fixed; When the data is of variable length or the amount of data is uncertain, the space occupied by the encoded data is dynamically calculated after the function is executed; When the data is of fixed length and the number of data is fixed, calculate the space occupied by the encoded data before the function is executed.
4. The smart contract access method as described in any one of claims 1 to 3, characterized in that: The input parameter types include: fixed-length data with fixed length, fixed-length data with variable length, variable-length data with fixed length, variable-length data with variable length, and tuples; Fixed-length data refers to data with a fixed length, while variable-length data refers to data with an uncertain length. Fixed-length data refers to data with a fixed quantity, while variable-length data refers to data with an uncertain quantity. The tuple is data composed of one or more of the following: fixed-length data with fixed length, fixed-length data with variable length, variable-length data with fixed length, variable-length data with variable length, and tuple. 5.The smart contract access method of claim 4, wherein, The generation of code for calculating the total length after encoding also includes: When the input parameter type is fixed-length data, the data corresponding to that type will be modified into a 32-byte array. When the input parameter type is one of the following types, the length of the array is added in the input of the function: fixed-length data indefinite-length, indefinite-length data fixed-length, indefinite-length data indefinite-length, tuple containing fixed-length data indefinite-length, indefinite-length data fixed-length, or indefinite-length data indefinite-length data. 6.The smart contract access method of claim 1, wherein, The application binary interface ABI field included in the JSON file generated after the smart contract to be accessed is compiled comprises: A JSON file generated after a smart contract is compiled by a contract compiler is read through a pre-written PYTHON script; The ABI field is read through the JSON file. 7.The smart contract access method of claim 2, wherein, When the type of the blockchain is to modify the state of the blockchain, interface code for calling the change of the state of the blockchain is generated, and code for returning the stored evidence on the chain is generated; When the type of the blockchain is not to modify the state of the blockchain, interface code for calling the change of the state of the blockchain is generated, and code for returning the stored data is generated. When the type of the blockchain is to modify the state of the blockchain, the transaction hash value of the blockchain is returned through a C language function. When the type of the blockchain is not to modify the state of the blockchain, the return value of the blockchain is returned through a C language function.
8. A smart contract access apparatus comprising: A memory and a processor; characterized in that: The memory is used to save a program for smart contract access; The processor is used to read the program for smart contract access and execute the method of any one of claims 1 to 7.
9. A computer storage medium, characterized in that The computer program is stored in the storage medium, and the computer program is set to execute the method of any one of claims 1 to 7 when running.
Citation Information
Patent Citations
Smart contract execution method and device, equipment and medium
CN110096338A
System and method for implementing native contract on blockchain
US20200133926A1