class Election: """Election class.""" def __init__(self, events_queue=None): self.bigchain = Bigchain() self.events_queue = events_queue def check_for_quorum(self, next_vote): """ Checks if block has enough invalid votes to make a decision Args: next_vote: The next vote. """ try: block_id = next_vote['vote']['voting_for_block'] node = next_vote['node_pubkey'] except KeyError: return next_block = self.bigchain.get_block(block_id) result = self.bigchain.block_election(next_block) self.handle_block_events(result, block_id) if result['status'] == self.bigchain.BLOCK_INVALID: return Block.from_dict(next_block) # Log the result if result['status'] != self.bigchain.BLOCK_UNDECIDED: msg = 'node:%s block:%s status:%s' % \ (node, block_id, result['status']) # Extra data can be accessed via the log formatter. # See logging.dictConfig. logger_results.debug(msg, extra={ 'current_vote': next_vote, 'election_result': result, }) def requeue_transactions(self, invalid_block): """ Liquidates transactions from invalid blocks so they can be processed again """ logger.info('Rewriting %s transactions from invalid block %s', len(invalid_block.transactions), invalid_block.id) for tx in invalid_block.transactions: self.bigchain.write_transaction(tx) return invalid_block def handle_block_events(self, result, block_id): if self.events_queue: if result['status'] == self.bigchain.BLOCK_UNDECIDED: return elif result['status'] == self.bigchain.BLOCK_INVALID: event_type = EventTypes.BLOCK_INVALID elif result['status'] == self.bigchain.BLOCK_VALID: event_type = EventTypes.BLOCK_VALID event = Event(event_type, self.bigchain.get_block(block_id)) self.events_queue.put(event)
class Election: """Election class.""" def __init__(self, events_queue=None): self.bigchain = Bigchain() self.events_queue = events_queue def check_for_quorum(self, next_vote): """Checks if block has enough invalid votes to make a decision Args: next_vote: The next vote. """ try: block_id = next_vote['vote']['voting_for_block'] node = next_vote['node_pubkey'] except KeyError: return next_block = self.bigchain.get_block(block_id) result = self.bigchain.block_election(next_block) self.handle_block_events(result, block_id) if result['status'] == self.bigchain.BLOCK_INVALID: return Block.from_dict(next_block) # Log the result if result['status'] != self.bigchain.BLOCK_UNDECIDED: msg = 'node:%s block:%s status:%s' % \ (node, block_id, result['status']) # Extra data can be accessed via the log formatter. # See logging.dictConfig. logger_results.debug(msg, extra={ 'current_vote': next_vote, 'election_result': result, }) def requeue_transactions(self, invalid_block): """Liquidates transactions from invalid blocks so they can be processed again """ logger.info('Rewriting %s transactions from invalid block %s', len(invalid_block.transactions), invalid_block.id) for tx in invalid_block.transactions: self.bigchain.write_transaction(tx) return invalid_block def handle_block_events(self, result, block_id): if self.events_queue: if result['status'] == self.bigchain.BLOCK_UNDECIDED: return elif result['status'] == self.bigchain.BLOCK_INVALID: event_type = EventTypes.BLOCK_INVALID elif result['status'] == self.bigchain.BLOCK_VALID: event_type = EventTypes.BLOCK_VALID event = Event(event_type, self.bigchain.get_block(block_id)) self.events_queue.put(event)
class Election: """Election class.""" def __init__(self): self.bigchain = Bigchain() def check_for_quorum(self, next_vote): """ Checks if block has enough invalid votes to make a decision Args: next_vote: The next vote. """ next_block = self.bigchain.get_block( next_vote['vote']['voting_for_block']) block_status = self.bigchain.block_election_status(next_block['id'], next_block['block']['voters']) if block_status == self.bigchain.BLOCK_INVALID: return Block.from_dict(next_block) def requeue_transactions(self, invalid_block): """ Liquidates transactions from invalid blocks so they can be processed again """ logger.info('Rewriting %s transactions from invalid block %s', len(invalid_block.transactions), invalid_block.id) for tx in invalid_block.transactions: self.bigchain.write_transaction(tx) return invalid_block
class Election: """Election class.""" def __init__(self): self.bigchain = Bigchain() def check_for_quorum(self, next_vote): """ Checks if block has enough invalid votes to make a decision Args: next_vote: The next vote. """ next_block = self.bigchain.get_block( next_vote['vote']['voting_for_block']) block_status = self.bigchain.block_election_status( next_block['id'], next_block['block']['voters']) if block_status == self.bigchain.BLOCK_INVALID: return Block.from_dict(next_block) def requeue_transactions(self, invalid_block): """ Liquidates transactions from invalid blocks so they can be processed again """ logger.info('Rewriting %s transactions from invalid block %s', len(invalid_block.transactions), invalid_block.id) for tx in invalid_block.transactions: self.bigchain.write_transaction(tx) return invalid_block