What if you could stop a bad blockchain transaction before it happens?
Why Timelock pattern in Blockchain / Solidity? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to send money or change important settings on a blockchain contract immediately without any delay.
But what if a mistake happens or someone hacks your account? You have no time to stop it.
Doing actions instantly means no chance to review or cancel them.
This can lead to lost funds or irreversible errors because once a transaction is sent, it cannot be undone.
The Timelock pattern adds a waiting period before an action happens.
This delay gives you time to check, cancel, or fix mistakes before the change is final.
function changeOwner(address newOwner) public { owner = newOwner; }function queueChangeOwner(address newOwner) public { queuedOwner = newOwner; }It enables safer and more secure contract management by preventing rushed or harmful changes.
A company uses a Timelock to delay changes to its treasury control, allowing stakeholders to review and react before funds move.
Instant actions can cause irreversible mistakes.
Timelock adds a delay to allow review and cancellation.
This pattern improves security and trust in blockchain contracts.
Practice
What is the main purpose of the Timelock pattern in blockchain smart contracts?
Solution
Step 1: Understand the Timelock pattern concept
The Timelock pattern is designed to delay actions in smart contracts until a set time has passed.Step 2: Identify the purpose of the delay
This delay helps protect users by preventing instant changes that could be harmful or unexpected.Final Answer:
To delay certain actions until a specific time has passed -> Option AQuick Check:
Timelock pattern = delay actions [OK]
- Thinking it speeds up transactions
- Confusing with encryption
- Assuming it lowers gas fees
Which of the following Solidity code snippets correctly enforces a timelock using block.timestamp?
function execute() public {
require(__________, "Too early to execute");
// action code
}Solution
Step 1: Understand the condition for timelock
The action should only execute if the current time is equal or after the unlock time.Step 2: Choose the correct comparison
Usingblock.timestamp >= unlockTimeensures the function runs only after the unlock time.Final Answer:
block.timestamp >= unlockTime -> Option AQuick Check:
Time check uses block.timestamp >= unlockTime [OK]
- Using < instead of >=
- Using block.number instead of block.timestamp
- Using unrelated block properties
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;
}Solution
Step 1: Compare block.timestamp and unlockTime values
Given block.timestamp = 1650000000 and unlockTime = 1650000100, block.timestamp is less than unlockTime.Step 2: Evaluate the return statement
The expressionblock.timestamp >= unlockTimeevaluates to false.Final Answer:
false -> Option DQuick Check:
1650000000 >= 1650000100 = false [OK]
- Assuming >= means true when timestamp is smaller
- Confusing block.timestamp with block.number
- Expecting errors instead of boolean
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
}Solution
Step 1: Analyze the require condition
The conditionblock.timestamp > unlockTimedisallows execution exactly at unlockTime.Step 2: Adjust condition to allow execution at unlockTime
Changing toblock.timestamp >= unlockTimeallows execution starting from unlockTime.Final Answer:
Change block.timestamp > unlockTime to block.timestamp >= unlockTime -> Option BQuick Check:
Use >= to include unlockTime moment [OK]
- Using > excludes unlockTime moment
- Removing require loses protection
- Using block.number causes wrong timing
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
}
}Solution
Step 1: Check scheduling sets unlockTime correctly
ThescheduleWithdrawalfunction setsunlockTimeto current time plus 86400 seconds (1 day).Step 2: Verify withdraw enforces timelock
Thewithdrawfunction requires current time to be at or afterunlockTime, enforcing the delay.Final Answer:
Correctly enforces 1-day delay before withdrawal -> Option CQuick Check:
UnlockTime = now + 1 day, withdraw requires >= unlockTime [OK]
- Not adding delay in schedule function
- Using > instead of >= in withdraw
- Not restricting functions to admin
