-
Notifications
You must be signed in to change notification settings - Fork 33
/
rpc_peer_as_peer.js
135 lines (115 loc) · 4.03 KB
/
rpc_peer_as_peer.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const {featureFlagDetails} = require('bolt09');
const {syncTypes} = require('./constants');
const {ceil} = Math;
const date = n => new Date(Number(BigInt(n) / BigInt(1e6))).toISOString();
const isActiveSync = n => [syncTypes.active, syncTypes.pinned].includes(n);
const isBool = n => n === false || n === true;
const isNumber = n => !isNaN(n);
const isString = n => typeof n === 'string';
const isZero = n => n === '0';
const {keys} = Object;
const microPerMilli = 1e3;
const minTime = n => n < 1 ? 0 : n;
/** Translate an RPC peer into a peer
{
address: <Network Address String>
bytes_recv: <Number of Bytes Received String>
bytes_sent: <Number of Bytes Send String>
features: {
<Feature Bit Number String>: {
is_known: <Feature is Known Bool>
is_required: <Feature is Required Bool>
}
}
flap_count: <Reconnection Count Number>
inbound: <Peer Is Inbound Connection Bool>
last_flap_ns: <Last Reconnection Time in Epoch Nanoseconds String>
ping_time: <Peer Ping Time String>
pub_key: <Peer Public Key Hex String>
sat_recv: <Peer Tokens Received String>
sat_sent: <Peer Tokens Sent String>
sync_type: <Peer Sync Type String>
}
@throws
<Error>
@returns
{
bytes_received: <Bytes Received Number>
bytes_sent: <Bytes Sent Number>
features: [{
bit: <BOLT 09 Feature Bit Number>
is_known: <Feature is Known Bool>
is_required: <Feature Support is Required Bool>
type: <Feature Type String>
}]
is_inbound: <Is Inbound Peer Bool>
[is_sync_peer]: <Is Syncing Graph Data Bool>
[last_reconnection]: <Peer Last Reconnected At ISO 8601 Date String>
ping_time: <Milliseconds Number>
public_key: <Public Key String>
[reconnection_rate]: <Count of Reconnections Over Time Number>
socket: <Network Address And Port String>
tokens_received: <Amount Received Tokens Number>
tokens_sent: <Amount Sent Tokens Number>
}
*/
module.exports = peer => {
if (!peer) {
throw new Error('ExpectedRpcPeerToDerivePeerDetails');
}
if (!isString(peer.address)) {
throw new Error('ExpectedPeerAddressInRpcPeer');
}
if (!isString(peer.bytes_recv)) {
throw new Error('ExpectedPeerBytesReceivedInRpcPeer');
}
if (!isString(peer.bytes_sent)) {
throw new Error('ExpectedPeerBytesSentInRpcPeer');
}
if (!peer.features) {
throw new Error('ExpectedPeerFeaturesInRpcPeer');
}
if (!isNumber(peer.flap_count)) {
throw new Error('ExpectedPeerFlapCounterInRpcPeer');
}
if (!isBool(peer.inbound)) {
throw new Error('ExpectedPeerInboundStatusInRpcPeer');
}
if (!isString(peer.last_flap_ns)) {
throw new Error('ExpectedPeerLastFlapTimeInRpcPeer');
}
if (!isString(peer.ping_time)) {
throw new Error('ExpectedPeerPingTimeInRpcPeer');
}
if (!isString(peer.pub_key)) {
throw new Error('ExpectedPeerPublicKeyInRpcPeer');
}
if (!isString(peer.sat_recv)) {
throw new Error('ExpectedReceiveAmountInRpcPeer');
}
if (!isString(peer.sat_sent)) {
throw new Error('ExpectedSentAmountInRpcPeer');
}
const isPassiveSync = peer.sync_type === syncTypes.passive;
const lastReconnected = isZero(peer.last_flap_ns) ? null : peer.last_flap_ns;
const isKnownSyncType = isActiveSync(peer.sync_type) || isPassiveSync;
return {
bytes_received: Number(peer.bytes_recv),
bytes_sent: Number(peer.bytes_sent),
features: keys(peer.features).map(bit => ({
bit: Number(bit),
is_known: peer.features[bit].is_known,
is_required: peer.features[bit].is_required,
type: featureFlagDetails({bit}).type,
})),
is_inbound: peer.inbound,
is_sync_peer: isKnownSyncType ? isActiveSync(peer.sync_type) : undefined,
last_reconnection: !!lastReconnected ? date(lastReconnected) : undefined,
ping_time: minTime(ceil(Number(peer.ping_time) / microPerMilli)),
public_key: peer.pub_key,
reconnection_rate: !!peer.flap_count ? peer.flap_count : undefined,
socket: peer.address,
tokens_received: Number(peer.sat_recv),
tokens_sent: Number(peer.sat_sent),
};
};