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

Network tests & updates #692

Merged
merged 17 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hostlist: make random an option.
  • Loading branch information
nodech committed Feb 15, 2022
commit 2a07d3c044e84e7a3b120d833cc1b1153b8b5ce9
19 changes: 13 additions & 6 deletions lib/net/hostlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class HostList {
this.address = this.options.address;
this.brontide = this.options.brontide;
this.resolve = this.options.resolve;
this.random = this.options.random;
pinheadmz marked this conversation as resolved.
Show resolved Hide resolved

this.key = rng.randomBytes(32);
this.hash = new Hash256();
Expand Down Expand Up @@ -412,7 +413,7 @@ class HostList {
buckets = this.fresh;

if (this.totalUsed > 0) {
if (this.totalFresh === 0 || random(2) === 0)
if (this.totalFresh === 0 || this.random(2) === 0)
buckets = this.used;
}

Expand All @@ -424,13 +425,13 @@ class HostList {
let factor = 1;

for (;;) {
const i = random(buckets.length);
const i = this.random(buckets.length);
const bucket = buckets[i];

if (bucket.size === 0)
continue;

let index = random(bucket.size);
let index = this.random(bucket.size);
let entry;

if (buckets === this.used) {
Expand All @@ -445,7 +446,7 @@ class HostList {
}
}

const num = random(1 << 30);
const num = this.random(1 << 30);

if (num < factor * entry.chance(now) * (1 << 30))
return entry;
Expand Down Expand Up @@ -586,7 +587,7 @@ class HostList {
for (let i = 0; i < entry.refCount; i++)
factor *= 2;

if (random(factor) !== 0)
if (this.random(factor) !== 0)
nodech marked this conversation as resolved.
Show resolved Hide resolved
return false;
} else {
if (!src)
Expand Down Expand Up @@ -862,7 +863,7 @@ class HostList {
items.push(entry);

for (let i = 0; i < items.length && out.length < 2500; i++) {
const j = random(items.length - i);
const j = this.random(items.length - i);

[items[i], items[i + j]] = [items[i + j], items[i]];

Expand Down Expand Up @@ -1672,6 +1673,7 @@ class HostListOptions {
this.onion = false;
this.brontideOnly = false;
this.banTime = common.BAN_TIME;
this.random = random;

this.address = new NetAddress();
this.address.services = this.services;
Expand Down Expand Up @@ -1804,6 +1806,11 @@ class HostListOptions {
this.flushInterval = options.flushInterval;
}

if (options.random != null) {
assert(typeof options.random === 'function');
this.random = options.random;;
}

this.address.time = this.network.now();
this.address.services = this.services;

Expand Down
Loading