In the first phase of the smart contract library award-winning activity, I developed a generalized functional contract template based on the sharing economy scenario, and contributed to the blockchain application development component WeBankBlockchain-SmartDev.
According to this development, I have summarized some personal experiences and shared them with community developers. I hope to help industry practitioners, as well as those who have just come into contact with blockchain technology and are eager to understand how to connect the blockchain with the actual production environment, and design some scenarios. The interactive partners bring inspiration and reference.
This sharing will be carried out from three dimensions: first, analysis of the demand for the combination of sharing economy scenarios and blockchain; second, focusing on the planning and design of generalized smart contracts; third, summary of the main points of generalized contracts with the summary.
Demand analysis of the combination of sharing economy scenarios and blockchain
Sharing economy scenarios can be seen everywhere. Shared bicycles, shared power banks, shared printers, these products have long been integrated into people's lives. From different application scenarios, we can see the commonality of the sharing economy: multi-party data flow, identity authentication, certificate storage, traceability, and autonomous operations.
These scenarios also reflect the current pain points of the sharing economy:
- Due to the lack of artificial supervision and control, the scene is prone to business loopholes;
- In some commercial application scenarios, there may be risks of data forgery and identity forgery;
- There are disadvantages of poor retrospective effect after the fact.
Based on the above pain points, sharing economy products need trust endorsement very much, and the blockchain itself has the characteristics of non-tampering and distributed accounting, which just meets the needs of trust endorsement.
The blockchain can also store certificates through user signatures, automatic device chaining, and blockchain authentication, so as to achieve the purpose of non-tampering, distributed storage, and traceability of the entire process.
Compared with the original product, blockchain technology, as a technical blessing, fixes many loopholes and strengthens data security.
Take shared charging as an example. Suppose I am a manufacturer and form a cooperative business group with the platform operator and partners to jointly conduct the commercial operation of shared charging. Through the introduction of blockchain, the transparency of data in the process of multi-party collaboration and even the traceability of subsequent data are realized.
The three main bodies of the user terminal, the Internet of Things equipment, and the business platform are the common triangle points in the interaction between the Internet of Things and people. After the user uses the mobile APP to scan the code of the shared charging IoT device, the device side interacts with the platform, and the device side will get the relevant information of the current scan code user and enter the subsequent business process.
The business platform is highly centralized. In the process of multi-party participation, the platform party has the right to modify and adjust the data, which means that there is a risk of tampering with business data.
Our basic demand for data is: the entire life cycle of business data from the beginning to the end should be immutable, and ensuring the true integrity of the data is the cornerstone of building business trust; so we introduced the blockchain to realize the demand.
For example, the user scans the code to use the IoT device. The IoT device directly and automatically packages the data to build the transaction and upload it to the chain without going through the business platform. Multi-party confirmation.
At present, some of the technical bottlenecks and positioning of the blockchain are not suitable for complex calculations and large-scale storage, but are more suitable for ledger-type accounting work, recording results such as summary data.
With the increase of usage scenarios in the future, the functions of blockchain extension will become more and more diverse. But back to the essence, initially we wanted to incorporate blockchain out of a desire to strengthen trust.
First of all, requirements of this type are usually accompanied by user participation, authority control, identity authentication, etc. of all parties;
Secondly, the storage of business data is also an indispensable link;
Finally, supporting derivative functions, such as point generation, trading, write-off, and additional demand functions in various scenarios.
Smart Contract Planning and Design
In the sharing economy scenario, we refine the common functions in the scenario and write generalized contracts. The purpose of designing a general contract is like developing an open source framework, extracting and realizing common functions, reducing the development workload and improving the reuse rate, so that we can expand flexibly according to specific business.
Generic contracts are planned and designed from different dimensions. We can first plan from the perspective of the business system and split the user system into different subjects:
The super administrator is the user at the time of the initial blockchain deployment.
Authorized managers mainly conduct operational management and control in the business, such as: authorizing IoT devices in some business systems, writing off points, and disabling users.
Registered users are the mapping of users who have passed the registration and certification of the business platform in the blockchain system.
The above picture is the most core deposit design. I can refer to the 5W rule, conceive the elements in the depository system, and map them to our actual blockchain business products.
For example: who is the business subject; when is the timestamp, the time certificate issued by the third-party certification agency; what is the data summary of the business system; where is the device information;
In the sharing economy scenario, it is clearly required that only IoT devices or authorized objects can be operated on the chain. In addition, there are some derivative functions in sharing economy products, such as signature verification, following official contracts + business transformation, reserved expansion functions, and tools.
Then consider from the technical dimension, which can be divided into different levels.
The first is normalization . We usually do not incorporate all business code into one contract, but do split, that is, hierarchical design, to make code decoupling and structure clearer.
The second is business functions . Choose the corresponding programming mode from the perspectives of business complexity, performance, and security risk control. If the business is not particularly complicated, it can be combined with a single contract + library; if the business complexity is relatively high, it can be split into multiple contracts, each contract is deployed in the actual environment, and then the contract is managed and controlled.
The operating cost of this complex model will be relatively high, and we need to control the control authority of each contract.
The third is forward-looking considerations . More is the future contract expansion. For example, I wrote version 1.0, and then wrote version 2.0, which is the iteration of the contract.
If I want to transform the business code, and there are no redundant parameters reserved at the beginning of the deployment, then the code needs to be re-modified and redeployed. There may be the problem of incompatibility between the original data and the new data.
In this case, we have multiple options:
The first is to layer business logic and data;
The second is to use the native contract to modify the contract address, so that the data and the newly transformed contract can be seamlessly connected. At the same time, the precompiled contract--table provided by FISCO BCOS itself can be used to solve the problem of data sustainability.
After considering the dimensions from business to technology, we can design the contract. First, use the hierarchical warehousing model for splitting; second, use the native contract, and use the model of single-contract deployment plus multiple contract class libraries. At present, the logic layer and data layer are not split, because it can be reserved for the actual scene to be split selectively in the actual process.
In the composition of this set of contracts, the main contract mainly has functions such as depository, identity authentication, point generation, and circulation, and auxiliary contract library: including two warehouse contract libraries and three tool contract libraries.
The complete business operation process is as follows:
Process one
When the contract is written, we will compile the main contract and deploy it to the chain by the blockchain administrator, authorize multiple managers, and then these managers will authorize the corresponding IoT devices.
Process 2
On the business platform side, assuming that the user Zhang San uses an APP or a small program to register on the platform, after the real-name authentication on the business platform side, the platform will generate the identity address and public and private key on the corresponding blockchain and give it to Zhang San.
Process three
Zhang San first used the APP to scan the code of the shared charging equipment;
The device side interacts with the business platform after receiving the scan code information to obtain user information.
The user performs the charging service, and the order payment is made after the service is completed. At this time, the APP will prompt the user to authorize the APP to authorize the signature of the order when the user pays.
The user's order signature can be directly sent to the IoT device by the mobile phone Bluetooth, or it can be pushed to the IoT device after the user APP interacts with the business platform.
The device side verifies the validity of the data. After the verification, the device side packages the corresponding user order signature, the charging record and order information generated by itself to construct a transaction and send it to the blockchain.
The blockchain performs identity authentication for the operator sent in the transaction, and the user in the business data also needs to perform identity authentication to ensure that he is a licensed user registered and authenticated by the platform, and finally performs other business correlation verification. Evidence.
In actual business scenarios, we can also effectively feed back blockchain information to the business platform through middleware such as oracles.
Summary and summary of the main points of generalized contracts
From the perspective of the business system, user roles, authority control, and certificate storage functions are the foundation and must be satisfied. Secondly, the customization function of general contracts is based on actual needs and future reservations. Generally, a modular contract library is used to split different business systems. Other content can be considered in practice according to business needs.
At present, the smart contract library award-winning code collection activity for the majority of open source contributors is still going on. I hope everyone can work together to provide safe and reliable contracts for subsequent developers.
In the development process, using open source software can reduce the development investment and focus on the content that we need to invest more time in research.
https://www.qq.com/video/q3261sfbavt
"Super Blockchain"
"Super Talk Blockchain" is a live broadcast event launched by the FISCO BCOS open source community. Every Thursday at 8 pm, the community invites a technology geek or application pioneer to share development practices or application experience as a guest in the live broadcast room. As a regular community column, "Super Talk Blockchain" has held nearly 100 sessions, ranging from technical seminars to industrial applications. You are welcome to recommend yourself or recommend friends to share in the live broadcast room. Add [Little Assistant] to join the group to watch the live broadcast.
For more dry goods, please pay attention to the FISCO BCOS open source community public account, visit the FISCO BCOS code repository to download all the source code of the project: https://github.com/FISCO-BCOS/FISCO-BCOS , please click the star collection in the upper right corner of the page , to get the latest version.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。