Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <[email protected]>
  • Loading branch information
sfroment committed Feb 4, 2025
1 parent 93d19e7 commit d4aa8d9
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 46 deletions.
8 changes: 6 additions & 2 deletions configs/e2e-bootstrap.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"network_config": {
"addresses": ["/ip4/0.0.0.0/tcp/50000/ws", "/ip4/0.0.0.0/tcp/50001"],
"listen_addresses": ["/ip4/0.0.0.0/tcp/50000/ws", "/ip4/0.0.0.0/tcp/50001"],
"bootstrap": true,
"bootstrap_peers": [],
"private_key_seed": "bootstrap",
"pubsub_peer_discovery_interval": 1000
"pubsub_peer_discovery_interval": 1000,
"pubsub": {
"prune_backoff": 10,
"heartbeat_interval": 10
}
}
}
17 changes: 7 additions & 10 deletions examples/grid/e2e/grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ async function getGlowingPeer(page: Page, peerID: string) {

const matchLeft = style.match(/left: ([0-9]+)px/);
const matchTop = style.match(/top: ([0-9]+)px/);
if (!matchLeft || !matchTop)
throw new Error("matchLeft or matchTop is not defined");
if (!matchLeft || !matchTop) throw new Error("matchLeft or matchTop is not defined");

return {
peerID: matchPeerID[1],
Expand All @@ -43,10 +42,12 @@ test.describe("grid", () => {

test.beforeEach(async ({ browser }) => {
page1 = await browser.newPage();
page2 = await browser.newPage();

await page1.goto("/");
await page1.waitForSelector("#loadingMessage", { state: "hidden" });

page2 = await browser.newPage();
await page2.goto("/");
await page2.waitForSelector("#loadingMessage", { state: "hidden" });
});

test.afterEach(async () => {
Expand Down Expand Up @@ -99,12 +100,8 @@ test.describe("grid", () => {
await page1.keyboard.press("w");
await page2.keyboard.press("s");

await expect(
page2.locator(`div[data-glowing-peer-id="${peerID1}"]`),
).toBeVisible();
await expect(
page2.locator(`div[data-glowing-peer-id="${peerID2}"]`),
).toBeVisible();
await expect(page2.locator(`div[data-glowing-peer-id="${peerID1}"]`)).toBeVisible();
await expect(page2.locator(`div[data-glowing-peer-id="${peerID2}"]`)).toBeVisible();

const glowingPeer1 = await getGlowingPeer(page1, peerID1);
const glowingPeer2 = await getGlowingPeer(page1, peerID2);
Expand Down
121 changes: 88 additions & 33 deletions examples/grid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,59 @@ import type { DRPObject } from "@ts-drp/object";
import { Grid } from "./objects/grid";
import { hslToRgb, rgbToHex, rgbToHsl } from "./util/color";

const node = new DRPNode({
network_config: {
...(import.meta.env.VITE_BOOTSTRAP_PEERS && {
bootstrap_peers: import.meta.env.VITE_BOOTSTRAP_PEERS.split(","),
}),
...(import.meta.env.VITE_DISCOVERY_INTERVAL && {
discovery_interval: import.meta.env.VITE_DISCOVERY_INTERVAL,
}),
},
});
const networkConfig = getNetworkConfigFromEnv();
const node = new DRPNode(networkConfig ? { network_config: networkConfig } : undefined);

let drpObject: DRPObject;
let gridDRP: Grid;
const peers: string[] = [];
const discoveryPeers: string[] = [];
let peers: string[] = [];
let discoveryPeers: string[] = [];
let objectPeers: string[] = [];

export function getNetworkConfigFromEnv() {
const hasBootstrapPeers = Boolean(import.meta.env.VITE_BOOTSTRAP_PEERS);
const hasDiscoveryInterval = Boolean(import.meta.env.VITE_DISCOVERY_INTERVAL);
const hasPubsubPruneBackoff = Boolean(import.meta.env.VITE_PUBSUB_PRUNE_BACKOFF);
const hasPubsubHeartbeatInterval = Boolean(import.meta.env.VITE_PUBSUB_HEARTBEAT_INTERVAL);

const hasEnv =
hasBootstrapPeers ||
hasDiscoveryInterval ||
hasPubsubPruneBackoff ||
hasPubsubHeartbeatInterval;

const config: Record<string, unknown> = {
browser_metrics: true,
};

if (!hasEnv) {
return config;
}

if (hasBootstrapPeers) {
config.bootstrap_peers = import.meta.env.VITE_BOOTSTRAP_PEERS.split(",");
}

if (hasDiscoveryInterval) {
config.pubsub_peer_discovery_interval = import.meta.env.VITE_DISCOVERY_INTERVAL;
}

if (hasPubsubPruneBackoff) {
config.pubsub = {
prune_backoff: import.meta.env.VITE_PUBSUB_PRUNE_BACKOFF,
};
}

if (hasPubsubHeartbeatInterval) {
config.pubsub = {
...(config.pubsub || {}),
heartbeat_interval: import.meta.env.VITE_PUBSUB_HEARTBEAT_INTERVAL,
};
}

return config;
}

const formatPeerId = (id: string): string => {
return `${id.slice(0, 4)}...${id.slice(-4)}`;
};
Expand Down Expand Up @@ -96,6 +132,8 @@ const renderClickablePeerList = (
let isDiscoveryPeersOpen = false;

const renderDiscoveryPeers = () => {
discoveryPeers = node.networkNode.getGroupPeers("drp::discovery");

renderClickablePeerList(discoveryPeers, isDiscoveryPeersOpen, "discoveryPeers", () => {
isDiscoveryPeersOpen = !isDiscoveryPeersOpen;
});
Expand All @@ -104,6 +142,8 @@ const renderDiscoveryPeers = () => {
let isPeersOpen = false;

const renderPeers = () => {
peers = node.networkNode.getAllPeers();

renderClickablePeerList(peers, isPeersOpen, "peers", () => {
isPeersOpen = !isPeersOpen;
});
Expand All @@ -112,6 +152,8 @@ const renderPeers = () => {
let isPeersInDRPOpen = false;

const renderPeersInDRP = () => {
if (drpObject) objectPeers = node.networkNode.getGroupPeers(drpObject.id);

renderClickablePeerList(
objectPeers,
isPeersInDRPOpen,
Expand Down Expand Up @@ -296,31 +338,34 @@ async function createConnectHandlers() {
});
}

async function main() {
await node.start();
render();
async function enableInterface() {
const loadingMessage = document.getElementById("loadingMessage");
if (loadingMessage) {
loadingMessage.style.display = "none";
}

//node.addNodeEventListener("peer:connect", () => {
// peers = node.networkNode.getAllPeers();
// discoveryPeers = node.networkNode.getGroupPeers("drp::discovery");
// render();
//});
const joinButton = <HTMLButtonElement>document.getElementById("joinGrid");
const createButton = <HTMLButtonElement>document.getElementById("createGrid");
const gridInput = <HTMLInputElement>document.getElementById("gridInput");
const copyButton = <HTMLButtonElement>document.getElementById("copyGridId");

//node.addNodeEventListener("peer:disconnect", () => {
// peers = node.networkNode.getAllPeers();
// discoveryPeers = node.networkNode.getGroupPeers("drp::discovery");
// render();
//});
joinButton.disabled = false;
createButton.disabled = false;
gridInput.disabled = false;
copyButton.disabled = false;
}

//node.addPubsubEventListener("subscription-change", (e) => {
// peers = node.networkNode.getAllPeers();
// discoveryPeers = node.networkNode.getGroupPeers("drp::discovery");
// if (e.detail.subscriptions.find((s) => s.topic === drpObject?.id)) {
// objectPeers = node.networkNode.getGroupPeers(drpObject.id);
// }
function renderInfo() {
renderPeerId();
renderPeers();
renderDiscoveryPeers();
renderPeersInDRP();
}

async function run() {
await enableInterface();

// render();
//});
renderInfo();

const button_create = <HTMLButtonElement>document.getElementById("createGrid");
button_create.addEventListener("click", async () => {
Expand Down Expand Up @@ -370,4 +415,14 @@ async function main() {
});
}

async function main() {
await node.start();
await node.networkNode.isDialable(async () => {
console.log("Started node", import.meta.env);
await run();
});

setInterval(renderInfo, import.meta.env.VITE_RENDER_INFO_INTERVAL);
}

void main();
3 changes: 3 additions & 0 deletions examples/grid/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
interface ImportMetaEnv {
readonly VITE_BOOTSTRAP_PEERS: string;
readonly VITE_DISCOVERY_INTERVAL: number;
readonly VITE_RENDER_INFO_INTERVAL: number;
readonly VITE_PUBSUB_PRUNE_BACKOFF: number;
readonly VITE_PUBSUB_HEARTBEAT_INTERVAL: number;
}

interface ImportMeta {
Expand Down
3 changes: 3 additions & 0 deletions examples/grid/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { defineConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";

export default defineConfig({
define: {
"import.meta.env.VITE_RENDER_INFO_INTERVAL": process.env.VITE_RENDER_INFO_INTERVAL || 1000,
},
build: {
target: "esnext",
},
Expand Down
16 changes: 16 additions & 0 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export interface DRPNetworkNodeConfig {
log_config?: LoggerOptions;
private_key_seed?: string;
pubsub_peer_discovery_interval?: number;
pubsub?: {
prune_backoff?: number;
heartbeat_interval?: number;
};
}

type PeerDiscoveryFunction =
Expand Down Expand Up @@ -115,6 +119,12 @@ export class DRPNetworkNode {
pubsub: gossipsub({
doPX: true,
allowPublishToZeroTopicPeers: true,
...(this._config?.pubsub?.prune_backoff
? { pruneBackoff: this._config.pubsub.prune_backoff }
: {}),
...(this._config?.pubsub?.heartbeat_interval
? { heartbeatInterval: this._config.pubsub.heartbeat_interval }
: {}),
scoreParams: createPeerScoreParams({
IPColocationFactorWeight: 0,
appSpecificScore: (peerId: string) => {
Expand Down Expand Up @@ -146,6 +156,12 @@ export class DRPNetworkNode {
doPX: true,
ignoreDuplicatePublishError: true,
allowPublishToZeroTopicPeers: true,
...(this._config?.pubsub?.prune_backoff
? { pruneBackoff: this._config.pubsub.prune_backoff }
: {}),
...(this._config?.pubsub?.heartbeat_interval
? { heartbeatInterval: this._config.pubsub.heartbeat_interval }
: {}),
scoreParams: createPeerScoreParams({
topicScoreCap: 50,
IPColocationFactorWeight: 0,
Expand Down
5 changes: 4 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export default defineConfig({
VITE_BOOTSTRAP_PEERS: [
"/ip4/127.0.0.1/tcp/50000/ws/p2p/12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ",
].join(","),
VITE_DISCOVERY_INTERVAL: "1000",
VITE_DISCOVERY_INTERVAL: "10",
VITE_RENDER_INFO_INTERVAL: "300",
VITE_PUBSUB_PRUNE_BACKOFF: "100",
VITE_PUBSUB_HEARTBEAT_INTERVAL: "1",
},
},
{
Expand Down

0 comments on commit d4aa8d9

Please sign in to comment.