Skip to content

Commit

Permalink
chore: auto handshake on connect (#17)
Browse files Browse the repository at this point in the history
# Description

- What does this PR do?
Specify dependencies versions and handshake new peer connections

## Checklist:
- [x] I have read the [contributing
guidelines](https://github.com/LimeChain/Fruzhin/blob/dev/CONTRIBUTING.md).
- [x] My PR title matches the [Conventional Commits
spec](https://www.conventionalcommits.org/).
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
  • Loading branch information
ablax authored Sep 11, 2024
1 parent a82181a commit 1ec9c92
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ public static String getHandshake() {

@JSBody(params = {"announceExport", "protocolId"}, script =
"window.fruzhin.libp.handle(protocolId, async ({connection, stream}) => {" +
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 69) {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" announceExport.blockAnnounce(Ed25519.b2h(subarr.slice(2)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
" });")
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 69) {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" announceExport.blockAnnounce(Ed25519.b2h(subarr.slice(2)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
"});" +
"fruzhin.libp.addEventListener('peer:connect', async (evt) => {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(await window.fruzhin.libp.dialProtocol(evt.detail, protocolId))).writeLP(Ed25519.h2b(handshake));" +
"});")
public static native void registerHandler(JSObject announceExport, String protocolId);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,26 @@ public static String getNeighbourMessage() {

@JSBody(params = {"grandpaExport", "protocolId"}, script =
"window.fruzhin.libp.handle(protocolId, async ({connection, stream}) => {" +
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" + " let subarr = msg.subarray();" +
" if(subarr.length === 1) {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" if(subarr.slice(1)[0] === 2) {" +
" let niehgbourMessage = grandpaExport.getNeighbourMessage();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(niehgbourMessage));" +
" }" +
" grandpaExport.handleMessage(Ed25519.b2h(subarr.slice(1)), connection.remotePeer.toString());" +
" }" + " }" + " });" + " });")
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 1) {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" if(subarr.slice(1)[0] === 2) {" +
" let niehgbourMessage = grandpaExport.getNeighbourMessage();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(niehgbourMessage));" +
" }" +
" grandpaExport.handleMessage(Ed25519.b2h(subarr.slice(1)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
"});" +
"fruzhin.libp.addEventListener('peer:connect', async (evt) => {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(await window.fruzhin.libp.dialProtocol(evt.detail, protocolId))).writeLP(Ed25519.h2b(handshake));" +
"});")
public static native void registerHandler(JSObject grandpaExport, String protocolId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,6 @@ public GrandpaService(String protocolId) {
this.protocolId = protocolId;
}

// /**
// * Sends a neighbour message to a peer. If there is no initiator stream opened with the peer,
// * sends a handshake instead.
// *
// * @param us our host object
// * @param peerId message receiver
// */
// public void sendNeighbourMessage(Host us, PeerId peerId) {
// Optional.ofNullable(connectionManager.getPeerInfo(peerId))
// .map(p -> p.getGrandpaStreams().getInitiator())
// .ifPresentOrElse(
// this::sendNeighbourMessage,
// () -> sendHandshake(us, peerId)
// );
// }
//
// private void sendNeighbourMessage(Stream stream) {
// GrandpaController controller = new GrandpaController(stream);
// controller.sendNeighbourMessage();
// }

private boolean isRegistered = false;

public void sendHandshake() {
Expand Down
26 changes: 13 additions & 13 deletions src/main/webapp/js/fruzhin-lib.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-noise/dist/index.min.js';
import 'https://unpkg.com/@libp2p/websockets/dist/index.min.js';
import 'https://unpkg.com/@libp2p/kad-dht/dist/index.min.js';
import 'https://unpkg.com/@libp2p/identify/dist/index.min.js';
import 'https://unpkg.com/@libp2p/bootstrap/dist/index.min.js';
import 'https://unpkg.com/@libp2p/ping/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-gossipsub/dist/index.min.js';
import 'https://unpkg.com/libp2p/dist/index.min.js';
import 'https://unpkg.com/it-pipe/dist/index.min.js';
import 'https://unpkg.com/it-pb-stream/dist/index.min.js';
import 'https://unpkg.com/@muradsenteca/ed25519/dist/index.min.js'
import 'https://unpkg.com/@muradsenteca/blake2b/dist/index.min.js'
import 'https://unpkg.com/@chainsafe/libp2p-yamux@6.0.2/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-noise@15.1.2/dist/index.min.js';
import 'https://unpkg.com/@libp2p/websockets@8.2.0/dist/index.min.js';
import 'https://unpkg.com/@libp2p/kad-dht@12.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/identify@2.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/bootstrap@10.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/ping@1.1.6/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-gossipsub@13.2.0/dist/index.min.js';
import 'https://unpkg.com/libp2p@1.9.4/dist/index.min.js';
import 'https://unpkg.com/it-pipe@3.0.1/dist/index.min.js';
import 'https://unpkg.com/it-pb-stream@4.0.2/dist/index.min.js';
import 'https://unpkg.com/@muradsenteca/ed25519@1.0.0/dist/index.min.js'
import 'https://unpkg.com/@muradsenteca/blake2b@1.0.1/dist/index.min.js'

import * as HTTP from './http.js';
import * as Fruzhin from './fruzhin.js'
Expand Down

0 comments on commit 1ec9c92

Please sign in to comment.