Esempio n. 1
0
def init_blockchain(rtx):
    '''
    If blockchain is empty this function will set genesis.
  '''
    genesis = Block(storage_space=storage_space)
    genesis.deserialize(serialized_genesis_block)
    storage_space.headers_manager.set_genesis(genesis.header, wtx=rtx)
    storage_space.headers_manager.best_tip = (
        storage_space.blockchain.current_tip(rtx=rtx),
        storage_space.blockchain.current_height(rtx=rtx))
    logger.info("Best header tip from blockchain state %d" %
                storage_space.headers_manager.best_tip[1])
    #greedy search
    current_tip = storage_space.headers_manager.best_tip[0]
    while True:
        try:
            header = storage_space.headers_storage.get(current_tip, rtx=rtx)
        except KeyError:
            break
        new_current_tip = current_tip
        if len(header.descendants):
            for d in header.descendants:
                dh = storage_space.headers_storage.get(d, rtx=rtx)
                if not dh.invalid:
                    new_current_tip = d
                    break
        if not new_current_tip == current_tip:
            current_tip = new_current_tip
        else:
            break
    storage_space.headers_manager.best_tip = (
        current_tip, storage_space.headers_storage.get(current_tip,
                                                       rtx=rtx).height)
    logger.info("Best header tip after greedy search %d" %
                storage_space.headers_manager.best_tip[1])
Esempio n. 2
0
def process_new_blocks(message):
    try:
        serialized_blocks = message["blocks"]
        num = message["num"]
        for i in range(num):
            block = Block(storage_space=storage_space)
            serialized_blocks = block.deserialize_raw(serialized_blocks)
            storage_space.blockchain.add_block(block, no_update=True)
        storage_space.blockchain.update(reason="downloaded new blocks")
    except Exception as e:
        raise e  #XXX "DoS messages should be returned"
Esempio n. 3
0
 def __getitem__(self, _hash):
     if not _hash in self.storage:
         raise KeyError(_hash)
     serialized_context_block = self.storage.get_by_hash(_hash)
     block = Block(storage_space=self.storage_space)
     cblock = ContextBlock(block=block)
     cblock.deserialize_raw(serialized_context_block)
     return cblock
Esempio n. 4
0
def process_new_blocks(message, wtx, core):
    try:
        serialized_blocks = message["blocks"]
        num = message["num"]
        for i in range(num):
            block = Block(storage_space=core.storage_space)
            serialized_blocks = block.deserialize_raw(serialized_blocks)
            core.storage_space.blockchain.add_block(block,
                                                    wtx=wtx,
                                                    no_update=True)
            if not i % 5:
                core.storage_space.blockchain.update(
                    wtx=wtx, reason="downloaded new blocks")
                core.notify(
                    "blockchain height",
                    core.storage_space.blockchain.current_height(rtx=wtx))
        core.storage_space.blockchain.update(wtx=wtx,
                                             reason="downloaded new blocks")
        core.notify("blockchain height",
                    core.storage_space.blockchain.current_height(rtx=wtx))
    except Exception as e:
        raise e
        raise DOSException()  #TODO add info
Esempio n. 5
0
def init_blockchain():
    '''
    If blockchain is empty this function will set genesis.
  '''
    genesis = Block(storage_space=storage_space)
    genesis.deserialize(serialized_genesis_block)
    storage_space.headers_manager.set_genesis(genesis.header)
    if not storage_space.blockchain.current_height >= 0:
        storage_space.headers_manager.context_validation(genesis.header.hash)
        genesis.non_context_verify()
        storage_space.blockchain.add_block(genesis)
    else:
        storage_space.headers_manager.best_tip = (
            storage_space.blockchain.current_tip,
            storage_space.blockchain.current_height)
        logger.info("Best header tip from blockchain state %d" %
                    storage_space.headers_manager.best_tip[1])
        #greedy search
        current_tip = storage_space.headers_manager.best_tip[0]
        while True:
            try:
                header = storage_space.headers_storage[current_tip]
            except KeyError:
                break
            new_current_tip = current_tip
            if len(header.descendants):
                for d in header.descendants:
                    dh = storage_space.headers_storage[d]
                    if not dh.invalid:
                        new_current_tip = d
                        break
            if not new_current_tip == current_tip:
                current_tip = new_current_tip
            else:
                break
        storage_space.headers_manager.best_tip = (
            current_tip, storage_space.headers_storage[current_tip].height)
        logger.info("Best header tip after greedy search %d" %
                    storage_space.headers_manager.best_tip[1])