Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. int findInvalidInterimBlockIndex(Keccak256Hash sessionId) throws Exception{
  2. List<Sha256Hash> hashesFromContract = ethWrapper.getBlockHashesBySession(sessionId);
  3. Keccak256Hash superblockId = ethWrapper.getSuperblockIdBySession(sessionId);
  4. BigInteger height = ethWrapper.getSuperblockHeight(superblockId);
  5. // find local superblock based on height of superblock being challenged
  6. Superblock superblock = superblockChain.getSuperblockByHeight(height.longValue());
  7. if(superblock == null)
  8. throw new Exception("Superblock {} not found in local chain at height {} " + superblockId + height.longValue());
  9.  
  10.  
  11.  
  12. List<Sha256Hash> localHashes = superblock.getSyscoinBlockHashes();
  13. if(localHashes.size() != superblockChain.SUPERBLOCK_DURATION)
  14. throw new Exception("Local superblock must have 60 hashes, we found: " + localHashes.size());
  15. if(hashesFromContract.size() != superblockChain.SUPERBLOCK_DURATION)
  16. throw new Exception("Stored superblock must have 60 hashes, we found: " + hashesFromContract.size());
  17.  
  18. // we want to ensure block->prev of first header matches previous superblock's last hash to check continuation of superblocks
  19. StoredBlock firstBlock = syscoinWrapper.getBlock(hashesFromContract.get(0));
  20. // 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
  21. if(firstBlock == null) {
  22. return 0;
  23. }
  24. // check first block prev hash matches prev superblock last block hash
  25. Sha256Hash lastBlockHash = ethWrapper.getSuperblockLastHash(superblock.getParentId());
  26. // 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
  27. hashesFromContract.add(0, lastBlockHash);
  28. for (int i = 0; i < localHashes.size(); i++) {
  29. StoredBlock block = syscoinWrapper.getBlock(localHashes.get(i));
  30. if(block == null) {
  31. throw new Exception("Cannot find local block at index: " + i);
  32. }
  33. if(block.getHeader().getPrevBlockHash() != hashesFromContract.get(i)){
  34. return i;
  35. }
  36. }
  37. // if all matches then just return -1 meaning we don't have to check interim block for this challenge
  38. return -1;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement