-
Notifications
You must be signed in to change notification settings - Fork 795
/
Copy pathprotocol.ts
110 lines (96 loc) · 3.16 KB
/
protocol.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import debugDefault from 'debug'
import { EventEmitter } from 'events'
import { DISCONNECT_REASON, ProtocolType } from '../types.js'
import { devp2pDebug } from '../util.js'
import type { Peer } from '../rlpx/peer.js'
import type { SendMethod } from '../types.js'
import type { Debugger } from 'debug'
type MessageCodes = { [key: number | string]: number | string }
export abstract class Protocol {
public events: EventEmitter
protected _version: number
protected _peer: Peer
protected _send: SendMethod
protected _statusTimeoutId?: NodeJS.Timeout
protected _messageCodes: MessageCodes
private _debug: Debugger
protected _verbose: boolean
/**
* Will be set to the first successfully connected peer to allow for
* debugging with the `devp2p:FIRST_PEER` debugger
*/
_firstPeer = ''
// Message debuggers (e.g. { 'GET_BLOCK_HEADERS': [debug Object], ...})
protected msgDebuggers: { [key: string]: (debug: string) => void } = {}
constructor(
peer: Peer,
send: SendMethod,
protocol: ProtocolType,
version: number,
messageCodes: MessageCodes
) {
this.events = new EventEmitter()
this._peer = peer
this._send = send
this._version = version
this._messageCodes = messageCodes
this._statusTimeoutId =
protocol !== ProtocolType.SNAP
? setTimeout(() => {
this._peer.disconnect(DISCONNECT_REASON.TIMEOUT)
}, 5000) // 5 sec * 1000
: undefined
this._debug = devp2pDebug.extend(protocol)
this._verbose = debugDefault('verbose').enabled
this.initMsgDebuggers(protocol)
}
private initMsgDebuggers(protocol: ProtocolType) {
const MESSAGE_NAMES = Object.values(this._messageCodes).filter(
(value) => typeof value === 'string'
) as string[]
for (const name of MESSAGE_NAMES) {
this.msgDebuggers[name] = devp2pDebug.extend(protocol).extend(name)
}
// Remote Peer IP logger
const ip = this._peer['_socket'].remoteAddress
if (typeof ip === 'string') {
this.msgDebuggers[ip] = devp2pDebug.extend(ip)
}
}
/**
* Called once on the peer where a first successful `STATUS`
* msg exchange could be achieved.
*
* Can be used together with the `devp2p:FIRST_PEER` debugger.
*/
_addFirstPeerDebugger() {
const ip = this._peer['_socket'].remoteAddress
if (typeof ip === 'string') {
this.msgDebuggers[ip] = devp2pDebug.extend('FIRST_PEER')
this._peer._addFirstPeerDebugger()
this._firstPeer = ip
}
}
/**
* Debug message both on the generic as well as the
* per-message debug logger
* @param messageName Capitalized message name (e.g. `GET_BLOCK_HEADERS`)
* @param msg Message text to debug
*/
protected debug(messageName: string, msg: string) {
this._debug(msg)
if (this.msgDebuggers[messageName] !== undefined) {
this.msgDebuggers[messageName](msg)
}
const ip = this._peer['_socket'].remoteAddress
if (typeof ip === 'string' && this.msgDebuggers[ip] !== undefined) {
this.msgDebuggers[ip](msg)
}
}
/**
* Abstract method to handle incoming messages
* @param code
* @param data
*/
abstract _handleMessage(code: number, data: Uint8Array): void
}