0
0
Blockchain / Solidityprogramming~10 mins

Timelock pattern in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Timelock pattern
Start
Request Action
Set Timelock Expiry
Wait Until Expiry
Check Current Time >= Expiry?
NoWait
Yes
Execute Action
End
The Timelock pattern delays execution of an action until a set time has passed, ensuring a waiting period before the action runs.
Execution Sample
Blockchain / Solidity
uint public unlockTime;

function requestAction() public {
  unlockTime = block.timestamp + 1 days;
}

function executeAction() public {
  require(block.timestamp >= unlockTime, "Action is locked");
  // action code
}
This code sets a future unlock time when an action is requested, then only allows execution after that time.
Execution Table
StepActionblock.timestampunlockTimeConditionResult
1Call requestAction()10000001000000 + 86400 = 1086400N/AunlockTime set to 1086400
2Call executeAction() at time 1005000100500010864001005000 >= 1086400?False - revert, cannot execute
3Call executeAction() at time 1086400108640010864001086400 >= 1086400?True - action executes
4Call executeAction() at time 1086500108650010864001086500 >= 1086400?True - action executes
💡 Execution stops if current time is less than unlockTime; action only runs after unlockTime.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
unlockTime01086400108640010864001086400
block.timestampN/A1000000100500010864001086500
Key Moments - 3 Insights
Why can't executeAction run immediately after requestAction?
Because unlockTime is set to a future timestamp (current time + 1 day). The execution_table row 2 shows the condition fails when current time is less than unlockTime.
What happens if executeAction is called exactly at unlockTime?
The condition block.timestamp >= unlockTime is true, so the action executes successfully as shown in execution_table row 3.
Does the unlockTime change after it is set?
No, unlockTime stays the same until requestAction is called again. The variable_tracker shows unlockTime remains constant after step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of unlockTime after calling requestAction()?
A1000000
B1086400
C0
D1005000
💡 Hint
Check execution_table row 1 under unlockTime column.
At which step does the condition block.timestamp >= unlockTime become true for the first time?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See execution_table Condition and Result columns for each step.
If the unlockTime was set to current time + 2 days instead of 1 day, how would step 2 change?
ACondition would still be false at step 2
BunlockTime would be less than block.timestamp
CCondition would be true at step 2
DexecuteAction would execute immediately
💡 Hint
Compare unlockTime and block.timestamp values in execution_table row 2.
Concept Snapshot
Timelock pattern delays actions until a set future time.
Set unlockTime = current time + delay.
Only allow execution if current time >= unlockTime.
Prevents immediate execution, adds security delay.
Common in blockchain governance and contracts.
Full Transcript
The Timelock pattern is a way to delay an action in a blockchain contract until a certain time has passed. First, when an action is requested, the contract sets a future unlockTime by adding a delay (like 1 day) to the current block timestamp. Then, when trying to execute the action, the contract checks if the current time is at or past the unlockTime. If not, the action is blocked. This ensures a waiting period before the action runs, adding security and transparency. The example code sets unlockTime in requestAction and requires the current time to be greater or equal in executeAction. The execution table shows that calling executeAction too early fails, but succeeds once the unlockTime is reached or passed. Variables like unlockTime stay fixed until reset. This pattern is useful to prevent sudden changes or actions in blockchain systems.