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?
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
Think about the require condition and the current block timestamp compared to unlockTime.
The withdraw function requires the current block timestamp to be greater than or equal to unlockTime. Since the call happens before the timelock expires, the require fails and the transaction reverts with the error message.
Choose the best description of why developers use the timelock pattern in blockchain smart contracts.
Think about security and transparency in decentralized systems.
The timelock pattern delays execution of sensitive functions, giving users time to review or react before changes take effect. This improves security and trust.
execute() too early?Analyze the following Solidity contract snippet. What error message will the execute() function produce if called before the timelock expires?
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 } }
Check the require condition and error message.
The require statement checks if the current time is greater than releaseTime. If called too early, it reverts with 'Too early to execute'.
Choose the correct Solidity code snippet that declares a public variable named unlockTime to store a UNIX timestamp for a timelock.
Remember Solidity variable declaration syntax and visibility keywords.
Option D correctly declares a public unsigned integer variable. Other options have syntax errors or incorrect order.
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?
Think about adding and removing items from a list.
Scheduling three transactions adds three items. Executing one removes it, leaving two remaining in the queue.