Skip to content

Commit

Permalink
feat: Add fallback bootstrap peers for mainnet (#1222)
Browse files Browse the repository at this point in the history
* feat: Add fallback bootstrap peers for mainnet

* changeset

* tests
  • Loading branch information
adityapk00 authored Aug 7, 2023
1 parent 65a4faf commit 40c17c9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-foxes-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

feat: Add fallback bootstrap peers for mainnet
6 changes: 6 additions & 0 deletions apps/hubble/src/bootstrapPeers.mainnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// List of bootstrap peers for mainnet
export const MAINNET_BOOTSTRAP_PEERS = [
"/dns/hoyt.farcaster.xyz/tcp/2282",
"/dns/lamia.farcaster.xyz/tcp/2282",
"/dns/nemes.farcaster.xyz/tcp/2282",
];
38 changes: 27 additions & 11 deletions apps/hubble/src/hubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ import {
getPublicIp,
ipFamilyToString,
p2pMultiAddrStr,
parseAddress,
} from "./utils/p2p.js";
import { PeriodicTestDataJobScheduler, TestUser } from "./utils/periodicTestDataJob.js";
import { ensureAboveMinFarcasterVersion, VersionSchedule } from "./utils/versions.js";
import { CheckFarcasterVersionJobScheduler } from "./storage/jobs/checkFarcasterVersionJob.js";
import { ValidateOrRevokeMessagesJobScheduler } from "./storage/jobs/validateOrRevokeMessagesJob.js";
import { GossipContactInfoJobScheduler } from "./storage/jobs/gossipContactInfoJob.js";
import { MAINNET_ALLOWED_PEERS } from "./allowedPeers.mainnet.js";
import { MAINNET_BOOTSTRAP_PEERS } from "./bootstrapPeers.mainnet.js";
import StoreEventHandler from "./storage/stores/storeEventHandler.js";
import { FNameRegistryClient, FNameRegistryEventsProvider } from "./eth/fnameRegistryEventsProvider.js";
import { L2EventsProvider, OptimismConstants } from "./eth/l2EventsProvider.js";
Expand Down Expand Up @@ -442,11 +444,8 @@ export class Hub implements HubInterface {
// Append and de-dup the allowed peers
this.allowedPeerIds = [...new Set([...(this.allowedPeerIds ?? []), ...MAINNET_ALLOWED_PEERS])];
}

this.deniedPeerIds = this.options.deniedPeers ?? [];
if (this.options.network === FarcasterNetwork.MAINNET) {
// Append and de-dup the denied peers
this.deniedPeerIds = [...new Set([...(this.deniedPeerIds ?? [])])];
}
}

get rpcAddress() {
Expand Down Expand Up @@ -574,7 +573,25 @@ export class Hub implements HubInterface {
this.updateNameRegistryEventExpiryJobWorker.start();
}

await this.gossipNode.start(this.options.bootstrapAddrs ?? [], {
let bootstrapAddrs = this.options.bootstrapAddrs ?? [];
// Add mainnet bootstrap addresses if none are provided
if (bootstrapAddrs.length === 0 && this.options.network === FarcasterNetwork.MAINNET) {
bootstrapAddrs = MAINNET_BOOTSTRAP_PEERS.map((a) => parseAddress(a))
.map((r) => {
if (r.isErr()) {
logger.warn(
{ errorCode: r.error.errCode, message: r.error.message },
"Couldn't parse bootstrap address from MAINNET_BOOTSTRAP_PEERS, ignoring",
);
}
return r;
})
.filter((a) => a.isOk())
.map((a) => a._unsafeUnwrap());
}

// Start the Gossip node
await this.gossipNode.start(bootstrapAddrs, {
peerId: this.options.peerId,
ipMultiAddr: this.options.ipMultiAddr,
announceIp: this.options.announceIp,
Expand Down Expand Up @@ -1186,10 +1203,9 @@ export class Hub implements HubInterface {
return ResultAsync.fromPromise(this.rocksDB.commit(txn), (e) => e as HubError);
}

async isValidPeer(ourPeerId: PeerId, message: ContactInfoContent) {
const peerId = ourPeerId.toString();
if (this.allowedPeerIds?.length && !this.allowedPeerIds.includes(peerId)) {
log.warn(`Peer ${ourPeerId.toString()} is not in the allowed peers list`);
async isValidPeer(otherPeerId: PeerId, message: ContactInfoContent) {
if (!this.gossipNode.isPeerAllowed(otherPeerId)) {
log.warn(`Peer ${otherPeerId.toString()} is not in allowlist or is in the denylist`);
return false;
}

Expand All @@ -1199,15 +1215,15 @@ export class Hub implements HubInterface {
const versionCheckResult = ensureAboveMinFarcasterVersion(theirVersion);
if (versionCheckResult.isErr()) {
log.warn(
{ peerId: ourPeerId, theirVersion, ourVersion: FARCASTER_VERSION, errMsg: versionCheckResult.error.message },
{ peerId: otherPeerId, theirVersion, ourVersion: FARCASTER_VERSION, errMsg: versionCheckResult.error.message },
"Peer is running an outdated version, ignoring",
);
return false;
}

if (theirNetwork !== this.options.network) {
log.warn(
{ peerId: ourPeerId, theirNetwork, ourNetwork: this.options.network },
{ peerId: otherPeerId, theirNetwork, ourNetwork: this.options.network },
"Peer is running a different network, ignoring",
);
return false;
Expand Down
10 changes: 10 additions & 0 deletions apps/hubble/src/network/p2p/gossipNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ export class GossipNode extends TypedEmitter<NodeEvents> {
return this._node?.peerStore.get(peerId);
}

async isPeerAllowed(peerId: PeerId) {
if (this._connectionGater) {
return await this._connectionGater.filterMultiaddrForPeer(peerId);
} else {
return true;
}
}

/** Returns the GossipSub instance used by the Node */
get gossip() {
const pubsub = this._node?.pubsub;
Expand Down Expand Up @@ -429,6 +437,8 @@ export class GossipNode extends TypedEmitter<NodeEvents> {

/* Attempts to dial all the addresses in the bootstrap list */
public async bootstrap(bootstrapAddrs: Multiaddr[]): Promise<HubResult<void>> {
log.info({ bootstrapAddrs }, "Bootstrapping Gossip Node");

if (bootstrapAddrs.length === 0) return ok(undefined);
const results = await Promise.all(bootstrapAddrs.map((addr) => this.connectAddress(addr)));

Expand Down

0 comments on commit 40c17c9

Please sign in to comment.