Instruction expansion management method of block chain smart contract
Through dynamic expansion and modular management strategies, flexible expansion of smart contract instruction sets is achieved, which solves the problem that existing instruction sets cannot meet complex application scenarios, and improves the functional customization and expansion capabilities of smart contracts.
Patent Information
- Application Number
- CN202411959635.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2044-12-30
AI Technical Summary
Due to the static and closed design of the existing smart contract instruction set, it cannot meet the needs of complex application scenarios, lacks flexibility and expansion capabilities, which limits the development of blockchain technology and the implementation of innovative applications.
Using dynamic expansion and modular management strategies, a blockchain smart contract instruction expansion management method is designed. By introducing instruction expansion manager, smart contract compiler and function expansion manager, the dynamic loading, execution and management of new instructions is realized.
On the basis of ensuring the efficient implementation environment of existing smart contracts, it provides flexible and customizable instruction expansion solutions, supports the addition and execution of new instructions, enhances the customization of the functions of smart contracts, and meets diverse needs.
Smart Images

Figure CN119847605A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain technology, and in particular to an instruction expansion management method for a blockchain smart contract. Background Art
[0002] With the rapid development of blockchain technology, smart contracts, as an important part of the blockchain ecosystem, have been widely used in many industries. Smart contracts automatically execute contract terms based on the blockchain system through preset logic, which improves the transparency and efficiency of transactions and reduces the possibility of human intervention and manual operation errors. This automated feature makes smart contracts an indispensable foundation for distributed applications, and is used in many fields such as financial services, data security, digital identity authentication, and digital asset management. Especially in the financial field, the application of blockchain and smart contracts reduces transaction costs and improves the credibility of services. However, with the continuous enrichment of blockchain application scenarios, the challenges faced by smart contracts are becoming more prominent, especially in scientific research innovation and specific industry applications with increasingly complex requirements. The existing smart contract instruction set has gradually exposed the bottleneck of being unable to meet diverse needs.
[0003] The current smart contract instruction set is usually predefined by the smart contract compiler, with limited quantity and functions, and its expansion space is relatively limited. Although this static and closed instruction set design can meet basic needs in traditional applications, it is powerless when faced with complex application scenarios. For example, with the diversified development trend of blockchain technology application scenarios, the development needs of smart contracts are not only limited to traditional finance and asset management, but also extended to more complex scenarios such as scientific research exploration, cross-industry cooperation and multi-party interaction. In these scenarios, the existing instruction sets often lack sufficient flexibility, have limited expansion capabilities, and cannot efficiently support developers to implement new business logic and functions. This defect not only limits the further development of blockchain technology, but also affects the implementation of innovative applications. Therefore, how to break through the limitations of the existing instruction set and improve its expansion capabilities has become an important issue in the development of blockchain technology.
[0004] The expansion of the instruction set is not just a simple increase in functions, but also involves the optimization and adjustment of the overall process of smart contracts from compilation to calling, and it is necessary to consider factors such as compatibility, efficiency and operability. At present, some researchers have added custom instructions to implement specified functions in the research process, such as permission control, vulnerability checking, etc., but excessive instruction expansion will increase the complexity of the contract execution environment and affect performance and resource utilization. Therefore, instruction expansion also needs to provide developers with a flexible and operable custom instruction interface without affecting the execution efficiency of the existing contract code. In summary, it is particularly important to design an efficient and operable smart contract instruction expansion management method, which not only needs to be able to solve the problem of limited instruction set space, but also needs to enable the smart contract execution environment to efficiently manage the newly added instructions and provide execution functions that support these new instructions.
[0005] In order to solve this problem, the present invention proposes an instruction expansion management method for smart contracts, which aims to break the limitations of traditional instruction sets by introducing dynamic expansion and modular management strategies, and provide a flexible and customizable instruction expansion solution. The expansion of the instruction set is designed as a modular structure, allowing developers to flexibly add new instructions according to actual needs, and can efficiently manage the execution of new instructions. Unlike traditional static instruction sets, this dynamic expansion method can flexibly support the addition and execution of new instructions while ensuring the efficiency of the existing smart contract execution environment. This method not only provides effective technical support for developers of blockchains and smart contracts, but also provides new ideas for the development of blockchain technology, especially in meeting scientific research needs, promoting cross-industry applications, and supporting the implementation of complex logic of smart contracts. It has important theoretical value and practical significance. Summary of the invention
[0006] A method for managing instruction expansion of a blockchain smart contract, characterized in that the following operations are performed in an instruction expansion manager, including:
[0007] The parameter parser parses the parameters input by the user, obtains the basic configuration information and sends it to the instruction list loader parser. The basic configuration information includes the path of the newly added function mapping file and the name of the new instruction used by the current smart contract;
[0008] The parser loaded by the instruction list accesses the new function mapping file according to the basic configuration information, reads the basic information of the new instructions used by the current smart contract, and generates a new instruction operation table. The new function mapping file consists of two parts: the basic information of the new instructions and the operation function.
[0009] Preferably, the following operations are performed in the smart contract compiler, including:
[0010] The compilation parameter parser parses the compilation command input by the user, reads the smart contract according to the name and path of the smart contract in the compilation command, parses it into a character stream form and then sends it to the contract parser;
[0011] The contract parser performs lexical analysis, grammatical analysis, and semantic analysis on the smart contract in the form of a character stream, and obtains a semantic information table and an abstract syntax tree containing the name of the new instruction;
[0012] The instruction processor performs instruction analysis, calls default instructions, and calls new instructions according to the semantic information table and the abstract syntax tree, and generates a contract intermediate expression including new instruction information, data, data flow, and control flow information;
[0013] The bytecode generator generates bytecode including new instruction information and corresponding application program interface according to the intermediate expression of the contract, and deploys it to the smart contract running environment.
[0014] Preferably, the following operations are performed in the function extension manager, including:
[0015] The dynamic library generator reads the parameters input by the user, including the command rules and path of the new function dynamic library and the path of the new function mapping file, checks the correctness of the new function mapping file path, and if correct, reads the basic information and operation function of the new instruction from the new function mapping file, wherein the operation function of the new instruction represents the execution logic of the new instruction;
[0016] The new function mapping file calls the basic operation function module in the basic function library to cooperate with the operation function of the new instruction, and the basic operation function module represents the common and general basic operation logic;
[0017] The dynamic library generator operates the newly added function mapping file and the basic function library to generate a new function dynamic library, wherein the new function dynamic library includes the operation function of the new instruction;
[0018] The new function processor manages the new function dynamic library and performs operating environment processing and mapping file monitoring.
[0019] Preferably, the following operations are performed in the smart contract running environment, including:
[0020] The running parameter parser parses the call command input by the user, determines the bytecode involved in the currently running smart contract according to the call command, and then parses the bytecode to extract the opcode and operand from it, and sends the opcode and operand to the opcode parser. The opcode corresponds to the instruction one by one, the default opcode corresponds to the default instruction, and the new opcode corresponds to the new instruction;
[0021] The default operation functions corresponding to the default instructions are stored in the default function library;
[0022] The operation code parser calls the operation function according to the operation code, including calling the default function and calling the newly added function. If the current operation code is the default operation code, the operation code parser accesses the default function library to load the default operation function module corresponding to the default operation code; if the current operation code is a new operation code, the operation code parser transmits the name of the new operation code to the operation extension processor, and the operation extension processor calls the corresponding newly added function to obtain the operation code and operand containing the operation function, and then sends the operation code and operand to the execution engine;
[0023] The running extension processor loads the new function dynamic library to call the new function, which specifically includes two parts: preprocessing and calling functions. The preprocessing is that the running extension processor saves the memory address and library name of the received new function dynamic library; the calling function is that the running extension processor loads the new function dynamic library according to the memory address and library name of the new function dynamic library, and calls the corresponding new function module;
[0024] The execution engine executes the operation code and outputs the execution result.
[0025] Preferably, the newly added function mapping file consists of two parts: basic information of the newly added instructions and operation functions, including:
[0026] a. Basic information of new instructions: The basic information of the new instructions includes type, group number, instruction name and bytecode. The type indicates the category to which the new instruction belongs. The new instructions are divided into identification instructions and character instructions. The identification instructions guide the behavior of the contract through specific symbols or marks and are used to transmit control information. The character instructions are composed of letters and numbers and are used to execute specific business logic or data processing. The group number indicates the group to which the new instruction belongs. The instruction name indicates the name of the new instruction. The bytecode indicates the byte representation of the new instruction. The bytecode has a unique correspondence with the instruction name and cannot be repeated with the bytecode corresponding to the default instruction in the compiler. Users can support further classification of instructions according to functional characteristics by defining different group numbers;
[0027] b. Operation function: The operation function of the new instruction refers to the instruction function executed during the contract operation, including the function name and the function body. The function name indicates the function name of the instruction, and the function body indicates the execution function corresponding to the instruction. It is divided into contract behavior control function and business logic processing function according to the instruction type.
[0028] Preferably, the contract parser sequentially performs lexical analysis, grammatical analysis, and semantic analysis on the smart contract in the form of a character stream, including:
[0029] a. Lexical analysis: Scan the character stream, identify the basic elements of the contract, and convert them into tokens with grammatical meaning. New instructions will be processed as new special identifiers;
[0030] b. Syntax analysis: Parse the token stream according to the syntax rules to generate an abstract syntax tree. When encountering a new instruction, the contract parser will match the instruction with the relevant function or variable in the program, and add a new field identifier to the corresponding node of the abstract syntax tree to mark the target and context environment associated with the new instruction operation;
[0031] c. Semantic analysis: Based on the information in the abstract syntax tree, check whether the contract complies with the semantic rules. The instruction processor will add the identifier corresponding to the new instruction to the semantic information table. At the same time, the contract parser will ensure that the relationship and logic between the various elements of the contract are correct based on the context information in the contract semantic information table.
[0032] Preferably, the generating of the contract intermediate expression by the instruction processor includes:
[0033] a. Instruction analysis: determine the instructions to be used according to the semantic information table and the abstract syntax tree, and analyze the instructions to determine whether the instructions are default instructions or new instructions;
[0034] b. Retrieve the default instruction: If the current instruction is a default instruction, the instruction processor accesses the default instruction table to retrieve detailed information related to the current instruction;
[0035] c. Retrieve new instruction: If the current instruction is a new instruction, the instruction processor accesses the new instruction execution table and retrieves the relevant information of the new instruction. Specifically, the entry corresponding to the name of the new instruction is searched in the new instruction execution table to obtain the bytecode of the instruction.
[0036] Preferably, the new function processor performs operating environment processing and mapping file monitoring, including:
[0037] a. Runtime environment processing: The new function processor sends the memory address and library name of the new function dynamic library to the run extension processor, so that the opcode parser can correctly call the function module in the new function dynamic library through the run extension processor when calling the new function;
[0038] b. Mapping file monitoring: Monitor the new function mapping file according to user needs, and continue monitoring at the set interval. If the monitoring finds that there are new instructions that have not been loaded, regenerate a new function dynamic library containing all new instructions, and resend the memory address and library name of the new function dynamic library to the running extension processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Hereinafter, some specific embodiments of the present invention will be described in detail in an exemplary and non-limiting manner with reference to the accompanying drawings. The same reference numerals in the accompanying drawings indicate the same or similar components or parts. It should be understood by those skilled in the art that these drawings are not necessarily drawn to scale. The objects and features of the present invention will become more apparent in view of the following description in conjunction with the accompanying drawings, in which:
[0040] Figure 1 Provides a flowchart of the execution summary of the instruction expansion management method for blockchain smart contracts;
[0041] Figure 2 Detailed flow chart for running the instruction expansion management method of blockchain smart contracts;
[0042] Figure 3 It is a flowchart of the operation of the instruction expansion manager;
[0043] Figure 4 This is a flowchart of the function extension manager operation. DETAILED DESCRIPTION
[0044] In order to solve the contradiction between the demand for new smart contract instructions in current application scenarios and scientific research exploration and the fixed and limited number of instructions in the existing smart contract compilation and running environment, the present invention proposes an instruction expansion management method for blockchain smart contracts, which supports users to customize instructions according to actual needs, and can realize dynamic loading, execution and management of new instructions in the smart contract compiler and running environment, providing users with a flexible and extensible smart contract instruction usage environment.
[0045] The present invention proposes a method for managing instruction expansion of blockchain smart contracts. The overall operation process is as follows: Figure 1 shown.
[0046] The overall operation process involves the user-written smart contract with new instructions, the new function mapping file, the instruction expansion manager, the contract compiler, the function extension manager, and the smart contract operating environment, such as Figure 1As shown. The instruction expansion manager generates a new instruction run table based on the parameters passed in by the user and the instruction information in the new function mapping file through the parameter parser and instruction list loading parser. The smart contract compiler completes the compilation operation of the smart contract with new instructions through the compilation parameter parser, contract parser, instruction processor, default instruction table and bytecode generator according to the compilation command passed in by the user. During this process, the instruction processor will access the new instruction run table to query the relevant information of the new instruction, and finally generate bytecode and application interface and deploy it to the smart contract runtime environment. The function extension manager generates a new function dynamic library based on the parameters passed in by the user and the information in the new function mapping file through the dynamic library generator and the basic function library. The smart contract runtime environment runs the smart contract through the running parameter parser, opcode parser, running extension processor and execution engine according to the call command entered by the user. During this process, the running extension processor will call the operation function corresponding to the new instruction in the new function dynamic library, and finally output the execution result of the smart contract.
[0047] Further, the process is described in detail as follows Figure 2 As shown in the figure, it includes the contract writing stage, contract compilation stage and contract running stage. The components involved are described as follows:
[0048] (1) Instruction expansion manager: It consists of a parameter parser, an instruction list loader parser, and a new instruction run table. It can provide new instruction information for compilation as needed.
[0049] (2) Parameter parser: parses the parameters input by the user before compilation begins to obtain basic configuration information, which includes the name of the new instruction used in the contract and the path of the new function mapping file;
[0050] (3) Instruction list loading parser: access the new function mapping file according to the basic configuration information, obtain the relevant information of the new instructions involved in the current smart contract, and generate a new instruction operation table;
[0051] (4) New instruction operation table: provides relevant information of new instructions, including type, group number, instruction name and byte code;
[0052] (5) Smart contract compiler: It consists of a compilation parameter parser, a contract parser, an instruction processor, a default instruction table, and a bytecode generator. It can compile smart contracts and, combined with an instruction expansion manager, enables new instructions to be compiled correctly.
[0053] (6) Compilation parameter parser: parses the compilation command entered by the user, reads the compilation parameters, and converts the specified smart contract into a character stream;
[0054] (7) Contract parser: performs lexical analysis, grammatical analysis, and semantic analysis on the smart contract in the form of a character stream to obtain a semantic information table and an abstract syntax tree;
[0055] (8) Instruction processor: Determine the instructions to be used based on the semantic information table and abstract syntax tree information, and analyze the instructions. If it is a default instruction, access the default instruction table and call the default instruction; if it is a new instruction, access the new instruction operation table in the instruction expansion manager, call the new instruction, and generate the corresponding contract intermediate expression based on the instruction information;
[0056] (9) Bytecode generator: generates bytecode based on the intermediate expression of the contract, and outputs the bytecode and the corresponding application interface;
[0057] (10) Function extension manager: It consists of a dynamic library generator, a basic function library, and a new function processor. It can dynamically load the operation functions of new instructions and integrate them into the smart contract running environment.
[0058] (11) Dynamic library generator: by parsing the parameters input by the user, checking whether the new function mapping file exists, and reading the basic information and operation functions of the new instructions from it to support the generation of the new function dynamic library;
[0059] (12) Basic function library: provides basic operation function modules for the newly added function mapping files, which can be called by new instructions in the newly added function mapping files;
[0060] (13) New function processor: includes two operations: operating environment processing and mapping file monitoring, and can also manage the generated new function dynamic library;
[0061] (14) Smart contract runtime environment: It consists of a runtime parameter parser, an opcode parser, a default function library, a runtime extension processor, and an execution engine, and can execute smart contract bytecode;
[0062] (15) Run the parameter parser: parse the call command input by the user, determine the bytecode involved in the current call, and extract the corresponding operation code and operand from the bytecode;
[0063] (16) Operation code parser: receives the operation code and operand passed in by the operation parameter parser, and calls the corresponding default operation function and new operation function according to the operation code;
[0064] (17) Default function library. It stores the default operation function modules corresponding to the default operation codes;
[0065] (18) Run extension processor: It is the connection bridge between the smart contract runtime environment and the function extension manager. It can receive the call new function requirement information passed in by the opcode parser and dynamically load the new function dynamic library in the new function processor according to the requirements;
[0066] (19) Execution engine: executes the operation function corresponding to the contract operation code called this time and outputs the corresponding execution result.
[0067] Based on the above functional components, the execution process of the blockchain smart contract instruction expansion management method designed by the present invention is described as follows:
[0068] During the contract writing phase, the user first writes a new function mapping file, which includes the basic information of the new instruction (type, group number, instruction name and bytecode) and the operation function corresponding to the instruction (①). After that, the user can write a smart contract and add new instructions (character instructions and identification instructions) to the contract (②).
[0069] After the writing phase is completed, the contract compilation phase begins. Before starting to compile the contract, the user needs to first input parameters to the parameter parser (③). The parameters include the path of the new function mapping file and the name of the new instruction used in the smart contract (i.e., basic configuration information). The parameter parser sends the above basic configuration information to the instruction list loading parser (④). The instruction list loading parser reads the corresponding basic information from the new function mapping file according to the name of the new instruction used (⑤⑥), and generates a new instruction operation table (⑦). The user enters the compilation command to start the compilation operation (⑧). The compilation parameter parser parses the compilation parameters from the compilation command, determines the smart contract to be compiled, converts the smart contract into a character stream, and inputs it into the contract parser (⑨). The contract parser performs lexical analysis (a), grammatical analysis (b), and semantic analysis (c) on the smart contract in turn, generates a semantic information table and an abstract syntax tree carrying the name of the new instruction, and transmits it to the instruction processor (⑩). The instruction processor performs instruction analysis based on the input information (a). If a default instruction is encountered, the default instruction information is retrieved from the default instruction table (b) If you encounter a new instruction, go to the new instruction operation table to retrieve the new instruction information (c) After the instruction is called, an intermediate expression of the contract carrying the new instruction information is generated and input into the bytecode generator Generate bytecode and application program interface Deploy to the smart contract runtime environment Complete the compilation phase.
[0070] After the contract compilation phase is completed, the contract operation phase begins. Before starting to call the contract, the user needs to first enter the parameters (naming rules, path of the new function dynamic library, and path of the new function mapping file) to the dynamic library generator. The dynamic library generator parses the parameters and first checks whether the new function mapping file in the specified path exists. If it exists, it reads the basic information of the new instruction and the corresponding operation function from it. The operation functions corresponding to the new instructions can be implemented by calling the basic operation function modules in the basic dynamic library Generate a new function dynamic library based on the basic information and operation functions of the new instructions The new function processor can not only manage the generated new function dynamic library, but also perform runtime processing (a), sending the preprocessing information to the runtime extension processor in the smart contract runtime environment. Run the extension processor to perform preprocessing operations and record the memory address of the new function dynamic library to facilitate the subsequent retrieval of the operation function corresponding to the new instruction. At the same time, the new function processor can monitor whether the new function mapping file has changed (b) If there is a change, a new function dynamic library will be dynamically generated. After the preparation is completed, the user sends a call command to the smart contract running environment The call command is parsed by the running parameter parser, the bytecode used for this call is determined according to the call command, the bytecode is parsed, and the parsed opcode and operand are sent to the opcode parser The opcode parser loads the default operation function corresponding to the default opcode from the default function library (a) By running the extension processor to the new function dynamic library, the new operation function corresponding to the new opcode is retrieved (b) Send the opcode and operands of the function module to the execution engine The execution engine executes the opcode and outputs the execution result, completing the contract operation.
[0071] Based on the above overall process design, the specific work of the important steps is described in detail as follows:
[0072] During the contract writing phase, users need to write new function mapping files and smart contracts with new instructions.
[0073] (1) User writes new function mapping file
[0074] The new function mapping file defines information about the new instructions.
[0075] The information of the new instruction includes two parts: basic information of the new instruction and operation function.
[0076] The basic information of the new instruction is used to generate the corresponding bytecode during the contract compilation process, including type, group number, instruction name and bytecode. The type indicates the category to which the new instruction belongs. The new instruction is divided into identification instructions and character instructions. The identification instruction guides the behavior of the contract through specific symbols or marks, and is used to transmit control information without involving newly added business logic. It is mainly used to instruct specific functional components to perform some functions as required. The character instruction is composed of letters and numbers, and is used to execute specific business logic or data processing, which can directly affect the running content and results of the contract. The introduction of these two types of instructions can enhance the expressive power of the smart contract language, enabling it to respond to complex compilation and execution requirements more flexibly. The group number indicates the group to which the new instruction belongs, the instruction name indicates the name of the new instruction, and the bytecode indicates the hexadecimal byte representation of the new instruction. The bytecode has a unique correspondence with the instruction name, and cannot be repeated with the bytecode corresponding to the default instruction in the compiler. Users can support further classification of instructions according to functional characteristics by defining different group numbers. The operation function of the new instruction refers to the instruction function executed during the contract operation, including a function name and a function body. The function name indicates the function name of the instruction, and the function body indicates the execution function corresponding to the instruction. It is divided into contract behavior control function and business logic processing function according to the instruction type.
[0077] (2) Users write smart contracts with new instructions
[0078] The smart contract with new instructions refers to adding new instructions based on the original smart contract writing logic. Users can select instructions in the new function mapping file and write these instructions in the appropriate position of the smart contract according to actual needs.
[0079] During the contract compilation phase, all operations are completed in the instruction extension manager and the smart contract compiler. The user needs to pass the name of the new instruction used in the smart contract and the path of the new function mapping file to the instruction extension manager, and enter the compilation command to execute the compilation operation of the smart contract.
[0080] The operations involved in the instruction extension manager are as follows Figure 2 shown.
[0081] (3) User input parameters
[0082] Before the smart contract compilation process begins, the user needs to input parameters (the path of the new function mapping file and the name of the new instruction used in the smart contract, that is, the basic configuration information) to the parameter parser. The parameter parser is responsible for processing and parsing these parameters so that the basic configuration information can be accurately passed to the subsequent processing stage.
[0083] (4) The parameter parser sends the parsed basic configuration information to the instruction list loader parser
[0084] After receiving the parameters input by the user, the parameter parser first parses the basic configuration information to confirm the path of the new function mapping file and the name of the new instruction contained therein, and then passes the parsed information to the instruction list loader parser.
[0085] (5) Access the new function mapping file
[0086] The instruction list loader parser accesses the specified new function mapping file according to the file path in the passed basic configuration information.
[0087] (6) Read instruction information
[0088] The instruction list loader parser extracts specific data related to the new instructions in the smart contract from the new function mapping file. These data include the type, group number, instruction name and corresponding bytecode of each new instruction.
[0089] (7) Dynamically generate new instruction execution table
[0090] The instruction list loader parser dynamically generates a new instruction run table based on the instruction data read. The new instruction run table provides the necessary instruction information for the subsequent compilation of the smart contract compiler, enabling the new instructions to be correctly integrated and function.
[0091] At this point, the new instruction information used by the current smart contract has been dynamically loaded, and then the smart contract needs to be compiled in the smart contract compiler.
[0092] (8) User enters the compile command
[0093] At the beginning of the smart contract compilation process, the user needs to provide a compilation command to start the entire compilation process. The compilation command includes the smart contract file name and compilation options.
[0094] (9) Parsing compilation commands and smart contracts
[0095] The compilation parameter parser parses the compilation command, obtains the compilation parameters, loads and parses the smart contract, and transmits the parsed contract in the form of a character stream to the contract parser.
[0096] (10) Analysis of smart contracts
[0097] The contract parser performs a. lexical analysis, b. syntax analysis, and c. semantic analysis on the contract. The contract parser is one of the core components in the smart contract compilation process and is responsible for comprehensive analysis of the contract. The process includes three stages: lexical analysis, syntax analysis, and semantic analysis to ensure that the syntax and semantics of the contract are correct and provide a basis for subsequent instruction processing.
[0098] a. Lexical analysis: Lexical analysis is the process of converting the character stream of a smart contract into a series of token streams. At this stage, the contract parser scans the character stream in the source code, identifies the basic elements of the contract, such as keywords, identifiers, constants, operators, separators, etc., and converts them into tokens with grammatical meaning. Each token represents a specific language component in the program, such as a variable name, type, control statement, etc. For new instructions encountered, the lexical analyzer will treat it as a new, special identifier, thereby providing accurate input for subsequent syntax analysis.
[0099] b. Syntax analysis: Syntax analysis is based on lexical analysis, and parses the token stream according to the syntax rules of the smart contract to generate an abstract syntax tree. The abstract syntax tree is a tree representation of the source code structure, which can record the grammatical relationship and hierarchy of each part of the program. In the process of building the syntax analysis, when encountering new instructions, the contract parser will match these instructions with the relevant functions or variables in the program, and add new field identifiers to the corresponding nodes of the abstract syntax tree to indicate the target (such as function or variable) of the instruction, so as to identify the specific operation location of the new instruction in the tree structure.
[0100] c. Semantic analysis: Semantic analysis is an operation based on the abstract syntax tree. Its main purpose is to verify the logical consistency of the program, the correctness of data types, and the compliance with other semantic rules. In this process, the contract parser will check whether the contract follows the semantic rules of the language based on the information in the abstract syntax tree, such as whether the variables are correctly declared and whether the types match. For new instructions, the instruction processor will add its corresponding identifier to the semantic information table and ensure that the instruction is logically and syntactically correct.
[0101] The semantic information table and abstract syntax tree generated here have been integrated into the name of the new instruction. The contract parser transmits the semantic information table and abstract syntax tree to the instruction processor.
[0102] (11) Instruction analysis and instruction retrieval
[0103] The instruction processor includes three operations: a. instruction analysis, b. calling default instructions and c. calling new instructions.
[0104] a. Instruction analysis: Instruction analysis refers to determining and analyzing the instructions to be used based on the semantic information table and the abstract syntax tree. At this stage, the instruction processor will first accurately identify the instructions that need to be applied in the contract based on the variables, functions and their types recorded in the semantic information table, combined with the operation structure defined in the abstract syntax tree, and analyze the instructions to determine the instruction type. The instruction type here refers to the default instruction and the new instruction. Once the instruction processor determines the instruction type, it will launch the next instruction call.
[0105] b. Retrieve default instructions: If the current instruction is a default instruction, the instruction processor will access the default instruction table and retrieve detailed information related to the instruction. Default instructions refer to the instruction types that have been predefined and widely used in the contract compilation process. The behavior and bytecode of these instructions are fixed. By looking up the entries in the default instruction table, the instruction processor can quickly find information related to the target instruction, thereby accurately embedding it into the contract intermediate representation.
[0106] c. Retrieve new instructions: If the current instruction is a new instruction, the instruction processor needs to access the new instruction execution table and retrieve the relevant information of the new instruction. The instruction processor searches for the corresponding entry in the new instruction execution table based on the name of the new instruction and obtains the bytecode of the instruction. These bytecodes will be used to generate the intermediate expression of the contract and eventually converted into executable smart contract bytecode.
[0107] (12) Transmitting Contract Intermediate Expressions
[0108] After the instruction processor calls the default instruction and the new instruction in turn, it will generate the corresponding contract intermediate expression, which has already integrated the relevant information of the new instruction. Next, the intermediate expression of the contract will be transmitted to the bytecode generator for further processing and generation of the final bytecode.
[0109] (13) Generate bytecode
[0110] The bytecode generator converts the intermediate expression of the contract into bytecode that can be executed in the smart contract runtime environment. Finally, the bytecode and the corresponding application interface file are output.
[0111] (14) Deployment
[0112] Deploy the bytecode and application program interface to the smart contract runtime environment for subsequent user calls.
[0113] At this point, all operations in the current smart contract compilation phase are completed. Next, users can start calling operations and running smart contracts.
[0114] During the contract operation phase, all operations are completed in the function extension manager and the smart contract operation environment. The user needs to transfer the naming rules and paths of the new function dynamic library and the path of the newly added function mapping file to the function extension manager, and enter the call command to execute the call operation of the smart contract.
[0115] The operations involved in the function extension manager are as follows: Figure 3 shown.
[0116] (15) User input parameters
[0117] Before the smart contract call operation begins, the user needs to input parameters (naming rules, path of the new function dynamic library, and path of the new function mapping file) into the dynamic library generator for subsequent generation of the new function dynamic library.
[0118] (16) Check and read the information of the new instruction
[0119] The dynamic library generator parses the parameters to obtain the naming rules, path and path of the new function dynamic library and the path of the new function mapping file. First, check whether the new function mapping file exists. If it exists, access the new function mapping file to read the basic information of the new instruction and the corresponding operation function.
[0120] (17) Calling basic operation functions
[0121] The operation function of the new instruction in the new function mapping file can be implemented on the basic operation function in the basic dynamic library, that is, the basic operation function module in the basic dynamic library can be directly called.
[0122] (18) The new function processor manages the new function dynamic library and is responsible for operating environment processing and mapping file monitoring.
[0123] The dynamic library generator operates the newly added function mapping file and the basic function library to generate the corresponding new function dynamic library. The new function processor can manage the new function dynamic library. During the dynamic library generation process, the mapping relationship between function functions and instructions is strictly managed so that the instructions and function execution can be correctly matched during subsequent calls. At the same time, the new function processor can also perform two operations: a. operating environment processing and b. mapping file monitoring.
[0124] a. Runtime environment processing: Send the memory address and library name of the loaded new function dynamic library to the runtime extension processor in the runtime environment of the smart contract, so that the runtime environment of the smart contract can correctly identify and call the function modules in the new function library. If the new function dynamic library is updated later, only this operation needs to be performed to resend the memory address and name of the library. This operation can not only ensure the flexibility of the contract runtime, but also adapt to new functional requirements in real time without large-scale modifications to the original structure.
[0125] b. Mapping file monitoring: The mapping file monitoring part can monitor the mapping files of the newly added functional modules according to the needs of the users. The monitoring process continuously monitors at the set interval to ensure that the new instructions in the library can be loaded and updated in time. Since the mapping file contains the information of the new instructions and related functional modules, by monitoring the mapping file, when new instructions that have not been loaded are found, they can be automatically loaded into the running environment, and the address and name of the library can be updated as needed, so that the smart contract can call the latest function as needed during execution, and resend the memory address and library name of the new function dynamic library to the running extension processor.
[0126] At this point, all new instruction information and its functions in the current new function mapping file have been dynamically loaded. Then, the new function dynamic library needs to be called during the operation of the smart contract to execute the functions corresponding to the new instructions.
[0127] (19) User enters the call command
[0128] The user needs to provide a call command to start the smart contract execution process. The call command includes the smart contract file name, the function name to be called, and the function input.
[0129] (20) Get the opcode involved in the current call
[0130] The runtime parameter parser parses the previously deployed bytecode and the call command entered by the user, determines the bytecode involved in this call, and extracts the opcode and operand from the bytecode.
[0131] (21) Call the function corresponding to the opcode
[0132] The running parameter parser sends the parsed opcode to the opcode parser. The opcode parser determines whether the default opcode corresponding to the default instruction of the opcode currently being processed is a new opcode corresponding to the new instruction, and executes a. calling the default function and b. calling the new function.
[0133] a. Call the default function: If it is recognized as the default opcode, the opcode parser will access the default function library and load the operation function module corresponding to the default opcode.
[0134] b. Calling new functions: When the run parameter parser recognizes a new opcode, it is necessary to call the new function corresponding to the new opcode. First, the instruction name needs to be transmitted to the run extension processor, which will load the new function dynamic library based on the name and memory address of the new function dynamic library received previously, and call the corresponding new function.
[0135] (22) Execute opcode
[0136] The opcode parser transmits the opcode with the operation function attached to it to the execution engine. The execution engine executes the operation function corresponding to the opcode and finally outputs the execution result.
[0137] Beneficial effects of the present invention:
[0138] A method for managing instruction expansion of blockchain smart contracts is proposed, which is as follows: designing an instruction expansion manager to load the basic information of new instructions, so that new instructions can be correctly identified and processed during the contract compilation process. At the same time, since the instruction expansion manager can be dynamically loaded in the memory according to the needs of the application and can interact with the smart contract compiler, it is flexible and solves the problem of customization and identification of new instructions for application compilation; the smart contract compiler includes the parsing and processing process of new instructions, and works with the instruction expansion manager to jointly complete the compilation process of smart contracts including new instructions, so that the problem of the fixed number of smart contract instructions can be solved. Different applications load different new instructions through the instruction expansion manager, and the extensibility of new instructions is significantly increased; designing a function expansion manager to realize the generation of a new function dynamic library, so that the contract can correctly call the operation function of the new instruction when it is executed in the smart contract running environment. The function expansion manager also has a dynamic monitoring function and can adaptively and flexibly load new function modules.
[0139] In summary, the present invention provides a flexible and scalable smart contract instruction management method, which enhances the customizability of the functions of smart contracts, thereby meeting the needs of different application scenarios and diversified scientific research.
[0140] In the above embodiments, all or part of the functions can be implemented by software, hardware, or a combination of software and hardware. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integrations. The available medium can be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state disk (SSD)), etc.
[0141] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the embodiment of the present application should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.
Claims
1. A method for managing instruction expansion of blockchain smart contracts, characterized in that: Perform the following operations in the Command Extension Manager, including: The parameter parser parses the parameters input by the user, obtains the basic configuration information and sends it to the instruction list loader parser. The basic configuration information includes the path of the newly added function mapping file and the name of the new instruction used by the current smart contract; The parser loaded by the instruction list accesses the new function mapping file according to the basic configuration information, reads the basic information of the new instructions used by the current smart contract, and generates a new instruction operation table. The new function mapping file consists of two parts: the basic information of the new instructions and the operation function.
2. The method according to claim 1, characterized in that: The following operations are performed in the smart contract compiler, including: The compilation parameter parser parses the compilation command input by the user, reads the smart contract according to the name and path of the smart contract in the compilation command, parses it into a character stream form and then sends it to the contract parser; The contract parser performs lexical analysis, grammatical analysis, and semantic analysis on the smart contract in the form of a character stream, and obtains a semantic information table and an abstract syntax tree containing the new instruction name; The instruction processor performs instruction analysis, calls default instructions, and calls new instructions according to the semantic information table and the abstract syntax tree, and generates a contract intermediate expression including new instruction information, data, data flow, and control flow information; The bytecode generator generates bytecode including new instruction information and corresponding application program interface according to the intermediate expression of the contract, and deploys it to the smart contract running environment.
3. The method according to claim 1, characterized in that: Perform the following operations in the Extension Manager, including: The dynamic library generator reads the parameters input by the user, including the command rules and path of the new function dynamic library and the path of the new function mapping file, checks the correctness of the new function mapping file path, and if correct, reads the basic information and operation function of the new instruction from the new function mapping file, wherein the operation function of the new instruction represents the execution logic of the new instruction; The new function mapping file calls the basic operation function module in the basic function library to cooperate with the operation function of the new instruction, and the basic operation function module represents the common and general basic operation logic; The dynamic library generator operates the newly added function mapping file and the basic function library to generate a new function dynamic library, wherein the new function dynamic library includes the operation function of the new instruction; The new function processor manages the new function dynamic library and performs operating environment processing and mapping file monitoring.
4. The method according to claim 1, characterized in that: Perform the following operations in the smart contract runtime environment, including: The running parameter parser parses the call command input by the user, determines the bytecode involved in the currently running smart contract according to the call command, and then parses the bytecode to extract the opcode and operand from it, and sends the opcode and operand to the opcode parser. The opcode corresponds to the instruction one by one, the default opcode corresponds to the default instruction, and the new opcode corresponds to the new instruction; The default operation functions corresponding to the default instructions are stored in the default function library; The operation code parser calls the operation function according to the operation code, including calling the default function and calling the newly added function. If the current operation code is the default operation code, the operation code parser accesses the default function library to load the default operation function module corresponding to the default operation code; if the current operation code is a new operation code, the operation code parser transmits the name of the new operation code to the operation extension processor, and the operation extension processor calls the corresponding newly added function to obtain the operation code and operand containing the operation function, and then sends the operation code and operand to the execution engine; The running extension processor loads the new function dynamic library to call the new function, which specifically includes two parts: preprocessing and calling functions. The preprocessing is that the running extension processor saves the memory address and library name of the received new function dynamic library; the calling function is that the running extension processor loads the new function dynamic library according to the memory address and library name of the new function dynamic library, and calls the corresponding new function module; The execution engine executes the operation code and outputs the execution result.
5. The method according to claim 1, characterized in that: The new function mapping file consists of two parts: basic information of the new instruction and operation function, including: a. Basic information of new instructions: The basic information of the new instructions includes type, group number, instruction name and bytecode. The type indicates the category to which the new instruction belongs. The new instructions are divided into identification instructions and character instructions. The identification instructions guide the behavior of the contract through specific symbols or marks and are used to transmit control information. The character instructions are composed of letters and numbers and are used to execute specific business logic or data processing. The group number indicates the group to which the new instruction belongs. The instruction name indicates the name of the new instruction. The bytecode indicates the byte representation of the new instruction. The bytecode has a unique correspondence with the instruction name and cannot be repeated with the bytecode corresponding to the default instruction in the compiler. Users can support further classification of instructions according to functional characteristics by defining different group numbers; b. Operation function: The operation function of the new instruction refers to the instruction function executed during the contract operation, including the function name and the function body. The function name indicates the function name of the instruction, and the function body indicates the execution function corresponding to the instruction. It is divided into contract behavior control function and business logic processing function according to the instruction type.
6. The method according to claim 2, characterized in that: The contract parser performs lexical analysis, grammatical analysis, and semantic analysis on the smart contract in the form of a character stream in sequence, including: a. Lexical analysis: Scan the character stream, identify the basic elements of the contract, and convert them into tokens with grammatical meaning. New instructions will be processed as new, special identifiers; b. Syntax analysis: Parse the token stream according to the syntax rules to generate an abstract syntax tree. When encountering a new instruction, the contract parser will match the instruction with the relevant function or variable in the program, and add a new field identifier to the corresponding node of the abstract syntax tree to mark the target and context environment associated with the new instruction operation; c. Semantic analysis: Based on the information in the abstract syntax tree, check whether the contract complies with the semantic rules. The instruction processor will add the identifier corresponding to the new instruction to the semantic information table. At the same time, the contract parser will ensure that the relationship and logic between the various elements of the contract are correct based on the context information in the contract semantic information table.
7. The method according to claim 2, characterized in that: The generation of the contract intermediate expression by the instruction processor includes: a. Instruction analysis: determine the instructions to be used according to the semantic information table and the abstract syntax tree, and analyze the instructions to determine whether the instructions are default instructions or new instructions; b. Retrieve the default instruction: If the current instruction is a default instruction, the instruction processor accesses the default instruction table to retrieve detailed information related to the current instruction; c. Retrieve new instruction: If the current instruction is a new instruction, the instruction processor accesses the new instruction execution table and retrieves the relevant information of the new instruction. Specifically, the entry corresponding to the name of the new instruction is searched in the new instruction execution table to obtain the bytecode of the instruction.
8. The method according to claim 3, characterized in that: The new function processor performs operating environment processing and mapping file monitoring, including: a. Runtime environment processing: The new function processor sends the memory address and library name of the new function dynamic library to the run extension processor, so that the opcode parser can correctly call the function module in the new function dynamic library through the run extension processor when calling the new function; b. Mapping file monitoring: Monitor the new function mapping file according to user needs, and continue monitoring at the set interval. If the monitoring finds that there are new instructions that have not been loaded, regenerate a new function dynamic library containing all new instructions, and resend the memory address and library name of the new function dynamic library to the running extension processor.
Citation Information
Patent Citations
Highly-available contract executing method and system
CN107844294A
Data processing method and system based on blockchain smart contract
CN111782338A
Vulnerability detection method for binary code of intelligent contract
CN113051574A
Intelligent contract one-stop development system and method
CN113254054A
Block chain intelligent contract threat detection device and method
CN115022026A