Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling toString on a peer id breaks deep comparison #141

Closed
achingbrain opened this issue Mar 22, 2021 · 2 comments · Fixed by #142
Closed

Calling toString on a peer id breaks deep comparison #141

achingbrain opened this issue Mar 22, 2021 · 2 comments · Fixed by #142

Comments

@achingbrain
Copy link
Member

const PeerId = require('peer-id')

const peerId1 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
const peerId2 = PeerId.createFromB58String('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')

assert.deepEqual(peerId1, peerId2)
// 'bafzbeifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'

peerId1.toString()

assert.deepEqual(peerId1, peerId2)
// Uncaught AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:

This is because .toString() has the side effect of setting a ._idCIDString property on this which the other peer ID doesn't have.

@vasco-santos
Copy link
Member

Users should not rely on deepEqual in my opinion, as if you create the same PeerId via privateKey, or B58String they are the same PeerId (per PeerId.equals), but the deep will fail.

Even though, we should address this problem. If this is not a major problem, we should probably wait until we move into using default format from RFC 0001 by default in libp2p

@achingbrain
Copy link
Member Author

achingbrain commented Mar 23, 2021

Users should not rely on deepEqual in my opinion

It's a pretty common test assertion which we can't use. It could be fixed my marking using Object.defineProperty to mark the property as non-enumerable?

achingbrain added a commit that referenced this issue Mar 26, 2021
Makes the cached string cid property non-enumerable so deep equals
still works even after a peer id has been stringifed.

Fixes #141
vasco-santos pushed a commit that referenced this issue Mar 29, 2021
Makes the cached string cid property non-enumerable so deep equals
still works even after a peer id has been stringifed.

Fixes #141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants