hypercore-storage 2.8.0 → 3.0.0

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.
@@ -0,0 +1,14 @@
1
+ function headLegacyMap(m) {
2
+ return {
3
+ version: 2, // version bumped from 1
4
+ cores: m.allocated.cores,
5
+ datas: m.allocated.datas,
6
+ groups: 0,
7
+ seed: m.seed,
8
+ defaultDiscoveryKey: m.defaultDiscoveryKey
9
+ }
10
+ }
11
+
12
+ module.exports = {
13
+ headLegacyMap
14
+ }
@@ -5,6 +5,7 @@
5
5
  /* eslint-disable space-before-function-paren */
6
6
 
7
7
  const { c } = require('hyperschema/runtime')
8
+ const external0 = require('../../external.js')
8
9
 
9
10
  const VERSION = 1
10
11
 
@@ -32,10 +33,9 @@ const encoding0 = {
32
33
  }
33
34
  }
34
35
 
35
- // @corestore/head
36
+ // @corestore/head-v1
36
37
  const encoding1 = {
37
38
  preencode(state, m) {
38
- c.uint.preencode(state, m.version)
39
39
  state.end++ // max flag is 4 so always one byte
40
40
 
41
41
  if (m.allocated) encoding0.preencode(state, m.allocated)
@@ -45,7 +45,6 @@ const encoding1 = {
45
45
  encode(state, m) {
46
46
  const flags = (m.allocated ? 1 : 0) | (m.seed ? 2 : 0) | (m.defaultDiscoveryKey ? 4 : 0)
47
47
 
48
- c.uint.encode(state, m.version)
49
48
  c.uint.encode(state, flags)
50
49
 
51
50
  if (m.allocated) encoding0.encode(state, m.allocated)
@@ -53,11 +52,11 @@ const encoding1 = {
53
52
  if (m.defaultDiscoveryKey) c.fixed32.encode(state, m.defaultDiscoveryKey)
54
53
  },
55
54
  decode(state) {
56
- const r0 = c.uint.decode(state)
55
+ const v = c.uint.decode(state)
57
56
  const flags = c.uint.decode(state)
58
57
 
59
58
  return {
60
- version: r0,
59
+ version: v,
61
60
  allocated: (flags & 1) !== 0 ? encoding0.decode(state) : null,
62
61
  seed: (flags & 2) !== 0 ? c.fixed32.decode(state) : null,
63
62
  defaultDiscoveryKey: (flags & 4) !== 0 ? c.fixed32.decode(state) : null
@@ -127,10 +126,10 @@ const encoding4_enum = {
127
126
 
128
127
  // @core/hashes enum
129
128
  const encoding4 = {
130
- preencode (state, m) {
129
+ preencode(state, m) {
131
130
  state.end++ // max enum is 0 so always one byte
132
131
  },
133
- encode (state, m) {
132
+ encode(state, m) {
134
133
  switch (m) {
135
134
  case 'blake2b':
136
135
  c.uint.encode(state, 0)
@@ -139,11 +138,12 @@ const encoding4 = {
139
138
  throw new Error('Unknown enum')
140
139
  }
141
140
  },
142
- decode (state) {
141
+ decode(state) {
143
142
  switch (c.uint.decode(state)) {
144
143
  case 0:
145
144
  return 'blake2b'
146
- default: return null
145
+ default:
146
+ return null
147
147
  }
148
148
  }
149
149
  }
@@ -154,10 +154,10 @@ const encoding5_enum = {
154
154
 
155
155
  // @core/signatures enum
156
156
  const encoding5 = {
157
- preencode (state, m) {
157
+ preencode(state, m) {
158
158
  state.end++ // max enum is 0 so always one byte
159
159
  },
160
- encode (state, m) {
160
+ encode(state, m) {
161
161
  switch (m) {
162
162
  case 'ed25519':
163
163
  c.uint.encode(state, 0)
@@ -166,11 +166,12 @@ const encoding5 = {
166
166
  throw new Error('Unknown enum')
167
167
  }
168
168
  },
169
- decode (state) {
169
+ decode(state) {
170
170
  switch (c.uint.decode(state)) {
171
171
  case 0:
172
172
  return 'ed25519'
173
- default: return null
173
+ default:
174
+ return null
174
175
  }
175
176
  }
176
177
  }
@@ -299,15 +300,15 @@ const encoding9 = {
299
300
  const encoding10 = {
300
301
  preencode(state, m) {
301
302
  c.buffer.preencode(state, m.publicKey)
302
- c.buffer.preencode(state, m.secretKey)
303
+ c.optionalBuffer.preencode(state, m.secretKey)
303
304
  },
304
305
  encode(state, m) {
305
306
  c.buffer.encode(state, m.publicKey)
306
- c.buffer.encode(state, m.secretKey)
307
+ c.optionalBuffer.encode(state, m.secretKey)
307
308
  },
308
309
  decode(state) {
309
310
  const r0 = c.buffer.decode(state)
310
- const r1 = c.buffer.decode(state)
311
+ const r1 = c.optionalBuffer.decode(state)
311
312
 
312
313
  return {
313
314
  publicKey: r0,
@@ -316,17 +317,43 @@ const encoding10 = {
316
317
  }
317
318
  }
318
319
 
320
+ // @core/group
321
+ const encoding11 = {
322
+ preencode(state, m) {
323
+ state.end++ // max flag is 2 so always one byte
324
+
325
+ if (m.key) c.fixed32.preencode(state, m.key)
326
+ if (m.pointer) c.uint.preencode(state, m.pointer)
327
+ },
328
+ encode(state, m) {
329
+ const flags = (m.key ? 1 : 0) | (m.pointer ? 2 : 0)
330
+
331
+ c.uint.encode(state, flags)
332
+
333
+ if (m.key) c.fixed32.encode(state, m.key)
334
+ if (m.pointer) c.uint.encode(state, m.pointer)
335
+ },
336
+ decode(state) {
337
+ const flags = c.uint.decode(state)
338
+
339
+ return {
340
+ key: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
341
+ pointer: (flags & 2) !== 0 ? c.uint.decode(state) : 0
342
+ }
343
+ }
344
+ }
345
+
319
346
  // @core/auth.manifest
320
- const encoding11_2 = c.frame(encoding9)
347
+ const encoding12_2 = c.frame(encoding9)
321
348
 
322
349
  // @core/auth
323
- const encoding11 = {
350
+ const encoding12 = {
324
351
  preencode(state, m) {
325
352
  c.fixed32.preencode(state, m.key)
326
353
  c.fixed32.preencode(state, m.discoveryKey)
327
354
  state.end++ // max flag is 4 so always one byte
328
355
 
329
- if (m.manifest) encoding11_2.preencode(state, m.manifest)
356
+ if (m.manifest) encoding12_2.preencode(state, m.manifest)
330
357
  if (m.keyPair) encoding10.preencode(state, m.keyPair)
331
358
  if (m.encryptionKey) c.buffer.preencode(state, m.encryptionKey)
332
359
  },
@@ -337,7 +364,7 @@ const encoding11 = {
337
364
  c.fixed32.encode(state, m.discoveryKey)
338
365
  c.uint.encode(state, flags)
339
366
 
340
- if (m.manifest) encoding11_2.encode(state, m.manifest)
367
+ if (m.manifest) encoding12_2.encode(state, m.manifest)
341
368
  if (m.keyPair) encoding10.encode(state, m.keyPair)
342
369
  if (m.encryptionKey) c.buffer.encode(state, m.encryptionKey)
343
370
  },
@@ -349,7 +376,7 @@ const encoding11 = {
349
376
  return {
350
377
  key: r0,
351
378
  discoveryKey: r1,
352
- manifest: (flags & 1) !== 0 ? encoding11_2.decode(state) : null,
379
+ manifest: (flags & 1) !== 0 ? encoding12_2.decode(state) : null,
353
380
  keyPair: (flags & 2) !== 0 ? encoding10.decode(state) : null,
354
381
  encryptionKey: (flags & 4) !== 0 ? c.buffer.decode(state) : null
355
382
  }
@@ -357,36 +384,46 @@ const encoding11 = {
357
384
  }
358
385
 
359
386
  // @core/head
360
- const encoding12 = {
387
+ const encoding13 = {
361
388
  preencode(state, m) {
362
389
  c.uint.preencode(state, m.fork)
363
390
  c.uint.preencode(state, m.length)
364
391
  c.fixed32.preencode(state, m.rootHash)
365
- c.buffer.preencode(state, m.signature)
392
+ c.optionalBuffer.preencode(state, m.signature)
393
+ state.end++ // max flag is 1 so always one byte
394
+
395
+ if (m.timestamp) c.uint64.preencode(state, m.timestamp)
366
396
  },
367
397
  encode(state, m) {
398
+ const flags = m.timestamp ? 1 : 0
399
+
368
400
  c.uint.encode(state, m.fork)
369
401
  c.uint.encode(state, m.length)
370
402
  c.fixed32.encode(state, m.rootHash)
371
- c.buffer.encode(state, m.signature)
403
+ c.optionalBuffer.encode(state, m.signature)
404
+ c.uint.encode(state, flags)
405
+
406
+ if (m.timestamp) c.uint64.encode(state, m.timestamp)
372
407
  },
373
408
  decode(state) {
374
409
  const r0 = c.uint.decode(state)
375
410
  const r1 = c.uint.decode(state)
376
411
  const r2 = c.fixed32.decode(state)
377
- const r3 = c.buffer.decode(state)
412
+ const r3 = c.optionalBuffer.decode(state)
413
+ const flags = c.uint.decode(state)
378
414
 
379
415
  return {
380
416
  fork: r0,
381
417
  length: r1,
382
418
  rootHash: r2,
383
- signature: r3
419
+ signature: r3,
420
+ timestamp: (flags & 1) !== 0 ? c.uint64.decode(state) : 0
384
421
  }
385
422
  }
386
423
  }
387
424
 
388
425
  // @core/hints
389
- const encoding13 = {
426
+ const encoding14 = {
390
427
  preencode(state, m) {
391
428
  state.end++ // max flag is 4 so always one byte
392
429
 
@@ -416,7 +453,7 @@ const encoding13 = {
416
453
  }
417
454
 
418
455
  // @core/session
419
- const encoding14 = {
456
+ const encoding15 = {
420
457
  preencode(state, m) {
421
458
  c.string.preencode(state, m.name)
422
459
  c.uint.preencode(state, m.dataPointer)
@@ -437,10 +474,10 @@ const encoding14 = {
437
474
  }
438
475
 
439
476
  // @core/sessions
440
- const encoding15 = c.array(encoding14)
477
+ const encoding16 = c.array(encoding15)
441
478
 
442
479
  // @core/dependency
443
- const encoding16 = {
480
+ const encoding17 = {
444
481
  preencode(state, m) {
445
482
  c.uint.preencode(state, m.dataPointer)
446
483
  c.uint.preencode(state, m.length)
@@ -460,6 +497,97 @@ const encoding16 = {
460
497
  }
461
498
  }
462
499
 
500
+ // @corestore/head-v2
501
+ const encoding18 = {
502
+ preencode(state, m) {
503
+ c.uint.preencode(state, m.cores)
504
+ c.uint.preencode(state, m.datas)
505
+ c.uint.preencode(state, m.groups)
506
+ state.end++ // max flag is 2 so always one byte
507
+
508
+ if (m.seed) c.fixed32.preencode(state, m.seed)
509
+ if (m.defaultDiscoveryKey) c.fixed32.preencode(state, m.defaultDiscoveryKey)
510
+ },
511
+ encode(state, m) {
512
+ const flags = (m.seed ? 1 : 0) | (m.defaultDiscoveryKey ? 2 : 0)
513
+
514
+ c.uint.encode(state, m.cores)
515
+ c.uint.encode(state, m.datas)
516
+ c.uint.encode(state, m.groups)
517
+ c.uint.encode(state, flags)
518
+
519
+ if (m.seed) c.fixed32.encode(state, m.seed)
520
+ if (m.defaultDiscoveryKey) c.fixed32.encode(state, m.defaultDiscoveryKey)
521
+ },
522
+ decode(state) {
523
+ const v = c.uint.decode(state)
524
+ const r0 = c.uint.decode(state)
525
+ const r1 = c.uint.decode(state)
526
+ const r2 = c.uint.decode(state)
527
+ const flags = c.uint.decode(state)
528
+
529
+ return {
530
+ version: v,
531
+ cores: r0,
532
+ datas: r1,
533
+ groups: r2,
534
+ seed: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
535
+ defaultDiscoveryKey: (flags & 2) !== 0 ? c.fixed32.decode(state) : null
536
+ }
537
+ }
538
+ }
539
+
540
+ // @corestore/head
541
+ const encoding19 = {
542
+ preencode(state, m) {
543
+ c.uint.preencode(state, m.version)
544
+ switch (m.version) {
545
+ case 0:
546
+ case 1:
547
+ encoding1.preencode(state, m)
548
+ break
549
+ case 2:
550
+ encoding18.preencode(state, m)
551
+ break
552
+ default:
553
+ throw new Error('Unsupported version')
554
+ }
555
+ },
556
+ encode(state, m) {
557
+ c.uint.encode(state, m.version)
558
+ switch (m.version) {
559
+ case 0:
560
+ case 1:
561
+ encoding1.encode(state, m)
562
+ break
563
+ case 2:
564
+ encoding18.encode(state, m)
565
+ break
566
+ default:
567
+ throw new Error('Unsupported version')
568
+ }
569
+ },
570
+ decode(state) {
571
+ const start = state.start
572
+ const v = c.uint.decode(state)
573
+ state.start = start
574
+ switch (v) {
575
+ case 0:
576
+ case 1: {
577
+ const decoded = encoding1.decode(state)
578
+ const map = external0.headLegacyMap
579
+ return map(decoded)
580
+ }
581
+ case 2: {
582
+ const decoded = encoding18.decode(state)
583
+ return decoded
584
+ }
585
+ default:
586
+ throw new Error('Unsupported version')
587
+ }
588
+ }
589
+ }
590
+
463
591
  function setVersion(v) {
464
592
  version = v
465
593
  }
@@ -489,7 +617,7 @@ function getEncoding(name) {
489
617
  switch (name) {
490
618
  case '@corestore/allocated':
491
619
  return encoding0
492
- case '@corestore/head':
620
+ case '@corestore/head-v1':
493
621
  return encoding1
494
622
  case '@corestore/alias':
495
623
  return encoding2
@@ -509,18 +637,24 @@ function getEncoding(name) {
509
637
  return encoding9
510
638
  case '@core/keyPair':
511
639
  return encoding10
512
- case '@core/auth':
640
+ case '@core/group':
513
641
  return encoding11
514
- case '@core/head':
642
+ case '@core/auth':
515
643
  return encoding12
516
- case '@core/hints':
644
+ case '@core/head':
517
645
  return encoding13
518
- case '@core/session':
646
+ case '@core/hints':
519
647
  return encoding14
520
- case '@core/sessions':
648
+ case '@core/session':
521
649
  return encoding15
522
- case '@core/dependency':
650
+ case '@core/sessions':
523
651
  return encoding16
652
+ case '@core/dependency':
653
+ return encoding17
654
+ case '@corestore/head-v2':
655
+ return encoding18
656
+ case '@corestore/head':
657
+ return encoding19
524
658
  default:
525
659
  throw new Error('Encoder not found ' + name)
526
660
  }
@@ -0,0 +1,481 @@
1
+ {
2
+ "version": 1,
3
+ "schema": [
4
+ {
5
+ "name": "allocated",
6
+ "namespace": "corestore",
7
+ "compact": true,
8
+ "flagsPosition": -1,
9
+ "fields": [
10
+ {
11
+ "name": "cores",
12
+ "required": true,
13
+ "type": "uint",
14
+ "version": 1
15
+ },
16
+ {
17
+ "name": "datas",
18
+ "required": true,
19
+ "type": "uint",
20
+ "version": 1
21
+ }
22
+ ]
23
+ },
24
+ {
25
+ "name": "head-v1",
26
+ "namespace": "corestore",
27
+ "compact": false,
28
+ "flagsPosition": 0,
29
+ "fields": [
30
+ {
31
+ "name": "allocated",
32
+ "type": "@corestore/allocated",
33
+ "version": 1
34
+ },
35
+ {
36
+ "name": "seed",
37
+ "type": "fixed32",
38
+ "version": 1
39
+ },
40
+ {
41
+ "name": "defaultDiscoveryKey",
42
+ "type": "fixed32",
43
+ "version": 1
44
+ }
45
+ ]
46
+ },
47
+ {
48
+ "name": "alias",
49
+ "namespace": "corestore",
50
+ "compact": true,
51
+ "flagsPosition": -1,
52
+ "fields": [
53
+ {
54
+ "name": "name",
55
+ "required": true,
56
+ "type": "string",
57
+ "version": 1
58
+ },
59
+ {
60
+ "name": "namespace",
61
+ "required": true,
62
+ "type": "fixed32",
63
+ "version": 1
64
+ }
65
+ ]
66
+ },
67
+ {
68
+ "name": "core",
69
+ "namespace": "corestore",
70
+ "compact": false,
71
+ "flagsPosition": 3,
72
+ "fields": [
73
+ {
74
+ "name": "version",
75
+ "required": true,
76
+ "type": "uint",
77
+ "version": 1
78
+ },
79
+ {
80
+ "name": "corePointer",
81
+ "required": true,
82
+ "type": "uint",
83
+ "version": 1
84
+ },
85
+ {
86
+ "name": "dataPointer",
87
+ "required": true,
88
+ "type": "uint",
89
+ "version": 1
90
+ },
91
+ {
92
+ "name": "alias",
93
+ "type": "@corestore/alias",
94
+ "version": 1
95
+ }
96
+ ]
97
+ },
98
+ {
99
+ "name": "hashes",
100
+ "namespace": "core",
101
+ "offset": 0,
102
+ "enum": [
103
+ {
104
+ "key": "blake2b",
105
+ "version": 1
106
+ }
107
+ ],
108
+ "strings": true
109
+ },
110
+ {
111
+ "name": "signatures",
112
+ "namespace": "core",
113
+ "offset": 0,
114
+ "enum": [
115
+ {
116
+ "key": "ed25519",
117
+ "version": 1
118
+ }
119
+ ],
120
+ "strings": true
121
+ },
122
+ {
123
+ "name": "tree-node",
124
+ "namespace": "core",
125
+ "compact": true,
126
+ "flagsPosition": -1,
127
+ "fields": [
128
+ {
129
+ "name": "index",
130
+ "required": true,
131
+ "type": "uint",
132
+ "version": 1
133
+ },
134
+ {
135
+ "name": "size",
136
+ "required": true,
137
+ "type": "uint",
138
+ "version": 1
139
+ },
140
+ {
141
+ "name": "hash",
142
+ "required": true,
143
+ "type": "fixed32",
144
+ "version": 1
145
+ }
146
+ ]
147
+ },
148
+ {
149
+ "name": "signer",
150
+ "namespace": "core",
151
+ "compact": true,
152
+ "flagsPosition": -1,
153
+ "fields": [
154
+ {
155
+ "name": "signature",
156
+ "required": true,
157
+ "type": "@core/signatures",
158
+ "version": 1
159
+ },
160
+ {
161
+ "name": "namespace",
162
+ "required": true,
163
+ "type": "fixed32",
164
+ "version": 1
165
+ },
166
+ {
167
+ "name": "publicKey",
168
+ "required": true,
169
+ "type": "fixed32",
170
+ "version": 1
171
+ }
172
+ ]
173
+ },
174
+ {
175
+ "name": "prologue",
176
+ "namespace": "core",
177
+ "compact": true,
178
+ "flagsPosition": -1,
179
+ "fields": [
180
+ {
181
+ "name": "hash",
182
+ "required": true,
183
+ "type": "fixed32",
184
+ "version": 1
185
+ },
186
+ {
187
+ "name": "length",
188
+ "required": true,
189
+ "type": "uint",
190
+ "version": 1
191
+ }
192
+ ]
193
+ },
194
+ {
195
+ "name": "manifest",
196
+ "namespace": "core",
197
+ "compact": false,
198
+ "flagsPosition": 1,
199
+ "fields": [
200
+ {
201
+ "name": "version",
202
+ "required": true,
203
+ "type": "uint",
204
+ "version": 1
205
+ },
206
+ {
207
+ "name": "hash",
208
+ "required": true,
209
+ "type": "@core/hashes",
210
+ "version": 1
211
+ },
212
+ {
213
+ "name": "quorum",
214
+ "required": true,
215
+ "type": "uint",
216
+ "version": 1
217
+ },
218
+ {
219
+ "name": "allowPatch",
220
+ "type": "bool",
221
+ "version": 1
222
+ },
223
+ {
224
+ "name": "signers",
225
+ "required": true,
226
+ "array": true,
227
+ "type": "@core/signer",
228
+ "version": 1
229
+ },
230
+ {
231
+ "name": "prologue",
232
+ "type": "@core/prologue",
233
+ "version": 1
234
+ },
235
+ {
236
+ "name": "linked",
237
+ "array": true,
238
+ "type": "fixed32",
239
+ "version": 1
240
+ },
241
+ {
242
+ "name": "userData",
243
+ "type": "buffer",
244
+ "version": 1
245
+ }
246
+ ]
247
+ },
248
+ {
249
+ "name": "keyPair",
250
+ "namespace": "core",
251
+ "compact": true,
252
+ "flagsPosition": -1,
253
+ "fields": [
254
+ {
255
+ "name": "publicKey",
256
+ "required": true,
257
+ "type": "buffer",
258
+ "version": 1
259
+ },
260
+ {
261
+ "name": "secretKey",
262
+ "required": true,
263
+ "type": "optionalBuffer",
264
+ "version": 1
265
+ }
266
+ ]
267
+ },
268
+ {
269
+ "name": "group",
270
+ "namespace": "core",
271
+ "compact": false,
272
+ "flagsPosition": 0,
273
+ "fields": [
274
+ {
275
+ "name": "key",
276
+ "type": "fixed32",
277
+ "version": 1
278
+ },
279
+ {
280
+ "name": "pointer",
281
+ "type": "uint",
282
+ "version": 1
283
+ }
284
+ ]
285
+ },
286
+ {
287
+ "name": "auth",
288
+ "namespace": "core",
289
+ "compact": false,
290
+ "flagsPosition": 2,
291
+ "fields": [
292
+ {
293
+ "name": "key",
294
+ "required": true,
295
+ "type": "fixed32",
296
+ "version": 1
297
+ },
298
+ {
299
+ "name": "discoveryKey",
300
+ "required": true,
301
+ "type": "fixed32",
302
+ "version": 1
303
+ },
304
+ {
305
+ "name": "manifest",
306
+ "type": "@core/manifest",
307
+ "version": 1
308
+ },
309
+ {
310
+ "name": "keyPair",
311
+ "type": "@core/keyPair",
312
+ "version": 1
313
+ },
314
+ {
315
+ "name": "encryptionKey",
316
+ "type": "buffer",
317
+ "version": 1
318
+ }
319
+ ]
320
+ },
321
+ {
322
+ "name": "head",
323
+ "namespace": "core",
324
+ "compact": false,
325
+ "flagsPosition": 4,
326
+ "fields": [
327
+ {
328
+ "name": "fork",
329
+ "required": true,
330
+ "type": "uint",
331
+ "version": 1
332
+ },
333
+ {
334
+ "name": "length",
335
+ "required": true,
336
+ "type": "uint",
337
+ "version": 1
338
+ },
339
+ {
340
+ "name": "rootHash",
341
+ "required": true,
342
+ "type": "fixed32",
343
+ "version": 1
344
+ },
345
+ {
346
+ "name": "signature",
347
+ "required": true,
348
+ "type": "optionalBuffer",
349
+ "version": 1
350
+ },
351
+ {
352
+ "name": "timestamp",
353
+ "required": false,
354
+ "type": "uint64",
355
+ "version": 1
356
+ }
357
+ ]
358
+ },
359
+ {
360
+ "name": "hints",
361
+ "namespace": "core",
362
+ "compact": false,
363
+ "flagsPosition": 0,
364
+ "fields": [
365
+ {
366
+ "name": "contiguousLength",
367
+ "type": "uint",
368
+ "version": 1
369
+ },
370
+ {
371
+ "name": "remoteContiguousLength",
372
+ "type": "uint",
373
+ "version": 1
374
+ },
375
+ {
376
+ "name": "recovering",
377
+ "type": "uint",
378
+ "version": 1
379
+ }
380
+ ]
381
+ },
382
+ {
383
+ "name": "session",
384
+ "namespace": "core",
385
+ "compact": true,
386
+ "flagsPosition": -1,
387
+ "fields": [
388
+ {
389
+ "name": "name",
390
+ "required": true,
391
+ "type": "string",
392
+ "version": 1
393
+ },
394
+ {
395
+ "name": "dataPointer",
396
+ "required": true,
397
+ "type": "uint",
398
+ "version": 1
399
+ }
400
+ ]
401
+ },
402
+ {
403
+ "name": "sessions",
404
+ "namespace": "core",
405
+ "array": true,
406
+ "type": "@core/session"
407
+ },
408
+ {
409
+ "name": "dependency",
410
+ "namespace": "core",
411
+ "compact": true,
412
+ "flagsPosition": -1,
413
+ "fields": [
414
+ {
415
+ "name": "dataPointer",
416
+ "required": true,
417
+ "type": "uint",
418
+ "version": 1
419
+ },
420
+ {
421
+ "name": "length",
422
+ "required": true,
423
+ "type": "uint",
424
+ "version": 1
425
+ }
426
+ ]
427
+ },
428
+ {
429
+ "name": "head-v2",
430
+ "namespace": "corestore",
431
+ "compact": false,
432
+ "flagsPosition": 3,
433
+ "fields": [
434
+ {
435
+ "name": "cores",
436
+ "required": true,
437
+ "type": "uint",
438
+ "version": 1
439
+ },
440
+ {
441
+ "name": "datas",
442
+ "required": true,
443
+ "type": "uint",
444
+ "version": 1
445
+ },
446
+ {
447
+ "name": "groups",
448
+ "required": true,
449
+ "type": "uint",
450
+ "version": 1
451
+ },
452
+ {
453
+ "name": "seed",
454
+ "type": "fixed32",
455
+ "version": 1
456
+ },
457
+ {
458
+ "name": "defaultDiscoveryKey",
459
+ "type": "fixed32",
460
+ "version": 1
461
+ }
462
+ ]
463
+ },
464
+ {
465
+ "name": "head",
466
+ "namespace": "corestore",
467
+ "versions": [
468
+ {
469
+ "type": "@corestore/head-v1",
470
+ "map": "headLegacyMap",
471
+ "version": 1
472
+ },
473
+ {
474
+ "type": "@corestore/head-v2",
475
+ "map": null,
476
+ "version": 2
477
+ }
478
+ ]
479
+ }
480
+ ]
481
+ }
package/index.js CHANGED
@@ -4,9 +4,10 @@ const ScopeLock = require('scope-lock')
4
4
  const DeviceFile = require('device-file')
5
5
  const path = require('path')
6
6
  const fs = require('fs')
7
+
7
8
  const View = require('./lib/view.js')
8
9
 
9
- const VERSION = 1
10
+ const VERSION = 2
10
11
  const COLUMN_FAMILY = 'corestore'
11
12
 
12
13
  const { store, core } = require('./lib/keys.js')
@@ -22,7 +23,8 @@ const {
22
23
  createUserDataStream,
23
24
  createTreeNodeStream,
24
25
  createMarkStream,
25
- createLocalStream
26
+ createLocalStream,
27
+ createGroupUpdateStream
26
28
  } = require('./lib/streams.js')
27
29
 
28
30
  const EMPTY = new View()
@@ -647,6 +649,10 @@ class CorestoreStorage {
647
649
  await require('./migrations/0').store(this, target)
648
650
  break
649
651
  }
652
+ case 1: {
653
+ // implicit migration on write
654
+ break
655
+ }
650
656
  default: {
651
657
  throw new Error(
652
658
  'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
@@ -738,7 +744,7 @@ class CorestoreStorage {
738
744
  try {
739
745
  const head = await this._getHead(view)
740
746
 
741
- dataPointer = head.allocated.datas++
747
+ dataPointer = head.datas++
742
748
 
743
749
  tx.setHead(head)
744
750
  tx.apply()
@@ -801,6 +807,10 @@ class CorestoreStorage {
801
807
  return createDiscoveryKeyStream(this.db, EMPTY, namespace)
802
808
  }
803
809
 
810
+ createGroupUpdateStream(group, { since, reverse } = {}) {
811
+ return createGroupUpdateStream(this.db, EMPTY, group, { gte: since, reverse })
812
+ }
813
+
804
814
  async getAlias(alias) {
805
815
  if (this.version === 0) await this._migrateStore()
806
816
 
@@ -822,6 +832,15 @@ class CorestoreStorage {
822
832
  return head === null ? null : head.seed
823
833
  }
824
834
 
835
+ async getGroup(topic) {
836
+ if (this.version === 0) await this._migrateStore()
837
+
838
+ const rx = new CorestoreRX(this.db, EMPTY)
839
+ const groupPromise = rx.getGroup(topic)
840
+ rx.tryFlush()
841
+ return groupPromise
842
+ }
843
+
825
844
  async setSeed(seed, { overwrite = true } = {}) {
826
845
  if (this.version === 0) await this._migrateStore()
827
846
 
@@ -1079,8 +1098,8 @@ class CorestoreStorage {
1079
1098
  if (head === null) head = initStoreHead()
1080
1099
  if (head.defaultDiscoveryKey === null) head.defaultDiscoveryKey = discoveryKey
1081
1100
 
1082
- const corePointer = ptrs ? ptrs.corePointer : head.allocated.cores++
1083
- const dataPointer = ptrs ? ptrs.dataPointer : head.allocated.datas++
1101
+ const corePointer = ptrs ? ptrs.corePointer : head.cores++
1102
+ const dataPointer = ptrs ? ptrs.dataPointer : head.datas++
1084
1103
 
1085
1104
  core = { version: VERSION, corePointer, dataPointer, alias }
1086
1105
 
@@ -1122,6 +1141,51 @@ class CorestoreStorage {
1122
1141
  }
1123
1142
  }
1124
1143
 
1144
+ // not allowed to throw validation errors as its a shared tx!
1145
+ async _createGroup(view, topic) {
1146
+ const rx = new CorestoreRX(this.db, view)
1147
+ const tx = new CorestoreTX(view)
1148
+
1149
+ const groupPromise = rx.getGroup(topic)
1150
+ const headPromise = rx.getHead()
1151
+
1152
+ rx.tryFlush()
1153
+
1154
+ let group = await groupPromise
1155
+ if (group !== null) {
1156
+ return {
1157
+ key: topic,
1158
+ pointer: group
1159
+ }
1160
+ }
1161
+
1162
+ let head = await headPromise
1163
+ if (head === null) head = initStoreHead()
1164
+
1165
+ group = head.groups++
1166
+
1167
+ tx.putGroup(topic, group)
1168
+ tx.setHead(head)
1169
+ tx.apply()
1170
+
1171
+ return {
1172
+ key: topic,
1173
+ pointer: group
1174
+ }
1175
+ }
1176
+
1177
+ async createGroup(topic) {
1178
+ if (this.version === 0) await this._migrateStore()
1179
+
1180
+ const view = await this._enter()
1181
+
1182
+ try {
1183
+ return await this._createGroup(view, topic)
1184
+ } finally {
1185
+ await this._exit()
1186
+ }
1187
+ }
1188
+
1125
1189
  static isParentStorage(storage, parent) {
1126
1190
  return HypercoreStorage.isParentStorage(storage, parent)
1127
1191
  }
@@ -1132,10 +1196,9 @@ module.exports = CorestoreStorage
1132
1196
  function initStoreHead() {
1133
1197
  return {
1134
1198
  version: 0, // cause we wanna run the migration
1135
- allocated: {
1136
- datas: 0,
1137
- cores: 0
1138
- },
1199
+ datas: 0,
1200
+ cores: 0,
1201
+ groups: 0,
1139
1202
  seed: null,
1140
1203
  defaultDiscoveryKey: null
1141
1204
  }
package/lib/keys.js CHANGED
@@ -7,11 +7,14 @@ const TL_CORE_BY_DKEY = 1
7
7
  const TL_CORE_BY_ALIAS = 2
8
8
  const TL_CORE = 3
9
9
  const TL_DATA = 4
10
+ const TL_GROUP_BY_TOPIC = 5
11
+ const TL_GROUP = 6
10
12
 
11
- const TL_END = TL_DATA + 1
13
+ const TL_END = TL_GROUP + 1
12
14
 
13
15
  const CORE_AUTH = 0
14
16
  const CORE_SESSIONS = 1
17
+ const CORE_GROUP = 2
15
18
 
16
19
  const DATA_HEAD = 0
17
20
  const DATA_DEPENDENCY = 1
@@ -27,6 +30,7 @@ const slab = { buffer: b4a.allocUnsafe(65536), start: 0, end: 0 }
27
30
 
28
31
  const store = {}
29
32
  const core = {}
33
+ const group = {}
30
34
 
31
35
  store.clear = function () {
32
36
  const state = alloc()
@@ -139,6 +143,15 @@ core.auth = function (ptr) {
139
143
  return state.buffer.subarray(start, state.start)
140
144
  }
141
145
 
146
+ core.group = function (ptr) {
147
+ const state = alloc()
148
+ const start = state.start
149
+ UINT.encode(state, TL_CORE)
150
+ UINT.encode(state, ptr)
151
+ UINT.encode(state, CORE_GROUP)
152
+ return state.buffer.subarray(start, state.start)
153
+ }
154
+
142
155
  core.sessions = function (ptr) {
143
156
  const state = alloc()
144
157
  const start = state.start
@@ -300,7 +313,25 @@ core.localKey = function (buffer) {
300
313
  return state.buffer.subarray(state.start, state.end)
301
314
  }
302
315
 
303
- module.exports = { store, core }
316
+ group.topic = function (topic) {
317
+ const state = alloc()
318
+ const start = state.start
319
+ UINT.encode(state, TL_GROUP_BY_TOPIC)
320
+ c.fixed32.encode(state, topic)
321
+ return state.buffer.subarray(start, state.start)
322
+ }
323
+
324
+ group.entry = function (group, timestamp, ptr) {
325
+ const state = alloc()
326
+ const start = state.start
327
+ UINT.encode(state, TL_GROUP)
328
+ UINT.encode(state, group)
329
+ c.uint64be.encode(state, timestamp)
330
+ UINT.encode(state, ptr)
331
+ return state.buffer.subarray(start, state.start)
332
+ }
333
+
334
+ module.exports = { store, core, group }
304
335
 
305
336
  function alloc() {
306
337
  if (slab.buffer.byteLength - slab.start < 4096) {
package/lib/streams.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const b4a = require('b4a')
2
2
  const BlockDependencyStream = require('./block-dependency-stream.js')
3
- const { core, store } = require('./keys.js')
4
- const schema = require('../spec/hyperschema')
3
+ const { core, store, group } = require('./keys.js')
4
+ const schema = require('../encoding/spec/hyperschema')
5
5
 
6
6
  const CORESTORE_CORE = schema.getEncoding('@corestore/core')
7
7
  const CORE_TREE_NODE = schema.getEncoding('@core/tree-node')
@@ -16,7 +16,8 @@ module.exports = {
16
16
  createDiscoveryKeyStream,
17
17
  createTreeNodeStream,
18
18
  createMarkStream,
19
- createLocalStream
19
+ createLocalStream,
20
+ createGroupUpdateStream
20
21
  }
21
22
 
22
23
  function createCoreStream(db, view) {
@@ -49,6 +50,16 @@ function createAliasStream(db, view, namespace) {
49
50
  return ite
50
51
  }
51
52
 
53
+ function createGroupUpdateStream(db, view, groupID, { gte = 0, reverse = true } = {}) {
54
+ const start = group.entry(groupID, gte, 0)
55
+ const end = group.entry(groupID + 1, 0, 0)
56
+
57
+ const ite = view.iterator(db, start, end, reverse)
58
+
59
+ ite._readableState.map = mapGroupUpdate
60
+ return ite
61
+ }
62
+
52
63
  function createBlockIterator(ptr, db, view, start, end, reverse) {
53
64
  if (ptr.dependencies.length > 0) {
54
65
  return new BlockDependencyStream(ptr, db, view, start, end, reverse)
@@ -164,6 +175,10 @@ function mapLocal(data) {
164
175
  return { key, value: data.value }
165
176
  }
166
177
 
178
+ function mapGroupUpdate(data) {
179
+ return data.value
180
+ }
181
+
167
182
  function mapUserData(data) {
168
183
  const key = core.userDataKey(data.key)
169
184
  return { key, value: data.value }
package/lib/tx.js CHANGED
@@ -1,13 +1,17 @@
1
- const schema = require('../spec/hyperschema')
2
- const { store, core } = require('./keys.js')
3
- const View = require('./view.js')
4
1
  const b4a = require('b4a')
2
+ const c = require('compact-encoding')
5
3
  const flat = require('flat-tree')
6
4
 
5
+ const schema = require('../encoding/spec/hyperschema')
6
+ const { store, core, group } = require('./keys.js')
7
+ const View = require('./view.js')
8
+
7
9
  const CORESTORE_HEAD = schema.getEncoding('@corestore/head')
8
10
  const CORESTORE_CORE = schema.getEncoding('@corestore/core')
11
+ const CORESTORE_GROUP = c.uint
9
12
 
10
13
  const CORE_AUTH = schema.getEncoding('@core/auth')
14
+ const CORE_GROUP = schema.getEncoding('@core/group')
11
15
  const CORE_SESSIONS = schema.getEncoding('@core/sessions')
12
16
  const CORE_HEAD = schema.getEncoding('@core/head')
13
17
  const CORE_TREE_NODE = schema.getEncoding('@core/tree-node')
@@ -43,6 +47,14 @@ class CoreTX {
43
47
  this.changes.push([core.dependency(this.core.dataPointer), encode(CORE_DEPENDENCY, dep), null])
44
48
  }
45
49
 
50
+ setGroup(group) {
51
+ this.changes.push([core.group(this.core.dataPointer), encode(CORE_GROUP, group), null])
52
+ }
53
+
54
+ deleteGroup() {
55
+ this.changes.push([core.group(this.core.dataPointer), null, null])
56
+ }
57
+
46
58
  setHints(hints) {
47
59
  this.changes.push([core.hints(this.core.dataPointer), encode(CORE_HINTS, hints), null])
48
60
  }
@@ -140,6 +152,14 @@ class CoreTX {
140
152
  ])
141
153
  }
142
154
 
155
+ putGroupUpdate(groupPtr, timestamp, key) {
156
+ this.changes.push([group.entry(groupPtr, timestamp, this.core.corePointer), key, null])
157
+ }
158
+
159
+ deleteGroupUpdate(groupPtr, timestamp) {
160
+ this.changes.push([group.entry(groupPtr, timestamp, this.core.corePointer), null, null])
161
+ }
162
+
143
163
  flush() {
144
164
  const changes = this.changes
145
165
  if (changes === null) return Promise.resolve(!this.view)
@@ -194,6 +214,13 @@ class CoreRX {
194
214
  )
195
215
  }
196
216
 
217
+ async getGroup() {
218
+ return await decode(
219
+ CORE_GROUP,
220
+ await this.view.get(this.read, core.group(this.core.corePointer))
221
+ )
222
+ }
223
+
197
224
  static async getHints(db, c) {
198
225
  return await decode(CORE_HINTS, await db.get(core.hints(c.dataPointer)))
199
226
  }
@@ -276,6 +303,14 @@ class CorestoreTX {
276
303
  this.changes.push([store.coreByAlias(alias), discoveryKey, null])
277
304
  }
278
305
 
306
+ putGroup(topic, groupPtr) {
307
+ this.changes.push([group.topic(topic), encode(CORESTORE_GROUP, groupPtr), null])
308
+ }
309
+
310
+ deleteGroup(topic) {
311
+ this.changes.push([group.topic(topic), null, null])
312
+ }
313
+
279
314
  clear() {
280
315
  const [start, end] = store.clear()
281
316
  this.changes.push([start, null, end])
@@ -296,6 +331,10 @@ class CorestoreRX {
296
331
  view.readStart()
297
332
  }
298
333
 
334
+ async getHeadRaw(db) {
335
+ return await this.view.get(this.read, store.head())
336
+ }
337
+
299
338
  async getHead() {
300
339
  return decode(CORESTORE_HEAD, await this.view.get(this.read, store.head()))
301
340
  }
@@ -308,6 +347,10 @@ class CorestoreRX {
308
347
  return this.view.get(this.read, store.coreByAlias(alias))
309
348
  }
310
349
 
350
+ async getGroup(topic) {
351
+ return decode(CORESTORE_GROUP, await this.view.get(this.read, group.topic(topic)))
352
+ }
353
+
311
354
  tryFlush() {
312
355
  this.read.tryFlush()
313
356
  this._free()
@@ -364,14 +364,14 @@ const dataUpgrade = {
364
364
  c.uint.preencode(state, u.length)
365
365
  nodeArray.preencode(state, u.nodes)
366
366
  nodeArray.preencode(state, u.additionalNodes)
367
- c.buffer.preencode(state, u.signature)
367
+ c.optionalBuffer.preencode(state, u.signature)
368
368
  },
369
369
  encode (state, u) {
370
370
  c.uint.encode(state, u.start)
371
371
  c.uint.encode(state, u.length)
372
372
  nodeArray.encode(state, u.nodes)
373
373
  nodeArray.encode(state, u.additionalNodes)
374
- c.buffer.encode(state, u.signature)
374
+ c.optionalBuffer.encode(state, u.signature)
375
375
  },
376
376
  decode (state) {
377
377
  return {
@@ -379,7 +379,7 @@ const dataUpgrade = {
379
379
  length: c.uint.decode(state),
380
380
  nodes: nodeArray.decode(state),
381
381
  additionalNodes: nodeArray.decode(state),
382
- signature: c.buffer.decode(state)
382
+ signature: c.optionalBuffer.decode(state)
383
383
  }
384
384
  }
385
385
  }
@@ -404,18 +404,18 @@ const dataSeek = {
404
404
  const dataBlock = {
405
405
  preencode (state, b) {
406
406
  c.uint.preencode(state, b.index)
407
- c.buffer.preencode(state, b.value)
407
+ c.optionalBuffer.preencode(state, b.value)
408
408
  nodeArray.preencode(state, b.nodes)
409
409
  },
410
410
  encode (state, b) {
411
411
  c.uint.encode(state, b.index)
412
- c.buffer.encode(state, b.value)
412
+ c.optionalBuffer.encode(state, b.value)
413
413
  nodeArray.encode(state, b.nodes)
414
414
  },
415
415
  decode (state) {
416
416
  return {
417
417
  index: c.uint.decode(state),
418
- value: c.buffer.decode(state) || EMPTY,
418
+ value: c.optionalBuffer.decode(state) || EMPTY,
419
419
  nodes: nodeArray.decode(state)
420
420
  }
421
421
  }
@@ -633,16 +633,16 @@ wire.extension = {
633
633
  const keyValue = {
634
634
  preencode (state, p) {
635
635
  c.string.preencode(state, p.key)
636
- c.buffer.preencode(state, p.value)
636
+ c.optionalBuffer.preencode(state, p.value)
637
637
  },
638
638
  encode (state, p) {
639
639
  c.string.encode(state, p.key)
640
- c.buffer.encode(state, p.value)
640
+ c.optionalBuffer.encode(state, p.value)
641
641
  },
642
642
  decode (state) {
643
643
  return {
644
644
  key: c.string.decode(state),
645
- value: c.buffer.decode(state)
645
+ value: c.optionalBuffer.decode(state)
646
646
  }
647
647
  }
648
648
  }
@@ -652,20 +652,20 @@ const treeUpgrade = {
652
652
  c.uint.preencode(state, u.fork)
653
653
  c.uint.preencode(state, u.ancestors)
654
654
  c.uint.preencode(state, u.length)
655
- c.buffer.preencode(state, u.signature)
655
+ c.optionalBuffer.preencode(state, u.signature)
656
656
  },
657
657
  encode (state, u) {
658
658
  c.uint.encode(state, u.fork)
659
659
  c.uint.encode(state, u.ancestors)
660
660
  c.uint.encode(state, u.length)
661
- c.buffer.encode(state, u.signature)
661
+ c.optionalBuffer.encode(state, u.signature)
662
662
  },
663
663
  decode (state) {
664
664
  return {
665
665
  fork: c.uint.decode(state),
666
666
  ancestors: c.uint.decode(state),
667
667
  length: c.uint.decode(state),
668
- signature: c.buffer.decode(state)
668
+ signature: c.optionalBuffer.decode(state)
669
669
  }
670
670
  }
671
671
  }
@@ -737,17 +737,17 @@ oplog.entry = {
737
737
 
738
738
  const keyPair = {
739
739
  preencode (state, kp) {
740
- c.buffer.preencode(state, kp.publicKey)
741
- c.buffer.preencode(state, kp.secretKey)
740
+ c.optionalBuffer.preencode(state, kp.publicKey)
741
+ c.optionalBuffer.preencode(state, kp.secretKey)
742
742
  },
743
743
  encode (state, kp) {
744
- c.buffer.encode(state, kp.publicKey)
745
- c.buffer.encode(state, kp.secretKey)
744
+ c.optionalBuffer.encode(state, kp.publicKey)
745
+ c.optionalBuffer.encode(state, kp.secretKey)
746
746
  },
747
747
  decode (state) {
748
748
  return {
749
- publicKey: c.buffer.decode(state),
750
- secretKey: c.buffer.decode(state)
749
+ publicKey: c.optionalBuffer.decode(state),
750
+ secretKey: c.optionalBuffer.decode(state)
751
751
  }
752
752
  }
753
753
  }
@@ -795,21 +795,21 @@ const treeHeader = {
795
795
  preencode (state, t) {
796
796
  c.uint.preencode(state, t.fork)
797
797
  c.uint.preencode(state, t.length)
798
- c.buffer.preencode(state, t.rootHash)
799
- c.buffer.preencode(state, t.signature)
798
+ c.optionalBuffer.preencode(state, t.rootHash)
799
+ c.optionalBuffer.preencode(state, t.signature)
800
800
  },
801
801
  encode (state, t) {
802
802
  c.uint.encode(state, t.fork)
803
803
  c.uint.encode(state, t.length)
804
- c.buffer.encode(state, t.rootHash)
805
- c.buffer.encode(state, t.signature)
804
+ c.optionalBuffer.encode(state, t.rootHash)
805
+ c.optionalBuffer.encode(state, t.signature)
806
806
  },
807
807
  decode (state) {
808
808
  return {
809
809
  fork: c.uint.decode(state),
810
810
  length: c.uint.decode(state),
811
- rootHash: c.buffer.decode(state),
812
- signature: c.buffer.decode(state)
811
+ rootHash: c.optionalBuffer.decode(state),
812
+ signature: c.optionalBuffer.decode(state)
813
813
  }
814
814
  }
815
815
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "2.8.0",
3
+ "version": "3.0.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
7
7
  "lib/*.js",
8
- "spec/hyperschema/*.js",
8
+ "encoding/*",
9
9
  "migrations/0/*.js"
10
10
  ],
11
11
  "scripts": {
@@ -41,11 +41,11 @@
41
41
  "b4a": "^1.6.7",
42
42
  "bare-fs": "^4.0.1",
43
43
  "bare-path": "^3.0.0",
44
- "compact-encoding": "^2.16.0",
44
+ "compact-encoding": "^3.1.0",
45
45
  "device-file": "^2.1.2",
46
46
  "flat-tree": "^1.12.1",
47
47
  "hypercore-crypto": "^3.4.2",
48
- "hyperschema": "^1.7.0",
48
+ "hyperschema": "^1.21.0",
49
49
  "index-encoder": "^3.3.2",
50
50
  "resolve-reject-promise": "^1.0.0",
51
51
  "rocksdb-native": "^3.11.0",