Skip to content

Commit

Permalink
feat: remove pow and created_at limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Sep 28, 2024
1 parent 0aa27d5 commit 5e8c4fa
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 173 deletions.
69 changes: 0 additions & 69 deletions packages/common/__test__/utils/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
EventType,
EventUtils,
TagName,
countPowDifficulty,
getTimestampInSeconds,
schnorrSign,
sha256,
Expand Down Expand Up @@ -45,54 +44,6 @@ describe('EventUtils', () => {
.mockReturnValueOnce(getTimestampInSeconds() - 1);
expect(EventUtils.validate(validEvent)).toBe('reject: event is expired');

expect(
EventUtils.validate(
createEvent({ created_at: getTimestampInSeconds() + 200 }),
{ createdAtUpperLimit: 100 },
),
).toBe(
'invalid: created_at must not be later than 100 seconds from the current time',
);
expect(
EventUtils.validate(
createEvent({ created_at: getTimestampInSeconds() - 200 }),
{ createdAtLowerLimit: 100 },
),
).toBe(
'invalid: created_at must not be earlier than 100 seconds from the current time',
);

const validEventPowDifficulty = countPowDifficulty(validEvent.id);
expect(
EventUtils.validate(validEvent, {
minPowDifficulty: 64,
}),
).toBe(`pow: difficulty ${validEventPowDifficulty} is less than 64`);

expect(
EventUtils.validate(createEvent({ minPowDifficulty: 4 }), {
minPowDifficulty: 4,
}),
).toBeUndefined();

expect(
EventUtils.validate(
createEvent({ minPowDifficulty: 8, targetPowDifficulty: 4 }),
{
minPowDifficulty: 8,
},
),
).toBe('pow: difficulty 4 is less than 8');

expect(
EventUtils.validate(
createEvent({ minPowDifficulty: 4, targetPowDifficulty: 4 }),
{
minPowDifficulty: 4,
},
),
).toBeUndefined();

jest.spyOn(EventUtils, 'isDelegationEventValid').mockReturnValueOnce(false);
expect(EventUtils.validate(validEvent)).toBe(
'invalid: delegation tag verification failed',
Expand Down Expand Up @@ -522,23 +473,10 @@ export function createEvent(
created_at?: number;
tags?: string[][];
content?: string;
minPowDifficulty?: number;
targetPowDifficulty?: number;
} = {},
): Event {
const tags = params.tags ?? [];
let nonce = 0;
if (params.minPowDifficulty) {
tags.push(
params.targetPowDifficulty
? [
TagName.NONCE,
nonce.toString(),
params.targetPowDifficulty.toString(),
]
: [TagName.NONCE, nonce.toString()],
);
}

const baseEvent = {
pubkey: 'a09659cd9ee89cd3743bc29aa67edf1d7d12fb624699fcd3d6d33eef250b01e7',
Expand All @@ -549,13 +487,6 @@ export function createEvent(
};

let id = getEventHash(baseEvent);
if (params.minPowDifficulty) {
while (countPowDifficulty(id) < params.minPowDifficulty) {
baseEvent.tags.find(tag => tag[0] === TagName.NONCE)![1] =
(++nonce).toString();
id = getEventHash(baseEvent);
}
}
const sig = signEvent(
id,
'3689c9acc44041d38a44d0cb777e30f51f295a5e5565b4edb661e8f24eece569',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export type NostrRelayOptions = {
* The minimum log level to log. `Default: LogLevel.INFO`
*/
logLevel?: LogLevel;
createdAtUpperLimit?: number;
createdAtLowerLimit?: number;
/**
* Allowed minimum PoW difficulty for events.` Default: 0`
*/
minPowDifficulty?: number;
/**
* Maximum number of subscriptions per client. `Default: 20`
*/
Expand Down
45 changes: 1 addition & 44 deletions packages/common/src/utils/event.util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EventKind, EventType, TagName } from '../constants';
import { Event, Filter, Tag } from '../interfaces';
import { schnorrVerify, sha256 } from './crypto.util';
import { countPowDifficulty } from './proof-of-work.util';
import { isNil } from './shared.util';
import { getTimestampInSeconds } from './time.util';

export class EventUtils {
Expand Down Expand Up @@ -33,14 +31,7 @@ export class EventUtils {
return EventType.REGULAR;
}

static validate(
event: Event,
options: {
createdAtUpperLimit?: number;
createdAtLowerLimit?: number;
minPowDifficulty?: number;
} = {},
): string | undefined {
static validate(event: Event): string | undefined {
if (!EventUtils.isIdValid(event)) {
return 'invalid: id is wrong';
}
Expand All @@ -56,40 +47,6 @@ export class EventUtils {
return 'reject: event is expired';
}

if (
!isNil(options.createdAtUpperLimit) &&
event.created_at - now > options.createdAtUpperLimit
) {
return `invalid: created_at must not be later than ${options.createdAtUpperLimit} seconds from the current time`;
}

if (
!isNil(options.createdAtLowerLimit) &&
now - event.created_at > options.createdAtLowerLimit
) {
return `invalid: created_at must not be earlier than ${options.createdAtLowerLimit} seconds from the current time`;
}

if (options.minPowDifficulty && options.minPowDifficulty > 0) {
const pow = countPowDifficulty(event.id);
if (pow < options.minPowDifficulty) {
return `pow: difficulty ${pow} is less than ${options.minPowDifficulty}`;
}

const nonceTag = event.tags.find(
tag => tag[0] === TagName.NONCE && tag.length === 3,
);
if (!nonceTag) {
// could not reject an event without a committed target difficulty
return;
}

const targetPow = parseInt(nonceTag[2]);
if (isNaN(targetPow) || targetPow < options.minPowDifficulty) {
return `pow: difficulty ${targetPow} is less than ${options.minPowDifficulty}`;
}
}

if (!EventUtils.isDelegationEventValid(event)) {
return 'invalid: delegation tag verification failed';
}
Expand Down
2 changes: 0 additions & 2 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * from './crypto.util';
export * from './event.util';
export * from './filter.util';
export * from './proof-of-work.util';
export * from './response.util';
export * from './rxjs.util';
export * from './shared.util';
export * from './time.util';
30 changes: 0 additions & 30 deletions packages/common/src/utils/proof-of-work.util.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/common/src/utils/shared.util.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/core/src/nostr-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ export class NostrRelay {
this.pluginManagerService,
logger,
{
createdAtUpperLimit: options.createdAtUpperLimit,
createdAtLowerLimit: options.createdAtLowerLimit,
minPowDifficulty: options.minPowDifficulty,
filterResultCacheTtl: options.filterResultCacheTtl,
},
);
Expand Down
15 changes: 1 addition & 14 deletions packages/core/src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import { PluginManagerService } from './plugin-manager.service';
import { SubscriptionService } from './subscription.service';

type EventServiceOptions = {
createdAtUpperLimit?: number;
createdAtLowerLimit?: number;
minPowDifficulty?: number;
filterResultCacheTtl?: number;
};

Expand All @@ -28,9 +25,6 @@ export class EventService {
private readonly findLazyCache?:
| LazyCache<string, Observable<Event> | Event[]>
| undefined;
private readonly createdAtUpperLimit: number | undefined;
private readonly createdAtLowerLimit: number | undefined;
private readonly minPowDifficulty: number | undefined;

constructor(
eventRepository: EventRepository,
Expand All @@ -43,9 +37,6 @@ export class EventService {
this.subscriptionService = subscriptionService;
this.pluginManagerService = pluginManagerService;
this.logger = logger;
this.createdAtUpperLimit = options.createdAtUpperLimit;
this.createdAtLowerLimit = options.createdAtLowerLimit;
this.minPowDifficulty = options.minPowDifficulty;

const filterResultCacheTtl = options.filterResultCacheTtl ?? 1000;
if (filterResultCacheTtl > 0) {
Expand Down Expand Up @@ -84,11 +75,7 @@ export class EventService {
};
}

const validateErrorMsg = EventUtils.validate(event, {
createdAtUpperLimit: this.createdAtUpperLimit,
createdAtLowerLimit: this.createdAtLowerLimit,
minPowDifficulty: this.minPowDifficulty,
});
const validateErrorMsg = EventUtils.validate(event);
if (validateErrorMsg) {
return {
success: false,
Expand Down

0 comments on commit 5e8c4fa

Please sign in to comment.