Last active
August 24, 2017 12:24
-
-
Save mcfearsome/e5234ae355fc84b5f8f450126d167c0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.11; | |
import "./owned.sol"; | |
contract Remit is owned { | |
uint public deadlineDelta = 100000; | |
enum ChallengeState { Active, Aborted, Claimed } | |
struct Challenge { | |
ChallengeState state; | |
address creator; | |
bytes32 passwordHash; | |
uint amount; | |
uint deadline; | |
} | |
uint numChallenges = 1; | |
mapping(uint => Challenge) public challengeIndex; | |
mapping(address => uint) public challengeByAddress; | |
mapping(bytes32 => uint) public challengeByHash; | |
function deposit(bytes32 passwordHash, uint deadline) payable { | |
require(msg.value > 0); | |
require(challengeByHash[passwordHash] == 0); | |
require(challengeByAddress[msg.sender] == 0); | |
if(deadline != 0) { | |
require(block.number + deadlineDelta >= deadline); | |
} | |
uint theIndex = numChallenges++; | |
challengeIndex[theIndex] = Challenge(ChallengeState.Active, msg.sender, passwordHash, msg.value, deadline); | |
challengeByAddress[msg.sender] = theIndex; | |
challengeByHash[passwordHash] = theIndex; | |
} | |
function abort() { | |
uint theIndex = challengeByAddress[msg.sender]; | |
require(theIndex != 0); | |
Challenge challenge = challengeIndex[theIndex]; | |
require(challenge.deadline != 0); | |
require(block.number > challenge.deadline); | |
require(challenge.state == ChallengeState.Active); | |
challenge.state = ChallengeState.Aborted; | |
resetAddressIndex(msg.sender); | |
msg.sender.transfer(challenge.amount); | |
} | |
function claim(bytes32 partOne, bytes32 partTwo) { | |
bytes32 claimHash = sha3(partOne, partTwo); | |
uint theIndex = challengeByHash[claimHash]; | |
require(theIndex != 0); | |
Challenge challenge = challengeIndex[theIndex]; | |
require(challenge.state == ChallengeState.Active); | |
challenge.state = ChallengeState.Claimed; | |
resetAddressIndex(challenge.creator); | |
msg.sender.transfer(challenge.amount); | |
} | |
// Allow address to create a new challenge | |
function resetAddressIndex(address toReset) internal { | |
challengeByAddress[toReset] = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you think of a data structure where we do not care about "index"?