Method and device for smart contract calling ledger, electronic equipment and storage medium
By saving and jumping instruction addresses, the interaction between smart contracts and blockchain ledgers is realized, which solves the problem that blockchain platforms using memory management mechanism languages cannot call ledger operations and realizes effective ledger operations.
Patent Information
- Application Number
- CN202211551574.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-05
- Publication Date
- 2025-10-10
- Estimated Expiration
- 2042-12-05
AI Technical Summary
In the prior art, blockchain platforms written in languages that include memory management mechanisms cannot implement smart contract operations on blockchain ledgers through CALL machine instructions.
By saving the address of the first instruction after the ledger call instruction in the smart contract machine code, executing the preset ledger operation instructions in the blockchain platform machine code, and finally jumping back to the smart contract machine code, the interaction between the smart contract and the blockchain ledger is realized.
It enables smart contracts to effectively call blockchain ledgers on blockchain platforms written in languages that include memory management mechanisms, avoiding the limitations of the CALL machine instruction.
Smart Images

Figure CN116155934B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of blockchain technology, and in particular to a method, device, electronic device, and storage medium for calling an account book through a smart contract. Background Art
[0002] During the execution of smart contracts, operations on the blockchain ledger are often required. The blockchain ledger is part of the blockchain platform and contains the blockchain's state. Typically, the blockchain platform encapsulates certain ledger operation methods for smart contracts to call. These ledger operation methods ultimately become part of the blockchain platform's machine code in the form of ledger operation instructions.
[0003] Currently, for smart contracts compiled into machine code that need to interact with blockchain ledgers, if the blockchain platform is written in a language like C++, the CALL machine instruction can be used to call ledger operation methods. The CALL machine instruction is a type of instruction in the machine code instruction set, such as the CALL instruction in the x86-64 instruction set architecture. It saves the current program information on the stack and jumps to the calling program specified by the target operand for execution.
[0004] However, if the blockchain platform is written in a language with memory management mechanisms (such as Golang), the limitations of this memory management mechanism make it impossible to use the CALL machine instruction to call ledger operation methods. Therefore, it is currently impossible to call ledger operation methods from smart contracts on blockchain platforms written in languages with memory management mechanisms. Summary of the Invention
[0005] In view of this, the embodiments of the present application provide a method, device, electronic device and storage medium for a smart contract to call an account book, which can implement the call of the smart contract to the account book operation method even for a blockchain platform written in a language that includes a memory management mechanism.
[0006] A first aspect of an embodiment of the present application provides a method for a smart contract to call an account book, comprising:
[0007] Call and execute the compiled smart contract machine code;
[0008] When executing the ledger call instruction in the smart contract machine code, save the address of the first instruction after the ledger call instruction in the smart contract machine code, and return to execute the blockchain platform machine code;
[0009] Call and execute the account book operation instructions preset in the blockchain platform machine code;
[0010] After executing the account book operation instruction, jump to the first instruction after the account book call instruction in the smart contract machine code according to the address.
[0011] In an embodiment of the present application, the compiled smart contract machine code is first called and executed. When the ledger call instruction in the smart contract machine code is executed, the address of the first instruction after the ledger call instruction is saved, and the execution of the blockchain platform machine code is returned. Then, the ledger operation instruction preset in the blockchain platform machine code is called and executed, that is, the ledger operation method is called to implement the interaction between the smart contract and the blockchain ledger. Finally, after the ledger operation instruction is executed, the first instruction after the ledger call instruction in the smart contract machine code can be jumped to according to the previously saved address, thereby continuing to complete the call of the smart contract. The above process does not use the CALL machine instruction. Instead, it uses the saved instruction address to achieve the purpose of switching from the smart contract machine code to the blockchain platform machine code and re-entering the smart contract machine code from the blockchain platform machine code. In other words, the call of the ledger operation method by the smart contract is implemented by calling the switching machine code. This method is suitable for blockchain platforms written in languages with memory management mechanisms.
[0012] In one implementation of the embodiment of the present application, when executing the ledger call instruction in the smart contract machine code, the following steps may also be included:
[0013] Set the call state quantity to the specified value;
[0014] The calling and executing of the account book operation instructions preset in the blockchain platform machine code may include:
[0015] If it is detected that the call status amount is the specified value, determining that the call return status of the smart contract machine code is a ledger call;
[0016] If the call return status of the smart contract machine code is a ledger call, the ledger operation instruction preset in the blockchain platform machine code is called and executed.
[0017] In one implementation of the embodiment of the present application, calling and executing the compiled smart contract machine code may include:
[0018] When the smart contract call instruction in the blockchain platform machine code is executed, the smart contract machine code is called and executed.
[0019] Furthermore, after jumping to the first instruction after the account call instruction in the smart contract machine code according to the address, the method may further include:
[0020] After executing the last instruction of the smart contract machine code, return to execute the first instruction after the smart contract call instruction in the blockchain platform machine code.
[0021] In one implementation of the embodiment of the present application, before calling and executing the compiled smart contract machine code, the following steps may also be included:
[0022] Get the bytecode of the smart contract;
[0023] Detecting target instructions for operating the ledger in the bytecode;
[0024] The target instruction is replaced with a specified instruction to obtain an updated bytecode; wherein the processing logic of the specified instruction includes saving the address of the first instruction after the specified instruction, setting a call state and a subroutine return;
[0025] The updated bytecode is compiled to obtain the smart contract machine code; wherein the ledger call instruction in the smart contract machine code is obtained by compiling the specified instruction.
[0026] In another implementation of the embodiment of the present application, before calling and executing the compiled smart contract machine code, the following steps may also be included:
[0027] Get the bytecode of the smart contract;
[0028] Detecting target instructions for operating the ledger in the bytecode;
[0029] In the process of compiling the bytecode into the smart contract machine code, the target instruction is compiled into the ledger call instruction in the smart contract machine code; wherein the processing logic of the ledger call instruction includes saving the address of the first instruction after the ledger call instruction, setting the call state, and returning from the subroutine.
[0030] In one implementation of the embodiment of the present application, saving the address of the first instruction after the ledger call instruction in the smart contract machine code may include:
[0031] Storing the address in memory space;
[0032] Before jumping to the first instruction after the account call instruction in the smart contract machine code according to the address, the method may further include:
[0033] The address is read from the memory space.
[0034] A second aspect of an embodiment of the present application provides a device for a smart contract to call an account book, including:
[0035] The contract machine code calling module is used to call and execute the compiled smart contract machine code;
[0036] An address saving module is configured to save the address of the first instruction after the ledger call instruction in the smart contract machine code when the ledger call instruction in the smart contract machine code is executed, and return to execute the blockchain platform machine code;
[0037] An account book operation instruction calling module, used to call and execute the account book operation instructions preset in the blockchain platform machine code;
[0038] An instruction jump module is used to jump to the first instruction after the ledger call instruction in the smart contract machine code according to the address after executing the ledger operation instruction.
[0039] A third aspect of an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the method for calling a ledger through a smart contract as provided in the first aspect of the embodiment of the present application is implemented.
[0040] A fourth aspect of an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the method for a smart contract to call an account book as provided in the first aspect of the embodiment of the present application.
[0041] A fifth aspect of the embodiments of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the method for calling an account book by a smart contract provided in the first aspect of the embodiments of the present application.
[0042] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments or descriptions of the prior art. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0044] Figure 1 This is a flowchart of a method for a smart contract to call an account book provided by an embodiment of the present application;
[0045] Figure 2This is a schematic diagram of the operation of the method for calling a ledger by a smart contract provided in an embodiment of the present application in an actual application scenario;
[0046] Figure 3 This is a schematic diagram of the structure of a device for calling a ledger by a smart contract provided in an embodiment of the present application;
[0047] Figure 4 This is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0048] In the following description, for the purpose of illustration rather than limitation, specific details such as specific system structures and technologies are provided to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application can also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid obstructing the description of the present application with unnecessary details. In addition, in the description of the present application specification and the appended claims, the terms "first," "second," "third," etc. are only used to distinguish descriptions and are not to be understood as indicating or implying relative importance.
[0049] In the blockchain technology field, smart contracts are typically compiled into machine code to improve execution efficiency. When a smart contract compiled into machine code needs to interact with a blockchain ledger, it can typically use the CALL machine instruction to invoke ledger operations. However, if the blockchain platform is written in a language with memory management mechanisms (such as Golang), the limitations of this memory management mechanism (i.e., garbage collection limitations) make it impossible to use the CALL machine instruction to invoke ledger operations.
[0050] In view of this, embodiments of the present application provide a method, apparatus, electronic device, and storage medium for enabling smart contracts to call ledger operations. These methods implement the invocation of ledger operation methods by smart contracts by switching machine code rather than using the CALL machine instruction. This method is also applicable to blockchain platforms written in languages that include memory management mechanisms. For more specific technical implementation details of the embodiments of the present application, please refer to the method embodiments described below.
[0051] See also Figure 1 , shows a method for a smart contract to call an account book provided by an embodiment of the present application, including:
[0052] 101. Call and execute the compiled smart contract machine code;
[0053] It should be understood that the execution entity of the embodiment of this method is any blockchain node in the blockchain system.
[0054] First, the smart contract is compiled into smart contract machine code, which is then called and executed. This starts with the first instruction in the smart contract machine code's instruction list and continues with each instruction. Additionally, the smart contract machine code includes ledger call instructions for operating the blockchain ledger. For information on how to generate these ledger call instructions, see below.
[0055] In one implementation of the embodiment of the present application, before calling and executing the compiled smart contract machine code, the following steps may also be included:
[0056] (1) Obtain the bytecode of the smart contract;
[0057] (2) detecting target instructions for operating the ledger in the bytecode;
[0058] (3) replacing the target instruction with a specified instruction to obtain an updated bytecode; wherein the processing logic of the specified instruction includes saving the address of the first instruction after the specified instruction, setting a call state, and returning from the subroutine;
[0059] (4) Compiling the updated bytecode to obtain the smart contract machine code; wherein the ledger call instruction in the smart contract machine code is obtained by compiling the specified instruction.
[0060] Before implementing the embodiment of this method, the smart contract needs to be compiled into machine code. First, the bytecode of the smart contract is obtained, and the target instructions for operating the ledger in the bytecode are detected. The target instructions here can be understood as the code segment that implements the processing logic of operating the ledger; after finding the target instructions in the bytecode, the target instructions are replaced with designated instructions with special processing logic. The special processing logic here includes: saving the address of the first instruction after the instruction, setting the call state and the subroutine return (for example, using the RET machine instruction); finally, the bytecode after replacing the target instructions (that is, the updated bytecode) is compiled to obtain the smart contract machine code. The designated instructions in the bytecode become the ledger call instructions in the smart contract machine code after compilation. When compiling the bytecode, the code segment (that is, the target instruction) that needs to call the ledger in all the method bodies of the bytecode can be parsed, and then the code segment is replaced with the designated instructions of the above-mentioned special processing logic.
[0061] In another implementation of the embodiment of the present application, before calling and executing the compiled smart contract machine code, the following steps may also be included:
[0062] (1) Obtain the bytecode of the smart contract;
[0063] (2) detecting target instructions for operating the ledger in the bytecode;
[0064] (3) In the process of compiling the bytecode into the smart contract machine code, compiling the target instruction into the ledger call instruction in the smart contract machine code; wherein the processing logic of the ledger call instruction includes saving the address of the first instruction after the ledger call instruction, setting the call state, and returning from the subroutine.
[0065] Another implementation method for compiling smart contracts into machine code begins by obtaining the smart contract's bytecode and then detecting the target instructions within the bytecode for operating the ledger. Next, rather than replacing the target instructions within the bytecode, the target instructions are directly compiled into multiple machine instructions with specialized processing logic during the compilation process. These instructions become the ledger call instructions within the smart contract's machine code. This specialized processing logic also includes saving the address of the first instruction after the instruction, setting the call state, and returning from the subroutine.
[0066] Furthermore, detecting the target instruction for operating the ledger in the bytecode may include:
[0067] For any instruction in the bytecode, if it is detected that the any instruction contains a key name related to the operation ledger, the any instruction is determined as the target instruction.
[0068] In the bytecode of a smart contract, instructions for operating the ledger often contain certain key names related to these operations. Therefore, this characteristic can be exploited to detect target instructions from the bytecode. Specifically, any instruction in the bytecode that contains a key name related to operating the ledger can be identified as a target instruction. For example, a method that writes a state value to the ledger contains the key name "SetState," so any instruction in the bytecode containing the key name "SetState" can be identified as a target instruction.
[0069] In one implementation of the embodiment of the present application, calling and executing the compiled smart contract machine code may include:
[0070] When the smart contract call instruction in the blockchain platform machine code is executed, the smart contract machine code is called and executed.
[0071] Blockchain nodes typically execute the blockchain platform's machine code. When they encounter a smart contract call instruction within that blockchain platform's machine code, they locate the corresponding smart contract and begin calling and executing the corresponding smart contract's machine code. Furthermore, the blockchain platform encapsulates certain ledger operation methods for smart contracts to call. These ledger operation methods ultimately become part of the blockchain platform's machine code in the form of ledger operation instructions.
[0072] 102. When executing the ledger call instruction in the smart contract machine code, save the address of the first instruction after the ledger call instruction in the smart contract machine code, and return to execute the blockchain platform machine code;
[0073] During the execution of the smart contract machine code, if a ledger call instruction is executed, the special processing logic corresponding to the ledger call instruction will be executed (see the previous article). At this time, the address of the first instruction after the ledger call instruction (i.e. the next instruction) in the smart contract machine code will be saved, and then the execution will be switched back to the blockchain platform machine code.
[0074] In one implementation of the embodiment of the present application, saving the address of the first instruction after the ledger call instruction in the smart contract machine code may include:
[0075] The address is stored in the memory space.
[0076] When saving the address of the first instruction after the ledger call instruction in the smart contract machine code, in order to improve data access efficiency, the address can be saved to the memory space of the blockchain node, such as the Golang memory space.
[0077] In one implementation of the embodiment of the present application, when executing the ledger call instruction in the smart contract machine code, the following steps may also be included:
[0078] Sets the call state to the specified value.
[0079] The special processing logic described above also includes setting a call status variable. Therefore, when executing a ledger call instruction in the smart contract machine code, a call status variable can also be set to a specified value. This call status variable is primarily used by the blockchain node to determine the call return status of the smart contract machine code. If the call return status is a ledger call, the ledger operation can be performed; conversely, if the call return status is not a ledger call, such as when the smart contract execution is completed or an error occurs, other corresponding operations can be performed. For example, the correspondence between the value of the call status variable and the call return status can be preset, such as 0 corresponding to a ledger call, 1 corresponding to operating other objects on the blockchain, 2 corresponding to the completion of smart contract execution, etc. In this case, when executing the ledger call instruction in the smart contract machine code, the call status variable will be set to 0.
[0080] 103. Call and execute the account book operation instructions preset in the blockchain platform machine code;
[0081] After returning to the blockchain platform machine code, the pre-configured ledger operation instructions in the blockchain platform machine code can be called and executed. This means calling the pre-packaged ledger operation methods on the blockchain platform to operate on the blockchain ledger, thereby realizing the interaction between the smart contract and the blockchain ledger. When returning to the blockchain platform machine code, the ledger operation instructions can be called directly. However, switching from the smart contract machine code back to the blockchain platform machine code does not necessarily mean that ledger operations need to be performed (for example, when the smart contract machine code is fully executed, the blockchain platform machine code will also be returned to execution). To improve the accuracy of the logical processing, a step can be added to determine the call return status, which requires the use of the call status quantity described above.
[0082] In one implementation of the embodiment of the present application, calling and executing the account operation instructions preset in the blockchain platform machine code may include:
[0083] (1) If it is detected that the call status quantity is the specified value, determining that the call return status of the smart contract machine code is a ledger call;
[0084] (2) If the call return status of the smart contract machine code is a ledger call, the ledger operation instruction preset in the blockchain platform machine code is called and executed.
[0085] The blockchain node detects whether the call status is a specified value, corresponding to a ledger call. If so, the node determines that the return status of the smart contract machine code is a ledger call, indicating that a ledger operation is required. The node then calls and executes the ledger operation instructions pre-set in the blockchain platform machine code. Conversely, if the call status is not the specified value, the node determines that the return status of the smart contract machine code is not a ledger call. In this case, the ledger operation instructions will not be invoked, and the node will instead execute the appropriate processing based on the actual call return status. For example, if the return status indicates a smart contract execution error, the node will then execute the appropriate error handling.
[0086] 104. After executing the ledger operation instruction, jump to the first instruction after the ledger call instruction in the smart contract machine code according to the address.
[0087] After executing the ledger operation instruction, that is, calling the ledger operation method to complete the operation on the blockchain ledger, it is necessary to return to the smart contract machine code to continue executing the subsequent instructions. At this time, you can jump to the first instruction after the ledger call instruction in the smart contract machine code according to the previously saved address, that is, the next instruction after the ledger call instruction.
[0088] In one implementation of the embodiment of the present application, before jumping to the first instruction after the ledger call instruction in the smart contract machine code according to the address, the following steps may also be included:
[0089] The address is read from the memory space.
[0090] If the address of the first instruction after the ledger call instruction in the smart contract machine code is saved in the memory space in the previous step, then the address needs to be read from the memory space before jumping, and then jump to the location pointed to by the address in the smart contract machine code for execution.
[0091] In one implementation of the embodiment of the present application, after jumping to the first instruction after the account call instruction in the smart contract machine code according to the address, the following may also be included:
[0092] After executing the last instruction of the smart contract machine code, return to execute the first instruction after the smart contract call instruction in the blockchain platform machine code.
[0093] After jumping back to the smart contract machine code, execution continues sequentially, starting with the first instruction after the ledger call instruction, until the last instruction of the smart contract machine code is executed. At this point, the smart contract call is complete, and the blockchain node returns to executing the blockchain platform machine code, specifically starting with the first instruction after the smart contract call instruction and continuing sequentially. It should be noted that a single smart contract machine code may contain multiple ledger call instructions, and the same method described above can be used to complete blockchain ledger operations for each ledger call instruction.
[0094] In an embodiment of the present application, the compiled smart contract machine code is first called and executed. When the ledger call instruction in the smart contract machine code is executed, the address of the first instruction after the ledger call instruction is saved, and the execution of the blockchain platform machine code is returned. Then, the ledger operation instruction preset in the blockchain platform machine code is called and executed, that is, the ledger operation method is called to implement the interaction between the smart contract and the blockchain ledger. Finally, after the ledger operation instruction is executed, the first instruction after the ledger call instruction in the smart contract machine code can be jumped to according to the previously saved address, thereby continuing to complete the call of the smart contract. The above process does not use the CALL machine instruction. Instead, it uses the saved instruction address to achieve the purpose of switching from the smart contract machine code to the blockchain platform machine code and re-entering the smart contract machine code from the blockchain platform machine code. In other words, the call of the ledger operation method by the smart contract is implemented by calling the switching machine code. This method is suitable for blockchain platforms written in languages with memory management mechanisms.
[0095] To facilitate understanding of the method of calling a ledger by a smart contract provided in an embodiment of the present application, an actual application scenario is listed below.
[0096] like Figure 2 The figure shows an operation diagram of the method for calling a ledger by a smart contract provided in an embodiment of the present application in an actual application scenario.
[0097] Figure 2 The lower left corner is the blockchain platform machine code list, which includes a preset ledger operation instruction list and a Native Call instruction list. The ledger operation instruction list records the ledger operation instructions used to operate the blockchain ledger, and the Native Call instruction list records the instructions used to implement the call switching processing logic.
[0098] For example, the pseudo code and corresponding comments corresponding to the processing logic of the Native Call instruction list are as follows:
[0099] entry:
[0100] {
[0101] nativecall(entryPoint, uintptr(unsafe.Pointer(enginContext)), modAddr)
[0102] / / Enter the smart contract machine code execution, the machine code can operate the memory space represented by enginContext;
[0103] switch status:=enginContext.statusCode; status{
[0104] case callLedgerFunctionStatus:
[0105] / / Check the call return status of the smart contract machine code execution and judge the status; if it is the ledger call status, perform the ledger call operation here;
[0106] …
[0107] …
[0108] / / Here is the omitted method code for calling account operations;
[0109] goto entry
[0110] / / After calling the ledger operation method, return to entry, that is, continue to execute the smart contract machine code;
[0111] case callNormalReturn:
[0112] / / Normal return indicates that the smart contract has been executed;
[0113] default:
[0114] / / The processing logic for abnormal situations. In theory, no abnormal situations will occur. If an abnormal situation such as an error in the execution of a smart contract occurs, the error reporting and other processing logic can be executed.
[0115] }
[0116] }
[0117] Through the processing logic of the pseudocode above, combined with the reading of memory state, it is possible to control the entry and return of the smart contract machine code. The entry here refers to switching from the blockchain platform machine code to the smart contract machine code, and the return here refers to switching from the smart contract machine code to the blockchain platform machine code.
[0118] Figure 2the right lower part of the figure is a smart contract machine code list, which contains operation instructions executed when the smart contract is called, and at least one ledger call instruction is included in the operation instructions. Figure 2 the upper part of the figure is a Golang memory space of a blockchain node, which can be used to access memory state quantities.
[0119] Figure 2 The figure has 6 call chains, which are represented by numbers 1-6, and the following will be described respectively.
[0120] 1. The blockchain platform calls the compiled smart contract machine code.
[0121] 2. When the smart contract machine code executes to the operation ledger logic (i.e. the ledger call instruction), the calling state quantity and the address of the first instruction after the ledger call instruction are saved to the Golang memory space, and the blockchain platform machine code is returned.
[0122] 3. The control logic of the Native Call instruction list finds that the calling return state is a ledger call according to the saved calling state quantity, and starts the calling of the ledger operation method.
[0123] 4. After the execution of the ledger operation method is completed, the control logic is returned.
[0124] 5. The control logic reenters the smart contract machine code, and at this time, the execution position after the ledger call instruction of the smart contract machine code is found according to the instruction address read from the Golang memory space.
[0125] 6. The smart contract machine code is executed completely, and the control logic is returned, and thus the calling process of the smart contract is completed.
[0126] In summary, the embodiments of the present application realize the calling of the ledger operation method by the smart contract by calling the switching machine code instead of using the CALL machine instruction, and this method can be applied to the blockchain platform written by a language including a memory management mechanism.
[0127] It should be understood that the size of the serial number of each step in each embodiment described above does not mean the order of execution, and the execution order of each process should be determined according to its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0128] The above mainly describes a method for calling a ledger by a smart contract, and a device for calling a ledger by a smart contract will be described below.
[0129] Referring to Figure 3 , one embodiment of a device for calling a ledger by a smart contract in the embodiments of the present application includes:
[0130] The contract machine code calling module 301 is used to call and execute the compiled smart contract machine code;
[0131] The address saving module 302 is used to save the address of the first instruction after the ledger call instruction in the smart contract machine code when executing the ledger call instruction in the smart contract machine code, and return to execute the blockchain platform machine code;
[0132] The ledger operation instruction calling module 303 is used to call and execute the ledger operation instructions preset in the blockchain platform machine code;
[0133] The instruction jump module 304 is used to jump to the first instruction after the ledger call instruction in the smart contract machine code according to the address after executing the ledger operation instruction.
[0134] In one implementation of the embodiment of the present application, the apparatus for invoking an account book by a smart contract may further include:
[0135] The state quantity setting module is used to set the calling state quantity to a specified value;
[0136] The account book operation instruction calling module may include:
[0137] a call return status determining unit, configured to determine that the call return status of the smart contract machine code is a ledger call if it is detected that the call status amount is the specified value;
[0138] The ledger operation instruction calling unit is configured to call and execute the ledger operation instruction preset in the blockchain platform machine code if the call return status of the smart contract machine code is a ledger call.
[0139] In one implementation of the embodiment of the present application, the contract machine code calling module may include:
[0140] The contract machine code execution unit is used to call and execute the smart contract machine code when executing the smart contract call instruction in the blockchain platform machine code.
[0141] Furthermore, the device for the smart contract to call the ledger may also include:
[0142] The instruction return module is used to return to execute the first instruction after the smart contract call instruction in the blockchain platform machine code after executing the last instruction of the smart contract machine code.
[0143] In one implementation of the embodiment of the present application, the apparatus for invoking an account book by a smart contract may further include:
[0144] The first bytecode acquisition module is used to obtain the bytecode of the smart contract;
[0145] A first target instruction detection module is used to detect target instructions for operating the account book in the bytecode;
[0146] A target instruction replacement module is used to replace the target instruction with a specified instruction to obtain an updated bytecode; wherein the processing logic of the specified instruction includes saving the address of the first instruction after the specified instruction, setting a call state and a subroutine return;
[0147] A first bytecode compilation module is configured to compile the updated bytecode to obtain the smart contract machine code; wherein the ledger call instruction in the smart contract machine code is obtained by compiling the specified instruction.
[0148] In another implementation of the embodiment of the present application, the apparatus for invoking an account book by a smart contract may further include:
[0149] The second bytecode acquisition module is used to obtain the bytecode of the smart contract;
[0150] A second target instruction detection module is used to detect target instructions for operating the account book in the bytecode;
[0151] A second bytecode compilation module is configured to compile the target instruction into the ledger call instruction in the smart contract machine code during the process of compiling the bytecode into the smart contract machine code; wherein the processing logic of the ledger call instruction includes saving the address of the first instruction after the ledger call instruction, setting a call state, and returning from a subroutine.
[0152] Furthermore, the target instruction detection module may include:
[0153] A target instruction determining unit is configured to determine, for any instruction in the bytecode, if it is detected that the instruction contains a key name related to the operation account book, the instruction as the target instruction.
[0154] In one implementation of the embodiment of the present application, the address storage module may include:
[0155] An address storage unit, configured to store the address in a memory space;
[0156] The device for calling the ledger by the smart contract may further include:
[0157] The address reading module is used to read the address from the memory space.
[0158] An embodiment of the present application also provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the method of calling a ledger by a smart contract described in any of the above embodiments.
[0159] An embodiment of the present application also provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the method of calling a ledger by a smart contract as described in any of the above embodiments.
[0160] Figure 4 Schematic diagram of an electronic device provided by an embodiment of the present application. Figure 4 As shown, the electronic device 4 of this embodiment includes: a processor 40, a memory 41, and a computer program 42 stored in the memory 41 and executable on the processor 40. When the processor 40 executes the computer program 42, the steps in the above-mentioned embodiments of the method for calling the ledger by each smart contract are implemented, for example Figure 1 Alternatively, when the processor 40 executes the computer program 42, the functions of the modules / units in the above-mentioned device embodiments are realized, for example, Figure 3 Functions of modules 301 to 304 are shown.
[0161] The computer program 42 may be divided into one or more modules / units, which are stored in the memory 41 and executed by the processor 40 to implement the present application. The one or more modules / units may be a series of computer program instruction segments capable of performing specific functions, and the instruction segments are used to describe the execution process of the computer program 42 in the electronic device 4.
[0162] The processor 40 may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0163] The memory 41 may be an internal storage unit of the electronic device 4, such as a hard disk or memory of the electronic device 4. The memory 41 may also be an external storage device of the electronic device 4, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 4. Furthermore, the memory 41 may include both an internal storage unit of the electronic device 4 and an external storage device. The memory 41 is used to store the computer program and other programs and data required by the electronic device. The memory 41 may also be used to temporarily store data that has been output or is about to be output.
[0164] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0165] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0166] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0167] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0168] In the embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the system embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0169] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present application.
[0170] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0171] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the process in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program, when executed by the processor, can implement the steps of the above-mentioned various method embodiments. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0172] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present application, and should all be included in the scope of protection of the present application.
Claims
1. A method for a smart contract to call an account book, characterized in that: include: Call and execute the compiled smart contract machine code; When executing the ledger call instruction in the smart contract machine code, save the address of the first instruction after the ledger call instruction in the smart contract machine code, and return to execute the blockchain platform machine code; Call and execute the account book operation instructions preset in the blockchain platform machine code; After executing the account book operation instruction, jump to the first instruction after the account book call instruction in the smart contract machine code according to the address.
2. The method according to claim 1, wherein When the account book call instruction in the smart contract machine code is executed, it also includes: Set the call state of the smart contract machine code to a specified value; The calling and executing of the account book operation instructions preset in the blockchain platform machine code includes: If it is detected that the call status amount is the specified value, determining that the call return status of the smart contract machine code is a ledger call; If the call return status of the smart contract machine code is a ledger call, the ledger operation instruction preset in the blockchain platform machine code is called and executed.
3. The method according to claim 1, wherein The calling and executing of the compiled smart contract machine code includes: When the smart contract call instruction in the blockchain platform machine code is executed, the smart contract machine code is called and executed.
4. The method according to claim 3, wherein After jumping to the first instruction after the account call instruction in the smart contract machine code according to the address, the method further includes: After executing the last instruction of the smart contract machine code, return to execute the first instruction after the smart contract call instruction in the blockchain platform machine code.
5. The method according to claim 1, wherein Before calling and executing the compiled smart contract machine code, it also includes: Get the bytecode of the smart contract; Detecting target instructions for operating the ledger in the bytecode; Replacing the target instruction with a specified instruction to obtain an updated bytecode; wherein the processing logic of the specified instruction includes saving the address of the first instruction after the specified instruction, setting the call state of the smart contract machine code, and the subroutine return; The updated bytecode is compiled to obtain the smart contract machine code; wherein the ledger call instruction in the smart contract machine code is obtained by compiling the specified instruction.
6. The method according to claim 1, wherein Before calling and executing the compiled smart contract machine code, it also includes: Get the bytecode of the smart contract; Detecting target instructions for operating the ledger in the bytecode; In the process of compiling the bytecode into the smart contract machine code, the target instruction is compiled into the ledger call instruction in the smart contract machine code; wherein the processing logic of the ledger call instruction includes saving the address of the first instruction after the ledger call instruction, setting the call state of the smart contract machine code, and returning from the subroutine.
7. The method according to any one of claims 1 to 6, wherein: The storing of the address of the first instruction after the ledger call instruction in the smart contract machine code includes: Storing the address in memory space; Before jumping to the first instruction after the account call instruction in the smart contract machine code according to the address, the method further includes: The address is read from the memory space.
8. A device for calling a ledger by a smart contract, characterized in that: include: The contract machine code calling module is used to call and execute the compiled smart contract machine code; An address saving module is configured to save the address of the first instruction after the ledger call instruction in the smart contract machine code when the ledger call instruction in the smart contract machine code is executed, and return to execute the blockchain platform machine code; An account book operation instruction calling module, used to call and execute the account book operation instructions preset in the blockchain platform machine code; An instruction jump module is used to jump to the first instruction after the ledger call instruction in the smart contract machine code according to the address after executing the ledger operation instruction.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method for calling a ledger by a smart contract as described in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method for calling a ledger by a smart contract as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Data access method and device based on block chain and electronic equipment
CN113077260A
Implementation method and device of multiple intelligent contract engines, electronic equipment and storage medium
CN115022312A