Skip to content

Unable to sparql query paranet: Request failed with status code 500 #242

@Valcyclovir

Description

@Valcyclovir

Steps:

  1. run a testnet node on base testnet
  2. create a paranet
  3. create assets on the paranet
  4. paranet sync on the testnet node (sync verified working in the logs)
  5. run a sparql query to that paranet using same priv key that created paranet
require('dotenv').config();
const DKGClient = require('dkg.js');

const node_options = {
  endpoint: process.env.OTNODE_HOST || 'http://localhost',
  port: process.env.OTNODE_PORT || '8900',
  blockchain: {
    name: process.env.BLOCKCHAIN_NAME || 'base:84532',
    privateKey: process.env.PRIVATE_KEY || '',
  },
  maxNumberOfRetries: parseInt(process.env.MAX_NUMBER_OF_RETRIES) || 300,
  frequency: parseInt(process.env.FREQUENCY) || 2,
  contentType: process.env.CONTENT_TYPE || 'all',
  nodeApiVersion: '/v1',
  useSSL: process.env.USE_SSL,
};

// Load PARANET_UAL
const PARANET_UAL = process.env.PARANET_UAL || 'did:dkg:base:84532/0xd5550173b0f7b8766ab2770e4ba86caf714a5af5/191718/1';

function validateConfig(config, paranetUAL) {
  console.log('Validating configuration...');
  const requiredStringFields = ['endpoint', 'port'];
  for (const field of requiredStringFields) {
    if (typeof config[field] !== 'string' || !config[field]) {
      throw new Error(`Invalid configuration: Missing or invalid value for '${field}'`);
    }
  }
  if (!config.blockchain || typeof config.blockchain !== 'object') {
    throw new Error("Invalid configuration: 'blockchain' must be an object");
  }
  const blockchainFields = ['name', 'privateKey'];
  for (const field of blockchainFields) {
    if (typeof config.blockchain[field] !== 'string' || !config.blockchain[field]) {
      throw new Error(`Invalid configuration: Missing or invalid value for 'blockchain.${field}'`);
    }
  }
  if (!paranetUAL || typeof paranetUAL !== 'string') {
    throw new Error("Invalid configuration: 'PARANET_UAL' must be a string");
  }
  const ualPattern = /^did:dkg:base:84532\/0x[a-fA-F0-9]{40}\/\d+\/\d+$/;
  if (!ualPattern.test(paranetUAL)) {
    throw new Error("Invalid configuration: 'PARANET_UAL' has an invalid format");
  }
  console.log('Configuration validated successfully.');
  console.log('Config:', {
    endpoint: config.endpoint,
    port: config.port,
    blockchain: config.blockchain.name,
    maxNumberOfRetries: config.maxNumberOfRetries,
    frequency: config.frequency,
    paranetUAL: paranetUAL,
  });
}

// Initialize DKG Client
try {
  validateConfig(node_options, PARANET_UAL);
} catch (error) {
  console.error('Configuration validation failed:', error.message);
  process.exit(1);
}
const dkg = new DKGClient(node_options);

// SPARQL query to retrieve Dataset assets
const sparqlQuery = `
PREFIX SCHEMA: <http://schema.org/>
SELECT DISTINCT ?asset ?name ?description ?keywords ?datePublished
WHERE {
  ?asset a SCHEMA:Dataset ;
         SCHEMA:name ?name ;
         SCHEMA:description ?description .
  OPTIONAL { ?asset SCHEMA:keywords ?keywords . }
  OPTIONAL { ?asset SCHEMA:datePublished ?datePublished . }
}
LIMIT 10
`.trim();

async function executeQuery() {
  try {
    // Verify node connection
    console.log('Checking node connection...');
    const nodeInfo = await dkg.node.info();
    console.log('Connected to node:', JSON.stringify(nodeInfo, null, 2));

    // Wait for graph propagation
    console.log('Waiting for graph propagation...');
    await new Promise(resolve => setTimeout(resolve, 100));

    // Execute query with PARANET_UAL
    console.log('Executing SPARQL query with PARANET_UAL:\n', sparqlQuery);
    console.log('Query options:', {
      blockchain: node_options.blockchain.name,
      maxNumberOfRetries: node_options.maxNumberOfRetries,
      frequency: node_options.frequency,
      paranetUAL: PARANET_UAL,
    });
    let queryResult;
    try {
      queryResult = await dkg.graph.query(sparqlQuery, 'SELECT', {
        blockchain: {
          name: node_options.blockchain.name,
          privateKey: node_options.blockchain.privateKey,
        },
        maxNumberOfRetries: node_options.maxNumberOfRetries,
        frequency: node_options.frequency,
        paranetUAL: PARANET_UAL,
      });
    } catch (queryError) {
      console.error('Raw query error:', queryError.message);
      throw new Error(`DKG query failed: ${queryError.message}`);
    }

    if (!queryResult || !queryResult.data) {
      throw new Error('DKG query failed: No data returned');
    }

    const results = queryResult.data.map(entry => {
      const formattedParts = Object.keys(entry).map(key => `${key}: ${entry[key]}`);
      return formattedParts.join(', ');
    });

    console.log('Query results:', JSON.stringify(results, null, 2));
  } catch (error) {
    console.error('Error executing query:', error.message);
    console.error('Error details:', JSON.stringify(error, null, 2));
    throw error;
  }
}

// Run the script
executeQuery().catch(error => {
  console.error('Execution failed:', error.message);
  console.error('Stack trace:', error.stack);
  process.exit(1);
});

Error returned:

Raw query error: Unable to query: Request failed with status code 500
Error executing query: DKG query failed: Unable to query: Request failed with status code 500
Error details: {}
Execution failed: DKG query failed: Unable to query: Request failed with status code 500
Stack trace: Error: DKG query failed: Unable to query: Request failed with status code 500
at executeQuery (/root/dkg-scripts/paranetSparqlQuery.js:111:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions