In the previous post we started to look at how to implement a doodle using smart contracts written in DAML. The last code snippet we looked at was the IssueInvites choice, which is used to send out invites to each of the voters. There’s one more detail about the code in that choice that I’d like to point out.

Divulged Contracts

The Doodle Voting Invite

Now that we’ve seen how the doodle organizer issues voting invites, let’s have a look at what those invites look like:

The Doodle’s CastVote Choice

To quickly recap: in order for a voter to effectively vote, they need to receive a DoodleInvite contract and exercise the Vote choice on it. The Vote choice will then look up he Doodle contract by name and organizer, and exercise CastVote on it.

CastVote Parameters

CastVote takes three parameters:

  • the option the voter wants to cast a vote for
  • the ID of the DoodleInvite the voter has received

Making sure the vote is legitimate

In line 8 we are fetching the invite contract. If we succeed, it will guarantee that the invite provided by the user actually exists. Once we get hold of the invite payload, we can go on and assert a certain number of conditions to make sure the vote is legitimate:

  • in line 10, we check that the party exercising the CastVote choice is the one who received the invite. This check is defending against malicious parties hijacking the Doodle and voting on behalf of somebody they are not. Only if the controller party is able to produce an invite in their name is the vote allowed to go through.
  • in line 11 we make sure the invite provided by the voter was issued by the Doodle’s organizer. This defends against a situation where a voter with a valid invite for doodle A would try to use that to vote on a Doodle with the same name, but created by a different organizer.
  • in line 12 we check that the Doodle is open for voting
  • and finally in lines 13 and 14, we check that the controller is actually a registered voter and that the option they’re trying to cast a vote for is a valid one for this Doodle.

Using TextMap to keep count of the votes

Let’s zoom into the code that’s actually casting the vote.

Lead Architect at LiquidShare, building a cloud native, blockchain enabled, financial services SaaS platform.