Updates Instructions 4: Coding Smart Contract

Some more explanations for `revert with reason` and some grammar and stylistic changes.
This commit is contained in:
Andy Tudhope 2018-08-25 10:26:29 +02:00 committed by GitHub
parent 0d78c99dc3
commit 29901c00bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 5 deletions

View File

@ -24,7 +24,7 @@ emit NewPost(postId, msg.sender, _description);
```
#### Upvote/Downvote a post
We want to be able to asign a score to the posts. For that, let's work on the function `vote()` from which the users will be able to upvote or downvote each post. NOTE: this voting mechanism is very naive and not recomended to be used on real life due to not being Sybil resistant:
We want to be able to asign a score to the posts. For that, let's work on the function `vote()` from which the users will be able to upvote or downvote each post. NOTE: this voting mechanism is very naive and we recomend that you do not use it in production because it is not Sybil resistant:
1. Determine if the post exists, and if we have already voted
```
@ -33,6 +33,8 @@ require(p.creationDate != 0, "Post does not exist");
require(p.voters[msg.sender] == Ballot.NONE, "You already voted on this post");
```
You'll notice that we're using a new feature of Solidity here: `revert with reason`, which makes it significantly easier to debug your smart contracts. It should be fairly self-evident: just pass in a string stating the reason this `require` statement might fail as the second parameter so that you can track it easily. For a full discussion of this, go [here](https://github.com/ethereum/solidity/issues/1686).
2. Store the vote and update the post score
```
Ballot b = Ballot(_vote);
@ -49,10 +51,10 @@ p.voters[msg.sender] = b;
emit Vote(_postId, msg.sender, _vote);
````
> Building a Sybil resistant voting mechanism using a Proof of Identity that takes in account Privacy is a great challenge . How would you solve this problem?
> Building a Sybil resistant voting mechanism using a Proof of Identity that takes into account the privacy of users is a great challenge. How would you solve this problem?
#### Determine if an user can vote for a post
The users need to know somehow if they're able to vote on a post or not. There are two scenarios where a user should not be able to vote: a) The post doesn't exist, and b) The user already voted. This logic should be developed inside `canVote()`:
The users needs to know somehow if they're able to vote on a post or not. There are two scenarios where a user should not be able to vote: a) The post doesn't exist, and b) The user already voted. This logic should be developed inside `canVote()`:
1. Determine if the posts exists
```
@ -66,9 +68,9 @@ return (p.voters[msg.sender] == Ballot.NONE);
```
#### Determine the user's vote
It's possible an user would like to know what was his choice on a previously voted post. Implement this on `getVote()`.
It's possible that a user would like to be reminded about what was their choice was for a post they voted on previously. Implement this in `getVote()`.
```
Post storage p = posts[_postId];
return uint8(p.voters[msg.sender]);
```
> Notice that on the last two functions, the input and output values related to votes are uin8 instead of an enum. Enum are not a valid input/output value for functions at least on the current solidity version.
> Notice that, in the last two functions, the input and output values related to votes are `uint8` instead of an `enum`. This is because `enum`s are not a valid input/output value for functions, at least in the current version of Solidity.