WO2021179697A1 - Method and device for executing functional module in virtual machine - Google Patents

Method and device for executing functional module in virtual machine Download PDF

Info

Publication number
WO2021179697A1
WO2021179697A1 PCT/CN2020/132824 CN2020132824W WO2021179697A1 WO 2021179697 A1 WO2021179697 A1 WO 2021179697A1 CN 2020132824 W CN2020132824 W CN 2020132824W WO 2021179697 A1 WO2021179697 A1 WO 2021179697A1
Authority
WO
WIPO (PCT)
Prior art keywords
instruction
wasm
sequence
instruction sequence
function
Prior art date
Application number
PCT/CN2020/132824
Other languages
French (fr)
Chinese (zh)
Inventor
郭学鹏
张磊
Original Assignee
支付宝(杭州)信息技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 支付宝(杭州)信息技术有限公司 filed Critical 支付宝(杭州)信息技术有限公司
Publication of WO2021179697A1 publication Critical patent/WO2021179697A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/455Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
    • G06F9/45504Abstract machines for programme code execution, e.g. Java virtual machine [JVM], interpreters, emulators
    • G06F9/45516Runtime code conversion or optimisation

Definitions

  • One or more embodiments of this specification relate to the field of blockchain technology, and more specifically, to methods and devices for caching and executing smart contracts in a blockchain.
  • Virtual Machine is a complete computer system with complete hardware system functions that is simulated by software and runs in a completely isolated environment. Since the virtual machine can isolate the impact of the underlying hardware platform and operating system on the upper-layer applications, it is very beneficial to the development of the upper-layer applications. There is no need to pay attention to the details of the underlying platform in the upper-level application development process, only the specific business logic. After the development is completed, the virtual machine runs the upper-layer application and is responsible for converting the application code into code suitable for execution on the underlying platform. Specifically, in many scenarios, upper-level applications are written and developed by developers using high-level languages, and then compiled into bytecode by a compiler.
  • Bytecode is an execution program, a binary file composed of a sequence of operation code (op code)-data pairs, which is a kind of intermediate code. Then, the interpreter in the virtual machine interprets and executes the instruction stream represented by the bytecode.
  • a virtual machine can be deployed in each node of the blockchain network.
  • Users can write a smart contract in a high-level language, and then compile it into bytecode by a compiler, include the bytecode in the transaction used to deploy the smart contract, and publish it to the blockchain network, that is, deploy to the block In each node of the chain network.
  • the virtual machine in each node interprets and executes the bytecode.
  • WASM As the writing language for smart contracts.
  • WASM or WebAssembly
  • the WASM virtual machine is a stack type virtual machine in which the operands of all instructions are placed on the stack, that is, the operands are obtained from the stack for each operation, and the result is pushed onto the stack after the instruction execution ends.
  • the speed of interpretation and execution of bytecode instruction sequences by the WASM virtual machine interpreter is critical to the performance of the entire system. Therefore, it is hoped that there will be an improved scheme to further improve the execution efficiency of the bytecode instruction stream in the WASM virtual machine.
  • the embodiments of this specification aim to provide a more effective solution for executing function modules in a virtual machine.
  • one aspect of this specification provides a method for executing a functional module in a virtual machine, the method comprising: obtaining a WASM instruction sequence of the functional module; determining whether the WASM instruction sequence includes a predetermined continuous instruction, The predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result; in the case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions , Merge each group of predetermined consecutive instructions in the instruction sequence into corresponding merged instructions respectively; interpret and execute the instruction-merged WASM instruction sequence of the functional module.
  • the virtual machine is a virtual machine in a blockchain node
  • the function module is a first function defined in a first contract
  • obtaining the WASM instruction sequence of the function module includes: When the first transaction that calls the first function in the first contract is executed, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
  • obtaining the WASM instruction sequence of the first function includes obtaining the variable value of the first contract from the node local storage medium.
  • the long coded instruction sequence decodes the variable-length coded instruction sequence of the first contract to obtain the WASM instruction sequence of the first function.
  • interpreting the combined WASM instruction sequence for executing the function module includes interpreting the combined WASM instruction sequence for executing the first function based on the data field of the first transaction.
  • the predetermined continuous instruction includes at least two instructions, and the operating parameter corresponding to the last instruction in the predetermined continuous instruction is associated with at least one instruction before the last instruction in the predetermined continuous instruction.
  • the last instruction includes a basic operation instruction.
  • the apparatus includes: an acquiring unit configured to acquire a WASM instruction sequence of the functional module; and a determining unit configured to determine the WASM instruction Whether the sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merge instruction, and the predetermined continuous instruction and its corresponding merged instruction have the same interpretation execution result; the merging unit is configured to determine the When the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence are respectively combined into corresponding combined instructions; the execution unit is configured to interpret and execute the instructions of the functional module Combined WASM instruction sequence.
  • the virtual machine is a virtual machine in a blockchain node
  • the function module is a first function defined in a first contract
  • the acquiring unit is further configured to: During the first transaction of the first function in a contract, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
  • the acquiring unit is further configured to acquire the variable-length-encoded instruction sequence of the first contract from the node local storage medium, and perform the variable-length-encoded instruction sequence of the first contract. Decode, get the WASM instruction sequence of the first function.
  • the execution unit is further configured to interpret the combined WASM instruction sequence for executing the first function based on the data field of the first transaction.
  • Another aspect of this specification provides a computer-readable storage medium on which a computer program is stored.
  • the computer program is executed in a computer, the computer is caused to execute any of the above-mentioned methods.
  • Another aspect of this specification provides a computing device, including a memory and a processor, the memory stores executable code, and when the processor executes the executable code, any one of the above methods is implemented.
  • the predetermined continuous instruction is merged into a preset new instruction by loading and parsing the WASM code in the virtual machine, thereby reducing the number of instructions, shortening the code execution time, and improving the virtual machine effectiveness.
  • the instruction form of the smart contract stored in the blockchain which has higher flexibility and applicability.
  • Figure 1 is a schematic diagram of the process of calling the function calculation in the contract SCORE in the blockchain network
  • Figure 2 illustrates the data changes in the stack when the instruction sequence of the calculation function is interpreted in the WASM virtual machine
  • Fig. 3 shows a flow chart of a method for executing the function calculation of the contract SCORE in a virtual machine according to an embodiment of the present specification
  • Figure 4 schematically shows the data change process in the stack when the fusion instruction sequence of the calculation function is interpreted in the WASM virtual machine
  • Figure 5 shows a flowchart of a smart contract caching and execution method according to another embodiment of this specification
  • Fig. 6 shows an apparatus for executing function modules in a virtual machine according to an embodiment of the present specification.
  • the blockchain network includes several nodes, and each node can communicate with each other.
  • a user can send a transaction to any node in the blockchain through the client.
  • the transaction can be an ordinary transfer transaction, or it can be used to deploy a smart contract or call a smart contract.
  • the user Bob of the blockchain wants to issue a smart contract Contract SCORE.
  • the smart contract uses the following formula (1) to calculate the user's reward score s based on the user's consumption amount x and sales amount y in the platform, for example:
  • Bob can write the smart contract in a high-level language such as C++, and then use the compiler to convert the C++ smart contract into the following WASM instruction sequence:
  • the above WASM instruction sequence is displayed in the form of text.
  • the WASM instruction sequence is expressed in binary format, so that the above function calculation has the following form (here for simplicity, it is expressed in hexadecimal Show binary code):
  • each node in the blockchain network can obtain and deploy the above-mentioned contract SCORE in the local storage medium.
  • FIG. 1 is a schematic diagram of the process of calling the function calculation in the contract SCORE in the blockchain network 100.
  • the blockchain network 100 includes nodes A to D.
  • the users of the blockchain can connect to any of the nodes A to D through the client. .
  • Alice can send, for example, transaction 1 (Tx 1) to node A in the blockchain through her client to call the contract.
  • Tx 1 transaction 1
  • the from field can be the address of the caller Alice, and the to field is the contract name and contract address of the SCORE contract, indicating the smart contract called by the exchange.
  • the instruction sequence of the function calculation is obtained from the local storage medium, and the instruction sequence is executed sequentially.
  • Figure 2 illustrates the data changes in the stack when the instruction sequence of the calculation function is interpreted in the WASM virtual machine.
  • the interpreter in the virtual machine interprets the execution instruction get_local y, and pushes the value of the parameter y obtained from the corresponding transaction of the function calculation of the call contract SCORE onto the stack, assuming that the value of y is 1000; in Figure 2b, By interpreting the execution instruction i32.const 2, the constant value 2 is pushed onto the stack; in Figure 2c, by interpreting the execution instruction i32.mul, the two latest values (that is, the value of the y parameter 1000 and the constant value) are first popped from the stack. Value 2), multiply them to obtain the product 2000, and then push the result 2000 into the stack.
  • the dotted line indicates that after 1000 and 2 are popped, 1000 and 2 are no longer in the stack; in Figure 2d
  • the execution instruction get_local x the value 500 of the parameter x obtained from the corresponding transaction is pushed into the stack, assuming that the value of x is 500; in Figure 2e, the execution instruction i32.add is interpreted from the stack Pop two values (that is, the product result of the above multiplication 2000 and the value of parameter x 500), add them to obtain the sum 2500, and then push the result 2500 onto the stack;
  • the instruction i32 is executed through interpretation .const 3, push the constant value 3 into the stack; in Figure 2g, by interpreting the execution instruction i32.mul, two values are popped from the stack (that is, the result of the above addition is 2500 and the constant value 3), and they are added together Multiply to obtain the product 7500, and then push the result 7500 into the stack, so that the remaining value 7500 in the stack is the return value of
  • the predetermined consecutive instructions in the WASM instruction sequence of the function calculation are merged in the WASM virtual machine to obtain the fused instruction sequence, and Execute the fused instruction sequence, which can greatly reduce the execution time.
  • each node can obtain transaction 1, and execute the function calculation in the contract SCORE through the WASM virtual machine, thereby returning the contract execution result.
  • the command sequence of the contract SCORE is obtained from the local storage medium
  • the WASM command sequence of the function calculation is obtained therefrom
  • the WASM command sequence of the function calculation is fused, and based on The data field of transaction 1 interprets and executes the instruction sequence fused with instructions, thereby shortening the transaction execution time.
  • FIG. 1 and the corresponding description are only schematic and not restrictive.
  • the blockchain network is not limited to a consortium chain network, but may also be a public chain network.
  • the WASM instruction sequence is not limited to the instruction sequence of the smart contract in the blockchain, but may also be the instruction sequence of the computing module in other application scenarios.
  • a smart contract in the blockchain will be taken as an example for an exemplary description.
  • Fig. 3 shows a flow chart of a method for executing function calculation of contract SCORE in a virtual machine according to an embodiment of the present specification, including steps S302 to S308.
  • Step S302 Obtain the WASM instruction sequence of the function calculation.
  • Step S304 Determine whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result.
  • Step S306 In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions.
  • Step S308 Interpret the combined WASM instruction sequence that executes the calculation function.
  • Step S302 Obtain the WASM instruction sequence of the function calculation.
  • the pre-deployed contract SCORE instruction sequence can be found from the local storage medium.
  • a key value table corresponding to each contract is generated in a storage medium (such as a hard disk), where the key is the hash value of the contract name, and the value is the storage address of the contract, so that the hash value of the contract address of the contract SCORE can be retrieved.
  • find the storage address of the contract SCORE in the storage medium so that the instruction sequence of the contract SCORE can be read from the storage medium according to the storage address.
  • the instruction sequence of the contract SCORE stored in the storage medium is a WASM instruction sequence, so that the WASM instruction sequence of the contract SCORE can be directly obtained from the storage medium, so that the WASM instruction sequence of the function calculation can be obtained therefrom.
  • the instruction sequence of the SCORE contract stored in the storage medium is encoded in a leb 128 variable-length encoding method, so as to reduce the file size and make better use of storage space.
  • Leb 128, or "Little-Endian Base 128” is a variable-length encoding for any signed or unsigned integer. Therefore, an integer encoded with Leb128 will change the number of bytes according to the size of the number.
  • the encoding method of leb 128, of the 8 bits (8 bits) of each byte the lower 7 bits are the valid data part, and the highest bit is used to indicate whether the current byte is the last byte, and 1 means the last byte. Byte, 0 means it is not the last byte, that is, the subsequent bytes need to be continued. In this way, different numbers of bytes can be used to encode integers of different sizes.
  • each byte is first restored to 8 bits, and the highest bit is used to determine whether to follow the following bytes to form the target data together. Therefore, if the instruction sequence is decoded, an instruction of variable length will be obtained. In order to store these instructions, variable-length instructions can be converted into memory-aligned fixed-length instructions. In this way, the WASM instruction sequence of the contract SCORE can be obtained. After the WASM instruction sequence of the contract SCORE is obtained, the WASM instruction sequence of the function calculation can be obtained.
  • the pre-cached WASM instruction sequence of the SCORE contract can be read from the memory.
  • step S304 it is determined whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merged instruction, and the predetermined continuous instruction and its corresponding merged instruction have the same interpretation execution result.
  • the predetermined continuous instruction is a number of predetermined continuous instructions that can be combined. For such predetermined continuous instructions, a corresponding combined instruction and a machine code corresponding to the combined instruction have been set in advance, and the machine code is executing After that, the corresponding consecutive instructions have the same execution result.
  • a combined instruction “i32.mul y, const 2” can be preset relative to the continuous instruction “get_local y/i32.const 2/i32.mul”.
  • the combined instruction actually The middle is a binary code, and a binary code that is not used in the WASM instruction set can be selected to represent the combined instruction.
  • a machine code (machine code) corresponding to the combined instruction is preset, and the corresponding operation of the machine code is to obtain the value of parameter y, and compare the value of parameter y with 2.
  • the value of the parameter y can be obtained from the data in the transaction that calls the SCORE contract. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the three consecutive instructions, and the product of the value of the parameter y and 2 is pushed onto the stack.
  • a combined instruction "i32.add x" can be preset relative to the continuous instruction “get_local x/i32.add”, and at the same time, the machine code corresponding to the combined instruction is preset in the interpreter of the virtual machine , The corresponding operation of this machine code is to pop the latest value from the stack, get the value of parameter x, add the popped value to the value of parameter x, and push the result of the addition into the stack, where Get the value of x from the transaction data of the call contract SCORE. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the two consecutive instructions, both of which are the result of adding the product and the value of the parameter x to the stack.
  • a combined instruction “i32.mul const 3” can be preset relative to the continuous instruction “i32.const 3/i32.mul”, and at the same time, the virtual machine interpreter is preset to correspond to the combined instruction
  • the machine code corresponding to the machine code is to pop the latest value from the stack, multiply the popped value by 3, and push the product into the stack. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the two consecutive instructions. Both the result of the above addition and the product of 3 are pushed into the stack, and the final push is The value in the stack is the return value of the function.
  • each instruction has a certain correlation.
  • the last instruction i32.mul will use the obtained value (y) or immediate value (ie constant 2) of the previous two instructions.
  • the last instruction in the predetermined continuous instruction may also be a basic operation instruction such as a subtraction instruction and a division instruction, so that instruction merging can be performed similarly.
  • Step S306 In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions.
  • the instruction sequence includes three sets of predetermined consecutive instructions, so that the three sets of instructions can be merged into corresponding merged instructions respectively, that is, the merged WASM instruction sequence is:
  • Step S308 Interpret the combined WASM instruction sequence that executes the calculation function.
  • the combined WASM instruction sequence that executes the calculation function is interpreted.
  • FIG. 4 schematically shows the data change process in the stack when the fusion instruction sequence of the calculation execution function is interpreted in the WASM virtual machine.
  • the execution instruction i32.mul y const2 is first interpreted by an interpreter, specifically by interpreting the combined instruction, Obtain the preset machine code of the merge instruction, and then execute the machine code, thereby obtaining the value of parameter y 1000 from the data field of transaction 1, and multiplying 1000 by the constant 2 to obtain the product 2000, and then multiply the product 2000 pushed into the stack.
  • the execution instruction i32.add x is interpreted in the virtual machine of node D, and the value in it is popped from the stack, that is, 2000.
  • the value of parameter x is obtained from the data of transaction 1. Add to get the sum 2500 and push 2500 onto the stack.
  • the interpretation and execution of the merge instruction corresponds to Fig. 2d and Fig. 2e in Fig. 2. It can be seen that by merging the two instructions get_local x and i32.add, when interpreting and executing the merge instruction, the data push to the stack is reduced. , To speed up the contract execution speed.
  • the execution instruction i32.mul const 3 is interpreted in the virtual machine of node E or node D, and the latest value, namely 2500, is popped from the stack, and it is multiplied by the constant 3 to obtain the product 7500, and 7500 is pushed into the stack.
  • Fig. 5 shows a flowchart of a smart contract caching and execution method according to another embodiment of the present specification. The method is executed in a virtual machine of any node in the blockchain, and includes the following steps.
  • Step S502 in response to transaction 2 received by the blockchain node, obtain the WASM instruction sequence of the contract SCORE from the local storage medium. In the transaction 2, the function calculation in the contract SCORE is called.
  • Step S504 Determine whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result.
  • Step S506 In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions, respectively.
  • step S502 to step S506 can refer to the above description of step S302 to step S306, which will not be repeated here.
  • the difference is that the WASM command sequence in these steps is the contract SCORE command sequence.
  • step S508 the WASM instruction sequence combined with the instruction is cached in the memory in association with the contract SCORE.
  • node D can allocate a starting address for storing the WASM instruction sequence in its memory, so that the instruction sequence is stored in the memory starting from the starting address, and the instruction sequence in the
  • the parameters related to the storage start address are modified to the start address, and the contract SCORE is recorded in a predetermined table in the memory corresponding to the start address.
  • the table records key-value pairs, for example, the key is, for example, the hash value of the contract address of the contract SCORE, and the value is the start address.
  • the cached contract SCORE can be directly obtained from the memory
  • the fusion instruction sequence of this function eliminates the need to obtain the instruction sequence from the storage medium and perform the instruction fusion process every time a transaction that calls the function in the SCORE contract is executed.
  • step S510 based on the data field of transaction 2, the fused instruction sequence of the function calculation of the execution contract SCORE is interpreted.
  • step S308 For this step, reference may be made to the above description of step S308, which will not be repeated here.
  • the embodiment of this specification does not Limited to the above-mentioned embodiment.
  • the WASM language can be used in a variety of scenarios. For example, in AutoCAD, Google Earth, Unity, and WebPack, the corresponding functions are implemented by executing the WASM program in a virtual machine. In these scenarios, similarly, when executing the function module, by In the virtual machine, the instruction sequence of the functional module is merged in the static analysis stage, so that when the merged instruction sequence is executed, the program execution time can be greatly reduced, and the merged instruction sequence can be cached in the memory to perform repeated functions. , The cached fusion instruction sequence can be directly obtained from the memory, further accelerating the execution speed of the program.
  • FIG. 6 shows an apparatus 600 for executing functional modules in a virtual machine according to an embodiment of the present specification.
  • the apparatus includes: an acquiring unit 61, a determining unit 62, a merging unit 63, and an executing unit 64.
  • the obtaining unit 61 is configured to obtain the WASM instruction sequence of the functional module.
  • the determining unit 62 is configured to determine whether a predetermined continuous instruction is included in the WASM instruction sequence, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation and execution result.
  • the merging unit 63 is configured to, in a case where it is determined that the WASM instruction sequence includes at least one group of predetermined consecutive instructions, merge each group of predetermined consecutive instructions in the instruction sequence into corresponding merged instructions, respectively.
  • the execution unit 64 is configured to interpret and execute the combined WASM instruction sequence of the functional module.
  • the virtual machine is a virtual machine in a blockchain node
  • the function module is a first function defined in a first contract.
  • the acquiring unit 61 is further configured to execute the call During the first transaction of the first function in the first contract, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
  • the obtaining unit 61 is further configured to obtain the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and obtain the variable-length-encoded instruction sequence of the first contract Decode and get the WASM instruction sequence of the first function.
  • the execution unit 64 is further configured to interpret the instruction-fused WASM instruction sequence for executing the first function based on the data field of the first transaction.
  • Another aspect of this specification provides a computer-readable storage medium on which a computer program is stored.
  • the computer program is executed in a computer, the computer is caused to execute any of the above-mentioned methods.
  • Another aspect of this specification provides a computing device, including a memory and a processor, the memory stores executable code, and when the processor executes the executable code, any one of the above methods is implemented.
  • the predetermined continuous instruction is merged into a preset new instruction by loading and parsing the WASM code in the virtual machine, thereby reducing the number of instructions, shortening the code execution time, and improving the virtual machine effectiveness.
  • the instruction form of the smart contract stored in the blockchain which has higher flexibility and applicability.
  • the software module can be placed in random access memory (RAM), memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, or technical fields Any other form of storage medium known within.
  • RAM random access memory
  • ROM read-only memory
  • electrically programmable ROM electrically erasable programmable ROM
  • register hard disk, removable disk, CD-ROM, or technical fields Any other form of storage medium known within.

Abstract

A method and device for executing a functional module in a virtual machine. The method comprises: obtaining a WASM instruction sequence of the functional module; determining whether the WASM instruction sequence comprises predetermined continuous instructions, wherein corresponding combined instructions are preset for the predetermined continuous instructions; the predetermined continuous instructions have the same interpretation execution result as combined instructions corresponding to the predetermined continuous instructions; in the case where it is determined that the WASM instruction sequence comprises at least one group of predetermined continuous instructions, respectively combining each group of predetermined continuous instructions in the instruction sequence into corresponding combined instructions; and interpretating and executing the WASM instruction sequence subjected to instruction combination of the functional module.

Description

一种在虚拟机中执行功能模块的方法和装置Method and device for executing function module in virtual machine 技术领域Technical field
本说明书一个或多个实施例涉及区块链技术领域,更具体地,涉及在区块链中缓存和执行智能合约的方法和装置。One or more embodiments of this specification relate to the field of blockchain technology, and more specifically, to methods and devices for caching and executing smart contracts in a blockchain.
背景技术Background technique
虚拟机(Virtual Machine)是通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统。由于虚拟机可以隔离底层硬件平台以及操作系统对上层应用的影响,因此非常有利于上层应用的开发。上层应用开发过程中无需关注底层平台的细节,只需要关注具体的业务逻辑。开发完成后,由虚拟机运行上层应用,负责将应用的代码转换为适于底层平台执行的代码。具体地,在许多场景中,上层应用由开发人员使用高级语言编写开发,之后通过编译器编译为字节码(bytecode)。字节码是一种执行程序,由一序列操作码(op码)-数据对组成的二进制文件,是一种中间码。然后,虚拟机中的解释器对字节码代表的指令流进行解释和执行。Virtual Machine (Virtual Machine) is a complete computer system with complete hardware system functions that is simulated by software and runs in a completely isolated environment. Since the virtual machine can isolate the impact of the underlying hardware platform and operating system on the upper-layer applications, it is very beneficial to the development of the upper-layer applications. There is no need to pay attention to the details of the underlying platform in the upper-level application development process, only the specific business logic. After the development is completed, the virtual machine runs the upper-layer application and is responsible for converting the application code into code suitable for execution on the underlying platform. Specifically, in many scenarios, upper-level applications are written and developed by developers using high-level languages, and then compiled into bytecode by a compiler. Bytecode is an execution program, a binary file composed of a sequence of operation code (op code)-data pairs, which is a kind of intermediate code. Then, the interpreter in the virtual machine interprets and executes the instruction stream represented by the bytecode.
例如,在支持智能合约的区块链应用场景中,可以在区块链网络的每个节点中部署虚拟机。用户可以用高级语言编写智能合约,然后经由编译器编译为字节码之后,将该字节码包含在用于部署智能合约的交易中,发布到区块链网络中,也就是部署到区块链网络的各个节点中。在需要执行智能合约时,由各个节点中的虚拟机对该字节码进行解释执行。For example, in a blockchain application scenario that supports smart contracts, a virtual machine can be deployed in each node of the blockchain network. Users can write a smart contract in a high-level language, and then compile it into bytecode by a compiler, include the bytecode in the transaction used to deploy the smart contract, and publish it to the blockchain network, that is, deploy to the block In each node of the chain network. When the smart contract needs to be executed, the virtual machine in each node interprets and executes the bytecode.
随着WASM语言的出现,越来越多的区块链中使用WASM作为智能合约的编写语言,相对应地,使用WASM虚拟机来执行智能合约。WASM即WebAssembly,是由W3C社区组织推出的一种为栈式虚拟机设计的二进制指令形式,是一种新的平台无关的中间字节码格式。WASM虚拟机为栈式虚拟机,其中,将所有指令的操作数都放到栈上,即,每次操作都从栈上获取操作数,并在指令执行结束后将结果压入栈中。With the emergence of the WASM language, more and more blockchains use WASM as the writing language for smart contracts. Correspondingly, the WASM virtual machine is used to execute smart contracts. WASM, or WebAssembly, is a binary instruction format designed for stack virtual machines launched by the W3C community organization, and it is a new platform-independent intermediate bytecode format. The WASM virtual machine is a stack type virtual machine in which the operands of all instructions are placed on the stack, that is, the operands are obtained from the stack for each operation, and the result is pushed onto the stack after the instruction execution ends.
在包括但不限于区块链的各种应用场景中,WASM虚拟机解释器对字节码指令序列的解释执行速度对于整个系统的性能都至关重要。因此,希望能有改进的方案,进一步提高WASM虚拟机中字节码指令流的执行效率。In various application scenarios including but not limited to blockchain, the speed of interpretation and execution of bytecode instruction sequences by the WASM virtual machine interpreter is critical to the performance of the entire system. Therefore, it is hoped that there will be an improved scheme to further improve the execution efficiency of the bytecode instruction stream in the WASM virtual machine.
发明内容Summary of the invention
本说明书实施例旨在提供一种更有效的在虚拟机中执行功能模块的方案。The embodiments of this specification aim to provide a more effective solution for executing function modules in a virtual machine.
为实现上述目的,本说明书一个方面提供一种在虚拟机中执行功能模块的方法,所述方法包括:获取所述功能模块的WASM指令序列;确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令;解释执行所述功能模块的经指令合并的WASM指令序列。In order to achieve the above objective, one aspect of this specification provides a method for executing a functional module in a virtual machine, the method comprising: obtaining a WASM instruction sequence of the functional module; determining whether the WASM instruction sequence includes a predetermined continuous instruction, The predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result; in the case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions , Merge each group of predetermined consecutive instructions in the instruction sequence into corresponding merged instructions respectively; interpret and execute the instruction-merged WASM instruction sequence of the functional module.
在一种实施方式中,所述虚拟机为区块链节点中的虚拟机,所述功能模块为第一合约中定义的第一函数,其中,获取所述功能模块的WASM指令序列包括,在执行调用第一合约中的第一函数的第一交易时,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列。In one embodiment, the virtual machine is a virtual machine in a blockchain node, and the function module is a first function defined in a first contract, and obtaining the WASM instruction sequence of the function module includes: When the first transaction that calls the first function in the first contract is executed, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
在一种实施方式中,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列包括,从节点本地存储介质中获取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取第一函数的WASM指令序列。In one embodiment, based on the instruction sequence of the first contract stored in the node local storage medium, obtaining the WASM instruction sequence of the first function includes obtaining the variable value of the first contract from the node local storage medium. The long coded instruction sequence decodes the variable-length coded instruction sequence of the first contract to obtain the WASM instruction sequence of the first function.
在一种实施方式中,解释执行所述功能模块的经指令合并的WASM指令序列包括,基于所述第一交易的数据字段,解释执行所述第一函数的经指令合并的WASM指令序列。In one embodiment, interpreting the combined WASM instruction sequence for executing the function module includes interpreting the combined WASM instruction sequence for executing the first function based on the data field of the first transaction.
在一种实施方式中,所述预定连续指令包括至少两个指令,所述预定连续指令中的最后一个指令对应的操作参数与该预定连续指令中在最后一个指令之前的至少一个指令相关联。In one embodiment, the predetermined continuous instruction includes at least two instructions, and the operating parameter corresponding to the last instruction in the predetermined continuous instruction is associated with at least one instruction before the last instruction in the predetermined continuous instruction.
在一种实施方式中,所述最后一个指令包括基本运算指令。In one embodiment, the last instruction includes a basic operation instruction.
本说明书另一方面提供一种在虚拟机中执行功能模块的装置,所述装置包括:获取单元,配置为,获取所述功能模块的WASM指令序列;确定单元,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;合并单元,配置为,在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令;执行单元,配置为,解释 执行所述功能模块的经指令合并的WASM指令序列。Another aspect of this specification provides an apparatus for executing a function module in a virtual machine. The apparatus includes: an acquiring unit configured to acquire a WASM instruction sequence of the functional module; and a determining unit configured to determine the WASM instruction Whether the sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merge instruction, and the predetermined continuous instruction and its corresponding merged instruction have the same interpretation execution result; the merging unit is configured to determine the When the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence are respectively combined into corresponding combined instructions; the execution unit is configured to interpret and execute the instructions of the functional module Combined WASM instruction sequence.
在一种实施方式中,所述虚拟机为区块链节点中的虚拟机,所述功能模块为第一合约中定义的第一函数,其中,所述获取单元还配置为,在执行调用第一合约中的第一函数的第一交易时,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列。In one embodiment, the virtual machine is a virtual machine in a blockchain node, and the function module is a first function defined in a first contract, wherein the acquiring unit is further configured to: During the first transaction of the first function in a contract, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
在一种实施方式中,所述获取单元还配置为,从节点本地存储介质中获取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取第一函数的WASM指令序列。In one embodiment, the acquiring unit is further configured to acquire the variable-length-encoded instruction sequence of the first contract from the node local storage medium, and perform the variable-length-encoded instruction sequence of the first contract. Decode, get the WASM instruction sequence of the first function.
在一种实施方式中,所述执行单元还配置为,基于所述第一交易的数据字段,解释执行所述第一函数的经指令合并的WASM指令序列。In an embodiment, the execution unit is further configured to interpret the combined WASM instruction sequence for executing the first function based on the data field of the first transaction.
本说明书另一方面提供一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行上述任一项方法。Another aspect of this specification provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed in a computer, the computer is caused to execute any of the above-mentioned methods.
本说明书另一方面提供一种计算设备,包括存储器和处理器,所述存储器中存储有可执行代码,所述处理器执行所述可执行代码时,实现上述任一项方法。Another aspect of this specification provides a computing device, including a memory and a processor, the memory stores executable code, and when the processor executes the executable code, any one of the above methods is implemented.
在根据本说明书实施例的WASM指令融合方案中,通过在虚拟机加载解析WASM代码阶段,将预定连续指令合并为一条预设的新指令,从而减少指令数目,缩短代码执行时间,提高了虚拟机执行效率。另外,通过本说明书实施例的方案,不需要改变智能合约存入区块链的指令形式,具有更高的灵活性和适用性。In the WASM instruction fusion scheme according to the embodiment of this specification, the predetermined continuous instruction is merged into a preset new instruction by loading and parsing the WASM code in the virtual machine, thereby reducing the number of instructions, shortening the code execution time, and improving the virtual machine effectiveness. In addition, through the solution of the embodiment of this specification, there is no need to change the instruction form of the smart contract stored in the blockchain, which has higher flexibility and applicability.
附图说明Description of the drawings
通过结合附图描述本说明书实施例,可以使得本说明书实施例更加清楚:By describing the embodiments of this specification in conjunction with the accompanying drawings, the embodiments of this specification can be made clearer:
图1是在区块链网络中调用合约SCORE中函数calculation的过程示意图;Figure 1 is a schematic diagram of the process of calling the function calculation in the contract SCORE in the blockchain network;
图2示意出当在WASM虚拟机中解释执行函数calculation的指令序列时栈中的数据变化;Figure 2 illustrates the data changes in the stack when the instruction sequence of the calculation function is interpreted in the WASM virtual machine;
图3出根据本说明书实施例的一种在虚拟机中执行合约SCORE的函数calculation的方法流程图;Fig. 3 shows a flow chart of a method for executing the function calculation of the contract SCORE in a virtual machine according to an embodiment of the present specification;
图4示意示出当在WASM虚拟机中解释执行函数calculation的融合指令序列时栈中的数据变化过程;Figure 4 schematically shows the data change process in the stack when the fusion instruction sequence of the calculation function is interpreted in the WASM virtual machine;
图5示出根据本说明书另一个实施例的智能合约缓存和执行方法流程图;Figure 5 shows a flowchart of a smart contract caching and execution method according to another embodiment of this specification;
图6示出根据本说明书实施例的一种在虚拟机中执行功能模块的装置。Fig. 6 shows an apparatus for executing function modules in a virtual machine according to an embodiment of the present specification.
具体实施方式Detailed ways
下面将结合附图描述本说明书实施例。The embodiments of this specification will be described below in conjunction with the drawings.
如本领域技术人员所知,区块链网络中包含若干节点,各个节点之间可以进行通信。在例如联盟链的区块链中,用户可以通过客户端向区块链中任一节点发送交易,交易既可以是普通的转账交易,也可以用于部署智能合约或调用智能合约。假设区块链的用户Bob想要发布智能合约Contract SCORE,该智能合约例如通过如下的公式(1)基于平台中用户的消费金额x和销售金额y计算用户的奖励分值s:As those skilled in the art know, the blockchain network includes several nodes, and each node can communicate with each other. In a blockchain such as a consortium chain, a user can send a transaction to any node in the blockchain through the client. The transaction can be an ordinary transfer transaction, or it can be used to deploy a smart contract or call a smart contract. Suppose that the user Bob of the blockchain wants to issue a smart contract Contract SCORE. The smart contract uses the following formula (1) to calculate the user's reward score s based on the user's consumption amount x and sales amount y in the platform, for example:
S=(x+y*2)*3               (1)S=(x+y*2)*3 (1)
Bob可使用C++等高级语言编写该智能合约,然后通过编译器将该C++智能合约转换成如下所示的WASM指令序列:Bob can write the smart contract in a high-level language such as C++, and then use the compiler to convert the C++ smart contract into the following WASM instruction sequence:
Contract SCOREContract SCORE
function calculation(param x i32)(param y i32){function calculation(param x i32)(param y i32){
get_local yget_local y
i32.const 2i32.const 2
i32.muli32.mul
get_local xget_local x
i32.addi32.add
i32.const 3i32.const 3
i32.muli32.mul
}。}.
可以理解,为了便于阅读,上述WASM指令序列以文本的形式显示,在实际中,WASM指令序列以二进制的格式表示,从而上述函数calculation具有如下的形式(这里为了简化表示,以十六进制数示出二进制码):It can be understood that, for ease of reading, the above WASM instruction sequence is displayed in the form of text. In practice, the WASM instruction sequence is expressed in binary format, so that the above function calculation has the following form (here for simplicity, it is expressed in hexadecimal Show binary code):
{20y{20y
41 0241 02
6c6c
20x20x
6a6a
41 0341 03
6c}。6c}.
然后,Bob可通过其客户端向区块链中任一节点发布用于部署该合约SCORE的交易。从而,区块链网络中的各个节点均可以获取并在本地存储介质中部署上述合约SCORE。Then, Bob can publish the transaction for deploying the SCORE contract to any node in the blockchain through his client. Therefore, each node in the blockchain network can obtain and deploy the above-mentioned contract SCORE in the local storage medium.
图1是在区块链网络100中调用合约SCORE中函数calculation的过程示意图,其中区块链网络100包括节点A~D,区块链的用户可通过客户端连接节点A~D中任一节点。假定区块链的用户Alice想要调用合约SCORE中的函数calculation,Alice可以通过其客户端向区块链中的节点A发送例如交易1(Tx 1),进行合约调用。Figure 1 is a schematic diagram of the process of calling the function calculation in the contract SCORE in the blockchain network 100. The blockchain network 100 includes nodes A to D. The users of the blockchain can connect to any of the nodes A to D through the client. . Assuming that the user Alice of the blockchain wants to call the function calculation in the contract SCORE, Alice can send, for example, transaction 1 (Tx 1) to node A in the blockchain through her client to call the contract.
具体而言,如图1中所示,在交易1中,from字段可以是调用者Alice的地址,to字段即为上述SCORE合约的合约名称和合约地址,表示该交易所调用的智能合约。在Data字段,包含要调用的合约中的方法或函数名称(calculation)和传入的参数值(Alice的消费金额x=50和销售金额y=100)。Specifically, as shown in Figure 1, in transaction 1, the from field can be the address of the caller Alice, and the to field is the contract name and contract address of the SCORE contract, indicating the smart contract called by the exchange. In the Data field, it contains the method or function name (calculation) in the contract to be called and the parameter value passed in (Alice's consumption amount x=50 and sales amount y=100).
通常,在各个节点部署了合约SCORE之后,在后期执行调用合约SCORE中函数calculation的交易时,从本地存储介质中获取函数calculation的指令序列,并顺序执行该指令序列。Generally, after each node deploys the contract SCORE, when the transaction that calls the function calculation in the contract SCORE is executed later, the instruction sequence of the function calculation is obtained from the local storage medium, and the instruction sequence is executed sequentially.
图2示意出当在WASM虚拟机中解释执行函数calculation的指令序列时栈中的数据变化。在图2a,通过虚拟机中的解释器解释执行指令get_local y,将从调用合约SCORE的函数calculation的相应交易获取的参数y的值推入栈中,假设y的值为1000;在图2b,通过解释执行指令i32.const 2,将常数值2推入栈中;在图2c中,通过解释执行指令i32.mul,首先从栈中弹出两个最新的值(即y参数的值1000和常数值2),将它们相乘,以获取其乘积2000,然后将结果2000推入栈中,图2c中,以虚线表示在弹出1000和2之后,栈中已经没有1000和2;在图2d中,通过解释执行指令get_local x,则将从所述相应交易获取的参数x的值500推入栈中,假设x的值为500;在图2e中,通过解释执行指令i32.add,从栈中弹出两个值(即上述乘法的乘积结果2000和参数x的值500),将它们相加,以获得其和2500,然后将结果2500推入栈中;在图2f中, 通过解释执行指令i32.const 3,将常数值3推入栈中;在图2g中,通过解释执行指令i32.mul,从栈中弹出两个值(即上述相加的结果2500和常数值3),将它们相乘,以获取乘积7500,然后将结果7500推入栈中,从而该栈中剩下的值7500即为执行该函数的返回值。从该过程可见,如果直接执行上述指令序列,将存在多次对栈的数据推入、数据弹出过程,耗时较多。Figure 2 illustrates the data changes in the stack when the instruction sequence of the calculation function is interpreted in the WASM virtual machine. In Figure 2a, the interpreter in the virtual machine interprets the execution instruction get_local y, and pushes the value of the parameter y obtained from the corresponding transaction of the function calculation of the call contract SCORE onto the stack, assuming that the value of y is 1000; in Figure 2b, By interpreting the execution instruction i32.const 2, the constant value 2 is pushed onto the stack; in Figure 2c, by interpreting the execution instruction i32.mul, the two latest values (that is, the value of the y parameter 1000 and the constant value) are first popped from the stack. Value 2), multiply them to obtain the product 2000, and then push the result 2000 into the stack. In Figure 2c, the dotted line indicates that after 1000 and 2 are popped, 1000 and 2 are no longer in the stack; in Figure 2d By interpreting the execution instruction get_local x, the value 500 of the parameter x obtained from the corresponding transaction is pushed into the stack, assuming that the value of x is 500; in Figure 2e, the execution instruction i32.add is interpreted from the stack Pop two values (that is, the product result of the above multiplication 2000 and the value of parameter x 500), add them to obtain the sum 2500, and then push the result 2500 onto the stack; in Figure 2f, the instruction i32 is executed through interpretation .const 3, push the constant value 3 into the stack; in Figure 2g, by interpreting the execution instruction i32.mul, two values are popped from the stack (that is, the result of the above addition is 2500 and the constant value 3), and they are added together Multiply to obtain the product 7500, and then push the result 7500 into the stack, so that the remaining value 7500 in the stack is the return value of executing the function. It can be seen from this process that if the above instruction sequence is directly executed, there will be multiple data push and data pop processes on the stack, which consumes more time.
为了减少执行调用函数calculation的时间,在本说明书实施例中,在该调用过程中,通过在WASM虚拟机中将函数calculation的WASM指令序列中的预定连续指令进行合并从而获取融合的指令序列,并执行融合的指令序列,从而可大大减少执行时间。In order to reduce the time to execute the calculation of the calling function, in the embodiment of this specification, in the calling process, the predetermined consecutive instructions in the WASM instruction sequence of the function calculation are merged in the WASM virtual machine to obtain the fused instruction sequence, and Execute the fused instruction sequence, which can greatly reduce the execution time.
具体是,在Alice将交易1发布到区块链中去之后,各个节点都可以获取交易1,通过WASM虚拟机执行合约SCORE中的函数calculation,从而返回合约执行结果。其中,例如在节点D的虚拟机中执行交易1时,从本地存储介质中获取上述合约SCORE的指令序列,从中获取函数calculation的WASM指令序列,对函数calculation的WASM指令序列进行指令融合,并基于交易1的数据字段解释执行该经指令融合的指令序列,从而缩短交易执行时间。Specifically, after Alice publishes transaction 1 to the blockchain, each node can obtain transaction 1, and execute the function calculation in the contract SCORE through the WASM virtual machine, thereby returning the contract execution result. Among them, for example, when transaction 1 is executed in the virtual machine of node D, the command sequence of the contract SCORE is obtained from the local storage medium, the WASM command sequence of the function calculation is obtained therefrom, the WASM command sequence of the function calculation is fused, and based on The data field of transaction 1 interprets and executes the instruction sequence fused with instructions, thereby shortening the transaction execution time.
可以理解,图1及相应的描述仅仅是示意性的,而不是限制性的,例如,所述区块链网络不限于为联盟链网络,而也可以为公链网络。另外,所述WASM指令序列不限于为区块链中智能合约的指令序列,而也可以为其它应用场景中的计算模块的指令序列。下文中将以区块链中的智能合约为例进行示例性描述。It can be understood that FIG. 1 and the corresponding description are only schematic and not restrictive. For example, the blockchain network is not limited to a consortium chain network, but may also be a public chain network. In addition, the WASM instruction sequence is not limited to the instruction sequence of the smart contract in the blockchain, but may also be the instruction sequence of the computing module in other application scenarios. In the following, a smart contract in the blockchain will be taken as an example for an exemplary description.
图3出根据本说明书实施例的一种在虚拟机中执行合约SCORE的函数calculation的方法流程图,包括步骤S302~S308。Fig. 3 shows a flow chart of a method for executing function calculation of contract SCORE in a virtual machine according to an embodiment of the present specification, including steps S302 to S308.
步骤S302,获取函数calculation的WASM指令序列。Step S302: Obtain the WASM instruction sequence of the function calculation.
步骤S304,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果。Step S304: Determine whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result.
步骤S306,在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令。Step S306: In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions.
步骤S308,解释执行函数calculation的经指令合并的WASM指令序列。Step S308: Interpret the combined WASM instruction sequence that executes the calculation function.
下面详细描述图3中各个步骤的执行过程。The following describes the execution process of each step in FIG. 3 in detail.
步骤S302,获取函数calculation的WASM指令序列。Step S302: Obtain the WASM instruction sequence of the function calculation.
如上文所述,当执行调用合约SCORE中某个函数(例如函数calculation)的交易(例如交易1)时,可从本地存储介质中找到预先部署的合约SCORE的指令序列。通常,在存储介质(例如硬盘)中生成与各个合约对应的键值表,其中,键为例如合约名称的哈希值,值为合约的存储地址,从而可通过检索合约SCORE的合约地址的哈希值,找到合约SCORE在存储介质中的存储地址,从而可根据所述存储地址从存储介质中读取合约SCORE的指令序列。As described above, when a transaction (for example, transaction 1) that calls a certain function in the contract SCORE (for example, function calculation) is executed, the pre-deployed contract SCORE instruction sequence can be found from the local storage medium. Usually, a key value table corresponding to each contract is generated in a storage medium (such as a hard disk), where the key is the hash value of the contract name, and the value is the storage address of the contract, so that the hash value of the contract address of the contract SCORE can be retrieved. Hopefully, find the storage address of the contract SCORE in the storage medium, so that the instruction sequence of the contract SCORE can be read from the storage medium according to the storage address.
在一种实施方式中,所述存储介质中存储的合约SCORE的指令序列为WASM指令序列,从而可从存储介质中直接获取合约SCORE的WASM指令序列,从而可从其中获取函数calculation的WASM指令序列。In one embodiment, the instruction sequence of the contract SCORE stored in the storage medium is a WASM instruction sequence, so that the WASM instruction sequence of the contract SCORE can be directly obtained from the storage medium, so that the WASM instruction sequence of the function calculation can be obtained therefrom. .
在一种实施方式中,所述存储介质中存储的合约SCORE的指令序列采用leb 128可变长编码方式进行编码,以减小文件体积,更好利用存储空间。Leb 128即"Little-Endian Base 128",是对任意有符号或者无符号整型数的可变长度的编码,因此,用Leb128编码的整数,会根据数字的大小改变所占字节数。根据leb 128的编码方式,每个字节的8位(8个bits)中,较低的7位为有效数据部分,最高位用于指示当前字节是否为最后一个字节,1表示最后一个字节,0表示不是最后一个字节,也就是需要接续后续字节。如此,可以采用不同字节数编码不同大小的整数。In one embodiment, the instruction sequence of the SCORE contract stored in the storage medium is encoded in a leb 128 variable-length encoding method, so as to reduce the file size and make better use of storage space. Leb 128, or "Little-Endian Base 128", is a variable-length encoding for any signed or unsigned integer. Therefore, an integer encoded with Leb128 will change the number of bytes according to the size of the number. According to the encoding method of leb 128, of the 8 bits (8 bits) of each byte, the lower 7 bits are the valid data part, and the highest bit is used to indicate whether the current byte is the last byte, and 1 means the last byte. Byte, 0 means it is not the last byte, that is, the subsequent bytes need to be continued. In this way, different numbers of bytes can be used to encode integers of different sizes.
相应地,对于leb编码的指令序列,在解码数据时,先将各个字节还原为8位,通过最高位判断是否要接续后面的字节,共同形成目标数据。因此,对该指令序列进行解码,会得到不定长的指令。为了对这些指令进行存储,可以将可变长指令转换为内存对齐的定长指令。从而可获取合约SCORE的WASM指令序列,在获取合约SCORE的WASM指令序列之后,可获取函数calculation的WASM指令序列。Correspondingly, for the leb-encoded instruction sequence, when decoding the data, each byte is first restored to 8 bits, and the highest bit is used to determine whether to follow the following bytes to form the target data together. Therefore, if the instruction sequence is decoded, an instruction of variable length will be obtained. In order to store these instructions, variable-length instructions can be converted into memory-aligned fixed-length instructions. In this way, the WASM instruction sequence of the contract SCORE can be obtained. After the WASM instruction sequence of the contract SCORE is obtained, the WASM instruction sequence of the function calculation can be obtained.
可以理解,在该实施例中,不限于从节点本地存储介质中获取WASM指令序列,例如,在一种实施方式中,可从内存中读取预先缓存的合约SCORE的WASM指令序列。It can be understood that in this embodiment, it is not limited to obtain the WASM instruction sequence from the local storage medium of the node. For example, in one embodiment, the pre-cached WASM instruction sequence of the SCORE contract can be read from the memory.
在步骤S304,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果。In step S304, it is determined whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merged instruction, and the predetermined continuous instruction and its corresponding merged instruction have the same interpretation execution result.
所述预定连续指令是预先确定的可以进行合并的若干个连续指令,对于这样的预 定连续指令,已经预先设置了对应的合并指令,以及与该合并指令相对应的机器码,该机器码在执行之后与其对应的连续指令具有相同的执行结果。The predetermined continuous instruction is a number of predetermined continuous instructions that can be combined. For such predetermined continuous instructions, a corresponding combined instruction and a machine code corresponding to the combined instruction have been set in advance, and the machine code is executing After that, the corresponding consecutive instructions have the same execution result.
例如,对于上述函数calculation的指令序列,可以相对于连续指令“get_local y/i32.const 2/i32.mul”预设一个合并指令“i32.mul y,const 2”,同样地,该合并指令实际中为二进制码,可选定一个在WASM指令集中未使用的二进制码来表示该合并指令。同时,在虚拟机的解释器中,预先设定了与该合并指令相对应的机器码(machine code),该机器码对应的操作为,获取参数y的值,将参数y的值与2相乘,并将乘积推入栈中,其中,可从调用合约SCORE的交易中的数据中获取参数y的值。可见,该合并指令对应的机器码的操作结果与所述三个连续指令对应的机器码执行结果是相同的,都是将参数y的值与2的乘积推入栈中。For example, for the instruction sequence of the above function calculation, a combined instruction “i32.mul y, const 2” can be preset relative to the continuous instruction “get_local y/i32.const 2/i32.mul”. Similarly, the combined instruction actually The middle is a binary code, and a binary code that is not used in the WASM instruction set can be selected to represent the combined instruction. At the same time, in the interpreter of the virtual machine, a machine code (machine code) corresponding to the combined instruction is preset, and the corresponding operation of the machine code is to obtain the value of parameter y, and compare the value of parameter y with 2. Multiply and push the product into the stack, where the value of the parameter y can be obtained from the data in the transaction that calls the SCORE contract. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the three consecutive instructions, and the product of the value of the parameter y and 2 is pushed onto the stack.
另外,可相对于连续指令“get_local x/i32.add”预设一个合并指令“i32.add x”,同时,在虚拟机的解释器中,预先设定了与该合并指令相对应的机器码,该机器码对应的操作为,从栈中弹出最新的值,获取参数x的值,将弹出的值与参数x的值相加,并将相加的结果推入栈中,其中,可从调用合约SCORE的交易的数据中获取x的值。可见,该合并指令对应的机器码的操作结果与所述两个连续指令对应的机器码执行结果是相同的,都是将上述乘积与参数x的值的相加结果推入栈中。In addition, a combined instruction "i32.add x" can be preset relative to the continuous instruction "get_local x/i32.add", and at the same time, the machine code corresponding to the combined instruction is preset in the interpreter of the virtual machine , The corresponding operation of this machine code is to pop the latest value from the stack, get the value of parameter x, add the popped value to the value of parameter x, and push the result of the addition into the stack, where Get the value of x from the transaction data of the call contract SCORE. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the two consecutive instructions, both of which are the result of adding the product and the value of the parameter x to the stack.
另外,可相对于连续指令“i32.const 3/i32.mul”预设一个合并指令“i32.mul const 3”,同时,在虚拟机的解释器中,预先设定了与该合并指令相对应的机器码,该机器码对应的操作为,从栈中弹出最新的值,并该弹出的值与3相乘,并将乘积推入栈中。可见,该合并指令对应的机器码的操作结果与所述两个连续指令对应的机器码执行结果是相同的,都是将上述相加的结果与3的乘积推入栈中,该最后推入栈中的值即为该函数的返回值。In addition, a combined instruction “i32.mul const 3” can be preset relative to the continuous instruction “i32.const 3/i32.mul”, and at the same time, the virtual machine interpreter is preset to correspond to the combined instruction The machine code corresponding to the machine code is to pop the latest value from the stack, multiply the popped value by 3, and push the product into the stack. It can be seen that the operation result of the machine code corresponding to the combined instruction is the same as the execution result of the machine code corresponding to the two consecutive instructions. Both the result of the above addition and the product of 3 are pushed into the stack, and the final push is The value in the stack is the return value of the function.
从上面三个示例中可以看出,在预定连续指令中,各个指令具有一定的相关性。例如,对于连续指令“get_local y/i32.const 2/i32.mul”,最后一个指令i32.mul会用到前面两个指令的获取值(y)或立即数(即常数2)。所述预定连续指令中的最后一个指令除了为乘法、加法指令之外,例如还可以为减法指令、除法指令等基本运算指令,从而可类似地进行指令合并。It can be seen from the above three examples that in the predetermined consecutive instructions, each instruction has a certain correlation. For example, for the continuous instruction "get_local y/i32.const 2/i32.mul", the last instruction i32.mul will use the obtained value (y) or immediate value (ie constant 2) of the previous two instructions. In addition to the multiplication and addition instructions, the last instruction in the predetermined continuous instruction may also be a basic operation instruction such as a subtraction instruction and a division instruction, so that instruction merging can be performed similarly.
步骤S306,在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令。Step S306: In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions.
例如,在上述步骤S304中,已经确定所述指令序列中包括三组预定连续指令,从而,可将这三组指令分别合并为对应的合并指令,即,经合并之后的WASM指令序列为:For example, in the above step S304, it has been determined that the instruction sequence includes three sets of predetermined consecutive instructions, so that the three sets of instructions can be merged into corresponding merged instructions respectively, that is, the merged WASM instruction sequence is:
{i32.mul y const 2{i32.mul y const 2
i32.add xi32.add x
i32.mul const 3i32.mul const 3
}。}.
步骤S308,解释执行函数calculation的经指令合并的WASM指令序列。Step S308: Interpret the combined WASM instruction sequence that executes the calculation function.
也即,基于交易1的数据字段,解释执行函数calculation的经指令合并的WASM指令序列。That is, based on the data field of transaction 1, the combined WASM instruction sequence that executes the calculation function is interpreted.
图4示意示出当在WASM虚拟机中解释执行函数calculation的融合指令序列时栈中的数据变化过程。对于上述在步骤S306中获取的函数calculation的融合指令序列,在图4a中,在例如节点D的虚拟机中首先通过解释器解释执行指令i32.mul y const 2,具体是通过解释该合并指令,获取与该合并指令的预设的机器码,然后执行该机器码,从而从交易1的数据字段中获取参数y的值1000,并将1000与常数2相乘,得到乘积2000,并将该乘积2000推入栈中。从图4a可见,对合并指令i32.mul y const 2的解释执行,只对栈进行了一次数据推入,即推入乘积2000,而在图2中,解释执行与该合并指令对应的连续三个指令,参考图2a-2c,需要向栈中推入三次数据,从栈中弹出一次数据,才获取将乘积2000推入栈中的结果。对比图4a和图2a-2c可见,通过根据本说明书实施例的融合指令序列的方案,大大简化了对虚拟机中栈的操作过程,加快了交易执行速度。FIG. 4 schematically shows the data change process in the stack when the fusion instruction sequence of the calculation execution function is interpreted in the WASM virtual machine. For the above-mentioned fusion instruction sequence of function calculation obtained in step S306, in Fig. 4a, in a virtual machine such as node D, the execution instruction i32.mul y const2 is first interpreted by an interpreter, specifically by interpreting the combined instruction, Obtain the preset machine code of the merge instruction, and then execute the machine code, thereby obtaining the value of parameter y 1000 from the data field of transaction 1, and multiplying 1000 by the constant 2 to obtain the product 2000, and then multiply the product 2000 pushed into the stack. It can be seen from Figure 4a that the interpretation and execution of the merge instruction i32.mul y const 2 only pushes data once to the stack, that is, pushes the product 2000. In Figure 2, the interpretation and execution corresponds to the merge instruction for three consecutive times. This instruction, referring to Figures 2a-2c, needs to push data into the stack three times and pop the data once from the stack to obtain the result of pushing the product 2000 into the stack. Comparing FIGS. 4a and 2a-2c, it can be seen that through the fusion instruction sequence solution according to the embodiment of the present specification, the operation process of the stack in the virtual machine is greatly simplified, and the transaction execution speed is accelerated.
类似地,在图4b中,在节点D的虚拟机中解释执行指令i32.add x,从栈中弹出其中的值,即2000,从交易1的数据获取参数x的值500,将2000与500相加,以获取和2500,并将2500推入栈中。该合并指令的解释执行对应于图2中的图2d和图2e,可见,通过对get_local x和i32.add两个指令的合并,在解释执行合并指令时,减少了一次对栈的数据推入,加快了合约执行速度。Similarly, in Figure 4b, the execution instruction i32.add x is interpreted in the virtual machine of node D, and the value in it is popped from the stack, that is, 2000. The value of parameter x is obtained from the data of transaction 1. Add to get the sum 2500 and push 2500 onto the stack. The interpretation and execution of the merge instruction corresponds to Fig. 2d and Fig. 2e in Fig. 2. It can be seen that by merging the two instructions get_local x and i32.add, when interpreting and executing the merge instruction, the data push to the stack is reduced. , To speed up the contract execution speed.
在图4c中,在节点E或节点D的虚拟机中解释执行指令i32.mul const 3,从栈中弹出最新的值,即2500,将其与常数3相乘,以获得乘积7500,并将7500推入栈中。In Figure 4c, the execution instruction i32.mul const 3 is interpreted in the virtual machine of node E or node D, and the latest value, namely 2500, is popped from the stack, and it is multiplied by the constant 3 to obtain the product 7500, and 7500 is pushed into the stack.
通过比较图2和图4可直观地得出,通过根据图3所示方法对指令序列进行融合, 相比于解释执行初始的指令序列,使得在解释执行融合指令序列时,以对栈的更少的数据操作实现了相同的合约执行结果,大大加快了合约执行速度。By comparing Figure 2 and Figure 4, it can be intuitively concluded that by fusing the instruction sequence according to the method shown in Figure 3, compared to interpreting and executing the initial instruction sequence, the stack is more accurate when interpreting and executing the fused instruction sequence. Fewer data operations achieve the same contract execution result, which greatly accelerates the contract execution speed.
图5示出根据本说明书另一个实施例的智能合约缓存和执行方法流程图。该方法在区块链中任一节点的虚拟机中执行,包括以下步骤。Fig. 5 shows a flowchart of a smart contract caching and execution method according to another embodiment of the present specification. The method is executed in a virtual machine of any node in the blockchain, and includes the following steps.
步骤S502,响应于区块链节点接收的交易2,从本地存储介质中获取合约SCORE的WASM指令序列。所述交易2中调用合约SCORE中的函数calculation。Step S502, in response to transaction 2 received by the blockchain node, obtain the WASM instruction sequence of the contract SCORE from the local storage medium. In the transaction 2, the function calculation in the contract SCORE is called.
步骤S504,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果。Step S504: Determine whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation execution result.
步骤S506,在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令。Step S506: In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is merged into corresponding merged instructions, respectively.
步骤S502~步骤S506的执行可参考上文对步骤S302~步骤S306的描述,在此不再赘述,所不同的是,这些步骤中的WASM指令序列为合约SCORE的指令序列。The execution of step S502 to step S506 can refer to the above description of step S302 to step S306, which will not be repeated here. The difference is that the WASM command sequence in these steps is the contract SCORE command sequence.
步骤S508,将经指令合并的WASM指令序列与合约SCORE关联地缓存到内存中。In step S508, the WASM instruction sequence combined with the instruction is cached in the memory in association with the contract SCORE.
在一种实施方式中,节点D可在其内存中分配用于存储所述WASM指令序列的起始地址,从而将该指令序列从该起始地址开始存入内存中,将该指令序列中的与其存储起始地址相关的参数修改为所述起始地址,并在内存中的预定表格中记录合约SCORE与该起始地址相对应。该表例如记录键值对,键例如为合约SCORE的合约地址的哈希值,值为所述起始地址。In an implementation manner, node D can allocate a starting address for storing the WASM instruction sequence in its memory, so that the instruction sequence is stored in the memory starting from the starting address, and the instruction sequence in the The parameters related to the storage start address are modified to the start address, and the contract SCORE is recorded in a predetermined table in the memory corresponding to the start address. The table records key-value pairs, for example, the key is, for example, the hash value of the contract address of the contract SCORE, and the value is the start address.
在将经指令合并的WASM指令序列缓存到内存中去之后,在该节点再次接收调用合约SCORE中某个函数(函数calculation或其它函数)的交易时,可直接从内存中获取缓存的合约SCORE中该函数的融合指令序列,从而不需要每次在执行调用合约SCORE中函数的交易时都从存储介质中获取指令序列、并进行指令融合过程。After the WASM instruction sequence merged by the instructions is cached in the memory, when the node again receives a transaction that calls a function in the contract SCORE (function calculation or other functions), the cached contract SCORE can be directly obtained from the memory The fusion instruction sequence of this function eliminates the need to obtain the instruction sequence from the storage medium and perform the instruction fusion process every time a transaction that calls the function in the SCORE contract is executed.
步骤S510,基于交易2的数据字段解释执行合约SCORE的函数calculation的经融合的指令序列。In step S510, based on the data field of transaction 2, the fused instruction sequence of the function calculation of the execution contract SCORE is interpreted.
该步骤可参考上文对步骤S308的描述,在此不再赘述。For this step, reference may be made to the above description of step S308, which will not be repeated here.
可以理解,虽然上文以在WASM虚拟机中对智能合约的指令序列进行融合为例说明了在虚拟机中对WASM指令序列进行融合的方法步骤和所带来的技术效果,本说明 书实施例不限于上述实施例。WASM语言可用于多种场景中,如在AutoCAD、GoogleEarth、Unity、WebPack中都通过在虚拟机中执行WASM程序而实施相应的功能,在这些场景中,类似地,在执行功能模块时,通过在虚拟机中在静态解析阶段对功能模块的指令序列进行融合,从而在执行融合的指令序列时,可大大减少程序执行时间,并且通过将融合指令序列缓存到内存中,可在执行重复的功能时,可直接从内存中获取缓存的融合指令序列,进一步加快了程序执行速度。It can be understood that although the above example uses the fusion of the instruction sequence of the smart contract in the WASM virtual machine to illustrate the method steps and the technical effects brought by the fusion of the WASM instruction sequence in the virtual machine, the embodiment of this specification does not Limited to the above-mentioned embodiment. The WASM language can be used in a variety of scenarios. For example, in AutoCAD, Google Earth, Unity, and WebPack, the corresponding functions are implemented by executing the WASM program in a virtual machine. In these scenarios, similarly, when executing the function module, by In the virtual machine, the instruction sequence of the functional module is merged in the static analysis stage, so that when the merged instruction sequence is executed, the program execution time can be greatly reduced, and the merged instruction sequence can be cached in the memory to perform repeated functions. , The cached fusion instruction sequence can be directly obtained from the memory, further accelerating the execution speed of the program.
图6示出根据本说明书实施例的一种在虚拟机中执行功能模块的装置600,所述装置包括:获取单元61、确定单元62、合并单元63、执行单元64。FIG. 6 shows an apparatus 600 for executing functional modules in a virtual machine according to an embodiment of the present specification. The apparatus includes: an acquiring unit 61, a determining unit 62, a merging unit 63, and an executing unit 64.
获取单元61,配置为,获取所述功能模块的WASM指令序列。The obtaining unit 61 is configured to obtain the WASM instruction sequence of the functional module.
确定单元62,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果。The determining unit 62 is configured to determine whether a predetermined continuous instruction is included in the WASM instruction sequence, the predetermined continuous instruction is preset with a corresponding combined instruction, and the predetermined continuous instruction and its corresponding combined instruction have the same interpretation and execution result.
合并单元63,配置为,在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令。The merging unit 63 is configured to, in a case where it is determined that the WASM instruction sequence includes at least one group of predetermined consecutive instructions, merge each group of predetermined consecutive instructions in the instruction sequence into corresponding merged instructions, respectively.
执行单元64,配置为,解释执行所述功能模块的经指令合并的WASM指令序列。The execution unit 64 is configured to interpret and execute the combined WASM instruction sequence of the functional module.
在一种实施方式中,所述虚拟机为区块链节点中的虚拟机,所述功能模块为第一合约中定义的第一函数,其中,所述获取单元61还配置为,在执行调用第一合约中的第一函数的第一交易时,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列。In one embodiment, the virtual machine is a virtual machine in a blockchain node, and the function module is a first function defined in a first contract. The acquiring unit 61 is further configured to execute the call During the first transaction of the first function in the first contract, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
在一种实施方式中,所述获取单元61还配置为,从节点本地存储介质中获取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取第一函数的WASM指令序列。In one embodiment, the obtaining unit 61 is further configured to obtain the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and obtain the variable-length-encoded instruction sequence of the first contract Decode and get the WASM instruction sequence of the first function.
在一种实施方式中,所述执行单元64还配置为,基于所述第一交易的数据字段,解释执行所述第一函数的经指令融合的WASM指令序列。In one embodiment, the execution unit 64 is further configured to interpret the instruction-fused WASM instruction sequence for executing the first function based on the data field of the first transaction.
本说明书另一方面提供一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行上述任一项方法。Another aspect of this specification provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed in a computer, the computer is caused to execute any of the above-mentioned methods.
本说明书另一方面提供一种计算设备,包括存储器和处理器,所述存储器中存储有可执行代码,所述处理器执行所述可执行代码时,实现上述任一项方法。Another aspect of this specification provides a computing device, including a memory and a processor, the memory stores executable code, and when the processor executes the executable code, any one of the above methods is implemented.
在根据本说明书实施例的WASM指令融合方案中,通过在虚拟机加载解析WASM代码阶段,将预定连续指令合并为一条预设的新指令,从而减少指令数目,缩短代码执行时间,提高了虚拟机执行效率。另外,通过本说明书实施例的方案,不需要改变智能 合约存入区块链的指令形式,具有更高的灵活性和适用性。In the WASM instruction fusion scheme according to the embodiment of this specification, the predetermined continuous instruction is merged into a preset new instruction by loading and parsing the WASM code in the virtual machine, thereby reducing the number of instructions, shortening the code execution time, and improving the virtual machine effectiveness. In addition, through the solution of the embodiment of this specification, there is no need to change the instruction form of the smart contract stored in the blockchain, which has higher flexibility and applicability.
需要理解,本文中的“第一”,“第二”等描述,仅仅为了描述的简单而对相似概念进行区分,并不具有其他限定作用。It needs to be understood that the descriptions of "first", "second", etc. in this article are only used to distinguish similar concepts for the sake of simplicity of description, and do not have other limiting effects.
本说明书中的各个实施例均采用递进的方式描述,各个实施例之间相同相似的部分互相参见即可,每个实施例重点说明的都是与其他实施例的不同之处。尤其,对于系统实施例而言,由于其基本相似于方法实施例,所以描述的比较简单,相关之处参见方法实施例的部分说明即可。The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referred to each other, and each embodiment focuses on the difference from other embodiments. In particular, as for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and for related parts, please refer to the part of the description of the method embodiment.
上述对本说明书特定实施例进行了描述。其它实施例在所附权利要求书的范围内。在一些情况下,在权利要求书中记载的动作或步骤可以按照不同于实施例中的顺序来执行并且仍然可以实现期望的结果。另外,在附图中描绘的过程不一定要求示出的特定顺序或者连续顺序才能实现期望的结果。在某些实施方式中,多任务处理和并行处理也是可以的或者可能是有利的。The foregoing describes specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims can be performed in a different order than in the embodiments and still achieve desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or sequential order shown in order to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
本领域普通技术人员应该还可以进一步意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、计算机软件或者二者的结合来实现,为了清楚地说明硬件和软件的可互换性,在上述说明中已经按照功能一般性地描述了各示例的组成及步骤。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。本领域普通技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本申请的范围。其中,软件模块可以置于随机存储器(RAM)、内存、只读存储器(ROM)、电可编程ROM、电可擦除可编程ROM、寄存器、硬盘、可移动磁盘、CD-ROM、或技术领域内所公知的任意其它形式的存储介质中。Those of ordinary skill in the art should be further aware that the units and algorithm steps of the examples described in the embodiments disclosed herein can be implemented by electronic hardware, computer software or a combination of the two, in order to clearly illustrate the hardware For the interchangeability with software, the composition and steps of each example have been described generally in terms of function in the above description. Whether these functions are executed by hardware or software depends on the specific application and design constraint conditions of the technical solution. A person of ordinary skill in the art may use different methods for each specific application to implement the described functions, but such implementation should not be considered beyond the scope of the present application. Among them, the software module can be placed in random access memory (RAM), memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, or technical fields Any other form of storage medium known within.
以上所述的具体实施方式,对本发明的目的、技术方案和有益效果进行了进一步详细说明,所应理解的是,以上所述仅为本发明的具体实施方式而已,并不用于限定本发明的保护范围,凡在本发明的精神和原则之内,所做的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。The specific embodiments described above further describe the purpose, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific embodiments of the present invention, and are not intended to limit the scope of the present invention. The scope of protection, any modification, equivalent replacement, improvement, etc., made within the spirit and principle of the present invention shall be included in the scope of protection of the present invention.

Claims (14)

  1. 一种在虚拟机中执行功能模块的方法,所述方法包括:A method for executing functional modules in a virtual machine, the method including:
    获取所述功能模块的WASM指令序列;Acquiring the WASM instruction sequence of the functional module;
    确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令包括连续排列的多个指令,所述预定连续指令被预设有相应的合并指令,所述合并指令为单个指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;Determine whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction includes a plurality of instructions arranged consecutively, the predetermined continuous instruction is preset with a corresponding merge instruction, the merge instruction is a single instruction, and The predetermined continuous instruction and its corresponding combined instruction have the same interpretation and execution result;
    在确定所述WASM指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令;In a case where it is determined that the WASM instruction sequence includes at least one group of predetermined continuous instructions, each group of predetermined continuous instructions in the instruction sequence is respectively merged into corresponding merged instructions;
    解释执行所述功能模块的经指令合并的WASM指令序列。Interpret the combined WASM instruction sequence that executes the function module.
  2. 根据权利要求1所述的方法,其中,所述虚拟机为区块链节点中的虚拟机,所述功能模块为第一合约中定义的第一函数,其中,获取所述功能模块的WASM指令序列包括,在执行调用第一合约中的第一函数的第一交易时,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列。The method according to claim 1, wherein the virtual machine is a virtual machine in a blockchain node, the function module is a first function defined in a first contract, and the WASM instruction of the function module is obtained The sequence includes obtaining the WASM instruction sequence of the first function based on the instruction sequence of the first contract stored in the local storage medium of the node when the first transaction that calls the first function in the first contract is executed.
  3. 根据权利要求2所述的方法,其中,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列包括,从节点本地存储介质中获取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取第一函数的WASM指令序列。The method according to claim 2, wherein, based on the instruction sequence of the first contract stored in the node local storage medium, obtaining the WASM instruction sequence of the first function includes obtaining the first contract from the node local storage medium The variable-length-encoded instruction sequence of the first contract is decoded to obtain the WASM instruction sequence of the first function.
  4. 根据权利要求2所述的方法,其中,解释执行所述功能模块的经指令合并的WASM指令序列包括,基于所述第一交易的数据字段,解释执行所述第一函数的经指令合并的WASM指令序列。The method according to claim 2, wherein interpreting the instruction-merged WASM instruction sequence that executes the function module comprises, based on the data field of the first transaction, interpreting the instruction-merged WASM that executes the first function Sequence of instructions.
  5. 根据权利要求1所述的方法,其中,所述预定连续指令包括至少两个指令,所述预定连续指令中的最后一个指令对应的操作参数与该预定连续指令中在最后一个指令之前的至少一个指令相关联。The method according to claim 1, wherein the predetermined continuous instruction includes at least two instructions, and the operating parameter corresponding to the last instruction in the predetermined continuous instruction is at least one before the last instruction in the predetermined continuous instruction. The instruction is associated.
  6. 根据权利要求5所述的方法,其中,所述最后一个指令包括基本运算指令。The method according to claim 5, wherein the last instruction includes a basic operation instruction.
  7. 一种在虚拟机中执行功能模块的装置,所述装置包括:A device for executing function modules in a virtual machine, the device comprising:
    获取单元,配置为,获取所述功能模块的WASM指令序列;The obtaining unit is configured to obtain the WASM instruction sequence of the functional module;
    确定单元,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令包括连续排列的多个指令,所述预定连续指令被预设有相应的合并指令,所述合并指令为单个指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;The determining unit is configured to determine whether a predetermined continuous instruction is included in the WASM instruction sequence, the predetermined continuous instruction includes a plurality of instructions arranged consecutively, the predetermined continuous instruction is preset with a corresponding merge instruction, and the merge instruction Is a single instruction, and the predetermined consecutive instruction and its corresponding combined instruction have the same interpretation and execution result;
    合并单元,配置为,在确定所述WASM指令序列中包括至少一组预定连续指令的 情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令;The merging unit is configured to, in a case where it is determined that the WASM instruction sequence includes at least one group of predetermined consecutive instructions, merge each group of predetermined consecutive instructions in the instruction sequence into corresponding merged instructions respectively;
    执行单元,配置为,解释执行所述功能模块的经指令合并的WASM指令序列。The execution unit is configured to interpret and execute the combined WASM instruction sequence of the functional module.
  8. 根据权利要求7所述的装置,其中,所述虚拟机为区块链节点中的虚拟机,所述功能模块为第一合约中定义的第一函数,其中,所述获取单元还配置为,在执行调用第一合约中的第一函数的第一交易时,基于所述节点本地存储介质中存储的第一合约的指令序列,获取所述第一函数的WASM指令序列。8. The device according to claim 7, wherein the virtual machine is a virtual machine in a blockchain node, and the function module is a first function defined in a first contract, and wherein the obtaining unit is further configured to: When the first transaction that calls the first function in the first contract is executed, the WASM command sequence of the first function is obtained based on the command sequence of the first contract stored in the local storage medium of the node.
  9. 根据权利要求8所述的装置,其中,所述获取单元还配置为,从节点本地存储介质中获取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取第一函数的WASM指令序列。8. The device according to claim 8, wherein the obtaining unit is further configured to obtain the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and to obtain the variable-length-encoded instruction sequence of the first contract Decode the instruction sequence of the first function to obtain the WASM instruction sequence of the first function.
  10. 根据权利要求8所述的装置,其中,所述执行单元还配置为,基于所述第一交易的数据字段,解释执行所述第一函数的经指令合并的WASM指令序列。8. The device according to claim 8, wherein the execution unit is further configured to interpret the combined WASM instruction sequence for executing the first function based on the data field of the first transaction.
  11. 根据权利要求7所述的装置,其中,所述预定连续指令包括至少两个指令,所述预定连续指令中的最后一个指令对应的操作参数与该预定连续指令中在最后一个指令之前的至少一个指令相关联。8. The apparatus according to claim 7, wherein the predetermined continuous instruction includes at least two instructions, and the operating parameter corresponding to the last instruction in the predetermined continuous instruction is at least one of the operating parameters before the last instruction in the predetermined continuous instruction. The instruction is associated.
  12. 根据权利要求11所述的装置,其中,所述最后一个指令包括基本运算指令。The apparatus according to claim 11, wherein the last instruction includes a basic operation instruction.
  13. 一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行权利要求1-6中任一项的所述的方法。A computer-readable storage medium having a computer program stored thereon, and when the computer program is executed in a computer, the computer is caused to execute the method according to any one of claims 1-6.
  14. 一种计算设备,包括存储器和处理器,所述存储器中存储有可执行代码,所述处理器执行所述可执行代码时,实现权利要求1-6中任一项所述的方法。A computing device includes a memory and a processor, the memory stores executable code, and when the processor executes the executable code, the method according to any one of claims 1 to 6 is implemented.
PCT/CN2020/132824 2020-03-13 2020-11-30 Method and device for executing functional module in virtual machine WO2021179697A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010172888.2A CN111045793A (en) 2020-03-13 2020-03-13 Method and device for executing functional module in virtual machine
CN202010172888.2 2020-03-13

Publications (1)

Publication Number Publication Date
WO2021179697A1 true WO2021179697A1 (en) 2021-09-16

Family

ID=70230818

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/132824 WO2021179697A1 (en) 2020-03-13 2020-11-30 Method and device for executing functional module in virtual machine

Country Status (2)

Country Link
CN (1) CN111045793A (en)
WO (1) WO2021179697A1 (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111045793A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for executing functional module in virtual machine
CN112905472A (en) * 2021-03-04 2021-06-04 黑芝麻智能科技(上海)有限公司 Kernel debugging system and method

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050160415A1 (en) * 2004-01-17 2005-07-21 Samsung Electronics Co., Ltd. Method and system for improving performance of Java virtual machine
CN103365626A (en) * 2012-03-28 2013-10-23 国际商业机器公司 Method and system used for caching optimized internal instructions in loop buffer
CN107844294A (en) * 2017-11-17 2018-03-27 杭州秘猿科技有限公司 A kind of contract of High Availabitity performs method and system
CN109542455A (en) * 2018-11-29 2019-03-29 杭州复杂美科技有限公司 Contract executes method, contract compressing method, equipment and storage medium
CN110675256A (en) * 2019-08-30 2020-01-10 阿里巴巴集团控股有限公司 Method and device for deploying and executing intelligent contracts
CN111045793A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for executing functional module in virtual machine

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104346212A (en) * 2014-10-20 2015-02-11 浪潮电子信息产业股份有限公司 Optimization method of I/O (input/output) performance of virtual machine
CN106503496B (en) * 2016-12-08 2019-03-19 合肥康捷信息科技有限公司 Based on operation code replacement and combined Python shell script anti-reversal method

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050160415A1 (en) * 2004-01-17 2005-07-21 Samsung Electronics Co., Ltd. Method and system for improving performance of Java virtual machine
CN103365626A (en) * 2012-03-28 2013-10-23 国际商业机器公司 Method and system used for caching optimized internal instructions in loop buffer
CN107844294A (en) * 2017-11-17 2018-03-27 杭州秘猿科技有限公司 A kind of contract of High Availabitity performs method and system
CN109542455A (en) * 2018-11-29 2019-03-29 杭州复杂美科技有限公司 Contract executes method, contract compressing method, equipment and storage medium
CN110675256A (en) * 2019-08-30 2020-01-10 阿里巴巴集团控股有限公司 Method and device for deploying and executing intelligent contracts
CN111045793A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for executing functional module in virtual machine

Also Published As

Publication number Publication date
CN111045793A (en) 2020-04-21

Similar Documents

Publication Publication Date Title
CN110675256B (en) Method and device for deploying and executing intelligent contracts
WO2021179809A1 (en) Intelligent contract caching and executing method and apparatus
US11010303B2 (en) Deploying a smart contract
US10877899B2 (en) System and method for data processing
CN110249307A (en) System and method for executing primary contract on block chain
CN110688122B (en) Method and device for compiling and executing intelligent contract
CN108846753B (en) Method and apparatus for processing data
WO2021179697A1 (en) Method and device for executing functional module in virtual machine
JP6506412B2 (en) Application loading method and apparatus
CN110704064B (en) Method and device for compiling and executing intelligent contract
JP2016517562A (en) Instruction to perform pseudo-random number generation operation
WO2021175053A1 (en) Method and apparatus for executing functional module in virtual machine
JP2016517561A (en) Instruction to perform pseudo-random seed operation
WO2018102767A1 (en) Obfuscating source code sent, from a server computer, to a browser on a client computer
CN113516961B (en) Note generation method, related device, storage medium and program product
WO2024045382A1 (en) Implementation of reflective mechanism in blockchain
WO2023024735A1 (en) Compilation method for machine learning algorithm script for privacy protection, and compiler
EP2860656B1 (en) Method for execution by a microprocessor of a polymorphic binary code of a predetermined function
US9747448B2 (en) Cryptographic mechanisms to provide information privacy and integrity
CN114610364A (en) Application program updating method, application program developing method, application program updating device, application program developing device and computer equipment
CN113391813A (en) Program compiling method and device, storage medium and electronic equipment
JP2010140233A (en) Emulation system and emulation method
CN111443906B (en) Application access method and device
CN113778564B (en) Method, equipment and storage medium for efficiently executing EVM intelligent contract
CN113689293B (en) Method and device for determining intelligent contract file in alliance chain

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20924125

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20924125

Country of ref document: EP

Kind code of ref document: A1