1

In the Ethereum Wiki it is stated that you can delay a smart contract's action in this defined way :

struct RequestedWithdrawal {
    uint amount;
    uint time;
}

mapping (address => uint) private balances;
mapping (address => RequestedWithdrawal) private requestedWithdrawals;
uint constant withdrawalWaitPeriod = 28 days; // 4 weeks

function requestWithdrawal() public {
    if (balances[msg.sender] > 0) {
        uint amountToWithdraw = balances[msg.sender];
        balances[msg.sender] = 0; // for simplicity, we withdraw everything;
        // presumably, the deposit function prevents new deposits when withdrawals are in progress

        requestedWithdrawals[msg.sender] = RequestedWithdrawal({
            amount: amountToWithdraw,
            time: now
        });
    }
}

function withdraw() public {
    if(requestedWithdrawals[msg.sender].amount > 0 && now > requestedWithdrawals[msg.sender].time + withdrawalWaitPeriod) {
        uint amountToWithdraw = requestedWithdrawals[msg.sender].amount;
        requestedWithdrawals[msg.sender].amount = 0;

        if(!msg.sender.send(amountToWithdraw)) {
            throw;
        }
    }
}

Using "now", though it is stated here that :

The timestamp of the block can be manipulated by the miner, and so should not be used for critical components of the contract.

It is quite confusing, and I wonder both how one can safely delay a contract's action and how one can get trusted time from within a smart contract.

2 Answers 2

0

Using now / block.timestamp should not be a problem except for random number generation or logic that depends on fine timing.

Miners can manipulate the block.timestamp to some extent, which would allow them to discard transactions that yield a random number they don't like (if the number is generated using the timestamp).

In your example, the worst that could happen is that someone could withdraw the funds a few hours before the wait period.

Btw: the example is a bit outdated. Throw() has been deprecated in favor of revert(), for example.

2
  • Thanks for the answer, so someone could manipulate time in order to withdraw funds earlier or even instantly? This surely is a problem. Is there a way to get trusted incorruptible time and or to surely delay an action? Dec 15, 2017 at 15:46
  • Hi I'm quite worried about this problem, do you have an idea please? Dec 21, 2017 at 10:37
0

There is a smart contract called the ethereum alarm clock that basically checks that a call is not executed before or after a certain window of blocks.

The idea is that you can check the current block number to have a rough estimate of the time that has passed, you could derive it from the approximate hash rate.

Eg. Say an ethereum network creates about 1000 blocks per hour you can set a function to be called only if the block number is superior to the current block number plus 1000. Then it should be delayed by about an hour.

This solution is awfully approximate though it can't be manipulated by the minor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.