Skip to content

Commit

Permalink
Merge branch 'feat/pub-sub' into 'dev'
Browse files Browse the repository at this point in the history
feat(rosenet-node): add pubsub methods

Closes #65

See merge request ergo/rosen-bridge/rosenet!27
  • Loading branch information
vorujack committed May 13, 2024
2 parents f094d2c + a158352 commit 0d8cb79
Show file tree
Hide file tree
Showing 18 changed files with 793 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/neat-hotels-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rosen-bridge/rosenet-relay': minor
'@rosen-bridge/rosenet-node': minor
---

add pubsub apis
2 changes: 1 addition & 1 deletion .github/workflows/scaling-test-docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
role: [node, relay]
test-name: ['', peer-discovery]
test-name: ['', peer-discovery, pubsub]

name: Build and Push Scaling Test Docker Image
permissions: write-all
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ dist
tests/*/.terraform
tests/*/*.tfstate*
tests/*/terraform.tfvars

.vscode/settings.json
24 changes: 24 additions & 0 deletions package-lock.json

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

32 changes: 29 additions & 3 deletions packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ const createRoseNetNode = async ({
tcp(),
],
addresses: {
listen: [`/ip4/0.0.0.0/tcp/${port}`],
announce: [announceMultiaddr],
listen: [
`/ip4/0.0.0.0/tcp/${port}`,
...config.relayMultiaddrs.map(
(multiaddr) => `${multiaddr}/p2p-circuit`,
),
],
announce: [
announceMultiaddr,
...config.relayMultiaddrs.map(
(multiaddr) => `${multiaddr}/p2p-circuit`,
),
],
},
connectionEncryption: [noise()],
connectionGater: {
Expand All @@ -97,7 +107,10 @@ const createRoseNetNode = async ({
},
services: {
identify: identify(),
pubsub: gossipsub({ allowPublishToZeroPeers: true }),
pubsub: gossipsub({
allowPublishToZeroPeers: true,
runOnTransientConnection: true,
}),
},
});
RoseNetNodeContext.logger.debug('RoseNet node created');
Expand Down Expand Up @@ -151,6 +164,19 @@ const createRoseNetNode = async ({
`handler for ${ROSENET_DIRECT_PROTOCOL_V1} protocol set`,
);
},
publish: async (topic: string, message: string) => {
const textEncoder = new TextEncoder();
node.services.pubsub.publish(topic, textEncoder.encode(message));
},
subscribe: async (topic: string, handler: (message: string) => void) => {
node.services.pubsub.subscribe(topic);
node.services.pubsub.addEventListener('message', (event) => {
if (event.detail.topic === topic) {
const textDecoder = new TextDecoder();
handler(textDecoder.decode(event.detail.data));
}
});
},
};
};

Expand Down
5 changes: 4 additions & 1 deletion packages/rosenet-relay/lib/createRoseNetRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ const createRoseNetRelay = async ({
applyDefaultLimit: false,
},
}),
pubsub: gossipsub({ allowPublishToZeroPeers: true }),
pubsub: gossipsub({
allowPublishToZeroPeers: true,
runOnTransientConnection: true,
}),
identify: identify(),
},
peerDiscovery: [pubsubPeerDiscovery()],
Expand Down
1 change: 1 addition & 0 deletions tests/scaling-pubsub/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
24 changes: 24 additions & 0 deletions tests/scaling-pubsub/.terraform.lock.hcl

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

23 changes: 23 additions & 0 deletions tests/scaling-pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RoseNet scaling pubsub test

## Table of contents

- [Introduction](#introduction)
- [Usage](#usage)

## Introduction

RoseNet scaling pubsub test

## Usage

You need to have [OpenTofu](https://opentofu.org/) installed. Then, after
[passing required variables](https://opentofu.org/docs/language/values/variables/#assigning-values-to-root-module-variables) (defined in [`variables.tf`](./variables.tf)),
run the following commands. It should complete with no errors.

```bash
tofu init
tofu apply
```

Finally run `tofu destroy` to destroy the infrastructure.
Loading

0 comments on commit 0d8cb79

Please sign in to comment.