concept remix IDE in category blockchain

appears as: Remix IDE
Blockchain in Action

This is an excerpt from Manning's book Blockchain in Action.

// include listing 3.1 data here
   
   // modifiers
   modifier validPhase(Phase reqPhase) 
    { require(state == reqPhase); 
      _; 
    } 
       
    constructor (uint numProposals) public  {
        chairperson = msg.sender;
        voters[chairperson].weight = 2; // weight 2 for testing purposes
        for (uint prop = 0; prop < numProposals; prop ++)
            proposals.push(Proposal(0));
        state = Phase.Regs; // change Phase to Regs
    
    }
       
    function changeState(Phase x) public {                                #1
        if (msg.sender != chairperson) {revert();}                        #2
        if (x < state ) revert();
        state = x;
    }
    
    
    function register(address voter) public validPhase(Phase.Regs) {      #3
        if (msg.sender != chairperson || voters[voter].voted) revert();   #2
        voters[voter].weight = 1;
        voters[voter].voted = false;
          
    }
 
   
    function vote(uint toProposal) public validPhase(Phase.Vote)  {       #3
       
        Voter memory sender = voters[msg.sender];
        if (sender.voted || toProposal >= proposals.length) revert();     #2
        sender.voted = true;
        sender.vote = toProposal;   
        proposals[toProposal].voteCount += sender.weight;
            
        
    }
 
    function reqWinner() public validPhase(Phase.Done) view returns (uint #3
    ➥ winningProposal) {                                                 #4
       
        uint winningVoteCount = 0;
        for (uint prop = 0; prop < proposals.length; prop++) 
            if (proposals[prop].voteCount > winningVoteCount) {
                winningVoteCount = proposals[prop].voteCount;
                winningProposal = prop;
            }
       
    }

Let’s study the problem and apply the design principles (DPs) you’ve learned, which are provided in appendix B. You’ll use DP 2 and DP 3 to guide you in defining the data structures. You’ll represent the design by using a contract diagram (DP 4) and the auction state transitions by using a finite state machine (FSM; DP 5), and you’ll use modifiers for any rules (DP 6) to be implemented by the smart contract. Applying the DPs provides a structured approach to starting blockchain solution development.