Skip to content

Instantly share code, notes, and snippets.

@osteotek
Created June 1, 2018 12:06
Show Gist options
  • Save osteotek/075d711e73ece69c0a25a4c937b92b25 to your computer and use it in GitHub Desktop.
Save osteotek/075d711e73ece69c0a25a4c937b92b25 to your computer and use it in GitHub Desktop.
ethereum private chain anchoring
pragma solidity ^0.4.18;
contract PrivateChain {
struct BLOCK {
address miner;
address validator;
uint time;
uint txListHash;
}
enum NewBlockStatus {NONE, WAIT_FOR_VALIDATION}
address[] public participants;
BLOCK[] public blocks;
NewBlockStatus public status;
BLOCK public candidate;
uint public currentMiner = 0;
function PrivateChain () public {
status = NewBlockStatus.NONE;
participants.push(msg.sender);
}
function addBlockCandidate(uint txListHash) public {
assert(status == NewBlockStatus.NONE);
assert(msg.sender == participants[currentMiner]);
status = NewBlockStatus.WAIT_FOR_VALIDATION;
candidate.miner = msg.sender;
candidate.time = now;
candidate.txListHash = txListHash;
}
function confirmBlock(uint expectedTxListHash) public {
assert(status == NewBlockStatus.WAIT_FOR_VALIDATION);
assert(msg.sender == participants[(currentMiner + 1) % participants.length]);
assert(candidate.txListHash == expectedTxListHash);
candidate.validator = msg.sender;
blocks.push(candidate);
status = NewBlockStatus.NONE;
currentMiner = (currentMiner + 1) % participants.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment