Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Timelock pattern 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
🎖️
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.

Practice

(1/5)
1.

What is the main purpose of the Timelock pattern in blockchain smart contracts?

easy
A. To delay certain actions until a specific time has passed
B. To speed up transaction processing
C. To encrypt user data
D. To reduce gas fees

Solution

  1. Step 1: Understand the Timelock pattern concept

    The Timelock pattern is designed to delay actions in smart contracts until a set time has passed.
  2. Step 2: Identify the purpose of the delay

    This delay helps protect users by preventing instant changes that could be harmful or unexpected.
  3. Final Answer:

    To delay certain actions until a specific time has passed -> Option A
  4. Quick Check:

    Timelock pattern = delay actions [OK]
Hint: Timelock means waiting before action happens [OK]
Common Mistakes:
  • Thinking it speeds up transactions
  • Confusing with encryption
  • Assuming it lowers gas fees
2.

Which of the following Solidity code snippets correctly enforces a timelock using block.timestamp?

function execute() public {
  require(__________, "Too early to execute");
  // action code
}
easy
A. block.timestamp >= unlockTime
B. block.timestamp < unlockTime
C. block.number >= unlockTime
D. block.difficulty > unlockTime

Solution

  1. Step 1: Understand the condition for timelock

    The action should only execute if the current time is equal or after the unlock time.
  2. Step 2: Choose the correct comparison

    Using block.timestamp >= unlockTime ensures the function runs only after the unlock time.
  3. Final Answer:

    block.timestamp >= unlockTime -> Option A
  4. Quick Check:

    Time check uses block.timestamp >= unlockTime [OK]
Hint: Use block.timestamp and >= for timelock checks [OK]
Common Mistakes:
  • Using < instead of >=
  • Using block.number instead of block.timestamp
  • Using unrelated block properties
3.

What will be the output of the following Solidity function call if block.timestamp is 1650000000 and unlockTime is 1650000100?

function canExecute() public view returns (bool) {
  return block.timestamp >= unlockTime;
}
medium
A. true
B. Revert with error
C. Compilation error
D. false

Solution

  1. Step 1: Compare block.timestamp and unlockTime values

    Given block.timestamp = 1650000000 and unlockTime = 1650000100, block.timestamp is less than unlockTime.
  2. Step 2: Evaluate the return statement

    The expression block.timestamp >= unlockTime evaluates to false.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    1650000000 >= 1650000100 = false [OK]
Hint: Compare timestamps carefully for true/false output [OK]
Common Mistakes:
  • Assuming >= means true when timestamp is smaller
  • Confusing block.timestamp with block.number
  • Expecting errors instead of boolean
4.

Identify the error in this Solidity timelock function and choose the fix:

uint256 public unlockTime;

function execute() public {
  require(block.timestamp > unlockTime, "Too early");
  // perform action
}
medium
A. Use block.number instead of block.timestamp
B. Change block.timestamp > unlockTime to block.timestamp >= unlockTime
C. Remove the require statement
D. Change unlockTime to block.timestamp

Solution

  1. Step 1: Analyze the require condition

    The condition block.timestamp > unlockTime disallows execution exactly at unlockTime.
  2. Step 2: Adjust condition to allow execution at unlockTime

    Changing to block.timestamp >= unlockTime allows execution starting from unlockTime.
  3. Final Answer:

    Change block.timestamp > unlockTime to block.timestamp >= unlockTime -> Option B
  4. Quick Check:

    Use >= to include unlockTime moment [OK]
Hint: Use >= to allow execution at unlock time [OK]
Common Mistakes:
  • Using > excludes unlockTime moment
  • Removing require loses protection
  • Using block.number causes wrong timing
5.

You want to create a timelock contract that allows an admin to schedule a withdrawal only after 1 day from scheduling. Which approach correctly implements this?

contract Timelock {
  address public admin;
  uint256 public unlockTime;

  constructor() {
    admin = msg.sender;
  }

  function scheduleWithdrawal() public {
    require(msg.sender == admin, "Not admin");
    unlockTime = block.timestamp + 86400; // 1 day
  }

  function withdraw() public {
    require(msg.sender == admin, "Not admin");
    require(block.timestamp >= unlockTime, "Too early");
    // withdrawal logic
  }
}
hard
A. Admin cannot schedule withdrawal
B. Withdrawal can happen immediately after scheduling
C. Correctly enforces 1-day delay before withdrawal
D. unlockTime is set incorrectly causing errors

Solution

  1. Step 1: Check scheduling sets unlockTime correctly

    The scheduleWithdrawal function sets unlockTime to current time plus 86400 seconds (1 day).
  2. Step 2: Verify withdraw enforces timelock

    The withdraw function requires current time to be at or after unlockTime, enforcing the delay.
  3. Final Answer:

    Correctly enforces 1-day delay before withdrawal -> Option C
  4. Quick Check:

    UnlockTime = now + 1 day, withdraw requires >= unlockTime [OK]
Hint: Add 86400 seconds and check block.timestamp >= unlockTime [OK]
Common Mistakes:
  • Not adding delay in schedule function
  • Using > instead of >= in withdraw
  • Not restricting functions to admin