forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathban-list.ts
34 lines (27 loc) · 992 Bytes
/
ban-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import debugDefault from 'debug'
import { LRUCache } from 'lru-cache'
import { formatLogId } from '../util.js'
import { KBucket } from './kbucket.js'
import type { PeerInfo } from '../types.js'
const debug = debugDefault('devp2p:dpt:ban-list')
const verbose = debugDefault('verbose').enabled
export class BanList {
private _lru: LRUCache<string, boolean>
private DEBUG: boolean
constructor() {
this._lru = new LRUCache({ max: 10000 })
this.DEBUG =
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false
}
add(obj: string | Uint8Array | PeerInfo, maxAge?: number) {
for (const key of KBucket.getKeys(obj)) {
this._lru.set(key, true, { ttl: maxAge })
if (this.DEBUG) {
debug(`Added peer ${formatLogId(key, verbose)}, size: ${this._lru.size}`)
}
}
}
has(obj: string | Uint8Array | PeerInfo): boolean {
return KBucket.getKeys(obj).some((key: string) => Boolean(this._lru.get(key)))
}
}