A method and device for writing data
By establishing a cache layer in the virtual machine and using the transaction control interface standard, the coupling problem between the virtual machine and the storage layer is solved, ensuring the overall transactionality of data writing and database security, and achieving the flexibility and versatility of the storage layer. , suitable for smart contract business scenarios.
Patent Information
- Application Number
- CN202111546691.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-16
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2041-12-16
AI Technical Summary
Existing technology cannot ensure the overall transactionality of back-end services and virtual machines writing operations to the storage layer, resulting in database security that cannot be guaranteed, and the virtual machine and storage layer are highly coupled, making it impossible to achieve flexible replacement of the storage layer.
A data writing method and device are designed. By establishing a cache layer in a virtual machine instance and using the transaction control interface standard, the data for the smart contract executed by the virtual machine is stored in the cache layer, and when a transaction commit or rollback request is received, the data will be stored in the cache layer. Perform local transaction commit or rollback to ensure the overall transactionality of data and the security of the database, while decoupling the virtual machine and storage layer.
It realizes the overall transactional control of the virtual machine and back-end service logic, ensures the security of the database, and provides the flexibility and versatility of the storage layer, which is suitable for various smart contract business scenarios.
Smart Images

Figure CN116266127B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a data writing method and device. Background Art
[0002] The execution of smart contracts by virtual machines is part of a transaction processing flow. Existing solutions cannot ensure the overall transactionality of backend services and virtual machines writing to the storage layer. For example, the existing solution uses TCC (a distributed transaction solution, which is the abbreviation of try, confirm, and cancel), which writes data to the database (the try process) and copies the database image. When submitting (confirm), the data has been written to the database. If dirty data is written, the database is restored using the image when rolling back (cancel). In addition, during the execution of smart contracts by virtual machines, the storage of smart contracts will be modified, and transactions will be triggered. These transactions need to be cached first. Existing solutions, such as Ethereum 1.0, have highly coupled virtual machines and storage layers, and cannot achieve flexible replacement of the storage layer.
[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0004] The overall transactionality of write operations of the backend service and virtual machine to the storage layer cannot be ensured. Direct operation of the database makes it impossible to guarantee the security of the database. In addition, the virtual machine and the storage layer are highly coupled, and the storage layer cannot be flexibly replaced. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a data writing method and device, which can ensure the overall transactionality of all logics of the back-end service and the virtual machine, design a set of transaction control interface standards for the upper-level users of the virtual machine, and realize the overall control of the virtual machine's execution of smart contract transactions by the external system. It also ensures the security of the database and decouples the virtual machine from the storage layer. It is suitable for various actual smart contract business scenarios and has good versatility and flexibility.
[0006] To achieve the above objective, according to one aspect of an embodiment of the present invention, a data writing method is provided.
[0007] A data writing method, comprising: establishing a virtual machine instance according to an environment interface implemented by a backend service and a smart contract sent by the backend service, and returning a virtual machine instance handle to the backend service; after receiving the virtual machine instance handle and transaction parameters passed by the backend service, executing the smart contract through the virtual machine instance, and returning the execution result of the smart contract to the backend service, wherein the data generated during the execution of the smart contract is stored in a cache layer; when a call request for a local transaction submission interface is received from the backend service, a local transaction is submitted through the local transaction submission interface to write the data in the cache layer into a database.
[0008] Optionally, before establishing the virtual machine instance, the method includes: registering an environment interface method implemented by the backend service, where the environment interface method implemented by the backend service is an interface method corresponding to a predefined environment interface.
[0009] Optionally, the predefined environment interface is implemented based on an environment interface framework for executing smart contracts on a virtual machine, and the environment interface framework includes an external input general interface, an external output general interface, an environment parameter general interface, and a database I / O general interface.
[0010] Optionally, when the virtual machine instance executes the smart contract, it modifies the cache layer through the environment interface and receives the operation result returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
[0011] Optionally, the method further includes: when a call request for a local transaction rollback interface is received from the backend service, a local transaction rollback is performed through the local transaction rollback interface to clean up data in the cache layer.
[0012] Optionally, when the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, a call request from the backend service to the local transaction commit interface is received; when either the smart contract or the subsequent business logic of the backend service fails to execute, a call request from the backend service to the local transaction rollback interface is received.
[0013] According to another aspect of an embodiment of the present invention, a data writing device is provided.
[0014] A data writing device comprises: a virtual machine instance establishment module, which is used to establish a virtual machine instance according to an environment interface implemented by a backend service and a smart contract sent by the backend service, and return a virtual machine instance handle to the backend service; a smart contract execution module, which is used to execute the smart contract through the virtual machine instance after receiving the virtual machine instance handle and transaction parameters passed by the backend service, and return the execution result of the smart contract to the backend service, wherein the data generated during the execution of the smart contract is stored in a cache layer; a transaction submission module, which is used to submit a local transaction through the local transaction submission interface when receiving a call request for a local transaction submission interface from the backend service, so as to write the data in the cache layer into a database.
[0015] Optionally, it further includes a registration module for registering the environment interface method implemented by the backend service, where the environment interface method implemented by the backend service is an interface method corresponding to a predefined environment interface.
[0016] Optionally, the predefined environment interface is implemented based on an environment interface framework for executing smart contracts on a virtual machine, and the environment interface framework includes an external input general interface, an external output general interface, an environment parameter general interface, and a database I / O general interface.
[0017] Optionally, when the virtual machine instance executes the smart contract, it modifies the cache layer through the environment interface and receives the operation result returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
[0018] Optionally, a transaction rollback module is further included, which is used to: when a call request for a local transaction rollback interface is received from the backend service, perform a local transaction rollback through the local transaction rollback interface to clear the data in the cache layer.
[0019] Optionally, when the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, the transaction submission module receives a call request from the backend service to the local transaction submission interface; when either the smart contract or the subsequent business logic of the backend service fails to execute, the transaction rollback module receives a call request from the backend service to the local transaction rollback interface.
[0020] According to yet another aspect of the embodiments of the present invention, an electronic device is provided.
[0021] An electronic device includes: one or more processors; and a memory for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the data writing method provided by an embodiment of the present invention.
[0022] According to yet another aspect of an embodiment of the present invention, a computer readable medium is provided.
[0023] A computer readable medium stores a computer program, which, when executed by a processor, implements a data writing method provided by an embodiment of the present invention.
[0024] One embodiment of the above invention has the following advantages or beneficial effects: a virtual machine instance is established according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and the virtual machine instance handle is returned to the backend service. After receiving the virtual machine instance handle and transaction parameters passed by the backend service, the smart contract is executed through the virtual machine instance, and the execution result of the smart contract is returned to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer, and when a call request for the local transaction submission interface is received from the backend service, a local transaction is submitted through the local transaction submission interface to write the data in the cache layer into the database. It can ensure the overall transactionality of all logics of the backend service and the virtual machine, provide a set of transaction control interface standards for the upper-level users of the virtual machine, realize the overall control of the virtual machine execution smart contract transaction to the external system, and ensure the security of the database, and decouple the virtual machine from the storage layer, which is suitable for various actual smart contract business scenarios, and has good versatility and flexibility.
[0025] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0027] Figure 1 is a schematic diagram of main steps of a data writing method according to an embodiment of the present invention;
[0028] Figure 2 is a schematic diagram of a virtual machine environment interface framework according to an embodiment of the present invention;
[0029] Figure 3 is a schematic diagram of a virtual machine local method write transaction framework according to an embodiment of the present invention;
[0030] Figure 4 is a schematic diagram of main modules of a data writing device according to an embodiment of the present invention;
[0031] Figure 5 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0032] Figure 6It is a schematic diagram of the structure of a computer system suitable for implementing a server of an embodiment of the present invention. DETAILED DESCRIPTION
[0033] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0034] Figure 1 FIG. 1 is a schematic diagram of the main steps of a data writing method according to an embodiment of the present invention. Figure 1 As shown, a data writing method according to an embodiment of the present invention mainly includes the following steps S101 to S103.
[0035] Step S101: Create a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and return the virtual machine instance handle to the backend service.
[0036] Step S102: After receiving the virtual machine instance handle and transaction parameters passed in by the backend service, the smart contract is executed through the virtual machine instance, and the execution result of the smart contract is returned to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer.
[0037] Step S103: When a call request for a local transaction commit interface is received from the backend service, a local transaction is committed through the local transaction commit interface to write the data in the cache layer into the database.
[0038] Among them, when the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, a call request for the local transaction submission interface from the backend service is received. The subsequent business logic is the business logic that needs to be executed by the backend service after the smart contract is executed in the transaction.
[0039] The backend service is an external system that interacts with the virtual machine in a transaction. The smart contract sent by the backend service is specifically the binary code of the smart contract.
[0040] The virtual machine of the embodiment of the present invention may specifically be a virtual machine developed based on Webassembly. Webassembly is a new bytecode format, a binary instruction format based on a stack-based virtual machine, designed to be directly compiled into Webassembly intermediate bytecode using a high-level language (such as C / C++ / Rust). In the early days, Webassembly was mainly used in web applications, but with its continuous development, more and more projects use it as the final format of smart contracts and use virtual machines that support Webassembly to load and run.
[0041] When the virtual machine developed based on Webassembly in the embodiment of the present invention performs a write operation on the storage layer, the overall transactionality of the operation is ensured, that is, the write operation is either successful as a whole or rolled back as a whole. The execution of a smart contract by a virtual machine is part of a transaction processing flow. To ensure the correctness of the transaction processing, all transactions involved in the processing flow will only be submitted if they are all successful. In the process of executing a smart contract by a virtual machine, the storage of the smart contract will be modified, and transactions will be triggered, etc. These transactions need to be cached first and submitted together after all transactions are successful. Therefore, it is necessary to design a set of transaction control interface standards for the upper-level users of the virtual machine, and hand over the control of the virtual machine's execution of smart contract transactions to an external system for overall control.
[0042] The data writing method of the embodiment of the present invention can be executed by a virtual machine SDK (software development kit). The virtual machine SDK can provide interface capabilities. Specifically, the virtual machine SDK user can inject his own local environment interface implementation. The virtual machine SDK creates a new virtual machine instance and returns the virtual machine instance handle to the virtual machine SDK user. The virtual machine SDK user passes in the acquired virtual machine instance handle and transaction parameters. The transaction parameters include specific smart contract binary code, smart contract operation parameters, etc. The virtual machine instance runs the smart contract to generate queries and modifications to the data. The virtual machine SDK also provides a local transaction submission interface. The external system (i.e., the backend service) can call the local transaction submission interface to refresh the data generated by the virtual machine running the contract from the cache to the database. The interface returns the specific result of the transaction submission success or failure, and then the backend service uploads the results fed back by this transaction submission to the upper layer layer by layer to determine the success or failure of this transaction. The virtual machine SDK also provides a local transaction rollback interface. The external system calls the local transaction rollback interface to clean up the data stored in the cache layer generated by the virtual machine running the smart contract, that is, when the virtual machine SDK receives a call request for the local transaction rollback interface from the backend service, it rolls back the local transaction through the local transaction rollback interface to clean up the data in the cache layer. Among them, if either the smart contract or the subsequent business logic of the backend service fails to execute, the backend service receives a call request for the local transaction rollback interface.
[0043] When a virtual machine instance executes a smart contract, it modifies the cache layer through the environment interface and receives the operation results returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
[0044] Before establishing a virtual machine instance, the embodiment of the present invention can register the environment interface method implemented by the backend service, which is an interface method corresponding to the predefined environment interface. The environment interface implemented by the backend service is also the predefined environment interface.
[0045] The predefined environment interface is implemented based on the environment interface framework of the virtual machine executing the smart contract. The environment interface framework includes the external input general interface, the external output general interface, the environment parameter general interface, and the database I / O general interface.
[0046] Figure 2 Schematic diagram of a virtual machine environment interface framework according to an embodiment of the present invention. Smart contracts applied to the financial system have some local environment common interfaces (such as Figure 2 Through these common interfaces, virtual machines (such as Figure 2 The Webassembly virtual machine shown, i.e., the virtual machine developed based on Webassembly) can obtain input from an external system when running a smart contract. The smart contract can also obtain the transaction initiator, the address of the called smart contract, the amount of the transfer, and other environmental data that will be carried in each transaction from env. The smart contract can also output data to the outside. The smart contract will query the status in the database during operation, or change the status stored in the database. The virtual machine environment interface framework of an embodiment of the present invention includes four parts: input, output, env, and i / o. Among them, input corresponds to the external input general interface, which is used to input data from the outside of the contract; output corresponds to the external output general interface, which is used to return data to the external system after the contract is run; env corresponds to the environment parameter general interface, which is used to obtain external environment parameters, such as the contract caller, contract ID (identification), etc., which are required for each contract call; i / o corresponds to the database I / O general interface, which is used to load data from the database that the contract operation depends on, and store data to the database that the contract operation depends on.
[0047] Based on the environment interface framework for executing smart contracts in a virtual machine, the embodiment of the present invention specifically implements ten environment interfaces, which are pre-defined environment interfaces in the embodiment of the present invention. The ten environment interfaces are environment interfaces abstracted from reading and writing the above four parts (input, output, env, i / o), as follows:
[0048] 1)envGetInputData
[0049] Used to read data from input to the memory address of the smart contract. After deserialization, the smart contract can perform subsequent operations.
[0050] 2)envSetOutputData
[0051] It is used to read data from the memory address of the smart contract and store it in the memory of a system outside the virtual machine (i.e., an external system). The external system performs corresponding deserialization and subsequent processing and returns it to the initiator of the transaction;
[0052] 3)envGetId
[0053] Used to obtain the contract wallet id, which is also the contract wallet address;
[0054] 4)envGetBalance
[0055] Used to obtain the caller's wallet balance. When sending a transfer transaction, you should ensure that the transfer amount is less than the caller's wallet balance, otherwise the transaction should be rejected;
[0056] 5)envGetCallerId
[0057] Used to obtain the caller's wallet ID, which is also the wallet ID of the transaction initiator;
[0058] 6)envGetCallAmount
[0059] Used to obtain the transaction amount. For financial smart contract calls, each call initiated will have a transaction amount.
[0060] 7)envSetStorage
[0061] It is used to store the contract status data that needs to be stored during the execution of smart contracts. If it is a key and value stateless storage type, the key value needs to be concatenated with the corresponding contract wallet id, otherwise it is impossible to distinguish the respective storage status under the contract wallet id;
[0062] 8)envGetStorage
[0063] Used to obtain contract status data during smart contract execution. Because the key value carries the contract wallet id prefix when storing, the contract wallet id prefix also needs to be carried when querying;
[0064] 9)envDelStorage
[0065] Used to delete contract status data during smart contract execution. Because the key value carries the contract wallet id prefix when it is stored, the contract wallet id prefix also needs to be carried when it is deleted;
[0066] 10)envTransfer
[0067] Used by the transaction initiator’s wallet ID to transfer the transaction amount to the contract wallet ID.
[0068] The operations of modifying the database of types 2), 9), and 10) in the above-mentioned environment interface should meet the local transaction standards of TCC, that is, the operation of modifying the state of the database executed by the virtual machine is part of the global transaction. The transactional support standard capability of the virtual machine executing the smart contract proposed in the embodiment of the present invention allows the external system to control the transaction submission and rollback of this transaction. A virtual machine instance is established based on the environment interface implemented by the back-end service (that is, the back-end service implements the above-mentioned ten environment interfaces) and the smart contract sent by the back-end service. The virtual machine instance executes the smart contract. The data generated during the execution of the smart contract is stored in the cache layer. After the subsequent business logic of the smart contract and the back-end service are successfully executed, the transaction is committed and the data is refreshed (that is, written) from the cache layer to the database to prevent dirty writes to the database. The database is operated through the environment interface, which is in turn operated by the smart contract, thereby ensuring the security of the database.
[0069] The above-defined environment interface is implemented based on the environment interface framework of the virtual machine executing the smart contract. Specifically, the reading and writing of input abstracts envGetInputData (an environment interface for reading data from the input to the memory address of the smart contract), and the reading and writing of output abstracts envSetOutputData (an environment interface for reading data from the memory address of the smart contract and storing it in the memory of the system outside the virtual machine (i.e., the external system)). The reading and writing of env abstracts envGetId (an environment interface for obtaining the contract wallet id), envGetBalance (an environment interface for obtaining the caller's wallet balance), envGetCallerId (an environment interface for obtaining the caller's wallet id), and envGetCallAmount (an environment interface for obtaining the current transaction amount). The reading and writing of i / o abstracts envSetStorage (an environment interface for storing contract status data that needs to be stored during the execution of the smart contract), envGetStorage (an environment interface for obtaining contract status data during the execution of the smart contract), envDelStorage (an environment interface for deleting contract status data during the execution of the smart contract), and envTransfer (an environment interface for transferring the transaction amount from the wallet id of the transaction initiator to the contract wallet id).
[0070] As an example, ten environment interfaces may be defined as follows:
[0071] public abstract VmEnv extends BaseHostEnv{
[0072] public static final String ENV_MODULE_NAME="env";
[0073] @Override
[0074] public Memory getMemory(){
[0075] return super.getMemory();
[0076] }
[0077] public abstract RuntimeValues envGetInputData(Memory memory,int addr,int len,int offset);
[0078] public abstract RuntimeValues envSetOutputData(Memory memory,intaddr,int len);
[0079] public abstract RuntimeValues envGetId(Memory memory,int addr);
[0080] public abstract RuntimeValues envGetBalance(Memory memory,int addr);
[0081] public abstract RuntimeValues envGetCallerId(Memory memory,int addr);
[0082] public abstract RuntimeValues envGetCallAmount(Memory memory,intaddr);
[0083] public abstract RuntimeValues envSetStorage(Memory memory,int key,intkeyLen,int data,int dataLen);
[0084] public abstract RuntimeValues envGetStorage(Memory memory,int key,intkeyLen,int data,int dataLen,int offset);
[0085] public abstract RuntimeValues envDelStorage(Memory memory,int key,intkeyLen);
[0086] public abstract RuntimeValues envTransfer(Memory memory,long id,longamount);
[0087] @Override
[0088] public String getModuleName(){
[0089] return ENV_MODULE_NAME;
[0090] }
[0091] }
[0092] It should be noted that the above example is only an exemplary definition method, and the example does not constitute a limitation on the environment interface definition method of the embodiment of the present invention.
[0093] Figure 3 1 is a schematic diagram of a virtual machine local method write transaction framework according to an embodiment of the present invention. Figure 3 As shown, after receiving the transaction, the backend service registers the local environment interface method implemented by itself to the virtual machine SDK, and sends the binary code of the .wasm contract (smart contract, referred to as contract) in the transaction to the virtual machine SDK. The virtual machine SDK calls (i.e., establishes) a virtual machine instance according to the binary code of the .wasm contract and the local environment interface. The virtual machine SDK returns the newly created virtual machine instance handle to the backend service, and the backend service passes the virtual machine instance handle and transaction parameters to the virtual machine SDK. The virtual machine SDK executes 1 to N times (the specific number varies depending on the business) to query the database for status and modify the status. The operation of modifying the status requires the key value to be added with the contract wallet id prefix and then stored in the cache layer. Before executing the smart contract, the environment parameters are obtained through the environment interface. The environment interface returns the environment parameter data, such as the contract caller, contract ID, and other data required for each contract call. After the contract is executed, the operation result is returned. The virtual machine of Webassembly (referred to as Wasm) only supports integers, so the operation result returned by the contract is an integer. If the integer zero is returned, it means that the virtual machine executes the smart contract successfully. If a non-zero integer is returned, it means that the virtual machine fails to execute the smart contract. After the backend service obtains the execution result of the smart contract, it performs subsequent logical processing (i.e., executes subsequent business logic). If the subsequent logical processing is successful and the return value of the virtual machine execution contract is also zero (i.e., the smart contract is successfully executed), the backend service calls the local transaction submission interface provided by the virtual machine SDK, and the local transaction submission interface refreshes the cached data to the database. If an error occurs when the backend service executes the subsequent business logic or the virtual machine SDK executes the contract, and the execution fails, the backend service calls the local transaction rollback interface of the virtual machine SDK to clean up the data in the cache layer.
[0094] When the virtual machine executes the smart contract, it needs to obtain or modify the state from the storage layer indirectly through the environment interface. In order to make the virtual machine execute the smart contract transactionally, the virtual machine SDK of the embodiment of the present invention encapsulates the virtual machine in one layer. The virtual machine SDK provides the following four interface capabilities: 1) The virtual machine SDK user injects his own local environment interface implementation, the virtual machine SDK creates a new virtual machine instance, and returns the virtual machine instance handle to the virtual machine SDK user. 2) The virtual machine SDK user passes in the obtained virtual machine instance handle and transaction. The transaction includes the specific smart contract binary code, smart contract operation parameters, etc. The virtual machine instance runs the smart contract and generates queries and modifications to the data. 3) Local transaction submission interface. The external system (i.e., the backend service) calls the local transaction submission interface to refresh the data generated by the virtual machine running the contract from the cache to the database. The interface returns the specific transaction submission success or failure result, and then the backend service uploads this submission feedback layer by layer to the upper layer to determine whether the transaction result is successful or failed. 4) Local transaction rollback interface. The external system calls the local transaction rollback interface to clean up the data generated by the virtual machine running the contract and stored in the cache layer.
[0095] The embodiment of the present invention designs ten local environment interfaces, which are universal and applicable to various actual smart contract business scenarios. The decoupling of the virtual machine and the storage layer is achieved through the design of the environment interface. In the prior art, the virtual machine and the storage layer of Ethereum 1.0 are highly coupled, and the storage layer cannot be flexibly replaced. The ten environment interfaces of the embodiment of the present invention enable the virtual machine user to register the environment interface implemented by it and call up the virtual machine instance according to his own needs, which is flexible. At the same time, the database can only be operated through the environment interface, and the environment interface can only be operated by the smart contract. Therefore, the security of the database is guaranteed through this mechanism, and external users cannot directly operate the database. The modification of the database by the smart contract can only be carried out by sending transactions. All transactions that write the contract status need to carry the user's private key signature. In addition, the virtual machine SDK of the embodiment of the present invention provides four interface capabilities, namely, an interface for creating a new virtual machine instance and returning the virtual machine instance handle to the virtual machine SDK user, an interface for the virtual machine SDK user to pass in the acquired virtual machine instance handle and transaction, a local transaction submission interface, and a local transaction rollback interface. The four interface capabilities designed in the virtual machine local method writing transaction framework of the embodiment of the present invention enable the virtual machine SDK to have transaction consistency technical capabilities through these four interfaces, so that the back-end service and all the logic of the virtual machine SDK constitute an overall transaction, ensuring the overall transactionality of all the logic of the back-end service and the virtual machine, and designing a set of transaction control interface standards for the upper-level users of the virtual machine to realize the overall control of the virtual machine executing smart contract transactions by the external system.
[0096] Figure 4 4 is a schematic diagram of main modules of a data writing device according to an embodiment of the present invention.
[0097] like Figure 4 As shown, a data writing device 400 according to an embodiment of the present invention mainly includes: a virtual machine instance establishment module 401, a smart contract execution module 402, and a transaction submission module 403.
[0098] The virtual machine instance establishment module 401 is used to establish a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and return the virtual machine instance handle to the backend service.
[0099] The smart contract execution module 402 is used to execute the smart contract through the virtual machine instance after receiving the virtual machine instance handle and transaction parameters passed by the backend service, and return the execution result of the smart contract to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer.
[0100] The transaction commit module 403 is used to commit a local transaction through the local transaction commit interface when receiving a call request from the backend service to the local transaction commit interface, so as to write the data in the cache layer into the database.
[0101] When the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, the transaction submission module 403 receives a call request from the backend service to the local transaction submission interface.
[0102] The data writing device 400 further includes a registration module for registering an environment interface method implemented by a backend service, wherein the environment interface method implemented by the backend service is an interface method corresponding to a predefined environment interface.
[0103] The predefined environment interface is implemented based on the environment interface framework of the virtual machine executing the smart contract. The environment interface framework includes the external input general interface, the external output general interface, the environment parameter general interface, and the database I / O general interface.
[0104] When a virtual machine instance executes a smart contract, it modifies the cache layer through the environment interface and receives the operation results returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
[0105] The data writing device 400 may further include a transaction rollback module, which is used to: when receiving a call request for a local transaction rollback interface from a backend service, perform a local transaction rollback through the local transaction rollback interface to clear the data in the cache layer.
[0106] In the event that either the smart contract or the subsequent business logic of the backend service fails to execute, the transaction rollback module receives a call request from the backend service to the local transaction rollback interface.
[0107] In addition, the specific implementation content of the data writing device in the embodiment of the present invention has been described in detail in the data writing method described above, so the repeated content will not be described again here.
[0108] Figure 5 An exemplary system architecture 500 is shown to which the data writing method or data writing device according to the embodiment of the present invention can be applied.
[0109] like Figure 5 As shown, system architecture 500 may include terminal devices 501, 502, 503, a network 504 and a server 505. Network 504 is used to provide a medium for communication links between terminal devices 501, 502, 503 and server 505. Network 504 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0110] Users can use terminal devices 501, 502, 503 to interact with server 505 through network 504 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 501, 502, 503, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).
[0111] The terminal devices 501 , 502 , and 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.
[0112] The server 505 may be a server that provides various services, such as a background management server (only an example) that provides support for websites browsed by users using the terminal devices 501, 502, and 503. The background management server may analyze and process the received information query request and other data, and feed back the processing results (such as transaction results - only an example) to the terminal device.
[0113] It should be noted that the data writing method provided in the embodiment of the present invention is generally executed by the server 505 , and accordingly, the data writing device is generally disposed in the server 505 .
[0114] It should be understood that Figure 5 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0115] Reference below Figure 6 , which shows a schematic diagram of the structure of a computer system 600 suitable for implementing a server of an embodiment of the present application. Figure 6 The server shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0116] like Figure 6 As shown, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage part 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the system 600 are also stored. The CPU 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0117] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed, so that a computer program read therefrom is installed into the storage section 608 as needed.
[0118] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the main step schematic diagram can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the main step schematic diagram. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the central processing unit (CPU) 601, the above-mentioned functions defined in the system of the present application are executed.
[0119] It should be noted that the computer-readable medium shown in the present invention can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present application, a computer-readable signal medium can include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal can take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0120] The main step schematic diagram and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the main step schematic diagram or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or the main step schematic diagram, and the combination of the boxes in the block diagram or the main step schematic diagram can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0121] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be set in a processor. For example, they may be described as: a processor including a virtual machine instance establishment module, a smart contract execution module, and a transaction submission module. The names of these modules do not, in some cases, constitute limitations on the modules themselves. For example, the virtual machine instance establishment module may also be described as "a module for establishing a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and returning a virtual machine instance handle to the backend service."
[0122] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently and not be assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device includes: establishing a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and returning the virtual machine instance handle to the backend service; after receiving the virtual machine instance handle and transaction parameters passed by the backend service, executing the smart contract through the virtual machine instance, and returning the execution result of the smart contract to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer; when receiving a call request for the local transaction submission interface from the backend service, a local transaction is submitted through the local transaction submission interface to write the data in the cache layer into the database.
[0123] According to the technical solution of the embodiment of the present invention, a virtual machine instance is established according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and the virtual machine instance handle is returned to the backend service. After receiving the virtual machine instance handle and transaction parameters passed by the backend service, the smart contract is executed through the virtual machine instance, and the execution result of the smart contract is returned to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer, and when a call request to the local transaction submission interface is received from the backend service, a local transaction is submitted through the local transaction submission interface to write the data in the cache layer into the database. It can ensure the overall transactionality of all logics of the backend service and the virtual machine, design a set of transaction control interface standards for the upper-level users of the virtual machine, realize the overall control of the virtual machine execution smart contract transaction by the external system, and ensure the security of the database, and decouple the virtual machine from the storage layer, which is suitable for various actual smart contract business scenarios, and has good versatility and flexibility.
[0124] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A data writing method, characterized in that: include: Establish a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and return the virtual machine instance handle to the backend service; After receiving the virtual machine instance handle and transaction parameters passed by the backend service, the smart contract is executed through the virtual machine instance, and the execution result of the smart contract is returned to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer; When a call request for a local transaction commit interface is received from the backend service, a local transaction is committed through the local transaction commit interface to write the data in the cache layer into a database.
2. The method according to claim 1, characterized in that: Before establishing the virtual machine instance, the following steps are included: The environment interface method implemented by the backend service is registered, where the environment interface method implemented by the backend service is an interface method corresponding to a predefined environment interface.
3. The method according to claim 2, characterized in that The predefined environment interface is implemented based on the environment interface framework of the virtual machine executing the smart contract, and the environment interface framework includes an external input general interface, an external output general interface, an environment parameter general interface, and a database I / O general interface.
4. The method according to claim 1, characterized in that: When the virtual machine instance executes the smart contract, it modifies the cache layer through the environment interface and receives the operation result returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
5. The method according to claim 1, characterized in that Also includes: When a call request for a local transaction rollback interface is received from the backend service, a local transaction rollback is performed through the local transaction rollback interface to clean up the data in the cache layer.
6. The method according to claim 5, characterized in that In the event that the execution of either the smart contract or the subsequent business logic of the backend service fails, a call request for a local transaction rollback interface from the backend service is received.
7. The method according to claim 1, characterized in that When the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, a call request from the backend service to the local transaction submission interface is received.
8. A data writing device, characterized in that: include: A virtual machine instance creation module, used to create a virtual machine instance according to the environment interface implemented by the backend service and the smart contract sent by the backend service, and return the virtual machine instance handle to the backend service; A smart contract execution module, configured to execute the smart contract through the virtual machine instance after receiving the virtual machine instance handle and transaction parameters passed by the backend service, and return the execution result of the smart contract to the backend service, wherein the data generated during the execution of the smart contract is stored in the cache layer; The transaction commit module is used to, when receiving a call request from the backend service to the local transaction commit interface, perform a local transaction commit through the local transaction commit interface to write the data in the cache layer into the database.
9. The device according to claim 8, characterized in that Also includes registration modules for: The environment interface method implemented by the backend service is registered, where the environment interface method implemented by the backend service is an interface method corresponding to a predefined environment interface.
10. The device according to claim 9, characterized in that The predefined environment interface is implemented based on the environment interface framework of the virtual machine executing the smart contract, and the environment interface framework includes an external input general interface, an external output general interface, an environment parameter general interface, and a database I / O general interface.
11. The device according to claim 8, characterized in that When the virtual machine instance executes the smart contract, it modifies the cache layer through the environment interface and receives the operation result returned by the cache layer through the environment interface to obtain the execution result of the smart contract.
12. The device according to claim 8, characterized in that Also includes a transaction rollback module for: When a call request for a local transaction rollback interface is received from the backend service, a local transaction rollback is performed through the local transaction rollback interface to clean up the data in the cache layer.
13. The device according to claim 12, characterized in that In the event that either the smart contract or the subsequent business logic of the backend service fails to execute, the transaction rollback module receives a call request from the backend service to the local transaction rollback interface.
14. The device according to claim 8, characterized in that When the smart contract is executed successfully and the subsequent business logic of the backend service is executed successfully, the transaction submission module receives a call request from the backend service to the local transaction submission interface.
15. An electronic device, characterized in that: include: one or more processors; a memory for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors are enabled to implement the method according to any one of claims 1 to 7.
16. A computer readable medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Transaction processing method and device for Ethereum virtual machine, equipment, program and medium
CN112286643A
Transaction processing method and device based on blockchain virtual machine multiplexing
CN112529711A
EVM-based transaction processing method, device, program and medium
US20210365903A1
