-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.js
More file actions
executable file
·656 lines (599 loc) · 22.3 KB
/
DB.js
File metadata and controls
executable file
·656 lines (599 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/******************************************************************************
TYPES / SCHEMA
******************************************************************************/
// for more types, see: https://pastebin.com/5nAb6XHQ
// ============================================================================
// storage - append only log to assign un ique id's to entries
// ============================================================================
// 'DB.storage' // create, get, (set), (del)
// ============================================================================
// status - onchain status
// ============================================================================
// 'DB.status.idleHosters' // push, pop, set
// 'DB.status.idleHosters[x]' // get, splice/del
// 'DB.status.idleAttesters' // push, pop, set
// 'DB.status.idleAttesters[x]' // get, splice/del
// 'DB.status.idleEncoders' // push, pop, set
// 'DB.status.idleEncoders[x]' // get, splice/del
// ============================================================================
// status - onchain status
// ============================================================================
// 'DB.status.pendingAmendments' // push, pop, set // ammendments => contract.update // { fnName: 'makePerformanceChallenge', opts: contractID ) }
// 'DB.status.pendingAmendments[x]' // get, splice/del
// 'DB.status.attestersJobQueue' // size, add, take, peek, drop // { planID, feedID, set }
// ============================================================================
// active - offchain status
// ============================================================================
// 'DB.activeQueue.activePerformanceChallenges' // ???
// 'DB.activeQueue.activeStorageChallenges' // ???
// activeAmmendments (or contracts)
// ============================================================================
// query / lookup - immutable
// ============================================================================
// 'DB.lookups.userByAddress' // set, get
// 'DB.lookups.userIDByKey' // set, get // { key: id }
// 'DB.lookups.feedByKey' // set, get
// ============================================================================
// performance challenge:
// ============================================================================
// /* set, get */ `storage/:id/performance_challenge/id`
// /* set, get */ `storage/:id/performance_challenge/contract_id`
// /* set, get */ `storage/:id/performance_challenge/hoster_id`
// /* push, get, forEach */ `storage/:id/performance_challenge/attester/:id`
// // VS.
// /* set, get */ `storage/:id/performance_challenge/attester_ids`
// {
// id,
// contract: 'contractID',
// hosters // all from active amendment
// attester ['attesterID'],
// }
// ============================================================================
// storage challenge:
// ============================================================================
// /* set, get */ `storage/:id/storage_challenge/id`
// /* set, get */ `storage/:id/storage_challenge/contract_id`
// /* set, get */ `storage/:id/storage_challenge/hoster_id`
// /* set, get */ `storage/:id/storage_challenge/attester_id`
// /* push, get, forEach */ `storage/:id/storage_challenge/chunks` // = [1,4,6]
// {
// id,
// contract: 'contractID',
// hoster,
// attester,
// chunks: [1,4,6]
// }
// ============================================================================
// contract
// ============================================================================
// /* set, get */ `storage/:id/hosting/id`
// /* set, get */ `storage/:id/hosting/plan_id`
// /* set, get */ `storage/:id/hosting/feed_id`
// /* set, get */ `storage/:id/hosting/ranges`
// /* push, get */ `storage/:id/hosting/amendments`
// /* get */ `storage/:id/hosting/amendments/:id_x`
// /* set, get */ `storage/:id/hosting/status/active_hoster/:id_h`
// /* set, get */ `storage/:id/hosting/status/scheduler_id`
// {
// id,
// feed,
// plan: planID,
// ranges,
// amendments: [],
// status: {
// schedulerID: integer
// }
// }
// ============================================================================
// amendment
// ============================================================================
// /* set, get */ `storage/:id/amendment/id`
// /* set, get */ `storage/:id/amendment/contract_id`
// /* set, get, push */ `storage/:id/amendment/providers/encoders`
// /* set, get */ `storage/:id/amendment/providers/encoders/:id`
// /* set, get, push */ `storage/:id/amendment/providers/hosters`
// /* set, get */ `storage/:id/amendment/providers/hosters/:id`
// /* set, get, push */ `storage/:id/amendment/providers/attesters`
// /* set, get */ `storage/:id/amendment/providers/attesters/:id`
// {
// id,
// contract: contractID,
// providers: {
// encoders,
// hosters,
// attesters,
// },
// }
// ============================================================================
// user
// ============================================================================
// `storage/:id/user/id` // boolean `if (user[5]) const user = storage[5]`
// `storage/:id/user/noisekey` // ed2519 public key
// `storage/:id/user/signkey` // ed25519 public key
// `storage/:id/user/form/from`
// `storage/:id/user/form/until`
// `storage/:id/user/form/timing`
// `storage/:id/user/form/region`
// `storage/:id/user/form/performance`
// `storage/:id/user/form/resources`
// `storage/:id/user/jobs/hoster`
// `storage/:id/user/jobs/attester`
// `storage/:id/user/jobs/encoder`
// `storage/:id/user/status/capacity`
// `storage/:id/user/status/idleStorage`
// {
// id, 776
// key: noiseKey, // public noise key
// address: signer, //public chain key
// form: {
// components: {
// resources,
// performance,
// timings,
// region,
// },
// from : blockNow, // or new Date('Apr 30, 2000')
// until : untilBlock, // date
// timetable : [0, 1],
// region : 0,
// performance : 0,
// resources : 0,
// },
// jobs: {
// hoster: {},
// encoder: {},
// attester: {},
// }
// status: {
// capacity: 5 // based on resources needed for one job // getCapacity (jobType, job, userid)
// idleStorage,
// }
// }
// 'storage[15].type' //
// 'storage[15].hoster.jobs[5]' //
// ============================================================================
// feed:
// ============================================================================
// `storage/:id`
// `storage/:id/id`
// `storage/:id/publickey`
// `storage/:id/meta/signature/:version`
// `storage/:id/meta/hashType`
// `storage/:id/meta/children`
// {
// id: 1,
// publickey: 'key',
// meta: '{ signature, hashType, children }',
// }
// `feed/:id/publickey`
// `feed/:id/version/:v/` // signature
// `feed/:id/chunk/:v` // cummulative size for that version
// const feed = {
// publickey,
// version: {
// // short array
// // only one signature on feed publish + one per feed update
// 23: signature1 // feed length old
// 42: signature2 // feed length new
// },
// size: { // for nodes
// // multiple sizes for each storage proof get added
// 23: 64.05
// 24: 63.251
// 25: ...
// }
// }
// Encoder sends total size of all compressed chunks to attester
// attester send total compressed size and signatures to chain
// => attester submits all 6 signatures
// => two versions can calculate size for any range
// => one lookup per calculated hash in proofs
// => native api feed.getRoothashes() provides the values
// @NOTE: how to store sizes for proof hashes for chunks
// 1. random attester provides root signature to chain
// 2. try single random contract to get max X times failed hoster proof
// => to try new attester to get corrected root signature
// 3. if any hoster provides valid proof, start hosting all contracts
// 4. sizes are provided by hosters with proof and size is stored on chain
// 5. hoster can skip node sizes for nodes that were provided in the past
// 6. how to decide contract set sizes, given no 64kb chunk guarantee?
// => maybe host one contract with 10 chunks at a time
// =>
// => random attester will have
// ==> lots of work collecting sizes for e.g. 1 billion chunks
// BUT:
// per proof we will get correct sizes from hoster
// on average we need to make sure stored contracts are correct size
// => so we pay hosters appropriately
// => hoster knows what they store compressed
// => hosters might wanna decompress to verify proofs
// ==> but if they see problem, they cant say anything
// ==> if attester reports +1, hosters get bad grades
// ==> but attester doesnt know uncompressed size and doesnt verify proofs
// ==> encoders could provide crap data
// ==> but attester gets only paid when all hosters work
// ==> encoders too
// ====> so no incentive to cheat
// ====> encoders providing correct size + malicious error
// =====> would go undetected
// =====> attester has cached wrong data
// ======> would get switched out after X fails from all hosters
// =======> after X fails of attester we might need to replace encoders too
// =========> encoder has lots of power to slow down chain
// =========> let attester verify encoder data ( if they want )
// ==> if attester find out bug (or not)
// ==> they can forward to hoster
// ===> hoster has risk of:
// 1. encoder bugging and attester not checking
// 2. attester bugging
// => in both cases they have no fault
// ===> but if attester reports +1 hoster gets punished
// ===> better hoster verifies immediately and signs ok back to attester
// ====> so attester has to provide hoster failed or hoster signature
// attester cannot make hoster ok when hoster is not
// hosters verify encoders, by sending back signed total uncompressed size
// => attester compares
// => nobody knows what they save based on compression
// ==> but encoders & attesters do know, but how can we trust?
// ============================================================================
// plan:
// ============================================================================
// `storage/:id`
// `storage/:id/id`
// `storage/:id/sponsor_id`
// `storage/:id/from`
// `storage/:id/until` // until: time, budget, price...???
// `storage/:id/program`
// `storage/:id/status/contract_ids`
// `storage/:id/status/contract_ids/:c_id`
// {
// id,
// // sponsor_id: 'userID',
// components: { dataset, performances, timings, regions },
// from : blockNow, // or new Date('Apr 30, 2000')
// until : untilBlock, // date
// program : [
// {
// // TODO: if you publish a few local components (e.g. 5)
// // and you want to reference the global component with id=3
// // how to figure out if thats local ID or global ID
// // e.g. positive vs. negative numbers to differentiate
// dataset, // [0, 1]
// regions, // [0, 1]
// performance: 0,
// timetable, // [0]
// },
// { dataset, regions, performance, timetables },
// { dataset, regions, performance, timetables }
// ],
// status: {
// contract_ids: [],
// }
// }
// ----------------------------------------------------------------------
// `components`
// => allows transactions to include data
// => which will be stored on chain and given a unique id
// => so that that data can be referenced by the same or future transactions
// e.g. plan.performance
// 1. sort all keys alphabetically
// 2. e.g. stringify and hash to see if it already exists on chain
// 3. if YES: re-use id, if NO: store and make new id
// 4. alternative to hashing => store in trie
// ============================================================================
// plan - component - dataset
// ============================================================================
// `type/dataset/:id`
// `storage/:id`
// `storage/:id/id`
// `storage/:id/ranges/:i/:first`
// `storage/:id/ranges/:i/:last`
// const dataset = [{ id, ranges }]
// ============================================================================
// plan - component - performance
// ============================================================================
// `type/performance/:id`
// `storage/:id` // = { availability, bandwidth, latency }
// `storage/:id/availability`
// `storage/:id/bandwidth/ingress/speed`
// `storage/:id/bandwidth/ingress/guarantee`
// `storage/:id/bandwidth/egress/speed`
// `storage/:id/bandwidth/egress/guarantee`
// `storage/:id/latency/speed`
// `storage/:id/latency/guarantee`
// const performances = [{ // OPTIONAL
// availability: '', // percentage_decimal
// bandwidth: { /*'speed', 'guarantee'*/ }, // bitspersecond, percentage_decimal
// latency: { /*'lag', 'guarantee'*/ }, // milliseconds, percentage_decimal
// }]
// ============================================================================
// user - component - performance
// ============================================================================
// `storage/:id/user/form/components/performance`
// ============================================================================
// user - component - timetables
// ============================================================================
// `storage/:id/user/form/components/timing`
// ============================================================================
// plan - component - timing
// ============================================================================
// /* make, get */ `timing/:id` // = { delay, duration, pause, repeat }
// * we always store as a blob
// * it never gets updated
// * we always need all values (we read it always as a blob)
// * we always know from context (e.g. plan, form) that an id is a timing id
// * this format uses least amount of space and computation for the use case
// ----------------------------------------------
// BLOB STORAGE:
// 1. store timing ON CHAIN independently with a unique id
// /* make, get */ `storage/:id` // = { delay, duration, pause, repeat }
// leveldb.set('storage/5', { delay: 5, duration: 5, pause: 5, repeat: 5 })
// leveldb.set('storage/5', { delay: 15, duration: 5, pause: 5, repeat: 5 })
// const timing = leveldb.get('storage/5')
// 2. store as blob, because individual values are never needed in isolation
// PROs:
// * easier to store all
// * easier to read all
// * minimal storage space
// CONs:
// * more work to update individually <= never happens
// * more work to read individual keys <= never needed
//
// INDIVIDUAL STORAGE:
// `storage/:id/id`
// `storage/:id/delay`
// `storage/:id/duration`
// `storage/:id/pause`
// `storage/:id/repeat`
// leveldb.set('storage/5/delay', 5)
// leveldb.set('storage/5/duration', 5)
// leveldb.set('storage/5/pause', 5)
// leveldb.set('storage/5/repeat', 5)
// leveldb.set('storage/5/delay', 15)
// PROs:
// * easier to update individually <= never happens
// * easier to read individual keys <= never needed
// CONs:
// * more work to store all
// * more work to read all
//
// C: ALL by TYPE
// /* make, get */ `type/timing/:id` // true
// /* make, get */ `timing/:id` // true
// PROs:
// * easier to see all of given type
// CONs:
// * more work to see type of id
// * more storage on chain <= can be done in frontend instead
//
// // E: STORING + ALL BY TYPE
// `timing/:id` // = { delay, duration, pause, repeat }
// // PROs:
// // * easier to see all types
// // * easier to store all
// // * easier to read all
// // CONs:
// // * more work to see type of id
// // * more work to update individually
// // * more work to read individual keys
//
// // F: STORING + ALL BY TYPE
// `timing/:id/delay`
// `timing/:id/duration`
// `timing/:id/pause`
// `timing/:id/repeat`
// // PROs:
// // * easier to see all types
// // * easier to update individually
// // * easier to read individual keys
// // CONs:
// // * more work to see type of id
// // * more work to store all
// // * more work to read all
//
// D: LOOKUP TYPE
// `storage/:id/type` // 'timing'
// `type/:id` // 'timing'
// PROs:
// * easy to see type of id
// CONs:
// * more work to see all of given type
// ============================================================================
// user - component - resources
// ============================================================================
// `storage/:id/user/form/components/resources`
// TODO: decide later, once it is clear which resources we are tracking
// ============================================================================
// plan - component - regions
// ============================================================================
// `region/:id` = ['X3F', 'A0K']
// * we always store as a blob
// * it never gets updated
// * we always need all values (we read it always as a blob)
// * we always know from context (e.g. plan, form) that an id is a region id
// * this format uses least amount of space and computation for the use case
// ----------------------------------------------
// `type/regions/:id`
// `storage/:id`
// `storage/:id/id`
// `storage/:id/geohash`
// const regions = [
// ['X3F', 'A0K'],
// ['AX3F', 'A0TY'],
// ]
// TODO: solve later
// 1. are geo hashes the best format to store?
// 2. how can we make it easy to match hosters with plans to
// * minimize storage
// * minimize computation
// ============================================================================
// user - component - region
// ============================================================================
// `storage/:id/user/form/components/region`
// `region/:id` = ['X3F', 'A0K']
// hoster region is one hash
// plan regions is array of hashes
// matching process:
// 1. check if hoster hash included in plan hashes
// 2. check if hoster hash starts with plan hash
// 3. check if hoster have neighbour hashes to plan hash ?
//
// 1. hoster region is hoster hash + area of size X
// * e.g. X3FaaaF1 => area: X3FaaaF1 + radius of Y hashes
// 2. we match hoster hashes with plan hashes
// * select 3 hosters with the best match
// TODO: solve later
// 1. are geo hashes the best format to store?
// 2. how can we make it easy to match hosters with plans to
// * minimize storage
// * minimize computation
// 3. how to make hoster regions dynamic based on performance measurements
//
/*
1. a network of nodes
2. randomly ping nodes in the graph
1. update performance map every time a proof of performance is submitted
* PoP includes many attester stats
* PoP includes hoster stats
2. re-assign existing jobs to new best hosters maybe?
3.
*/
// SCENARIO
// const nodes = [
// { id: 1, location: 'XF3', lag: [
// [2, 0.3],
// [3, 0.3],
// [4, 0.3],
// ]}, { id: 2, location: 'AF3', lag: [
// [1, 0.3],
// [3, 0.3],
// [4, 0.3],
// ]}, { id: 3, location: 'XA3', lag: [
// [2, 0.3],
// [1, 0.3],
// [4, 0.3],
// ]}, { id: 4, location: 'XF3', lag: [
// [2, 0.3],
// [3, 0.3],
// [1, 0.3],
// ]}
// ]
// const nodes = [
// {
// id: 1, location: 'Berlin', lag: [
// ['South London', 0.1],
// ['East London', 0.3],
// ['Rome', 0.1],
// ]}, {
// id: 2, location: 'South London', lag: [
// ['Berlin'], 0.2],
// ['East London', 0.2],
// ['Rome', 0.3],
// ]}, {
// id: 3, location: 'East London', lag: [
// ['South London', 0.3],
// ['Berlin'], 0.5],
// ['Rome', 0.2],
// ]}, {
// id: 4, location: 'Rome', lag: [
// ['South London', 0.2],
// ['East London', 0.5],
// ['Berlin'], 0.1],
// ]}
// // ...
// ]
/******************************************************************************
STATE
******************************************************************************/
const storage = [] // append only log to assign unique id's to entries
/******************************************************************************
LOOKUP - IMMUTABLE
******************************************************************************/
const userByAddress = {} // address
const feedByKey = {
// { key: id }
}
const userIDByNoiseKey = {}
const userIDBySigningKey = {}
const lookups = { // for immutable components
userByAddress,
userIDBySigningKey,
userIDByNoiseKey,
feedByKey,
}
/******************************************************************************
ONCHAIN STATUS
******************************************************************************/
const idleHosters = [] // user ids
const idleEncoders = [] // user ids
const idleAttesters = [] // user ids
const status = {
idleHosters,
idleEncoders,
idleAttesters,
}
const tasks = [] // { fnName: 'makePerformanceChallenge', id ) }
const queues = {
tasks
}
// ----------------------------------------------------------------------------
// @NOTE:
// ...
// * do they need to be prioritized and/or grouped?
// * how should this be efficiently structured?
// * is `doesQualify` related? (what else?)
//
// => we want RANDOM for security
// => we want SPECIFIC for best quality of service
// - location
// makeX.js
// function make (opts) {
// return {
// push(type, peerID) {
// var arr = ALL[type]
// if (!arr) ALL[type] = []
// arr.push(peerID)
// }
// }
// }
// removeJobForRole
// giveJobToRoles
// tryNextChallenge
// select
// const opts = {}
// const idleHosters = make(opts) // should be a `new Set()`
// const idleEncoders = make(opts)
// const idleAttesters = make(opts)
// const idlePeers = make(opts)
// idlePeers.push('attester', peerID)
// idleHosters.push(peerID)
// const providers = idlehosters.select(jobID)
// idlehosters.select(jobID)
// idlehosters.select(jobID)
// const providers = {}
// ----------------------------------------------------------------------------
/******************************************************************************
OFFCHAIN STATUS
******************************************************************************/
const storageChallenges = {
/*
challengeID1: true,
challengeID_2: true,
...
*/
}
const performanceChallenges = {
/*
challengeID1: true,
challengeID_2: true,
...
*/
}
const active = {
// activeAmmendments (or contracts)
performanceChallenges,
storageChallenges
}
/*****************************************************************************/
const DB = { storage, lookups, active, queues, status }
module.exports = DB