Skip to content

Commit

Permalink
Merge pull request #84 from ChainSafe/cayman/disable-enr-update
Browse files Browse the repository at this point in the history
disable enr update
  • Loading branch information
wemeetagain committed Aug 7, 2020
2 parents ef3cab2 + 0244773 commit 62eaa1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export type IDiscv5Config = ISessionConfig &
* defined in milliseconds
*/
pingInterval: number;
/**
* Whether to enable enr auto-updating
*/
enrUpdate: boolean;
};

export const defaultConfig: IDiscv5Config = {
Expand All @@ -18,4 +22,5 @@ export const defaultConfig: IDiscv5Config = {
lookupParallelism: 3,
lookupNumResults: 16,
pingInterval: 300 * 1000,
enrUpdate: true,
};
24 changes: 14 additions & 10 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,20 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
if (!this.retrieveRequest(srcId, message)) {
return;
}
this.addrVotes.addVote(
srcId,
Multiaddr(`/${isIp.v4(message.recipientIp) ? "ip4" : "ip6"}/${message.recipientIp}/udp/${message.recipientPort}`)
);
const currentAddr = this.enr.multiaddrUDP;
const votedAddr = this.addrVotes.best(currentAddr);
if ((currentAddr && votedAddr && !votedAddr.equals(currentAddr)) || (!currentAddr && votedAddr)) {
log("Local ENR (IP & UDP) updated: %s", votedAddr);
this.enr.multiaddrUDP = votedAddr;
this.emit("multiaddrUpdated", votedAddr);
if (this.config.enrUpdate) {
this.addrVotes.addVote(
srcId,
Multiaddr(
`/${isIp.v4(message.recipientIp) ? "ip4" : "ip6"}/${message.recipientIp}/udp/${message.recipientPort}`
)
);
const currentAddr = this.enr.multiaddrUDP;
const votedAddr = this.addrVotes.best(currentAddr);
if ((currentAddr && votedAddr && !votedAddr.equals(currentAddr)) || (!currentAddr && votedAddr)) {
log("Local ENR (IP & UDP) updated: %s", votedAddr);
this.enr.multiaddrUDP = votedAddr;
this.emit("multiaddrUpdated", votedAddr);
}
}

// Check if we need to request a new ENR
Expand Down

0 comments on commit 62eaa1b

Please sign in to comment.