Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

feat: connectedMultiaddr #49

Merged
merged 6 commits into from
Mar 30, 2017
Merged

feat: connectedMultiaddr #49

merged 6 commits into from
Mar 30, 2017

Conversation

daviddias
Copy link
Member

No description provided.

src/index.js Outdated
constructor (peerId) {
assert(peerId, 'Missing peerId. Use Peer.create(cb) to create one')

this.id = peerId
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do this.peerId for consistency everywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, please not I need to type that so often, and it's already clear that this things i a peer, as it is a peerInfo

src/index.js Outdated
this._observedMultiaddrs = []
this._connectedMultiaddr = undefined

this.multiaddr = {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make all methods first class, new names welcome

src/index.js Outdated
this.multiaddr.add(addr)
observedMultiaddrs.splice(i, 1)
check = true
this.multiaddr.replace = (existing, fresh) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe break this into two functions "replace and merge"?

src/index.js Outdated
fresh.forEach((m) => {
this.multiaddr.add(m)
})
setConnectedMultiaddr (ma) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not super happy with this name

src/index.js Outdated
this.id = peerId
this.multiaddrs = new MultiaddrSet()
this.protocols = new Set()
this._connectedMultiaddr = undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we are ever only connect on one addr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise this could just be another multiaddrset

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there might be situations where we are dialing on several or in between a upgrade, but that is swarm magic, for the rest of js-ipfs, it is always just one.

return this._multiaddrs
}

get length () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am torn on this one, Set uses .size and we are calling this a set..so not sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. I actually used your gist as a template. size or lenght then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't be torn, we will solve it :)

}

toArray () {
return this._multiaddrs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's return a copy to avoid accidental changes, this._multiaddrs.slice()

add (ma) {
ma = ensureMultiaddr(ma)

if (!this.multiaddrHas(ma)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiaddrHas is not a function ;)

}

// to prevent multiaddr explosion due to identify
addSafe (ma) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this now that we already check for uniqueness on the regular add?

Copy link
Member Author

@daviddias daviddias Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiaddr explosion is when you dial to a bunch of nodes and every node gives you a different observed address and you start storing them all to share with other peers. This seems like a good idea until you realize that most of those addresses are unique to the subnet that peer is in and so, they are completely worthless for all the other peers. This method is exclusively used by identify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to add that explanation into here :)

}

// to prevent multiaddr explosion due to identify
addSafe (ma) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to add that explanation into here :)

const ensureMultiaddr = require('./utils').ensureMultiaddr

// Because JavaScript doesn't let you overload the compare in Set()..
class MultiaddrSet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I get some nice jsdoc comments while we are at it? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do that on the ✈️, already have enough 'fun' scheduled for today. Updated the readme with all methods listing though :)

this.multiaddr.add(ma)
this._observedMultiaddrs.splice(i, 1)
check = true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this never returns true, so you could just use forEach if you want to do it on all of them, otherwise just do const check = multiaddrs.some() and return true when matching.

README.md Outdated
- [`.multiaddrs.add(addr)`](#multiaddraddaddr)
- [`.multiaddrs.addSafe(addr)`](#multiaddraddsafeaddr)
- [`.multiaddrs.forEach(fn)`](#multiaddrforeachfn)
- [`.multaiddrs.size()`]((#multiaddrsize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size is a getter not a method


it('.disconnect', () => {
pi.disconnect('/ip4/127.0.0.1')
expect(pi.isConnected()).to.equal(false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing some tests for toArray, forEach, clear and has

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@daviddias daviddias merged commit 422d0fd into master Mar 30, 2017
@daviddias daviddias deleted the feat/advanced-peer-book branch March 30, 2017 11:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants