trac-msb 0.1.39 → 0.1.41

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.39",
4
+ "version": "0.1.41",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -128,6 +128,10 @@ export class MainSettlementBus extends ReadyResource {
128
128
  return this.#swarm;
129
129
  }
130
130
 
131
+ getNetwork(){
132
+ return this.#network;
133
+ }
134
+
131
135
  #boot() {
132
136
  const _this = this;
133
137
  this.#base = new Autobase(this.#store, this.#bootstrap, {
@@ -762,8 +766,8 @@ export class MainSettlementBus extends ReadyResource {
762
766
  this.#swarm.joinPeer(b4a.from(address, 'hex'));
763
767
  let cnt = 0;
764
768
  while(false === this.#swarm.peers.has(address)){
765
- if(cnt >= 15) break;
766
- await sleep(1_000);
769
+ if(cnt >= 1500) break;
770
+ await sleep(10);
767
771
  cnt += 1;
768
772
  }
769
773
  }
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
- if(enable_txchannel) {
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
- onclose() { }
61
- })
62
- message_channel.open()
63
- const message = message_channel.addMessage({
64
- encoding: c.json,
65
- async onmessage(msg) {
66
- try{
67
- if(msg === 'get_validator'){
68
- const nonce = Wallet.generateNonce().toString('hex');
69
- const _msg = {op:'validator', key : writingKey, address : wallet.publicKey, channel : b4a.toString(channel, 'utf8')};
70
- const sig = wallet.sign( JSON.stringify(_msg) + nonce );
71
- message.send({response : _msg, sig, nonce})
72
- } else if(msg === 'get_admin'){
73
- const res = await msb.get(EntryType.ADMIN);
74
- if(wallet.publicKey !== res.tracPublicKey) return;
75
- const nonce = Wallet.generateNonce().toString('hex');
76
- const _msg = {op:'admin', key : writingKey, address : wallet.publicKey, channel : b4a.toString(channel, 'utf8')};
77
- const sig = wallet.sign( JSON.stringify(_msg) + nonce );
78
- message.send({response : _msg, sig, nonce})
79
- swarm.leavePeer(connection.remotePublicKey)
80
- } else if(msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'validator'){
81
- const res = await msb.get(msg.response.address);
82
- if(res === null) return;
83
- const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, msg.response.address)
84
- if(verified && msg.response.channel === b4a.toString(channel, 'utf8')){
85
- console.log('Validator stream established', msg.response.address)
86
- network.validator_stream = connection;
87
- network.validator = msg.response.address;
88
- }
89
- } else if(msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'admin'){
90
- const res = await msb.get(EntryType.ADMIN);
91
- if(res === null || res.tracPublicKey !== msg.response.address) return;
92
- const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, res.tracPublicKey)
93
- if(verified && msg.response.channel === b4a.toString(channel, 'utf8')){
94
- console.log('Admin stream established', res.tracPublicKey)
95
- network.admin_stream = connection;
96
- network.admin = res.tracPublicKey;
97
- }
98
- swarm.leavePeer(connection.remotePublicKey)
99
- }else if(msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter'){
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
- else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addAdmin') {
120
- const adminEntry = await msb.get(EntryType.ADMIN);
121
- if (null === adminEntry || (adminEntry.tracPublicKey !== msg.key)) return;
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
- } else {
124
- if (base.isIndexer || !base.writable) return;
125
-
126
- if(true !== disable_rate_limit) {
127
- const peer = b4a.toString(connection.remotePublicKey, 'hex');
128
- const _now = Date.now();
129
-
130
- if(_now - clean >= 120_000) {
131
- clean = _now;
132
- conns = {};
133
- }
134
-
135
- if(conns[peer] === undefined){
136
- conns[peer] = { prev : _now, now: 0, tx_cnt : 0 }
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
- conns[peer].now = _now;
140
- conns[peer].tx_cnt += 1;
146
+ if (conns[peer] === undefined) {
147
+ conns[peer] = {prev: _now, now: 0, tx_cnt: 0}
148
+ }
141
149
 
142
- if(conns[peer].now - conns[peer].prev >= 60_000){
143
- delete conns[peer];
144
- }
150
+ conns[peer].now = _now;
151
+ conns[peer].tx_cnt += 1;
145
152
 
146
- if(conns[peer] !== undefined && conns[peer].now - conns[peer].prev >= 1000 && conns[peer].tx_cnt >= 50){
147
- swarm.leavePeer(connection.remotePublicKey);
148
- connection.end()
149
- }
153
+ if (conns[peer].now - conns[peer].prev >= 60_000) {
154
+ delete conns[peer];
150
155
  }
151
156
 
152
- if (network.tx_pool.length >= 1000) {
153
- console.log('pool full');
154
- return
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
- if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
158
-
159
- const parsedPreTx = msg;
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
- connection.messenger = message;
168
+ if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
193
169
 
194
- connection.on('close', () => {
195
- if(network.validator_stream === connection){
196
- network.validator_stream = null;
197
- network.validator = null;
198
- }
199
- if(network.admin_stream === connection){
200
- network.admin_stream = null;
201
- network.admin = null;
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
- message_channel.close()
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);