Taxonomy Icon

Blockchain

To create a successful voting application, you need to ensure that the votes can never be changed or altered. A blockchain’s immutability provides this assurance. In this tutorial, you’ll use the Hyperledger Composer Playground and some provided sample code to create, deploy locally, and experiment with a prototype for a secure and fair voting application.

The Hyperledger Composer Playground is the user interface contained in the open source Hyperledger Composer. The Playground is ideal for configuring, deploying, and testing a business blockchain network. Advanced features help you manage the security of your blockchain network, invite participants to your network, and connect to multiple networks.

Learning objectives

  • Create the logic and permissions for a voting application on a blockchain network
  • Deploy your application locally
  • Test your application

Time to complete this tutorial

  • Approximately 10 minutes

Prerequisites

All you need to complete the tutorial is a browser and some familiarity with JavaScript.

1

Deploy and connect to an empty business network

In this step, you’ll create a business network card, which provides the configuration information needed to connect to a blockchain network.

  1. Go to the Hyperledger Composer Playground.
  2. Click Let’s Blockchain!
  3. Click Deploy a new business network.
  4. On the page that appears, scroll down to step 2 and click empty-business-network.
  5. Click Deploy.
  6. Click Connect now.
2

Fill in the model file

A model file uses Hyperledger Composer modeling language to specify the participants who will execute transactions, the assets that will move across the network, and the transactions themselves. In our model file for the voting application, the voters are the participants, the party they’re voting for is the asset, and the act of voting is the transaction.

  1. Click Model File in the left navigator.
  2. Copy and paste this sample code into the Model File window (overlay the code that’s initially there):
    
    /*
      Write your model definitions here
     */
    namespace org.acme.voting
    
    participant voter identified by voterID {
      o String voterID
      o String fullName
    }
    
    asset ifVoted identified by voterID {
      o String voterID
      o Boolean votedOrNot
    }
    
    asset candidateVote identified by politicalParty {
      o String politicalParty
      o Integer totalVote
    }
    transaction vote {
      --> candidateVote candidateVoteAsset
      --> ifVoted ifVotedAsset
    }
    
3

Fill in the script file

Next you need to create a script file, using JavaScript, to define the actual business logic and functionality of the voting application. For the voting application, the script file defines the voting logic and rules, such as one vote per participant.

  1. In the left navigator, click Add a file….
  2. On the “Add a file” panel, select Script File and click Add.
  3. Copy and paste this code into the Script File window (overlay the code that’s initially there):
'use strict';
/*
  Write your transaction processor functions here
 */

/*
  Sample transaction
  @param {org.acme.voting.vote} vote
  @transaction
 */
function vote(tx) {
    if (!tx.ifVotedAsset.votedOrNot) {
        tx.candidateVoteAsset.totalVote = tx.candidateVoteAsset.totalVote + 1;
        return getAssetRegistry('org.acme.voting.candidateVote')
            .then(function (assetRegistry) {
                return assetRegistry.update(tx.candidateVoteAsset);
            })
            .then(function () {
                return getAssetRegistry('org.acme.voting.ifVoted')
                    .then(function (assetRegistry) {
                        tx.ifVotedAsset.votedOrNot = true;
                        return assetRegistry.update(tx.ifVotedAsset);
                    })
            });
    } else {
        throw new Error('Vote already submitted!');
    }
}
  1. Click Deploy.
4

Test your prototype

You can now create assets, add participants, and do some transactions to test your prototype.

  1. Click the Test tab, select Voter in the left nav, and then click Create New Participant.Starting to add participants
  2. Enter the voter’s ID after the hash sign, and enter the voter’s name. Add additional IDs and names as needed. Click Create New.Adding participants
  3. You should see a result like this:Result of adding participants
  4. Now add one or more assets. In the left nav, select candidateVote and click Create New Asset. When prompted, add a political party name. Add additional political party names as needed. Click Create New.Adding political parties
  5. In the left nav, select ifVoted and click Create New Asset. When prompted, add the voter ID that you entered in step 2. Repeat this step for all voters you added. Click Create New.Associating ifVoted with participants
  6. You should see a result like this:Result of associating ifVoted with participants
  7. Click Test at the top.Testing the result

Now let’s vote and test the transactions!

  1. In the lower left, click Submit Transaction.Submitting a transaction
  2. Enter the ID of the voter (line 3) and the political party he wants to vote for (line 2). Click Submit.Entering a vote
  3. You should see a result like this:Result of the vote
  4. You can submit any votes you like and verify that every voter can perform one vote only. To view the ledger of submitted votes, click All Transactions in the left nav. Result of the vote

Summary

Congratulations! You’ve now used Hyperledger Composer to build a quick prototype for a voting application. You can now export the prototype for deployment on the IBM Blockchain Platform Starter Plan by exporting the business network archive (.bna) file. You can then customize it for your needs and interests.

Next steps

  • Learn more about Hyperledger Composer and Hyperledger Fabric.

  • Learn how to create business networks from scratch using Hyperledger Composer in our popular series, Hyperledger Composer basics, by Steve Perry.

  • Try the new IBM Blockchain Starter Membership Plan. It gives you an economical way to kick-start a blockchain network where you can easily deploy and test sample blockchain applications. You can also deploy your business network model that you create using Hyperledger Composer Playground, as described in this article, to run on the Starter Plan.

  • Stay in the know with the monthly Blockchain Newsletter for developers. Check out the current issue and subscribe.

  • Check out the many code patterns, which provide roadmaps for solving complex problems with blockchain technology, and include architecture diagrams, code repos, and additional reading.

  • Stop by the Blockchain Developer Center. It’s your source for free tools and tutorials, along with code and community support, for developing and deploying blockchain solutions for business.