Method and device for generating smart contracts for testing blockchain services
By automatically generating smart contracts for blockchain services, it solves the problems of low efficiency and insufficient security of developers manually writing smart contracts, and achieves efficient, accurate testing and secure blockchain services.
Patent Information
- Application Number
- CN202011042154.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-28
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2040-10-18
AI Technical Summary
In blockchain services, it is inefficient for developers to write smart contracts with restricted codes to test service quality, and security cannot be guaranteed. There is a risk of malicious contracts affecting services.
By determining the contract restriction rules of the target blockchain service, the matching restriction code is automatically obtained from the code associated with the development language, and converted into the target smart contract according to the smart contract writing specifications, automatically generating smart contracts with restriction codes, reducing manual labor Writing and errors.
It improves the efficiency and accuracy of smart contract generation, reduces the risk of malicious contracts, and improves the security and testing efficiency of blockchain services.
Smart Images

Figure CN112148613B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of blockchain technology, and in particular to a method and device for generating a smart contract for testing blockchain services. Background Art
[0002] In blockchain technology, in order to prevent smart contracts from accessing resources in blockchain services without restriction, which may destroy the consistency consensus of nodes in blockchain services, smart contracts can be prohibited from accessing specific resources in blockchain services through restriction codes; therefore, before developing or releasing blockchain services, it is necessary to verify or test the service quality of blockchain services for smart contracts with restriction codes. In the process of verifying or testing the above blockchain services, developers need to write a large number of smart contracts with restriction codes to test blockchain services. However, the generation of smart contracts with restriction codes is time-consuming and inefficient, which leads to low efficiency in testing the service quality of blockchain services for smart contracts with restriction codes. Summary of the invention
[0003] The embodiments of the present application provide a method and device for generating a smart contract for testing blockchain services, which are used to improve the generation efficiency of smart contracts with restriction codes.
[0004] In a first aspect, the present application provides a method for generating a smart contract for testing a blockchain service, comprising:
[0005] Determine the contract restriction rules corresponding to the target blockchain service; the contract restriction rules include information prohibiting the use of target functions when implementing the target smart contract through the target development language, and the target smart contract is used to test the target blockchain service;
[0006] Acquire a restriction code matching the contract restriction rule from the code associated with the target development language;
[0007] According to the smart contract writing specification corresponding to the target blockchain service, the restriction code is converted into the target smart contract.
[0008] In a second aspect of the present application, a device for generating a smart contract for testing a blockchain service is provided, comprising:
[0009] A restriction rule determination unit, used to determine the contract restriction rules corresponding to the target blockchain service; the contract restriction rules include information prohibiting the use of target functions when implementing the target smart contract through the target development language, and the target smart contract is used to test the target blockchain service;
[0010] A restriction code acquisition unit, configured to acquire a restriction code matching the contract restriction rule from a code associated with the target development language;
[0011] The contract generation unit is used to convert the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service.
[0012] In a possible implementation process, the contract generation unit is specifically used to:
[0013] Performing a preprocessing operation on the restriction code to obtain a test code, wherein the preprocessing operation includes removing abnormal codes from the restriction code;
[0014] Compiling and running the test code to determine the test code that has been compiled and run successfully;
[0015] According to the smart contract writing specification corresponding to the target blockchain service, the determined test code is converted into the target smart contract.
[0016] In a possible implementation manner, the restriction code obtaining unit is specifically used to:
[0017] determining a target website associated with the target development language;
[0018] The restriction code matching the contract restriction rule is captured from the sample code of the target website by using a code capture tool.
[0019] In a possible implementation, the contract restriction rule includes at least one of the following restriction information:
[0020] The syntax of the target development language, corresponding to the prohibition of using the target function;
[0021] Code libraries that use the target functionality are prohibited.
[0022] In a possible implementation, the contract generation unit is further used to:
[0023] Publishing the target smart contract to the target blockchain service;
[0024] Determine the service quality of the target blockchain service for the restriction code according to the execution result of the target smart contract.
[0025] In a third aspect of the present application, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in the first aspect and any possible implementation manner is implemented.
[0026] In a fourth aspect of the present application, a computer program product is provided, the computer program product comprising computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method provided in various possible implementations of the first aspect above.
[0027] In a fifth aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions. When the computer instructions are executed on a computer, the computer executes the method described in the first aspect and any possible implementation manner.
[0028] Since the embodiment of the present application adopts the above technical solution, it has at least the following technical effects:
[0029] On the one hand, in the embodiment of the present application, according to the smart contract writing specification, the restriction code matching the contract restriction rule is automatically converted into the target smart contract, that is, the target smart contract with the restriction code is automatically generated, and the developer does not need to analyze and construct the target smart contract, which reduces the time for generating the target smart contract and improves the efficiency of generating the target smart contract. It can further improve the efficiency of testing the service quality of the blockchain service for the target smart contract;
[0030] On the other hand, the target smart contract in the embodiment of the present application is obtained by converting the acquired restriction code, which reduces the errors and omissions in the developer's manual writing of the target smart contract, improves the accuracy of the generated target smart contract, and thus can improve the accuracy of the service quality of the test blockchain service for the target smart contract; and the process of generating the target smart contract is completed automatically, avoiding the phenomenon that a malicious target smart contract is generated, thereby the malicious target smart contract affects the normal operation of the blockchain service, thereby improving the security of the blockchain service.
[0031] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 A schematic diagram of an application environment for a smart contract testing method provided in an embodiment of the present application;
[0033] Figure 2 An example diagram of a blockchain service configuration provided in an embodiment of the present application;
[0034] Figure 3 A schematic diagram of a method for generating a smart contract for testing a blockchain service provided in an embodiment of the present application;
[0035] Figure 4 A schematic diagram of a complete process of generating a target smart contract provided in an embodiment of the present application;
[0036] Figure 5 An example diagram of a test process for testing a target blockchain service using a target smart contract provided in an embodiment of the present application;
[0037] Figure 6 A schematic diagram of the structure of a device for generating a smart contract for testing blockchain services provided in an embodiment of the present application;
[0038] Figure 7 A structural diagram of a terminal device provided in an embodiment of the present application;
[0039] Figure 8 A structural diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0040] In order to make the purpose, technical solution and beneficial effects of this application clearer, the technical solution in the embodiment of this application will be clearly and completely described below in conjunction with the drawings in the embodiment of this application. Obviously, the described embodiment is only a part of the embodiment of this application, not all the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0041] The terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in sequences other than those illustrated or described herein.
[0042] Some of the terms used in the embodiments of the present application are explained below to facilitate understanding by those skilled in the art.
[0043] 1. Target blockchain services
[0044] The blockchain service to be tested is also called blockchain or blockchain technology.
[0045] 2. Smart Contracts
[0046] A smart contract is a computer protocol designed to communicate, verify or execute contracts in an information-based manner. Smart contracts allow for trusted transactions without a third party, which are traceable and irreversible. The purpose of smart contracts is to provide a security method that is superior to traditional contracts and reduce transaction costs.
[0047] 3. Target Smart Contract
[0048] The target smart contract in the embodiment of the present application refers to a smart contract with a restriction code, and the target smart contract is used to test the blockchain service (target blockchain service) corresponding to the target smart contract.
[0049] 4. Restriction Code
[0050] The restriction code in the embodiment of the present application is a code used to prohibit the execution of a smart contract, that is, the restriction code in the target smart contract will prohibit the execution of the target smart contract to prevent the execution of the target smart contract from affecting the overall consensus mechanism of the blockchain service.
[0051] 5. Contractual restrictions
[0052] The contract restriction rules in the embodiment of the present application include information prohibiting the use of target functions when the target smart contract is implemented in the target blockchain service through the target development language; the above-mentioned target development language refers to the programming language for implementing the smart contract, such as but not limited to at least one programming language including C language, C++ language, Python, Java, etc.
[0053] 6. Blockchain Virtual Machine
[0054] The blockchain virtual machine is used to identify and run smart contracts in blockchain services. The blockchain virtual machine is essentially a code operating environment, thereby ensuring the consistent consensus of nodes in the blockchain service. For example, the Ethereum virtual machine EVM of the Ethereum blockchain, the Ethereum virtual machine EVM is the operating environment for smart contracts in the Ethereum blockchain.
[0055] 7. Smart contract writing specifications
[0056] Smart contract writing specifications refer to specifications (or guidelines) for writing smart contracts used in blockchain services. In the embodiments of the present application, the smart contract writing rules for different blockchain services may be different, and the smart contract writing specifications may be flexibly set in advance according to the characteristics or functions of the corresponding blockchain services; for example, the content of the smart contract writing specifications may include but is not limited to the need to deploy functions or destructors in the smart contract, or indicate the requirements for naming methods (or functions) in the smart contract. For example, the smart contract writing specifications in the Ethereum blockchain may include: the compiler version number needs to be specified in the first line of code of the smart contract, and the smart contract code is declared and defined in the Contract structure, etc.
[0057] 8. Consensus
[0058] Consensus is a process in blockchain services that is used to reach agreement on transactions in a block among multiple nodes involved. The agreed-upon blocks will be appended to the end of the blockchain. Mechanisms for achieving consensus include Proof of Work (PoW), Proof of Stake (PoS), Delegated Proof-of-Stake (DPoS), Proof of Elapsed Time (PoET), etc.
[0059] 9. Trading
[0060] A transaction, also called a transaction request, is equivalent to a computer term transaction. A transaction includes operations that need to be submitted to the blockchain service for execution, as well as the corresponding transaction results. It does not refer only to transactions in a business context. In view of the conventional use of the term "transaction" in blockchain technology, the embodiments of the present invention follow this convention; for example, a transaction may include a deployment transaction and an invoke transaction. The deployment transaction is used to deploy the smart contract to the node of the blockchain service and prepare it to be invoked; the invoke transaction is used to query the state database in the ledger (i.e., read operation) or update the state database (i.e., write operation, including adding, deleting, and modifying).
[0061] The design concept of this application is described below.
[0062] The smart contracts in the blockchain service are implemented through programming languages. The codes corresponding to the programming languages can access all resources on the server (such as CPU, memory, file system, network, etc.). However, for blockchain services, if smart contracts access resources in blockchain services without restriction, it will seriously affect the security of blockchain services and even destroy the consistency consensus of nodes in blockchain services. Therefore, restriction codes can be added to smart contracts to prohibit smart contracts from accessing specific resources in blockchain services through restriction codes, such as prohibiting smart contracts from accessing certain file systems and network resources or prohibiting the opening of concurrent mechanisms through restriction codes; when testing blockchain services, it is necessary to test the service quality of blockchain services for smart contracts with restriction codes. Currently, smart contracts with restriction codes used for testing are written by developers. The efficiency of generating the above smart contracts is low and the behavior of developers cannot be restricted. Malicious developers may write malicious smart contracts, which may cause the blockchain service to be unavailable when malicious smart contracts test blockchain services.
[0063] In view of this, the inventor has designed a method and device for generating smart contracts for testing blockchain services. Considering that the efficiency of developers writing smart contracts with restriction codes is low when testing the service quality of blockchain services for restriction codes, and the security of the written smart contracts with restriction codes cannot be guaranteed, for this reason, in the embodiment of the present application, based on the contract restriction rules, the restriction code is automatically obtained from the code associated with the development language of the smart contract, and the restriction code is converted into a target smart contract based on the smart contract writing specification, so as to improve the efficiency of generating the target smart contract and avoid developers from writing malicious target smart contracts.
[0064] See also Figure 1 , an application environment diagram of a smart contract testing method is provided, the application environment includes a test request node 110 and a blockchain service 120, and the same blockchain service 120 may include but is not limited to at least one blockchain node (such as may include but is not limited to blockchain node 120-1, blockchain node 120-2, blockchain node 120-3 or blockchain node 120-4 in the figure) 1, blockchain node 2, blockchain node 3 and blockchain node 4, wherein the blockchain node is a data processing node in the blockchain network, and the blockchain service is the carrier and organizational method for running blockchain technology.
[0065] Each blockchain node may be deployed with a target smart contract that needs to be tested. When the target smart contract needs to be tested, the test request node 110 (such as but not limited to the terminal device 110-1, the terminal device 110-2 or the server 110-3 in the figure) sends a test contract call message to the blockchain service 120. The test contract call message carries the contract identification information of the target smart contract under test. The blockchain node can identify the corresponding target smart contract according to the test contract call message, and execute the call method of the target smart contract to obtain the contract test result, and send the contract test result to the test request node 110. It can be understood that the contract identification information of the above-mentioned target smart contract can be but not limited to the account address of the target smart contract.
[0066] It should be noted that the above-mentioned test request node 110 may also be a server or a blockchain node in the above-mentioned blockchain service, or a blockchain node in other blockchain services, etc.
[0067] The above-mentioned blockchain nodes can be connected to each other through the network for consensus authentication, and the test request node and the blockchain node can be connected through the network. Each blockchain node can be an independent physical server or terminal, or a server cluster composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud servers, cloud databases, cloud storage, and CDN. The test request node can be an independent physical server or terminal device, or a server cluster composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud servers, cloud databases, cloud storage, and CDN.
[0068] The terminal device in the embodiments of the present application can be a mobile terminal, a fixed terminal or a portable terminal, such as a mobile phone, a station, a unit, a device, a multimedia computer, a multimedia tablet, an Internet node, a communicator, a desktop computer, a laptop computer, a notebook computer, a netbook computer, a tablet computer, a personal communication system (PCS) device, a personal navigation device, a personal digital assistant (PDA), an audio / video player, a digital camera / camcorder, a positioning device, a television receiver, a radio broadcast receiver, an e-book device, a gaming device or any combination thereof, including accessories and peripherals of these devices or any combination thereof.
[0069] In some embodiments, the process of testing the target smart contract may be executed in a private blockchain, for example, in a test environment, so there may be only one blockchain node. The target smart contract may be deployed in the blockchain node, for example, the smart contract may be tested through web.3j of Ethereum; web.3j is an external API (Application Programming Interface) provided by Ethereum.
[0070] See also Figure 2 , providing a schematic diagram of a possible blockchain service composition. Blockchain service (also known as blockchain) is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithm. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the blockchain underlying platform, platform product service layer, and application service layer.
[0071] The underlying blockchain platform can include processing modules such as user management, basic services, smart contracts, and operation monitoring. Among them, the user management module is responsible for the identity information management of all blockchain participants, including maintaining public and private key generation (account management), key management, and the corresponding relationship between the user's real identity and the blockchain address (authority management), etc., and, under authorization, supervises and audits the transactions of certain real identities and provides risk control rule configuration (risk control audit); the basic service module is deployed on all blockchain node devices to verify the validity of business requests, and records valid requests to storage after consensus is reached. For a new business request, the basic service first performs interface adaptation analysis and authentication processing (interface adaptation), and then encrypts the business information through the consensus algorithm (consensus management), and then encrypts the business information. After encryption, it is transmitted completely and consistently to the shared ledger (network communication) and recorded and stored; the smart contract module is responsible for the registration and issuance of contracts, as well as contract triggering and contract execution. Developers can define the contract logic of smart contracts in a certain programming language and publish it to the blockchain (contract registration). According to the logic of the contract terms, the key or other events are called to trigger execution to complete the contract logic of the smart contract. At the same time, it also provides the function of contract upgrade and cancellation; the operation monitoring module is mainly responsible for the deployment, configuration modification, contract setting, cloud adaptation and real-time status visualization output of the product during the product release process, such as alarm, monitoring network conditions, monitoring node equipment health status, etc.
[0072] The platform product service layer provides the basic capabilities and implementation framework of typical applications. Developers can superimpose business features based on these basic capabilities to complete the blockchain implementation of business logic. The application service layer provides application services based on blockchain solutions for business participants to use.
[0073] The following describes the text classification method provided by the exemplary embodiment of the present application in combination with the application scenarios described above and with reference to the accompanying drawings. It should be noted that the above application scenarios are only shown to facilitate understanding of the spirit and principles of the present application, and the implementation of the present application is not limited in this respect.
[0074] An embodiment of the present application provides a method for generating a smart contract for testing a blockchain service. The method can be executed by a blockchain node in the above-mentioned blockchain service, or by the above-mentioned test request node, or by a separate electronic device for generating a smart contract. The electronic device can be an independent physical server or terminal device, or a server cluster composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud servers, cloud databases, cloud storage, and CDN, etc.
[0075] See also Figure 3 , the above-mentioned method for generating a smart contract for testing blockchain services specifically includes the following steps:
[0076] Step S301, determine the contract restriction rules corresponding to the target blockchain service to be tested.
[0077] Specifically, in the embodiments of the present application, the contract restriction rules corresponding to the target blockchain can be determined in advance based on factors such as the service characteristics, type, and attribute characteristics of the target blockchain service, but are not limited to such factors. Technical personnel in this field can flexibly set the contract restriction rules corresponding to each blockchain service according to actual needs.
[0078] Step S302, obtaining a restriction code matching the above-mentioned contract restriction rule from the code associated with the target development language that implements the target smart contract, wherein the target smart contract is used to test the target blockchain service.
[0079] In order to improve the matching degree between the obtained restriction code and the contract restriction rules, in an embodiment of the present application, the restriction code can be obtained according to the code keyword corresponding to the contract restriction rule in the target development language. Specifically, the code keyword corresponding to the above-mentioned contract restriction rule in the target development language can be determined, and then the code matching the code keyword can be obtained from the code associated with the above-mentioned target development language as the restriction code.
[0080] As an embodiment, in order to improve the efficiency of obtaining restriction codes, a code crawler tool can be used in the embodiment of the present application to crawl restriction codes from the code associated with the target development language. Specifically, the target website associated with the above-mentioned target development language can be determined; through the code crawler tool, the restriction code matching the above-mentioned contract restriction rules can be crawled from the sample code of the above-mentioned target website.
[0081] The target website may include, but is not limited to, at least one of a learning network, a technical exchange website, a question-and-answer website, or other types of websites related to the target development language; the code crawling tool may include, but is not limited to, a crawler tool.
[0082] Step S303: Convert the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service.
[0083] As an embodiment, considering that the acquired restriction code may have data missing, code errors, etc., which may lead to the inability to compile and run, in order to improve the efficiency of generating the target smart contract using the acquired restriction code, in the embodiment of the present application, when converting the restriction code into the target smart contract, the acquired abnormal code can be preprocessed to obtain the test code, and the test code is compiled and run to determine the test code that is compiled and run successfully; then, according to the smart contract writing specifications corresponding to the target blockchain service, the test code that is compiled and run successfully is converted into the target smart contract; the above-mentioned preprocessing operation includes the operation of removing the abnormal code in the above-mentioned restriction code.
[0084] The above-mentioned abnormal code may include but is not limited to at least one of the following restriction codes: an annotated restriction code, a restriction code with obvious missing information, a restriction code with inconsistent variable names, etc.
[0085] The following is a further explanation of the contract restriction rules in the embodiments of the present application.
[0086] In the embodiments of the present application, the contract restriction rules of different blockchain services may be different or the same. Please refer to Table 1, which provides a schematic diagram of the comparison relationship between blockchain services and contract restriction rules.
[0087] Table 1: Comparison between blockchain services and contract restriction rules
[0088] Blockchain Service A1 Contractual Restrictions Rule B1 Blockchain Service A2 Contractual Limitation Rule B2 Blockchain Service A3 Contractual Limitation Rule B3 Blockchain Service A4 Contractual Restrictions Rule B1 Blockchain Service A5 Contractual Restrictions B4
[0089] For the same blockchain service, only one target development language may be allowed to be used to write the target smart contract deployed to the blockchain service, or multiple target development languages may be allowed to be used to write the target smart contract deployed to the blockchain service; when the same blockchain service allows the use of multiple target development languages to write the target smart contract, different target development language settings may correspond to different contract restriction rules, or may correspond to the same contract restriction rules; please refer to Table 2, which provides a schematic diagram of the comparison relationship between different target development languages and contract restriction rules in the same blockchain service.
[0090] Table 2: Comparison between different target development languages and contract restriction rules in the same blockchain service
[0091] Target development language C1 Contractual Restriction Rules D1 Target development language C2 Contractual Limitation Rules D2 Target development language C3 Contractual Limitation Rules D3
[0092] As an embodiment, different blockchain services have different target functions allowed for use and different target functions prohibited for use. The situations of the target functions prohibited for use in the syntax of different target development languages and the code function libraries prohibited for access are different. Therefore, in the embodiments of the present application, the contract restriction rules corresponding to the target blockchain service can be set according to the correspondence between the syntax of the target development language and the target functions and the code function library prohibited for access.
[0093] Specifically, the contract restriction rules in the embodiments of the present application may include, but are not limited to, at least one of the following restriction information:
[0094] The first type of restriction information: the syntax of the target development language that corresponds to the syntax that prohibits the use of the target function.
[0095] For example, when the target function is to import or export data of a set type, the restriction information may be the syntax in the target development language that prohibits importing and exporting data of a set type. For example, when the target blockchain service is the Ethereum EOS blockchain service and the target development language of the target smart contract is C++, data of data types such as unint64_t, float, or double are not allowed. Therefore, the restriction information may be the prohibition of importing or exporting data of data types such as the above-mentioned 64-bit integer unint64_t, floating point float, or Boolean double. The restriction information may also be the syntax in the target development language that prohibits file operations, network operations, multi-threaded and multi-process operations, etc.
[0096] The second type of restriction information: code function libraries that prohibit the use of the above target functions.
[0097] The above-mentioned code function libraries prohibited from access by different target development languages may be the same or different, and the code function libraries prohibited from access by the target development languages of target smart contracts in different blockchain services may be the same or different.
[0098] If the target development language allows access to and use of code function libraries related to string processing, mathematical formula calculations, etc., but does not allow use of code function libraries related to file operations, network operations, and multi-process and multi-threaded operations, you can set a prohibition on the use of code function libraries related to file operation libraries, network operation libraries, and multi-process and multi-threaded operation libraries in the restriction information.
[0099] See also Figure 4 In the embodiment of the present application, an example of a complete process for generating a target smart contract is provided, and the process specifically includes the following steps:
[0100] Step S401, determining the contract restriction rules corresponding to the target blockchain service to be tested.
[0101] Step S402: Using a crawler tool, crawl sample code from a target website associated with a target development language that implements a target smart contract.
[0102] Here, learning website 1, learning website 2, question-and-answer website 1, and other websites are taken as examples for description as target websites associated with the target development language.
[0103] Step S403: extract the restriction code matching the above-mentioned contract restriction rule from the captured sample code.
[0104] Here, example codes 1, 2, 3 and 4 are taken as examples to illustrate the extracted restriction codes.
[0105] The specific method of extracting the restriction code can be found in the above description, which will not be repeated here.
[0106] Step S404, preprocessing the restriction code to remove abnormal codes in the restriction code to obtain the test code.
[0107] Step S405 , compile and run the test code through a compiler, and determine the test code that is compiled and successfully run as a valid code.
[0108] Step S406, according to the smart contract writing specification corresponding to the target blockchain service, use a converter to convert the valid code into the target smart contract.
[0109] It should be noted that in the embodiments of the present application, the sample codes captured from the same website are not necessarily converted into the same target smart contract, and the restriction codes in the same target smart contract are not necessarily captured from the same network; and a target smart contract can be converted from one or more valid codes. The specific method of converting different valid codes into a target smart contract can be set by technicians in this field according to actual needs, and no excessive restrictions are made here.
[0110] As an embodiment, in order to verify the service quality of the target blockchain service for the restricted code, after step S303, it is also possible to Figure 1 The illustrated test scenario tests the automatically generated target smart contract; specifically, the generated target smart contract can be published to the target blockchain service; and the service quality of the target blockchain service for the restricted code is determined according to the execution result of the target smart contract.
[0111] Specifically, when the target smart contract is requested to be called by the test request node, the blockchain virtual machine in the blockchain service can identify the target smart contract and determine the execution result of the target smart contract, and feed back the execution result of the target smart contract to the test request node, which determines the service quality of the target blockchain service for the restricted code.
[0112] The execution result of the above-mentioned target smart contract may include but is not limited to non-execution, normal execution, etc. If the blockchain virtual machine identifies the target smart contract and determines that the restriction code in the target smart contract prohibits the execution of the target smart contract, it will determine an error code for the target smart contract and send the error code to the test request node; if the target smart contract is executed normally, the blockchain virtual machine will send the normal execution result value of the normal execution of the target smart contract to the test request node.
[0113] After the test request node receives the error code, it determines that the requested target smart contract is not executed, the restriction code in the target smart contract is effective in the target blockchain service, and the service quality of the target blockchain service for the target smart contract is normal; after the test request node receives the normal execution result value, it determines that the requested target smart contract is executed normally, the restriction code in the target smart contract is not effective in the target blockchain service, and the service quality of the target blockchain service for the target smart contract is abnormal.
[0114] Furthermore, the test request node can send contract call messages for multiple target smart contracts, requesting to call multiple target smart contracts, and determine whether the restriction codes of each requested target smart contract are effective based on the error code or normal execution result value returned by the blockchain virtual machine, and then determine the service quality of the target blockchain service for the restriction code based on the ratio of the number of effective restriction codes to the total number of requested target smart contracts, so as to improve the target blockchain service according to the determined service quality or perform subsequent processing on the target blockchain service.
[0115] See also Figure 5 , provides an example diagram of the process of using the target smart contract to test the target blockchain service, the testing process includes the following steps:
[0116] Step S501: The target blockchain service deploys the generated target smart contract to the target blockchain service.
[0117] As shown in the figure, here we take the deployment of target smart contract 1, target smart contract 2, target smart contract 3 to target smart contract n (n is a positive integer) to the target blockchain service as an example for explanation.
[0118] Step S502: The test request node sends a contract call message of the target smart contract.
[0119] Specifically, the contract call message may include the contract identification information of the target smart contract.
[0120] Step S503: After receiving the contract call message, the target blockchain service calls the corresponding target smart contract.
[0121] Specifically, the target blockchain service can identify and call the target smart contract corresponding to the contract identification information based on the contract identification information in the contract call message.
[0122] Step S504: The target blockchain service determines the execution result of the called target smart contract.
[0123] Step S505, the target blockchain service sends the execution result of the target smart contract called in S505 to the test requesting node.
[0124] Among them, when the target blockchain service determines that the target smart contract is not executed, it sends the error code corresponding to the called target smart contract to the test request node; when the target blockchain service determines that the target smart contract is executed normally, it sends the normal execution result value corresponding to the called target smart contract to the test request node.
[0125] Step S506: The test requesting node determines the service quality of the target blockchain service for the restricted code based on the execution result of the target smart contract.
[0126] It should be noted that the process from step S503 to step S505 described above can be, but is not limited to, implemented by a blockchain virtual machine in the target blockchain service.
[0127] In the embodiment of the present application, by setting the contract restriction rules corresponding to the target blockchain service, the captured code can be automatically converted into a target smart contract for testing the target blockchain service. The target smart contract is automatically generated without manual analysis and construction, which reduces the cost of access and writing, and reduces the time spent on generating the target smart contract. Therefore, when the target blockchain service is tested with the automatically generated target smart contract, the test efficiency can be significantly improved; and the target smart contract in the embodiment of the present application is automatically generated, which avoids errors or omissions caused by manual writing, improves the accuracy of the target smart contract, and effectively ensures the effectiveness of the test.
[0128] Please refer to Figure 6 Based on the same inventive concept, the embodiment of the present application provides a device 600 for generating a smart contract for testing a blockchain service, comprising:
[0129] A restriction rule determination unit 601 is used to determine the contract restriction rules corresponding to the target blockchain service; the contract restriction rules include information prohibiting the use of target functions when implementing the target smart contract through the target development language, and the target smart contract is used to test the target blockchain service;
[0130] A restriction code acquisition unit 602 is used to acquire a restriction code matching the contract restriction rule from the code associated with the target development language;
[0131] The contract generation unit 603 is used to convert the above restriction code into the above target smart contract according to the smart contract writing specification corresponding to the above target blockchain service.
[0132] As an embodiment, the restriction code acquisition unit 602 is specifically used to: determine the code keyword corresponding to the contract restriction rule in the target development language; and obtain the code matching the code keyword as the restriction code from the code associated with the target development language.
[0133] As an embodiment, the contract generation unit 603 yuan is specifically used to perform a preprocessing operation on the restriction code to obtain a test code, and the preprocessing operation includes removing abnormal codes from the restriction code;
[0134] Compile and run the test code to determine the test code that has been successfully compiled and run.
[0135] According to the smart contract writing specifications corresponding to the above target blockchain service, the determined test code is converted into the above target smart contract.
[0136] As an embodiment, the restriction code acquisition unit 602 is specifically used to: determine a target website associated with the target development language; and capture the restriction code matching the contract restriction rule from the sample code of the target website using a code capture tool.
[0137] As an embodiment, the above-mentioned contract restriction rule includes at least one of the following restriction information:
[0138] The syntax of the target development language that corresponds to the prohibition of using the target function;
[0139] Code libraries that use the above target functions are prohibited.
[0140] As an embodiment, the contract generation unit 603 is also used to publish the above-mentioned target smart contract to the above-mentioned target blockchain service; according to the execution result of the above-mentioned target smart contract, determine the service quality of the above-mentioned target blockchain service for the above-mentioned restriction code.
[0141] As an example, Figure 6 The device in can be used to implement any of the methods for generating smart contracts for testing blockchain services discussed above.
[0142] Based on the same inventive concept, the generating device 600 provided in the embodiment of the present application is an example of a hardware entity. Figure 7 The terminal device 700 is shown and the terminal device 700 is introduced below.
[0143] Please refer to Figure 7 The terminal device includes a display unit 740, a processor 780 and a memory 720, wherein the display unit 740 includes a display panel 741, which is used to display information input by a user or information provided to a user with various operation pages, etc. In the embodiment of the present application, it is mainly used to display pages, shortcut windows, etc. of the client installed in the terminal device.
[0144] Optionally, the display panel 741 may be configured in the form of a liquid crystal display (LCD) or an organic light-emitting diode (OLED).
[0145] The processor 780 is used to read the computer program and then execute the method defined by the computer program. For example, the processor 780 reads various applications, so as to run the application on the terminal device and display the application page on the display unit 740. The processor 780 may include one or more general-purpose processors and may also include one or more DSPs (Digital Signal Processors) to perform related operations to implement the technical solutions provided in the embodiments of the present application.
[0146] The memory 720 generally includes internal memory and external memory, and the internal memory can be a random access memory (RAM), a read-only memory (ROM), and a cache (CACHE), etc. The external memory can be a hard disk, an optical disk, a USB disk, a floppy disk or a tape drive, etc. The memory 720 is used to store computer programs and other data, and the computer program includes an application corresponding to the client, etc. Other data may include data generated after the operating system or the application is run, and the data includes system data (such as configuration parameters of the operating system) and user data. In the embodiment of the present application, program instructions are stored in the memory 720, and the processor 780 executes the program instructions in the memory 720 to implement any one of the target function determination methods discussed in the previous figure.
[0147] In addition, the terminal device may also include a display unit 740 for receiving input digital information, character information or contact touch operation or non-contact gesture, and generating signal input related to user settings and function control of the terminal device. Specifically, in the embodiment of the present application, the display unit 740 may include a display panel 741. The display panel 741 is, for example, a touch screen, which can collect the user's touch operation on or near it (such as the user's operation on the display panel 741 or on the display panel 741 using any suitable object or accessory such as a finger, stylus, etc.), and drive the corresponding connection device according to a pre-set program. Optionally, the display panel 741 may include two parts: a touch detection device and a touch controller. Among them, the touch detection device detects the player's touch orientation, detects the signal brought by the touch operation, and transmits the signal to the touch controller; the touch controller receives the touch information from the touch detection device, converts it into contact point coordinates, and then sends it to the processor 780, and can receive and execute commands sent by the processor 780. In an embodiment of the present application, if a user clicks on the detected client 110, the touch detection device in the display panel 741 detects a touch operation, and sends a signal corresponding to the detected touch operation to the touch controller. The touch controller converts the signal into touch point coordinates and sends them to the processor 780. The processor 780 performs subsequent operations based on the received touch point coordinates.
[0148] The display panel 741 may be implemented in various types such as resistive, capacitive, infrared, and surface acoustic wave. In addition to the display unit 740, the terminal device 700 may further include an input unit 730, which may include, but is not limited to, an image input device 731 and other input devices 732, such as a physical keyboard, a function key (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.
[0149] In addition to the above, the terminal device 700 may also include a power supply 790 for supplying power to other modules, an audio circuit 760, a near field communication module 770, and an RF circuit. The terminal device 700 may also include one or more sensors 750, such as an acceleration sensor, a light sensor, a pressure sensor, etc. The audio circuit 760 specifically includes a speaker 761 and a microphone 762, etc. For example, the terminal device 700 can collect the user's voice through the microphone 762 and perform corresponding operations.
[0150] As an embodiment, the number of processors 780 may be one or more, and the processor 780 and the memory 720 may be coupled or relatively independently configured.
[0151] As an example, Figure 7 The processor 780 in the embodiment may be used to implement the following Figure 6 The functions of the restriction rule determination unit 601, the restriction code acquisition unit 602 and the contract generation unit 603 are implemented.
[0152] The above generating device 600 is an example of a hardware entity. Figure 8 The computer device shown includes a processor 801 , a storage medium 802 , and at least one external communication interface 803 ; the processor 801 , the storage medium 802 , and the external communication interface 803 are all connected via a bus 804 .
[0153] The storage medium 802 stores a computer program;
[0154] When the processor 801 executes the computer program, a method for generating a smart contract for testing a blockchain service discussed above is implemented.
[0155] Figure 8 In the figure, one processor 801 is taken as an example, but the number of processors 801 is not limited in practice.
[0156] The storage medium 802 may be a volatile memory, such as a random-access memory (RAM); the storage medium 802 may also be a non-volatile memory, such as a read-only storage medium, a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD), or the storage medium 802 may be any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The storage medium 802 may be a combination of the above storage media.
[0157] Those skilled in the art can understand that: all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiments; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), disks or optical disks, and other media that can store program codes.
[0158] Alternatively, if the above-mentioned integrated unit of the invention is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present invention can be essentially or partly reflected in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the above-mentioned methods of each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROM, RAM, magnetic disks or optical disks.
[0159] Based on the same technical concept, the embodiment of the present application also provides a computer-readable storage medium, which stores computer instructions. When the above-mentioned computer instructions are executed on a computer, the computer executes a method for generating a smart contract for testing blockchain services as discussed above.
[0160] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0161] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for generating a smart contract for testing a blockchain service, characterized in that: include: Determine the contract restriction rules corresponding to the target blockchain service; the contract restriction rules include information prohibiting the use of target functions when implementing the target smart contract through the target development language, and the target smart contract is used to test the target blockchain service; Acquire a restriction code matching the contract restriction rule from the code associated with the target development language; The restriction code is a code used to prohibit the execution of the smart contract; Convert the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service; The contract restriction rules include at least one of the following restriction information: The syntax of the target development language, corresponding to the prohibition of using the target function; Code libraries that use the target functionality are prohibited.
2. The method according to claim 1, characterized in that The acquiring, from the code associated with the target development language, a restriction code matching the contract restriction rule, comprises: Determining code keywords corresponding to the contract restriction rules in the target development language; From the code associated with the target development language, a code matching the code keyword is obtained as the restriction code.
3. The method according to claim 1, characterized in that The step of converting the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service includes: Performing a preprocessing operation on the restriction code to obtain a test code, wherein the preprocessing operation includes removing abnormal codes from the restriction code; Compiling and running the test code to determine the test code that has been compiled and run successfully; According to the smart contract writing specification corresponding to the target blockchain service, the determined test code is converted into the target smart contract.
4. The method according to any one of claims 1 to 3, characterized in that: The acquiring, from the code associated with the target development language, a restriction code matching the contract restriction rule, comprises: determining a target website associated with the target development language; The restriction code matching the contract restriction rule is captured from the sample code of the target website by using a code capture tool.
5. The method according to any one of claims 1 to 3, characterized in that: After converting the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service, the method further includes: Publishing the target smart contract to the target blockchain service; Determine the service quality of the target blockchain service for the restriction code according to the execution result of the target smart contract.
6. A device for generating a smart contract for testing a blockchain service, characterized in that: include: A restriction rule determination unit, used to determine the contract restriction rules corresponding to the target blockchain service; the contract restriction rules include information prohibiting the use of target functions when implementing the target smart contract through the target development language, and the target smart contract is used to test the target blockchain service; A restriction code acquisition unit, configured to acquire a restriction code matching the contract restriction rule from a code associated with the target development language; The restriction code is a code used to prohibit the execution of the smart contract; A contract generation unit, configured to convert the restriction code into the target smart contract according to the smart contract writing specification corresponding to the target blockchain service; The contract restriction rules include at least one of the following restriction information: The syntax of the target development language, corresponding to the prohibition of using the target function; Code libraries that use the target functionality are prohibited.
7. The device according to claim 6, characterized in that The restriction code acquisition unit is specifically used for: Determining code keywords corresponding to the contract restriction rules in the target development language; From the code associated with the target development language, a code matching the code keyword is obtained as the restriction code.
8. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the method according to any one of claims 1 to 5 are implemented.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed on a computer, the computer is caused to execute the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Blockchain smart contract formal verification code converter and conversion method
CN108459860A
Block issuing method and apparatus, electronic apparatus
CN109242676A
