Implementation method of a blockchain smart contract and electronic device
Patent Information
- Application Number
- CN202210612020.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-31
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2042-05-31
AI Technical Summary
[0031]根据本申请示例实施例,通过将智能合约状态数据的序列化与反序列化直接在合约执行引擎中进行实现,封装于读取智能合约状态数据方法和写入智能合约状态数据方法中,并作为外部方法引入到智能合约中,从而无需智能合约开发者自行实现相应方法,降低了其开发成本,减少了智能合约代码量。
Smart Images

Figure CN115185674B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of blockchain technology, and specifically to a method for implementing blockchain smart contracts and an electronic device. Background Technology
[0002] With the continuous development and improvement of blockchain technology, especially consortium blockchain technology, blockchain is increasingly becoming a supporting technology for solutions in scenarios requiring trusted data sharing, inter-institutional collaboration, and verifiable data traceability. Smart contracts in blockchain, as a type of digital contract, are essentially application logic running within the blockchain, and are crucial for integrating blockchain technology into traditional industry applications. Summary of the Invention
[0003] This application aims to provide a method for implementing blockchain smart contracts. By directly implementing the serialization and deserialization of smart contract state data in the contract execution engine, and encapsulating them in the methods for reading and writing smart contract state data, these methods are introduced into the smart contract as external methods. This eliminates the need for smart contract developers to implement the corresponding methods themselves, thereby reducing their development costs and the amount of smart contract code.
[0004] According to one aspect of this application, a method for implementing a blockchain smart contract is proposed, comprising:
[0005] Implement methods for reading and writing smart contract state data in the smart contract execution engine;
[0006] The method for reading smart contract state data and the method for writing smart contract state data use heap memory allocation and deallocation methods, and the method for reading smart contract state data and the method for writing smart contract state data are called as external methods.
[0007] According to some embodiments, the method further includes:
[0008] The method for reading smart contract state data is invoked to obtain serialized smart contract state data and corresponding smart contract state data structure information from the blockchain according to parameter information, and the smart contract state data is deserialized into data object instances in the smart contract program through heap memory management methods.
[0009] The method for writing smart contract state data is invoked to obtain the structure information of the corresponding smart contract state data from the blockchain according to the parameter information, read the data object instance data from the smart contract program memory space, form serialized smart contract state data, and store it in the blockchain.
[0010] According to some embodiments, the method further includes:
[0011] Define a data structure for the state data of one or more smart contracts, and specify the data alignment byte size of each attribute variable within the data structure.
[0012] According to some embodiments, the method further includes:
[0013] For each data structure, generate corresponding structure information and structure information identifier. The structure information includes hierarchical information composed of basic data types, the byte size of each basic data type, and the data alignment byte size of the attribute variables of the basic data types within the data structure.
[0014] According to some embodiments, the method includes:
[0015] When deploying a smart contract, the structure information and the identifier of the smart contract state data are obtained and persistently stored together.
[0016] According to some embodiments, the method includes:
[0017] The method for reading smart contract state data and the method for writing smart contract state data are declared and defined;
[0018] When reading smart contract state data, the method for reading smart contract state data is called to obtain a target data object instance;
[0019] When writing smart contract state data, the method for writing smart contract state data is called to write smart contract state data according to the target data object instance.
[0020] According to some embodiments, the method includes:
[0021] The method for calling and reading smart contract state data includes:
[0022] The calling parameters of the method for reading smart contract state data include the structure information identifier and the smart contract state data identifier of the smart contract state data;
[0023] The return value of the method for reading smart contract state data includes the memory address of the obtained binary form of the smart contract state data;
[0024] Then, the binary data pointed to by the memory address is converted into the data object instance.
[0025] According to some embodiments, the method includes:
[0026] The method for writing smart contract state data includes:
[0027] The call parameters of the method for writing smart contract state data include the structure information identifier of the smart contract state data, the smart contract state data identifier, and the memory address of the data object instance representing the smart contract state data.
[0028] According to another aspect of this application, an electronic device is provided, comprising:
[0029] A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the method described in any one of the above methods.
[0030] According to another aspect of this application, a computer program product is provided, comprising a computer program or instructions that, when executed by a processor, implement the method described in any one of the above methods.
[0031] According to the example embodiment of this application, the serialization and deserialization of smart contract state data are implemented directly in the contract execution engine, encapsulated in the methods for reading and writing smart contract state data, and introduced into the smart contract as external methods. This eliminates the need for smart contract developers to implement the corresponding methods themselves, reducing their development costs and the amount of smart contract code.
[0032] It should be understood that the above general description and the following detailed description are merely exemplary and do not limit this application. Attached Figure Description
[0033] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below.
[0034] Figure 1 A flowchart illustrating a method for implementing a blockchain smart contract according to an example embodiment of this application is shown.
[0035] Figure 2 A flowchart illustrating a smart contract writing method according to an example embodiment of this application is shown.
[0036] Figure 3 A flowchart of a smart contract execution engine method according to an example embodiment of this application is shown.
[0037] Figure 4 A flowchart illustrating a method for reading smart contract state data according to an example embodiment of this application is shown.
[0038] Figure 5 A flowchart illustrating a method for writing smart contract state data according to an example embodiment of this application is shown.
[0039] Figure 6A schematic diagram illustrating the interaction of a blockchain smart contract according to an example embodiment of this application is shown.
[0040] Figure 7 A block diagram of an electronic device according to an exemplary embodiment is shown. Detailed Implementation
[0041] Exemplary embodiments will now be described more fully with reference to the accompanying drawings. However, these exemplary embodiments can be implemented in many forms and should not be construed as limited to the embodiments set forth herein; rather, they are provided so that this application will be thorough and complete, and will fully convey the concept of the exemplary embodiments to those skilled in the art. The same reference numerals in the drawings denote the same or similar parts, and therefore repeated descriptions of them will be omitted.
[0042] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a thorough understanding of embodiments of this application. However, those skilled in the art will recognize that the technical solutions of this application can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of this application.
[0043] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0044] The flowcharts shown in the accompanying drawings are merely illustrative and do not necessarily include all content and operations / steps, nor do they necessarily have to be performed in the described order. For example, some operations / steps can be broken down, while others can be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.
[0045] It should be understood that although the terms first, second, third, etc., may be used herein to describe various components, these components should not be limited by these terms. These terms are used to distinguish one component from another. Therefore, the first component discussed below may be referred to as the second component without departing from the teachings of this application. As used herein, the term "and / or" includes all combinations of any one and more of the associated listed items.
[0046] Those skilled in the art will understand that the accompanying drawings are merely schematic diagrams of exemplary embodiments, and the modules or processes in the drawings are not necessarily essential for implementing this application, and therefore cannot be used to limit the scope of protection of this application.
[0047] Smart contracts themselves need to be written and implemented using programming languages, and use data structure instance objects supported by the corresponding programming language to represent business state data, i.e. contract state data. Further, the data structure instance objects need to be serialized and handed over to blockchain nodes for persistent storage, or the serialized contract state data can be obtained from the blockchain nodes and deserialized to obtain the data structure instance objects supported by the corresponding programming language.
[0048] Some programming languages support directly mapping binary data in memory pointed to by pointers to instances of data structures supported by the language, without requiring additional code to implement the conversion logic. In addition, they also support allowing developers to specify the data alignment byte size of basic data type attribute variables within the data structure.
[0049] Smart contracts need to run in a smart contract execution engine (hereinafter referred to as the contract execution engine). The contract execution engine is responsible for coordinating the interaction between the smart contract program and the blockchain nodes. Some contract execution engines can directly read and write access to the memory space of the smart contract program.
[0050] The performance of the serialization and deserialization methods for smart contract state data directly affects the running performance of the smart contract. The memory consumption required by the method also affects the memory consumption during the operation of the smart contract program. In addition, the development cost required to implement the method is also related to the overall development cost required to implement the smart contract, and affects the size of the deployed smart contract code, which may in turn affect the data storage and access efficiency of blockchain nodes.
[0051] Existing technical solutions require smart contract developers to add logic for serializing and deserializing contract state data into the smart contract code. This logic either needs to be written by the smart contract developer themselves or needs to rely on an imported third-party dependency library.
[0052] The inventors discovered that existing technical solutions can lead to excessive development costs for smart contract developers beyond expressing the business logic of the contract, or result in a large amount of code for smart contracts. In addition, inappropriate implementation or third-party dependency libraries can lead to poor performance in serialization and deserialization of contract state data, as well as large memory consumption of smart contract programs.
[0053] This application enables smart contract developers to avoid implementing contract state data serialization and deserialization logic themselves or introducing third-party dependency libraries, thereby reducing the corresponding development costs and the amount of smart contract code. In addition, it takes into account the performance of contract state data serialization and deserialization and does not bring too much memory space consumption to the smart contract program.
[0054] The terms used in this application are defined as follows.
[0055] Blockchain: In a narrow sense, blockchain is a chain-like data structure that combines data blocks sequentially according to time, and is a distributed ledger that is cryptographically guaranteed to be immutable and unforgeable. In a broader sense, blockchain technology utilizes a chain-like data structure to verify and store data, uses distributed node consensus algorithms to generate and update data, employs cryptography to ensure the security of data transmission and access, and utilizes smart contracts composed of automated script code to program and manipulate data—a new distributed infrastructure and computing paradigm.
[0056] A smart contract is a computer protocol designed to disseminate, verify, or execute contracts in an informational manner. A smart contract is a set of commitments defined in digital form, including the protocols by which the contract participants can execute these commitments. In a blockchain, smart contracts require developers to complete their development and deploy them to blockchain nodes.
[0057] Smart Contract Procedure: A smart contract instance expressed in the form of a computer program, which consumes the corresponding computer's computing and storage resources during its operation.
[0058] Smart Contract Function: A computer program function that expresses a specific set of contract logic in a smart contract program. The smart contract execution engine executes the smart contract logic by calling specific smart contract functions in the smart contract program.
[0059] Smart Contract Engine: Also known as the smart contract execution engine, this is the technological environment for executing smart contract programs. It is responsible for establishing, managing, and providing the resources and data required to execute smart contract programs, and for managing and coordinating the interaction between smart contract methods and blockchain nodes.
[0060] High-level programming language: A machine-independent, procedural or object-oriented language. High-level languages are designed with reference to mathematical languages and are close to everyday conversational languages. Computer languages are divided into high-level and low-level languages. High-level languages are mainly contrasted with assembly languages; they are closer to natural language and mathematical formulas, largely independent of the machine's hardware system, and use a more human-readable way to write programs.
[0061] Data alignment refers to the relationship between data in memory and their relative positions within memory. A variable is said to be naturally aligned if its memory address is an integer multiple of its own length. Data alignment is used to improve data read efficiency.
[0062] External function: A method defined and implemented outside the scope of the current program code. Developers can directly declare the use of external functions without specifying the exact location where the external function is defined.
[0063] Endianness: The order in which data is stored in memory, which can be either big-endian or little-endian. Big-endian means the most significant byte is stored at the lowest memory address, while the least significant byte is stored at the highest memory address. Little-endian means the most significant byte is stored at the highest memory address, while the least significant byte is stored at the lowest memory address. This storage pattern effectively combines address and bit weight, with higher addresses having higher weights and lower addresses having lower weights.
[0064] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of this application.
[0065] Figure 1 A flowchart illustrating a method for implementing a blockchain smart contract according to an example embodiment of this application is shown.
[0066] See Figure 1 In S101, methods for reading and writing smart contract state data are implemented in the smart contract execution engine.
[0067] According to some embodiments, heap memory allocation and deallocation methods are used in the methods for reading and writing smart contract state data, and the methods for reading and writing smart contract state data are marked as methods that can be called externally.
[0068] According to some embodiments, smart contracts can be written using high-level languages. The high-level languages must meet the following conditions: support the direct mapping of binary data in memory pointed to by pointer addresses to data structure object instances supported by the language, without the need for additional code to implement the conversion; and support developers to specify the data alignment byte size of basic data type attribute variables within the data structure body.
[0069] Smart contracts run in a contract execution engine. The relationship between a smart contract program and a contract execution engine must meet the following condition: the contract execution engine must be able to directly read and write access to the smart contract program's memory space.
[0070] Under the above premise, the serialization and deserialization of smart contract state data are implemented directly in the contract execution engine, and the function method containing this implementation is introduced into the smart contract as an external method.
[0071] In S103, the method for reading smart contract state data is called to obtain the serialized smart contract state data and the corresponding structure information of the smart contract state data from the blockchain according to the parameter information, and the heap memory management method is called to deserialize the smart contract state data into a data object instance in the smart contract program.
[0072] According to some embodiments, smart contracts are written using the aforementioned high-level language, and methods for reading smart contract state data (denoted as RSCS) and writing smart contract state data (denoted as WSCS) are declared as external methods and introduced.
[0073] The RSCS method's parameters include the structure information identifier (SCSSI) and the smart contract state data identifier (SCSI) for the smart contract state data. The RSCS method's return value includes the memory address of the obtained binary form of the smart contract state data (BSCSA).
[0074] When reading smart contract state data, the RSCS method is called with the target data object instance's structure information identifier (denoted as OSI) and the target smart contract state data identifier (denoted as OSCSI) as parameters. The binary data pointed to by BSCSA in its return value is then converted into the target data object instance, thus realizing the deserialization of smart contract state data into a data object instance in the smart contract program.
[0075] In S105, the method for writing smart contract state data is called to obtain the structure information of the corresponding smart contract state data from the blockchain based on the parameter information, read the data object instance data from the smart contract program memory space, form serialized smart contract state data, and store it in the blockchain.
[0076] According to some embodiments, the parameters of the method for writing smart contract state data (WSCS) include SCSSI, SCSI, and the memory address (denoted as SCSOA) of the data object instance representing the smart contract state data.
[0077] When writing smart contract state data, the WSCS method is called with the target data object instance's structure information identifier (OSI), target smart contract state data identifier (OSCSI), and target data object instance's memory address (OOA) as parameters.
[0078] The contract execution engine directly reads and writes the memory data of the smart contract program. By leveraging the characteristics of the high-level language used to write the smart contract, it directly converts the data written by the contract execution engine into data object instances supported by the corresponding language, or directly reads data object instance data from the smart contract program's memory, forming serialized smart contract state data and storing it in the blockchain.
[0079] Figure 2 A flowchart illustrating a smart contract writing method according to an example embodiment of this application is shown.
[0080] See Figure 2 According to some embodiments, the method for writing smart contracts includes the following steps.
[0081] S1. Use a high-level language to define a data structure for the state data of one or more smart contracts, and specify the data alignment byte size of each attribute variable within the data structure.
[0082] S2. Generate corresponding structure information for each of the above data structures. There is a one-to-one correspondence between the data structures and the structure information. The structure information is represented in the form of a string or binary. The structure information includes the hierarchical information composed of basic data types (denoted as HS), the byte size of each basic data type in the high-level language (denoted as ES), and the data alignment byte size of the attribute variables of the basic data types in the data structure body (denoted as AS).
[0083] S3. Generate a unique identifier for each structural information as the structural information identifier (denoted as OSI). There is a one-to-one correspondence between structural information and structural information identifier. The structural information identifier is represented as a string or binary.
[0084] S4. Using the high-level language described above, write a smart contract and declare a method for reading smart contract state data (denoted as RSCS) and a method for writing smart contract state data (denoted as WSCS) as external methods. The parameters of the RSCS method include the structure information identifier (denoted as SCSSI) and the smart contract state data identifier (denoted as SCSI) of the smart contract state data. The return value of the RSCS method includes the memory address of the obtained binary form of the smart contract state data (denoted as BSCSA). The parameters of the WSCS method include SCSSI, SCSI, and the memory address of the data object instance representing the smart contract state data (denoted as SCSOA).
[0085] S5. Using the above high-level language, write the smart contract logic. When reading smart contract state data, call the RSCS method with the target data object instance's structure information identifier (denoted as OSI) and the target smart contract state data identifier (denoted as OSCSI) as parameters, and convert the binary data pointed to by BSCSA in its return value into the target data object instance. When writing smart contract state data, call the WSCS method with OSI, OSCSI, and the target data object instance's memory address (OOA) as parameters.
[0086] S6. Use the above high-level language to write a smart contract, implement the heap memory allocation method (denoted as ALLOCATE) and deallocation method (denoted as DEALLOCATE), and mark them as methods that can be called externally.
[0087] Figure 3 A flowchart of a smart contract execution engine method according to an example embodiment of this application is shown.
[0088] See Figure 3 According to some embodiments, the method of a smart contract execution engine includes the following steps.
[0089] S1. Implement the RSCS and WSCS methods in the contract execution engine. Set an address list variable (denoted as AT) in the contract execution engine. This address list variable is used to store the starting address of the memory space requested from the smart contract program memory in the RSCS implementation logic.
[0090] S2. Implement the RSCS and WSCS methods in the contract execution engine, set the endianness of the serialized binary smart contract state data to be stored in the blockchain (denoted as the first endianness), and determine the endianness of the smart contract program's runtime environment (virtual machine or real machine) (denoted as the second endianness).
[0091] S3. Implement the RSCS method in the contract execution engine.
[0092] S4. Implement the WSCS method in the contract execution engine.
[0093] S5. After the smart contract method is executed, check if the AT list variable in the contract execution engine is not empty. If it is not empty, iterate through the list variable and call the DEALLOCATE method of the smart contract program for each element to release the smart contract program's memory.
[0094] Figure 4 A flowchart illustrating a method for reading smart contract state data according to an example embodiment of this application is shown.
[0095] See Figure 4 According to some embodiments, the RSCS method for reading smart contract state data obtains the corresponding smart contract state data structure information and the serialized smart contract state data (denoted as first state data) from the blockchain based on the SCSSI and SCSI passed as parameters when the smart contract program is called.
[0096] Based on the HS, ES, and AS information in the smart contract state data structure, the amount of memory to be requested from the smart contract program is calculated in conjunction with the first state data.
[0097] Then, based on the memory size, the ALLOCATE method in the smart contract program is called to obtain the returned memory address value (denoted as ARA), and this address value is added to the AT variable in the contract execution engine.
[0098] Then, based on the smart contract state data structure information mentioned above, the first state data is converted and written into the smart contract program memory space pointed to by ARA. If the conversion process involves the conversion of integer and floating-point data types, the corresponding data in the first endianness mode is converted into the corresponding data in the second endianness mode.
[0099] Figure 5 A flowchart illustrating a method for writing smart contract state data according to an example embodiment of this application is shown.
[0100] See Figure 5 The WSCS method for writing smart contract state data first obtains the corresponding smart contract state data structure information from the blockchain based on the SCSSI, SCSI, and SCSOA passed as parameters when the smart contract program is called.
[0101] Then, based on HS, ES, and AS in the smart contract state data structure information, the data object instance data is read from the smart contract program memory space pointed to by SCSOA, and the read data is converted and concatenated to obtain the serialized smart contract state data in binary form (denoted as the second state data). The second state data is then stored in the blockchain chain.
[0102] ng
[0103] Figure 6 A schematic diagram illustrating the interaction of a blockchain smart contract according to an example embodiment of this application is shown.
[0104] See Figure 6 According to some embodiments, after a smart contract is deployed, it is executed in a smart contract execution engine. The interaction process between the smart contract and the blockchain node during and after deployment and execution is as follows.
[0105] When deploying a smart contract, the blockchain node obtains the relevant smart contract state data structure information and structure information identifier and performs associated persistent storage.
[0106] The RSCS and WSCS methods are called in the contract execution engine.
[0107] The RSCS method is called in the contract execution engine. The RSCS method obtains the corresponding smart contract state data structure information and the serialized smart contract state data (denoted as the first state data) from the blockchain based on the SCSSI and SCSI parameters passed when the smart contract program is called. Based on the HS, ES and AS information in the smart contract state data structure information, the method calculates the amount of memory to be requested from the smart contract program in combination with the first state data.
[0108] Then, the contract execution engine calls the ALLOCATE method in the smart contract program based on the memory size and obtains the returned memory address value (denoted as ARA). At the same time, it adds the address value to the AT variable in the contract execution engine. Then, based on the smart contract state data structure information mentioned above, it converts the first state data and writes it into the smart contract program memory space pointed to by ARA.
[0109] The WSCS method is called in the contract execution engine. Based on the SCSSI, SCSI, and SCSOA parameters passed when the smart contract program is called, the WSCS method first obtains the corresponding smart contract state data structure information from the blockchain according to the SCSSI and SCSI. Then, based on the HS, ES, and AS in the smart contract state data structure information, it reads the data object instance data from the smart contract program memory space pointed to by SCSOA, and converts and concatenates the read data to obtain the serialized smart contract state data in binary form (denoted as the second state data). Finally, the second state data is stored in the blockchain chain.
[0110] After the smart contract method is executed, check if the AT list variable in the contract execution engine is not empty. If it is not empty, iterate through the list variable and call the DEALLOCATE method of the smart contract program for each element to release the smart contract program's memory.
[0111] It should be clearly understood that this application describes how specific examples are formed and used, but this application is not limited to any details of these examples. Rather, based on the teachings of the disclosure of this application, these principles can be applied to many other embodiments.
[0112] Those skilled in the art will understand that all or part of the steps of the above embodiments are implemented as a computer program executed by a CPU. When the computer program is executed by the CPU, the program that performs the functions defined by the methods provided in this application can be stored in a computer-readable storage medium, such as a read-only memory, a magnetic disk, or an optical disk.
[0113] Furthermore, it should be noted that the above figures are merely illustrative representations of the processes included in the method according to exemplary embodiments of this application, and are not intended to be limiting. It is readily understood that the processes shown in the above figures do not indicate or limit the temporal order of these processes. Additionally, it is readily understood that these processes may be executed synchronously or asynchronously, for example, in multiple modules.
[0114] Through the description of the example embodiments, those skilled in the art will readily understand that the implementation method of the blockchain smart contract according to the embodiments of this application has at least one or more of the following advantages.
[0115] According to the example implementation, the serialization and deserialization of smart contract state data are implemented directly in the contract execution engine, encapsulated in the methods for reading and writing smart contract state data, and introduced into the smart contract as external methods. This eliminates the need for smart contract developers to implement the corresponding methods themselves, reducing their development costs and the amount of smart contract code.
[0116] According to the example embodiment, the smart contract program memory data is directly read and written in the contract execution engine. By leveraging the characteristics of the high-level language used to write the smart contract, the data written by the contract execution engine into the smart contract program memory is directly converted into data object instances supported by the corresponding language, or the data object instance data is directly read from the smart contract program memory. This eliminates the need to run additional conversion logic in the smart contract program and eliminates the need for additional memory space for storing copied data, thereby ensuring the performance of the method and reducing the memory space consumption required by the smart contract program itself.
[0117] Figure 7 A block diagram of an electronic device according to an exemplary embodiment is shown.
[0118] The following reference Figure 7 To describe an electronic device 200 according to this embodiment of the present application. Figure 7 The electronic device 200 shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.
[0119] like Figure 7As shown, the electronic device 200 is presented in the form of a general-purpose computing device. The components of the electronic device 200 may include, but are not limited to: at least one processing unit 210, at least one storage unit 220, a bus 230 connecting different system components (including storage unit 220 and processing unit 210), a display unit 240, etc.
[0120] The storage unit stores program code, which can be executed by the processing unit 210, causing the processing unit 210 to perform the methods described in this specification according to various exemplary embodiments of this application.
[0121] Storage unit 220 may include readable media in the form of volatile storage units, such as random access memory (RAM) 2201 and / or cache memory 2202, and may further include read-only memory (ROM) 2203.
[0122] Storage unit 220 may also include a program / utility 2204 having a set (at least one) program module 2205, such program module 2205 including but not limited to: operating system, one or more application programs, other program modules and program data, each or some combination of these examples may include an implementation of a network environment.
[0123] Bus 230 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.
[0124] Electronic device 200 can also communicate with one or more external devices 300 (e.g., keyboard, pointing device, Bluetooth device, etc.), and with one or more devices that enable a user to interact with electronic device 200, and / or with any device that enables electronic device 200 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 250. Furthermore, electronic device 200 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 260. Network adapter 260 can communicate with other modules of electronic device 200 via bus 230. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 200, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0125] Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. The technical solutions according to the embodiments of this application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, or network device, etc.) to execute the methods described above according to the embodiments of this application.
[0126] Software products may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example,, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0127] Computer-readable storage media may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable storage medium may also be any readable medium other than a readable storage medium that can transmit, propagate, or transfer a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.
[0128] Program code for performing the operations of this application can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java and C++, and conventional procedural programming languages such as C or similar languages. The program code can execute entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0129] Those skilled in the art will understand that the above modules can be distributed in the device as described in the embodiments, or they can be modified accordingly and placed in one or more devices that are unique to this embodiment. The modules in the above embodiments can be combined into one module, or they can be further divided into multiple sub-modules.
[0130] Exemplary embodiments of this application have been specifically shown and described above. It should be understood that this application is not limited to the detailed structures, arrangements, or implementation methods described herein; rather, this application is intended to cover various modifications and equivalent arrangements contained within the spirit and scope of the appended claims.
Claims
1. A method for implementing blockchain smart contracts, characterized in that, include: Implement methods for reading and writing smart contract state data in the smart contract execution engine; The method for reading smart contract state data and the method for writing smart contract state data use heap memory allocation and release methods, and the method for reading smart contract state data and the method for writing smart contract state data are introduced into the smart contract program as external methods and called by the smart contract program during execution. The method for reading smart contract state data is invoked to obtain serialized smart contract state data and corresponding smart contract state data structure information from the blockchain according to parameter information, and the smart contract state data is deserialized into data object instances in the smart contract program through heap memory management methods. The method for writing smart contract state data is invoked to obtain the structure information of the corresponding smart contract state data from the blockchain according to the parameter information, read the data object instance data from the smart contract program memory space, form serialized smart contract state data, and store it in the blockchain; The structural information includes hierarchical information composed of basic data types, the byte size corresponding to each basic data type, and the data alignment byte size of the attribute variables of the basic data types within the data structure. The smart contract program implements the heap memory allocation method and the release method, and marks the heap memory allocation method and the release method as methods that can be called externally; An address list variable is set in the smart contract execution engine. The address list variable is used to store the starting address of the memory space requested from the smart contract program in the implementation logic of the method for reading smart contract state data. When reading smart contract state data, the system calculates the memory size to be requested from the smart contract program based on the hierarchical information, byte size, and data alignment byte size in the structure information, combined with the serialized smart contract state data. It then calls the heap memory allocation method in the smart contract program based on the memory size, obtains the returned memory address value, and adds the memory address value to the address list variable. Finally, it converts the serialized smart contract state data according to the structure information and writes it into the smart contract program memory space pointed to by the memory address value. After the smart contract method is executed, if the address list variable is not empty, the address list variable is traversed, and the release method of the smart contract program is called according to each element to release the smart contract program memory.
2. The method according to claim 1, characterized in that, Also includes: Define a data structure for the state data of one or more smart contracts, and specify the data alignment byte size of each attribute variable within the data structure.
3. The method according to claim 2, characterized in that, Also includes: For each data structure, generate corresponding structure information and structure information identifier. The structure information includes hierarchical information composed of basic data types, the byte size of each basic data type, and the data alignment byte size of the attribute variables of the basic data types within the data structure.
4. The method according to claim 3, characterized in that, Also includes: When deploying a smart contract, the structure information and the identifier of the smart contract state data are obtained and persistently stored together.
5. The method according to claim 4, characterized in that, Also includes: The method for reading smart contract state data and the method for writing smart contract state data are declared and defined; When reading smart contract state data, the method for reading smart contract state data is called to obtain a target data object instance; When writing smart contract state data, the method for writing smart contract state data is called to write smart contract state data according to the target data object instance.
6. The method according to claim 5, characterized in that, The steps of calling the method for reading smart contract state data when reading smart contract state data include: The calling parameters of the method for reading smart contract state data include the structure information identifier and the smart contract state data identifier of the smart contract state data; The return value of the method for reading smart contract state data includes the memory address of the obtained binary form of the smart contract state data; Then, the binary data pointed to by the memory address is converted into the data object instance.
7. The method according to claim 5, characterized in that, The steps of calling the method for writing smart contract state data when writing smart contract state data include: The call parameters of the method for writing smart contract state data include the structure information identifier of the smart contract state data, the smart contract state data identifier, and the memory address of the data object instance representing the smart contract state data.
8. An electronic device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the method of any one of claims 1-7.
9. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by the processor, they implement the method as described in any one of claims 1-7.
Citation Information
Patent Citations
A secure Java smart contract interpretation execution engine and method
CN109710384A
Memory management method and device, electronic equipment and automatic driving vehicle
CN114153614A