Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int findInvalidInterimBlockIndex(Keccak256Hash sessionId) throws Exception{
- List<Sha256Hash> hashesFromContract = ethWrapper.getBlockHashesBySession(sessionId);
- Keccak256Hash superblockId = ethWrapper.getSuperblockIdBySession(sessionId);
- BigInteger height = ethWrapper.getSuperblockHeight(superblockId);
- // find local superblock based on height of superblock being challenged
- Superblock superblock = superblockChain.getSuperblockByHeight(height.longValue());
- if(superblock == null)
- throw new Exception("Superblock {} not found in local chain at height {} " + superblockId + height.longValue());
- List<Sha256Hash> localHashes = superblock.getSyscoinBlockHashes();
- if(localHashes.size() != superblockChain.SUPERBLOCK_DURATION)
- throw new Exception("Local superblock must have 60 hashes, we found: " + localHashes.size());
- if(hashesFromContract.size() != superblockChain.SUPERBLOCK_DURATION)
- throw new Exception("Stored superblock must have 60 hashes, we found: " + hashesFromContract.size());
- // we want to ensure block->prev of first header matches previous superblock's last hash to check continuation of superblocks
- StoredBlock firstBlock = syscoinWrapper.getBlock(hashesFromContract.get(0));
- // if we don't have the block representing the first hash of the superblock then it must be a bad block to us, so we should ask submitter to prove 0th block
- if(firstBlock == null) {
- return 0;
- }
- // check first block prev hash matches prev superblock last block hash
- Sha256Hash lastBlockHash = ethWrapper.getSuperblockLastHash(superblock.getParentId());
- // add last block of prev superblock to the hashesFromContract as we will step through the local hashes and compare prev blocks to the contract hashes
- hashesFromContract.add(0, lastBlockHash);
- for (int i = 0; i < localHashes.size(); i++) {
- StoredBlock block = syscoinWrapper.getBlock(localHashes.get(i));
- if(block == null) {
- throw new Exception("Cannot find local block at index: " + i);
- }
- if(block.getHeader().getPrevBlockHash() != hashesFromContract.get(i)){
- return i;
- }
- }
- // if all matches then just return -1 meaning we don't have to check interim block for this challenge
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement