Skip to content

Instantly share code, notes, and snippets.

@alexvandesande
Last active July 21, 2016 16:27
Show Gist options
  • Select an option

  • Save alexvandesande/a1aae99267a79e82334b51f3e3448383 to your computer and use it in GitHub Desktop.

Select an option

Save alexvandesande/a1aae99267a79e82334b51f3e3448383 to your computer and use it in GitHub Desktop.
contract VoteRegistration {
// stores the information about the current node.
// This variable can only be changed via a hard fork
bytes32 public forkHash = sha3('');
// stores the array of all registered voters
mapping (address => address) public voteContracts;
// checks if the lock is active
bool public active = false;
// variables that will allow miners to activate the lock
uint public startTimer;
int public minerSupport;
// event used to count all votes
event VoteRegistered(address voteAddress, address owner);
// This contract creates a new LockVote contract
function registerVote () {
// can't sent ether now, one vote address per contract, before activation is on
if (msg.value > 0
|| voteContracts[msg.sender] > 0
|| active)
throw;
// create a new contract based on the LockVote template
// set the owner as the msg.sender
address newVoteContract = new LockVote(msg.sender);
// save on the mapping
voteContracts[msg.sender] = newVoteContract;
// emits an event log
VoteRegistered(newVoteContract, msg.sender);
}
// A function to start the fork countdown
function startCount(bool supportElection) {
// Only miners can call this contract
if (msg.sender != block.coinbase || active) throw;
// Check timer and reset countdown every 24 hours
if (startTimer + 24 hours < now) {
// if there is enough support activate the lock
if (minerSupport > 2000)
active = true;
// if there is an overwhelming support, deactivate the lock
else if (minerSupport < -4000)
active = false;
startTimer = now;
minerSupport = 0;
}
// Vote for or against activating the fork
if (supportElection)
minerSupport++;
else
minerSupport--;
}
}
contract LockVote {
// address of the voter registration contract
VoteRegistration parentContract;
// each fork can have different levels of support
mapping (bytes32 => uint) public lockDates;
// only the owner can touch this contract
address owner;
modifier onlyOwner {
if (msg.sender != owner) throw;
_
}
// Create the contract
function LockVote (address voteOwner) {
owner = voteOwner;
parentContract = VoteRegistration(msg.sender);
}
// Set your vote
function setVote(bytes32 supportingHash, uint duration) onlyOwner {
// If fork is already active then you can't do it
if (parentContract.active()) throw;
// Calculate the date based on today
if (lockDates[supportingHash] < now)
lockDates[supportingHash] = now;
uint newDate = lockDates[supportingHash] + duration * 1 days;
// Maximum 3 months, minimum 3 days from the moment the vote is made
// Miners will recalculate the duration based on when the vote takes place
if (newDate > now + 12 weeks)
throw;
// Set the lock date
lockDates[supportingHash] = newDate;
}
// allows money or tokens to be withdrawn only if fork is in place
// and if the date of the promise lock is past
function execute(address target, uint amount, bytes32 data) onlyOwner {
if (lockDates[parentContract.forkHash()] < now
&& parentContract.active()) throw;
// Maybe limit the gas here to 90k to avoid funny business?
target.call.value(amount)(data);
}
}
@doppio
Copy link

doppio commented Jul 1, 2016

// if there is enough support activate the lock
if (minerSupport > 2000)
    active = true;
// if there is an overwhelming support, deactivate the lock
else if (minerSupport < 4000)
    active = false;

Maybe I'm missing something, but why are you checking if miner support is less than 4000? And why is it an else-if? If it's not greater than 2000, it's definitely less than 4000. :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment