Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: use multihash bytes for provide message keys (#405)
Browse files Browse the repository at this point in the history
This fixes a bug where `ADD_PROVIDER` message keys were CID bytes instead of multihash bytes, and changes the CIDs generated in tests to be V1s with `raw` codec instead of V0s so that the difference is actually tested.

Fixes #381
  • Loading branch information
joeltg authored Dec 7, 2022
1 parent 9815e8e commit d7e7b5d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/content-routing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ContentRouting {
// Add peer as provider
await this.providers.addProvider(key, this.components.peerId)

const msg = new Message(MESSAGE_TYPE.ADD_PROVIDER, key.bytes, 0)
const msg = new Message(MESSAGE_TYPE.ADD_PROVIDER, key.multihash.bytes, 0)
msg.providerPeers = [{
id: this.components.peerId,
multiaddrs,
Expand Down
2 changes: 1 addition & 1 deletion test/kad-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ describe('KadDHT', () => {

// Expect an ADD_PROVIDER message to be sent to each peer for each value
const fn = dhts[3].lan.network.sendMessage
const valuesBuffs = values.map(v => v.cid.bytes)
const valuesBuffs = values.map(v => v.cid.multihash.bytes)
// @ts-expect-error fn is a spy
const calls = fn.getCalls().map(c => c.args)

Expand Down
4 changes: 3 additions & 1 deletion test/utils/create-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'

import { sha256 } from 'multiformats/hashes/sha2'
import { randomBytes } from '@libp2p/crypto'

Expand All @@ -13,7 +15,7 @@ export async function createValues (length: number): Promise<Value[]> {
const bytes = randomBytes(32)
const h = await sha256.digest(bytes)
return {
cid: CID.createV0(h),
cid: CID.createV1(raw.code, h),
value: bytes
}
})
Expand Down

0 comments on commit d7e7b5d

Please sign in to comment.