forked from szu-security-group/Trust-IoT-Data-Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
non-repudiation.sol
356 lines (279 loc) · 10.1 KB
/
non-repudiation.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
*Submitted for verification at Etherscan.io on 2021-01-19
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract Launchpad{
address private _cloud;
address private _client;
address private _arbitrator;
uint256 private _cloudPenalty;
uint256 private _clientServiceFee;
int private _serviceStatus;
bytes32 private _verifyResult;
bytes32 private _hashOfS1;
uint256 private _confirmStartTime;
uint256 private _duringTime;
uint256 private _s2PublishStartTime;
string private _serviceName;
string private _s2;
uint256 private _claimStartTime;
uint private _randomBlock;
/**
* @dev Emitted Received money sources
*
*/
event Received(string from, uint256 value);
/**
* @dev Emitted that client requests the service
*
*/
event RequestService(string ret);
/**
* @dev Emitted that cloud publishes the verify result, hashOfS1, and transfer penatly
*
*/
event CloudDoRound1(string ret);
/**
* @dev Emitted that client confirms the hash of s1 and transfer the service fees
*
*/
event ClientConfirm(string ret);
/**
* @dev Emitted that client terminates the service if the hash(s1) is wrong.
* Or cloud terminates the service if client does not confirms hash(s1) in time.
*/
event TerminatedService(string ret);
/**
* @dev Emitted that cloud publishes s2
*
*/
event CloudDoRound2(string ret);
/**
* @dev Emitted that cloud or the client launches the on-chain arbitration
*
*/
event Arbitrate(string ret);
constructor(
address cloud,
address client,
uint256 cloudPenalty,
uint256 clientServiceFee,
uint256 duringTime
)
public
{
_cloud = cloud;
_client = client;
_arbitrator = msg.sender;
_cloudPenalty = cloudPenalty;
_clientServiceFee = clientServiceFee;
_confirmStartTime = 0;
_duringTime = duringTime;
_s2PublishStartTime = 0;
_claimStartTime = 0;
_serviceStatus = 0;
}
/**
* @dev Function to get details of project
*/
function getBasicInfos() public view returns (
address cloud,
address client,
address arbitrator,
uint256 cloudPenalty,
uint256 clientServiceFee
) {
cloud = _cloud;
client = _client;
arbitrator = _arbitrator;
cloudPenalty = _cloudPenalty;
clientServiceFee = _clientServiceFee;
}
function getDetailsInfos() public view returns (
string memory serviceName,
bytes32 verifyResult,
bytes32 hashOfS1,
uint256 confirmStartTime,
uint256 s2PublishStartTime,
string memory s2,
uint256 claimStartTime,
uint256 duringTime,
int serviceStatus,
uint256 randomBlock
) {
serviceName = _serviceName;
verifyResult = _verifyResult;
hashOfS1 = _hashOfS1;
confirmStartTime = _confirmStartTime;
s2PublishStartTime = _s2PublishStartTime;
s2 = _s2;
claimStartTime = _claimStartTime;
duringTime = _duringTime;
serviceStatus = _serviceStatus;
randomBlock = _randomBlock;
}
/**
* @dev Client requests the service
*
*/
function requestService(string memory serviceName) public {
require(msg.sender == _client, "Should be the correct client address");
require(_serviceStatus == 0, "Service already started or finished");
_serviceName = serviceName;
_serviceStatus = 1;
emit RequestService('Client Requests Service');
}
/**
* cloud publishes the verify result, hashOfS1, and transfer penatly
*
*/
function cloudDoRound1(bytes32 verifyResult, bytes32 hashOfS1) payable public {
require(msg.sender == _cloud, "Should be the correct cloud address");
require(_serviceStatus == 1, "Error service sequence");
require(msg.value == _cloudPenalty, 'Error penatly amount');
address(this).transfer(msg.value);
_verifyResult = verifyResult;
_hashOfS1 = hashOfS1;
_serviceStatus = 2;
_confirmStartTime = now;
emit CloudDoRound1("Cloud do Round1");
}
/**
* client confirms the hash of s1 and transfer the service fees
*
*/
function clientConfirm() payable public {
require(msg.sender == _client, "Should be the correct client address");
require(_serviceStatus == 2, "Error service sequence");
require(now <= _confirmStartTime + _duringTime, 'The confirm time is over');
require(msg.value == _clientServiceFee, 'Error service fees');
address(this).transfer(msg.value);
_serviceStatus = 3;
_s2PublishStartTime = now;
emit ClientConfirm('Client confirms');
}
/**
* Emitted that cloud terminates the service if client does not confirms hash(s1) in time.
* Or client terminates the service if cloud does not publishes s2 in time.
*/
function terminatedService() public {
require(msg.sender == _cloud || msg.sender == _client, "Should be the correct address");
if (msg.sender == _cloud) {
require(_serviceStatus == 2, "Error service sequence");
require(now > _confirmStartTime + _duringTime, 'The confirm time is not over');
_serviceStatus = -1;
// return the money of Cloud
payable(_cloud).transfer(_cloudPenalty);
emit TerminatedService("Cloud Terminate Service");
}
if (msg.sender == _client) {
require(_serviceStatus == 3, "Error service sequence");
require(now > _s2PublishStartTime + _duringTime, 'The publish time of s2 is not over');
_serviceStatus = -1;
// return the money of client and cloud
payable(_client).transfer(_clientServiceFee);
payable(_cloud).transfer(_cloudPenalty);
emit TerminatedService("Client Terminate Service");
}
}
/**
* @dev cloud publishes s2
*
*/
function cloudDoRound2(string memory s2, uint256 randomBlock) public {
require(msg.sender == _cloud, "Should be the correct cloud address");
require(_serviceStatus == 3, "Error service sequence");
if (now > _s2PublishStartTime + _duringTime) {
_serviceStatus = -1;
// return the money of client and cloud
payable(_cloud).transfer(_cloudPenalty);
payable(_client).transfer(_clientServiceFee);
emit CloudDoRound2("The publish time of s2 is over");
}
else {
_s2 = s2;
_randomBlock = randomBlock;
_serviceStatus = 4;
_claimStartTime = now;
emit CloudDoRound2('Cloud do Round2');
}
}
function arbitrate() public { // client
require(msg.sender == _client, 'Should be the correct client address');
require(_serviceStatus == 4, "Error service sequence");
require(now <= _claimStartTime + _duringTime, 'Arbitration time is over');
bytes32 hash_s2 = sha256(bytes(_s2));
if (_hashOfS1 ^ hash_s2 == _verifyResult) {
emit Arbitrate('On-chain Result: Malicious Client');
}
else {
_serviceStatus = -1;
payable(_client).transfer(_cloudPenalty+_clientServiceFee);
emit Arbitrate('On-chain Result: Malicious SP');
}
}
function lauchoffChainArbitrate() public { // client
require (msg.sender == _client, 'Should be the correct client address');
require(_serviceStatus == 4, "Error service sequence");
require(now <= _claimStartTime + _duringTime, 'Arbitration time is over');
_serviceStatus = 5;
emit Arbitrate('Client Launches Off-chain Arbitration');
}
/**
* @dev cloud claim service fees and penatly after claim time started
*
*/
function claim() public {
require(msg.sender == _cloud, 'Should be the correct cloud address');
require(_serviceStatus == 4, "Error service sequence");
require(now > _claimStartTime + _duringTime, 'Claim time is not start');
// return the money
_serviceStatus = -1;
payable(_cloud).transfer(_cloudPenalty+_clientServiceFee);
}
/**
* @dev off-chain arbitrate result: 0: Malicious client, 1: Malicious client
*
*/
function offChainArbitrate(uint256 result) public {
require(msg.sender == _arbitrator);
require(_serviceStatus == 5, "Error service sequence");
if (result == 0) {
// return all the money to cloud
_serviceStatus = -1;
payable(_cloud).transfer(_cloudPenalty+_clientServiceFee);
emit Arbitrate('Off-chain Result: Malicious Client');
}
else {
// return all the money to client
_serviceStatus = -1;
payable(_client).transfer(_cloudPenalty+_clientServiceFee);
emit Arbitrate('Off-chain Result: Malicious Cloud');
}
}
function getHashOfS1() view public returns(bytes32) {
return _hashOfS1;
}
function getS2() view public returns(string memory) {
return _s2;
}
function getRandomBlock() view public returns(uint256) {
return _randomBlock;
}
function getVerifyResult() view public returns(bytes32) {
return _verifyResult;
}
function getBalance() view public returns(uint) {
return address(this).balance;
}
function restart() public {
require(_serviceStatus == -1 && msg.sender == _arbitrator);
_serviceStatus = 0;
}
/**
* @dev Receive cloud's penatly or client's service fees.
*/
receive() external payable {
}
}