How to write an upgradeable smart contract _ ether Square

Source: Internet
Author: User

Block chain trust based on the characteristics of the data can not be modified, so that the traditional application has a very different place is once published on the block chain can not be modified (can not directly modify the original contract and then redistribute). It 's written in front .

Before reading this article, you should have an understanding of the ether, the smart contract, and the solidity language, and if you don't understand it, I suggest you see what the ether Square is. when smart contract bugs

On the one hand, due to the immutable nature of the intelligent contract, because as long as the rules are determined, nobody can modify it, people can trust it. On the other hand, if the implementation of the rule has bugs, it may cause the tokens to be stolen, or the call consumes a lot of gas. Then we need to fix the mistake.

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

pragma solidity ^0.4.18;

Contract Mycontract {
    mapping (address => uint256) is public balanceof;
    
    function Setblance (address _address,uint256 v.) public {
        balanceof[_address] = v;
    }

    function Addten (address addr) public returns (UINT) {return
        balanceof[addr] + one;
    }


If we find this problem after deployment, and want to fix the bug, we have to redeploy the contract, but there will be an embarrassing 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, a solution is to separate the data in the contract, using a separate contract to store the data (hereinafter referred to as the data contract), using 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) is 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) + one;
    }
}

Now we have two contracts DataContract are designed to store data, controlcontract to process logic, and use DataContract to read and write data. With this design, you can keep the data contract unchanged after you update the control contract so that you do not lose data or migrate data. Read and write control

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

pragma solidity ^0.4.18;

Contract DataContract {
    mapping (address => uint256) is public balanceof;
    Mapping (address => bool) accessallowed;
    
    function 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 public {
        ACCESSALLOWED[_ADDR] = true;
    }
    
    function denyaccess (address _addr) platform public {
        ACCESSALLOWED[_ADDR] = false;
    }
}

...

Subscribe to my small column to see the complete code for the contract.

The deployment method is as follows: first deploy the DataContract contract use DataContract contract address as the parameters for the Controlcontract contract deployment The Allowaccess method of DataContract contract is invoked using CONTROLCONTRACT contract address as parameter.
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

When we implement a data contract, the less logic it contains, the better, and it should be rigorously tested, because once the data contract is deployed, it cannot be changed.
In most cases, the user interacts with the Dapp, so when the control contract is upgraded, the Dapp needs to be upgraded to associate the new control contract.

Although the contract can be upgraded in this way, we still need to upgrade carefully because the upgrade means you can rewrite the logic and reduce the user's trust.
This article describes the upgrade method is more a way of thinking, the actual project may correspond to a number of control contracts and data contracts.



Author: Chongli soldier
Link: https://www.jianshu.com/p/b7ae53b6ef2e
Source: Jianshu

Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.


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.