How to write a smart contract that can be upgraded

Source: Internet
Author: User

"Hui Xin Yun" for everyone regularly updated articles, "Hui new cloud" It hardware and software projects business platform

The blockchain trust-based data is not modifiable, so it is not possible to modify the traditional application in a very big way (it cannot be modified directly on the original contract or re-published) once it is posted on the blockchain. write in front

Before reading this article, you should know something about Ethereum, the smart contract and the solidity language, and if you don't understand it, it's recommended that you look at Ethereum first. when the smart contract bugs

On the one hand formally due to the non-modifiable nature of the smart contract, because as long as the rules are determined, nobody can modify it, we can trust it. On the other hand, if the rules are implemented with bugs, tokens can be stolen, or the call consumes a lot of gas. Then we need to fix the error.

We know that a smart contract consists of two parts: code logic and data, and the code logic is the most likely part of the problem, such as when the following contract is implemented, the 10 is written as 11 when the hand is written Addten ().

pragma solidity ^ 0.4.18; Contract Mycontract {mapping (address = uint256) public balanceof; function setblance (Address _address,uint256 v) PU Blic {Balanceof[_address] = V,} function Addten (address addr) public returns (UINT) {return balanceof[addr] + 11;}}

If we find this problem after deployment and want to fix this bug, we have to redeploy the contract, but there will be an awkward problem, the original contract has been used by many people, if the new contract, the old contract data will be lost. data contracts and control contracts

So how to solve the problem, one solution is to separate the data in the contract, use a separate contract to store the data (hereinafter referred to as the data contract), and use a separate contract to write the business logic (hereinafter referred to as the control contract).
Let's take a look at how the code is implemented.

pragma solidity ^ 0.4.18; Contract DataContract {mapping (address = uint256) public balanceof; function setblance (address _address,uint256 v) public {balanceof[_address] = V;}} Contract Controlcontract {DataContract datacontract; function controlcontract (address _datacontractaddr) public { DataContract = DataContract (_DATACONTRACTADDR); } function Addten (address addr) public returns (UINT) {return datacontract.balanceof (addr) + 11;}}

Now we have two contracts DataContract dedicated to data storage, controlcontract for logic, and DataContract to read and write data. With this design, you can keep the data contract intact after updating the control contract so that no data is lost and data is not migrated. read/write Control

With DataContract we can update the contract logic separately, but you may have found a new problem that DataContract's data can be read and written not only by Controlcontract, but also by other contracts. Therefore, you need to add read/write control to DataContract. We add a mapping to DataContract to control which addresses can access the data, add decorators and set access methods, and the code is as follows:

pragma solidity ^ 0.4.18; Contract DataContract {mapping (address = uint256) public balanceof; mapping (address = bool) accessallowed; Fu Nction DataContract () public {Accessallowed[msg.sender] = true,} function setblance (address _address,uint256 v) public { Balanceof[_address] = v; } modifier platform () {require (Accessallowed[msg.sender] = = True); _,} function allowaccess (address _addr) platform Publ IC {ACCESSALLOWED[_ADDR] = true,} function denyaccess (address _addr) platform Public {ACCESSALLOWED[_ADDR] = false;}} Contract Controlcontract {DataContract datacontract; function controlcontract (address _datacontractaddr) public {DataC Ontract = DataContract (_DATACONTRACTADDR); } function Addten (address addr) public returns (UINT) {return datacontract.balanceof (addr) + 11;}}

The

Deployment method is as follows: Deploy the DataContract contract first using the DataContract contract address as the parameter for deploying the Controlcontract contract Use the Controlcontract contract address as a parameter to invoke the Allowaccess method of the DataContract contract.
If you need to update the control contract (such as repairing Addten), perform the 第2-3 step again and execute denyaccess () on the old control contract. More

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.