-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSyscoinBattleManager.sol
497 lines (441 loc) · 21.2 KB
/
SyscoinBattleManager.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
pragma solidity ^0.5.10;
import './interfaces/SyscoinClaimManagerI.sol';
import './interfaces/SyscoinSuperblocksI.sol';
import './SyscoinErrorCodes.sol';
import './SyscoinParser/SyscoinMessageLibrary.sol';
// @dev - Manages a battle session between superblock submitter and challenger
contract SyscoinBattleManager is SyscoinErrorCodes {
enum ChallengeState {
Unchallenged, // Unchallenged submission
Challenged, // Claims was challenged
QueryMerkleRootHashes, // Challenger expecting block hashes
RespondMerkleRootHashes, // Block hashes were received and verified
QueryLastBlockHeader, // Challenger is requesting last block header
PendingVerification, // All block hashes were received and verified by merkle commitment to superblock merkle root set to pending superblock verification
SuperblockVerified, // Superblock verified
SuperblockFailed // Superblock not valid
}
enum BlockInfoStatus {
Uninitialized,
Requested,
Verified
}
struct BlockInfo {
bytes32 prevBlock;
uint64 timestamp;
uint32 bits;
BlockInfoStatus status;
bytes32 blockHash;
}
struct BattleSession {
bytes32 id;
bytes32 superblockHash;
address submitter;
address challenger;
uint lastActionTimestamp; // Last action timestamp
uint lastActionClaimant; // Number last action submitter
uint lastActionChallenger; // Number last action challenger
uint actionsCounter; // Counter session actions
bytes32[] blockHashes; // Block hashes
BlockInfo blocksInfo;
ChallengeState challengeState; // Claim state
}
mapping (bytes32 => BattleSession) sessions;
uint public superblockDuration; // Superblock duration (in blocks)
uint public superblockTimeout; // Timeout action (in seconds)
// network that the stored blocks belong to
SyscoinMessageLibrary.Network private net;
// Syscoin claim manager
SyscoinClaimManagerI trustedSyscoinClaimManager;
// Superblocks contract
SyscoinSuperblocksI trustedSuperblocks;
event NewBattle(bytes32 superblockHash, bytes32 sessionId, address submitter, address challenger);
event ChallengerConvicted(bytes32 superblockHash, bytes32 sessionId, address challenger);
event SubmitterConvicted(bytes32 superblockHash, bytes32 sessionId, address submitter);
event QueryMerkleRootHashes(bytes32 superblockHash, bytes32 sessionId, address submitter);
event RespondMerkleRootHashes(bytes32 superblockHash, bytes32 sessionId, address challenger);
event QueryLastBlockHeader(bytes32 sessionId, address submitter);
event RespondLastBlockHeader(bytes32 sessionId, address challenger);
event ErrorBattle(bytes32 sessionId, uint err);
modifier onlyFrom(address sender) {
require(msg.sender == sender);
_;
}
modifier onlyClaimant(bytes32 sessionId) {
require(msg.sender == sessions[sessionId].submitter);
_;
}
modifier onlyChallenger(bytes32 sessionId) {
require(msg.sender == sessions[sessionId].challenger);
_;
}
// @dev – Configures the contract managing superblocks battles
// @param _network Network type to use for block difficulty validation
// @param _superblocks Contract that manages superblocks
// @param _superblockDuration Superblock duration (in blocks)
// @param _superblockTimeout Time to wait for challenges (in seconds)
constructor(
SyscoinMessageLibrary.Network _network,
SyscoinSuperblocksI _superblocks,
uint _superblockDuration,
uint _superblockTimeout
) public {
net = _network;
trustedSuperblocks = _superblocks;
superblockDuration = _superblockDuration;
superblockTimeout = _superblockTimeout;
}
function setSyscoinClaimManager(SyscoinClaimManagerI _syscoinClaimManager) public {
require(address(trustedSyscoinClaimManager) == address(0) && address(_syscoinClaimManager) != address(0));
trustedSyscoinClaimManager = _syscoinClaimManager;
}
// @dev - Start a battle session
function beginBattleSession(bytes32 superblockHash, address submitter, address challenger)
onlyFrom(address(trustedSyscoinClaimManager)) public returns (bytes32) {
bytes32 sessionId = keccak256(abi.encode(superblockHash, msg.sender, challenger));
BattleSession storage session = sessions[sessionId];
if(session.id != 0x0){
revert();
}
session.id = sessionId;
session.superblockHash = superblockHash;
session.submitter = submitter;
session.challenger = challenger;
session.lastActionTimestamp = block.timestamp;
session.lastActionChallenger = 0;
session.lastActionClaimant = 1; // Force challenger to start
session.actionsCounter = 1;
session.challengeState = ChallengeState.Challenged;
emit NewBattle(superblockHash, sessionId, submitter, challenger);
return sessionId;
}
// @dev - Challenger makes a query for superblock hashes
function doQueryMerkleRootHashes(BattleSession storage session) internal returns (uint) {
if (session.challengeState == ChallengeState.Challenged) {
session.challengeState = ChallengeState.QueryMerkleRootHashes;
assert(msg.sender == session.challenger);
return ERR_SUPERBLOCK_OK;
}
return ERR_SUPERBLOCK_BAD_STATUS;
}
// @dev - Challenger makes a query for superblock hashes
function queryMerkleRootHashes(bytes32 superblockHash, bytes32 sessionId) onlyChallenger(sessionId) public {
BattleSession storage session = sessions[sessionId];
uint err = doQueryMerkleRootHashes(session);
if (err != ERR_SUPERBLOCK_OK) {
emit ErrorBattle(sessionId, err);
} else {
session.actionsCounter += 1;
session.lastActionTimestamp = block.timestamp;
session.lastActionChallenger = session.actionsCounter;
emit QueryMerkleRootHashes(superblockHash, sessionId, session.submitter);
}
}
// @dev - Submitter sends hashes to verify superblock merkle root
function doVerifyMerkleRootHashes(BattleSession storage session, bytes32[] memory blockHashes) internal returns (uint) {
require(session.blockHashes.length == 0);
if (session.challengeState == ChallengeState.QueryMerkleRootHashes) {
(bytes32 merkleRoot, , ,bytes32 lastHash,, , ,,) = getSuperblockInfo(session.superblockHash);
if (lastHash != blockHashes[blockHashes.length - 1]){
return ERR_SUPERBLOCK_BAD_LASTBLOCK;
}
if(net != SyscoinMessageLibrary.Network.REGTEST && blockHashes.length != superblockDuration){
return ERR_SUPERBLOCK_BAD_BLOCKHEIGHT;
}
if (merkleRoot != SyscoinMessageLibrary.makeMerkle(blockHashes)) {
return ERR_SUPERBLOCK_INVALID_MERKLE;
}
if (blockHashes.length > 1)
session.blockHashes.push(blockHashes[blockHashes.length-2]);
session.blockHashes.push(blockHashes[blockHashes.length-1]);
session.challengeState = ChallengeState.RespondMerkleRootHashes;
return ERR_SUPERBLOCK_OK;
}
return ERR_SUPERBLOCK_BAD_STATUS;
}
// @dev - For the submitter to respond to challenger queries
function respondMerkleRootHashes(bytes32 superblockHash, bytes32 sessionId, bytes32[] memory blockHashes) onlyClaimant(sessionId) public {
BattleSession storage session = sessions[sessionId];
uint err = doVerifyMerkleRootHashes(session, blockHashes);
if (err != 0) {
emit ErrorBattle(sessionId, err);
} else {
session.actionsCounter += 1;
session.lastActionTimestamp = block.timestamp;
session.lastActionClaimant = session.actionsCounter;
emit RespondMerkleRootHashes(superblockHash, sessionId, session.challenger);
}
}
// @dev - Challenger makes a query for last block header
function doQueryLastBlockHeader(BattleSession storage session) internal returns (uint) {
if (session.challengeState == ChallengeState.RespondMerkleRootHashes) {
require(session.blocksInfo.status == BlockInfoStatus.Uninitialized);
session.challengeState = ChallengeState.QueryLastBlockHeader;
session.blocksInfo.status = BlockInfoStatus.Requested;
return ERR_SUPERBLOCK_OK;
}
return ERR_SUPERBLOCK_BAD_STATUS;
}
// @dev - For the challenger to start a query
function queryLastBlockHeader(bytes32 sessionId) onlyChallenger(sessionId) public {
BattleSession storage session = sessions[sessionId];
uint err = doQueryLastBlockHeader(session);
if (err != ERR_SUPERBLOCK_OK) {
emit ErrorBattle(sessionId, err);
} else {
session.actionsCounter += 1;
session.lastActionTimestamp = block.timestamp;
session.lastActionChallenger = session.actionsCounter;
emit QueryLastBlockHeader(sessionId, session.submitter);
}
}
// @dev - Verify Syscoin block AuxPoW
function verifyBlockAuxPoW(
BlockInfo storage blockInfo,
bytes32 blockHash,
bytes memory blockHeader
) internal returns (uint) {
uint err = SyscoinMessageLibrary.verifyBlockHeader(blockHeader, 0, uint(blockHash));
if (err != 0) {
return err;
}
blockInfo.timestamp = SyscoinMessageLibrary.getTimestamp(blockHeader);
blockInfo.bits = SyscoinMessageLibrary.getBits(blockHeader);
blockInfo.prevBlock = bytes32(SyscoinMessageLibrary.getHashPrevBlock(blockHeader));
blockInfo.blockHash = blockHash;
return ERR_SUPERBLOCK_OK;
}
// @dev - Verify block header sent by challenger
function doRespondLastBlockHeader(
BattleSession storage session,
bytes memory blockHeader
) internal returns (uint) {
if (session.challengeState == ChallengeState.QueryLastBlockHeader) {
bytes32 blockSha256Hash = bytes32(SyscoinMessageLibrary.dblShaFlipMem(blockHeader, 0, 80));
if(session.blockHashes[session.blockHashes.length-1] != blockSha256Hash){
return (ERR_SUPERBLOCK_BAD_LASTBLOCK);
}
BlockInfo storage blockInfo = session.blocksInfo;
if (blockInfo.status != BlockInfoStatus.Requested) {
return (ERR_SUPERBLOCK_BAD_SYSCOIN_STATUS);
}
// pass in blockSha256Hash here instead of proposedScryptHash because we
// don't need a proposed hash (we already calculated it here, syscoin uses
// sha256 just like bitcoin)
uint err = verifyBlockAuxPoW(blockInfo, blockSha256Hash, blockHeader);
if (err != ERR_SUPERBLOCK_OK) {
return (err);
}
session.challengeState = ChallengeState.PendingVerification;
blockInfo.status = BlockInfoStatus.Verified;
return (ERR_SUPERBLOCK_OK);
}
return (ERR_SUPERBLOCK_BAD_STATUS);
}
function respondLastBlockHeader(
bytes32 sessionId,
bytes memory blockHeader
) onlyClaimant(sessionId) public {
BattleSession storage session = sessions[sessionId];
(uint err) = doRespondLastBlockHeader(session, blockHeader);
if (err != 0) {
emit ErrorBattle(sessionId, err);
}else{
session.actionsCounter += 1;
session.lastActionTimestamp = block.timestamp;
session.lastActionClaimant = session.actionsCounter;
emit RespondLastBlockHeader(sessionId, session.challenger);
}
}
// @dev - Validate superblock information from last blocks
function validateLastBlocks(BattleSession storage session) internal view returns (uint) {
if (session.blockHashes.length <= 0) {
return ERR_SUPERBLOCK_BAD_LASTBLOCK;
}
uint lastTimestamp;
uint prevTimestamp;
bytes32 parentId;
bytes32 lastBlockHash;
(, , lastTimestamp, lastBlockHash, ,parentId,,,) = getSuperblockInfo(session.superblockHash);
bytes32 blockSha256Hash = session.blockHashes[session.blockHashes.length - 1];
BlockInfo storage blockInfo = session.blocksInfo;
if(session.blockHashes.length > 2){
bytes32 prevBlockSha256Hash = session.blockHashes[session.blockHashes.length - 2];
if(blockInfo.prevBlock != prevBlockSha256Hash){
return ERR_SUPERBLOCK_BAD_PREVBLOCK;
}
}
if(blockSha256Hash != lastBlockHash){
return ERR_SUPERBLOCK_BAD_LASTBLOCK;
}
if (blockInfo.timestamp != lastTimestamp) {
return ERR_SUPERBLOCK_BAD_TIMESTAMP;
}
if (blockInfo.status != BlockInfoStatus.Verified) {
return ERR_SUPERBLOCK_BAD_LASTBLOCK_STATUS;
}
(, ,prevTimestamp , ,,,, , ) = getSuperblockInfo(parentId);
if (prevTimestamp > lastTimestamp) {
return ERR_SUPERBLOCK_BAD_TIMESTAMP;
}
return ERR_SUPERBLOCK_OK;
}
// @dev - Validate superblock accumulated work
function validateProofOfWork(BattleSession storage session) internal view returns (uint) {
uint accWork;
bytes32 prevBlock;
uint prevWork;
uint32 prevBits;
uint superblockHeight;
bytes32 superblockHash = session.superblockHash;
(, accWork, ,,prevBits,prevBlock,,,superblockHeight) = getSuperblockInfo(superblockHash);
BlockInfo storage blockInfo = session.blocksInfo;
if(accWork <= 0){
return ERR_SUPERBLOCK_BAD_ACCUMULATED_WORK;
}
if(prevBits != blockInfo.bits){
return ERR_SUPERBLOCK_BAD_MISMATCH;
}
(, prevWork, ,, prevBits,, ,,) = getSuperblockInfo(prevBlock);
if(accWork <= prevWork){
return ERR_SUPERBLOCK_INVALID_ACCUMULATED_WORK;
}
// make sure every 7th superblock adjusts difficulty
if(net == SyscoinMessageLibrary.Network.MAINNET){
if(((superblockHeight-2) % 6) == 0){
if(prevBits == blockInfo.bits){
return ERR_SUPERBLOCK_INVALID_DIFFICULTY_ADJUSTMENT;
}
// make sure difficulty adjustment is within bounds
uint32 lowerBoundDiff = SyscoinMessageLibrary.calculateDifficulty(SyscoinMessageLibrary.getLowerBoundDifficultyTarget()-1, prevBits);
uint32 upperBoundDiff = SyscoinMessageLibrary.calculateDifficulty(SyscoinMessageLibrary.getUpperBoundDifficultyTarget()+1, prevBits);
if(blockInfo.bits < lowerBoundDiff || blockInfo.bits > upperBoundDiff){
return ERR_SUPERBLOCK_BAD_RETARGET;
}
}
// within the 7th make sure bits don't change
else if(prevBits != blockInfo.bits){
return ERR_SUPERBLOCK_BAD_BITS;
}
uint newWork = prevWork + (SyscoinMessageLibrary.getWorkFromBits(blockInfo.bits)*superblockDuration);
if (newWork != accWork) {
return ERR_SUPERBLOCK_BAD_ACCUMULATED_WORK;
}
}
return ERR_SUPERBLOCK_OK;
}
// @dev - Verify whether a superblock's data is consistent
// Only should be called when all blocks header were submitted
function doVerifySuperblock(BattleSession storage session, bytes32 sessionId) internal returns (uint) {
if (session.challengeState == ChallengeState.PendingVerification) {
uint err;
err = validateLastBlocks(session);
if (err != 0) {
emit ErrorBattle(sessionId, err);
return 2;
}
err = validateProofOfWork(session);
if (err != 0) {
emit ErrorBattle(sessionId, err);
return 2;
}
return 1;
} else if (session.challengeState == ChallengeState.SuperblockFailed) {
return 2;
}
return 0;
}
// @dev - Perform final verification once all blocks were submitted
function verifySuperblock(bytes32 sessionId) public {
BattleSession storage session = sessions[sessionId];
uint status = doVerifySuperblock(session, sessionId);
if (status == 1) {
convictChallenger(sessionId, session.challenger, session.superblockHash, false);
} else if (status == 2) {
convictSubmitter(sessionId, session.submitter, session.superblockHash);
}
}
// @dev - Trigger conviction if response is not received in time
function timeout(bytes32 sessionId) public returns (uint) {
BattleSession storage session = sessions[sessionId];
if (session.challengeState == ChallengeState.SuperblockFailed ||
(session.lastActionChallenger > session.lastActionClaimant &&
block.timestamp > session.lastActionTimestamp + superblockTimeout)) {
convictSubmitter(sessionId, session.submitter, session.superblockHash);
return ERR_SUPERBLOCK_OK;
} else if (session.lastActionClaimant > session.lastActionChallenger &&
block.timestamp > session.lastActionTimestamp + superblockTimeout) {
convictChallenger(sessionId, session.challenger, session.superblockHash, true);
return ERR_SUPERBLOCK_OK;
}
emit ErrorBattle(sessionId, ERR_SUPERBLOCK_NO_TIMEOUT);
return ERR_SUPERBLOCK_NO_TIMEOUT;
}
// @dev - To be called when a challenger is convicted
function convictChallenger(bytes32 sessionId, address challenger, bytes32 superblockHash, bool timedOut) internal {
BattleSession storage session = sessions[sessionId];
sessionDecided(sessionId, superblockHash, session.submitter, session.challenger, timedOut);
disable(sessionId);
emit ChallengerConvicted(superblockHash, sessionId, challenger);
}
// @dev - To be called when a submitter is convicted
function convictSubmitter(bytes32 sessionId, address submitter, bytes32 superblockHash) internal {
BattleSession storage session = sessions[sessionId];
sessionDecided(sessionId, superblockHash, session.challenger, session.submitter, false);
disable(sessionId);
emit SubmitterConvicted(superblockHash, sessionId, submitter);
}
// @dev - Disable session
// It should be called only when either the submitter or the challenger were convicted.
function disable(bytes32 sessionId) internal {
delete sessions[sessionId];
}
// @dev - Check if a session's challenger did not respond before timeout
function getChallengerHitTimeout(bytes32 sessionId) public view returns (bool) {
BattleSession storage session = sessions[sessionId];
return (session.lastActionClaimant > session.lastActionChallenger &&
block.timestamp > session.lastActionTimestamp + superblockTimeout);
}
// @dev - Check if a session's submitter did not respond before timeout
function getSubmitterHitTimeout(bytes32 sessionId) public view returns (bool) {
BattleSession storage session = sessions[sessionId];
return (session.lastActionChallenger > session.lastActionClaimant &&
block.timestamp > session.lastActionTimestamp + superblockTimeout);
}
function getSuperblockBySession(bytes32 sessionId) public view returns (bytes32) {
return sessions[sessionId].superblockHash;
}
function getSessionStatus(bytes32 sessionId) public view returns (BlockInfoStatus) {
BattleSession storage session = sessions[sessionId];
return session.blocksInfo.status;
}
function getSessionChallengeState(bytes32 sessionId) public view returns (ChallengeState) {
return sessions[sessionId].challengeState;
}
// @dev - To be called when a battle sessions was decided
function sessionDecided(bytes32 sessionId, bytes32 superblockHash, address winner, address loser, bool timedOut) internal {
trustedSyscoinClaimManager.sessionDecided(sessionId, superblockHash, winner, loser, timedOut);
}
// @dev - Retrieve superblock information
function getSuperblockInfo(bytes32 superblockHash) internal view returns (
bytes32 _blocksMerkleRoot,
uint _accumulatedWork,
uint _timestamp,
bytes32 _lastHash,
uint32 _lastBits,
bytes32 _parentId,
address _submitter,
SyscoinSuperblocksI.Status _status,
uint32 _height
) {
return trustedSuperblocks.getSuperblock(superblockHash);
}
// @dev - Verify whether a user has a certain amount of deposits or more
function hasDeposit(address who, uint amount) internal view returns (bool) {
return trustedSyscoinClaimManager.getDeposit(who) >= amount;
}
// @dev – locks up part of a user's deposit into a claim.
function bondDeposit(bytes32 superblockHash, address account, uint amount) internal returns (uint) {
return trustedSyscoinClaimManager.bondDeposit(superblockHash, account, amount);
}
}