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.
@@ -1 +1 @@
1
- 7f40fd0b7e8d116e922345f74184aa5c2a67c6a4a1a76db306b19a2897dc25b4
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: '19a12e9bdaf1cd9ae8169fd87f3d6d63d441c046e37e2ac6c3c36bcb87c59019',
7
- channel: '0000tracnetworkmainsettlementbus',
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.38",
4
+ "version": "0.1.40",
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, {
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
- 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 nonce = Wallet.generateNonce().toString('hex');
74
- const _msg = {op:'admin', key : writingKey, address : wallet.publicKey, channel : b4a.toString(channel, 'utf8')};
75
- const sig = wallet.sign( JSON.stringify(_msg) + nonce );
76
- message.send({response : _msg, sig, nonce})
77
- } else if(msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'validator'){
78
- const res = await msb.get(msg.response.address);
79
- if(res === null) return;
80
- const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, msg.response.address)
81
- if(verified && msg.response.channel === b4a.toString(channel, 'utf8')){
82
- console.log('Validator stream established', msg.response.address)
83
- network.validator_stream = connection;
84
- network.validator = msg.response.address;
85
- }
86
- } else if(msg.response !== undefined && msg.response.op !== undefined && msg.response.op === 'admin'){
87
- const res = await msb.get(EntryType.ADMIN);
88
- if(res === null || res.tracPublicKey !== msg.response.address) return;
89
- const verified = wallet.verify(msg.sig, JSON.stringify(msg.response) + msg.nonce, res.tracPublicKey)
90
- if(verified && msg.response.channel === b4a.toString(channel, 'utf8')){
91
- console.log('Admin stream established', res.tracPublicKey)
92
- network.admin_stream = connection;
93
- network.admin = res.tracPublicKey;
94
- }
95
- }else if(msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter'){
96
- const adminEntry = await msb.get(EntryType.ADMIN);
97
- if (null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
98
- const nodeEntry = await msb.get(msg.value.pub);
99
- const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
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
- else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addAdmin') {
116
- const adminEntry = await msb.get(EntryType.ADMIN);
117
- 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) {
118
119
  await handleIncomingEvent(msg);
119
- } else {
120
- if (base.isIndexer || !base.writable) return;
121
-
122
- if(true !== disable_rate_limit) {
123
- const peer = b4a.toString(connection.remotePublicKey, 'hex');
124
- const _now = Date.now();
125
-
126
- if(_now - clean >= 120_000) {
127
- clean = _now;
128
- conns = {};
129
- }
130
-
131
- if(conns[peer] === undefined){
132
- conns[peer] = { prev : _now, now: 0, tx_cnt : 0 }
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
- conns[peer].now = _now;
136
- conns[peer].tx_cnt += 1;
146
+ if (conns[peer] === undefined) {
147
+ conns[peer] = {prev: _now, now: 0, tx_cnt: 0}
148
+ }
137
149
 
138
- if(conns[peer].now - conns[peer].prev >= 60_000){
139
- delete conns[peer];
140
- }
150
+ conns[peer].now = _now;
151
+ conns[peer].tx_cnt += 1;
141
152
 
142
- if(conns[peer] !== undefined && conns[peer].now - conns[peer].prev >= 1000 && conns[peer].tx_cnt >= 50){
143
- swarm.leavePeer(connection.remotePublicKey);
144
- connection.end()
145
- }
153
+ if (conns[peer].now - conns[peer].prev >= 60_000) {
154
+ delete conns[peer];
146
155
  }
147
156
 
148
- if (network.tx_pool.length >= 1000) {
149
- console.log('pool full');
150
- 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()
151
160
  }
161
+ }
152
162
 
153
- if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
154
-
155
- const parsedPreTx = msg;
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
- connection.messenger = message;
168
+ if (b4a.byteLength(JSON.stringify(msg)) > 3072) return;
189
169
 
190
- connection.on('close', () => {
191
- if(network.validator_stream === connection){
192
- network.validator_stream = null;
193
- network.validator = null;
194
- }
195
- if(network.admin_stream === connection){
196
- network.admin_stream = null;
197
- 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);
198
199
  }
199
- message_channel.close()
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);