trac-peer 0.0.30 → 0.0.32

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +25 -20
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.0.30",
4
+ "version": "0.0.32",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.0.45",
package/src/index.js CHANGED
@@ -69,6 +69,8 @@ export class Peer extends ReadyResource {
69
69
  apply: async (nodes, view, base) => {
70
70
  if(this.contract_instance === null) await this.initContract();
71
71
 
72
+ const batch = view.batch();
73
+
72
74
  for (const node of nodes) {
73
75
  if(!node.value || !node.value.type) continue;
74
76
  const op = node.value;
@@ -78,77 +80,77 @@ export class Peer extends ReadyResource {
78
80
  const post_tx = await msb_view_session.get(op.key);
79
81
  await msb_view_session.close();
80
82
  if (null !== post_tx &&
81
- null === await view.get('tx/'+op.key) &&
83
+ null === await batch.get('tx/'+op.key) &&
82
84
  post_tx.value &&
83
85
  post_tx.value.tx &&
84
86
  op.key === post_tx.value.tx &&
85
87
  post_tx.value.ch === createHash('sha256').update(JSON.stringify(op.value.dispatch)).digest('hex')) {
86
- await view.put('tx/'+op.key, op.value);
87
- await _this.contract_instance.dispatch(op, node, view);
88
+ await batch.put('tx/'+op.key, op.value);
89
+ await _this.contract_instance.dispatch(op, node, batch);
88
90
  console.log(`${op.key} appended`);
89
91
  }
90
92
  } else if (op.type === 'feature') {
91
93
  if(!op.key || !op.value || !op.value.dispatch || !op.value.dispatch.hash || !op.value.dispatch.value) continue;
92
- const admin = await view.get('admin');
94
+ const admin = await batch.get('admin');
93
95
  if(null !== admin &&
94
96
  typeof op.value.dispatch === "object" &&
95
97
  typeof op.value.dispatch.hash === "string" &&
96
98
  typeof op.value.dispatch.value !== "undefined" &&
97
- null === await view.get('sh/'+op.value.dispatch.hash)){
99
+ null === await batch.get('sh/'+op.value.dispatch.hash)){
98
100
  const verified = _this.wallet.verify(op.value.dispatch.hash, JSON.stringify(op.value.dispatch.value), admin.value);
99
101
  if(verified) {
100
- await _this.contract_instance.dispatch(op, node, view);
102
+ await _this.contract_instance.dispatch(op, node, batch);
101
103
  }
102
- await view.put('sh/'+op.value.dispatch.hash, '');
104
+ await batch.put('sh/'+op.value.dispatch.hash, '');
103
105
  }
104
106
  console.log(`Feature ${op.key} appended`);
105
107
  } else if (op.type === 'addIndexer') {
106
108
  if(!op.key || !op.value || !op.value.hash || !op.value.msg || !op.value.msg.key || !op.value.msg.type) continue;
107
- const admin = await view.get('admin');
109
+ const admin = await batch.get('admin');
108
110
  if(null !== admin &&
109
111
  op.value.msg.key === op.key &&
110
112
  op.value.msg.type === 'addIndexer' &&
111
- null === await view.get('sh/'+op.value.hash)) {
113
+ null === await batch.get('sh/'+op.value.hash)) {
112
114
  const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
113
115
  if(verified){
114
116
  const writerKey = b4a.from(op.key, 'hex');
115
117
  await base.addWriter(writerKey);
116
118
  console.log(`Indexer added: ${op.key}`);
117
119
  }
118
- await view.put('sh/'+op.value.hash, '');
120
+ await batch.put('sh/'+op.value.hash, '');
119
121
  }
120
122
  } else if (op.type === 'addWriter') {
121
123
  if(!op.key || !op.value || !op.value.hash || !op.value.msg || !op.value.msg.key || !op.value.msg.type) continue;
122
- const admin = await view.get('admin');
124
+ const admin = await batch.get('admin');
123
125
  if(null !== admin &&
124
126
  op.value.msg.key === op.key &&
125
127
  op.value.msg.type === 'addWriter' &&
126
- null === await view.get('sh/'+op.value.hash)) {
128
+ null === await batch.get('sh/'+op.value.hash)) {
127
129
  const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
128
130
  if(verified){
129
131
  const writerKey = b4a.from(op.key, 'hex');
130
132
  await base.addWriter(writerKey, { isIndexer : false });
131
133
  console.log(`Writer added: ${op.key}`);
132
134
  }
133
- await view.put('sh/'+op.value.hash, '');
135
+ await batch.put('sh/'+op.value.hash, '');
134
136
  }
135
137
  } else if (op.type === 'setAutoAddWriters') {
136
138
  if(!op.key || !op.value || !op.value.hash || !op.value.msg || !op.value.msg.key || !op.value.msg.type) continue;
137
- const admin = await view.get('admin');
139
+ const admin = await batch.get('admin');
138
140
  if(null !== admin && op.value.msg.key === op.key &&
139
141
  op.value.msg.type === 'setAutoAddWriters' &&
140
142
  (op.key === 'on' || op.key === 'off') &&
141
- null === await view.get('sh/'+op.value.hash)) {
143
+ null === await batch.get('sh/'+op.value.hash)) {
142
144
  const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
143
145
  if(verified){
144
- await view.put('auto_add_writers', op.key);
146
+ await batch.put('auto_add_writers', op.key);
145
147
  console.log(`Set auto_add_writers: ${op.key}`);
146
148
  }
147
- await view.put('sh/'+op.value.hash, '');
149
+ await batch.put('sh/'+op.value.hash, '');
148
150
  }
149
151
  } else if (op.type === 'autoAddWriter') {
150
152
  if(!op.key) continue;
151
- const auto_add_writers = await view.get('auto_add_writers');
153
+ const auto_add_writers = await batch.get('auto_add_writers');
152
154
  if(null !== auto_add_writers && auto_add_writers.value === 'on'){
153
155
  const writerKey = b4a.from(op.key, 'hex');
154
156
  await base.addWriter(writerKey, { isIndexer : false });
@@ -157,12 +159,15 @@ export class Peer extends ReadyResource {
157
159
  } else if (op.type === 'addAdmin') {
158
160
  if(!op.key) continue;
159
161
  const bootstrap = Buffer(node.from.key).toString('hex')
160
- if(null === await view.get('admin') && bootstrap === _this.bootstrap){
161
- await view.put('admin', op.key);
162
+ if(null === await batch.get('admin') && bootstrap === _this.bootstrap){
163
+ await batch.put('admin', op.key);
162
164
  console.log(`Admin added: ${op.key}`);
163
165
  }
164
166
  }
165
167
  }
168
+
169
+ await batch.flush();
170
+ await batch.close();
166
171
  }
167
172
  })
168
173
  this.base.on('warning', (e) => console.log(e))