class Branch(): def __init__(self, log, database, lasthash, firsthash=None): self.log = log self.database = database self.firsthash = firsthash self.lasthash = lasthash self.last = BlockIterator(database, lasthash) def is_mainchain(self): return (self.last.is_mainchain()) def work(self): return sum(blk.get_blockheader().work() for blk in self) def get_height(self): return self.last.get_height() def backward_iterblocks(self): pos = self.database.get_block_handle(self.lasthash) while pos.hasprev() and pos.hash != self.firsthash: yield pos pos = self.database.get_block_handle( pos.blockindex.blockheader.hash_prev) def foreward_iterblocks(self): #accumulate and reverse hash_prev links (TODO: add a chain length limit) blocks = list(self.backward_iterblocks()) blocks.reverse() for blk in blocks: yield blk def __iter__(self): return self.backward_iterblocks()
class Branch: def __init__(self, log, database, lasthash, firsthash=None): self.log = log self.database = database self.firsthash = firsthash self.lasthash = lasthash self.last = BlockIterator(database, lasthash) def is_mainchain(self): return self.last.is_mainchain() def work(self): return sum(blk.get_blockheader().work() for blk in self) def get_height(self): return self.last.get_height() def backward_iterblocks(self): pos = self.database.get_block_handle(self.lasthash) while pos.hasprev() and pos.hash != self.firsthash: yield pos pos = self.database.get_block_handle(pos.blockindex.blockheader.hash_prev) def foreward_iterblocks(self): # accumulate and reverse hash_prev links (TODO: add a chain length limit) blocks = list(self.backward_iterblocks()) blocks.reverse() for blk in blocks: yield blk def __iter__(self): return self.backward_iterblocks()
def get_block_locator(self): block_locator = [] it = BlockIterator(self.database, self.database.get_mainchain()) stepsize = 1 while (it.hash != self.database.genesishash): block_locator.append(it.hash) i = 0 while it.hasprev() and i < stepsize: it.prev() i += 1 stepsize*= 2 #tmp speedup hack #if stepsize >= 128: # break block_locator.append(self.database.genesishash) return BlockLocator(1, block_locator)
def __init__(self, log, database, lasthash, firsthash=None): self.log = log self.database = database self.firsthash = firsthash self.lasthash = lasthash self.last = BlockIterator(database, lasthash)