Skip to content

Commit

Permalink
Merge branch 'refactor/event-listener-diff-logs' into 'dev'
Browse files Browse the repository at this point in the history
refactor(utils): show diff log for update events in `addEventListeners`

Closes #72

See merge request ergo/rosen-bridge/rosenet!31
  • Loading branch information
vorujack committed Aug 19, 2024
2 parents 99720a9 + 9f18eaa commit 0282f68
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-eagles-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosenet-utils': minor
---

Show only a diff log for update events in `addEventListeners`
61 changes: 61 additions & 0 deletions package-lock.json

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

36 changes: 28 additions & 8 deletions packages/utils/lib/addEventListeners.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Libp2p } from 'libp2p';
import { diffString, DiffStringOptions } from 'json-diff';
import { AbstractLogger } from '@rosen-bridge/logger-interface';

const jsonDiffOptions: DiffStringOptions = {
color: false,
sort: true,
maxElisions: 0,
};

/**
* Log different events of a RoseNet node or relay
*
Expand Down Expand Up @@ -56,38 +63,51 @@ const addEventListeners = (
});
});
node.addEventListener('peer:update', ({ detail: peerUpdate }) => {
logger.debug('Peer updated', {
id: peerUpdate.peer.id.toString(),
previous: {
const diff = diffString(
{
multiAddresses: peerUpdate.previous?.addresses.map((address) =>
address.multiaddr.toString(),
),
protocols: peerUpdate.previous?.protocols,
},
current: {
{
multiAddresses: peerUpdate.peer.addresses.map((address) =>
address.multiaddr.toString(),
),
protocols: peerUpdate.peer.protocols,
},
jsonDiffOptions,
);

if (!diff) return;

logger.debug('Peer updated', {
id: peerUpdate.peer.id.toString(),
});
logger.debug(diff);
});

node.addEventListener('self:peer:update', ({ detail: peerUpdate }) => {
logger.debug('Our own peer updated', {
previous: {
const diff = diffString(
{
multiAddresses: peerUpdate.previous?.addresses.map((address) =>
address.multiaddr.toString(),
),
protocols: peerUpdate.previous?.protocols,
},
current: {
{
multiAddresses: peerUpdate.peer.addresses.map((address) =>
address.multiaddr.toString(),
),
protocols: peerUpdate.peer.protocols,
},
});
jsonDiffOptions,
);

if (!diff) return;

logger.debug('Our own peer updated');
logger.debug(diff);
});

node.addEventListener('start', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@multiformats/multiaddr": "^12.2.1",
"@types/json-diff": "^1.0.3",
"@types/node": "^20.11.9",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
Expand All @@ -38,6 +39,7 @@
"@libp2p/crypto": "^4.0.2",
"@libp2p/peer-id-factory": "^4.0.6",
"@rosen-bridge/logger-interface": "^0.2.0",
"json-diff": "^1.0.6",
"libp2p": "^1.2.3",
"multiformats": "^13.1.0"
}
Expand Down

0 comments on commit 0282f68

Please sign in to comment.