Skip to content

Commit

Permalink
fix: Cache numFnames and numFids (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 committed Oct 12, 2023
1 parent ba86d37 commit b518b97
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-singers-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

fix: Cache numFids and numFnames
8 changes: 7 additions & 1 deletion apps/hubble/src/network/sync/syncEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ describe("SyncEngine", () => {
expect(await syncEngine.trie.exists(SyncId.fromOnChainEvent(signerEvent))).toBeTruthy();
expect(await syncEngine.trie.exists(SyncId.fromOnChainEvent(storageEvent))).toBeTruthy();
expect(await syncEngine.trie.exists(SyncId.fromFName(fnameProof))).toBeTruthy();
expect((await syncEngine.getDbStats()).numFids).toEqual(1);
expect((await syncEngine.getDbStats()).numFnames).toEqual(1);
});

test("trie is not updated on merge failure", async () => {
Expand Down Expand Up @@ -556,6 +558,7 @@ describe("SyncEngine", () => {
expect(await syncEngine.trie.exists(SyncId.fromFName(userNameProof))).toBeFalsy();
await engine.mergeUserNameProof(userNameProof);
expect(await syncEngine.trie.exists(SyncId.fromFName(userNameProof))).toBeTruthy();
expect((await syncEngine.getDbStats()).numFnames).toEqual(1);
});
test("removes deleted fname proofs", async () => {
const supercedingUserNameProof = Factories.UserNameProof.build({
Expand All @@ -565,15 +568,18 @@ describe("SyncEngine", () => {

expect(await syncEngine.trie.exists(SyncId.fromFName(userNameProof))).toBeFalsy();
expect(await syncEngine.trie.exists(SyncId.fromFName(supercedingUserNameProof))).toBeFalsy();
expect((await syncEngine.getDbStats()).numFnames).toEqual(0);

await engine.mergeUserNameProof(userNameProof);
expect(await syncEngine.trie.exists(SyncId.fromFName(userNameProof))).toBeTruthy();
await engine.mergeUserNameProof(supercedingUserNameProof);
expect((await syncEngine.getDbStats()).numFnames).toEqual(1);

await engine.mergeUserNameProof(supercedingUserNameProof);
await sleepWhile(() => syncEngine.syncTrieQSize > 0, SLEEPWHILE_TIMEOUT);

expect(await syncEngine.trie.exists(SyncId.fromFName(userNameProof))).toBeFalsy();
expect(await syncEngine.trie.exists(SyncId.fromFName(supercedingUserNameProof))).toBeTruthy();
expect((await syncEngine.getDbStats()).numFnames).toEqual(1);
});

test("adds sync ids to the trie when not present and egnine rejects as duplicate", async () => {
Expand Down
24 changes: 23 additions & 1 deletion apps/hubble/src/network/sync/syncEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { sleepWhile } from "../../utils/crypto.js";
import { statsd } from "../../utils/statsd.js";
import { logger, messageToLog } from "../../utils/logger.js";
import { OnChainEventPostfix, RootPrefix } from "../../storage/db/types.js";
import { bytesCompare, bytesStartsWith, fromFarcasterTime } from "@farcaster/core";
import { bytesCompare, bytesStartsWith, fromFarcasterTime, isIdRegisterOnChainEvent } from "@farcaster/core";
import { L2EventsProvider } from "../../eth/l2EventsProvider.js";
import { SyncEngineProfiler } from "./syncEngineProfiler.js";
import os from "os";
Expand Down Expand Up @@ -173,6 +173,12 @@ class SyncEngine extends TypedEmitter<SyncEvents> {
// Has the syncengine started yet?
private _started = false;

private _dbStats: DbStats = {
numItems: 0,
numFids: 0,
numFnames: 0,
};

constructor(
hub: HubInterface,
rocksDb: RocksDB,
Expand Down Expand Up @@ -215,6 +221,11 @@ class SyncEngine extends TypedEmitter<SyncEvents> {
statsd().gauge("merkle_trie.merge_q", this._syncTrieQ);
await this.addOnChainEvent(onChainEvent);
this._syncTrieQ -= 1;

// Keep track of total FIDs
if (isIdRegisterOnChainEvent(event.mergeOnChainEventBody.onChainEvent)) {
this._dbStats.numFids += 1;
}
});

// Note: There's no guarantee that the message is actually deleted, because the transaction could fail.
Expand Down Expand Up @@ -256,6 +267,8 @@ class SyncEngine extends TypedEmitter<SyncEvents> {
statsd().gauge("merkle_trie.merge_q", this._syncTrieQ);
await this.addFname(event.mergeUsernameProofBody.usernameProof);
this._syncTrieQ -= 1;

this._dbStats.numFnames += 1;
}
if (
event.mergeUsernameProofBody.deletedUsernameProof &&
Expand All @@ -265,6 +278,8 @@ class SyncEngine extends TypedEmitter<SyncEvents> {
statsd().gauge("merkle_trie.merge_q", this._syncTrieQ);
await this.removeFname(event.mergeUsernameProofBody.deletedUsernameProof);
this._syncTrieQ -= 1;

this._dbStats.numFnames -= 1;
}
});
this._hub.engine.on("duplicateUserNameProofEvent", async (event: UserNameProof) => {
Expand Down Expand Up @@ -310,6 +325,9 @@ class SyncEngine extends TypedEmitter<SyncEvents> {

const rootHash = await this._trie.rootHash();

// Read the initial DB stats
this._dbStats = await this.readDbStatsFromDb();

this._started = true;
log.info({ rootHash }, "Sync engine initialized (eventsSync: true)");
}
Expand Down Expand Up @@ -1257,6 +1275,10 @@ class SyncEngine extends TypedEmitter<SyncEvents> {
}

public async getDbStats(): Promise<DbStats> {
return { ...this._dbStats, numItems: await this._trie.items() };
}

private async readDbStatsFromDb(): Promise<DbStats> {
let numFids = 0;
let numFnames = 0;

Expand Down

0 comments on commit b518b97

Please sign in to comment.