def notify_peer_connected(self, peer_id): """A new peer was added""" self._notify( validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, consensus_pb2.ConsensusNotifyPeerConnected( peer_info=consensus_pb2.ConsensusPeerInfo( peer_id=bytes.fromhex(peer_id))))
def handle_request(self, request, response, connection_id): startup_info = self._proxy.register(request.name, request.version, connection_id) if startup_info is None: response.status = consensus_pb2.ConsensusRegisterResponse.NOT_READY return HandlerStatus.RETURN chain_head = startup_info.chain_head peers = [bytes.fromhex(peer_id) for peer_id in startup_info.peers] local_peer_info = startup_info.local_peer_info response.chain_head.block_id = bytes.fromhex(chain_head.identifier) response.chain_head.previous_id =\ bytes.fromhex(chain_head.previous_block_id) response.chain_head.signer_id =\ bytes.fromhex(chain_head.signer_public_key) response.chain_head.block_num = chain_head.block_num response.chain_head.payload = chain_head.consensus response.peers.extend([ consensus_pb2.ConsensusPeerInfo(peer_id=peer_id) for peer_id in peers ]) response.local_peer_info.peer_id = local_peer_info LOGGER.info("Consensus engine registered: %s %s", request.name, request.version) return HandlerStatus.RETURN_AND_PASS
def notify_peer_join_cluster(self, peer_id, cname): """ peer change role or became arbiter """ LOGGER.debug( 'ConsensusNotifier: notify_peer_join_cluster peer_id=%s PARAM=%s', peer_id[:10], cname) self._notify( validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, ConsensusNotifyPeerConnected( peer_info=consensus_pb2.ConsensusPeerInfo( peer_id=bytes.fromhex(peer_id)), status=ConsensusNotifyPeerConnected.JOIN_CLUSTER, mode=ConsensusNotifyPeerConnected.NORMAL, info=cname))
def notify_topology_peer(self, peer_id, list, is_new=True): """ peer add/del """ LOGGER.debug('ConsensusNotifier: notify_topology_peer peer_id=%s', peer_id[:10]) self._notify( validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, ConsensusNotifyPeerConnected( peer_info=consensus_pb2.ConsensusPeerInfo( peer_id=bytes.fromhex(peer_id)), status=ConsensusNotifyPeerConnected.ADD_PEER if is_new else ConsensusNotifyPeerConnected.DEL_PEER, mode=ConsensusNotifyPeerConnected.NORMAL, info=list))
def notify_peer_change_role(self, peer_id, cname, is_arbiter=False): """ peer change role or became arbiter """ LOGGER.debug( 'ConsensusNotifier: notify_peer_change_role peer_id=%s CLUSTER=%s ARBITER=%s', peer_id[:10], cname, is_arbiter) self._notify( validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, ConsensusNotifyPeerConnected( peer_info=consensus_pb2.ConsensusPeerInfo( peer_id=bytes.fromhex(peer_id)), status=ConsensusNotifyPeerConnected.ARBITER_CHANGE if is_arbiter else ConsensusNotifyPeerConnected.ROLE_CHANGE, mode=ConsensusNotifyPeerConnected.NORMAL, info=cname))
def handle_request(self, request, response): #LOGGER.debug('ConsensusRegisterHandler: proxy.register') startup_info = self._proxy.register() if startup_info is None: # not ready for working with consensus engine response.status = consensus_pb2.ConsensusRegisterResponse.NOT_READY if self._last_status != response.status: LOGGER.debug('ConsensusRegisterHandler: NOT READY yet for working with consensus engine!\n') self._last_status = response.status return #if self._proxy.is_recovery : # recovery mode # response.status = consensus_pb2.ConsensusRegisterResponse.RECOVERY chain_head = startup_info.chain_head # README when not enought resource some peers could not be connected peers = [bytes.fromhex(peer_id) for peer_id in startup_info.peers if peer_id is not None] local_peer_info = startup_info.local_peer_info LOGGER.debug('ConsensusRegisterHandler: peers=%s local=%s chain_head[%s]=%s header=%s block=%s',peers,local_peer_info,type(chain_head),chain_head,type(chain_head.header),type(chain_head.block)) block_header = BlockHeader() """ for last version validator(rust) used chain_head.header because chain_head is Block not WrapperBlock as for python validator """ block_header.ParseFromString(chain_head.block.header) response.chain_head.block_id = bytes.fromhex(chain_head.header_signature) response.chain_head.previous_id = bytes.fromhex(block_header.previous_block_id) response.chain_head.signer_id = bytes.fromhex(block_header.signer_public_key) response.chain_head.block_num = block_header.block_num response.chain_head.payload = block_header.consensus response.peers.extend([ consensus_pb2.ConsensusPeerInfo(peer_id=peer_id) for peer_id in peers ]) response.local_peer_info.peer_id = local_peer_info response.peering_mode = startup_info.peering_mode self._consensus_notifier.add_registered_engine(request.name,request.version) LOGGER.info("Consensus engine registered: %s %s",request.name,request.version)
def notify_peer_connected(self, peer_id, assemble=True, mode=ConsensusNotifyPeerConnected.NORMAL): """ A new peer was added """ LOGGER.debug( 'ConsensusNotifier: notify_peer_connected peer_id=%s assemble=%s', peer_id[:10], assemble) self._notify( validator_pb2.Message.CONSENSUS_NOTIFY_PEER_CONNECTED, ConsensusNotifyPeerConnected( peer_info=consensus_pb2.ConsensusPeerInfo( peer_id=bytes.fromhex(peer_id)), status=ConsensusNotifyPeerConnected.OK if assemble else ConsensusNotifyPeerConnected.NOT_READY, mode=mode, ))
def handle_request(self, request, response): #LOGGER.debug('ConsensusRegisterHandler: proxy.register') startup_info = self._proxy.register() if startup_info is None: response.status = consensus_pb2.ConsensusRegisterResponse.NOT_READY return chain_head = startup_info.chain_head peers = [bytes.fromhex(peer_id) for peer_id in startup_info.peers] local_peer_info = startup_info.local_peer_info LOGGER.debug( 'ConsensusRegisterHandler: peers=%s local=%s chain_head[%s]=%s header=%s', peers, local_peer_info, type(chain_head), chain_head, type(chain_head.header)) block_header = BlockHeader() block_header.ParseFromString(chain_head.header) response.chain_head.block_id = bytes.fromhex( chain_head.header_signature) response.chain_head.previous_id =\ bytes.fromhex(block_header.previous_block_id) response.chain_head.signer_id =\ bytes.fromhex(block_header.signer_public_key) response.chain_head.block_num = block_header.block_num response.chain_head.payload = block_header.consensus response.peers.extend([ consensus_pb2.ConsensusPeerInfo(peer_id=peer_id) for peer_id in peers ]) response.local_peer_info.peer_id = local_peer_info self._consensus_notifier.add_registered_engine(request.name, request.version) LOGGER.info("Consensus engine registered: %s %s", request.name, request.version)