This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RockPaperScissorsGame.sol
127 lines (105 loc) · 6.11 KB
/
RockPaperScissorsGame.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
pragma solidity ^0.4.18;
import "fmg-core/contracts/State.sol";
import "./RockPaperScissorsState.sol";
contract RockPaperScissorsGame {
using RockPaperScissorsState for bytes;
string constant RESOLUTION_A_SAME = "The resolution for player A must be the same between states.";
string constant RESOLUTION_B_SAME = "The resolution for player B must be the same between states.";
string constant INTEGER_OVERFLOW_PREVENT = "Preventing a integer overflow attack";
string constant SAME_STAKE = "The stake should be the same between states";
// The following transitions are allowed:
//
// Start -> RoundProposed
// RoundProposed -> Start // reject game
// RoundProposed -> RoundAccepted
// RoundAccepted -> Reveal
// Reveal -> Start
// Start -> Concluded
//
function validTransition(bytes _old, bytes _new) public pure returns (bool) {
if (_old.positionType() == RockPaperScissorsState.PositionType.Start) {
if (_new.positionType() == RockPaperScissorsState.PositionType.RoundProposed) {
validateStartToRoundProposed(_old, _new);
return true;
} else if (_new.positionType() == RockPaperScissorsState.PositionType.Concluded) {
validateStartToConcluded(_old, _new);
return true;
}
} else if (_old.positionType() == RockPaperScissorsState.PositionType.RoundProposed) {
if (_new.positionType() == RockPaperScissorsState.PositionType.Start) { // game rejected
validateRoundProposedToRejected(_old, _new);
return true;
} else if (_new.positionType() == RockPaperScissorsState.PositionType.RoundAccepted) {
validateRoundProposedToRoundAccepted(_old, _new);
return true;
}
} else if (_old.positionType() == RockPaperScissorsState.PositionType.RoundAccepted) {
if (_new.positionType() == RockPaperScissorsState.PositionType.Reveal) {
validateRoundAcceptedToReveal(_old, _new);
return true;
}
} else if (_old.positionType() == RockPaperScissorsState.PositionType.Reveal) {
if (_new.positionType() == RockPaperScissorsState.PositionType.Start) {
validateRevealToStart(_old, _new);
return true;
}
}
revert("No valid transition found for states");
}
function winnings(RockPaperScissorsState.Play firstPlay, RockPaperScissorsState.Play secondPlay, uint256 stake)
private pure returns (uint256, uint256) {
if (firstPlay == secondPlay) { // no-one won
return (stake, stake);
} else if ((firstPlay == RockPaperScissorsState.Play.Rock && secondPlay == RockPaperScissorsState.Play.Scissors) ||
(firstPlay > secondPlay)) { // first player won
return (2 * stake, 0);
} else { // second player won
return (0, 2 * stake);
}
}
// transition validations
function validateStartToRoundProposed(bytes _old, bytes _new) private pure {
require(_new.stake() == _old.stake());
require(_old.aResolution() >= _new.stake(),INTEGER_OVERFLOW_PREVENT); // avoid integer overflow attacks
require(_old.bResolution() >= _new.stake(),INTEGER_OVERFLOW_PREVENT); // avoid integer overflow attacks
require(_new.aResolution() == _old.aResolution(),RESOLUTION_A_SAME); // resolution unchanged
require(_new.bResolution() == _old.bResolution(),RESOLUTION_B_SAME); // resolution unchanged
// we should maybe require that aPreCommit isn't empty, but then it will only hurt a later if it is
}
function validateStartToConcluded(bytes _old, bytes _new) private pure {
require(_new.stake() == _old.stake());
require(_new.aResolution() == _old.aResolution(),RESOLUTION_A_SAME);
require(_new.bResolution() == _old.bResolution(), RESOLUTION_B_SAME);
}
function validateRoundProposedToRejected(bytes _old, bytes _new) private pure {
require(_new.stake() == _old.stake());
require(_new.aResolution() == _old.aResolution(),RESOLUTION_A_SAME); // resolution unchanged
require(_new.bResolution() == _old.bResolution(),RESOLUTION_B_SAME); // resolution unchanged
}
function validateRoundProposedToRoundAccepted(bytes _old, bytes _new) private pure {
// a will have to reveal, so remove the stake beforehand
require(_new.aResolution() == _old.aResolution() - _old.stake(),"Resolution for player A should be decremented by 1 stake from the previous state.");
require(_new.bResolution() == _old.bResolution() + _old.stake(),"Resolution for player B should be incremented by 1 stake from the previous state.");
require(_new.stake() == _old.stake(),SAME_STAKE);
require(_new.preCommit() == _old.preCommit(),"Precommit should be the same as the previous state.");
}
function validateRoundAcceptedToReveal(bytes _old, bytes _new) private pure {
uint256 aWinnings;
uint256 bWinnings;
require(_new.stake() == _old.stake(),SAME_STAKE);
require(_new.bPlay() == _old.bPlay(),"Player Bs play should be the same between states.");
// check hash matches
// need to convert Play -> uint256 to get hash to work
bytes32 hashed = keccak256(abi.encodePacked(uint256(_new.aPlay()), _new.salt()));
require(hashed == _old.preCommit(),"The hash needs to match the precommit");
// calculate winnings
(aWinnings, bWinnings) = winnings(_new.aPlay(), _new.bPlay(), _new.stake());
require(_new.aResolution() == _old.aResolution() + aWinnings,"Player A's resolution should be updated with the winning");
require(_new.bResolution() == _old.bResolution() - 2 * _old.stake() + bWinnings,"Player B's resolution should be updated with the winning");
}
function validateRevealToStart(bytes _old, bytes _new) private pure {
require(_new.stake() == _old.stake(),SAME_STAKE);
require(_new.aResolution() == _old.aResolution(),RESOLUTION_A_SAME);
require(_new.bResolution() == _old.bResolution(),RESOLUTION_B_SAME);
}
}