Search code examples
blockchainsmartcontracts

What happens when you write an erroneous smart contract that is pushed onto the blockchain?


Since it's the blockchain you can't remove it. What if the erroneous code isn't some benign, what if it can cause damage to the company's business. So for example flooding the company's own API with requests effectively DDoSing itself. That's probably a bad example but my question is what can you do if a smart contract with erroneous code is pushed, what are the steps you can take to stop the smart contract from function?

If the question doesn't belong here, I'll move it elsewhere. As long as I am directed to the right place to ask the question.


Solution

  • Even though smart contract bytecode is immutable, there are two basic approaches to upgrading smart contracts:

    1. Deploy a new version to a separate address.
    2. Use an upgradable proxy pattern. The proxy contract holds the state (e.g. storage values) and points to a variable implementation contract.

    Example of the first approach is Uniswap. They're currently on v3 that is being used by most users and is connected to their UI, but the previous contracts (v1 and v2) are still available and being used to some extent (my guess is mostly by trading bots).

    One of the downsides is a need to migrate data to the newer version contracts. So in this specific case, Uniswap also released semi-automated tools for liquidity providers (one of the types of their users) to migrate their liquidity from v2 to v3.

    With the second approach, the data is still stored in the same proxy contract - while the implementation (the actual code) contract can change. There's a different set of challenges, for example possible storage collisions between the implementation contracts.


    Destruction of an already deployed contract depends on the network.

    On EVM networks, there's a deprecated native function selfdestruct that will be disabled in future versions, and it enables the caller (in most cases it's wrapped by an authorization mechanism) to destruct the contract. After it's disabled, there won't be any way to remove an already deployed code but it's possible that increasing size of the blockchain will open this discussion again later.

    On some of the other networks (e.g. Solana), you have an option to rent storage slots instead of the immutable state changes. So that could also serve as a failsafe mechanism if you don't prolong the rent in X amount of time, it disables the contract functionality.