-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add ability to persist and load nodes #21
Comments
Something like the following: class DHT extends EventEmitter {
...
toJSON () {
return {
nodes: this.nodes.toArray(),
}
}
addNodes (nodes) {
for (const node of nodes) {
const { id , token , to } = node
const peer = {
host: node.host,
port: node.port,
}
this._addNode(id, peer, token, to)
}
}
} |
Yep! I wouldn't call it toJSON though as the ids are binary. Just toArray() or toNodesArray() is fine |
What about the |
Good idea stripping the private info. A map of that array to { id, port, ip, referrer: { ip, port, id } } should be enough. I'm soft -1 on stringify'ing the ids. I think that is the responsibility of whoever is serialising. Alternatively addNodes should deserialise them from hex if they are strings. |
What should be in the referrer ? class DHT extends EventEmitter {
...
getNodes () {
return this.nodes.toArray().map(
({id, host, port, roundtripToken, to}) => ({
id,
host,
port,
referrer: { ? }
})
)
}
addNodes (nodes) {
for (const node of nodes) {
const { id , token , to } = node
const peer = { host , port } = node
this._addNode(id, peer, token, to)
}
}
} |
@aureooms node.referrer is set on the node. not all nodes have this |
@mafintosh I see. |
What should we do with class DHT extends EventEmitter {
...
getNodes () {
return this.nodes.toArray().map(
({id, host, port, referrer }) => ({
id,
host,
port,
referrer: referrer === null ? null : {
id: referrer.id,
host: referrer.host,
port: referrer.port,
}
})
)
}
addNodes (nodes) {
for (const node of nodes) {
const { id , token , to } = node
const peer = { host , port } = node
this._addNode(id, peer, token, to)
}
}
} |
We don't care about token/to. Actually we don't persist the referrer in the nodes table, so disregard that for now. |
Final iteration? class DHT extends EventEmitter {
...
getNodes () {
return this.nodes.toArray().map(
({id, host, port, referrer }) => ({
id,
host,
port,
referrer: referrer === null ? null : {
id: referrer.id,
host: referrer.host,
port: referrer.port,
}
})
)
}
addNodes (nodes) {
for (const { id , host , port } of nodes) {
this._addNode(id, { host , port })
}
}
} |
just skip the referrer on export for now, other than that looks good to me. can you open a PR? |
Yes! |
PR at #22. I added some tests. Could not get rid of the |
Continuing holepunchto/hyperdht#4.
The text was updated successfully, but these errors were encountered: