Looping through a mapping variable.

nsubrahmnsubrahm Member Posts: 3
This is my contract where I am building a list of members with buildList function. The members are then expected to use the addVote function. The allMembersVoted function always seems to return false even though the Remix debugger shows that, the function does return true correctly.

Can you please help me find out what is missing?

The flow is like this:
  1. Create new contract
  2. Build list
  3. Vote
  4. Check if every one has voted
  5. Post-process, if every one has voted (not shown here).
   
    struct ListMembers {
        bytes32 listOwner;
        bytes32[] members;
    }
    ListMembers public lm;
    mapping(bytes32 => bool) public addedMembers;

    // Constructor to initialise the ListMembers struct
    function Lister(bytes32 listOwner) {
        lm.listOwner = listOwner;
        lm.members.length = 1;
        lm.members[0] = listOwner;
    }

    function buildList(bytes32[] listMembers) {
        lm.members.length = listMembers.length + 1;
        for (uint i = 0; i < listMembers.length; i++) {
            lm.members[i + 1] = listMembers[i];
        }
    }
    function addVote(bytes32 member, uint256 number) {
        if (memberAdded(member) == true) { // Check double votes
            return;
        } else {
            addedMembers[member] = true; // Confirm that vote added
            if (memberIsOwner(member) == true) {
                // Add vote with weight
            } else {
                // Add vote without weight 
            }
        }
    }

    function memberAdded(bytes32 member) returns (bool) {
        return addedMembers[member];
    }

    function allMembersVoted() returns (bool) {
        bool voted = true;
        for (uint i = 0; i < lm.members.length; i++) {
            voted = voted && addedMembers[lm.members[i]];
        }

        return voted;
    }

Comments

Sign In or Register to comment.