@kadena/hardhat-chainweb
Version:
Hardhat plugin for Kadena's Chainweb network
97 lines • 2.71 kB
JavaScript
"use strict";
/* *************************************************************************** */
/* Compute Chain Distances */
Object.defineProperty(exports, "__esModule", { value: true });
exports.distance = distance;
exports.createGraph = createGraph;
// Compute shortest path via breadth first search. For small, connected,
// degree-diameter graphs this has acceptable performance. (well, it's bad, but
// not terribly bad)
//
function distance(srcChain, trgChain, graph) {
if (srcChain == trgChain) {
return 0;
}
const visited = [srcChain];
const queue = [[srcChain, 0]];
while (queue.length > 0) {
const [cur, d] = queue.shift();
for (const adj of graph[cur]) {
if (adj == trgChain) {
return d + 1;
}
if (!visited.includes(adj)) {
visited.push(adj);
queue.push([adj, d + 1]);
}
}
}
throw new Error('Chain not found in Chainweb');
}
function createGraph(chains = 2) {
switch (chains) {
case 2:
return [[1], [0]];
case 3:
return [
[],
[],
[],
];
case 4:
return [
[],
[],
[],
[],
];
case 5:
return [
[],
[],
[],
[],
[],
];
case 10:
return [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
case 20: {
return [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
];
}
default:
throw new Error('Valid chain counts are 2, 3, 4, 5, 10, 20; if you need a different chain count, please provide the graph explicitly');
}
}
//# sourceMappingURL=chainweb-graph.js.map