trac-msb 0.1.39 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +4 -0
- package/src/network.js +150 -140
package/package.json
CHANGED
package/src/index.js
CHANGED
package/src/network.js
CHANGED
|
@@ -50,159 +50,169 @@ class Network {
|
|
|
50
50
|
|
|
51
51
|
console.log(`Channel: ${b4a.toString(channel)}`);
|
|
52
52
|
swarm.on('connection', async (connection) => {
|
|
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
|
-
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
101
|
-
if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
102
|
-
const nodeEntry = await msb.get(msg.value.pub);
|
|
103
|
-
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
104
|
-
const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
105
|
-
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
106
|
-
if (msg.key !== wallet.publicKey && canAddWriter) {
|
|
107
|
-
await handleIncomingEvent(msg);
|
|
108
|
-
}
|
|
109
|
-
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
110
|
-
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
111
|
-
if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
112
|
-
const nodeEntry = await msb.get(msg.value.pub);
|
|
113
|
-
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
114
|
-
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
115
|
-
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
116
|
-
await handleIncomingEvent(msg);
|
|
117
|
-
}
|
|
53
|
+
|
|
54
|
+
const mux = Protomux.from(connection)
|
|
55
|
+
connection.userData = mux
|
|
56
|
+
|
|
57
|
+
const message_channel = mux.createChannel({
|
|
58
|
+
protocol: b4a.toString(channel, 'utf8'),
|
|
59
|
+
onopen() {
|
|
60
|
+
},
|
|
61
|
+
onclose() {
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
message_channel.open()
|
|
65
|
+
const message = message_channel.addMessage({
|
|
66
|
+
encoding: c.json,
|
|
67
|
+
async onmessage(msg) {
|
|
68
|
+
try {
|
|
69
|
+
if (msg === 'get_validator') {
|
|
70
|
+
const nonce = Wallet.generateNonce().toString('hex');
|
|
71
|
+
const _msg = {
|
|
72
|
+
op: 'validator',
|
|
73
|
+
key: writingKey,
|
|
74
|
+
address: wallet.publicKey,
|
|
75
|
+
channel: b4a.toString(channel, 'utf8')
|
|
76
|
+
};
|
|
77
|
+
const sig = wallet.sign(JSON.stringify(_msg) + nonce);
|
|
78
|
+
message.send({response: _msg, sig, nonce})
|
|
79
|
+
} else if (msg === 'get_admin') {
|
|
80
|
+
const res = await msb.get(EntryType.ADMIN);
|
|
81
|
+
if (wallet.publicKey !== res.tracPublicKey) return;
|
|
82
|
+
const nonce = Wallet.generateNonce().toString('hex');
|
|
83
|
+
const _msg = {
|
|
84
|
+
op: 'admin',
|
|
85
|
+
key: writingKey,
|
|
86
|
+
address: wallet.publicKey,
|
|
87
|
+
channel: b4a.toString(channel, 'utf8')
|
|
88
|
+
};
|
|
89
|
+
const sig = wallet.sign(JSON.stringify(_msg) + nonce);
|
|
90
|
+
message.send({response: _msg, sig, nonce})
|
|
91
|
+
swarm.leavePeer(connection.remotePublicKey)
|
|
92
|
+
} else if (msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'validator') {
|
|
93
|
+
const res = await msb.get(msg.response.address);
|
|
94
|
+
if (res === null) return;
|
|
95
|
+
const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, msg.response.address)
|
|
96
|
+
if (verified && msg.response.channel === b4a.toString(channel, 'utf8')) {
|
|
97
|
+
console.log('Validator stream established', msg.response.address)
|
|
98
|
+
network.validator_stream = connection;
|
|
99
|
+
network.validator = msg.response.address;
|
|
118
100
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
101
|
+
} else if (msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'admin') {
|
|
102
|
+
const res = await msb.get(EntryType.ADMIN);
|
|
103
|
+
if (res === null || res.tracPublicKey !== msg.response.address) return;
|
|
104
|
+
const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, res.tracPublicKey)
|
|
105
|
+
if (verified && msg.response.channel === b4a.toString(channel, 'utf8')) {
|
|
106
|
+
console.log('Admin stream established', res.tracPublicKey)
|
|
107
|
+
network.admin_stream = connection;
|
|
108
|
+
network.admin = res.tracPublicKey;
|
|
109
|
+
}
|
|
110
|
+
swarm.leavePeer(connection.remotePublicKey)
|
|
111
|
+
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter') {
|
|
112
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
113
|
+
if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
114
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
115
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
116
|
+
const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
117
|
+
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
118
|
+
if (msg.key !== wallet.publicKey && canAddWriter) {
|
|
122
119
|
await handleIncomingEvent(msg);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
120
|
+
}
|
|
121
|
+
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
122
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
123
|
+
if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
124
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
125
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
126
|
+
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
127
|
+
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
128
|
+
await handleIncomingEvent(msg);
|
|
129
|
+
}
|
|
130
|
+
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addAdmin') {
|
|
131
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
132
|
+
if (null === adminEntry || (adminEntry.tracPublicKey !== msg.key)) return;
|
|
133
|
+
await handleIncomingEvent(msg);
|
|
134
|
+
} else {
|
|
135
|
+
if (base.isIndexer || !base.writable) return;
|
|
136
|
+
|
|
137
|
+
if (true !== disable_rate_limit) {
|
|
138
|
+
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
139
|
+
const _now = Date.now();
|
|
140
|
+
|
|
141
|
+
if (_now - clean >= 120_000) {
|
|
142
|
+
clean = _now;
|
|
143
|
+
conns = {};
|
|
144
|
+
}
|
|
138
145
|
|
|
139
|
-
|
|
140
|
-
conns[peer]
|
|
146
|
+
if (conns[peer] === undefined) {
|
|
147
|
+
conns[peer] = {prev: _now, now: 0, tx_cnt: 0}
|
|
148
|
+
}
|
|
141
149
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
150
|
+
conns[peer].now = _now;
|
|
151
|
+
conns[peer].tx_cnt += 1;
|
|
145
152
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
connection.end()
|
|
149
|
-
}
|
|
153
|
+
if (conns[peer].now - conns[peer].prev >= 60_000) {
|
|
154
|
+
delete conns[peer];
|
|
150
155
|
}
|
|
151
156
|
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
157
|
+
if (conns[peer] !== undefined && conns[peer].now - conns[peer].prev >= 1000 && conns[peer].tx_cnt >= 50) {
|
|
158
|
+
swarm.leavePeer(connection.remotePublicKey);
|
|
159
|
+
connection.end()
|
|
155
160
|
}
|
|
161
|
+
}
|
|
156
162
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (network.check.sanitizePreTx(parsedPreTx) &&
|
|
162
|
-
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
163
|
-
parsedPreTx.wp === wallet.publicKey &&
|
|
164
|
-
null === await base.view.get(parsedPreTx.tx)
|
|
165
|
-
) {
|
|
166
|
-
const nonce = Wallet.generateNonce().toString('hex');
|
|
167
|
-
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
168
|
-
const append_tx = {
|
|
169
|
-
op: OperationType.POST_TX,
|
|
170
|
-
tx: parsedPreTx.tx,
|
|
171
|
-
is: parsedPreTx.is,
|
|
172
|
-
w: writingKey,
|
|
173
|
-
i: parsedPreTx.i,
|
|
174
|
-
ipk: parsedPreTx.ipk,
|
|
175
|
-
ch: parsedPreTx.ch,
|
|
176
|
-
in: parsedPreTx.in,
|
|
177
|
-
bs: parsedPreTx.bs,
|
|
178
|
-
mbs: parsedPreTx.mbs,
|
|
179
|
-
ws: signature.toString('hex'),
|
|
180
|
-
wp: wallet.publicKey,
|
|
181
|
-
wn: nonce
|
|
182
|
-
};
|
|
183
|
-
network.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
|
|
184
|
-
}
|
|
163
|
+
if (network.tx_pool.length >= 1000) {
|
|
164
|
+
console.log('pool full');
|
|
165
|
+
return
|
|
185
166
|
}
|
|
186
|
-
} catch (e) {
|
|
187
|
-
console.log(e);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
})
|
|
191
167
|
|
|
192
|
-
|
|
168
|
+
if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
|
|
193
169
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
170
|
+
const parsedPreTx = msg;
|
|
171
|
+
|
|
172
|
+
if (network.check.sanitizePreTx(parsedPreTx) &&
|
|
173
|
+
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
174
|
+
parsedPreTx.wp === wallet.publicKey &&
|
|
175
|
+
null === await base.view.get(parsedPreTx.tx)
|
|
176
|
+
) {
|
|
177
|
+
const nonce = Wallet.generateNonce().toString('hex');
|
|
178
|
+
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
179
|
+
const append_tx = {
|
|
180
|
+
op: OperationType.POST_TX,
|
|
181
|
+
tx: parsedPreTx.tx,
|
|
182
|
+
is: parsedPreTx.is,
|
|
183
|
+
w: writingKey,
|
|
184
|
+
i: parsedPreTx.i,
|
|
185
|
+
ipk: parsedPreTx.ipk,
|
|
186
|
+
ch: parsedPreTx.ch,
|
|
187
|
+
in: parsedPreTx.in,
|
|
188
|
+
bs: parsedPreTx.bs,
|
|
189
|
+
mbs: parsedPreTx.mbs,
|
|
190
|
+
ws: signature.toString('hex'),
|
|
191
|
+
wp: wallet.publicKey,
|
|
192
|
+
wn: nonce
|
|
193
|
+
};
|
|
194
|
+
network.tx_pool.push({tx: parsedPreTx.tx, append_tx: append_tx});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} catch (e) {
|
|
198
|
+
console.log(e);
|
|
202
199
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
connection.messenger = message;
|
|
204
|
+
|
|
205
|
+
connection.on('close', () => {
|
|
206
|
+
if (network.validator_stream === connection) {
|
|
207
|
+
network.validator_stream = null;
|
|
208
|
+
network.validator = null;
|
|
209
|
+
}
|
|
210
|
+
if (network.admin_stream === connection) {
|
|
211
|
+
network.admin_stream = null;
|
|
212
|
+
network.admin = null;
|
|
213
|
+
}
|
|
214
|
+
message_channel.close()
|
|
215
|
+
});
|
|
206
216
|
|
|
207
217
|
// must be called AFTER the protomux init above
|
|
208
218
|
wakeup.addStream(connection);
|