Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to borda #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lesson3_violations/Borda/BordaMissingRule.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Borda Missing Rule by Perito Flores
* -------------
*
* Verification of a simple voting contract which uses a Borda election.
* See https://en.wikipedia.org/wiki/Borda_count
*/



methods {
function points(address) external returns uint256 envfree;
function vote(address,address,address) external;
function voted(address) external returns bool envfree;
function winner() external returns address envfree;
}



rule BordaMissingRule(env e,method m){

require m.selector != sig:vote(address,address,address).selector;
address winnerBefore = winner();
calldataarg args;
m(e,args);
address winnerAfter = winner();
assert winnerAfter == winnerBefore , "The winner can be changed only after voting";
}
52 changes: 52 additions & 0 deletions lesson3_violations/Borda/BordaNewBug.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pragma solidity ^0.8.0;
import "./IBorda.sol";

contract BordaNewBug is IBorda{

// The current winner
address public _winner;

// A map storing whether an address has already voted. Initialized to false.
mapping (address => bool) _voted;

// Points each candidate has recieved, initialized to zero.
mapping (address => uint256) _points;

// current maximum points of all candidates.
uint256 public pointsOfWinner;


function vote(address f, address s, address t) public override {
require(!_voted[msg.sender], "this voter has already cast its vote");
require( f != s && f != t && s != t, "candidates are not different");
_voted[msg.sender] = true;
voteTo(f, 3);
voteTo(s, 2);
voteTo(t, 1);
}

function voteTo(address c, uint256 p) private {
//update points:w
_points[c] = _points[c]+ p;
// update winner if needed
if (_points[c] > _points[_winner] ) {
_winner = c;
}
}
function switchWinner(address newWinner) external {
require (points(newWinner)>=points(_winner));
_winner = newWinner;
}

function winner() external view override returns (address) {
return _winner;
}

function points(address c) public view override returns (uint256) {
return _points[c];
}

function voted(address x) public view override returns(bool) {
return _voted[x];
}
}
7 changes: 7 additions & 0 deletions lesson3_violations/Borda/BordaNewBugRuns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo Runing the specs...

certoraRun Borda.sol --verify Borda:BordaMissingRule.spec --msg 'Missing rule also pass in the original'

certoraRun BordaNewBug.sol --verify BordaNewBug:Borda.spec --msg 'Original rules passes in the buggy file'

certoraRun BordaNewBug.sol --verify BordaNewBug:BordaMissingRule.spec --msg 'New rule catches the bug'