1

My question is about the Ethereum DAO Association example found here.

My question is on this bit of code:

struct Proposal {
    address recipient;
    uint amount;
    string description;
    uint votingDeadline;
    bool executed;
    bool proposalPassed;
    uint numberOfVotes;
    bytes32 proposalHash;
    Vote[] votes;
    mapping (address => bool) voted;
}
/*....*/

function vote(uint proposalNumber, bool supportsProposal)
    onlyShareholders
    returns (uint voteID)
{
    Proposal p = proposals[proposalNumber];
    if (p.voted[msg.sender] == true) throw;

    voteID = p.votes.length++;
    p.votes[voteID] = Vote({inSupport: supportsProposal, voter: msg.sender});
    p.voted[msg.sender] = true;
    p.numberOfVotes = voteID +1;
    Voted(proposalNumber,  supportsProposal, msg.sender);
}

function executeProposal(uint proposalNumber, bytes transactionBytecode) returns (int result) {
    Proposal p = proposals[proposalNumber];
    /* Check if the proposal can be executed */
    if (now < p.votingDeadline  /* has the voting deadline arrived? */
        ||  p.executed        /* has it been already executed? */
        ||  p.proposalHash != sha3(p.recipient, p.amount, transactionBytecode)) /* Does the transaction code match the proposal? */
        throw;

    /* tally the votes */
    uint quorum = 0;
    uint yea = 0;
    uint nay = 0;

    for (uint i = 0; i <  p.votes.length; ++i) {
        Vote v = p.votes[i];
        uint voteWeight = sharesTokenAddress.balanceOf(v.voter);
        quorum += voteWeight;
        if (v.inSupport) {
            yea += voteWeight;
        } else {
            nay += voteWeight;
        }
    }

    /* execute result */
    if (quorum <= minimumQuorum) {
        /* Not enough significant voters */
        throw;
    } else if (yea > nay ) {
        /* has quorum and was approved */
        p.executed = true;
        if (!p.recipient.call.value(p.amount * 1 ether)(transactionBytecode)) {
            throw;
        }
        p.proposalPassed = true;
    } else {
        p.proposalPassed = false;
    }
    // Fire Events
    ProposalTallied(proposalNumber, result, quorum, p.proposalPassed);
}

Specifically,

If we add new votes doing voteID = p.votes.length++; p.votes[voteID] = /*...*/ and then count them doing (uint i = 0; i < p.votes.length; ++i) isn't there an index problem?

Here is the reasoning:

Initially the votes array is 0 hence p.votes.length = 0 and p.votes.length++ = 1 and then we increase from there. However, the loop starts counting from 0.

2

Like with many other programming language the ++ operator is very special here

voteID = p.votes.length++; 
//Translates to 
voteID = p.votes.length=p.votes.length+1;

//And using operator precedence when = is involved it further Translates
// to  the following sequence of operations

voteID = p.votes.length;
p.votes.length=p.votes.length+1; // one element got added at 0th index,so the lengh is 1

Would it make sense ?

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.