简体   繁体   English

鉴于智能合约一旦部署就不可更改,uniswap 如何升级其智能合约?

[英]How does uniswap upgrade its smart contract given that smart contract are immutable once deployed?

In my understanding, smart contracts on Ethereum are immutable once they are pushed onto chain.在我的理解中,以太坊上的智能合约一旦被推送到链上就不会改变。 Then how does uniswap keeps upgrading itself from v1 to v2 to v3?那么 uniswap 是如何不断地从 v1 升级到 v2 再到 v3 呢? How can they modify their smart contract code?他们如何修改他们的智能合约代码? "They" here apparently refer to Uniswap Labs.这里的“他们”显然是指 Uniswap Labs。 Additionally, since it is decentralized and nothing is special about Uniswap Labs, can anyone else modify the Uniswap contract as well?此外,由于它是去中心化的,Uniswap Labs 没有什么特别之处,其他人也可以修改 Uniswap 合约吗?

Each version of Uniswap is a different set of contracts, deployed on different addresses. Uniswap 的每个版本都是一组不同的合约,部署在不同的地址上。 So there is no upgrading of existing contracts.因此,现有合同没有升级。 See the links for the list of addresses:请参阅地址列表的链接:


Besides this approach of deploying each version to a new address, there's also the proxy pattern.除了这种将每个版本部署到新地址的方法之外,还有代理模式。 Similarly to networking proxies, you can redirect the request to a target contract, and possibly modify it along the way.与网络代理类似,您可以将请求重定向到目标合同,并可能在此过程中对其进行修改。 If the target address is stored in a variable, you can change the value without changing the actual contract bytecode.如果目标地址存储在变量中,您可以在不更改实际合约字节码的情况下更改该值。

pragma solidity ^0.8;

contract Proxy {
    address target;

    function setTarget(address _target) external {
        // you can change the value of `target` variable in storage
        // without chanding the `Proxy` contract bytecode
        target = _target;
    }

    fallback(bytes calldata) external returns (bytes memory) {
        (bool success, bytes memory returnedData) = target.delegatecall(msg.data);
        require(success);
        return returnedData;
    }
}

You can read more about the upgradable proxy pattern here: https://docs.openzeppelin.com/upgrades-plugins/1.x/proxies您可以在此处阅读有关可升级代理模式的更多信息: https://docs.openzeppelin.com/upgrades-plugins/1.x/proxies

Note that my code above is simplified to showcase just the basic proxy functionality.请注意,我上面的代码经过简化,只展示了基本的代理功能。 It is vulnerable to storage collision mentioned in the article, as well as to Delegatecall to Untrusted Callee .它容易受到文章中提到的存储冲突以及Delegatecall to Untrusted Callee的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM