0
0
Blockchain / Solidityprogramming~20 mins

Timelock pattern in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timelock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity timelock contract snippet?

Consider this simplified Solidity contract snippet implementing a timelock for a withdrawal function. What will be the output when withdraw() is called before the timelock expires?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleTimelock {
    uint256 public unlockTime;
    address public owner;

    constructor(uint256 _unlockTime) {
        owner = msg.sender;
        unlockTime = _unlockTime;
    }

    function withdraw() public {
        require(block.timestamp >= unlockTime, "Timelock not expired");
        payable(owner).transfer(address(this).balance);
    }

    receive() external payable {}
}

// Assume unlockTime is set to block.timestamp + 1 hour
// withdraw() is called 30 minutes after deployment
ATransaction reverts with error 'Timelock not expired'
BFunds are transferred to the owner
CTransaction succeeds but no funds are transferred
DCompilation error due to missing payable keyword
Attempts:
2 left
💡 Hint

Think about the require condition and the current block timestamp compared to unlockTime.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the purpose of a timelock pattern in smart contracts?

Choose the best description of why developers use the timelock pattern in blockchain smart contracts.

ATo delay execution of critical functions to allow users to react or audit changes
BTo permanently lock funds so they cannot be withdrawn
CTo speed up transaction processing by batching calls
DTo encrypt contract data for privacy
Attempts:
2 left
💡 Hint

Think about security and transparency in decentralized systems.

🔧 Debug
advanced
2:00remaining
What error does this Solidity timelock contract produce when calling execute() too early?

Analyze the following Solidity contract snippet. What error message will the execute() function produce if called before the timelock expires?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Timelock {
    uint256 public releaseTime;
    address public admin;

    constructor(uint256 _releaseTime) {
        admin = msg.sender;
        releaseTime = _releaseTime;
    }

    function execute() public {
        require(block.timestamp > releaseTime, "Too early to execute");
        // perform critical action
    }
}
ATransaction succeeds and performs critical action
BTransaction reverts with 'Too early to execute'
CCompilation error due to missing visibility on constructor
DTransaction reverts with 'Unauthorized caller'
Attempts:
2 left
💡 Hint

Check the require condition and error message.

📝 Syntax
advanced
1:00remaining
Which option correctly declares a timelock variable in Solidity to store a future timestamp?

Choose the correct Solidity code snippet that declares a public variable named unlockTime to store a UNIX timestamp for a timelock.

Auint256 unlockTime = public;
Buint public unlockTime() public;
Cpublic uint256 unlockTime;
Duint256 public unlockTime;
Attempts:
2 left
💡 Hint

Remember Solidity variable declaration syntax and visibility keywords.

🚀 Application
expert
2:30remaining
How many items are in the queue after scheduling three timelock transactions and executing one?

A timelock contract maintains a queue of scheduled transactions. Initially empty, it schedules three transactions with unique IDs. Then, it executes one transaction and removes it from the queue. How many transactions remain in the queue?

A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Think about adding and removing items from a list.