Wasm-based multi-smart contract instance management method
By setting up a smart contract engine in the blockchain to generate multiple contract instances and manage memory, the resource consumption and efficiency issues of WASM contract instances under high concurrency are solved, achieving efficient and secure contract calls.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- UNIV OF ELECTRONICS SCI & TECH OF CHINA
- Filing Date
- 2023-06-19
- Publication Date
- 2026-05-01
AI Technical Summary
Existing blockchain smart contracts suffer from high resource consumption, low execution efficiency, and performance impact under high concurrency, especially since WASM-based contract instance management fails to meet resource allocation and security requirements.
A smart contract engine is set up in the blockchain to generate multiple contract instances and perform memory allocation and management. Data interaction is carried out through the contract instance buffer pool and external functions, supporting concurrent calls and memory reclamation, thereby improving the efficiency of contract calls.
By using a contract instance buffer pool and memory management, high-concurrency calls are supported, memory resource waste is reduced, and contract call performance and security are improved.
Smart Images

Figure CN116680209B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of blockchain technology, and more specifically, relates to a method for managing multiple smart contract instances based on WASM. Background Technology
[0002] Blockchain technology is a decentralized distributed database technology that maintains data consistency and security through collaboration and verification among multiple nodes. Blockchain technology and smart contracts are two inseparable concepts. Blockchain technology achieves decentralized, immutable, secure, and reliable distributed database functionality, while smart contracts are programmable automated contracts that execute on the blockchain, enabling automated transactions and protocol execution. A smart contract is a computer program that runs on the blockchain, automatically executing a series of operations and judgments, and triggering corresponding behaviors when certain conditions are met. Smart contracts enable decentralized transactions, protocol execution, and information sharing. Their execution results are public and transparent, and any participant can view and verify them. Smart contracts can execute automatically on the blockchain without third-party intervention or supervision, thus achieving more efficient, transparent, and fair transactions and protocol execution.
[0003] Most existing blockchain smart contracts employ a Docker container-based mechanism, which enables dynamic deployment and management of a relatively isolated smart contract runtime environment. Specifically, each smart contract is packaged into a Docker image, containing all the code and dependencies required for the smart contract to run, and is loaded and run by the Docker engine during deployment. However, the Docker container mechanism is relatively resource-intensive and involves inter-process communication between nodes and containers, resulting in relatively low execution efficiency. Smart contracts driven by the WASM (WebAssembly) engine offer advantages over traditional smart contract engines, including lower resource consumption, higher isolation, and higher execution efficiency. To improve the performance of WASM-based smart contract execution, dynamic caching of WASM contract instances is necessary. Furthermore, considering resource consumption and security, memory allocation and management of the WASM contract virtual machine are required. However, current technologies do not fully meet these requirements and further improvements are needed.
[0004] Smart contracts can be implemented using different technical architectures. There are two common methods: virtual machine-based smart contracts and container-based smart contracts. Virtual machine-based smart contracts use virtual machine technology, where the bytecode of the smart contract is run by the virtual machine during execution. When the contract needs to be executed, the virtual machine loads the bytecode into memory and executes it. Container-based smart contracts use container technology, running the smart contract within a container. During contract execution, the container starts an independent runtime environment and runs the smart contract within that environment. A smart contract instance refers to the executable contract code deployed on the blockchain. When the smart contract is invoked, a contract instance needs to be created on the blockchain, containing the contract code, state data, and execution environment. However, existing smart contract execution methods all use a single contract instance model for contract invocation, meaning that a contract instance is generated for data interaction when processing a contract invocation request. This model negatively impacts contract call performance under high concurrency due to the need to generate a large number of contract instances. Summary of the Invention
[0005] The purpose of this invention is to overcome the shortcomings of the prior art and provide a multi-smart contract instance management method based on WASM. By setting up a smart contract engine, multiple contract instances are generated when deploying contracts and memory is allocated and managed for the instances for subsequent contract calls, thereby improving the efficiency of transaction execution.
[0006] To achieve the above-mentioned objectives, the WASM-based multi-smart contract instance management method of the present invention includes the following steps:
[0007] S1: Set up a smart contract engine in the blockchain as an intermediary between WASM smart contracts and blockchain nodes. The smart contract engine includes a smart contract management module, a contract instance module, and a smart contract driver, among which:
[0008] The smart contract management module is used to perform security verification on deployment and invocation requests of WASM smart contracts, and to obtain the contract source code and initialize the tools needed for contract instantiation during contract deployment.
[0009] Smart contract drivers are used to execute contract deployments and handle contract calls;
[0010] The contract instance module has several contract instance buffer pools for storing contract instances corresponding to WASM smart contracts;
[0011] S2: Deploy the contract, the specific method is as follows:
[0012] S2.1: The user writes a WASM smart contract according to the actual situation, including the entry function Invoke for receiving contract call requests. After writing, the contract is saved as a contract package. Then, a WASM smart contract deployment request is generated and sent to the smart contract engine. The request parameters include the contract package path and the contract name.
[0013] S2.2: The smart contract management module in the smart contract engine receives the contract deployment request, performs security verification on the contract deployment request, and after the security verification is passed, it reads the contract source code according to the contract package path, and then initializes the tools required for contract instantiation, that is, generates the wasm storage component, wasm module component and Wasm environment component, and sends the contract source code and instantiation tools to the smart contract driver.
[0014] S2.3: After receiving the contract source code, the smart contract driver puts the contract source code on the blockchain to complete the deployment of the WASM smart contract;
[0015] S2.4: After receiving the contract source code and instantiation tools, the smart contract driver generates N contract instances, creates a new contract instance buffer pool in the contract instance buffer module, and adds the N contract instances to this buffer pool; the method for generating contract instances is as follows:
[0016] First, external functions are created for the WASM smart contract to interact with the contract instance. These include the GetState function for querying data from the contract instance and the PutState function for inserting data into the contract instance. Within these external functions, the contract instance retrieves data from the WASM smart contract by reading a fixed address in the instance's memory. When passing data to the WASM smart contract, the external functions obtain the memory address of the data and pass it to the WASM smart contract. Then, the external functions are registered within the WASM module component. Next, the instantiation method is called to generate N contract instances. The value of N is determined based on actual needs, representing the maximum number of concurrent calls to the WASM smart contract. When generating contract instances, the memory of each instance is configured, specifying the address and size of the instance's memory.
[0017] S3: Contract calls are performed using the following method:
[0018] S3.1: When a blockchain node needs to call a WASM smart contract, it generates a contract call request and sends it to the smart contract engine. The contract call request includes the contract name, contract method, and parameters.
[0019] S3.2: The smart contract management module in the smart contract engine receives the contract call request and performs authentication. After successful authentication, it parses the contract name, contract method, and parameters from the contract call request and sends them to the smart contract driver. The smart contract driver randomly selects an idle contract instance from the corresponding contract instance buffer pool based on the contract name.
[0020] S3.3: The smart contract driver allocates a blank space in the memory space according to the memory size of the contract instance as the memory space of the contract instance. The memory.Data() method is used to convert the obtained contract instance memory space into a character slice format and write the contract call request into it.
[0021] S3.4: The smart contract driver exports the Invoke function of the contract instance, taking the starting address of the contract call request in the memory space of the contract instance as the input parameter of the Invoke function; the WASM smart contract directly reads the memory space of the contract instance through the starting address passed to the Invoke function to obtain the contract call request, parses the contract method and corresponding parameters in the request, and jumps to the corresponding contract method to process the contract request;
[0022] During the WASM smart contract's processing of contract requests, the contract instance and the WASM smart contract interact with data through external functions. The specific method is as follows:
[0023] WASM smart contracts obtain business operations provided by the contract instance through external functions registered during contract deployment. When a WASM smart contract needs to call the external function PutState to pass data to the contract instance, it obtains the key-value pair of the data and then passes the addresses of the key and value to the contract instance. The contract instance reads the actual key and value data from the current memory space through the external function PutState and stores it in the state database as key-value pairs. If this interaction process is successful, the node returns a data transfer success message to the WASM smart contract; otherwise, it returns a data transfer failure message. When the WASM smart contract calls the external function GetState, the contract instance reads the key value from the current memory space and then queries the node's state database using the key value as the search term. If the query is successful, it returns the obtained value to the WASM smart contract; otherwise, it returns a data query failure message.
[0024] S3.5: After the WASM smart contract completes the contract request, it passes the instance memory address of the contract processing result as the return value of the Invoke function to the smart contract driver. The node reads the instance memory according to the address to obtain the contract processing result of this call, and then feeds it back to the blockchain node that initiated this call through the smart contract management module.
[0025] S3.6: The smart contract driver will reclaim the memory space allocated to the contract instance by calling `instance.Close()` to release the resources occupied by the contract instance, including memory space and handle. Then, the used contract instance will be returned to the corresponding contract instance buffer pool, waiting for the next call.
[0026] This invention relates to a multi-smart contract instance management method based on WASM. A smart contract engine is set up in the blockchain as an intermediary node between the WASM smart contract and the blockchain nodes. The smart contract engine includes a smart contract management module, a contract instance module, and a smart contract driver. During contract deployment, the smart contract management module initializes the instantiation tool and sends it along with the contract source code to the smart contract driver. The smart contract driver generates N contract instances and creates a new contract instance buffer pool in the contract instance buffer module, adding the N contract instances to this buffer pool. During contract invocation, the smart contract driver randomly selects an idle contract instance from the contract instance buffer pool of the invoked contract, allocates memory space, interacts with the WASM smart contract through external functions to obtain the contract processing result, and then reclaims the memory space, waiting for the next invocation.
[0027] The present invention has the following beneficial effects:
[0028] 1) This invention sets up a contract instance buffer pool. The contract instance buffer pool and the smart contract have a one-to-one relationship. Each contract instance buffer pool has multiple available smart contract instances. When processing a contract call request, a smart contract instance is randomly selected from the corresponding instance buffer pool for call, thereby better supporting call concurrency.
[0029] 2) This invention supports memory allocation and reclamation for contract instances: when calling a contract instance, allocate an appropriate amount of memory space, reclaim the memory after the call ends, clear the data written in the instance memory during the call to prevent data overwriting during subsequent calls, reduce memory leaks, and put the instance back into the buffer pool after clearing to wait for the next call, thereby reducing the waste of memory resources;
[0030] 3) This invention supports data transfer from contract instances by writing to memory. During contract instantiation, an external function is defined for the WASM smart contract to transfer data to the contract instance. During contract calls, the WASM contract transmits the memory address of the data to the contract instance by calling the external function. The contract instance then retrieves and manipulates the data at the corresponding address by reading from the contract instance's memory. Attached Figure Description
[0031] Figure 1 This is a flowchart illustrating a specific implementation of the WASM-based multi-smart contract instance management method of the present invention.
[0032] Figure 2 This is a structural diagram of the smart contract engine in this invention;
[0033] Figure 3 This is a flowchart of the WASM smart contract deployment in this invention;
[0034] Figure 4 This is a flowchart of the contract instance generation process in this invention;
[0035] Figure 5 This is a flowchart of the smart contract invocation process in this invention;
[0036] Figure 6 This is a schematic diagram illustrating the data interaction between the contract instance and the WASM smart contract in this invention. Detailed Implementation
[0037] The specific embodiments of the present invention will now be described with reference to the accompanying drawings to enable those skilled in the art to better understand the invention. It should be particularly noted that in the following description, detailed descriptions of known functions and designs that might obscure the main content of the invention will be omitted here.
[0038] Example
[0039] Figure 1 This is a flowchart illustrating a specific implementation of the multi-smart contract instance management method based on WASM according to the present invention. Figure 1 As shown, the specific steps of the multi-smart contract instance management method based on WASM of the present invention include:
[0040] S101: Configure the smart contract engine:
[0041] Set up a smart contract engine in the blockchain as an intermediary node between WASM smart contracts and other blockchain nodes. Figure 2 This is a structural diagram of the smart contract engine in this invention. (See diagram below.) Figure 2 As shown, the smart contract engine in this invention includes a smart contract management module, a contract instance buffer module, and a smart contract driver, wherein:
[0042] The smart contract management module is used to perform security verification on deployment and invocation requests for WASM smart contracts, and to obtain the contract source code and initialize the tools needed for contract instantiation during contract deployment.
[0043] Smart contract drivers are used to execute contract deployments and handle contract calls.
[0044] The contract instance module has several contract instance buffer pools to store the contract instances corresponding to WASM smart contracts.
[0045] S102: WASM Smart Contract Deployment:
[0046] Unlike conventional contract deployment, in order to provide parallel contract invocation, this invention also requires the generation of contract instances during contract deployment. Figure 3 This is a flowchart of the WASM smart contract deployment in this invention. For example... Figure 3 As shown, the specific steps for contract deployment in this invention include:
[0047] S201: Send contract deployment request:
[0048] Users write WASM smart contracts according to their actual situation, including the entry function Invoke for receiving contract call requests. After writing, the contracts are saved as a contract package. Then, a WASM smart contract deployment request is generated and sent to the smart contract engine. The request parameters include the contract package path and the contract name.
[0049] S302: Import contract source code and initialize instantiation tools:
[0050] The smart contract management module in the smart contract engine receives contract deployment requests, performs security verification on the requests, and if the verification passes, reads the contract source code according to the contract package path. Then, it initializes the tools needed for contract instantiation, namely, generating the Wasm storage component, Wasm module component, and Wasi environment component, and sends the contract source code and instantiation tools to the smart contract driver. The Wasm storage component, Wasm module component, and Wasi environment component are key components for subsequent contract instance generation and are reused multiple times during code execution.
[0051] S303: On-chain WASM smart contract:
[0052] After receiving the contract source code, the smart contract driver places the contract source code on the blockchain, completing the deployment of the WASM smart contract.
[0053] S304: Generate and save the contract instance:
[0054] After receiving the contract source code and instantiation tools, the smart contract driver generates N contract instances, creates a new contract instance buffer pool in the contract instance buffer module, and puts the N contract instances into the contract instance buffer pool. Figure 4 This is a flowchart illustrating the generation of contract instances in this invention. For example... Figure 4 As shown, the method for generating contract instances is as follows:
[0055] First, external functions used by the WASM smart contract for data interaction with the contract instance are created. These include the `GetState` function for querying data from the contract instance and the `PutState` function for inserting data into the contract instance. Within these external functions, the contract instance retrieves data from the WASM smart contract by reading a fixed address in the instance's memory. When passing data to the WASM smart contract, the external functions obtain the memory address of the data and pass it to the WASM smart contract. Then, the external functions are registered within the WASM module component. Next, the instantiation method is called to generate N contract instances. The value of N is determined based on actual needs, representing the maximum number of concurrent calls to the WASM smart contract. When generating contract instances, the memory of each instance is configured, specifying the address and size of the instance memory. In this embodiment, the instance memory is exported using the `instance.Exports.GetMemory("memory")` method. The minimum instance memory size is one page (64KB), and the memory size is specified as n pages using the `memory.Grow(n)` method, thus setting the memory size of the contract instance.
[0056] S103: WASM smart contract call:
[0057] When processing contract call requests, this invention employs a concurrency management method to call contract instances in the contract instance buffer pool, thereby improving the performance of concurrent calls. Figure 5 This is a flowchart of the smart contract invocation process in this invention. For example... Figure 5 As shown, the specific method for invoking smart contracts in this invention is as follows:
[0058] S501: Send contract call request:
[0059] When a blockchain node needs to invoke a WASM smart contract, it generates a contract invocation request and sends it to the smart contract engine. The contract invocation request includes the contract name, contract method, and parameters.
[0060] S502: Example of an allocation contract:
[0061] The smart contract management module in the smart contract engine receives contract call requests and performs authentication. Upon successful authentication, it parses the contract name, contract method, and parameters from the request and sends them to the smart contract driver. The smart contract driver randomly selects an idle contract instance from the corresponding contract instance buffer pool based on the contract name. Because multiple contract instances in this invention do not interfere with each other, call requests can be processed concurrently under this mechanism.
[0062] S503: Memory allocation:
[0063] The smart contract driver allocates a blank space in the memory space according to the memory size of the contract instance. It then uses the memory.Data() method to convert the obtained contract instance memory space into a character slice format and writes the contract call request into it.
[0064] S504: Passing contract call requests based on the Invoke function:
[0065] The smart contract driver exports the Invoke function of the contract instance, using the starting address of the contract call request in the contract instance's memory space as the input parameter. The WASM smart contract directly reads the contract instance's memory space using the starting address passed to the Invoke function to obtain the contract call request, parses the contract methods and corresponding parameters in the request, and jumps to the corresponding contract method to process the contract request. Based on the above method, it can be seen that this invention can send contract call requests to the corresponding WASM smart contract using the Invoke function.
[0066] During the process of WASM smart contract processing contract requests, the contract instance and WASM smart contract interact with each other through external functions. Figure 6 This is a schematic diagram illustrating the data interaction between the contract instance and the WASM smart contract in this invention. For example... Figure 6 As shown, the specific method for data interaction between the contract instance and the WASM smart contract in this invention is as follows:
[0067] WASM smart contracts obtain business operations provided by the contract instance through external functions registered during contract deployment. When a WASM smart contract needs to call the external function PutState to pass data to the contract instance, it obtains the key-value pair of the data and then passes the addresses of the key and value to the contract instance. The contract instance reads the actual key and value data from the current memory space using the external function PutState and stores it in the state database as key-value pairs. If this interaction process is successful, the node returns a data transfer success message to the WASM smart contract; otherwise, it returns a data transfer failure message. When the WASM smart contract calls the external function GetState, the contract instance reads the key value from the current memory space and then queries the node's state database using the key value as the retrieval item. If the query is successful, it returns the obtained value to the WASM smart contract; otherwise, it returns a data query failure message.
[0068] As can be seen, in this invention, the return information of the contract instance is still transmitted to the WASM smart contract by writing it into the instance memory. The return value obtained by the WASM smart contract is actually the starting address of the return information in the instance memory, and the real data is read from the memory through this address.
[0069] S505: Feedback on contract processing results:
[0070] After processing the contract request, the WASM smart contract passes the instance memory address of the contract processing result as the return value of the Invoke function to the smart contract driver. The node reads the instance memory based on the address to obtain the contract processing result of this call, and then feeds it back to the blockchain node that initiated this call through the smart contract management module.
[0071] S506: Release instance memory:
[0072] The smart contract driver will reclaim the memory space allocated to the contract instance by calling `instance.Close()` to release the resources occupied by the contract instance, including memory space and handle. Then, the used contract instance will be returned to the corresponding contract instance buffer pool, awaiting the next call.
[0073] Although the illustrative specific embodiments of the present invention have been described above to enable those skilled in the art to understand the invention, it should be understood that the invention is not limited to the scope of the specific embodiments. For those skilled in the art, various changes are obvious as long as they are within the spirit and scope of the invention as defined and determined by the appended claims, and all inventions utilizing the concept of the present invention are protected.
Claims
1. A method for managing multiple smart contract instances based on WASM, characterized in that, Includes the following steps: S1: Set up a smart contract engine in the blockchain as an intermediary between WASM smart contracts and blockchain nodes. The smart contract engine includes a smart contract management module, a contract instance module, and a smart contract driver, among which: The smart contract management module is used to perform security verification on deployment and invocation requests of WASM smart contracts, and to obtain the contract source code and initialize the tools needed for contract instantiation during contract deployment. Smart contract drivers are used to execute contract deployments and handle contract calls; The contract instance module has several contract instance buffer pools for storing contract instances corresponding to WASM smart contracts; S2: Deploy the contract, the specific method is as follows: S2.1: The user writes a WASM smart contract according to the actual situation, including the entry function Invoke for receiving contract call requests. After writing, the contract is saved as a contract package. Then, a WASM smart contract deployment request is generated and sent to the smart contract engine. The request parameters include the contract package path and the contract name. S2.2: The smart contract management module in the smart contract engine receives the contract deployment request, performs security verification on the contract deployment request, and after the security verification is passed, it reads the contract source code according to the contract package path, and then initializes the tools required for contract instantiation, that is, generates the wasm storage component, wasm module component and Wasm environment component, and sends the contract source code and instantiation tools to the smart contract driver. S2.3: After receiving the contract source code, the smart contract driver puts the contract source code on the blockchain to complete the deployment of the WASM smart contract; S2.4: After receiving the contract source code and instantiation tool, the smart contract driver generates N contract instances, creates a new contract instance buffer pool in the contract instance buffer module, and puts the N contract instances into the contract instance buffer pool; S3: Contract calls are performed using the following method: S3.1: When a blockchain node needs to call a WASM smart contract, it generates a contract call request and sends it to the smart contract engine. The contract call request includes the contract name, contract method, and parameters. S3.2: The smart contract management module in the smart contract engine receives the contract call request and performs authentication. After successful authentication, it parses the contract name, contract method, and parameters from the contract call request and sends them to the smart contract driver. The smart contract driver randomly selects an idle contract instance from the corresponding contract instance buffer pool based on the contract name. S3.3: The smart contract driver allocates a blank space in the memory space according to the memory size of the contract instance as the memory space of the contract instance. The memory.Data() method is used to convert the obtained contract instance memory space into a character slice format and write the contract call request into it. S3.4: The smart contract driver exports the Invoke function of the contract instance, taking the starting address of the contract call request in the memory space of the contract instance as the input parameter of the Invoke function; the WASM smart contract directly reads the memory space of the contract instance through the starting address passed to the Invoke function to obtain the contract call request, parses the contract method and corresponding parameters in the request, and jumps to the corresponding contract method to process the contract request; S3.5: After the WASM smart contract completes the contract request, it passes the instance memory address of the contract processing result as the return value of the Invoke function to the smart contract driver. The node reads the instance memory according to the address to obtain the contract processing result of this call, and then feeds it back to the blockchain node that initiated this call through the smart contract management module. S3.6: The smart contract driver will reclaim the memory space allocated to the contract instance by calling instance.Close() to release the resources occupied by the contract instance, including memory space and handle; then, the used contract instance will be put back into the corresponding contract instance buffer pool, waiting for the next call.
2. The method for managing multiple smart contract instances based on WASM according to claim 1, characterized in that, The method for generating the contract instance in step S2.4 is as follows: First, external functions are created for the WASM smart contract to interact with the contract instance. These include the GetState function for querying data from the contract instance and the PutState function for inserting data into the contract instance. Within these external functions, the contract instance retrieves data from the WASM smart contract by reading a fixed address in the instance's memory. When passing data to the WASM smart contract, the external functions obtain the memory address of the data and pass it to the WASM smart contract. Then, the external functions are registered within the WASM module component. Next, the instantiation method is called to generate N contract instances. The value of N is determined based on actual needs, representing the maximum number of concurrent calls to the WASM smart contract. When generating contract instances, the memory of each instance is configured, specifying the address and size of the instance's memory.
3. The method for managing multiple smart contract instances based on WASM according to claim 1, characterized in that, In step S3.4, during the WASM smart contract's processing of contract requests, the contract instance and the WASM smart contract interact with each other through external functions. The specific method is as follows: WASM smart contracts obtain business operations provided by the contract instance through external functions registered during contract deployment; when a WASM smart contract needs to call the external function PutState to pass data to the contract instance, it obtains the key-value pair of the data and then passes the address of the key and value to the contract instance. The contract instance reads the actual key and value data from the current memory space through the external function PutState and stores them in the state database in the form of key-value pairs; If this interaction process is successful, the node returns a data transmission success message to the WASM smart contract; otherwise, it returns a data transmission failure message. When the WASM smart contract calls the external function GetState, the contract instance reads the key value from the current memory space, and then uses the key value as the retrieval item to query the node state database. If the query is correct, the obtained value is returned to the WASM smart contract; otherwise, a data query failure message is returned.
Citation Information
Patent Citations
Smart execution method, engine and block chain node
CN113688186A
Edge cloud function computing system and method
CN115599529A