3

Seems like this should run. I've stripped it down just so that I can enter two player arbitrary choices and have the action end add 1 + 1 and payout to the even player. Neither players get paid though. There are some artifacts from the original code I modified that was a blind auction.

contract OddsAndEvens2 {

    struct Choice
    {
        bytes32 blindedChoice;
        uint deposit;
    }

    uint public auctionStart;
    uint public biddingEnd;
    uint public revealEnd;
    bool public ended;

    // used to store OddsOrEvens
    uint public OddsOrEvens;

    // used to store addresses
    address public playerOneAddress;
    address public playerTwoAddress;
    address public gameWinner;

    // used to count choices 
    uint public choiceCount;

    // used to count reveals 
    uint public revealCount;

    // players choice
    uint public playerOneChoice;
    uint public playerTwoChoice;
    uint public sumChoices;

    mapping(address => Choice[]) public choices;

    event AuctionEnded(address winner, uint OddsOrEvens);

    /// Modifiers are a convenient way to validate inputs to
    /// functions. `onlyBefore` is applied to `bid` below:
    /// The new function body is the modifier's body where
    /// `_` is replaced by the old function body.
    modifier onlyBefore(uint _time) { if (now >= _time) throw; _ }
    modifier onlyAfter(uint _time) { if (now <= _time) throw; _ }

    function OddsAndEvens(uint _biddingTime,
                        uint _revealTime)
    {
        auctionStart = now;
        biddingEnd = now + _biddingTime;
        revealEnd = biddingEnd + _revealTime;
    }
    /// ******these comments are not relevant but ill leave them****
    /// Place a blinded choice with `_blindedChoice` = sha3(value,
    /// fake, secret).
    /// The sent ether is only refunded if the bid is correctly
    /// revealed in the revealing phase. The bid is valid if the
    /// ether sent together with the bid is at least "value" and
    /// "fake" is not true. Setting "fake" to true and sending
    /// not the exact amount are ways to hide the real bid but
    /// still make the required deposit. The same address can
    /// place multiple bids.
    function choice(bytes32 _blindedChoice)
        onlyBefore(biddingEnd)
    {
        //check if two bids have happened
        if (choiceCount == 2)
            throw;

        choices[msg.sender].push(Choice({
            blindedChoice: _blindedChoice,
            deposit: msg.value
        }));
        //raise the counter for every bet, not used yet
        choiceCount += 1;
    }

    /// Reveal your blinded bids. You will get a refund for all
    /// correctly blinded invalid bids and for all bids except for
    /// the totally highest.
    /// changed _secret to uint couldn't be bytes32 to work
    /// Here I only want to allow two players and _secret will be 
    ///used when  this code functions properly
    function reveal(uint _values, uint _secret)
        onlyAfter(biddingEnd)
        onlyBefore(revealEnd)
    {
        if (revealCount == 2)
            throw;
        //seemingly since each player bets once I can use the 
        //array position [0] for both players???
        var bid = choices[msg.sender][0];  

        if (revealCount == 0)    
            bid = choices[msg.sender][0];
            playerOneChoice = _values;
            revealCount = 1;
            playerOneAddress = msg.sender;
            return;

        if (revealCount == 1)   
            bid = choices[msg.sender][0];
            playerTwoChoice = _values;
            revealCount = 2;
            playerTwoAddress = msg.sender;
            return;   
     }        

    function auctionEnd()
        onlyAfter(revealEnd)
    {
        if (ended) throw;
        //for now I input '1' and '1' for both player values
        //later I will change this to test for evenness
        sumChoices = playerOneChoice + playerTwoChoice;
        // if (sumChoices % 2 == 0)
        if (sumChoices == 2)
            gameWinner = playerOneAddress;
            OddsOrEvens = 2;
        // if (sumChoices % 2 != 0)    
        if (sumChoices == 3)
            gameWinner = playerTwoAddress;
            OddsOrEvens = 1;

        AuctionEnded(gameWinner, OddsOrEvens);
        // We send all the money we have, because some
        // of the refunds might have failed.

        //this should send the money to playerOneAddress for having 
        //both inputs from players as 1
        gameWinner.send(this.balance);
        ended = true;
    }

    function () { throw; }
}
4
  • I dunno if this is suspect: //seemingly since each player bets once I can use the //array position [0] for both players??? var bid = choices[msg.sender][0];
    – JTicker12
    Commented Mar 8, 2016 at 9:59
  • gameWinner.send(this.balance); - probably not this, but you should always always check the return code. I'd suggest logging an event if it fails. In general this is too much code to analyze. I'd suggest writing unit tests until you find the bug.
    – Paul S
    Commented Mar 9, 2016 at 0:50
  • ah ha, yes fair enough thanks. I started in that direction while awaiting a response. You give me confidence to continue. I believe if sumChoices = 2; then I do get payouts to playerOneAddress so that's a clue for me. Thanks this gives me lots to try for now!
    – JTicker12
    Commented Mar 9, 2016 at 15:50
  • So, this compiles but runs out of gas?
    – Joël
    Commented Oct 2, 2016 at 4:11

1 Answer 1

3

There's one obvious "oddness" in this code. The code has numerous sections such as this:

   if (revealCount == 0)    
        bid = choices[msg.sender][0];
        playerOneChoice = _values;
        revealCount = 1;
        playerOneAddress = msg.sender;
        return;

where it looks oddly indented. The if statement only 'protects' the bid=choices... line.

This looks very much like some language where the code's scope depends on its indentation. In solidity (like Javascript and C++), you would need to surround the indented code with brackets to have it scoped by the if statement:

    if (test) {
       true condition code here
    } else {
       false here
    }

This is the first thing I would check.

Your Answer

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

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