def is_valid_block_timestamp(self, msg: AbstractBlockMessage) -> bool: max_time_offset = self.connection.node.opts.blockchain_block_interval * self.connection.node.opts.blockchain_ignore_block_interval_count if time.time() - msg.timestamp() >= max_time_offset: self.connection.log_trace("Received block {} more than {} seconds after it was created ({}). Ignoring.", msg.block_hash(), max_time_offset, msg.timestamp()) return False return True
def process_msg_block(self, msg: AbstractBlockMessage, block_number: Optional[int] = None) -> None: block_hash = msg.block_hash() # if gateway is still syncing, skip this process if not self.node.should_process_block_hash(block_hash): return self.node.block_cleanup_service.on_new_block_received( block_hash, msg.prev_block_hash()) block_stats.add_block_event_by_block_hash( block_hash, BlockStatEventType.BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE, network_num=self.connection.network_num, more_info="Protocol: {}, Network: {}".format( self.node.opts.blockchain_protocol, self.node.opts.blockchain_network, msg.extra_stats_data())) gateway_bdn_performance_stats_service.log_block_message_from_blockchain_node( self.connection.endpoint, True) if block_hash in self.node.blocks_seen.contents: self.node.on_block_seen_by_blockchain_node( block_hash, self.connection, msg, block_number=block_number) block_stats.add_block_event_by_block_hash( block_hash, BlockStatEventType. BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE_IGNORE_SEEN, network_num=self.connection.network_num) self.connection.log_info( "Discarding duplicate block {} from local blockchain node.", block_hash) self.node.log_blocks_network_content(self.node.network_num, msg) return if not self.is_valid_block_timestamp(msg): return gateway_bdn_performance_stats_service.log_block_from_blockchain_node( self.connection.endpoint) canceled_recovery = self.node.on_block_seen_by_blockchain_node( block_hash, self.connection, msg) if canceled_recovery: return self.node.track_block_from_node_handling_started(block_hash) self.node.on_block_seen_by_blockchain_node(block_hash, self.connection, msg, block_number=block_number) self.node.block_processing_service.queue_block_for_processing( msg, self.connection) self.node.block_queuing_service_manager.push( block_hash, msg, node_received_from=self.connection) return
def msg_block(self, msg: AbstractBlockMessage): """ Handle a block message. Sends to node for encryption, then broadcasts. """ block_hash = msg.block_hash() node = self.connection.node # if gateway is still syncing, skip this process if not node.should_process_block_hash(block_hash): return node.block_cleanup_service.on_new_block_received(block_hash, msg.prev_block_hash()) block_stats.add_block_event_by_block_hash(block_hash, BlockStatEventType.BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE, network_num=self.connection.network_num, more_info="Protocol: {}, Network: {}".format( node.opts.blockchain_protocol, node.opts.blockchain_network, msg.extra_stats_data() ) ) if block_hash in self.connection.node.blocks_seen.contents: node.on_block_seen_by_blockchain_node(block_hash) block_stats.add_block_event_by_block_hash(block_hash, BlockStatEventType.BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE_IGNORE_SEEN, network_num=self.connection.network_num) self.connection.log_info( "Discarding duplicate block {} from local blockchain node.", block_hash ) return if not self.is_valid_block_timestamp(msg): return node.track_block_from_node_handling_started(block_hash) node.on_block_seen_by_blockchain_node(block_hash, msg) node.block_processing_service.queue_block_for_processing(msg, self.connection) gateway_bdn_performance_stats_service.log_block_from_blockchain_node() node.block_queuing_service.store_block_data(block_hash, msg) return