3

I'm having problems with testrpc, or at least I think that's the cause. This particular portion of code

if (now > info.deadline) {
    ended = true;
    refundEnabled = true;
    return false;
}

is never executed, it's as if now (or block.timestamp) is always smaller than deadline even when it's clearly not. Deadline is in unix timestamp (for example 1487399645), but that code is not executed even if I put 1.

Here's the contract:

pragma solidity ^0.4.6;

contract Project {

    struct projectInfo {
        address projectOwner;
        string name;
        uint256 amountToRaise;
        uint256 deadline;
    }

    projectInfo public info;
    uint256 public amountRaised;
    bool public ended;
    bool public refundEnabled;
    mapping(address => uint256) public contributions;

    function Project(string name, uint256 amountToRaise, uint256 deadline) {
        info = projectInfo(tx.origin, name, amountToRaise, deadline);
        amountRaised = 0;
        ended = false;
        refundEnabled = false;
    }

    function getInfo() constant returns (address, string, uint256, uint256) {
        return (info.projectOwner, info.name, info.amountToRaise, info.deadline);
    }

    function fund() payable returns (bool successful) {
        if (ended) {
            return false;
        }
        contributions[tx.origin] += msg.value;
        amountRaised += msg.value;
        if (now > info.deadline) {
            ended = true;
            refundEnabled = true;
            return false;
        }
        if (amountRaised >= info.amountToRaise) {
            ended = true;
            payout();
        }
        return true;
    }

    function payout() private returns (bool successful) {
        if (ended) {
            if (!info.projectOwner.send(this.balance)) {
                throw;
            }
        }
    }

    function refund() public returns (bool successful) {
        if (refundEnabled) {
            if (contributions[msg.sender] > 0) {
                if (msg.sender.send(contributions[msg.sender])) {
                    contributions[msg.sender] = 0;
                    return true;
                } else {
                    return false;
                }
            } else {
                throw;
            }
        }
    }
}

I'm deploying the contract from another contract and I'm testing fund() with this piece of Javascript:

function fund(address) {
    project = Project.at(address);
    project.fund({from: coinbase, value: 50, gas: 150000}).then(function(value) {
        console.log(value);
    });
}

The contract works perfectly on Browser Solidity, on TestRPC everything works fine except that particular if statement. Is this a problem of TestRPC or am I doing something wrong? I'm using TestRPC v3.0.3.

  • 1
    Can I suggest that you create a debug event to log the values of now and info.deadline just before the if statement. Once the transaction is mined you will be able to see the exact values (without doubts) in the transactions logs. Good luck – Guenole de Cadoudal Feb 18 '17 at 12:57
  • 1
    I did like you said, the event returned the right values and the code actually executed correctly, I removed the event and the code still works. So now I have the exact same code as before but now it actually works. I will never understand how this stuff works. Thanks for your help. – Fyde Feb 19 '17 at 2:34
  • 1
    Few possibilities: you used the address of a contract deployed with an earlier version of the code, the solidity compiler did a stupid optimisation, there was a typo initially, etc or 42. :-) – Guenole de Cadoudal Feb 20 '17 at 7:06
  • Could anyone tell me the root cause of the issue. Or How the way it was esoved. Im getting the same. Is there any options i need to pass when I start testrpc. Please address. – Seetharaman GR Apr 26 '17 at 10:18
3

I had a similar problem, maybe it will help someone. Similar case as above, funding with deadline, testing with testrpc from javascript. While deadline functionality was working fine within each test case, I figured the date of contract creation is being changed in each test case. After some 'debugging' I figured the problem was with beforeEach close of test script - it deployed a new contract for each test case. :)

1

Recommend testing using truffle ganache: truffleframework.com/ganache

Their electron-based app complements testrpc pretty well for this kind of scenario.

Back to the question at hand, no mining happens on testrpc by default, so the blocktime only moves forward if you interact with the blockchain via a write.

enter image description here

https://ethfiddle.com/3MfpQS8bMy

0

In my experience, the timestamp "now" on testrpc is updated each time I send a Solidity Event. For example each time I call the following event inside my code:

Transfer(msg.sender, _to, _value);

When I have no events, then the "now" timestamp is always the same initial value when running on testrpc. Hope is accurate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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