Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support FastMsgIdFn returning a number #355

Merged
merged 1 commit into from
Oct 4, 2022
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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
): Promise<ReceivedMessageResult> {
// Fast message ID stuff
const fastMsgIdStr = this.fastMsgIdFn?.(rpcMsg)
const msgIdCached = fastMsgIdStr ? this.fastMsgIdCache?.get(fastMsgIdStr) : undefined
const msgIdCached = fastMsgIdStr !== undefined ? this.fastMsgIdCache?.get(fastMsgIdStr) : undefined

if (msgIdCached) {
// This message has been seen previously. Ignore it
Expand Down Expand Up @@ -1122,7 +1122,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
const messageId = { msgId, msgIdStr }

// Add the message to the duplicate caches
if (fastMsgIdStr) this.fastMsgIdCache?.put(fastMsgIdStr, msgIdStr)
if (fastMsgIdStr !== undefined) this.fastMsgIdCache?.put(fastMsgIdStr, msgIdStr)

if (this.seenCache.has(msgIdStr)) {
return { code: MessageStatus.duplicate, msgIdStr }
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface AddrInfo {
* Compute a local non-spec'ed msg-id for faster de-duplication of seen messages.
* Used exclusively for a local seen_cache
*/
export type FastMsgIdFn = (msg: RPC.IMessage) => string
export type FastMsgIdFn = (msg: RPC.IMessage) => string | number

/**
* By default, gossipsub only provide a browser friendly function to convert Uint8Array message id to string.
Expand Down
6 changes: 3 additions & 3 deletions src/utils/time-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type CacheValue<T> = {
* This gives 4x - 5x performance gain compared to npm TimeCache
*/
export class SimpleTimeCache<T> {
private readonly entries = new Map<string, CacheValue<T>>()
private readonly entries = new Map<string | number, CacheValue<T>>()
private readonly validityMs: number

constructor(opts: SimpleTimeCacheOpts) {
Expand All @@ -27,7 +27,7 @@ export class SimpleTimeCache<T> {
return this.entries.size
}

put(key: string, value: T): void {
put(key: string | number, value: T): void {
this.entries.set(key, { value, validUntilMs: Date.now() + this.validityMs })
}

Expand All @@ -48,7 +48,7 @@ export class SimpleTimeCache<T> {
return this.entries.has(key)
}

get(key: string): T | undefined {
get(key: string | number): T | undefined {
const value = this.entries.get(key)
return value && value.validUntilMs >= Date.now() ? value.value : undefined
}
Expand Down