forked from miladmostavi/gnosis-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateChannel.sol
133 lines (112 loc) · 4.42 KB
/
StateChannel.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
pragma solidity ^0.4.0;
contract Token {
function transfer(address to, uint256 value) returns (bool);
function transferFrom(address from, address to, uint256 value) returns (bool);
}
contract StateChannelProxy {
address public owner;
address public stateChannel;
bool public isSettled = false;
modifier isOwnerOrStateChannel () {
if (!(msg.sender == stateChannel || isSettled && msg.sender == owner))
throw;
_;
}
modifier isStateChannel () {
if (msg.sender != stateChannel)
throw;
_;
}
function sendTransaction(address destination, uint value, bytes data) isOwnerOrStateChannel {
if (!destination.call.value(value)(data))
throw;
}
function setSettled() isStateChannel {
isSettled = true;
}
function StateChannelProxy(address _owner) {
owner = _owner;
stateChannel = msg.sender;
}
}
/// @title State channel - Generic settlement contract for state channels.
/// @author Stefan George - <stefan.george@consensys.net>
contract StateChannel {
mapping (address => StateChannelProxy) public proxyContracts;
address[] owners;
State public state;
Token public securityToken;
uint public securityValue;
uint public challengePeriod;
struct State {
uint timestamp;
uint nonce;
bytes32 hash;
address requester;
bytes32[] txHashes;
uint txIndex;
}
function requestSettleZeroState() {
if (state.timestamp > 0 || !securityToken.transferFrom(msg.sender, this, securityValue))
throw;
state.timestamp = now;
state.requester = msg.sender;
}
function settleZeroState() {
if ( !(state.timestamp > 0 && state.timestamp + challengePeriod <= now && state.hash == 0)
|| !securityToken.transfer(msg.sender, securityValue))
throw;
for (uint i=0; i<owners.length; i++)
proxyContracts[owners[i]].setSettled();
}
function requestSettlement(bytes32 txsHash, uint nonce, uint timestamp, bytes32 hash, bytes32 secret, uint8[] sigV, bytes32[] sigR, bytes32[] sigS) {
bytes32 stateHash = sha3(txsHash, nonce, timestamp, hash);
for (uint i=0; i<owners.length; i++)
if (owners[i] != ecrecover(stateHash, sigV[i], sigR[i], sigS[i]))
throw;
if ( state.timestamp > 0
|| now > timestamp
|| hash > 0 && sha3(secret) != hash
|| !securityToken.transferFrom(msg.sender, this, securityValue))
throw;
state.timestamp = now;
state.nonce = nonce;
state.hash = stateHash;
state.requester = msg.sender;
}
function submitTradeHashes(bytes32[] txHashes, uint nonce, uint timestamp, bytes32 hash) {
bytes32 txsHash = sha3(txHashes);
bytes32 stateHash = sha3(txsHash, nonce, timestamp, hash);
if (state.hash == stateHash && state.timestamp + challengePeriod <= now)
state.txHashes = txHashes;
}
function executeTrade(address sender, address destination, uint value, bytes data) {
bytes32 txHash = sha3(sender, destination, value, data);
if (state.txHashes[state.txIndex] == txHash) {
StateChannelProxy(sender).sendTransaction(destination, value, data);
state.txIndex += 1;
if (state.txIndex == state.txHashes.length)
for (uint i=0; i<owners.length; i++)
proxyContracts[owners[i]].setSettled();
}
}
function punishWrongState(bytes32 txHash, uint nonce, uint timestamp, bytes32 hash, uint8[] sigV, bytes32[] sigR, bytes32[] sigS) {
bytes32 stateHash = sha3(txHash, nonce, timestamp, hash);
for (uint i=0; i<owners.length; i++)
if (owners[i] != ecrecover(stateHash, sigV[i], sigR[i], sigS[i]))
throw;
if ( state.nonce > nonce
|| now > timestamp
|| !securityToken.transfer(msg.sender, securityValue))
throw;
delete state;
}
function StateChannel(address[] _owners, address _securityToken, uint _securityValue, uint _challengePeriod) {
for (uint i=0; i<_owners.length; i++)
proxyContracts[_owners[i]] = new StateChannelProxy(_owners[i]);
owners = _owners;
securityToken = Token(_securityToken);
securityValue = _securityValue;
challengePeriod = _challengePeriod;
}
}