Example #1
0
    def add_block(self, block, forward=False):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        if not block.validate_uncles():
            logger.debug('Invalid uncles %r', block)
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False
        # Forward block w/ valid PoW asap (if not syncing)
        if forward:
            signals.broadcast_new_block.send(sender=None, block=block)
            logger.debug("broadcasting new %r" % block)

        if block.has_parent():
            try:
                #logger.debug('verifying: %s', block)
                #logger.debug('GETTING ACCOUNT FOR COINBASE:')
                #acct = block.get_acct(block.coinbase)
                #logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                logger.debug('### VERIFICATION FAILED ### %r', e)
                f = os.path.join(utils.data_dir, 'badblock.log')
                open(f, 'w').write(str(block.hex_serialize()))
                print block.hex_serialize()
                return False

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        #logger.debug('Head: %r @%s  New:%r @%d', self.head, self.head.chain_difficulty(), block, block.chain_difficulty())
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)
        elif block.number > self.head.number:
            logger.warn(
                '%r has higher blk number than head %r but lower chain_difficulty of %d vs %d',
                block, self.head, block.chain_difficulty(),
                self.head.chain_difficulty())
        self.commit()  # batch commits all changes that came with the new block

        return True
Example #2
0
    def add_block(self, block, forward=False):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        if not block.validate_uncles():
            logger.debug('Invalid uncles %r', block)
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False
        # Forward block w/ valid PoW asap (if not syncing)
        if forward:
            signals.broadcast_new_block.send(sender=None, block=block)
            logger.debug("broadcasting new %r" % block)

        if block.has_parent():
            try:
                #logger.debug('verifying: %s', block)
                #logger.debug('GETTING ACCOUNT FOR COINBASE:')
                #acct = block.get_acct(block.coinbase)
                #logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                logger.debug('### VERIFICATION FAILED ### %r', e)
                f = os.path.join(utils.data_dir, 'badblock.log')
                open(f, 'w').write(str(block.hex_serialize()))
                print block.hex_serialize()
                return False

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        #logger.debug('Head: %r @%s  New:%r @%d', self.head, self.head.chain_difficulty(), block, block.chain_difficulty())
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)
        elif block.number > self.head.number:
            logger.warn('%r has higher blk number than head %r but lower chain_difficulty of %d vs %d',
                        block, self.head, block.chain_difficulty(), self.head.chain_difficulty())
        self.commit()  # batch commits all changes that came with the new block

        return True
Example #3
0
    def add_block(self, block, forward=False):
        "returns True if block was added sucessfully"
        _log = log.bind(block_hash=block)
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            _log.debug('missing parent')
            return False

        if not block.validate_uncles():
            _log.debug('invalid uncles')
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            _log.debug('nonce not set')
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            _log.debug('invalid nonce')
            return False
        # Forward block w/ valid PoW asap (if not syncing)
        # FIXME: filter peer by wich block was received
        if forward:
            _log.debug("broadcasting new")
            signals.broadcast_new_block.send(sender=None, block=block)

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                _log.critical('VERIFICATION FAILED', error=e)
                f = os.path.join(utils.data_dir, 'badblock.log')
                open(f, 'w').write(str(block.hex_serialize()))
                return False

        if block.number < self.head.number:
            _log.debug("older than head", head_hash=self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        if block.chain_difficulty() > self.head.chain_difficulty():
            _log.debug('new head')
            self._update_head(block)
        elif block.number > self.head.number:
            _log.warn(
                'has higher blk number than head but lower chain_difficulty',
                head_hash=self.head,
                block_difficulty=block.chain_difficulty(),
                head_difficulty=self.head.chain_difficulty())
        self.commit()  # batch commits all changes that came with the new block
        return True
Example #4
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        if not block.validate_uncles():
            logger.debug('Invalid uncles %r', block)
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        # FIXME: Forward blocks w/ valid PoW asap
        if block.has_parent():
            try:
                #logger.debug('verifying: %s', block)
                #logger.debug('GETTING ACCOUNT FOR COINBASE:')
                #acct = block.get_acct(block.coinbase)
                #logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                logger.debug('%r', e)
                return False

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        #logger.debug('Head: %r @%s  New:%r @%d', self.head, self.head.chain_difficulty(), block, block.chain_difficulty())
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)
        elif block.number > self.head.number:
            logger.warn(
                '%r has higher blk number than head %r but lower chain_difficulty of %d vs %d',
                block, self.head, block.chain_difficulty(),
                self.head.chain_difficulty())
        self.commit()  # batch commits all changes that came with the new block

        return True
Example #5
0
    def add_block(self, block, forward=False):
        "returns True if block was added sucessfully"
        _log = log.bind(block_hash=block)
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            _log.debug('missing parent')
            return False

        if not block.validate_uncles():
            _log.debug('invalid uncles')
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            _log.debug('nonce not set')
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            _log.debug('invalid nonce')
            return False
        # Forward block w/ valid PoW asap (if not syncing)
        # FIXME: filter peer by wich block was received
        if forward:
            _log.debug("broadcasting new")
            signals.broadcast_new_block.send(sender=None, block=block)

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                _log.critical('VERIFICATION FAILED', error=e)
                f = os.path.join(utils.data_dir, 'badblock.log')
                open(f, 'w').write(str(block.hex_serialize()))
                return False

        if block.number < self.head.number:
            _log.debug("older than head", head_hash=self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        if block.chain_difficulty() > self.head.chain_difficulty():
            _log.debug('new head')
            self._update_head(block)
        elif block.number > self.head.number:
            _log.warn('has higher blk number than head but lower chain_difficulty',
                      head_hash=self.head, block_difficulty=block.chain_difficulty(),
                      head_difficulty=self.head.chain_difficulty())
        self.commit()  # batch commits all changes that came with the new block
        return True
Example #6
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        # make sure we know the uncles
        # for uncle_hash in block.uncles:
        #     if not uncle_hash in self:
        #         logger.debug('Missing uncle for block %r', block)
        #        return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        # FIXME: Forward blocks w/ valid PoW asap

        if block.has_parent():
            try:
                logger.debug('verifying: %s', block)
                logger.debug('GETTING ACCOUNT FOR COINBASE:')
                acct = block.get_acct(block.coinbase)
                logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)

                processblock.verify(block, block.get_parent())
            except AssertionError as e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False

        self.index.add_block(block)
        self._store_block(block)

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)

        # FIXME: Should we have any limitations on adding blocks?

        # set to head if this makes the longest chain w/ most work for that number
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)

        return True
Example #7
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        # make sure we know the uncles
        # for uncle_hash in block.uncles:
        #     if not uncle_hash in self:
        #         logger.debug('Missing uncle for block %r', block)
        #        return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        # FIXME: Forward blocks w/ valid PoW asap

        if block.has_parent():
            try:
                logger.debug('verifying: %s', block)
                logger.debug('GETTING ACCOUNT FOR COINBASE:')
                acct = block.get_acct(block.coinbase)
                logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)

                processblock.verify(block, block.get_parent())
            except AssertionError as e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False

        self.index.add_block(block)
        self._store_block(block)

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)

        # FIXME: Should we have any limitations on adding blocks?

        # set to head if this makes the longest chain w/ most work for that number
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)

        return True
Example #8
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        if not block.validate_uncles():
            logger.debug('Invalid uncles %r', block)
            return False

        # check PoW and forward asap in order to avoid stale blocks
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        # FIXME: Forward blocks w/ valid PoW asap
        if block.has_parent():
            try:
                #logger.debug('verifying: %s', block)
                #logger.debug('GETTING ACCOUNT FOR COINBASE:')
                #acct = block.get_acct(block.coinbase)
                #logger.debug('GOT ACCOUNT FOR COINBASE: %r', acct)
                processblock.verify(block, block.get_parent())
            except processblock.VerificationFailed as e:
                logger.debug('%r', e)
                return False

        if block.number < self.head.number:
            logger.debug("%r is older than head %r", block, self.head)
            # Q: Should we have any limitations on adding blocks?

        self.index.add_block(block)
        self._store_block(block)

        # set to head if this makes the longest chain w/ most work for that number
        #logger.debug('Head: %r @%s  New:%r @%d', self.head, self.head.chain_difficulty(), block, block.chain_difficulty())
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)
        elif block.number > self.head.number:
            logger.warn('%r has higher blk number than head %r but lower chain_difficulty of %d vs %d',
                                block, self.head, block.chain_difficulty(), self.head.chain_difficulty())
        self.commit() # batch commits all changes that came with the new block

        return True
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        # make sure we know the uncles
        # for uncle_hash in block.uncles:
        #     if not uncle_hash in self:
        #         logger.debug('Missing uncle for block %r', block)
        #        return False

        # check PoW
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except AssertionError as e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False

        self._children_index.append(block.prevhash, block.hash)
        self._store_block(block)
        # set to head if this makes the longest chain w/ most work
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)

        # log the block
        #chainlogger.log_block(block)
        return True
Example #10
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block.hex_hash())
            return False

        # check PoW
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block.hex_hash())
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block.hex_hash())
            return False

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except AssertionError, e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False
Example #11
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block.hex_hash())
            return False

        # check PoW
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block.hex_hash())
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block.hex_hash())
            return False

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except AssertionError, e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False
Example #12
0
    def add_block(self, block):
        "returns True if block was added sucessfully"
        # make sure we know the parent
        if not block.has_parent() and not block.is_genesis():
            logger.debug('Missing parent for block %r', block)
            return False

        # make sure we know the uncles
        for uncle_hash in block.uncles:
            if not uncle_hash in self:
                logger.debug('Missing uncle for block %r', block)
                return False

        # check PoW
        if not len(block.nonce) == 32:
            logger.debug('Nonce not set %r', block)
            return False
        elif not block.check_proof_of_work(block.nonce) and\
                not block.is_genesis():
            logger.debug('Invalid nonce %r', block)
            return False

        if block.has_parent():
            try:
                processblock.verify(block, block.get_parent())
            except AssertionError as e:
                logger.debug('verification failed: %s', str(e))
                processblock.verify(block, block.get_parent())
                return False

        self._children_index.append(block.prevhash, block.hash)
        self._store_block(block)
        # set to head if this makes the longest chain w/ most work
        if block.chain_difficulty() > self.head.chain_difficulty():
            logger.debug('New Head %r', block)
            self._update_head(block)
        return True