Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Upgrade strategies in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Upgrade Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a Proxy Upgrade Pattern
Consider a simple proxy contract pattern where the proxy delegates calls to an implementation contract. What will be the output of the following Solidity code snippet when calling getVersion() after upgrading the implementation?
Blockchain / Solidity
contract ImplementationV1 {
    function getVersion() public pure returns (string memory) {
        return "V1";
    }
}

contract ImplementationV2 {
    function getVersion() public pure returns (string memory) {
        return "V2";
    }
}

contract Proxy {
    address public implementation;

    constructor(address _impl) {
        implementation = _impl;
    }

    function upgrade(address _newImpl) public {
        implementation = _newImpl;
    }

    fallback() external payable {
        (bool success, bytes memory data) = implementation.delegatecall(msg.data);
        require(success);
        assembly {
            return(add(data, 0x20), mload(data))
        }
    }
}
A"V1"
BCompilation error due to delegatecall
C"V2"
DRuntime revert due to fallback
Attempts:
2 left
💡 Hint
Think about what happens when the proxy's implementation address is changed and a function is called.
🧠 Conceptual
intermediate
1:30remaining
Choosing Upgrade Strategy for Immutable Contracts
Which upgrade strategy is best suited for smart contracts that cannot be changed after deployment but require new features?
AUse a proxy contract with delegatecall to the implementation contract
BUse self-destruct to remove the old contract and redeploy
CModify the contract bytecode directly on the blockchain
DDeploy a new contract and use a registry to point users to the latest version
Attempts:
2 left
💡 Hint
Immutable contracts cannot be changed, so think about how to redirect users.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Upgradeable Contract Storage
Given the following Solidity contracts, what is the cause of the unexpected behavior where the stored value does not persist after upgrading?
Blockchain / Solidity
contract StorageV1 {
    uint256 public value;

    function setValue(uint256 _value) public {
        value = _value;
    }
}

contract StorageV2 {
    uint256 public newValue;
    uint256 public value;

    function setValue(uint256 _value) public {
        value = _value;
    }

    function setNewValue(uint256 _newValue) public {
        newValue = _newValue;
    }
}
AStorage layout mismatch between V1 and V2 causes data corruption
BDelegatecall is not used in the proxy contract
CThe upgrade function does not update the implementation address
DThe fallback function is missing in the proxy contract
Attempts:
2 left
💡 Hint
Think about how storage variables are ordered and how that affects upgradeable contracts.
📝 Syntax
advanced
1:00remaining
Syntax Error in Upgradeable Contract Code
Which option contains the correct syntax for a Solidity function that upgrades the implementation address in a proxy contract?
Afunction upgrade(address newImpl) public { implementation = newImpl; }
Bfunction upgrade(address newImpl) public { implementation = newImpl }
Cfunction upgrade(address newImpl) public { implementation == newImpl; }
Dfunction upgrade(address newImpl) public { implementation := newImpl; }
Attempts:
2 left
💡 Hint
Remember Solidity statements end with semicolons and assignment uses a single equals sign.
🚀 Application
expert
1:30remaining
Calculate Number of Upgrade Steps in a Multi-Stage Upgrade
A blockchain project uses a multi-stage upgrade strategy where each upgrade deploys a new implementation contract and updates the proxy. If the project starts at version 1 and plans to reach version 5, how many upgrade transactions are needed to reach version 5 from version 1?
A5
B4
C3
D1
Attempts:
2 left
💡 Hint
Count how many times the implementation must be changed to go from version 1 to 5.

Practice

(1/5)
1. Which of the following is a common upgrade strategy in blockchain development?
easy
A. Changing the blockchain consensus algorithm without notifying nodes
B. Using proxy contracts to allow logic changes without changing the contract address
C. Deleting old blocks to save space
D. Ignoring backward compatibility during upgrades

Solution

  1. Step 1: Understand upgrade strategies

    Common upgrade strategies include proxy contracts, hard forks, and soft forks.
  2. Step 2: Identify the correct method

    Proxy contracts allow changing logic while keeping the same address, enabling safe upgrades.
  3. Final Answer:

    Using proxy contracts to allow logic changes without changing the contract address -> Option B
  4. Quick Check:

    Proxy contracts = safe upgrade method [OK]
Hint: Proxy contracts keep address same for upgrades [OK]
Common Mistakes:
  • Confusing hard forks with proxy contracts
  • Thinking deleting blocks is an upgrade
  • Ignoring backward compatibility
2. Which syntax correctly declares a proxy contract upgrade function in Solidity?
easy
A. function upgradeTo(address newImplementation) external onlyOwner {}
B. upgradeTo(address newImplementation) public {}
C. function upgrade(address newImplementation) private {}
D. function upgradeTo() external {}

Solution

  1. Step 1: Check function declaration syntax

    In Solidity, functions must start with 'function' keyword and specify visibility.
  2. Step 2: Match upgrade function signature

    The upgrade function usually takes an address and is external with access control like 'onlyOwner'.
  3. Final Answer:

    function upgradeTo(address newImplementation) external onlyOwner {} -> Option A
  4. Quick Check:

    Correct Solidity function syntax = function upgradeTo(address newImplementation) external onlyOwner {} [OK]
Hint: Solidity functions need 'function' and visibility keywords [OK]
Common Mistakes:
  • Omitting 'function' keyword
  • Using wrong visibility like private for upgrade
  • Missing function parameters
3. Given this Solidity proxy upgrade snippet, what will be the output of implementation() after calling upgradeTo(newAddress)?
contract Proxy {
  address private _implementation;
  function implementation() public view returns (address) {
    return _implementation;
  }
  function upgradeTo(address newImplementation) public {
    _implementation = newImplementation;
  }
}
medium
A. Compilation error due to missing visibility
B. Always zero address (0x0)
C. The address of the Proxy contract itself
D. The address stored in _implementation after upgradeTo is called

Solution

  1. Step 1: Understand state variable update

    The function upgradeTo sets _implementation to newImplementation address.
  2. Step 2: Check implementation() return value

    implementation() returns the current _implementation address, which changes after upgradeTo call.
  3. Final Answer:

    The address stored in _implementation after upgradeTo is called -> Option D
  4. Quick Check:

    State variable updated = returned address [OK]
Hint: State variable returns updated address after upgrade [OK]
Common Mistakes:
  • Assuming implementation() returns Proxy address
  • Thinking _implementation stays zero
  • Confusing visibility keywords
4. Identify the bug in this upgrade function and how to fix it:
function upgradeTo(address newImplementation) public {
  _implementation = newImplementation;
}
medium
A. Incorrect parameter type; should be uint256 instead of address
B. Function should be private to prevent external calls
C. Missing access control; add 'onlyOwner' modifier to restrict upgrades
D. No bug; function is correct as is

Solution

  1. Step 1: Analyze function security

    The function allows anyone to call upgradeTo and change implementation, which is unsafe.
  2. Step 2: Add access control

    Adding 'onlyOwner' modifier restricts upgrades to contract owner, preventing unauthorized changes.
  3. Final Answer:

    Missing access control; add 'onlyOwner' modifier to restrict upgrades -> Option C
  4. Quick Check:

    Access control needed for upgrade functions [OK]
Hint: Always restrict upgrade functions with access control [OK]
Common Mistakes:
  • Ignoring security risks of public upgrade functions
  • Changing parameter type incorrectly
  • Making function private disables upgrades
5. You want to upgrade a deployed smart contract without changing its address or losing stored data. Which upgrade strategy should you use and why?
hard
A. Use a proxy contract pattern to separate logic and data storage
B. Perform a hard fork to replace the entire blockchain state
C. Deploy a new contract and ask users to switch manually
D. Delete the old contract and deploy a new one at the same address

Solution

  1. Step 1: Understand upgrade goals

    We want to keep the same contract address and preserve stored data during upgrade.
  2. Step 2: Evaluate upgrade strategies

    Proxy contracts separate logic and data, allowing logic upgrades without changing address or data loss.
  3. Step 3: Compare other options

    Hard forks replace blockchain state, deploying new contracts requires user action, deleting contracts is impossible on blockchain.
  4. Final Answer:

    Use a proxy contract pattern to separate logic and data storage -> Option A
  5. Quick Check:

    Proxy pattern = upgrade without address or data loss [OK]
Hint: Proxy pattern upgrades logic, keeps data and address [OK]
Common Mistakes:
  • Thinking hard forks preserve contract address
  • Assuming users will always switch to new contract
  • Trying to delete deployed contracts