-
Notifications
You must be signed in to change notification settings - Fork 19
/
SyscoinSuperblocks.sol
674 lines (605 loc) · 28.1 KB
/
SyscoinSuperblocks.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
pragma solidity ^0.5.10;
import './interfaces/SyscoinSuperblocksI.sol';
import "./SyscoinParser/SyscoinMessageLibrary.sol";
import "./SyscoinErrorCodes.sol";
import "./SyscoinTransactionProcessor.sol";
// @dev - Manages superblocks
//
// Management of superblocks and status transitions
contract SyscoinSuperblocks is SyscoinSuperblocksI, SyscoinErrorCodes {
struct SuperblockInfo {
bytes32 blocksMerkleRoot;
uint accumulatedWork;
uint timestamp;
bytes32 lastHash;
bytes32 parentId;
address submitter;
bytes32 ancestors;
uint32 lastBits;
uint32 index;
uint32 height;
Status status;
}
// Mapping superblock id => superblock data
mapping (bytes32 => SuperblockInfo) superblocks;
// Index to superblock id
mapping (uint32 => bytes32) private indexSuperblock;
struct ProcessTransactionParams {
uint value;
address destinationAddress;
uint32 assetGUID;
address superblockSubmitterAddress;
SyscoinTransactionProcessor untrustedTargetContract;
}
mapping (uint => ProcessTransactionParams) private txParams;
uint32 indexNextSuperblock;
bytes32 public bestSuperblock;
uint public bestSuperblockAccumulatedWork;
event NewSuperblock(bytes32 superblockHash, address who);
event ApprovedSuperblock(bytes32 superblockHash, address who);
event ChallengeSuperblock(bytes32 superblockHash, address who);
event SemiApprovedSuperblock(bytes32 superblockHash, address who);
event InvalidSuperblock(bytes32 superblockHash, address who);
event ErrorSuperblock(bytes32 superblockHash, uint err);
event VerifyTransaction(bytes32 txHash, uint returnCode);
event RelayTransaction(bytes32 txHash, uint returnCode);
// SyscoinClaimManager
address public trustedClaimManager;
modifier onlyClaimManager() {
require(msg.sender == trustedClaimManager);
_;
}
// @dev – the constructor
constructor() public {}
// @dev - sets ClaimManager instance associated with managing superblocks.
// Once trustedClaimManager has been set, it cannot be changed.
// @param _claimManager - address of the ClaimManager contract to be associated with
function setClaimManager(address _claimManager) public {
require(address(trustedClaimManager) == address(0) && _claimManager != address(0));
trustedClaimManager = _claimManager;
}
// @dev - Initializes superblocks contract
//
// Initializes the superblock contract. It can only be called once.
//
// @param _blocksMerkleRoot Root of the merkle tree of blocks contained in a superblock
// @param _accumulatedWork Accumulated proof of work of the last block in the superblock
// @param _timestamp Timestamp of the last block in the superblock
// @param _lastHash Hash of the last block in the superblock
// @param _lastBits Difficulty bits of the last block in the superblock bits used to verify accumulatedWork through difficulty calculation
// @param _parentId Id of the parent superblock
// @param _blockHeight Block height of last block in superblock
// @return Error code and superblockHash
function initialize(
bytes32 _blocksMerkleRoot,
uint _accumulatedWork,
uint _timestamp,
bytes32 _lastHash,
uint32 _lastBits,
bytes32 _parentId
) public returns (uint, bytes32) {
require(bestSuperblock == 0);
require(_parentId == 0);
bytes32 superblockHash = calcSuperblockHash(_blocksMerkleRoot, _accumulatedWork, _timestamp, _lastHash, _lastBits, _parentId);
SuperblockInfo storage superblock = superblocks[superblockHash];
require(superblock.status == Status.Unitialized);
indexSuperblock[indexNextSuperblock] = superblockHash;
superblock.blocksMerkleRoot = _blocksMerkleRoot;
superblock.accumulatedWork = _accumulatedWork;
superblock.timestamp = _timestamp;
superblock.lastHash = _lastHash;
superblock.parentId = _parentId;
superblock.submitter = msg.sender;
superblock.index = indexNextSuperblock;
superblock.height = 1;
superblock.lastBits = _lastBits;
superblock.status = Status.Approved;
superblock.ancestors = 0x0;
indexNextSuperblock++;
emit NewSuperblock(superblockHash, msg.sender);
bestSuperblock = superblockHash;
bestSuperblockAccumulatedWork = _accumulatedWork;
emit ApprovedSuperblock(superblockHash, msg.sender);
return (ERR_SUPERBLOCK_OK, superblockHash);
}
// @dev - Proposes a new superblock
//
// To be accepted, a new superblock needs to have its parent
// either approved or semi-approved.
//
// @param _blocksMerkleRoot Root of the merkle tree of blocks contained in a superblock
// @param _accumulatedWork Accumulated proof of work of the last block in the superblock
// @param _timestamp Timestamp of the last block in the superblock
// @param _lastHash Hash of the last block in the superblock
// @param _lastBits Difficulty bits of the last block in the superblock bits used to verify accumulatedWork through difficulty calculation
// @param _parentId Id of the parent superblock
// @return Error code and superblockHash
function propose(
bytes32 _blocksMerkleRoot,
uint _accumulatedWork,
uint _timestamp,
bytes32 _lastHash,
uint32 _lastBits,
bytes32 _parentId,
address submitter
) public returns (uint, bytes32) {
if (msg.sender != trustedClaimManager) {
emit ErrorSuperblock(0, ERR_SUPERBLOCK_NOT_CLAIMMANAGER);
return (ERR_SUPERBLOCK_NOT_CLAIMMANAGER, 0);
}
SuperblockInfo storage parent = superblocks[_parentId];
if (parent.status != Status.SemiApproved && parent.status != Status.Approved) {
emit ErrorSuperblock(bytes32(0), ERR_SUPERBLOCK_BAD_PARENT);
return (ERR_SUPERBLOCK_BAD_PARENT, 0);
}
bytes32 superblockHash = calcSuperblockHash(_blocksMerkleRoot, _accumulatedWork, _timestamp, _lastHash, _lastBits, _parentId);
SuperblockInfo storage superblock = superblocks[superblockHash];
if (superblock.status == Status.Unitialized) {
indexSuperblock[indexNextSuperblock] = superblockHash;
superblock.blocksMerkleRoot = _blocksMerkleRoot;
superblock.accumulatedWork = _accumulatedWork;
superblock.timestamp = _timestamp;
superblock.lastHash = _lastHash;
superblock.parentId = _parentId;
superblock.index = indexNextSuperblock;
superblock.height = parent.height + 1;
superblock.lastBits = _lastBits;
superblock.ancestors = updateAncestors(parent.ancestors, parent.index, parent.height);
indexNextSuperblock++;
}
superblock.status = Status.New;
superblock.submitter = submitter;
emit NewSuperblock(superblockHash, submitter);
return (ERR_SUPERBLOCK_OK, superblockHash);
}
// @dev - Confirm a proposed superblock
//
// An unchallenged superblock can be confirmed after a timeout.
// A challenged superblock is confirmed if it has enough descendants
// in the main chain.
//
// @param _superblockHash Id of the superblock to confirm
// @param _validator Address requesting superblock confirmation
// @return Error code and superblockHash
function confirm(bytes32 _superblockHash, address _validator) public returns (uint) {
if (msg.sender != trustedClaimManager) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_NOT_CLAIMMANAGER);
return ERR_SUPERBLOCK_NOT_CLAIMMANAGER;
}
SuperblockInfo storage superblock = superblocks[_superblockHash];
if (superblock.status != Status.New && superblock.status != Status.SemiApproved) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_BAD_STATUS);
return ERR_SUPERBLOCK_BAD_STATUS;
}
SuperblockInfo storage parent = superblocks[superblock.parentId];
if (parent.status != Status.Approved) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_BAD_PARENT);
return ERR_SUPERBLOCK_BAD_PARENT;
}
superblock.status = Status.Approved;
if (superblock.accumulatedWork > bestSuperblockAccumulatedWork) {
bestSuperblock = _superblockHash;
bestSuperblockAccumulatedWork = superblock.accumulatedWork;
}
emit ApprovedSuperblock(_superblockHash, _validator);
return ERR_SUPERBLOCK_OK;
}
// @dev - Challenge a proposed superblock
//
// A new superblock can be challenged to start a battle
// to verify the correctness of the data submitted.
//
// @param _superblockHash Id of the superblock to challenge
// @param _challenger Address requesting a challenge
// @return Error code and superblockHash
function challenge(bytes32 _superblockHash, address _challenger) public returns (uint) {
if (msg.sender != trustedClaimManager) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_NOT_CLAIMMANAGER);
return ERR_SUPERBLOCK_NOT_CLAIMMANAGER;
}
SuperblockInfo storage superblock = superblocks[_superblockHash];
if (superblock.status != Status.New && superblock.status != Status.InBattle) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_BAD_STATUS);
return ERR_SUPERBLOCK_BAD_STATUS;
}
if(superblock.submitter == _challenger){
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_OWN_CHALLENGE);
return ERR_SUPERBLOCK_OWN_CHALLENGE;
}
superblock.status = Status.InBattle;
emit ChallengeSuperblock(_superblockHash, _challenger);
return ERR_SUPERBLOCK_OK;
}
// @dev - Semi-approve a challenged superblock
//
// A challenged superblock can be marked as semi-approved
// if it satisfies all the queries or when all challengers have
// stopped participating.
//
// @param _superblockHash Id of the superblock to semi-approve
// @param _validator Address requesting semi approval
// @return Error code and superblockHash
function semiApprove(bytes32 _superblockHash, address _validator) public returns (uint, bytes32) {
if (msg.sender != trustedClaimManager) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_NOT_CLAIMMANAGER);
return (ERR_SUPERBLOCK_NOT_CLAIMMANAGER, 0);
}
SuperblockInfo storage superblock = superblocks[_superblockHash];
if (superblock.status != Status.InBattle && superblock.status != Status.New) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_BAD_STATUS);
return (ERR_SUPERBLOCK_BAD_STATUS, 0);
}
superblock.status = Status.SemiApproved;
emit SemiApprovedSuperblock(_superblockHash, _validator);
return (ERR_SUPERBLOCK_OK, _superblockHash);
}
// @dev - Invalidates a superblock
//
// A superblock with incorrect data can be invalidated immediately.
// Superblocks that are not in the main chain can be invalidated
// if not enough superblocks follow them, i.e. they don't have
// enough descendants.
//
// @param _superblockHash Id of the superblock to invalidate
// @param _validator Address requesting superblock invalidation
// @return Error code and superblockHash
function invalidate(bytes32 _superblockHash, address _validator) public returns (uint, bytes32) {
if (msg.sender != trustedClaimManager) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_NOT_CLAIMMANAGER);
return (ERR_SUPERBLOCK_NOT_CLAIMMANAGER, 0);
}
SuperblockInfo storage superblock = superblocks[_superblockHash];
if (superblock.status != Status.InBattle && superblock.status != Status.SemiApproved) {
emit ErrorSuperblock(_superblockHash, ERR_SUPERBLOCK_BAD_STATUS);
return (ERR_SUPERBLOCK_BAD_STATUS, 0);
}
superblock.status = Status.Invalid;
emit InvalidSuperblock(_superblockHash, _validator);
return (ERR_SUPERBLOCK_OK, _superblockHash);
}
// @dev - relays transaction `_txBytes` to `_untrustedTargetContract`'s processTransaction() method.
// Also logs the value of processTransaction.
// Note: callers cannot be 100% certain when an ERR_RELAY_VERIFY occurs because
// it may also have been returned by processTransaction(). Callers should be
// aware of the contract that they are relaying transactions to and
// understand what that contract's processTransaction method returns.
//
// @param _txBytes - transaction bytes
// @param _txIndex - transaction's index within the block
// @param _txSiblings - transaction's Merkle siblings
// @param _syscoinBlockHeader - block header containing transaction
// @param _syscoinBlockIndex - block's index withing superblock
// @param _syscoinBlockSiblings - block's merkle siblings
// @param _superblockHash - superblock containing block header
// @param _untrustedTargetContract - the contract that is going to process the transaction
function relayTx(
bytes memory _txBytes,
uint _txIndex,
uint[] memory _txSiblings,
bytes memory _syscoinBlockHeader,
uint _syscoinBlockIndex,
uint[] memory _syscoinBlockSiblings,
bytes32 _superblockHash,
SyscoinTransactionProcessor _untrustedTargetContract
) public returns (uint) {
// Check if Syscoin block belongs to given superblock
if (bytes32(SyscoinMessageLibrary.computeMerkle(SyscoinMessageLibrary.dblShaFlip(_syscoinBlockHeader), _syscoinBlockIndex, _syscoinBlockSiblings))
!= getSuperblockMerkleRoot(_superblockHash)) {
// Syscoin block is not in superblock
emit RelayTransaction(bytes32(0), ERR_SUPERBLOCK);
return ERR_SUPERBLOCK;
}
uint txHash = verifyTx(_txBytes, _txIndex, _txSiblings, _syscoinBlockHeader, _superblockHash);
if (txHash != 0) {
uint ret = parseTxHelper(_txBytes, txHash, _untrustedTargetContract);
if(ret != 0){
emit RelayTransaction(bytes32(0), ret);
return ret;
}
ProcessTransactionParams storage params = txParams[txHash];
params.superblockSubmitterAddress = superblocks[_superblockHash].submitter;
txParams[txHash] = params;
return verifyTxHelper(txHash);
}
emit RelayTransaction(bytes32(0), ERR_RELAY_VERIFY);
return(ERR_RELAY_VERIFY);
}
function parseTxHelper(bytes memory _txBytes, uint txHash, SyscoinTransactionProcessor _untrustedTargetContract) private returns (uint) {
uint value;
address destinationAddress;
uint32 _assetGUID;
uint ret;
(ret, value, destinationAddress, _assetGUID) = SyscoinMessageLibrary.parseTransaction(_txBytes);
if(ret != 0){
return ret;
}
ProcessTransactionParams memory params;
params.value = value;
params.destinationAddress = destinationAddress;
params.assetGUID = _assetGUID;
params.untrustedTargetContract = _untrustedTargetContract;
txParams[txHash] = params;
return 0;
}
function verifyTxHelper(uint txHash) private returns (uint) {
ProcessTransactionParams storage params = txParams[txHash];
uint returnCode = params.untrustedTargetContract.processTransaction(txHash, params.value, params.destinationAddress, params.assetGUID, params.superblockSubmitterAddress);
emit RelayTransaction(bytes32(txHash), returnCode);
return (returnCode);
}
// @dev - Checks whether the transaction given by `_txBytes` is in the block identified by `_txBlockHeaderBytes`.
// First it guards against a Merkle tree collision attack by raising an error if the transaction is exactly 64 bytes long,
// then it calls helperVerifyHash to do the actual check.
//
// @param _txBytes - transaction bytes
// @param _txIndex - transaction's index within the block
// @param _siblings - transaction's Merkle siblings
// @param _txBlockHeaderBytes - block header containing transaction
// @param _txsuperblockHash - superblock containing block header
// @return - SHA-256 hash of _txBytes if the transaction is in the block, 0 otherwise
// TODO: this can probably be made private
function verifyTx(
bytes memory _txBytes,
uint _txIndex,
uint[] memory _siblings,
bytes memory _txBlockHeaderBytes,
bytes32 _txsuperblockHash
) public returns (uint) {
uint txHash = SyscoinMessageLibrary.dblShaFlip(_txBytes);
if (_txBytes.length == 64) { // todo: is check 32 also needed?
emit VerifyTransaction(bytes32(txHash), ERR_TX_64BYTE);
return 0;
}
if (helperVerifyHash(txHash, _txIndex, _siblings, _txBlockHeaderBytes, _txsuperblockHash) == 1) {
return txHash;
} else {
// log is done via helperVerifyHash
return 0;
}
}
// @dev - Checks whether the transaction identified by `_txHash` is in the block identified by `_blockHeaderBytes`
// and whether the block is in the Syscoin main chain. Transaction check is done via Merkle proof.
// Note: no verification is performed to prevent txHash from just being an
// internal hash in the Merkle tree. Thus this helper method should NOT be used
// directly and is intended to be private.
//
// @param _txHash - transaction hash
// @param _txIndex - transaction's index within the block
// @param _siblings - transaction's Merkle siblings
// @param _blockHeaderBytes - block header containing transaction
// @param _txsuperblockHash - superblock containing block header
// @return - 1 if the transaction is in the block and the block is in the main chain,
// 20020 (ERR_CONFIRMATIONS) if the block is not in the main chain,
// 20050 (ERR_MERKLE_ROOT) if the block is in the main chain but the Merkle proof fails.
function helperVerifyHash(
uint256 _txHash,
uint _txIndex,
uint[] memory _siblings,
bytes memory _blockHeaderBytes,
bytes32 _txsuperblockHash
) private returns (uint) {
//TODO: Verify superblock is in superblock's main chain
if (!isApproved(_txsuperblockHash) || !inMainChain(_txsuperblockHash)) {
emit VerifyTransaction(bytes32(_txHash), ERR_CHAIN);
return (ERR_CHAIN);
}
// Verify tx Merkle root
uint merkle = SyscoinMessageLibrary.getHeaderMerkleRoot(_blockHeaderBytes);
if (SyscoinMessageLibrary.computeMerkle(_txHash, _txIndex, _siblings) != merkle) {
emit VerifyTransaction(bytes32(_txHash), ERR_MERKLE_ROOT);
return (ERR_MERKLE_ROOT);
}
emit VerifyTransaction(bytes32(_txHash), 1);
return (1);
}
// @dev - Calculate superblock hash from superblock data
//
// @param _blocksMerkleRoot Root of the merkle tree of blocks contained in a superblock
// @param _accumulatedWork Accumulated proof of work of the last block in the superblock
// @param _timestamp Timestamp of the last block in the superblock
// @param _lastHash Hash of the last block in the superblock
// @param _lastBits Difficulty bits of the last block in the superblock bits used to verify accumulatedWork through difficulty calculation
// @param _parentId Id of the parent superblock
// @return Superblock id
function calcSuperblockHash(
bytes32 _blocksMerkleRoot,
uint _accumulatedWork,
uint _timestamp,
bytes32 _lastHash,
uint32 _lastBits,
bytes32 _parentId
) public pure returns (bytes32) {
return keccak256(abi.encodePacked(
_blocksMerkleRoot,
_accumulatedWork,
_timestamp,
_lastHash,
_lastBits,
_parentId
));
}
// @dev - Returns the confirmed superblock with the most accumulated work
//
// @return Best superblock hash
function getBestSuperblock() public view returns (bytes32) {
return bestSuperblock;
}
function getBestSuperblockAccumulatedWork() public view returns (uint) {
return bestSuperblockAccumulatedWork;
}
// @dev - Returns the superblock data for the supplied superblock hash
//
function getSuperblock(bytes32 superblockHash) public view returns (
bytes32 _blocksMerkleRoot,
uint _accumulatedWork,
uint _timestamp,
bytes32 _lastHash,
uint32 _lastBits,
bytes32 _parentId,
address _submitter,
Status _status,
uint32 _height
) {
SuperblockInfo storage superblock = superblocks[superblockHash];
return (
superblock.blocksMerkleRoot,
superblock.accumulatedWork,
superblock.timestamp,
superblock.lastHash,
superblock.lastBits,
superblock.parentId,
superblock.submitter,
superblock.status,
superblock.height
);
}
// @dev - Returns superblock height
function getSuperblockHeight(bytes32 superblockHash) public view returns (uint32) {
return superblocks[superblockHash].height;
}
// @dev - Returns superblock internal index
function getSuperblockIndex(bytes32 superblockHash) public view returns (uint32) {
return superblocks[superblockHash].index;
}
// @dev - Return superblock ancestors' indexes
function getSuperblockAncestors(bytes32 superblockHash) public view returns (bytes32) {
return superblocks[superblockHash].ancestors;
}
// @dev - Return superblock blocks' Merkle root
function getSuperblockMerkleRoot(bytes32 _superblockHash) public view returns (bytes32) {
return superblocks[_superblockHash].blocksMerkleRoot;
}
// @dev - Return superblock timestamp
function getSuperblockTimestamp(bytes32 _superblockHash) public view returns (uint) {
return superblocks[_superblockHash].timestamp;
}
// @dev - Return superblock last block hash
function getSuperblockLastHash(bytes32 _superblockHash) public view returns (bytes32) {
return superblocks[_superblockHash].lastHash;
}
// @dev - Return superblock parent
function getSuperblockParentId(bytes32 _superblockHash) public view returns (bytes32) {
return superblocks[_superblockHash].parentId;
}
// @dev - Return superblock accumulated work
function getSuperblockAccumulatedWork(bytes32 _superblockHash) public view returns (uint) {
return superblocks[_superblockHash].accumulatedWork;
}
// @dev - Return superblock status
function getSuperblockStatus(bytes32 _superblockHash) public view returns (Status) {
return superblocks[_superblockHash].status;
}
// @dev - Return indexNextSuperblock
function getIndexNextSuperblock() public view returns (uint32) {
return indexNextSuperblock;
}
// @dev - Calculate Merkle root from Syscoin block hashes
function makeMerkle(bytes32[] memory hashes) public pure returns (bytes32) {
return SyscoinMessageLibrary.makeMerkle(hashes);
}
function isApproved(bytes32 _superblockHash) public view returns (bool) {
return (getSuperblockStatus(_superblockHash) == Status.Approved);
}
function isSemiApproved(bytes32 _superblockHash) public view returns (bool) {
return (getSuperblockStatus(_superblockHash) == Status.SemiApproved);
}
function getChainHeight() public view returns (uint) {
return superblocks[bestSuperblock].height;
}
// @dev - write `_fourBytes` into `_word` starting from `_position`
// This is useful for writing 32bit ints inside one 32 byte word
//
// @param _word - information to be partially overwritten
// @param _position - position to start writing from
// @param _eightBytes - information to be written
function writeUint32(bytes32 _word, uint _position, uint32 _fourBytes) private pure returns (bytes32) {
bytes32 result;
assembly {
let pointer := mload(0x40)
mstore(pointer, _word)
mstore8(add(pointer, _position), byte(28, _fourBytes))
mstore8(add(pointer, add(_position,1)), byte(29, _fourBytes))
mstore8(add(pointer, add(_position,2)), byte(30, _fourBytes))
mstore8(add(pointer, add(_position,3)), byte(31, _fourBytes))
result := mload(pointer)
}
return result;
}
uint constant ANCESTOR_STEP = 5;
uint constant NUM_ANCESTOR_DEPTHS = 8;
// @dev - Update ancestor to the new height
function updateAncestors(bytes32 ancestors, uint32 index, uint height) internal pure returns (bytes32) {
uint step = ANCESTOR_STEP;
ancestors = writeUint32(ancestors, 0, index);
uint i = 1;
while (i<NUM_ANCESTOR_DEPTHS && (height % step == 1)) {
ancestors = writeUint32(ancestors, 4*i, index);
step *= ANCESTOR_STEP;
++i;
}
return ancestors;
}
// @dev - Returns a list of superblock hashes (9 hashes maximum) that helps an agent find out what
// superblocks are missing.
// The first position contains bestSuperblock, then
// bestSuperblock - 1,
// (bestSuperblock-1) - ((bestSuperblock-1) % 5), then
// (bestSuperblock-1) - ((bestSuperblock-1) % 25), ... until
// (bestSuperblock-1) - ((bestSuperblock-1) % 78125)
//
// @return - list of up to 9 ancestor supeerblock id
function getSuperblockLocator() public view returns (bytes32[9] memory) {
bytes32[9] memory locator;
locator[0] = bestSuperblock;
bytes32 ancestors = getSuperblockAncestors(bestSuperblock);
uint i = NUM_ANCESTOR_DEPTHS;
while (i > 0) {
locator[i] = indexSuperblock[uint32(uint256(ancestors) & uint256(0xFFFFFFFF))];
ancestors >>= 32;
--i;
}
return locator;
}
// @dev - Return ancestor at given index
function getSuperblockAncestor(bytes32 superblockHash, uint index) internal view returns (bytes32) {
bytes32 ancestors = superblocks[superblockHash].ancestors;
uint32 ancestorsIndex =
uint32(uint8(ancestors[4*index + 0])) * 0x1000000 +
uint32(uint8(ancestors[4*index + 1])) * 0x10000 +
uint32(uint8(ancestors[4*index + 2])) * 0x100 +
uint32(uint8(ancestors[4*index + 3])) * 0x1;
return indexSuperblock[ancestorsIndex];
}
// dev - returns depth associated with an ancestor index; applies to any superblock
//
// @param _index - index of ancestor to be looked up; an integer between 0 and 7
// @return - depth corresponding to said index, i.e. 5**index
function getAncDepth(uint _index) private pure returns (uint) {
return ANCESTOR_STEP**(uint(_index));
}
// @dev - return superblock hash at a given height in superblock main chain
//
// @param _height - superblock height
// @return - hash corresponding to block of height _height
function getSuperblockAt(uint _height) public view returns (bytes32) {
bytes32 superblockHash = bestSuperblock;
uint index = NUM_ANCESTOR_DEPTHS - 1;
while (getSuperblockHeight(superblockHash) > _height) {
while (getSuperblockHeight(superblockHash) - _height < getAncDepth(index) && index > 0) {
index -= 1;
}
superblockHash = getSuperblockAncestor(superblockHash, index);
}
return superblockHash;
}
// @dev - Checks if a superblock is in superblock main chain
//
// @param _blockHash - hash of the block being searched for in the main chain
// @return - true if the block identified by _blockHash is in the main chain,
// false otherwise
function inMainChain(bytes32 _superblockHash) internal view returns (bool) {
uint height = getSuperblockHeight(_superblockHash);
if (height == 0) return false;
return (getSuperblockAt(height) == _superblockHash);
}
}