WO2021179809A1 - Intelligent contract caching and executing method and apparatus - Google Patents

Intelligent contract caching and executing method and apparatus Download PDF

Info

Publication number
WO2021179809A1
WO2021179809A1 PCT/CN2021/073557 CN2021073557W WO2021179809A1 WO 2021179809 A1 WO2021179809 A1 WO 2021179809A1 CN 2021073557 W CN2021073557 W CN 2021073557W WO 2021179809 A1 WO2021179809 A1 WO 2021179809A1
Authority
WO
WIPO (PCT)
Prior art keywords
contract
instruction sequence
wasm
memory
function
Prior art date
Application number
PCT/CN2021/073557
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 WO2021179809A1 publication Critical patent/WO2021179809A1/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/45533Hypervisors; Virtual machine monitors
    • G06F9/45558Hypervisor-specific management and integration aspects
    • 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/45533Hypervisors; Virtual machine monitors
    • G06F9/45558Hypervisor-specific management and integration aspects
    • G06F2009/45583Memory management, e.g. access or allocation

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 that creates the smart contract, and publish it to the blockchain network, that is, deploy to the blockchain network Of each node.
  • 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 embodiments of this specification aim to provide a more effective solution for caching and executing smart contracts to solve the deficiencies in the prior art.
  • one aspect of this specification provides a smart contract execution method, which is executed when the virtual machine of the blockchain node executes the first transaction, and the first transaction is used to call the first function of the first contract ,
  • the method includes: determining whether the WASM instruction sequence of the first contract is cached in the memory; in the case of determining that the WASM instruction sequence of the first contract is cached in the memory, reading the first contract from the memory The WASM instruction sequence of the contract to obtain the WASM instruction sequence of the first function; the WASM instruction sequence of executing the first function is interpreted based on the data field in the first transaction.
  • the method further includes, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, reading the instruction sequence of the first contract from the local storage medium of the node to Obtain the WASM instruction sequence of the first contract; and cache the WASM instruction sequence of the first contract in a memory in association with the identifier of the first contract.
  • reading the instruction sequence of the first contract from the node local storage medium to obtain the WASM instruction sequence of the first contract includes reading the first contract's instruction sequence from the node local storage medium.
  • the variable-length-encoded instruction sequence is obtained by decoding the variable-length-encoded instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
  • the method further includes, after obtaining the WASM instruction sequence of the first contract, preprocessing the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract, wherein, caching the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory includes: combining the preprocessed WASM instruction sequence of the first contract with the identifier of the first contract Cached in memory associatively.
  • preprocessing the WASM instruction sequence of the first contract includes determining the global variable identification range based on the WASM instruction sequence of the first contract; for each of the WASM instruction sequence of the first contract Instruction, parse whether the instruction is a global variable access instruction; in the case of analyzing that the instruction is a global variable access instruction, check whether the global variable identifier included in the instruction is within the scope of the global variable identifier; In the case where the global variable identifiers included in each global variable access instruction of a contract are all within the scope of the global variable identifiers, the WASM instruction sequence of the first contract that has been checked for global variable out-of-bounds checks is obtained.
  • preprocessing the WASM instruction sequence of the first contract includes determining whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merge instruction, and the The predetermined continuous instruction and its corresponding combined instruction have the same interpretation and execution result; in the case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, the groups of predetermined continuous instructions in the instruction sequence are respectively combined into corresponding ones The instructions are merged, thereby obtaining the fused WASM instruction sequence of the first contract.
  • the method further includes, after associating the WASM instruction sequence of the first contract with the identifier of the first contract and caching in the memory, based on the data field in the first transaction Explain the WASM instruction sequence that executes the first function.
  • caching the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory includes, and storing the storage address of the WASM instruction sequence of the first contract in the memory with the first The hash value of the contract address of the contract is stored in the memory in association.
  • Another aspect of this specification provides a smart contract execution method.
  • the method is executed when a virtual machine of a blockchain node executes a first transaction.
  • the first transaction is used to call a first function of the first contract.
  • the method Including: determining whether the WASM instruction sequence of the first function is cached in the memory; in the case of determining that the WASM instruction sequence of the first function is cached in the memory, reading the WASM instruction of the first function from the memory Sequence; interpret the WASM instruction sequence to execute the first function based on the data field in the first transaction.
  • the method further includes, in a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, reading the instruction sequence of the first contract from the local storage medium of the node , To obtain the WASM instruction sequence of the first function; and cache the WASM instruction sequence of the first function in the memory in association with the identifier of the first function.
  • caching the WASM instruction sequence of the first function in the memory in association with the identifier of the first function includes, and the storage address of the WASM instruction sequence of the first function in the memory and The hash value of the first data is stored in the memory in association, where the first data is obtained based on the contract address of the first contract and the name of the first function.
  • the device includes: a determining unit, configured to determine whether the WASM instruction sequence of the first contract is cached in the memory; a first reading unit, configured to determine that the WASM instruction sequence of the first contract is cached in the memory , Read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function; the first execution unit is configured to interpret and execute the command sequence based on the data field in the first transaction Describe the WASM instruction sequence of the first function.
  • the device further includes a second reading unit configured to read from the node local storage medium in the case that it is determined that the WASM instruction sequence of the first contract is not cached in the memory Fetch the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract; the cache unit is configured to cache the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory .
  • the second reading unit is further configured to read the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and pass the variable-length-encoded instruction sequence of the first contract. To decode the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
  • the device further includes a preprocessing unit configured to, after obtaining the WASM instruction sequence of the first contract, perform preprocessing on the WASM instruction sequence of the first contract to obtain the experience of the first contract.
  • the preprocessed WASM instruction sequence wherein the cache unit is further configured to cache the preprocessed WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory.
  • the preprocessing unit includes a first determining subunit configured to determine the global variable identification range based on the WASM instruction sequence of the first contract; and the parsing subunit is configured to: For each instruction in the WASM instruction sequence of a contract, analyze whether the instruction is a global variable access instruction; the check sub-unit is configured to, in the case that the instruction is analyzed to be a global variable access instruction, check the global variables included in the instruction Whether the variable identifier is within the scope of the global variable identifier; the acquiring subunit is configured to check that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifier To obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
  • the preprocessing unit includes a second determining subunit configured to determine whether the WASM instruction sequence includes a predetermined continuous instruction, and 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 merge sub-unit is configured to, in the case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, combine the instructions in the instruction sequence The groups of predetermined consecutive instructions are respectively merged into corresponding merged instructions, thereby obtaining the instruction-fused WASM instruction sequence of the first contract.
  • the device further includes a second execution unit configured to cache the WASM instruction sequence of the first contract in association with the identification of the first contract in the memory, and based on the The data field in the first transaction interprets the WASM instruction sequence for executing the first function.
  • a second execution unit configured to cache the WASM instruction sequence of the first contract in association with the identification of the first contract in the memory, and based on the The data field in the first transaction interprets the WASM instruction sequence for executing the first function.
  • the cache unit is further configured to store the storage address of the WASM instruction sequence of the first contract in the memory in association with the hash value of the contract address of the first contract in the memory.
  • the device includes: a determining unit, configured to determine whether a WASM instruction sequence of the first function is cached in the memory; a first reading unit, configured to determine a situation in which the WASM instruction sequence of the first function is cached in the memory , Read the WASM instruction sequence of the first function from the memory; the execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction.
  • the device further includes a second reading unit configured to read from the node local storage medium in the case that it is determined that the WASM instruction sequence of the first function is not cached in the memory Fetch the instruction sequence of the first contract to obtain the WASM instruction sequence of the first function; the cache unit is configured to cache the WASM instruction sequence of the first function in association with the identifier of the first function In memory.
  • the cache unit is further configured to store the storage address of the WASM instruction sequence of the first function in the memory in association with the hash value of the first data in the memory, wherein the The first data is obtained based on the contract address of the first contract and the name of the first function.
  • 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 virtual machine caches the acquired contract's instruction sequence into the memory when the transaction is executed, so that subsequent transactions can directly read the contract's instruction sequence from the memory, and There is no need to read the instruction sequence from the node storage medium, and there is no need to repeatedly decode the encoded instruction sequence, and there is no need to repeatedly preprocess the instruction sequence, which shortens the execution time of subsequent transactions and improves system performance.
  • Figure 1 is a schematic diagram of the process of calling SCORE contract in the blockchain network
  • Figure 2 shows a flowchart of a smart contract execution method according to an embodiment of the present specification
  • Figure 3 schematically shows an example of a table that records key-value pairs of contracts and addresses
  • Figure 4 shows a schematic diagram of caching and executing contract SCORE in a node
  • Fig. 5 shows a flowchart of a smart contract caching method according to another embodiment of this specification
  • Fig. 6 shows a flowchart of a smart contract caching method according to another embodiment of this specification
  • Fig. 7 shows a flowchart of a smart contract execution method according to another embodiment of this specification.
  • Fig. 8 shows a smart contract execution device 800 according to an embodiment of the present specification
  • Fig. 9 shows a smart contract execution device 900 according to another 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 create 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 text form.
  • 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 The number shows the binary code):
  • Bob can publish the transaction used to create the SCORE of the contract to any node in the blockchain through his client, so that each node in the blockchain network can obtain the bytecode of the above smart contract, so that the block
  • Each node of the chain network can deploy the smart contract to the local medium.
  • the WASM virtual machine in the process of executing the transaction that calls the contract SCORE, the WASM virtual machine obtains the leb 128-encoded instruction sequence of the contract SCORE from the local storage medium, and the coded instruction The sequence is decoded, and the decoded instruction sequence is cached in the memory for subsequent contract calls. Therefore, in the subsequent contract call, on the one hand, the instruction sequence of the contract SCORE can be directly obtained from the memory, without the need to obtain the instruction sequence from the storage medium, thereby reducing the reading time, and on the other hand, eliminating the need for encoding The process of decoding the instruction sequence, thereby greatly reducing the contract execution time.
  • 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 make contract calls.
  • 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 aforementioned SCORE contract, indicating the smart contract called by the exchange.
  • each node 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.
  • there is no cached WASM instruction sequence of the contract SCORE in the memory of node A so that node A obtains the encoded bytecode sequence of the contract SCORE from the local storage medium, and decodes the bytecode sequence .
  • the decoded instruction sequence is cached in the memory, and the decoded instruction sequence is interpreted and executed, so that subsequent transactions that call the contract can directly execute the decoded instruction sequence of the contract.
  • node D has previously executed other transactions that call SCORE of the contract, so the WASM instruction sequence of the contract SCORE is cached in the local memory, so that node D is executing the transaction of the call contract SCORE At the time, get the instruction sequence of the contract SCORE from the memory, and interpret the instruction sequence of the function calculation in it.
  • FIG. 1 and the corresponding description are only illustrative, and not restrictive.
  • the blockchain network is not limited to a consortium chain network, but may also be a public chain network, and so on.
  • Fig. 2 shows a flowchart of a smart contract execution method according to an embodiment of the present specification. The method is executed by a WASM virtual machine of any node of the blockchain, and includes the following steps.
  • step S202 it is determined whether the WASM instruction sequence of the SCORE contract is cached in the memory.
  • This method is executed by a virtual machine in node A or node D in FIG. 1, for example.
  • the node After receiving the block including the above transaction, the node instantiates a virtual machine relative to transaction 1 to execute transaction 1.
  • the virtual machine In the process of executing transaction 1, the virtual machine needs to obtain the WASM instruction sequence of the contract SCORE to execute the function calculation therein.
  • the association relationship between the cached contract SCORE and the memory address of the WASM instruction sequence of the storage contract SCORE is recorded in the memory.
  • the contract address hash value of the contract SCORE is compared with the memory of the contract SCORE in the form of a table.
  • the storage addresses are linked, so that by querying the table whether the hash value of the contract address of the contract SCORE is stored, it can be determined whether the WASM instruction sequence of the contract SCORE is cached in the memory.
  • the contract address of the SCORE contract can be searched in the memory to determine whether the WASM instruction sequence of the SCORE contract is cached in the memory.
  • Step S204 in a case where it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function.
  • the virtual machine set in node D can directly read the WASM instruction sequence of contract SCORE from the memory.
  • the contract address hash value of the contract SCORE stored in the table is queried, according to the table, the contract SCORE stored in the memory associated with the hash value can be read Store the address, so that the WASM instruction sequence of the contract SCORE can be read from the storage address.
  • the contract address and the contract code can be stored in association with each other in advance, so that the contract address of the contract SCORE can be found after searching
  • the storage address of the WASM instruction sequence of the contract SCORE can be read, so that the WASM instruction sequence of the contract SCORE can be read.
  • the instruction sequence of the function calculation can be read from the instruction sequence. For example, by searching the function name of the function calculation, the WASM of the function calculation can be found in the WASM instruction sequence of the contract SCORE Sequence of instructions.
  • Step S206 Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
  • the instruction sequence included in the function calculation in the contract is interpreted and executed based on the data field in the transaction 1, thereby completing the execution of the transaction 1.
  • Step S208 In the case where it is determined that the WASM instruction sequence of the contract SCORE is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first contract.
  • the pre-deployment can be found from the local storage medium
  • the sequence of instructions for the contract SCORE The node’s storage medium (for example, hard disk) stores the SCORE instruction sequence of the contract in the form of unstructured key-value pairs.
  • the contract SCORE can be found in the medium.
  • the storage address or, similar to the storage in the memory, the medium stores an association table between the contract address hash value of the contract and the storage address of the contract. By searching the contract SCORE hash value of the contract address from the table, the contract SCORE can be found The storage address in the medium, so that the instruction sequence of the contract SCORE can be read from the medium.
  • 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 read from the storage medium.
  • 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 jointly form the target data. Therefore, if the instruction sequence is parsed and 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.
  • step S210 the WASM instruction sequence of the first contract and the identification of the first contract are cached in the memory in association with each other.
  • node A may allocate a starting address for storing the WASM instruction sequence in its memory, so that the virtual machine 1 set in node A stores the instruction sequence from the starting address.
  • the memory and record the contract SCORE corresponding to the starting address in the predetermined table in the memory.
  • 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 starting address.
  • the embodiment of this specification is not limited to this, as long as the identifier that can uniquely identify the contract SCORE is recorded in the predetermined table,
  • the contract address of the contract SCORE can also be recorded in the reservation form to identify the contract SCORE.
  • Fig. 3 schematically shows an example of a table for recording contract identification and address key-value pairs.
  • the upper row of the table records the hash value of the contract address of each contract, and the lower row records the storage starting address of the corresponding contract in the memory.
  • the hash value of the contract address of the contract SCORE is "21c", and the corresponding memory storage address is 300.
  • node A can allocate the starting address for storing the WASM instruction sequence in its memory, so that the virtual machine 1 starts the instruction sequence together with the contract address of the contract SCORE from the starting address Stored in memory. Therefore, when the contract SCORE is subsequently called, the contract SCORE can be found by searching the contract address of the contract SCORE.
  • step S206 can be performed, that is, the virtual machine 1 in node A can interpret and execute the sequence of instructions included in the function calculation in the contract SCORE based on the data field in transaction 1, so as to complete the transaction 1 Implementation.
  • Figure 4 shows a schematic diagram of caching and executing contract SCORE in node A.
  • a memory 41 In node A, a memory 41, a CPU 42 and a storage medium 43 are included.
  • the virtual machine 1 When the transaction 1 in the transaction queue in the memory 41 that calls the function in the SCORE contract is executed, the virtual machine 1 is instantiated by the CPU 42 to execute the transaction 1.
  • the virtual machine 1 first determines whether the WASM instruction sequence of the contract SCORE is cached in the memory 41. When it is determined that the instruction sequence is not cached in the memory 41, the virtual machine 1 obtains the leb 128-encoded instruction of the contract SCORE from the medium 43.
  • the encoded instruction sequence is decoded by leb 128 (not shown), and the decoded instruction sequence is cached in the memory 41, and then the corresponding function in the contract SCORE is interpreted and executed based on the function parameters in the data field of transaction 1.
  • the virtual machine 2 is instantiated by the CPU 42 to execute the transaction 2.
  • the virtual machine 2 first determines whether the WASM instruction sequence of the contract SCORE is cached in the memory 41. When it is determined that the instruction sequence is cached in the memory, the WASM instruction sequence is read and based on the data field of transaction 2.
  • the function parameter executes the WASM instruction sequence of the corresponding function.
  • Figure 5 shows a flow chart of a smart contract caching method according to another embodiment of this specification.
  • the method is executed when a virtual machine of any node in the blockchain executes a transaction for calling a function in the contract SCORE.
  • the method includes Steps S502 to S510.
  • step S502 it is determined whether the WASM instruction sequence of the contract SCORE is cached in the memory.
  • step S504 in the case where it is determined that there is no WASM instruction sequence of the contract SCORE cached in the memory, the instruction sequence of the contract SCORE is read from the local storage medium of the node to obtain the WASM instruction sequence of the contract SCORE.
  • steps S502 and S504 reference may be made to the above description of steps S202 and S204, which will not be described in detail here.
  • Step S506 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.
  • 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.
  • Step S508 In a case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, the groups of predetermined continuous instructions in the instruction sequence are respectively merged into corresponding merged instructions, so as to obtain the final instruction of the contract SCORE. Converged WASM instruction sequence.
  • 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 S510 the WASM instruction sequence of the contract SCORE, which is fused with instructions, is cached in the memory in association with the contract SCORE identifier.
  • step S206 For this step, reference may be made to the above description of step S206, which will not be repeated here.
  • Fig. 6 shows a flowchart of a smart contract caching method according to another embodiment of the present specification.
  • the method is executed when a virtual machine of any node in the blockchain executes a transaction for calling a function in the contract SCORE2.
  • the method includes Steps S602 to S612.
  • Step S602 Determine whether the WASM instruction sequence of the contract SCORE2 is cached in the memory.
  • Step S604 in the case that it is determined that the WASM instruction sequence of the contract SCORE2 is not cached in the memory, read the instruction sequence of the contract SCORE2 from the local storage medium of the node to obtain the WASM instruction sequence of the contract SCORE2.
  • steps S602 and S604 reference may be made to the above description of steps S202 and S204, which will not be described in detail here.
  • Step S606 Determine the global variable identification range based on the WASM instruction sequence of the contract SCORE2.
  • the virtual machine After the virtual machine obtains the instruction sequence, by reading each instruction one by one, it can know how many global variables are included in the instruction sequence, that is, global variables 0 to 7, a total of 8 global variables , So the variable subscripts assigned to these global variables are 0-7 respectively, that is, the global variable identification range is 0-7.
  • Step S608 For each instruction in the WASM instruction sequence of the contract SCORE2, analyze whether the instruction is a global variable access instruction.
  • Global variable access instructions are not limited to global variable read instructions, but also include global variable write instructions, for example, "set_global 1".
  • Step S610 in the case where the instruction is resolved to be a global variable access instruction, check whether the global variable identifier included in the instruction is within the scope of the global variable identifier.
  • variable read command For example, for the global variable read command get_global 0 in the contract SCORE2, it is to check whether the variable subscript "0" is within the range 0-7 of the global variable identification. Obviously, 0 is within the range 0-7.
  • the contract SCORE2 also includes the global variable write instruction "set_global 10". Obviously, the variable subscript "10" in this instruction exceeds the global variable identification range 0-7 obtained by the virtual machine. Therefore, the variable index of the global variable write instruction is out of bounds, that is, the contract SCORE2 includes an incorrect instruction. In this case, the execution of the transaction will be withdrawn.
  • Step S612 when checking that the contract SCORE2 does not include a global variable access instruction, or checking that the global variable identifiers included in each global variable access instruction of the contract SCORE2 are all within the scope of the global variable identifiers
  • the WASM instruction sequence of the contract SCORE2 that has passed the cross-border check is cached in the memory in association with the identifier of the contract SCORE2.
  • the cache of the instruction sequence of the contract SCORE2 that has passed the cross-border check After the execution of the transaction, the cache of the instruction sequence of the contract SCORE2 that has passed the cross-border check, subsequent transactions that call the function in the contract SCORE2 no longer need to read the contract SCORE2 from the storage medium, and there is no need to repeat the leb 128 decoding, there is no need to repeat the out-of-bounds check, which shortens the execution time of subsequent transactions and improves the system performance.
  • the WASM instruction is executed in the interpretation During the sequence, the program execution time can be greatly reduced and the program execution speed can be accelerated.
  • Fig. 7 shows a flow chart of a smart contract execution method according to another embodiment of this specification.
  • the method is executed when the virtual machine of the blockchain node executes the first transaction, and the first transaction is used to call the first contract.
  • the method includes steps S702 to S706.
  • Step S702 Determine whether the WASM instruction sequence of the first function is cached in the memory.
  • the correlation between the function calculation of the cached contract SCORE and the memory storage address of the WASM instruction sequence storing the function calculation is recorded in the memory, for example, the contract address of the contract SCORE and the function calculation are included in the form of a table.
  • the hash value of the data of the function name is associated with the memory storage address of the function calculation.
  • the contract address of the contract SCORE can be searched in the memory to determine whether the WASM instruction sequence of the contract SCORE is cached in the memory, so as to determine whether the WASM instruction sequence of the function calculation is cached.
  • Step S704 in the case where it is determined that the WASM instruction sequence of the first function is cached in the memory, read the WASM instruction sequence of the first function from the memory.
  • the function calculation stored in association with the hash value can be read.
  • the contract address and the contract code can be stored in association with each other in advance, so that the contract address of the contract SCORE can be found after searching
  • the storage address of the WASM instruction sequence of the contract SCORE by searching the name of the function calculation from the storage address, the WASM instruction sequence of the function calculation can be read.
  • Step S706 Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
  • step S206 For this step, reference may be made to the above description of step S206, which will not be described in detail here.
  • Step S708 in the case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM of the first function Sequence of instructions.
  • the WASM instruction sequence of each function in the contract can be obtained.
  • the first contract includes not only the WASM instruction sequence of the above function calculation, but may also include the WASM instruction sequence of the function calculation2.
  • the function calculation is used to calculate the scores of users in the platform, and the function calculation2 is used to calculate the scores of merchants in the platform. They may have different calculation formulas and thus different WASM instruction sequences.
  • part of the transaction calls the function calculation to calculate the user's score, and part of the transaction calls the function calculation2 to calculate the merchant's score. Therefore, after obtaining the WASM instruction sequence of the contract SCORE, the WASM instruction sequence of each function included in it can be obtained to cache these functions in the memory for the transaction to call.
  • Step S710 Cache the WASM instruction sequence of the first function in the memory in association with the identifier of the first function.
  • the storage address of the WASM instruction sequence of the first function in the memory is stored in the memory in association with the hash value of the first data, wherein the first data is based on the first contract's Get the contract address and the name of the first function.
  • the first data is obtained by splicing the contract address of the first contract and the name of the first function together, or calculating the contract address of the first contract and the name of the first function based on a predetermined formula, etc. .
  • the above-mentioned association relationship may be stored in a table similar to the table shown in FIG. 3, the table for example records key-value pairs, the key is, for example, the hash value of the first data, and the value is the start address.
  • the node may store the WASM instruction sequence of the contract SCORE and the contract address of the contract SCORE into the memory starting from the starting address. Therefore, when the function calculation is subsequently called, the WASM instruction sequence of the function calculation can be found by searching the contract address of the contract SCORE and the function name of the function calculation.
  • the contract SCORE includes multiple functions, for example, it also includes the function calculation2.
  • the function calculation2 the same processing as the function calculation is also performed, so that subsequent transactions that call the function calculation2 can be directly read from the memory to the WASM of the function calculation2 Sequence of instructions.
  • Fig. 8 shows a smart contract execution device 800 according to an embodiment of the present specification.
  • the device is deployed when a virtual machine of a blockchain node executes a first transaction, and the first transaction is used to call the first contract of the first contract.
  • a function, the device includes the following units.
  • the determining unit 81 is configured to determine whether the WASM instruction sequence of the first contract is cached in the memory.
  • the first reading unit 82 is configured to, when it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the first The WASM instruction sequence of the function.
  • the first execution unit 83 is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction.
  • the device further includes a second reading unit 84 configured to, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, from the node local storage medium Read the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
  • the cache unit 85 is configured to cache the WASM instruction sequence of the first contract in the memory in association with the identifier of the first contract.
  • the second reading unit 84 is further configured to read the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and pass the variable-length-encoded instruction sequence of the first contract.
  • the encoded instruction sequence is decoded to obtain the WASM instruction sequence of the first contract.
  • the device 800 further includes a preprocessing unit 86 configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the first contract The pre-processed WASM instruction sequence of the first contract, wherein the cache unit 85 is further configured to cache the pre-processed WASM instruction sequence of the first contract in the memory in association with the identifier of the first contract.
  • a preprocessing unit 86 configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the first contract The pre-processed WASM instruction sequence of the first contract, wherein the cache unit 85 is further configured to cache the pre-processed WASM instruction sequence of the first contract in the memory in association with the identifier of the first contract.
  • the preprocessing unit 86 includes a first determining subunit 861 configured to determine a global variable identification range based on the WASM instruction sequence of the first contract; and a parsing subunit 862 configured to Each instruction in the WASM instruction sequence of the first contract analyzes whether the instruction is a global variable access instruction; the checking subunit 863 is configured to check the Whether the global variable identifier included in the instruction is within the scope of the global variable identifier; the obtaining subunit 864 is configured to check that the global variable identifiers included in each global variable access instruction of the first contract are all in the global variable identifier In the case of being within the range, obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
  • the preprocessing unit includes a second determining subunit 865 configured to determine whether the WASM instruction sequence includes a predetermined continuous instruction, and 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 merge subunit 866 is configured to, in the case where it is determined that the instruction sequence includes at least one set of predetermined continuous instructions, combine the instruction sequence The groups of predetermined consecutive instructions in the, respectively, are merged into corresponding merged instructions, so as to obtain the instruction-fused WASM instruction sequence of the first contract.
  • the device 800 further includes a second execution unit 87 configured to, after associating the WASM instruction sequence of the first contract with the identifier of the first contract and caching in the memory, Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
  • a second execution unit 87 configured to, after associating the WASM instruction sequence of the first contract with the identifier of the first contract and caching in the memory, Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
  • the cache unit 85 is further configured to store the storage address of the WASM instruction sequence of the first contract in the memory in association with the hash value of the contract address of the first contract in the memory.
  • FIG. 9 shows a smart contract execution device 900 according to another embodiment of this specification.
  • the device is deployed when a virtual machine of a blockchain node executes a first transaction, and the first transaction is used to call the first contract.
  • the first function the device includes: a determining unit 91 configured to determine whether the WASM instruction sequence of the first function is cached in the memory; the first reading unit 92 is configured to cache the In the case of the WASM instruction sequence of the first function, read the WASM instruction sequence of the first function from the memory; the execution unit 93 is configured to execute the first function based on the interpretation of the data field in the first transaction The WASM instruction sequence.
  • the device further includes a second reading unit 94 configured to, in a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, from the local storage medium of the node Read the instruction sequence of the first contract to obtain the WASM instruction sequence of the first function; the cache unit 95 is configured to associate the WASM instruction sequence of the first function with the identifier of the first function Cached in memory.
  • the caching unit 95 is further configured to store the storage address of the WASM instruction sequence of the first function in the memory and the hash value of the first data in the memory in association with each other.
  • the first data is obtained based on the contract address of the first contract and the name of the first function.
  • 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 virtual machine caches the acquired contract's instruction sequence into the memory when the transaction is executed, so that subsequent transactions can directly read the contract's instruction sequence from the memory, and There is no need to read the instruction sequence from the storage medium, and there is no need to repeatedly decode the encoded instruction sequence. In addition, there is no need to repeatedly preprocess the contract instruction sequence when executing the transaction that calls the contract, which shortens subsequent transactions.
  • the execution time has improved system performance.
  • 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

The embodiments of the present description provide an intelligent contract caching method and apparatus. The method is executed when a virtual machine of a blockchain node executes a first transaction, wherein the first transaction is used for calling a first function of a first contract. The method comprises: determining whether a WASM instruction sequence of the first contract is cached in a memory; when it is determined that the WASM instruction sequence of the first contract is cached in the memory, reading the WASM instruction sequence of the first contract from the memory to obtain a WASM instruction sequence of the first function; and on the basis of a data field in the first transaction, interpreting and executing the WASM instruction sequence of the first function.

Description

缓存和执行智能合约的方法和装置Method and device for caching and executing smart contract 技术领域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 that creates the smart contract, and publish it to the blockchain network, that is, deploy to the blockchain network Of each node. 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虚拟机解释器对合约的执行速度对于整个系统的性能都至关重要。因此,希望能有改进的方案,进一步提高WASM虚拟机对智能合约的执行效率。In the application scenario of executing smart contracts through the WASM virtual machine of each node of the blockchain, the execution speed of the contract by the WASM virtual machine interpreter is very important to the performance of the entire system. Therefore, it is hoped that there will be an improved scheme to further improve the efficiency of the WASM virtual machine's execution of smart contracts.
发明内容Summary of the invention
本说明书实施例旨在提供一种更有效的缓存和执行智能合约的方案,以解决现有技术中的不足。The embodiments of this specification aim to provide a more effective solution for caching and executing smart contracts to solve the deficiencies in the prior art.
为实现上述目的,本说明书一个方面提供一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:确定内存中是否缓存有所述第一合约的WASM指令序列;在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。In order to achieve the above objective, one aspect of this specification provides a smart contract execution method, which is executed when the virtual machine of the blockchain node executes the first transaction, and the first transaction is used to call the first function of the first contract , The method includes: determining whether the WASM instruction sequence of the first contract is cached in the memory; in the case of determining that the WASM instruction sequence of the first contract is cached in the memory, reading the first contract from the memory The WASM instruction sequence of the contract to obtain the WASM instruction sequence of the first function; the WASM instruction sequence of executing the first function is interpreted based on the data field in the first transaction.
在一种实施方式中,所述方法还包括,在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;将所述第一合约的WASM指令序列与所述第一 合约的标识关联地缓存到内存中。In an embodiment, the method further includes, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, reading the instruction sequence of the first contract from the local storage medium of the node to Obtain the WASM instruction sequence of the first contract; and cache the WASM instruction sequence of the first contract in a memory in association with the identifier of the first contract.
在一种实施方式中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列包括,从节点本地存储介质中读取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取所述第一合约的WASM指令序列。In one embodiment, reading the instruction sequence of the first contract from the node local storage medium to obtain the WASM instruction sequence of the first contract includes reading the first contract's instruction sequence from the node local storage medium. The variable-length-encoded instruction sequence is obtained by decoding the variable-length-encoded instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
在一种实施方式中,所述方法还包括,在获取第一合约的WASM指令序列之后,对第一合约的WASM指令序列进行预处理,以获取第一合约的经预处理的WASM指令序列,其中,将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中包括,将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中。In one embodiment, the method further includes, after obtaining the WASM instruction sequence of the first contract, preprocessing the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract, Wherein, caching the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory includes: combining the preprocessed WASM instruction sequence of the first contract with the identifier of the first contract Cached in memory associatively.
在一种实施方式中,对第一合约的WASM指令序列进行预处理包括,基于所述第一合约的WASM指令序列确定全局变量标识范围;对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。In an embodiment, preprocessing the WASM instruction sequence of the first contract includes determining the global variable identification range based on the WASM instruction sequence of the first contract; for each of the WASM instruction sequence of the first contract Instruction, parse whether the instruction is a global variable access instruction; in the case of analyzing that the instruction is a global variable access instruction, check whether the global variable identifier included in the instruction is within the scope of the global variable identifier; In the case where the global variable identifiers included in each global variable access instruction of a contract are all within the scope of the global variable identifiers, the WASM instruction sequence of the first contract that has been checked for global variable out-of-bounds checks is obtained.
在一种实施方式中,对第一合约的WASM指令序列进行预处理包括,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。In one embodiment, preprocessing the WASM instruction sequence of the first contract includes determining whether the WASM instruction sequence includes a predetermined continuous instruction, the predetermined continuous instruction is preset with a corresponding merge instruction, and the The predetermined continuous instruction and its corresponding combined instruction have the same interpretation and execution result; in the case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, the groups of predetermined continuous instructions in the instruction sequence are respectively combined into corresponding ones The instructions are merged, thereby obtaining the fused WASM instruction sequence of the first contract.
在一种实施方式中,所述方法还包括,在将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中之后,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。In one embodiment, the method further includes, after associating the WASM instruction sequence of the first contract with the identifier of the first contract and caching in the memory, based on the data field in the first transaction Explain the WASM instruction sequence that executes the first function.
在一种实施方式中,将所述第一合约的WASM指令序列与第一合约的标识关联地缓存到内存中包括,将所述第一合约的WASM指令序列在内存中的存储地址与第一合约的合约地址的哈希值关联地存储到内存中。In one embodiment, caching the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory includes, and storing the storage address of the WASM instruction sequence of the first contract in the memory with the first The hash value of the contract address of the contract is stored in the memory in association.
本说明书另一方面提供一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:确定内存中是否缓存有所述第一函数的WASM指令序列;在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。Another aspect of this specification provides a smart contract execution method. The method is executed when a virtual machine of a blockchain node executes a first transaction. The first transaction is used to call a first function of the first contract. The method Including: determining whether the WASM instruction sequence of the first function is cached in the memory; in the case of determining that the WASM instruction sequence of the first function is cached in the memory, reading the WASM instruction of the first function from the memory Sequence; interpret the WASM instruction sequence to execute the first function based on the data field in the first transaction.
在一种实施方式中,所述方法还包括,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;将所述第一函数的WASM指令序列与所述第一函数的标识关联地缓存到内存中。In one embodiment, the method further includes, in a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, reading the instruction sequence of the first contract from the local storage medium of the node , To obtain the WASM instruction sequence of the first function; and cache the WASM instruction sequence of the first function in the memory in association with the identifier of the first function.
在一种实施方式中,将所述第一函数的WASM指令序列与所述第一函数的标识关联地缓存到内存中包括,将所述第一函数的WASM指令序列在内存中的存储地址与第 一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。In one embodiment, caching the WASM instruction sequence of the first function in the memory in association with the identifier of the first function includes, and the storage address of the WASM instruction sequence of the first function in the memory and The hash value of the first data is stored in the memory in association, where the first data is obtained based on the contract address of the first contract and the name of the first function.
本说明书另一方面提供一种智能合约执行装置,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:确定单元,配置为,确定内存中是否缓存有所述第一合约的WASM指令序列;第一读取单元,配置为,在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;第一执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。Another aspect of this specification provides a smart contract execution device that is deployed when a virtual machine of a blockchain node executes a first transaction. The first transaction is used to call a first function of the first contract. The device It includes: a determining unit, configured to determine whether the WASM instruction sequence of the first contract is cached in the memory; a first reading unit, configured to determine that the WASM instruction sequence of the first contract is cached in the memory , Read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function; the first execution unit is configured to interpret and execute the command sequence based on the data field in the first transaction Describe the WASM instruction sequence of the first function.
在一种实施方式中,所述装置还包括,第二读取单元,配置为,在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;缓存单元,配置为,将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中。In an embodiment, the device further includes a second reading unit configured to read from the node local storage medium in the case that it is determined that the WASM instruction sequence of the first contract is not cached in the memory Fetch the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract; the cache unit is configured to cache the WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory .
在一种实施方式中,所述第二读取单元还配置为,从节点本地存储介质中读取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取所述第一合约的WASM指令序列。In one embodiment, the second reading unit is further configured to read the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and pass the variable-length-encoded instruction sequence of the first contract. To decode the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
在一种实施方式中,所述装置还包括,预处理单元,配置为,在获取第一合约的WASM指令序列之后,对第一合约的WASM指令序列进行预处理,以获取第一合约的经预处理的WASM指令序列,其中,所述缓存单元还配置为,将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中。In one embodiment, the device further includes a preprocessing unit configured to, after obtaining the WASM instruction sequence of the first contract, perform preprocessing on the WASM instruction sequence of the first contract to obtain the experience of the first contract. The preprocessed WASM instruction sequence, wherein the cache unit is further configured to cache the preprocessed WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory.
在一种实施方式中,所述预处理单元包括,第一确定子单元,配置为,基于所述第一合约的WASM指令序列确定全局变量标识范围;解析子单元,配置为,对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;检查子单元,配置为,在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;获取子单元,配置为,在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。In an embodiment, the preprocessing unit includes a first determining subunit configured to determine the global variable identification range based on the WASM instruction sequence of the first contract; and the parsing subunit is configured to: For each instruction in the WASM instruction sequence of a contract, analyze whether the instruction is a global variable access instruction; the check sub-unit is configured to, in the case that the instruction is analyzed to be a global variable access instruction, check the global variables included in the instruction Whether the variable identifier is within the scope of the global variable identifier; the acquiring subunit is configured to check that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifier To obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
在一种实施方式中,所述预处理单元包括,第二确定子单元,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;合并子单元,配置为,在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。In one embodiment, the preprocessing unit includes a second determining subunit configured to determine whether the WASM instruction sequence includes a predetermined continuous instruction, and 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 merge sub-unit is configured to, in the case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, combine the instructions in the instruction sequence The groups of predetermined consecutive instructions are respectively merged into corresponding merged instructions, thereby obtaining the instruction-fused WASM instruction sequence of the first contract.
在一种实施方式中,所述装置还包括,第二执行单元,配置为,在将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中之后,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。In one embodiment, the device further includes a second execution unit configured to cache the WASM instruction sequence of the first contract in association with the identification of the first contract in the memory, and based on the The data field in the first transaction interprets the WASM instruction sequence for executing the first function.
在一种实施方式中,所述缓存单元还配置为,将所述第一合约的WASM指令序列在内存中的存储地址与第一合约的合约地址的哈希值关联地存储到内存中。In an embodiment, the cache unit is further configured to store the storage address of the WASM instruction sequence of the first contract in the memory in association with the hash value of the contract address of the first contract in the memory.
本说明书另一方面提供一种智能合约执行装置,所述装置在区块链节点的虚拟机 执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:确定单元,配置为,确定内存中是否缓存有所述第一函数的WASM指令序列;第一读取单元,配置为,在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。Another aspect of this specification provides a smart contract execution device that is deployed when a virtual machine of a blockchain node executes a first transaction. The first transaction is used to call a first function of the first contract. The device It includes: a determining unit, configured to determine whether a WASM instruction sequence of the first function is cached in the memory; a first reading unit, configured to determine a situation in which the WASM instruction sequence of the first function is cached in the memory , Read the WASM instruction sequence of the first function from the memory; the execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction.
在一种实施方式中,所述装置还包括,第二读取单元,配置为,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;缓存单元,配置为,将所述第一函数的WASM指令序列与所述第一函数的标识关联地缓存到内存中。In an embodiment, the device further includes a second reading unit configured to read from the node local storage medium in the case that it is determined that the WASM instruction sequence of the first function is not cached in the memory Fetch the instruction sequence of the first contract to obtain the WASM instruction sequence of the first function; the cache unit is configured to cache the WASM instruction sequence of the first function in association with the identifier of the first function In memory.
在一种实施方式中,所述缓存单元还配置为,将所述第一函数的WASM指令序列在内存中的存储地址与第一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。In one embodiment, the cache unit is further configured to store the storage address of the WASM instruction sequence of the first function in the memory in association with the hash value of the first data in the memory, wherein the The first data is obtained based on the contract address of the first contract and the name of the first function.
本说明书另一方面提供一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行上述任一项方法。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.
在根据本说明书实施例的智能合约缓存和执行方案,通过虚拟机在执行交易时将获取的合约的指令序列缓存到内存中,从而后续交易可直接从内存中读取该合约的指令序列,而不需要从节点存储介质中读取该指令序列,也不需要对编码的指令序列重复解码,并且不需要对指令序列重复进行预处理,缩短了后续交易的执行时间,提升了系统性能。In the smart contract caching and execution scheme according to the embodiments of this specification, the virtual machine caches the acquired contract's instruction sequence into the memory when the transaction is executed, so that subsequent transactions can directly read the contract's instruction sequence from the memory, and There is no need to read the instruction sequence from the node storage medium, and there is no need to repeatedly decode the encoded instruction sequence, and there is no need to repeatedly preprocess the instruction sequence, which shortens the execution time of subsequent transactions and improves system performance.
附图说明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的过程示意图;Figure 1 is a schematic diagram of the process of calling SCORE contract in the blockchain network;
图2示出根据本说明书一个实施例的智能合约执行方法流程图;Figure 2 shows a flowchart of a smart contract execution method according to an embodiment of the present specification;
图3示意示出了一种记录合约与地址键值对的表格示例;Figure 3 schematically shows an example of a table that records key-value pairs of contracts and addresses;
图4示出在节点中缓存和执行合约SCORE的示意图;Figure 4 shows a schematic diagram of caching and executing contract SCORE in a node;
图5示出根据本说明书另一实施例的智能合约缓存方法流程图;Fig. 5 shows a flowchart of a smart contract caching method according to another embodiment of this specification;
图6示出根据本说明书另一实施例的智能合约缓存方法流程图;Fig. 6 shows a flowchart of a smart contract caching method according to another embodiment of this specification;
图7示出根据本说明书另一实施例的一种智能合约执行方法流程图;Fig. 7 shows a flowchart of a smart contract execution method according to another embodiment of this specification;
图8示出根据本说明书一个实施例的一种智能合约执行装置800;Fig. 8 shows a smart contract execution device 800 according to an embodiment of the present specification;
图9示出根据本说明书另一实施例的一种智能合约执行装置900。Fig. 9 shows a smart contract execution device 900 according to another 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 create 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, for example, 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:
Figure PCTCN2021073557-appb-000001
Figure PCTCN2021073557-appb-000001
可以理解,为了便于阅读,上述WASM指令序列以文本的形式显示,在实际运行中,WASM指令序列以二进制的格式表示,从而上述function calculation具有如下的形式(这里为了简化表示,以十六进制数示出二进制码):It can be understood that, in order to facilitate reading, the above WASM instruction sequence is displayed in text form. In actual operation, 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 The number shows the binary code):
{20 y{20 y
41 0241 02
6c6c
20 x20 x
6a6a
41 0341 03
6c}6c}
然后,Bob可通过其客户端向区块链中任一节点发布用于创建该合约SCORE的交易,从而区块链网络中的各个节点均可以获取到上述智能合约的字节码,从而区块链网络的各个节点可将该智能合约部署到本地介质中。Then, Bob can publish the transaction used to create the SCORE of the contract to any node in the blockchain through his client, so that each node in the blockchain network can obtain the bytecode of the above smart contract, so that the block Each node of the chain network can deploy the smart contract to the local medium.
在现有技术中,在各个节点部署了合约SCORE之后,在后期每次执行调用合约SCORE的交易时,都要从本地介质中获取合约SCORE的上述指令序列,并顺序执行上述指令序列。在交易1中的指令序列为经leb 128编码的指令序列的情况中,还需要在从介质中获取合约的指令序列之后,先进行leb 128解码,然后再顺序执行合约的指令序列。每次执行调用合约SCORE的交易时从存储介质中读取合约并解码的过程大大增加了交易执行时间。In the prior art, after the contract SCORE is deployed at each node, each time a transaction that calls the contract SCORE is executed later, the above-mentioned instruction sequence of the contract SCORE must be obtained from the local medium, and the above-mentioned instruction sequence must be executed sequentially. In the case that the instruction sequence in transaction 1 is an instruction sequence encoded by leb 128, it is also necessary to perform leb 128 decoding after obtaining the instruction sequence of the contract from the medium, and then execute the instruction sequence of the contract sequentially. The process of reading and decoding the contract from the storage medium every time a transaction that calls the contract SCORE is executed greatly increases the transaction execution time.
为了减少交易执行时间,在本说明书实施例中,在执行调用合约SCORE的交易的过程中,通过WASM虚拟机从本地存储介质中获取合约SCORE的经leb 128编码的指令序列,对该编码的指令序列进行解码,并将该解码的指令序列缓存入内存中,以供后续的合约调用。从而在后续进行合约调用时,一方面,可从内存直接获取合约SCORE的指令序列,而不需要从存储介质中获取指令序列,从而减少了读取时间,另一方面,省去了对编码的指令序列进行解码的过程,从而大大减少合约执行时间。In order to reduce the transaction execution time, in the embodiment of this specification, in the process of executing the transaction that calls the contract SCORE, the WASM virtual machine obtains the leb 128-encoded instruction sequence of the contract SCORE from the local storage medium, and the coded instruction The sequence is decoded, and the decoded instruction sequence is cached in the memory for subsequent contract calls. Therefore, in the subsequent contract call, on the one hand, the instruction sequence of the contract SCORE can be directly obtained from the memory, without the need to obtain the instruction sequence from the storage medium, thereby reducing the reading time, and on the other hand, eliminating the need for encoding The process of decoding the instruction sequence, thereby greatly reducing the contract execution time.
图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 blockchain user Alice 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 make contract calls.
具体而言,在交易1中,from字段可以是调用者Alice的地址,to字段即为上述SCORE合约的合约名称和合约地址,表示该交易所调用的智能合约。在Data字段,包含要调用的合约中的方法或函数名称(calculation)和传入的参数值(Alice的消费金额x=50和销售金额y=100)。Specifically, 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 aforementioned 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).
在Alice将交易1发布到区块链中去之后,各个节点都可以获取交易1,通过WASM虚拟机执行合约SCORE中的函数calculation,从而返回合约执行结果。在一种情况中,例如,节点A内存中没有缓存的合约SCORE的WASM指令序列,从而节点A从本地存储介质中获取合约SCORE的经编码的字节码序列,对该字节码序列进行解码,将经解码的指令序列缓存到内存中,并解释执行该经解码的指令序列,从而使得后续的调用该合约的交易可以直接执行该合约的经解码的指令序列。在另一种情况中,例如,例如节点D在先前已经执行了其它调用合约SCORE的交易,从而本地的内存中缓存有合约SCORE的WASM指令序列,从而,节点D在执行该调用合约SCORE的交易时,从内存中获取合约SCORE的指令序列,并解释执行其中的函数calculation的指令序列。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. In one case, for example, there is no cached WASM instruction sequence of the contract SCORE in the memory of node A, so that node A obtains the encoded bytecode sequence of the contract SCORE from the local storage medium, and decodes the bytecode sequence , The decoded instruction sequence is cached in the memory, and the decoded instruction sequence is interpreted and executed, so that subsequent transactions that call the contract can directly execute the decoded instruction sequence of the contract. In another case, for example, node D has previously executed other transactions that call SCORE of the contract, so the WASM instruction sequence of the contract SCORE is cached in the local memory, so that node D is executing the transaction of the call contract SCORE At the time, get the instruction sequence of the contract SCORE from the memory, and interpret the instruction sequence of the function calculation in it.
可以理解,图1及相应的描述仅仅是示意性的,而不是限制性的,例如,所述区块链网络不限于为联盟链网络,而也可以为公链网络,等等。It can be understood that FIG. 1 and the corresponding description are only illustrative, and not restrictive. For example, the blockchain network is not limited to a consortium chain network, but may also be a public chain network, and so on.
下文将详细描述上述智能合约缓存和执行方案。The following will describe the above smart contract caching and execution scheme in detail.
图2示出根据本说明书一个实施例的智能合约执行方法流程图,所述方法由区块链任一节点的WASM虚拟机执行,包括以下步骤。Fig. 2 shows a flowchart of a smart contract execution method according to an embodiment of the present specification. The method is executed by a WASM virtual machine of any node of the blockchain, and includes the following steps.
步骤S202,确定内存中是否缓存有合约SCORE的WASM指令序列。In step S202, it is determined whether the WASM instruction sequence of the SCORE contract is cached in the memory.
该方法例如由图1中的节点A或节点D中的虚拟机执行。节点在接收到包括上述交易的区块之后,相对于交易1实例化虚拟机来执行交易1。虚拟机在执行交易1的过程中,需要获取到合约SCORE的WASM指令序列,以执行其中的函数calculation。This method is executed by a virtual machine in node A or node D in FIG. 1, for example. After receiving the block including the above transaction, the node instantiates a virtual machine relative to transaction 1 to execute transaction 1. In the process of executing transaction 1, the virtual machine needs to obtain the WASM instruction sequence of the contract SCORE to execute the function calculation therein.
可通过多种方式确定内存中是否缓存有合约SCORE的WASM指令序列。There are many ways to determine whether there is a WASM instruction sequence of the contract SCORE cached in the memory.
在一种实施方式中,在内存中记录有缓存的合约SCORE与存储合约SCORE的WASM指令序列的内存地址的关联关系,例如通过表格的形式将合约SCORE的合约地址哈希值与合约SCORE的内存存储地址关联起来,从而,通过在该表格中查询是否存储有合约SCORE的合约地址哈希值,可确定内存中是否缓存有合约SCORE的WASM指令序列。In one embodiment, the association relationship between the cached contract SCORE and the memory address of the WASM instruction sequence of the storage contract SCORE is recorded in the memory. For example, the contract address hash value of the contract SCORE is compared with the memory of the contract SCORE in the form of a table. The storage addresses are linked, so that by querying the table whether the hash value of the contract address of the contract SCORE is stored, it can be determined whether the WASM instruction sequence of the contract SCORE is cached in the memory.
在一种实施方式中,可通过在内存中搜索所述合约SCORE的合约地址,从而确定内存中是否缓存有合约SCORE的WASM指令序列。In an implementation manner, the contract address of the SCORE contract can be searched in the memory to determine whether the WASM instruction sequence of the SCORE contract is cached in the memory.
步骤S204,在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列。Step S204, in a case where it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function.
例如图1中的节点D在确定内存中缓存有合约SCORE的WASM指令序列的情况中,在节点D中设置的虚拟机可直接从内存读取合约SCORE的WASM指令序列。例如,如在上文中所述,当在所述表格中查询到存储有合约SCORE的合约地址哈希值之后,根据该表格,可读取与该哈希值关联存储的合约SCORE在内存中的存储地址,从而可从该存储地址读取到合约SCORE的WASM指令序列。在另一种实施方式中,当在内存中搜索到合约SCORE的合约地址之后,例如,可预先将合约地址与合约代码关 联地存储在一起,从而在搜索到合约SCORE的合约地址之后,即找到了合约SCORE的WASM指令序列的存储地址,从而可读取合约SCORE的WASM指令序列。在获取到合约SCORE的WASM指令序列之后,可从该指令序列中读取函数calculation的指令序列,例如,通过搜索函数calculation的函数名称,从而可在合约SCORE的WASM指令序列中找到函数calculation的WASM指令序列。For example, in the case where node D in FIG. 1 determines that the WASM instruction sequence of contract SCORE is cached in the memory, the virtual machine set in node D can directly read the WASM instruction sequence of contract SCORE from the memory. For example, as mentioned above, when the contract address hash value of the contract SCORE stored in the table is queried, according to the table, the contract SCORE stored in the memory associated with the hash value can be read Store the address, so that the WASM instruction sequence of the contract SCORE can be read from the storage address. In another embodiment, after the contract address of the contract SCORE is searched in the memory, for example, the contract address and the contract code can be stored in association with each other in advance, so that the contract address of the contract SCORE can be found after searching The storage address of the WASM instruction sequence of the contract SCORE can be read, so that the WASM instruction sequence of the contract SCORE can be read. After obtaining the WASM instruction sequence of the contract SCORE, the instruction sequence of the function calculation can be read from the instruction sequence. For example, by searching the function name of the function calculation, the WASM of the function calculation can be found in the WASM instruction sequence of the contract SCORE Sequence of instructions.
步骤S206,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。Step S206: Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
在通过从内存中读取到函数calculation的WASM指令序列之后,基于交易1中的数据字段解释执行该合约中的函数calculation包括的指令序列,从而完成对交易1的执行。例如,如上文所述,交易1的数据字段中包含要调用的合约中的方法或函数名称(calculation)和传入的参数值(Alice的消费金额x=50和销售金额y=100),从而将x=50和y=100作为传入参数执行函数calculation的WASM指令序列,从而函数返回的值即为分数值。After the WASM instruction sequence of the function calculation is read from the memory, the instruction sequence included in the function calculation in the contract is interpreted and executed based on the data field in the transaction 1, thereby completing the execution of the transaction 1. For example, as mentioned above, the data field of transaction 1 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), thus Use x=50 and y=100 as the incoming parameters to execute the WASM instruction sequence of the function calculation, so that the value returned by the function is the score value.
步骤S208,在确定内存中没有缓存所述合约SCORE的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列。Step S208: In the case where it is determined that the WASM instruction sequence of the contract SCORE is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first contract.
例如,当上文中的节点A执行调用合约SCORE中某个函数的交易(例如交易1)时,当确定内存中没有缓存合约SCORE的WASM指令序列的情况下,可从本地存储介质中找到预先部署的合约SCORE的指令序列。在节点的存储介质(例如硬盘)中以非结构化的键值对的形式存储了合约SCORE的指令序列,例如,可通过搜索所述合约SCORE的合约地址,而找到所述合约SCORE在介质中的存储地址。或者,与内存中的存储类似地,介质中存储有合约的合约地址哈希值与合约的存储地址的关联表格,通过从该表格中搜索合约SCORE的合约地址哈希值,从而可找到合约SCORE在介质中的存储地址,从而可从介质中读取到合约SCORE的指令序列。For example, when the above node A executes a transaction that calls a function in the contract SCORE (for example, transaction 1), when it is determined that there is no WASM instruction sequence of the contract SCORE in the memory, the pre-deployment can be found from the local storage medium The sequence of instructions for the contract SCORE. The node’s storage medium (for example, hard disk) stores the SCORE instruction sequence of the contract in the form of unstructured key-value pairs. For example, by searching the contract address of the contract SCORE, the contract SCORE can be found in the medium. The storage address. Or, similar to the storage in the memory, the medium stores an association table between the contract address hash value of the contract and the storage address of the contract. By searching the contract SCORE hash value of the contract address from the table, the contract SCORE can be found The storage address in the medium, so that the instruction sequence of the contract SCORE can be read from the medium.
在一种实施方式中,在存储介质中存储的合约SCORE的指令序列为WASM指令序列,从而可从存储介质中直接读取合约SCORE的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 read from the storage medium.
在一种实施方式中,在存储介质中存储的合约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指令序列。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 jointly form the target data. Therefore, if the instruction sequence is parsed and 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.
步骤S210,将所述第一合约的WASM指令序列与第一合约的标识关联地缓存到内存中。In step S210, the WASM instruction sequence of the first contract and the identification of the first contract are cached in the memory in association with each other.
在一种实施方式中,节点A可在其内存中分配用于存储所述WASM指令序列的起始地址,从而在节点A中设置的虚拟机1将该指令序列从该起始地址开始存入内存中,并在内存中的预定表格中记录合约SCORE与该起始地址相对应。该表例如记录键值对,键例如为合约SCORE的合约地址哈希值,值为所述起始地址。可以理解,这里虽然描述了在预定表格中记录合约SCORE的合约地址哈希值用于标识合约SCORE,本说明书实施例不限于此,在预定表格中只要记录可唯一标识合约SCORE的标识即可,例如,在预定表格中也可以记录合约SCORE的合约地址用于标识合约SCORE。In an implementation manner, node A may allocate a starting address for storing the WASM instruction sequence in its memory, so that the virtual machine 1 set in node A stores the instruction sequence from the starting address. In the memory, and record the contract SCORE corresponding to the starting address in the predetermined table in the memory. 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 starting address. It is understandable that although it is described here that the hash value of the contract address of the contract SCORE is recorded in the predetermined table to identify the contract SCORE, the embodiment of this specification is not limited to this, as long as the identifier that can uniquely identify the contract SCORE is recorded in the predetermined table, For example, the contract address of the contract SCORE can also be recorded in the reservation form to identify the contract SCORE.
图3示意示出了一种记录合约标识与地址键值对的表格示例。如图3中所示,该表格中上部一行为各个合约的合约地址哈希值,下部一行记录相应合约在内存中的存储起始地址。例如合约SCORE的合约地址哈希值为“21c…”,其对应的内存存储地址为300。Fig. 3 schematically shows an example of a table for recording contract identification and address key-value pairs. As shown in Figure 3, the upper row of the table records the hash value of the contract address of each contract, and the lower row records the storage starting address of the corresponding contract in the memory. For example, the hash value of the contract address of the contract SCORE is "21c...", and the corresponding memory storage address is 300.
从而通过进行该缓存,节点A后续在执行其它调用该合约SCORE的交易时,可从其内存中的所述预定表格中找到合约SCORE的合约地址哈希值“21c…”,从而获取合约SCORE在内存中的存储地址300,通过在内存中访问该地址300,可直接读取到合约SCORE的WASM指令序列,从而加速了合约执行时间。Therefore, by performing this cache, when node A subsequently executes other transactions that call the contract SCORE, it can find the contract address hash value "21c..." of the contract SCORE from the predetermined table in its memory, so as to obtain the contract SCORE status The storage address 300 in the memory, by accessing the address 300 in the memory, the WASM instruction sequence of the contract SCORE can be directly read, thereby speeding up the contract execution time.
在一种实施方式中,节点A可在其内存中分配用于存储所述WASM指令序列的起始地址,从而虚拟机1将该指令序列与合约SCORE的合约地址一起从所述起始地址开始存入内存中。从而在后续调用该合约SCORE时,可通过搜索合约SCORE的合约地址而找到合约SCORE。In one embodiment, node A can allocate the starting address for storing the WASM instruction sequence in its memory, so that the virtual machine 1 starts the instruction sequence together with the contract address of the contract SCORE from the starting address Stored in memory. Therefore, when the contract SCORE is subsequently called, the contract SCORE can be found by searching the contract address of the contract SCORE.
在节点A在进行上述缓存之后,可执行上述步骤S206,即,节点A中的虚拟机1可基于交易1中的数据字段解释执行合约SCORE中的函数calculation包括的指令序列,从而完成对交易1的执行。After node A performs the above-mentioned caching, the above-mentioned step S206 can be performed, that is, the virtual machine 1 in node A can interpret and execute the sequence of instructions included in the function calculation in the contract SCORE based on the data field in transaction 1, so as to complete the transaction 1 Implementation.
图4示出在节点A中缓存和执行合约SCORE的示意图。在节点A中,包括内存41、CPU42和存储介质43。当执行内存41中的交易队列中的调用合约SCORE中函数的交易1时,通过CPU42实例化虚拟机1来执行交易1。虚拟机1首先确定内存41中是否缓存有合约SCORE的WASM指令序列,当确定内存41中未缓存所述指令序列的情况中,虚拟机1从介质43中获取合约SCORE的经leb 128编码的指令序列,对该编码指令序列进行leb 128解码(未示出),并将解码后的指令序列缓存到内存41中,然后基于交易1的数据字段中的函数参数解释执行合约SCORE中的相应函数。在执行了交易1之后,当执行交易队列中的调用合约SCORE中某函数的交易2时,通过CPU42实例化虚拟机2来执行交易2。类似地,虚拟机2首先确定内存41中是否缓存有合约SCORE的WASM指令序列,当确定内存中缓存有所述指令序列的情况中,读取该WASM指令序列,并基于交易2的数据字段中的函数参数执行相应函数的WASM指令序列。从而在执行交易1时进行对合约SCORE的WASM指令序列的缓存之后,后续在执行其它调用合约SCORE中函数的交易时不再需要从介质43中读取合约SCORE,也不需要重复进行leb 128解码,从而缩短了后续交易的执行时间,提升了系统性能。Figure 4 shows a schematic diagram of caching and executing contract SCORE in node A. In node A, a memory 41, a CPU 42 and a storage medium 43 are included. When the transaction 1 in the transaction queue in the memory 41 that calls the function in the SCORE contract is executed, the virtual machine 1 is instantiated by the CPU 42 to execute the transaction 1. The virtual machine 1 first determines whether the WASM instruction sequence of the contract SCORE is cached in the memory 41. When it is determined that the instruction sequence is not cached in the memory 41, the virtual machine 1 obtains the leb 128-encoded instruction of the contract SCORE from the medium 43. Sequence, the encoded instruction sequence is decoded by leb 128 (not shown), and the decoded instruction sequence is cached in the memory 41, and then the corresponding function in the contract SCORE is interpreted and executed based on the function parameters in the data field of transaction 1. After the transaction 1 is executed, when the transaction 2 in the transaction queue that calls a function in the SCORE contract is executed, the virtual machine 2 is instantiated by the CPU 42 to execute the transaction 2. Similarly, the virtual machine 2 first determines whether the WASM instruction sequence of the contract SCORE is cached in the memory 41. When it is determined that the instruction sequence is cached in the memory, the WASM instruction sequence is read and based on the data field of transaction 2. The function parameter executes the WASM instruction sequence of the corresponding function. Therefore, after the WASM instruction sequence of the contract SCORE is cached when transaction 1 is executed, it is no longer necessary to read the contract SCORE from the medium 43 when executing other transactions that call functions in the contract SCORE, and there is no need to repeat leb 128 decoding. , Thereby shortening the execution time of subsequent transactions and improving system performance.
图5示出根据本说明书另一实施例的智能合约缓存方法流程图,所述方法由区块链中任一节点的虚拟机执行用于调用合约SCORE中函数的交易时执行,所述方法包括步骤S502~S510。Figure 5 shows a flow chart of a smart contract caching method according to another embodiment of this specification. The method is executed when a virtual machine of any node in the blockchain executes a transaction for calling a function in the contract SCORE. The method includes Steps S502 to S510.
步骤S502,确定内存中是否缓存有合约SCORE的WASM指令序列。In step S502, it is determined whether the WASM instruction sequence of the contract SCORE is cached in the memory.
步骤S504,在确定内存中没有缓存合约SCORE的WASM指令序列的情况中,从所述节点本地存储介质中读取合约SCORE的指令序列,以获取合约SCORE的WASM指令序列。In step S504, in the case where it is determined that there is no WASM instruction sequence of the contract SCORE cached in the memory, the instruction sequence of the contract SCORE is read from the local storage medium of the node to obtain the WASM instruction sequence of the contract SCORE.
步骤S502和S504可参考上文对步骤S202和S204的描述,在此不再详述。For steps S502 and S504, reference may be made to the above description of steps S202 and S204, which will not be described in detail here.
步骤S506,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果。Step S506: 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.
所述预定连续指令是预先确定的可以进行合并的若干个连续指令,对于这样的预定连续指令,已经预先设置了对应的合并指令,以及与该合并指令相对应的机器码,该机器码在执行之后与其对应的连续指令具有相同的执行结果。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)。As can be seen from the above example, 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.
步骤S508,在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述合约SCORE的经指令融合的WASM指令序列。Step S508: In a case where it is determined that the instruction sequence includes at least one group of predetermined continuous instructions, the groups of predetermined continuous instructions in the instruction sequence are respectively merged into corresponding merged instructions, so as to obtain the final instruction of the contract SCORE. Converged WASM instruction sequence.
例如,在上述步骤S506中,已经确定所述指令序列中包括三组预定连续指令,从而,可将这三组指令分别合并为对应的合并指令,即,经合并之后的WASM指令序列 为:For example, in the above step S506, 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
}。}.
步骤S510,将所述合约SCORE的经指令融合的WASM指令序列与合约SCORE的标识关联地缓存到内存中。In step S510, the WASM instruction sequence of the contract SCORE, which is fused with instructions, is cached in the memory in association with the contract SCORE identifier.
该步骤可参考上文对步骤S206的描述,在此不再赘述。For this step, reference may be made to the above description of step S206, which will not be repeated here.
在执行交易1时进行对合约SCORE的经指令融合的指令序列的缓存之后,后续在执行调用合约SCORE中函数的交易时不再需要从存储介质中读取合约SCORE,也不需要重复进行leb 128解码,也不需要重复进行指令融合,从而缩短了后续交易的执行时间,提升了系统性能,而通过执行指令融合的WASM指令序列,可进一步减少交易的执行时间。After executing transaction 1 to cache the instruction sequence of the contract SCORE that is fused with instructions, it is no longer necessary to read the contract SCORE from the storage medium when executing the transaction that calls the function in the contract SCORE, and there is no need to repeat leb 128 Decoding does not require repeated instruction fusion, thereby shortening the execution time of subsequent transactions and improving system performance. By executing the WASM instruction sequence of instruction fusion, the execution time of the transaction can be further reduced.
图6示出根据本说明书另一实施例的智能合约缓存方法流程图,所述方法由区块链中任一节点的虚拟机执行用于调用合约SCORE2中函数的交易时执行,所述方法包括步骤S602~S612。Fig. 6 shows a flowchart of a smart contract caching method according to another embodiment of the present specification. The method is executed when a virtual machine of any node in the blockchain executes a transaction for calling a function in the contract SCORE2. The method includes Steps S602 to S612.
步骤S602,确定内存中是否缓存有所述合约SCORE2的WASM指令序列。Step S602: Determine whether the WASM instruction sequence of the contract SCORE2 is cached in the memory.
步骤S604,在确定内存中没有缓存所述合约SCORE2的WASM指令序列的情况中,从所述节点本地存储介质中读取合约SCORE2的指令序列,以获取合约SCORE2的WASM指令序列。Step S604, in the case that it is determined that the WASM instruction sequence of the contract SCORE2 is not cached in the memory, read the instruction sequence of the contract SCORE2 from the local storage medium of the node to obtain the WASM instruction sequence of the contract SCORE2.
步骤S602和S604可参考上文对步骤S202和S204的描述,在此不再详述。For steps S602 and S604, reference may be made to the above description of steps S202 and S204, which will not be described in detail here.
假设获取的合约SCORE2的WASM指令序列为:Assume that the WASM instruction sequence of the contract SCORE2 obtained is:
Figure PCTCN2021073557-appb-000002
Figure PCTCN2021073557-appb-000002
步骤S606,基于所述合约SCORE2的WASM指令序列确定全局变量标识范围。Step S606: Determine the global variable identification range based on the WASM instruction sequence of the contract SCORE2.
参考上述合约SCORE2的WASM指令序列,虚拟机在获取与该指令序列之后,通过逐条读取每条指令,可获知该指令序列中共包括多少全局变量,即全局变量0~7,共8个全局变量,从而为这些全局变量分配的变量下标分别为0~7,即全局变量标识范围为0~7。Refer to the WASM instruction sequence of the aforementioned contract SCORE2, after the virtual machine obtains the instruction sequence, by reading each instruction one by one, it can know how many global variables are included in the instruction sequence, that is, global variables 0 to 7, a total of 8 global variables , So the variable subscripts assigned to these global variables are 0-7 respectively, that is, the global variable identification range is 0-7.
步骤S608,对于所述合约SCORE2的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令。Step S608: For each instruction in the WASM instruction sequence of the contract SCORE2, analyze whether the instruction is a global variable access instruction.
例如,当解析到合约SCORE2的第4行的“get_global 0”,可解析出该指令为全局变量读指令。全局变量访问指令不限于为全局变量读指令,还包括全局变量写指令,例如,“set_global 1”。For example, when parsed to "get_global 0" in line 4 of contract SCORE2, it can be parsed that the instruction is a global variable read instruction. Global variable access instructions are not limited to global variable read instructions, but also include global variable write instructions, for example, "set_global 1".
步骤S610,在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内。Step S610, in the case where the instruction is resolved to be a global variable access instruction, check whether the global variable identifier included in the instruction is within the scope of the global variable identifier.
例如,对于合约SCORE2中的全局变量读指令get_global 0,即检查其中的变量下标“0”是否在上述全局变量标识范围0~7之内,显然,0在范围0~7之内。For example, for the global variable read command get_global 0 in the contract SCORE2, it is to check whether the variable subscript "0" is within the range 0-7 of the global variable identification. Obviously, 0 is within the range 0-7.
假设合约SCORE2中除了上述全局变量读指令,还包括全局变量写指令“set_global 10”,很显然,该指令中的变量下标“10”超出了上述虚拟机获取的全局变量标识范围0~7,从而,该全局变量写指令的变量下标是越界的,即,合约SCORE2中包括错误指令。在该情况中,将退出对该交易的执行。Assuming that in addition to the above global variable read instruction, the contract SCORE2 also includes the global variable write instruction "set_global 10". Obviously, the variable subscript "10" in this instruction exceeds the global variable identification range 0-7 obtained by the virtual machine. Therefore, the variable index of the global variable write instruction is out of bounds, that is, the contract SCORE2 includes an incorrect instruction. In this case, the execution of the transaction will be withdrawn.
步骤S612,在检查所述合约SCORE2中不包括全局变量访问指令的情况中、或者在检查所述合约SCORE2的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,将所述合约SCORE2的经越界检查的WASM指令序列与合约SCORE2的标识关联地缓存到内存中。Step S612, when checking that the contract SCORE2 does not include a global variable access instruction, or checking that the global variable identifiers included in each global variable access instruction of the contract SCORE2 are all within the scope of the global variable identifiers In the above, the WASM instruction sequence of the contract SCORE2 that has passed the cross-border check is cached in the memory in association with the identifier of the contract SCORE2.
在执行该交易时进行对合约SCORE2的经越界检查的指令序列的缓存之后,后续在执行其它调用合约SCORE2中函数的交易时不再需要从存储介质中读取合约SCORE2,也不需要重复进行leb 128解码,也不需要重复进行越界检查,从而缩短了后续交易的执行时间,提升了系统性能,而通过在虚拟机中在静态解析阶段对WASM指令序列进行越界检查,从而在解释执行该WASM指令序列时,可大大减少程序执行时间,加快程序执行速度。After the execution of the transaction, the cache of the instruction sequence of the contract SCORE2 that has passed the cross-border check, subsequent transactions that call the function in the contract SCORE2 no longer need to read the contract SCORE2 from the storage medium, and there is no need to repeat the leb 128 decoding, there is no need to repeat the out-of-bounds check, which shortens the execution time of subsequent transactions and improves the system performance. By performing the out-of-bounds check on the WASM instruction sequence in the static analysis stage in the virtual machine, the WASM instruction is executed in the interpretation During the sequence, the program execution time can be greatly reduced and the program execution speed can be accelerated.
图5和图6所示方法仅仅是在缓存合约之前对合约的WASM指令序列进行预处理的两个示例,实际中,在缓存之前可根据具体的场景对合约的WASM指令序列进行多种需要的预处理,在此不一一列出。The methods shown in Figures 5 and 6 are just two examples of preprocessing the WASM instruction sequence of the contract before caching the contract. In practice, before caching, various requirements can be performed on the WASM instruction sequence of the contract according to specific scenarios. The preprocessing is not listed here.
图7示出根据本说明书另一实施例的一种智能合约执行方法流程图,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括步骤S702~S706。Fig. 7 shows a flow chart of a smart contract execution method according to another embodiment of this specification. The method is executed when the virtual machine of the blockchain node executes the first transaction, and the first transaction is used to call the first contract. The method includes steps S702 to S706.
步骤S702,确定内存中是否缓存有所述第一函数的WASM指令序列。Step S702: Determine whether the WASM instruction sequence of the first function is cached in the memory.
在一种实施方式中,在内存中记录有缓存的合约SCORE的函数calculation与存储函数calculation的WASM指令序列的内存存储地址的关联关系,例如通过表格的形式将包括合约SCORE的合约地址和函数calculation的函数名称的数据的哈希值与函数calculation的内存存储地址关联起来,从而,通过在该表格中查询是否存储有与函数calculation对应的哈希值,可确定内存中是否缓存有函数calculation的WASM指令序列。In one embodiment, the correlation between the function calculation of the cached contract SCORE and the memory storage address of the WASM instruction sequence storing the function calculation is recorded in the memory, for example, the contract address of the contract SCORE and the function calculation are included in the form of a table. The hash value of the data of the function name is associated with the memory storage address of the function calculation. Thus, by querying whether the hash value corresponding to the function calculation is stored in the table, it can be determined whether the WASM of the function calculation is cached in the memory Sequence of instructions.
在一种实施方式中,可通过在内存中搜索所述合约SCORE的合约地址,从而确定内存中是否缓存有合约SCORE的WASM指令序列,从而可确定是否缓存有函数calculation的WASM指令序列。In one embodiment, the contract address of the contract SCORE can be searched in the memory to determine whether the WASM instruction sequence of the contract SCORE is cached in the memory, so as to determine whether the WASM instruction sequence of the function calculation is cached.
步骤S704,在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内 存中读取所述第一函数的WASM指令序列。Step S704, in the case where it is determined that the WASM instruction sequence of the first function is cached in the memory, read the WASM instruction sequence of the first function from the memory.
在一种实施方式中,如在上文中所述,当在所述表格中查询到与函数calculation对应的哈希值之后,根据该表格,可读取与该哈希值关联存储的函数calculation在内存中的存储地址,从而可从该存储地址读取到函数calculation的WASM指令序列。In one embodiment, as described above, after the hash value corresponding to the function calculation is queried in the table, according to the table, the function calculation stored in association with the hash value can be read. The storage address in the memory, from which the WASM instruction sequence of the function calculation can be read from the storage address.
在另一种实施方式中,当在内存中搜索到合约SCORE的合约地址之后,例如,可预先将合约地址与合约代码关联地存储在一起,从而在搜索到合约SCORE的合约地址之后,即找到了合约SCORE的WASM指令序列的存储地址,通过从该存储地址开始搜索函数calculation的名称,从而可读取函数calculation的WASM指令序列。In another embodiment, after the contract address of the contract SCORE is searched in the memory, for example, the contract address and the contract code can be stored in association with each other in advance, so that the contract address of the contract SCORE can be found after searching The storage address of the WASM instruction sequence of the contract SCORE, by searching the name of the function calculation from the storage address, the WASM instruction sequence of the function calculation can be read.
步骤S706,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。Step S706: Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
该步骤可参考上文中对步骤S206的描述,在此不再详述。For this step, reference may be made to the above description of step S206, which will not be described in detail here.
步骤S708,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列。Step S708, in the case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM of the first function Sequence of instructions.
从所述节点本地存储介质中读取所述第一合约的指令序列可参考上文中对步骤S208的描述,在此不再详述。在获取到第一合约的指令序列之后,可获取到该合约中的各个函数的WASM指令序列。例如,第一合约中不仅包括上述函数calculation的WASM指令序列,可能还包括函数calculation2的WASM指令序列。例如,函数calculation是用于计算平台中用户的分数,函数calculation2用于计算平台中商户的分数,它们可能具有不同的计算公式,从而具有不同的WASM指令序列。在执行交易的过程中,部分交易调用函数calculation,用于计算用户的分数,部分交易调用函数calculation2,用于计算商户的分数。因此,在获取合约SCORE的WASM指令序列之后,可获取其中包括的各个函数的WASM指令序列,以分别将这些函数缓存到内存中,以供交易进行调用。For reading the instruction sequence of the first contract from the local storage medium of the node, reference may be made to the above description of step S208, which will not be described in detail here. After obtaining the instruction sequence of the first contract, the WASM instruction sequence of each function in the contract can be obtained. For example, the first contract includes not only the WASM instruction sequence of the above function calculation, but may also include the WASM instruction sequence of the function calculation2. For example, the function calculation is used to calculate the scores of users in the platform, and the function calculation2 is used to calculate the scores of merchants in the platform. They may have different calculation formulas and thus different WASM instruction sequences. In the process of executing a transaction, part of the transaction calls the function calculation to calculate the user's score, and part of the transaction calls the function calculation2 to calculate the merchant's score. Therefore, after obtaining the WASM instruction sequence of the contract SCORE, the WASM instruction sequence of each function included in it can be obtained to cache these functions in the memory for the transaction to call.
步骤S710,将所述第一函数的WASM指令序列与所述第一函数的标识关联地缓存到内存中。Step S710: Cache the WASM instruction sequence of the first function in the memory in association with the identifier of the first function.
在一种实施方式中,将所述第一函数的WASM指令序列在内存中的存储地址与第一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。例如,所述第一数据通过将第一合约的合约地址和第一函数的名称拼接到一起获取,或者基于预定公式对第一合约的合约地址和第一函数的名称进行计算而获取,等等。具体地,可通过与图3所示表格类似的表格来存储上述关联关系,该表格例如记录键值对,键例如为第一数据的哈希值,值为所述起始地址。In an embodiment, the storage address of the WASM instruction sequence of the first function in the memory is stored in the memory in association with the hash value of the first data, wherein the first data is based on the first contract's Get the contract address and the name of the first function. For example, the first data is obtained by splicing the contract address of the first contract and the name of the first function together, or calculating the contract address of the first contract and the name of the first function based on a predetermined formula, etc. . Specifically, the above-mentioned association relationship may be stored in a table similar to the table shown in FIG. 3, the table for example records key-value pairs, the key is, for example, the hash value of the first data, and the value is the start address.
在一种实施方式中,节点可将合约SCORE的WASM指令序列与合约SCORE的合约地址一起从所述起始地址开始存入内存中。从而在后续调用函数calculation时,可通过搜索合约SCORE的合约地址和函数calculation的函数名称而找到函数calculation的WASM指令序列。In one embodiment, the node may store the WASM instruction sequence of the contract SCORE and the contract address of the contract SCORE into the memory starting from the starting address. Therefore, when the function calculation is subsequently called, the WASM instruction sequence of the function calculation can be found by searching the contract address of the contract SCORE and the function name of the function calculation.
在合约SCORE包括多个函数的情况中,例如还包括函数calculation2,对于函数calculation2,也进行与函数calculation相同的处理,从而使得后续调用函数calculation2的交易可直接从内存中读取到函数calculation2的WASM指令序列。In the case that the contract SCORE includes multiple functions, for example, it also includes the function calculation2. For the function calculation2, the same processing as the function calculation is also performed, so that subsequent transactions that call the function calculation2 can be directly read from the memory to the WASM of the function calculation2 Sequence of instructions.
图8示出根据本说明书一个实施例的一种智能合约执行装置800,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括以下单元。Fig. 8 shows a smart contract execution device 800 according to an embodiment of the present specification. The device is deployed when a virtual machine of a blockchain node executes a first transaction, and the first transaction is used to call the first contract of the first contract. A function, the device includes the following units.
确定单元81,配置为,确定内存中是否缓存有所述第一合约的WASM指令序列。The determining unit 81 is configured to determine whether the WASM instruction sequence of the first contract is cached in the memory.
第一读取单元82,配置为,在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列。The first reading unit 82 is configured to, when it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the first The WASM instruction sequence of the function.
第一执行单元83,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。The first execution unit 83 is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction.
在一种实施方式中,所述装置还包括,第二读取单元84,配置为,在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列。In an embodiment, the device further includes a second reading unit 84 configured to, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, from the node local storage medium Read the instruction sequence of the first contract to obtain the WASM instruction sequence of the first contract.
缓存单元85,配置为,将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中。The cache unit 85 is configured to cache the WASM instruction sequence of the first contract in the memory in association with the identifier of the first contract.
在一种实施方式中,所述第二读取单元84还配置为,从节点本地存储介质中读取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取所述第一合约的WASM指令序列。In one embodiment, the second reading unit 84 is further configured to read the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and pass the variable-length-encoded instruction sequence of the first contract. The encoded instruction sequence is decoded to obtain the WASM instruction sequence of the first contract.
在一种实施方式中,所述装置800还包括,预处理单元86,配置为,在获取第一合约的WASM指令序列之后,对第一合约的WASM指令序列进行预处理,以获取第一合约的经预处理的WASM指令序列,其中,所述缓存单元85还配置为,将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中。In an embodiment, the device 800 further includes a preprocessing unit 86 configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the first contract The pre-processed WASM instruction sequence of the first contract, wherein the cache unit 85 is further configured to cache the pre-processed WASM instruction sequence of the first contract in the memory in association with the identifier of the first contract.
在一种实施方式中,所述预处理单元86包括,第一确定子单元861,配置为,基于所述第一合约的WASM指令序列确定全局变量标识范围;解析子单元862,配置为,对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;检查子单元863,配置为,在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;获取子单元864,配置为,在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。In one embodiment, the preprocessing unit 86 includes a first determining subunit 861 configured to determine a global variable identification range based on the WASM instruction sequence of the first contract; and a parsing subunit 862 configured to Each instruction in the WASM instruction sequence of the first contract analyzes whether the instruction is a global variable access instruction; the checking subunit 863 is configured to check the Whether the global variable identifier included in the instruction is within the scope of the global variable identifier; the obtaining subunit 864 is configured to check that the global variable identifiers included in each global variable access instruction of the first contract are all in the global variable identifier In the case of being within the range, obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
在一种实施方式中,所述预处理单元包括,第二确定子单元865,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;合并子单元866,配置为,在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。In one embodiment, the preprocessing unit includes a second determining subunit 865 configured to determine whether the WASM instruction sequence includes a predetermined continuous instruction, and 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 merge subunit 866 is configured to, in the case where it is determined that the instruction sequence includes at least one set of predetermined continuous instructions, combine the instruction sequence The groups of predetermined consecutive instructions in the, respectively, are merged into corresponding merged instructions, so as to obtain the instruction-fused WASM instruction sequence of the first contract.
在一种实施方式中,所述装置800还包括,第二执行单元87,配置为,在将所述第一合约的WASM指令序列与所述第一合约的标识关联地缓存到内存中之后,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。In one embodiment, the device 800 further includes a second execution unit 87 configured to, after associating the WASM instruction sequence of the first contract with the identifier of the first contract and caching in the memory, Interpret the WASM instruction sequence for executing the first function based on the data field in the first transaction.
在一种实施方式中,所述缓存单元85还配置为,将所述第一合约的WASM指令 序列在内存中的存储地址与第一合约的合约地址的哈希值关联地存储到内存中。In one embodiment, the cache unit 85 is further configured to store the storage address of the WASM instruction sequence of the first contract in the memory in association with the hash value of the contract address of the first contract in the memory.
图9示出根据本说明书另一实施例的一种智能合约执行装置900,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:确定单元91,配置为,确定内存中是否缓存有所述第一函数的WASM指令序列;第一读取单元92,配置为,在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;执行单元93,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。FIG. 9 shows a smart contract execution device 900 according to another embodiment of this specification. The device is deployed when a virtual machine of a blockchain node executes a first transaction, and the first transaction is used to call the first contract. The first function, the device includes: a determining unit 91 configured to determine whether the WASM instruction sequence of the first function is cached in the memory; the first reading unit 92 is configured to cache the In the case of the WASM instruction sequence of the first function, read the WASM instruction sequence of the first function from the memory; the execution unit 93 is configured to execute the first function based on the interpretation of the data field in the first transaction The WASM instruction sequence.
在一种实施方式中,所述装置还包括,第二读取单元94,配置为,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;缓存单元95,配置为,将所述第一函数的WASM指令序列与所述第一函数的标识关联地缓存到内存中。In one embodiment, the device further includes a second reading unit 94 configured to, in a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, from the local storage medium of the node Read the instruction sequence of the first contract to obtain the WASM instruction sequence of the first function; the cache unit 95 is configured to associate the WASM instruction sequence of the first function with the identifier of the first function Cached in memory.
在一种实施方式中,所述缓存单元95还配置为,将所述第一函数的WASM指令序列在内存中的存储地址与第一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。In one embodiment, the caching unit 95 is further configured to store the storage address of the WASM instruction sequence of the first function in the memory and the hash value of the first data in the memory in association with each other. The first data is obtained based on the contract address of the first contract and the name of the first function.
本说明书另一方面提供一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行上述任一项方法。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.
在根据本说明书实施例的智能合约缓存和执行方案,通过虚拟机在执行交易时将获取的合约的指令序列缓存到内存中,从而后续交易可直接从内存中读取该合约的指令序列,而不需要从存储介质中读取该指令序列,也不需要对编码的指令序列重复解码,另外,也不需要对在执行调用合约的交易时对合约的指令序列重复进行预处理,缩短了后续交易的执行时间,提升了系统性能。In the smart contract caching and execution scheme according to the embodiments of this specification, the virtual machine caches the acquired contract's instruction sequence into the memory when the transaction is executed, so that subsequent transactions can directly read the contract's instruction sequence from the memory, and There is no need to read the instruction sequence from the storage medium, and there is no need to repeatedly decode the encoded instruction sequence. In addition, there is no need to repeatedly preprocess the contract instruction sequence when executing the transaction that calls the contract, which shortens subsequent transactions. The execution time has improved system performance.
需要理解,本文中的“第一”,“第二”等描述,仅仅为了描述的简单而对相似概念进行区分,并不具有其他限定作用。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 can 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 (18)

  1. 一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:A smart contract execution method, the method is executed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the method includes:
    确定内存中是否缓存有所述第一合约的WASM指令序列;Determining whether the WASM instruction sequence of the first contract is cached in the memory;
    在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;In the case of determining that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function; A data field in a transaction interprets the WASM instruction sequence for executing the first function;
    在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一合约的经预处理的WASM指令序列;将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中;In the case of determining that the WASM instruction sequence of the first contract is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first contract; After the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract; The WASM instruction sequence is cached in the memory in association with the identifier of the first contract;
    其中,对所述第一合约的WASM指令序列进行预处理包括:Wherein, preprocessing the WASM instruction sequence of the first contract includes:
    基于所述第一合约的WASM指令序列确定全局变量标识范围;Determining the global variable identification range based on the WASM instruction sequence of the first contract;
    对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;For each instruction in the WASM instruction sequence of the first contract, analyze whether the instruction is a global variable access instruction;
    在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;In the case where the instruction is resolved to be a global variable access instruction, checking whether the global variable identifier included in the instruction is within the scope of the global variable identifier;
    在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。In the case where it is checked that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifiers, obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
  2. 一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:A smart contract execution method, the method is executed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the method includes:
    确定内存中是否缓存有所述第一合约的WASM指令序列;Determining whether the WASM instruction sequence of the first contract is cached in the memory;
    在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;In the case of determining that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the WASM instruction sequence of the first function; A data field in a transaction interprets the WASM instruction sequence for executing the first function;
    在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一合约的经预处理的WASM指令序列;将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中;In the case of determining that the WASM instruction sequence of the first contract is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first contract; After the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract; The WASM instruction sequence is cached in the memory in association with the identifier of the first contract;
    其中,对所述第一合约的WASM指令序列进行预处理包括:Wherein, preprocessing the WASM instruction sequence of the first contract includes:
    确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;Determining 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 execution result;
    在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。In the case where it is determined that the 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, so as to obtain the instruction-fused instruction of the first contract WASM instruction sequence.
  3. 根据权利要求1或2所述的方法,其中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列包括,从节点本地存储介质中读取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取所述第一合约的WASM指令序列。The method according to claim 1 or 2, wherein reading the instruction sequence of the first contract from the node local storage medium to obtain the WASM instruction sequence of the first contract comprises: reading from the node local storage medium Take the variable-length-encoded instruction sequence of the first contract, and obtain the WASM instruction sequence of the first contract by decoding the variable-length-encoded instruction sequence of the first contract.
  4. 根据权利要求1或2所述的方法,还包括,在将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中之后,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。The method according to claim 1 or 2, further comprising, after associating the preprocessed WASM instruction sequence of the first contract with the identification of the first contract and caching in the memory, based on the first The data field in the transaction interprets the WASM instruction sequence for executing the first function.
  5. 根据权利要求1或2所述的方法,其中,将所述第一合约的经预处理的WASM 指令序列与所述第一合约的标识关联地缓存到内存中包括,将所述第一合约的经预处理的WASM指令序列在内存中的存储地址与第一合约的合约地址的哈希值关联地存储到内存中。The method according to claim 1 or 2, wherein the pre-processed WASM instruction sequence of the first contract and the identification of the first contract are cached in memory in association with the method comprising: The storage address of the preprocessed WASM instruction sequence in the memory is stored in the memory in association with the hash value of the contract address of the first contract.
  6. 一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:A smart contract execution method, the method is executed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the method includes:
    确定内存中是否缓存有所述第一函数的WASM指令序列;Determining whether the WASM instruction sequence of the first function is cached in the memory;
    在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;In the case of determining that the WASM instruction sequence of the first function is cached in the memory, read the WASM instruction sequence of the first function from the memory; interpret and execute the first function based on the data field in the first transaction. The WASM instruction sequence of the function;
    在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一函数的经预处理的WASM指令序列;将所述第一函数的经预处理的WASM指令序列与所述第一函数的标识关联地缓存到内存中;In a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first function; After acquiring the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first function; The processed WASM instruction sequence is cached in the memory in association with the identifier of the first function;
    其中,对所述第一合约的WASM指令序列进行预处理包括:Wherein, preprocessing the WASM instruction sequence of the first contract includes:
    基于所述第一合约的WASM指令序列确定全局变量标识范围;Determining the global variable identification range based on the WASM instruction sequence of the first contract;
    对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;For each instruction in the WASM instruction sequence of the first contract, analyze whether the instruction is a global variable access instruction;
    在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;In the case where the instruction is resolved to be a global variable access instruction, checking whether the global variable identifier included in the instruction is within the scope of the global variable identifier;
    在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。In the case where it is checked that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifiers, obtain the WASM instruction sequence of the first contract that has been checked for out-of-bounds global variables.
  7. 一种智能合约执行方法,所述方法由区块链节点的虚拟机执行第一交易时执行,所述第一交易用于调用第一合约的第一函数,所述方法包括:A smart contract execution method, the method is executed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the method includes:
    确定内存中是否缓存有所述第一函数的WASM指令序列;Determining whether the WASM instruction sequence of the first function is cached in the memory;
    在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;In the case of determining that the WASM instruction sequence of the first function is cached in the memory, read the WASM instruction sequence of the first function from the memory; interpret and execute the first function based on the data field in the first transaction. The WASM instruction sequence of the function;
    在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一函数的经预处理的WASM指令序列;将所述第一函数的经预处理的WASM指令序列与所述第一函数的标识关联地缓存到内存中;In a case where it is determined that the WASM instruction sequence of the first function is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the WASM instruction sequence of the first function; After acquiring the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first function; The processed WASM instruction sequence is cached in the memory in association with the identifier of the first function;
    其中,对所述第一合约的WASM指令序列进行预处理包括:Wherein, preprocessing the WASM instruction sequence of the first contract includes:
    确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;Determining 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 execution result;
    在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。In the case where it is determined that the 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, so as to obtain the instruction-fused instruction of the first contract WASM instruction sequence.
  8. 根据权利要求6或7所述的方法,其中,将所述第一函数的经预处理的WASM指令序列与所述第一函数的标识关联地缓存到内存中包括,将所述第一函数的经预处理的WASM指令序列在内存中的存储地址与第一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。The method according to claim 6 or 7, wherein caching the preprocessed WASM instruction sequence of the first function in association with the identifier of the first function in the memory comprises: The storage address of the preprocessed WASM instruction sequence in the memory is stored in the memory in association with the hash value of the first data, where the first data is obtained based on the contract address of the first contract and the name of the first function.
  9. 一种智能合约执行装置,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:A smart contract execution device, the device is deployed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the device includes:
    确定单元,配置为,确定内存中是否缓存有所述第一合约的WASM指令序列;The determining unit is configured to determine whether the WASM instruction sequence of the first contract is cached in the memory;
    第一读取单元,配置为,在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;The first reading unit is configured to, when it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the first function The WASM instruction sequence;
    第一执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;The first execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction;
    第二读取单元,配置为,在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;The second reading unit is configured to, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the first contract. A WASM command sequence of the contract;
    预处理单元,配置为,在获取所述第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取第一合约的经预处理的WASM指令序列;The preprocessing unit is configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract;
    缓存单元,配置为,将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中;A cache unit configured to cache the preprocessed WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory;
    其中,所述预处理单元包括:Wherein, the preprocessing unit includes:
    第一确定子单元,配置为,基于所述第一合约的WASM指令序列确定全局变量标识范围;The first determining subunit is configured to determine the global variable identification range based on the WASM instruction sequence of the first contract;
    解析子单元,配置为,对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;The parsing subunit is configured to, for each instruction in the WASM instruction sequence of the first contract, analyze whether the instruction is a global variable access instruction;
    检查子单元,配置为,在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;The checking subunit is configured to check whether the global variable identifier included in the instruction is within the scope of the global variable identifier when the instruction is parsed as a global variable access instruction;
    获取子单元,配置为,在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。The obtaining sub-unit is configured to obtain the global variable out-of-bounds of the first contract when it is checked that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifiers Check the sequence of WASM instructions.
  10. 一种智能合约执行装置,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:A smart contract execution device, the device is deployed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the device includes:
    确定单元,配置为,确定内存中是否缓存有所述第一合约的WASM指令序列;The determining unit is configured to determine whether the WASM instruction sequence of the first contract is cached in the memory;
    第一读取单元,配置为,在确定内存中缓存了所述第一合约的WASM指令序列的情况中,从内存中读取所述第一合约的WASM指令序列,以获取所述第一函数的WASM指令序列;The first reading unit is configured to, when it is determined that the WASM instruction sequence of the first contract is cached in the memory, read the WASM instruction sequence of the first contract from the memory to obtain the first function The WASM instruction sequence;
    第一执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;The first execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction;
    第二读取单元,配置为,在确定内存中没有缓存所述第一合约的WASM指令序列的情况中,从所述节点本地存储介质中读取第一合约的指令序列,以获取所述第一合约的WASM指令序列;The second reading unit is configured to, in a case where it is determined that the WASM instruction sequence of the first contract is not cached in the memory, read the instruction sequence of the first contract from the local storage medium of the node to obtain the first contract. A WASM command sequence of the contract;
    预处理单元,配置为,在获取所述第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取第一合约的经预处理的WASM指令序列;The preprocessing unit is configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first contract;
    缓存单元,配置为,将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中;A cache unit configured to cache the preprocessed WASM instruction sequence of the first contract in association with the identifier of the first contract in the memory;
    其中,所述预处理单元包括:Wherein, the preprocessing unit includes:
    第二确定子单元,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;The second determining subunit 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 merge instruction, and the predetermined continuous instruction and its corresponding merge instruction have the same Interpret the results of the implementation;
    合并子单元,配置为,在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。The merging subunit is configured to, in a case where it is determined that the instruction sequence includes at least one set of predetermined continuous instructions, merge each set of predetermined continuous instructions in the instruction sequence into corresponding merged instructions, thereby obtaining the first set of continuous instructions. A sequence of WASM instructions fused with instructions for a contract.
  11. 根据权利要求9或10所述的装置,其中,所述第二读取单元还配置为,从节 点本地存储介质中读取第一合约的经可变长编码的指令序列,通过对第一合约的经可变长编码的指令序列进行解码,获取所述第一合约的WASM指令序列。The device according to claim 9 or 10, wherein the second reading unit is further configured to read the variable-length-encoded instruction sequence of the first contract from the local storage medium of the node, and pass the first contract The variable-length-encoded instruction sequence is decoded to obtain the WASM instruction sequence of the first contract.
  12. 根据权利要求9或10所述的装置,还包括,第二执行单元,配置为,在将所述第一合约的经预处理的WASM指令序列与所述第一合约的标识关联地缓存到内存中之后,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列。The device according to claim 9 or 10, further comprising a second execution unit configured to cache the pre-processed WASM instruction sequence of the first contract in association with the identification of the first contract in a memory After processing, the WASM instruction sequence for executing the first function is interpreted based on the data field in the first transaction.
  13. 根据权利要求9或10所述的装置,其中,所述缓存单元还配置为,将所述第一合约的经预处理的WASM指令序列在内存中的存储地址与第一合约的合约地址的哈希值关联地存储到内存中。The device according to claim 9 or 10, wherein the caching unit is further configured to combine the storage address of the pre-processed WASM instruction sequence of the first contract in the memory with the hash of the contract address of the first contract. Hope values are stored in memory associatively.
  14. 一种智能合约执行装置,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:A smart contract execution device, the device is deployed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the device includes:
    确定单元,配置为,确定内存中是否缓存有所述第一函数的WASM指令序列;The determining unit is configured to determine whether the WASM instruction sequence of the first function is cached in the memory;
    第一读取单元,配置为,在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;The first reading unit is configured to read the WASM instruction sequence of the first function from the memory when it is determined that the WASM instruction sequence of the first function is cached in the memory;
    执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;The execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction;
    第二读取单元,配置为,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;The second reading unit is configured to read the instruction sequence of the first contract from the local storage medium of the node in the case where it is determined that the WASM instruction sequence of the first function is not cached in the memory to obtain all The WASM instruction sequence of the first function;
    预处理单元,配置为,在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一函数的经预处理的WASM指令序列;The preprocessing unit is configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first function;
    缓存单元,配置为,将所述第一函数的经预处理的WASM指令序列与所述第一函数的标识关联地缓存到内存中;A cache unit configured to cache the preprocessed WASM instruction sequence of the first function in association with the identifier of the first function in the memory;
    其中,所述预处理单元包括:Wherein, the preprocessing unit includes:
    第一确定子单元,配置为,基于所述第一合约的WASM指令序列确定全局变量标识范围;The first determining subunit is configured to determine the global variable identification range based on the WASM instruction sequence of the first contract;
    解析子单元,配置为,对于所述第一合约的WASM指令序列中的每个指令,解析该指令是否为全局变量访问指令;The parsing subunit is configured to, for each instruction in the WASM instruction sequence of the first contract, analyze whether the instruction is a global variable access instruction;
    检查子单元,配置为,在解析出所述指令为全局变量访问指令的情况中,检查该指令包括的全局变量标识是否在所述全局变量标识范围内;The checking subunit is configured to check whether the global variable identifier included in the instruction is within the scope of the global variable identifier when the instruction is parsed as a global variable access instruction;
    获取子单元,配置为,在检查所述第一合约的各个全局变量访问指令中包括的全局变量标识都在所述全局变量标识范围内的情况中,获取所述第一合约的经全局变量越界检查的WASM指令序列。The obtaining sub-unit is configured to obtain the global variable out-of-bounds of the first contract when it is checked that the global variable identifiers included in each global variable access instruction of the first contract are all within the scope of the global variable identifiers Check the sequence of WASM instructions.
  15. 一种智能合约执行装置,所述装置在区块链节点的虚拟机执行第一交易时部署,所述第一交易用于调用第一合约的第一函数,所述装置包括:A smart contract execution device, the device is deployed when a virtual machine of a blockchain node executes a first transaction, the first transaction is used to call a first function of the first contract, and the device includes:
    确定单元,配置为,确定内存中是否缓存有所述第一函数的WASM指令序列;The determining unit is configured to determine whether the WASM instruction sequence of the first function is cached in the memory;
    第一读取单元,配置为,在确定内存中缓存了所述第一函数的WASM指令序列的情况中,从内存中读取所述第一函数的WASM指令序列;The first reading unit is configured to read the WASM instruction sequence of the first function from the memory when it is determined that the WASM instruction sequence of the first function is cached in the memory;
    执行单元,配置为,基于所述第一交易中的数据字段解释执行所述第一函数的WASM指令序列;The execution unit is configured to interpret and execute the WASM instruction sequence of the first function based on the data field in the first transaction;
    第二读取单元,配置为,在确定内存中没有缓存所述第一函数的WASM指令序列的情况中,从所述节点本地存储介质中读取所述第一合约的指令序列,以获取所述第一函数的WASM指令序列;The second reading unit is configured to read the instruction sequence of the first contract from the local storage medium of the node in the case where it is determined that the WASM instruction sequence of the first function is not cached in the memory to obtain all The WASM instruction sequence of the first function;
    预处理单元,配置为,在获取第一合约的WASM指令序列之后,对所述第一合约的WASM指令序列进行预处理,以获取所述第一函数的经预处理的WASM指令序列;The preprocessing unit is configured to, after obtaining the WASM instruction sequence of the first contract, preprocess the WASM instruction sequence of the first contract to obtain the preprocessed WASM instruction sequence of the first function;
    缓存单元,配置为,将所述第一函数的经预处理的WASM指令序列与所述第一函数的标识关联地缓存到内存中;A cache unit configured to cache the preprocessed WASM instruction sequence of the first function in association with the identifier of the first function in the memory;
    其中,所述预处理单元包括:Wherein, the preprocessing unit includes:
    第二确定子单元,配置为,确定所述WASM指令序列中是否包括预定连续指令,所述预定连续指令被预设有相应的合并指令,并且所述预定连续指令与其对应的合并指令具有相同的解释执行结果;The second determining subunit 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 merge instruction, and the predetermined continuous instruction and its corresponding merge instruction have the same Interpret the results of the implementation;
    合并子单元,配置为,在确定所述指令序列中包括至少一组预定连续指令的情况中,将所述指令序列中的各组预定连续指令分别合并为对应的合并指令,从而获取所述第一合约的经指令融合的WASM指令序列。The merging subunit is configured to, in a case where it is determined that the instruction sequence includes at least one set of predetermined continuous instructions, merge each set of predetermined continuous instructions in the instruction sequence into corresponding merged instructions, thereby obtaining the first set of continuous instructions. A sequence of WASM instructions fused with instructions for a contract.
  16. 根据权利要求14或15所述的装置,其中,所述缓存单元还配置为,将所述第一函数的经预处理的WASM指令序列在内存中的存储地址与第一数据的哈希值关联地存储到内存中,其中,所述第一数据基于第一合约的合约地址和第一函数的名称获取。The device according to claim 14 or 15, wherein the cache unit is further configured to associate the storage address of the preprocessed WASM instruction sequence of the first function in the memory with the hash value of the first data The first data is obtained based on the contract address of the first contract and the name of the first function.
  17. 一种计算机可读存储介质,其上存储有计算机程序,当所述计算机程序在计算机中执行时,令计算机执行权利要求1-8中任一项的所述的方法。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 of any one of claims 1-8.
  18. 一种计算设备,包括存储器和处理器,所述存储器中存储有可执行代码,所述处理器执行所述可执行代码时,实现权利要求1-8中任一项所述的方法。A computing device, comprising a memory and a processor, the memory is stored with executable code, and when the processor executes the executable code, the method according to any one of claims 1-8 is implemented.
PCT/CN2021/073557 2020-03-13 2021-01-25 Intelligent contract caching and executing method and apparatus WO2021179809A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010172887.8A CN111045792A (en) 2020-03-13 2020-03-13 Method and device for caching and executing intelligent contracts
CN202010172887.8 2020-03-13

Publications (1)

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

Family

ID=70231109

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2021/073557 WO2021179809A1 (en) 2020-03-13 2021-01-25 Intelligent contract caching and executing method and apparatus

Country Status (2)

Country Link
CN (1) CN111045792A (en)
WO (1) WO2021179809A1 (en)

Families Citing this family (10)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111045792A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for caching and executing intelligent contracts
CN111381938B (en) * 2020-05-29 2020-09-25 支付宝(杭州)信息技术有限公司 Method and system for executing intelligent contracts in block chain based on basic instruction set
CN111770204B (en) * 2020-08-31 2021-10-22 支付宝(杭州)信息技术有限公司 Method for executing intelligent contract, block chain node and storage medium
CN111768184A (en) 2020-08-31 2020-10-13 支付宝(杭州)信息技术有限公司 Method for executing intelligent contract and block link point
CN111815330A (en) * 2020-08-31 2020-10-23 支付宝(杭州)信息技术有限公司 Method for deploying intelligent contract, block chain node and storage medium
CN111770202B (en) 2020-08-31 2021-10-29 支付宝(杭州)信息技术有限公司 Method for executing intelligent contract, block chain node and storage medium
CN111768183B (en) 2020-08-31 2024-04-19 支付宝(杭州)信息技术有限公司 Method for executing intelligent contract, block chain node and storage medium
CN112001799B (en) * 2020-10-28 2021-02-05 支付宝(杭州)信息技术有限公司 Method and device for executing intelligent contract in block chain
CN112631613A (en) * 2020-12-29 2021-04-09 杭州趣链科技有限公司 Intelligent contract deploying and calling method based on block chain platform and related equipment
CN112380149B (en) * 2021-01-18 2021-04-06 腾讯科技(深圳)有限公司 Data processing method, device, equipment and medium based on node memory

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103365626A (en) * 2012-03-28 2013-10-23 国际商业机器公司 Method and system used for caching optimized internal instructions in loop buffer
CN109949156A (en) * 2019-02-28 2019-06-28 矩阵元技术(深圳)有限公司 A kind of data processing method and server of block chain contract
CN110675256A (en) * 2019-08-30 2020-01-10 阿里巴巴集团控股有限公司 Method and device for deploying and executing intelligent contracts
CN110704063A (en) * 2019-09-30 2020-01-17 支付宝(杭州)信息技术有限公司 Method and device for compiling and executing intelligent contract
CN111045792A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for caching and executing intelligent contracts

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110647542B (en) * 2018-06-11 2022-07-19 北京神州泰岳软件股份有限公司 Data acquisition method and device

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103365626A (en) * 2012-03-28 2013-10-23 国际商业机器公司 Method and system used for caching optimized internal instructions in loop buffer
CN109949156A (en) * 2019-02-28 2019-06-28 矩阵元技术(深圳)有限公司 A kind of data processing method and server of block chain contract
CN110675256A (en) * 2019-08-30 2020-01-10 阿里巴巴集团控股有限公司 Method and device for deploying and executing intelligent contracts
CN110704063A (en) * 2019-09-30 2020-01-17 支付宝(杭州)信息技术有限公司 Method and device for compiling and executing intelligent contract
CN111045792A (en) * 2020-03-13 2020-04-21 支付宝(杭州)信息技术有限公司 Method and device for caching and executing intelligent contracts

Also Published As

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

Similar Documents

Publication Publication Date Title
WO2021179809A1 (en) Intelligent contract caching and executing method and apparatus
WO2021036174A1 (en) Method and device for deploying and executing intelligent contract
US11307990B2 (en) Deploying a smart contract
CN108846753B (en) Method and apparatus for processing data
WO2021175053A1 (en) Method and apparatus for executing functional module in virtual machine
US11216701B1 (en) Unsupervised representation learning for structured records
CN111249736B (en) Code processing method and device
WO2022153722A1 (en) Systems and methods for semi-supervised extraction of text classification information
CN104798075A (en) Application randomization
US20230367815A1 (en) Energy-efficient hardware-software collaborative method and apparatus for graph processing
WO2021179697A1 (en) Method and device for executing functional module in virtual machine
KR20080021071A (en) Translating expressions in a computing environment
US20220261926A1 (en) Method and system for credit card holder identification
CN113961768B (en) Sensitive word detection method and device, computer equipment and storage medium
CN114328632A (en) User data analysis method and device based on bitmap and computer equipment
CN116415564B (en) Functional point amplification method and system based on knowledge graph
US20220215170A1 (en) Framework for chinese text error identification and correction
US11341190B2 (en) Name matching using enhanced name keys
CN114610364A (en) Application program updating method, application program developing method, application program updating device, application program developing device and computer equipment
US11163953B2 (en) Natural language processing and candidate response evaluation
JP2023525814A (en) User search category predictor
JP7175148B2 (en) Judgment device and judgment method
CN111639260A (en) Content recommendation method, device and storage medium thereof
CN111443906B (en) Application access method and device
CN117081727B (en) Weak password detection method and device

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: 21767412

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: 21767412

Country of ref document: EP

Kind code of ref document: A1