Skip to content

[DO-NOT-MERGE] randomized voting prototype #71

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

Open
wants to merge 1 commit 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
31 changes: 2 additions & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,2 @@
*.swp
*.swo

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed
allFiredEvents
scTopics

# node-waf configuration
.lock-wscript

# Dependency directory
node_modules
installed_contracts

# Debug log from npm
npm-debug.log

# local env variables
.env

# lol macs
*.DS_Store
node_modules/*
build/*
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.23;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
45 changes: 45 additions & 0 deletions contracts/Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
pragma solidity^0.4.24;

contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address=> uint)) public allowed;
event transfer_event(address from, address to, uint value);

constructor() public {
balances[msg.sender] = 10000;

}
function transfer(address from, address to, uint value) public {
require(msg.sender == from);
require(balances[msg.sender]>=value);
balances[msg.sender]-= value;
balances[to]+= value;
emit transfer_event(from,to,value);
}


function approve(address spender, uint value) public {
require(get_balance(msg.sender) >= value);
allowed[msg.sender][spender] = value;
}

function send_from(address sender, address receiver, uint value) public {
require( allowed[sender][msg.sender] >= value);
allowed[sender][msg.sender] -= value;
balances[sender] -= value;
balances[receiver] +=value;
emit transfer_event(sender, receiver,value);
}

function get_balance(address user) public view returns(uint){
return(balances[user]);
}

function is_allowed(address from, address sender) public view returns(uint) {
return(allowed[from][sender]);
}




}
141 changes: 141 additions & 0 deletions contracts/rcc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
pragma solidity^0.4.24;
import "./Token.sol";

contract RCC {
// to do: make it so that voters can't see each other until reveal stage
struct Challenge {
//mapping(address => uint) public secret_votes;
mapping(address => bool) won_lottery;
mapping(address => bytes32) concealed_votes;
mapping(address => bool) open_votes;
mapping(address => bool) has_voted;
mapping(address => bool) has_revealed;
mapping(address => uint) rewards;

uint votes_counter;
uint revealed_votes_counter;
bool recruiting_votes;
bool reveal_stage;
bool finished;
uint yes_vote_counter;
bool result;
bool can_claim_rewards;

}
uint counter;
uint vote_deposit;
uint votes_per_challenge;
// we keep track of who staked how how much
mapping(address => uint) public stakes;
// we need to track how many challenges the agents participate in, to ensure they can't withdraw stake in the middle of vote
mapping(address => uint) public num_active_challenges;
// also the total number of active challenges
uint public total_active_challenges;
// we index challenges with ints
mapping(uint => Challenge) public challenges;


Token public token;
uint public throughput; // this is actually throughput times 1000, to avoid fixed point types. Hacky, need a better fix.

constructor(address token_address) public {
counter = 0;
vote_deposit = 1;
votes_per_challenge = 3;
token = Token(token_address);
throughput = 990;

}

function create_challenge() public returns(uint) {
challenges[counter] = Challenge({votes_counter: 0, revealed_votes_counter: 0, recruiting_votes: true, reveal_stage: false, finished:false, yes_vote_counter:0, result:false, can_claim_rewards: false});
counter +=1;
total_active_challenges+=1;
return(counter-1);
}

function claim_lottery_win(uint challenge_id) public {

uint p = stakes[msg.sender] * throughput /total_active_challenges;
uint256 random = uint256(uint256(keccak256(abi.encodePacked(block.blockhash(block.number-1), 420))) % 1000);
if (p>= random) {
challenges[challenge_id].won_lottery[msg.sender] = true;
}
}

function submit_concealed_vote(bytes32 concealed_vote,uint challenge_id) public {
require(token.is_allowed(msg.sender,this) >= vote_deposit);
require(challenges[challenge_id].recruiting_votes);
require(challenges[challenge_id].has_voted[msg.sender] == false);
num_active_challenges[msg.sender] +=1;
token.send_from(msg.sender,this, vote_deposit);
challenges[challenge_id].votes_counter +=1;
challenges[challenge_id].has_voted[msg.sender] = true;
challenges[challenge_id].concealed_votes[msg.sender] = concealed_vote;
//if (vote_value) {
// challenge.yes_vote_counter +=1;
//}
if (challenges[challenge_id].votes_counter>=votes_per_challenge){
challenges[challenge_id].reveal_stage = true;
challenges[challenge_id].recruiting_votes=false;
//tally_up(challenge_id);
}

}

function reveal_vote(bool vote,uint challenge_id, uint salt) public {
// vote is in the reveal stage
require(challenges[challenge_id].reveal_stage);
// voter has voted
require(challenges[challenge_id].has_voted[msg.sender]);
// voter hasn't revealed the vote yet
require(!challenges[challenge_id].has_revealed[msg.sender]);
// the vote is consistent with the hash
uint vote_int = 0;
if (vote) {
vote_int = 1;
}


require(keccak256(vote_int, salt) == challenges[challenge_id].concealed_votes[msg.sender]);
challenges[challenge_id].open_votes[msg.sender] = vote;
if (vote) {
challenges[challenge_id].yes_vote_counter +=1;
}
challenges[challenge_id].revealed_votes_counter +=1;
challenges[challenge_id].has_revealed[msg.sender] = true;
if (challenges[challenge_id].revealed_votes_counter>= votes_per_challenge) {
challenges[challenge_id].reveal_stage = false;
challenges[challenge_id].finished = true;
tally_up(challenge_id);
}

}

function tally_up(uint challenge_id) public {
require(challenges[challenge_id].finished);
if (2*challenges[challenge_id].yes_vote_counter>= votes_per_challenge) {
challenges[challenge_id].result = true;
}
challenges[challenge_id].can_claim_rewards = true;
total_active_challenges-=1;

//challenge.tokens_distributed=true
}

function challenge_finished(uint challenge_id) public returns(bool) {
return(challenges[challenge_id].finished);
}
function challenge_result(uint challenge_id) public returns(bool) {
return(challenges[challenge_id].result);
}

function claim_reward(uint challenge_id) public {
// can this fail due to rounding or something?
require(challenges[challenge_id].can_claim_rewards);
token.transfer(this,msg.sender,challenges[challenge_id].rewards[msg.sender]);
challenges[challenge_id].rewards[msg.sender] = 0;
num_active_challenges[msg.sender]-=1;

}
}
5 changes: 5 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
deployer.deploy(Migrations);
};
11 changes: 11 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var Token = artifacts.require("Token");
var RCC = artifacts.require("RCC");

module.exports = function(deployer) {
deployer.deploy(Token).then(() => { return deployer.deploy(RCC,Token.address)});
}

//module.exports = function(deployer) {

//}
//};
Loading