Skip to content

Commit

Permalink
net: Fix connectedGroup filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Mar 30, 2022
1 parent 5fc3122 commit 09f9133
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 18 additions & 11 deletions lib/net/netaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ class NetAddress extends bio.Struct {
}

/**
* Get the canonical identifier of our network group
* @returns {Buffer}
*/
* Get the canonical identifier of our network group
* @returns {Buffer}
*/

getGroup() {
return groupKey(this.raw);
return groupKey(this);
}

/**
Expand Down Expand Up @@ -542,7 +542,14 @@ NetAddress.DEFAULT_SERVICES = 0
* Helpers
*/

function groupKey(raw) {
/**
* @param {NetAddress} addr
* @returns {Number}
*/

function groupKey(addr) {
const raw = addr.raw;

// See: https://github.com/bitcoin/bitcoin/blob/e258ce7/src/netaddress.cpp#L413
// Todo: Use IP->ASN mapping, see:
// https://github.com/bitcoin/bitcoin/blob/adea5e1/src/addrman.h#L274
Expand All @@ -551,25 +558,25 @@ function groupKey(raw) {
let bits = 16;
let i = 0;

if (IP.isLocal(raw)) {
if (addr.isLocal()) {
type = 255; // NET_LOCAL
bits = 0;
} else if (!IP.isRoutable(raw)) {
} else if (!addr.isRoutable()) {
type = IP.networks.NONE; // NET_UNROUTABLE
bits = 0;
} else if (IP.isIPv4(raw) || IP.isRFC6145(raw) || IP.isRFC6052(raw)) {
} else if (addr.isIPv4() || addr.isRFC6145() || addr.isRFC6052()) {
type = IP.networks.INET4; // NET_IPV4
start = 12;
} else if (IP.isRFC3964(raw)) {
} else if (addr.isRFC3964()) {
type = IP.networks.INET4; // NET_IPV4
start = 2;
} else if (IP.isRFC4380(raw)) {
} else if (addr.isRFC4380()) {
const buf = Buffer.alloc(3);
buf[0] = IP.networks.INET4; // NET_IPV4
buf[1] = raw[12] ^ 0xff;
buf[2] = raw[13] ^ 0xff;
return buf;
} else if (IP.isOnion(raw)) {
} else if (addr.isOnion()) {
type = IP.networks.ONION; // NET_ONION
start = 6;
bits = 4;
Expand Down
3 changes: 2 additions & 1 deletion lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const InvItem = require('../primitives/invitem');
const packets = require('./packets');
const consensus = require('../protocol/consensus');
const NameState = require('../covenants/namestate');
const NetAddress = require('./netaddress');
const services = common.services;
const invTypes = InvItem.types;
const packetTypes = packets.types;
Expand Down Expand Up @@ -84,7 +85,7 @@ class Pool extends EventEmitter {
this.pendingFilter = null;
this.refillTimer = null;
this.discoverTimer = null;
this.connectedGroups = new Set();
this.connectedGroups = new BufferSet();

this.checkpoints = false;
this.headerChain = new List();
Expand Down

0 comments on commit 09f9133

Please sign in to comment.