Method and system for realizing intelligent contract life cycle management based on external chain code containerTechnical Field
The invention relates to the technical field of block chains, in particular to a method and a system for realizing intelligent contract life cycle management based on an external chain code container.
Background
In a blockchain network, multiple organizations perform the logic associated with a business by executing intelligent contracts (i.e., a piece of chain code). Because one organization can join a plurality of channels, and when the organization and the organization perform transactions, the types of completed services also have a plurality of types, and a plurality of intelligent contracts are often deployed in the channels according to requirements, the management of the intelligent contracts is very important.
In the existing intelligent contract management technology, the life cycle of an intelligent contract is not complete enough, but in a block chain network deployed by using k8s, a chain code is always stored inside the network, and when the contract is installed, the contract is excessively dependent on contract construction logic hard-coded by an endorsement node, so that the contract is not flexible to update. How to make each intelligent contract have a very complete life cycle, realize the real-time management to the contract, improve the availability of the contract, finally achieve the goal that the organization in the channel finishes every business better, it is the problem that needs to be solved at present.
Disclosure of Invention
Based on the problems proposed in the background art, the invention provides a method for realizing intelligent contract life cycle management based on an external chain code container, which specifically comprises the following steps:
acquiring an intelligent contract proposal request initiated by a proposal initiating organization in a channel, and judging whether the proposal request is successfully initiated, wherein the intelligent contract proposal is the life cycle management of an intelligent contract;
if the proposal request is successfully initiated, initiating a voting notification to enable all organizations in the channel to vote for the proposal request;
and if the vote exceeds half, generating a proposed to-be-executed message and sending the proposed to-be-executed message to the BaaS terminal.
Further, the lifecycle management includes at least one or more of deployment, upgrade, freeze, unfreeze, version rollback, version advance, and parameter upgrade;
the deployment is to obtain a new intelligent contract and upload the new intelligent contract to a channel, and install the new intelligent contract at an endorsement node needing to deploy the new intelligent contract;
the upgrading is to obtain an upgraded intelligent contract and install the upgraded intelligent contract, wherein the upgraded intelligent contract is an existing intelligent contract in a channel;
the freezing is carried out, all the calls of the current intelligent contract are modified to return that the contract is frozen, the endorsement strategy of the current intelligent contract is replaced by the endorsement strategy which needs all organizations, the ID of the endorsement strategy is set to be null, the intelligent contract obtained after the modification is the frozen contract, and the frozen contract is installed;
the unfreezing is carried out, an original intelligent contract before freezing is obtained, and the original intelligent contract is installed;
the version is returned and advanced, the current intelligent contract has the intelligent contract of the previous version/the next version, and the intelligent contract which meets the conditions is selected for installation;
upgrading the parameters, namely modifying the endorsement strategy and the private data set corresponding to the current intelligent contract without changing the version number of the current intelligent contract to obtain the modified intelligent contract and installing the modified intelligent contract;
the method comprises the steps of upgrading, freezing, unfreezing, version rollback, version advancing and parameter upgrading, and when a related intelligent contract is installed, an endorsement node where a current intelligent contract is installed is selected to be installed.
Further, there is also a possibility of failure of proposal initiation, including the following cases:
when the proposal is the deployment of the intelligent contract, if the new intelligent contract and the existing intelligent contract in the channel are renamed, the proposal initiation fails;
when the proposal is upgrading, freezing, unfreezing, version backing, version advancing and parameter upgrading of the intelligent contract, if the current intelligent contract has the proposal in the process, the proposal initiation fails;
and setting the permission for redeploying the intelligent contract for certain specific organizations in the channel, and when the proposal initiation fails and is the deployment of the intelligent contract, if the proposal initiation organization has the permission for redeploying, initiating a request for redeploying, and deploying the intelligent contract again.
Further, the proposal information includes:
the deployment and proposal information at least comprises the name and version number of the new intelligent contract, the channel name, initialization parameters, endorsement strategies and private data;
the upgrading and proposal information at least comprises the name and the version number of the current intelligent contract, the name and the version number of the upgraded intelligent contract, the channel name, the endorsement policy and private data;
freezing, unfreezing and proposing information at least comprises the name, the version number and the channel name of the current intelligent contract;
the version is returned and advanced, and the proposal information at least comprises the name of the current intelligent contract, the version number of the intelligent contract of the previous/next version and the channel name;
the parameter upgrading, the proposal information at least includes the name, version number, channel name, endorsement strategy and private data of the current intelligent contract;
on the other hand, the method for realizing intelligent contract life cycle management based on the external chain code container comprises the following steps:
the BaaS end receives a proposed to-be-executed message;
installing related intelligent contracts in endorsement nodes selected by a proposal initiating organization;
configuring Stateful and Service for the external chain code container;
and after the external chain code container is normally started, the current intelligent contract ID of the organization where the endorsement node is located points to the installed related intelligent contract ID, and the related intelligent contract is instantiated.
Further, the intelligent contract runs on an external chain code container;
if the related intelligent contract is about the first installation, uploading an intelligent contract chain code packet before the installation, otherwise, not uploading; if the intelligent contract chain code package needs to be uploaded, in order to enable the installed intelligent contract to run in an external chain code container, the original intelligent contract chain code package needs to be decompressed, a metadata configuration file and a connection configuration file are generated based on an index and a set uploaded during intelligent contract creation, the metadata configuration file and the connection configuration file are repackaged together with the decompressed file, and the new intelligent contract chain code package is uploaded after being obtained.
Further, the installation of the related intelligent contracts further comprises:
after the intelligent contract chain code packet is uploaded, calling an intelligent contract installation method of an SDK interface to carry out intelligent contract installation, after an external chain code container is deployed, creating a Pod based on StateUfSet and Service, and writing a chain code number and a corresponding chain code Service address into an environment variable;
when the intelligent contract is installed and instantiated, if the related intelligent contract is not installed for the first time, the initialization function in the instantiation is replaced by a null implementation.
Furthermore, the method also provides intelligent contract detail inquiry, and can check the intelligent contract information deployed in each channel and the historical version information thereof; when the intelligent contract proposal is operated, the current version information and the original version information of the intelligent contract are saved, and the saved information at least comprises the following information: the name, version number, path, initialization parameter, endorsement policy, state, MD5 value, channel name, set configuration, proposal initiating organization, initiating time and submitting time of the intelligent contract.
The invention also provides a system for realizing intelligent contract life cycle management based on the external chain code container, which comprises a proposal processing module, a message module and a proposal execution module;
the proposal processing module comprises proposal initiation and voting;
acquiring an intelligent contract proposal request initiated by a proposal initiating organization in a channel, and judging whether the proposal request is initiated successfully; the intelligent contract proposal request comprises deployment, upgrading, freezing, unfreezing, version rollback, version advancing and parameter upgrading of an intelligent contract;
if the proposal request is successfully initiated, initiating a voting notification to enable all organizations in the channel to vote for the proposal request;
and if the vote exceeds half, generating a proposed to-be-executed message and sending the proposed to-be-executed message to the BaaS terminal.
The message module comprises the steps of generating and processing a message, generating the message by an intelligent contract end, and processing the message by a BaaS end;
the type of the message comprises a proposal initiating result, a proposal voting result and a proposal executing result; if the proposal is initiated completely, newly adding an initiating result message in a database; if the proposal voting is finished, newly adding voting result information in a database; if the proposal is executed, adding an execution result message in the database;
the proposal execution module executes the installation and instantiation operations of different intelligent contracts according to different types of proposals, wherein the related intelligent contracts run in an external chain code container.
The system further comprises a BaaS end, wherein the BaaS end comprises a message receiving module, a selective installation module, a configuration module and a pointing example module when the message is processed;
the message receiving module receives a proposed plan message to be executed;
the selection installation module is used for installing related intelligent contracts at endorsement nodes selected by a proposal initiating organization;
the configuration module is used for configuring Stateresult and Service for the external chain code container;
and the pointing instance module is used for pointing the current intelligent contract ID of the organization where the endorsement node is located to the installed related intelligent contract ID and instantiating the related intelligent contract after the external chain code container is normally started.
A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out the method steps of any of the above.
An apparatus for implementing intelligent contract lifecycle management based on external chain code containers, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, the processor implementing the method steps of any one of the above when executing the computer program.
For a better understanding of the present invention, certain terms of art referred to in this specification are explained below, including: the BaaS is a block chain, namely a service platform, provides a convenient and high-performance block chain ecological environment and ecological matching service for developers, and supports the business expansion and operation of the developers; the SDK is a software development kit, and is a set of related documents, paradigms and tools for assisting in developing certain software; k8s is an open source platform for automated container operation that can be used to deploy blockchain networks; pod is the smallest unit managed, created, planned in k8 s; the Stateful is designed for solving the service problem, and can realize stable persistent storage, stable network marks, ordered deployment, expansion, contraction, deletion and the like; service is an abstraction of a policy that defines a set of Pod, also called a macro Service; MD5 is a widely used cryptographic hash function that functions to ensure that information is transmitted in a complete and consistent manner.
Compared with the prior art, the invention provides a method for realizing intelligent contract life cycle management based on an external chain code container, when intelligent contract management is carried out and contracts are installed, the contracts are operated in the external chain code container, the contract construction logic of endorsement node hard coding is not relied on, and the high availability of chain codes is ensured. Meanwhile, the management of the intelligent contract is more perfect, the intelligent contract has a complete life cycle, and the intelligent contract comprises multiple operations of deployment, upgrading, freezing, unfreezing, version rollback, version forwarding and parameter upgrading of the intelligent contract, so that organizations in a channel can conveniently manage the contract in real time aiming at specific services, and the related logic of the services can be better completed.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings without creative efforts.
FIG. 1 is a schematic diagram illustrating an overall process flow of a smart contract lifecycle in one embodiment;
FIG. 2 is a schematic flow chart illustrating the execution of an intelligent contract proposal in one embodiment;
FIG. 3 is a schematic flow chart illustrating intelligent contract deployment in one embodiment;
FIG. 4 is a schematic diagram illustrating a process for intelligent contract upgrade in one embodiment;
FIG. 5 is a schematic flow chart illustrating intelligent contract freezing in one embodiment;
FIG. 6 is a schematic diagram illustrating a flow of intelligent contract unfreezing in one embodiment;
FIG. 7 is a flow diagram illustrating intelligent contract version rollback in an embodiment;
FIG. 8 is a schematic flow chart illustrating intelligent contract version progression in one embodiment;
fig. 9 is a schematic flow chart of upgrading intelligent contract parameters in a specific embodiment.
Detailed Description
To make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings, and it is apparent that the described embodiments are some, but not all embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In the description herein, references to the description of "an embodiment," "a particular embodiment," "an embodiment," "for example," mean that a particular feature, structure, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the application. In this specification, the schematic representations of the terms used above do not necessarily refer to the same embodiment or example. Furthermore, the particular features, structures, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. The sequence of steps involved in the embodiments is for illustrative purposes to illustrate the implementation of the present application, and the sequence of steps is not limited and can be adjusted as needed.
In a specific embodiment, as shown in fig. 1, a life cycle of an intelligent contract mainly includes deployment, upgrade, freezing, thawing, version rollback, version forwarding, and parameter upgrade of the intelligent contract, and a system back end thereof is logically divided into three modules including a proposal processing module, a message module, and a proposal execution module;
the proposal processing module comprises proposal initiation and voting; the message module comprises the generation and processing of messages; the proposal execution module executes the installation and instantiation operations of different intelligent contracts according to different types of proposals.
In this embodiment, as shown in fig. 2, the step of managing the intelligent contract specifically includes:
s1, acquiring an intelligent contract proposal request initiated by a proposal initiating organization in the channel;
s2, if the proposal initiating is successful, initiating a voting notification to make all organizations in the channel vote for the proposal request;
s3, if the vote exceeds half, generating a proposal to-be-executed message and sending the proposal to the BaaS terminal;
s4, executing proposal, and carrying out installation and instantiation operation of related intelligent contracts, wherein the related intelligent contracts run in an external chain code container;
s40, the BaaS end receives the proposed to-be-executed message; installing related intelligent contracts in endorsement nodes selected by a proposal initiating organization; configuring Stateful and Service for the external chain code container;
and S41, after the external chain code container is normally started, the current intelligent contract ID of the organization where the endorsement node is located points to the installed related intelligent contract ID, and the related intelligent contract is instantiated.
For all the passing operation intelligent contract proposals, the installation and instantiation operation of related intelligent contracts are required during the execution, the intelligent contracts are operated in an external chain code container, the influence of an isolated container on the overall scheduling under the k8s environment is reduced, the contract construction logic hard-coded by an endorsement node is not relied on, and the high availability of the chain code is ensured, and the method mainly comprises the following steps:
if the related intelligent contract is about the first installation, uploading an intelligent contract chain code packet before the installation, otherwise, not uploading; if the intelligent contract chain code package needs to be uploaded, in order to enable the installed intelligent contract to run in an external chain code container, the original intelligent contract chain code package needs to be decompressed, a metadata configuration file and a connection configuration file are generated based on an index and a set uploaded during intelligent contract creation, and the metadata configuration file and the connection configuration file are repackaged together with the decompressed file to obtain a new intelligent contract chain code package and then uploaded;
after the intelligent contract chain code packet is uploaded, calling an intelligent contract installation method of an SDK interface to carry out intelligent contract installation, after an external chain code container is deployed, creating a Pod based on StateUfSet and Service, and writing a chain code number and a corresponding chain code Service address into an environment variable;
when the intelligent contract is installed and instantiated, if the related intelligent contract is not installed for the first time, the initialization function in the instantiation is replaced by a null implementation.
For the specific steps of managing an intelligent contract (hereinafter referred to as a contract), the main behaviors of the three modules in the embodiment include:
in a first aspect, for a proposal for operating an intelligent contract, the proposal processing module first verifies whether the state of the intelligent contract meets the corresponding state requirement, and needs to meet the following rules:
1) for the contract deployment, the contract to be deployed cannot be identical to other contracts in the block chain network, and once the intelligent contracts with the same name and the same version number are repeatedly installed on the same endorsement node, an installation error can be caused;
2) if the proposal is to deploy other contract operations outside the contract, the current contract cannot have the proposal in process, and the condition that one contract is operated by a plurality of proposals simultaneously can cause confusion occurs; for example, if the current state of the contract is frozen and an upgrade request and a unfreeze request are initiated at the same time, the state of the contract is uncertain after the proposal is executed, and therefore transactional guarantee must be provided for contract operation;
3) a deployment contract must be one that does not exist; upgrading a contract must be upgrading an existing contract; freezing a contract must be freezing an existing and running contract; the thawing contract must be a contract that is in freezing; version rollback must be that the contract already exists and is running and there is the last version, and version advance must be that the contract already exists and is running and there is the next version.
After the proposal is successfully initiated, informing relevant organizations to carry out voting operation on the proposal, and if more than half of the organizations agree with the contract operation proposal, generating a proposal to-be-executed message and sending the proposal to the BaaS terminal; if half of organizations refuse contract operation proposal, the proposal voting state is marked as that the table fails;
particularly, the permission for redeploying the intelligent contract is set for certain specific organizations in the channel, when the proposal initiation fails and is the deployment of the intelligent contract, if the proposal initiation organization has the permission for redeploying, a request for redeploying is initiated, and the deployment of the intelligent contract is carried out again.
In this embodiment, unlike other management contract proposals, the deployment contract is the deployment of a new contract, and before deployment, a new contract needs to be created, and the creation of a new contract is also performed in the form of initiating a proposal.
In the second aspect, the message module is divided into two parts, including contract end message generation and BaaS end message processing;
the type of the message comprises a proposal initiating result, a proposal voting result and a proposal executing result;
the BaaS terminal acquires all state information with Message and MspID as prefixes when calling a function for acquiring the Message, wherein the MspID is acquired based on information of a caller; if the proposal is initiated completely, newly adding an initiating result message in a database; if the proposal voting is finished, newly adding voting result information in a database; and if the proposal is executed, newly adding an execution result message in the database.
In a third aspect, the proposal execution module performs installation and instantiation operations of related contracts;
the proposal execution is a part of the BaaS for carrying out actual operation on the contract, and the BaaS end can carry out actual operation on the contract when receiving a new proposal to be executed message;
the processing according to the different proposal types is as follows:
1) the contract deployment is to deploy a new contract, upload the new contract to the channel, and install the new contract at an endorsement node where the new contract needs to be deployed, wherein the proposal information at least comprises the name and the version number of the new contract, the channel name, the initialization parameter, the endorsement policy and the private data, and the specific flow is shown in fig. 3;
2) the contract upgrading is to upgrade the current contract, select the existing contract in the channel and install, wherein, the proposal information at least includes the name and version number of the current contract, the name and version number of the upgraded contract, the channel name, the endorsement policy and the private data, and the specific flow is as shown in fig. 4;
3) all the calls of the current contract are modified to return 'the contract is frozen', the endorsement policy of the current contract is replaced by the endorsement policy of all the organizations, the ID of the contract is set to be null, the modified contract is the frozen contract, and the frozen contract is installed, wherein the proposal information at least comprises the name, the version number and the channel name of the current contract, and the specific flow is shown in FIG. 5;
4) the unfreezing contract is the original contract before freezing is installed, wherein the proposal information at least comprises the name, the version number and the channel name of the current contract, the current contract is selected, and the specific flow is shown in figure 6;
5) the version rollback and version advance of the contract require that the current contract is in operation, and the contract with the previous version/the next version is available, and the contract meeting the conditions is selected for installation, wherein the proposal information at least comprises the name of the current contract, the version number of the contract with the previous/next version, and the channel name, and the specific flow is shown in fig. 7 and 8;
6) upgrading parameters of the contract, modifying the endorsement policy and the private data set of the contract without changing the version number of the current contract, and installing the modified contract, wherein the proposal information at least comprises the name, the version number, the channel name, the endorsement policy and the private data of the current contract, and the specific flow is shown in fig. 9;
note that the upgrading, freezing, unfreezing, version rollback, version advance, parameter upgrading, when installing the relevant contract, the endorsement node where the current contract is installed is selected for installation.
In addition to the above three modules, the BaaS end in this embodiment further includes a message receiving module, a selective installation module, a configuration module, and a direction instance module when performing message processing;
the message receiving module receives a proposed plan message to be executed; the selection installation module is used for installing related intelligent contracts at endorsement nodes selected by a proposal initiating organization; the configuration module is used for configuring Stateresult and Service for the external chain code container; and the pointing instance module is used for pointing the current intelligent contract ID of the organization where the endorsement node is located to the installed related intelligent contract ID and instantiating the related intelligent contract after the external chain code container is normally started.
In the embodiment, contract detail inquiry is also provided, and intelligent contract information and historical version information thereof deployed in each channel can be checked; when the intelligent contract proposal is operated, the current version information and the original version information of the intelligent contract are saved, and the saved information at least comprises the following information: the name, version number, path, initialization parameter, endorsement policy, state, MD5 value, channel name, set configuration, proposal initiating organization, initiating time and submitting time of the intelligent contract.
Other embodiments of the present disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This specification is intended to cover any variations, uses, or adaptations of the specification following, in general, the principles of the specification and including such departures from the present disclosure as come within known or customary practice within the art to which the specification pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the specification being indicated by the following claims.
It will be understood that the present description is not limited to the precise arrangements described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present description is to be limited only by the terms of the appended claims.