Skip to content

Instantly share code, notes, and snippets.

@tscs37
Last active August 2, 2016 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tscs37/dccee713495dd1502f8dfede1740dda8 to your computer and use it in GitHub Desktop.
Save tscs37/dccee713495dd1502f8dfede1740dda8 to your computer and use it in GitHub Desktop.
A Beacon Storage Contract
contract EthereumTransactionBeacons {
// Beacon Pool
mapping (bytes32 => uint256) beacons;
// Event so that online nodes can immediately know of a new Beacon in the Pool
event NewBeacon(uint beaconNumber, bytes32 beaconHash);
// Every n-th Block is a Beacon Candidate
// Max: 254
uint constant BeaconInterval = 128;
// Last Beacon Saved to the Pool as Block Hash
bytes32 public lastBeaconHash = 0x0;
// Last Beacon Saved to the Pool as Block Number
uint public lastBeaconNumber = 0;
// Do not accept money
function () {
throw;
}
// Sets the Genesis Beacon
function EthereumTransactionBeacons() {
if (msg.value != 0) { throw; }
saveBeacon();
}
// saveBeacon()
// Params: none
// Saves a beacon to the blockchain
function saveBeacon() public {
if (msg.value != 0) { throw; }
lastBeaconNumber = block.number - (block.number % BeaconInterval);
lastBeaconHash = block.blockhash(lastBeaconNumber );
beacons[lastBeaconHash] = lastBeaconNumber + 1;
NewBeacon(lastBeaconNumber, lastBeaconHash);
}
// checkBeacon(blockHash) const
// Params:
// - blockHash: A block hash that is to be checked if it is beacon or not
// Returns:
// - uint256: The blocknumber of the beacon or ~uint256(0);
// if the output is smaller than the current blocknumber, it's a beacon
function checkBeacon(bytes32 blockHash) constant public returns(uint256) {
return beacons[blockHash] - 1;
}
// getNextBeaconNumber() const
// Returns:
// - uint256: The block number of the next beacon candidate
function getNextBeaconNumber() constant public returns (uint256) {
return block.number - (block.number % BeaconInterval) + BeaconInterval;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment