Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/little-cycles-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ensindexer": minor
---

now explicitly strips null bytes from resolver text values rather than relying on ponder's default behavior
23 changes: 15 additions & 8 deletions apps/ensindexer/src/handlers/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Address, Hash, type Hex, hexToBytes } from "viem";
import { makeSharedEventValues, upsertAccount, upsertResolver } from "@/lib/db-helpers";
import { decodeTXTData, parseRRSet } from "@/lib/dns-helpers";
import { makeResolverId } from "@/lib/ids";
import { hasNullByte, uniq } from "@/lib/lib-helpers";
import { hasNullByte, stripNullBytes, uniq } from "@/lib/lib-helpers";
import type { EventWithArgs } from "@/lib/ponder-helpers";
import { decodeDNSPacketBytes } from "@ensnode/utils/subname-helpers";

Expand Down Expand Up @@ -187,16 +187,21 @@ export const makeResolverHandlers = ({ pluginName }: { pluginName: PluginName })
.update(schema.resolver, { id })
.set({ texts: uniq([...(resolver.texts ?? []), key]) });

// NOTE: ponder's (viem's) event parsing produces empty string for some TextChanged events
// (which is correct) but the subgraph records null for these instances, so we coalesce
// falsy strings to null for compatibility
// ex: https://etherscan.io/tx/0x7fac4f1802c9b1969311be0412e6f900d531c59155421ff8ce1fda78b87956d0#eventlog
//
// NOTE: we also must strip null bytes in strings, which are unindexable by Postgres
// ex: https://etherscan.io/tx/0x2eb93d872a8f3e4295ea50773c3816dcaea2541f202f650948e8d6efdcbf4599#eventlog
const sanitizedValue = !value ? null : stripNullBytes(value) || null;

// log ResolverEvent
await context.db.insert(schema.textChanged).values({
...sharedEventValues(context.network.chainId, event),
resolverId: id,
key,
// ponder's (viem's) event parsing produces empty string for some TextChanged events
// (which is correct) but the subgraph records null for these instances, so we coalesce
// falsy strings to null for compatibility
// ex: last TextChanged in tx 0x7fac4f1802c9b1969311be0412e6f900d531c59155421ff8ce1fda78b87956d0
value: value || null,
value: sanitizedValue,
});
},

Expand Down Expand Up @@ -374,6 +379,9 @@ export const makeResolverHandlers = ({ pluginName }: { pluginName: PluginName })
// https://github.com/mafintosh/dns-packet
const value = decodeTXTData(answer.data as Buffer[]);

// note: sanitize value, see `handleTextChanged` for context
const sanitizedValue = !value ? null : stripNullBytes(value) || null;

// upsert new key
await context.db
.update(schema.resolver, { id })
Expand All @@ -384,8 +392,7 @@ export const makeResolverHandlers = ({ pluginName }: { pluginName: PluginName })
...sharedEventValues(context.network.chainId, event),
resolverId: id,
key,
// note: coalesce empty string to null, see `handleTextChanged` for context
value: value || null,
value: sanitizedValue,
});
break;
}
Expand Down
4 changes: 3 additions & 1 deletion apps/ensindexer/src/lib/lib-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const uniq = <T>(arr: T[]): T[] => [...new Set(arr)];

export const bigintMax = (...args: bigint[]): bigint => args.reduce((a, b) => (a > b ? a : b));

export const hasNullByte = (value: string) => value.indexOf("\u0000") !== -1;

export const bigintMax = (...args: bigint[]): bigint => args.reduce((a, b) => (a > b ? a : b));
export const stripNullBytes = (value: string) => value.replaceAll("\u0000", "");
16 changes: 12 additions & 4 deletions apps/ensindexer/test/lib-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bigintMax, hasNullByte, uniq } from "@/lib/lib-helpers";
import { bigintMax, hasNullByte, stripNullBytes, uniq } from "@/lib/lib-helpers";
import { describe, expect, it } from "vitest";

describe("helpers", () => {
Expand All @@ -8,6 +8,12 @@ describe("helpers", () => {
});
});

describe("bigintMax", () => {
it("should return the maximum bigint value", () => {
expect(bigintMax(1n, 2n, 3n)).toBe(3n);
});
});

describe("hasNullByte", () => {
it("should return true if the string contains a null byte", () => {
expect(hasNullByte("hello\u0000world")).toBe(true);
Expand All @@ -18,9 +24,11 @@ describe("helpers", () => {
});
});

describe("bigintMax", () => {
it("should return the maximum bigint value", () => {
expect(bigintMax(1n, 2n, 3n)).toBe(3n);
describe("stripNullBytes", () => {
it("should remove null bytes", () => {
expect(stripNullBytes("hello\u0000world")).toBe("helloworld");
expect(stripNullBytes("\0")).toBe("");
expect(stripNullBytes("x\0y\0z\0")).toBe("xyz");
});
});
});