-
Notifications
You must be signed in to change notification settings - Fork 13
/
channel.sol
233 lines (200 loc) · 6.82 KB
/
channel.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
pragma solidity ^0.4.10;
// External interface
contract RebalanceAvailabilityContract {
function submitChallenge(bytes32 instanceHash) {}
function answerChallenge(
uint8[] V,
bytes32[] R,
bytes32[] S,
address[] participants,
bytes32 transactionMerkleTreeRoot) {}
function isChallengeSuccess(bytes32 instanceHash) returns(bool) {}
}
// Note: Initial version does NOT support concurrent conditional payments!
contract PaymentChannelRebalanceable {
// Blocks for grace period
uint constant DELTA = 10;
// Events
event EventInit();
event EventUpdate(int r);
event LogBytes32(bytes32 b);
event LogAddress(address a);
event LogInt(int i);
event LogUInt(uint ui);
event LogInts(int[2] i);
event LogUInts(uint[2] ui);
event LogBool(bool b);
event EventPending(uint T1, uint T2);
// Utility functions
modifier onlyplayers {
if (playermap[msg.sender] > 0)
_;
else
throw;
}
function max(uint a, uint b) internal returns(uint) {
if (a>b)
return a;
else
return b;
}
function min(uint a, uint b) internal returns(uint) {
if (a<b)
return a;
else
return b;
}
function verifySignature(address pub, bytes32 h, uint8 v, bytes32 r, bytes32 s) {
if (pub != ecrecover(h,v,r,s))
throw;
}
function verifyMerkleChain(bytes32 link, bytes32[] chain, bool[] markleChainLinkleft) {
for (uint i = 0; i < chain.length-1; i ++) {
link = markleChainLinkleft[i] ? sha3(chain[i], link) : sha3(link, chain[i]);
}
if(link != chain[chain.length-1])
throw;
}
///////////////////////////////
// State channel data
///////////////////////////////
int bestRound = -1;
enum Status { OK, PENDING }
Status public status;
uint deadline;
// Constant (set in constructor)
address[2] public players;
mapping (address => uint) playermap;
RebalanceAvailabilityContract public rac;
/////////////////////////////////////
// Payment Channel - Application specific data
////////////////////////////////////
// State channel states
int [2] public credits;
uint[2] public withdrawals;
// Externally affected states
uint[2] public deposits; // Monotonic, only incremented by deposit() function
uint[2] public withdrawn; // Monotonic, only incremented by withdraw() function
function sha3int(int r) constant returns(bytes32) {
return sha3(r);
}
function PaymentChannelRebalanceable(
RebalanceAvailabilityContract _rac,
address[2] _players) {
rac = _rac;
for (uint i = 0; i < 2; i++) {
players[i] = _players[i];
playermap[_players[i]] = i + 1;
}
EventInit();
}
// Increment on new deposit
function deposit() payable onlyplayers {
deposits[playermap[msg.sender]-1] += msg.value;
}
// Increment on withdrawal
function withdraw() onlyplayers {
uint i = playermap[msg.sender]-1;
uint toWithdraw = withdrawals[i] - withdrawn[i];
withdrawn[i] = withdrawals[i];
assert(msg.sender.send(toWithdraw));
}
// State channel update function
function update(uint[3] sig, int r, int[2] _credits, uint[2] _withdrawals)
onlyplayers {
// Only update to states with larger round number
if (r <= bestRound)
return;
// Check the signature of the other party
uint i = (3 - playermap[msg.sender]) - 1;
var _h = sha3(address(this), r, _credits, _withdrawals);
var V = uint8 (sig[0]);
var R = bytes32(sig[1]);
var S = bytes32(sig[2]);
verifySignature(players[i], _h, V, R, S);
// Update the state
credits[0] = _credits[0];
credits[1] = _credits[1];
withdrawals[0] = _withdrawals[0];
withdrawals[1] = _withdrawals[1];
bestRound = r;
EventUpdate(r);
}
// State channel update function when latest change was due to rebalance
function updateAfterRebalance(
uint8[] V,
bytes32[] R,
bytes32[] S,
address[] participants,
bytes32[] transactionMerkleChain,
bool[] markleChainLinkleft,
int r,
int[2] _credits,
uint[2] _withdrawals)
onlyplayers {
rac.answerChallenge(
V,
R,
S,
participants,
transactionMerkleChain[transactionMerkleChain.length-1]);
updateAfterRebalanceChallenged(
participants,
transactionMerkleChain,
markleChainLinkleft,
r,
_credits,
_withdrawals);
}
// State channel update function when latest change was due to rebalance
function updateAfterRebalanceChallenged(
address[] participants,
bytes32[] transactionMerkleChain,
bool[] markleChainLinkleft,
int r,
int[2] _credits,
uint[2] _withdrawals)
onlyplayers {
// Only update to states with larger round number
if (r <= bestRound)
return;
// Check that this rebalance instance was verified
var instanceHash = sha3(sha3(participants), transactionMerkleChain[transactionMerkleChain.length-1]);
assert(rac.isChallengeSuccess(instanceHash));
// Check that counterparty was a signatory
uint j = (3 - playermap[msg.sender]) - 1;
for (uint i = 0; i < participants.length; i++) {
if (participants[i] == players[j])
break;
else if (i == participants.length - 1)
throw;
}
// Verify transaction membership proof
var h = sha3(address(this), r, _credits, _withdrawals);
verifyMerkleChain(h, transactionMerkleChain, markleChainLinkleft);
// Update the state
credits[0] = _credits[0];
credits[1] = _credits[1];
withdrawals[0] = _withdrawals[0];
withdrawals[1] = _withdrawals[1];
bestRound = r;
EventUpdate(r);
}
// Causes a timeout for the finalize time
function trigger() onlyplayers {
assert( status == Status.OK );
status = Status.PENDING;
deadline = block.number + DELTA; // Set the deadline for collecting inputs or updates
EventPending(block.number, deadline);
}
function finalize() {
assert( status == Status.PENDING );
assert( block.number > deadline );
// Note: Is idempotent, may be called multiple times
// Withdraw the maximum amount of money
withdrawals[0] += uint(int(deposits[0]) + credits[0]);
withdrawals[1] += uint(int(deposits[1]) + credits[1]);
credits[0] = -int(deposits[0]);
credits[1] = -int(deposits[1]);
}
}