Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion src/did/did-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,50 @@ if (fs.existsSync(config.did.storagePath)) {
didState = JSON.parse(didStateJson);
} else {
if (config.did.method === 'ion') {
didState = await DIDIon.generate({ serviceEndpoint: 'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/' });



const endpoints = [
'https://dwn-usa-1.ue8cktdq71va0.us-east-2.cs.amazonlightsail.com/',
'https://dwn-aggregator.faktj7f1fndve.ap-southeast-2.cs.amazonlightsail.com/',
'https://dwn-india.vtv94qck5sjvq.ap-south-1.cs.amazonlightsail.com/'
];

const selectedEndpoints = async () => {
let responseTimes = [];

// Pick 3 random endpoints
let randomEndpoints = [];
while (randomEndpoints.length < 3) {
let randomIndex = Math.floor(Math.random() * endpoints.length);
if (!randomEndpoints.includes(endpoints[randomIndex])) {
randomEndpoints.push(endpoints[randomIndex]);
}
}

// Check the response time of each endpoint and make sure it returns 200 OK
for (let endpoint of randomEndpoints) {
let start = performance.now();
let response = await fetch(endpoint + "health");
let end = performance.now();
if (response.status === 200) {
responseTimes.push({
endpoint,
responseTime: end - start
});
}
}

// Sort the endpoints based on response time, fastest first
responseTimes.sort((a, b) => a.responseTime - b.responseTime);

return responseTimes.map(rt => rt.endpoint);
}

const serviceEndpoints = await selectedEndpoints();
const serviceEndpoint = serviceEndpoints[0];

didState = await DIDIon.generate({ serviceEndpoint: serviceEndpoint });
fs.writeFileSync(config.did.storagePath, JSON.stringify(didState, null, 2));
} else {
throw new Error(`DID Method ${config.did.method} not supported`);
Expand Down