trac-msb 0.1.38 → 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/Whitelist/pubkeys.csv +1 -1
- package/msb.mjs +4 -2
- package/package.json +1 -1
- package/src/index.js +4 -0
- package/src/network.js +150 -136
package/Whitelist/pubkeys.csv
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
320fef56d8fa3e2a226aacc2acdb397d63abfb96b60b8ea3b0624045fc6b3eae
|
package/msb.mjs
CHANGED
|
@@ -3,8 +3,10 @@ import {MainSettlementBus} from './src/index.js';
|
|
|
3
3
|
const opts = {
|
|
4
4
|
stores_directory : 'stores/',
|
|
5
5
|
store_name : typeof process !== "undefined" ? process.argv[2] : Pear.config.args[0],
|
|
6
|
-
bootstrap: '
|
|
7
|
-
channel: '
|
|
6
|
+
bootstrap: 'd7cce91547144be2e234992501b556dcd1591ab44313cf3036ddd3137afbc347',
|
|
7
|
+
channel: '00axtracnetworkmainsettlementbus',
|
|
8
|
+
enable_txlogs : true,
|
|
9
|
+
disable_rate_limit : false
|
|
8
10
|
};
|
|
9
11
|
|
|
10
12
|
const msb = new MainSettlementBus(opts);
|
package/package.json
CHANGED
package/src/index.js
CHANGED
package/src/network.js
CHANGED
|
@@ -50,155 +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 isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
101
|
-
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
102
|
-
if (msg.key !== wallet.publicKey && canAddWriter) {
|
|
103
|
-
await handleIncomingEvent(msg);
|
|
104
|
-
}
|
|
105
|
-
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
106
|
-
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
107
|
-
if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
108
|
-
const nodeEntry = await msb.get(msg.value.pub);
|
|
109
|
-
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
110
|
-
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
111
|
-
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
112
|
-
await handleIncomingEvent(msg);
|
|
113
|
-
}
|
|
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;
|
|
114
100
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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) {
|
|
118
119
|
await handleIncomingEvent(msg);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
}
|
|
134
145
|
|
|
135
|
-
|
|
136
|
-
conns[peer]
|
|
146
|
+
if (conns[peer] === undefined) {
|
|
147
|
+
conns[peer] = {prev: _now, now: 0, tx_cnt: 0}
|
|
148
|
+
}
|
|
137
149
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
150
|
+
conns[peer].now = _now;
|
|
151
|
+
conns[peer].tx_cnt += 1;
|
|
141
152
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
connection.end()
|
|
145
|
-
}
|
|
153
|
+
if (conns[peer].now - conns[peer].prev >= 60_000) {
|
|
154
|
+
delete conns[peer];
|
|
146
155
|
}
|
|
147
156
|
|
|
148
|
-
if (
|
|
149
|
-
|
|
150
|
-
|
|
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()
|
|
151
160
|
}
|
|
161
|
+
}
|
|
152
162
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (network.check.sanitizePreTx(parsedPreTx) &&
|
|
158
|
-
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
159
|
-
parsedPreTx.wp === wallet.publicKey &&
|
|
160
|
-
null === await base.view.get(parsedPreTx.tx)
|
|
161
|
-
) {
|
|
162
|
-
const nonce = Wallet.generateNonce().toString('hex');
|
|
163
|
-
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
164
|
-
const append_tx = {
|
|
165
|
-
op: OperationType.POST_TX,
|
|
166
|
-
tx: parsedPreTx.tx,
|
|
167
|
-
is: parsedPreTx.is,
|
|
168
|
-
w: writingKey,
|
|
169
|
-
i: parsedPreTx.i,
|
|
170
|
-
ipk: parsedPreTx.ipk,
|
|
171
|
-
ch: parsedPreTx.ch,
|
|
172
|
-
in: parsedPreTx.in,
|
|
173
|
-
bs: parsedPreTx.bs,
|
|
174
|
-
mbs: parsedPreTx.mbs,
|
|
175
|
-
ws: signature.toString('hex'),
|
|
176
|
-
wp: wallet.publicKey,
|
|
177
|
-
wn: nonce
|
|
178
|
-
};
|
|
179
|
-
network.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
|
|
180
|
-
}
|
|
163
|
+
if (network.tx_pool.length >= 1000) {
|
|
164
|
+
console.log('pool full');
|
|
165
|
+
return
|
|
181
166
|
}
|
|
182
|
-
} catch (e) {
|
|
183
|
-
console.log(e);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
167
|
|
|
188
|
-
|
|
168
|
+
if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
|
|
189
169
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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);
|
|
198
199
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
+
});
|
|
202
216
|
|
|
203
217
|
// must be called AFTER the protomux init above
|
|
204
218
|
wakeup.addStream(connection);
|