Skip to content
This repository was archived by the owner on Jul 10, 2025. 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
9 changes: 9 additions & 0 deletions src/__test__/integration/builtins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../internal/builtins';
import { ModuleConfig } from '../../internal/moduleConfig';
import { createConnectedClient } from '../util';
import {checkConnection} from "../../api";

const dev2multiaddr = '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb';
const dev3multiaddr = '/dns4/dev.fluence.dev/tcp/19004/wss/p2p/12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB';
Expand Down Expand Up @@ -43,6 +44,14 @@ describe('Builtins usage suite', () => {
expect(bpList).not.toBeUndefined;
});

it('check_connection', async function () {
const client = await createConnectedClient(dev2multiaddr);

let isConnected = await checkConnection(client);

expect(isConnected).toEqual(true);
});

it('upload_modules', async function () {
const client = await createConnectedClient(dev2multiaddr);

Expand Down
35 changes: 12 additions & 23 deletions src/__test__/integration/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { encode } from 'bs58';
import { generatePeerId, peerIdToSeed, seedToPeerId } from '../../internal/peerIdUtils';
import { FluenceClientImpl } from '../../internal/FluenceClientImpl';
import { createConnectedClient } from '../util';
import {encode} from 'bs58';
import {generatePeerId, peerIdToSeed, seedToPeerId} from '../../internal/peerIdUtils';
import {FluenceClientImpl} from '../../internal/FluenceClientImpl';
import {createConnectedClient} from '../util';
import log from 'loglevel';
import { createClient } from '../../api';
import {createClient} from '../../api';
import Multiaddr from 'multiaddr';

const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
Expand Down Expand Up @@ -40,8 +40,7 @@ describe('Typescript usage suite', () => {

await client.sendScript(script, data);

const res = await resMakingPromise;
return res;
return await resMakingPromise;
};

it('address as string', async function () {
Expand Down Expand Up @@ -198,13 +197,8 @@ describe('Typescript usage suite', () => {

it('two clients should work inside the same time browser', async function () {
// arrange
const pid1 = await generatePeerId();
const client1 = new FluenceClientImpl(pid1);
await client1.connect(devNodeAddress);

const pid2 = await generatePeerId();
const client2 = new FluenceClientImpl(pid2);
await client2.connect(devNodeAddress);
const client1 = await createConnectedClient(devNodeAddress);
const client2 = await createConnectedClient(devNodeAddress);

let resMakingPromise = new Promise((resolve) => {
client2.registerCallback('test', 'test', (args, _) => {
Expand All @@ -216,7 +210,7 @@ describe('Typescript usage suite', () => {
let script = `
(seq
(call "${client1.relayPeerId}" ("op" "identity") [])
(call "${pid2.toB58String()}" ("test" "test") [a b c d])
(call "${client2.selfPeerId}" ("test" "test") [a b c d])
)
`;

Expand All @@ -234,13 +228,8 @@ describe('Typescript usage suite', () => {

it('event registration should work', async function () {
// arrange
const pid1 = await generatePeerId();
const client1 = new FluenceClientImpl(pid1);
await client1.connect(devNodeAddress);

const pid2 = await generatePeerId();
const client2 = new FluenceClientImpl(pid2);
await client2.connect(devNodeAddress);
const client1 = await createConnectedClient(devNodeAddress);
const client2 = await createConnectedClient(devNodeAddress);

client2.registerEvent('event_stream', 'test');
const resMakingPromise = new Promise((resolve) => {
Expand All @@ -249,7 +238,7 @@ describe('Typescript usage suite', () => {

// act
let script = `
(call "${pid2.toB58String()}" ("event_stream" "test") [hello])
(call "${client2.selfPeerId}" ("event_stream" "test") [hello])
`;

let data: Map<string, any> = new Map();
Expand Down
4 changes: 2 additions & 2 deletions src/__test__/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generatePeerId } from '..';
import { FluenceClientImpl } from '../internal/FluenceClientImpl';
import {generatePeerId} from '..';
import {FluenceClientImpl} from '../internal/FluenceClientImpl';

export const createLocalClient = async () => {
const peerId = await generatePeerId();
Expand Down
42 changes: 41 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Multiaddr from 'multiaddr';
import PeerId, { isPeerId } from 'peer-id';
import { generatePeerId, seedToPeerId } from './internal/peerIdUtils';
import { FluenceClientImpl } from './internal/FluenceClientImpl';
import log from "loglevel";

type Node = {
peerId: string;
Expand Down Expand Up @@ -44,6 +45,9 @@ export const createClient = async (
}

await client.connect(theAddress);
if (!await checkConnection(client)) {
throw new Error("Connection check failed. Check if the node is working or try to connect to another node")
}
}

return client;
Expand Down Expand Up @@ -147,7 +151,43 @@ export const sendParticleAsFetch = async <T>(
}, particle.ttl);
});

sendParticle(client, particle);
await sendParticle(client, particle);

return promise;
};

export const checkConnection = async (client: FluenceClient): Promise<boolean> => {
let msg = Math.random().toString(36).substring(7);
let callbackFn = "checkConnection"
let callbackService = "_callback"

const particle = new Particle(
`
(seq
(call __relay ("op" "identity") [msg] result)
(call myPeerId ("${callbackService}" "${callbackFn}") [result])
)
`,
{
__relay: client.relayPeerId,
myPeerId: client.selfPeerId,
msg
},
3000
);

if (!client.isConnected) {
return false;
}

try {
let result = await sendParticleAsFetch<string[][]>(client, particle, callbackFn, callbackService)
if (result[0][0] != msg) {
log.warn("unexpected behavior. 'identity' must return arguments the passed arguments.")
}
return true;
} catch (e) {
log.error("Error on establishing connection: ", e)
return false;
}
}