# argument is an array that consists of a file descriptor with the file number def getAllBlocks(blkFileWithNumber): blkInfoList = [] with open(blkFileWithNumber[1], 'rb') as f: fSize = getFileSize(f) while f.tell() < fSize: blkInfo = getNextBlock(f) if blkInfo != None: blkInfoWithFileNum = [blkFileWithNumber[0]] blkInfoWithFileNum.extend(blkInfo) blkInfoList.append(blkInfoWithFileNum) return blkInfoList # only goes back 2 files so anything over 800 could get cut off # at 2 block files worth of data def getLastNBlocks(n=10): lastFile, secondToLastFile = getLastTwoFiles() lastBlocks = getAllBlocks(lastFile) if len(lastBlocks) < n: secondToLastBlocks = getAllBlocks(secondToLastFile) secondToLastBlocks.extend(lastBlocks) lastBlocks = secondToLastBlocks return lastBlocks[-n:] lastNBlocks = getLastNBlocks(3) for blk in lastNBlocks: print blk[0], binary_to_hex(blk[1], BIGENDIAN), blk[2], blk[3]
f.seek(0,2) result = f.tell() f.seek(pos) return result def getNextBlockHash(f): fileOffset = f.tell() f.seek(MAGIC_NUMBER_LENGTH, 1) blkSize = binary_to_int(f.read(BLOCK_SIZE_LENGTH), LITTLEENDIAN) result = None if blkSize > 0: blkString = f.read(blkSize) blkHdrBinary = blkString[:HEADER_LENGTH] result = sha256(sha256(blkHdrBinary)) else: f.seek(0,2) return result def getLastBlockHash(blkFile): result = None with open(blkFile, 'rb') as f: fSize = getFileSize(f) while f.tell() < fSize: blockHash = getNextBlockHash(f) if blockHash != None: result = blockHash return result print binary_to_hex(getLastBlockHash(getLastBlockFile()), BIGENDIAN)
return result # argument is an array that consists of a file descriptor with the file number def getAllBlocks(blkFileWithNumber): blkInfoList = [] with open(blkFileWithNumber[1], 'rb') as f: fSize = getFileSize(f) while f.tell() < fSize: blkInfo = getNextBlock(f) if blkInfo != None: blkInfoWithFileNum = [blkFileWithNumber[0]] blkInfoWithFileNum.extend(blkInfo) blkInfoList.append(blkInfoWithFileNum) return blkInfoList # only goes back 2 files so anything over 800 could get cut off # at 2 block files worth of data def getLastNBlocks(n=10): lastFile, secondToLastFile = getLastTwoFiles() lastBlocks = getAllBlocks(lastFile) if len(lastBlocks) < n: secondToLastBlocks = getAllBlocks(secondToLastFile) secondToLastBlocks.extend(lastBlocks) lastBlocks = secondToLastBlocks return lastBlocks[-n:] lastNBlocks = getLastNBlocks(3) for blk in lastNBlocks: print blk[0], binary_to_hex(blk[1], BIGENDIAN), blk[2], blk[3]