concept state in category blockchain

appears as: states, states, state, state
Blockchain in Action

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

FSM diagrams define the states that define the operational flow on execution of code, and the transitions among them. This a classic diagram from the theoretical foundations of mathematics and computer science. In the context of blockchain, it is used to define the states and state transitions when a smart contract executes; it’s a convenient tool for expressing the behavior of a smart contract.

Now you’re ready to apply design principle 3. Recall from the problem statement that the goal is for users to choose one of the many available proposals by voting on them. Based on the use case analysis in figure 3.3, the following are the users of the ballot system: the chairperson, the voters (including the chairperson), and anybody who’s interested in the outcome of the balloting process.

The data assets, in this case, are the proposals on which the voters are voting. You also need to keep track of the voters, whether they have voted or not, and the weight of their votes. (Recall that the chairperson is also a voter and that the chairperson’s vote counts as double, weight=2.) Let’s use this analysis as a guideline and code the two data items identified: voters and proposals, as shown in listing 3.1. The phases of the voting specified in the problem statement are also coded into an enum or enumerated data type. enum is an internal data type provided by Solidity. A Voter type and Proposal type are defined using the struct construct, and a special voter, chairperson, is also defined. A mapping data structure maps the voter account address to the voter details, and an array defines the proposals (numbers) that are being voted on.

// include the code from BallotV1.sol, not shown here
    
 enum Phase {Init, Regs, Vote, Done}                                     #1
    // Phase can take only 0,1,2,3 values: Others invalid

 Phase public state = Phase.Init;

 constructor (uint numProposals) public  {                               #2
        chairperson = msg.sender;                                        #2
        voters[chairperson].weight = 2; // weight 2 for testing purposes
        for (uint prop = 0; prop < numProposals; prop ++)                #3
            proposals.push(Proposal(0));                                 #3
   
    }
    
    
    // function for changing Phase: can be done only by chairperson
    function changeState(Phase x) public {                               #4
       if (msg.sender != chairperson) {revert();}                        #5
       if (x < state) revert();                                          #6
       state = x;
    }  
    }
Permissioned Blockchains in Action

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

With those preliminaries out of the way, let us look at the state of computing today.

  • Now, deploy the Dealer contract in Dealer.sol to the Quorum network from node 1 (i.e. port 22000). It is recommended to use Remix for this deployment as it gives you an easy way to monitor the state of the contract.
  • This is executed when the user clicks on the “Make Bet” button. Here, we retrieve the value of the bet from the text field with the ID “bet”: $('#bet').val()). We then call the makeBet method in Dealer with this value. The transaction that is thus formed is sent to API using send. Keep in mind that we have to use the send command to call functions that change the state of the blockchain. We then log the hash of the transaction and the error returned by send to the browser’s console.

    Dealer.methods.getWinner().call((err, result) => {
    console.log(result, err),
    $('#textid2').html("Winner for round " +result[1]+ " is " + result[0]);
    if (result[0] === Dealer.options.from) {
             $('#textid1').html("You won round " + result[1] + "!");
    }
    else
    $('#textid1').html("");
    });

    Here we are calling the getWinner method in the contract. Now, since getWinner is a view function, we know that it does not change the state of the blockchain. You can verify this by looking at the ABI which says: "stateMutability": "view" for getWinner. This is why we use call here instead of send. We know that getWinner returns the winner and the round number (again, verifiable by looking at the ABI) so we can parse the results accordingly. We first print the result in the textid2 heading. Then, we check if the winner is the same as the address used by this player. If it is then we print a congratulatory message to textid1 else we make it blank.