Skip to content

Commit

Permalink
Queue incoming events. Add log package.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 10, 2024
1 parent 0efb6c0 commit 32b7d5e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 20 deletions.
21 changes: 21 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ export * as nostrToolsRelay from "npm:nostr-tools/relay";
export * as nostrTools from "npm:nostr-tools";
export * as cliffy from "https://deno.land/x/[email protected]/mod.ts";
export * as nostrify from "jsr:@nostrify/nostrify@^0.30.0";
export * as queue from "jsr:@henrygd/[email protected]";
import { newQueue as newQueueImport } from "jsr:@henrygd/[email protected]";
export const newQueue = newQueueImport;
// import * as logImport from "jsr:@std/[email protected]";
// export const log = logImport.getLogger("nostroots-server");
export * as logPackage from "jsr:@std/[email protected]";
21 changes: 21 additions & 0 deletions log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logPackage } from "./deps.ts";

logPackage.setup({
handlers: {
console: new logPackage.ConsoleHandler("DEBUG", {
formatter: (record) =>
`${record.datetime.toISOString()} [${record.levelName}] ${
record.msg
} ${JSON.stringify(record.args)}`,
useColors: true,
}),
},
loggers: {
default: {
level: "DEBUG",
handlers: ["console"],
},
},
});

export const log = logPackage.getLogger();
5 changes: 3 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cliffy, nostrTools } from "./deps.ts";
import { log } from "./log.ts";
import { repost } from "./validation/repost.ts";

function getOrCreatePrivateKey(maybePrivateKeyNsec?: string) {
Expand All @@ -12,7 +13,7 @@ function getOrCreatePrivateKey(maybePrivateKeyNsec?: string) {

const key = nostrTools.generateSecretKey();
const nsec = nostrTools.nip19.nsecEncode(key);
console.log(`#2yrJza Using random nsec ${nsec}`);
log.info(`#2yrJza Using random nsec ${nsec}`);
return key;
}

Expand All @@ -31,7 +32,7 @@ await new cliffy.Command()
const privateKey = getOrCreatePrivateKey(options.privateKeyNsec);
const maxAgeMinutes = options.maxAgeMinutes;

console.log(`#PnFUPS Startup isDev ${isDev}`);
log.debug(`#PnFUPS Startup isDev ${isDev}`);

repost(privateKey, isDev, maxAgeMinutes);
})
Expand Down
39 changes: 30 additions & 9 deletions validation/repost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from "../common/constants.ts";
import { DEV_PUBKEY } from "../common/constants.ts";
import { validateEvent } from "./validate.ts";
import { newQueue } from "../deps.ts";
import { nostrTools } from "../deps.ts";
import { log } from "../log.ts";

async function getRelayPool(isDev: true | undefined) {
const relays = isDev ? DEV_RELAYS : DEFAULT_RELAYS;
Expand Down Expand Up @@ -39,9 +42,9 @@ async function publishEvent(
relayPool: nostrify.NPool,
event: nostrify.NostrEvent
) {
console.log("Publishing event…");
log.debug("#aSmTVL Publishing event…");
await relayPool.event(event);
console.log("Event published.");
log.info("#p26tur Event published.");
}

/**
Expand Down Expand Up @@ -102,11 +105,32 @@ function createFilter(
return [baseFilter];
}

function processEventFactoryFactory(
relayPool: nostrify.NPool,
privateKey: Uint8Array
) {
return function processEventFactory(event: nostrify.NostrEvent) {
return async function () {
log.debug(`#C1NJbQ Got event`, event);

const isEventValid = await validateEvent(relayPool, event);
if (!isEventValid) {
log.info(`#u0Prc5 Discarding invalid event ${event.id}`);
return;
}
const repostedEvent = await generateRepostedEvent(event, privateKey);
publishEvent(relayPool, repostedEvent);
};
};
}

export async function repost(
privateKey: Uint8Array,
isDev: true | undefined,
maxAgeMinutes: number | undefined
) {
log.debug(`#BmseJH Startup`);

const relayPool = await getRelayPool(isDev);

const filter = createFilter(isDev, maxAgeMinutes);
Expand All @@ -115,16 +139,13 @@ export async function repost(
const signal = controller.signal;
const subscription = relayPool.req(filter, { signal });

const queue = newQueue(3);
const processEventFactory = processEventFactoryFactory(relayPool, privateKey);

for await (const msg of subscription) {
if (msg[0] === "EVENT") {
const event = msg[2];
const isEventValid = await validateEvent(relayPool, event);
if (!isEventValid) {
console.info(`Discarding event…`);
return;
}
const repostedEvent = await generateRepostedEvent(event, privateKey);
publishEvent(relayPool, repostedEvent);
queue.add(processEventFactory(event));
} else if (msg[0] === "EOSE") {
if (isDev) {
globalThis.setTimeout(() => {
Expand Down
17 changes: 9 additions & 8 deletions validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { MINIMUM_TRUSTROOTS_USERNAME_LENGTH } from "../common/constants.ts";
import { MAP_NOTE_KIND } from "../common/constants.ts";
import { nostrify, nostrTools } from "../deps.ts";
import { log } from "../log.ts";
import { Profile } from "../types.ts";

async function getKindZeroEvent(relayPool: nostrify.NPool, pubKey: string) {
Expand All @@ -30,7 +31,7 @@ async function getKindZeroEvent(relayPool: nostrify.NPool, pubKey: string) {
}

function getProfileFromEvent(event: nostrTools.Event): Profile | undefined {
console.log("kindZeroEvent", event);
log.debug("#GHg51j kindZeroEvent", event);
try {
const profile = JSON.parse(event.content);

Expand Down Expand Up @@ -79,7 +80,7 @@ async function getNip5PubKey(
*/
export async function validateEvent(
relayPool: nostrify.NPool,
event: nostrTools.Event
event: nostrify.NostrEvent
) {
if (event.kind !== MAP_NOTE_KIND) {
return false;
Expand All @@ -93,33 +94,33 @@ export async function validateEvent(
const kindZeroEvent = await getKindZeroEvent(relayPool, event.pubkey);

if (typeof kindZeroEvent === "undefined") {
console.log("#Kmf59M Skipping event with no kind zero event", { event });
log.debug("#Kmf59M Skipping event with no kind zero event", { event });
return false;
}

const profile = getProfileFromEvent(kindZeroEvent);

if (typeof profile === "undefined") {
console.log("#pd4X7C Skipping event with invalid profile", { event });
log.debug("#pd4X7C Skipping event with invalid profile", { event });
return false;
}

const { trustrootsUsername } = profile;

console.log(`Checking username ${trustrootsUsername}`);
log.debug(`#yUtER5 Checking username ${trustrootsUsername}`);

const nip5PubKey = await getNip5PubKey(trustrootsUsername);

if (typeof nip5PubKey !== "string") {
console.log("#b0gWmE Failed to get string nip5 pubkey", { event });
log.debug("#b0gWmE Failed to get string nip5 pubkey", { event });
return false;
}

if (event.pubkey !== nip5PubKey) {
console.log("#dtKr5H Event failed nip5 validation", { event });
log.debug("#dtKr5H Event failed nip5 validation", { event });
return false;
}

console.log("#lpglLu Event passed validation", event);
log.debug("#lpglLu Event passed validation", event);
return true;
}

0 comments on commit 32b7d5e

Please sign in to comment.