def initialize_block(self, previous_id): request = ( consensus_pb2.ConsensusInitializeBlockRequest( previous_id=previous_id) if previous_id else consensus_pb2.ConsensusInitializeBlockRequest() ) response_type = consensus_pb2.ConsensusInitializeBlockResponse response = self._send( request=request, message_type=Message.CONSENSUS_INITIALIZE_BLOCK_REQUEST, response_type=response_type) status = response.status if status == response_type.INVALID_STATE: raise exceptions.InvalidState( 'Cannot initialize block in current state') if status == response_type.UNKNOWN_BLOCK: raise exceptions.UnknownBlock() if status != response_type.OK: raise exceptions.ReceiveError( 'Failed with status {}'.format(status))
def test_initialize_block(self): self.mock_stream.send.return_value = self._make_future( message_type=Message.CONSENSUS_INITIALIZE_BLOCK_RESPONSE, content=consensus_pb2.ConsensusInitializeBlockResponse( status=consensus_pb2.ConsensusInitializeBlockResponse.OK)) self.service.initialize_block(previous_id=b'test') self.mock_stream.send.assert_called_with( message_type=Message.CONSENSUS_INITIALIZE_BLOCK_REQUEST, content=consensus_pb2.ConsensusInitializeBlockRequest( previous_id=b'test').SerializeToString())