Skip to content
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
2 changes: 1 addition & 1 deletion packages/gossipsub/.aegir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('aegir').PartialOptions} */
export default {
build: {
bundlesizeMax: '85KB'
bundlesizeMax: '62KB'
}
}
4 changes: 2 additions & 2 deletions packages/gossipsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"release": "aegir release --no-types",
"build": "aegir build",
"generate": "protons ./src/message/rpc.proto",
Expand Down Expand Up @@ -75,7 +76,7 @@
"@libp2p/interface": "^3.1.0",
"@libp2p/interface-internal": "^3.0.9",
"@libp2p/peer-id": "^6.0.4",
"@libp2p/pubsub": "^10.0.0",
"@libp2p/utils": "^7.0.9",
"@multiformats/multiaddr": "^13.0.1",
"denque": "^2.1.0",
"it-length-prefixed": "^10.0.1",
Expand All @@ -90,7 +91,6 @@
"@chainsafe/as-sha256": "^1.2.0",
"@dapplion/benchmark": "^1.0.0",
"@libp2p/floodsub": "^11.0.10",
"@libp2p/interface-compliance-tests": "^7.0.10",
"@libp2p/logger": "^6.2.2",
"@libp2p/peer-store": "^12.0.9",
"@types/node": "^22.18.1",
Expand Down
18 changes: 17 additions & 1 deletion packages/gossipsub/src/utils/msgIdFn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { msgId } from '@libp2p/pubsub/utils'
import { publicKeyToProtobuf } from '@libp2p/crypto/keys'
import { sha256 } from 'multiformats/hashes/sha2'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import type { Message } from '../index.js'
import type { PublicKey } from '@libp2p/interface'

/**
* Generate a message id, based on the `key` and `seqno`
*/
export const msgId = (key: PublicKey, seqno: bigint): Uint8Array => {
const seqnoBytes = uint8ArrayFromString(seqno.toString(16).padStart(16, '0'), 'base16')
const keyBytes = publicKeyToProtobuf(key)

const msgId = new Uint8Array(keyBytes.byteLength + seqnoBytes.length)
msgId.set(keyBytes, 0)
msgId.set(seqnoBytes, keyBytes.byteLength)

return msgId
}

/**
* Generate a message id, based on the `key` and `seqno`
Expand Down
9 changes: 7 additions & 2 deletions packages/gossipsub/test/message-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as utils from '@libp2p/pubsub/utils'
import { randomBytes } from '@libp2p/crypto'
import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { MessageCache } from '../src/message-cache.js'
import { messageIdToString } from '../src/utils/messageIdToString.js'
import { getMsgId } from './utils/index.js'
import type { RPC } from '../src/message/rpc.js'
import type { MessageId } from '../src/types.js'

function randomSeqno (): bigint {
return BigInt(`0x${uint8ArrayToString(randomBytes(8), 'base16')}`)
}

const toMessageId = (msgId: Uint8Array): MessageId => {
return {
msgId,
Expand All @@ -28,7 +33,7 @@ describe('Testing Message Cache Operations', () => {
return {
from: new Uint8Array(0),
data: uint8ArrayFromString(n.toString()),
seqno: uint8ArrayFromString(utils.randomSeqno().toString(16).padStart(16, '0'), 'base16'),
seqno: uint8ArrayFromString(randomSeqno().toString(16).padStart(16, '0'), 'base16'),
topic
}
}
Expand Down