@fireproof/core 0.5.11 → 0.5.13
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/dist/src/fireproof.d.ts +12 -11
- package/dist/src/fireproof.js +52 -29
- package/dist/src/fireproof.js.map +1 -1
- package/dist/src/fireproof.mjs +52 -29
- package/dist/src/fireproof.mjs.map +1 -1
- package/package.json +1 -1
- package/src/blockstore.js +2 -2
- package/src/sync.js +50 -27
package/package.json
CHANGED
package/src/blockstore.js
CHANGED
@@ -164,8 +164,8 @@ export class TransactionBlockstore {
|
|
164
164
|
if (doSync) {
|
165
165
|
// const all =
|
166
166
|
await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore).catch(e => {
|
167
|
-
console.error('sync error', e)
|
168
|
-
|
167
|
+
console.error('sync error, cancelling', e)
|
168
|
+
sync.destroy()
|
169
169
|
})))
|
170
170
|
}
|
171
171
|
}
|
package/src/sync.js
CHANGED
@@ -14,6 +14,7 @@ export class Sync {
|
|
14
14
|
* @memberof Sync
|
15
15
|
* @static
|
16
16
|
*/
|
17
|
+
status = 'new'
|
17
18
|
constructor (database, PeerClass = SimplePeer) {
|
18
19
|
this.database = database
|
19
20
|
this.database.blocks.syncs.add(this) // should this happen during setup?
|
@@ -29,6 +30,7 @@ export class Sync {
|
|
29
30
|
}
|
30
31
|
|
31
32
|
async offer () {
|
33
|
+
this.status = 'offering'
|
32
34
|
return this.setupPeer(true)
|
33
35
|
}
|
34
36
|
|
@@ -36,11 +38,13 @@ export class Sync {
|
|
36
38
|
const offer = JSON.parse(atob(base64offer))
|
37
39
|
const p = this.setupPeer(false)
|
38
40
|
this.peer.signal(offer)
|
41
|
+
this.status = 'accepting'
|
39
42
|
return p
|
40
43
|
}
|
41
44
|
|
42
45
|
connect (base64accept) {
|
43
46
|
const accept = JSON.parse(atob(base64accept))
|
47
|
+
this.status = 'connecting'
|
44
48
|
this.peer.signal(accept)
|
45
49
|
}
|
46
50
|
|
@@ -71,39 +75,46 @@ export class Sync {
|
|
71
75
|
// console.log('not a car', data.toString())
|
72
76
|
}
|
73
77
|
if (reader) {
|
78
|
+
console.log('got car')
|
79
|
+
this.status = 'parking car'
|
74
80
|
const blz = new Set()
|
75
81
|
for await (const block of reader.blocks()) {
|
76
82
|
blz.add(block)
|
77
83
|
}
|
78
84
|
const roots = await reader.getRoots()
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
console.log(
|
86
|
+
'got car',
|
87
|
+
roots.map(c => c.toString()),
|
88
|
+
this.database.clock.map(c => c.toString())
|
89
|
+
)
|
90
|
+
console.log(
|
91
|
+
'got blocks',
|
92
|
+
[...blz].map(({ cid }) => cid.toString())
|
93
|
+
)
|
88
94
|
// @ts-ignore
|
89
95
|
reader.entries = reader.blocks
|
90
|
-
await this.database.blocks.commit(
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
+
await this.database.blocks.commit(
|
97
|
+
{
|
98
|
+
label: 'sync',
|
99
|
+
entries: () => [...blz],
|
100
|
+
get: async cid => await reader.get(cid),
|
101
|
+
lastCid: [...blz][0].cid // doesn't matter
|
102
|
+
},
|
103
|
+
false
|
104
|
+
)
|
96
105
|
// first arg could be the roots parents?
|
97
106
|
// get the roots parents
|
98
|
-
const parents = await Promise.all(
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
+
const parents = await Promise.all(
|
108
|
+
roots.map(async cid => {
|
109
|
+
const rbl = await reader.get(cid)
|
110
|
+
if (!rbl) {
|
111
|
+
console.log('missing root block', cid.toString(), reader)
|
112
|
+
throw new Error('missing root block')
|
113
|
+
}
|
114
|
+
const block = await decodeEventBlock(rbl.bytes)
|
115
|
+
return block.value.parents
|
116
|
+
})
|
117
|
+
)
|
107
118
|
this.database.applyClock(parents.flat(), roots)
|
108
119
|
this.database.notifyReset()
|
109
120
|
// console.log('after', this.database.clockToJSON())
|
@@ -113,6 +124,7 @@ export class Sync {
|
|
113
124
|
const message = JSON.parse(data.toString())
|
114
125
|
// console.log('got message', message)
|
115
126
|
if (message.ok) {
|
127
|
+
this.status = 'ok'
|
116
128
|
this.pushBacklogResolve({ ok: true })
|
117
129
|
} else if (message.clock) {
|
118
130
|
const reqCidDiff = message
|
@@ -120,12 +132,14 @@ export class Sync {
|
|
120
132
|
console.log('got diff', reqCidDiff)
|
121
133
|
const carBlock = await Sync.makeCar(this.database, null, reqCidDiff.cids)
|
122
134
|
if (!carBlock) {
|
123
|
-
|
124
|
-
|
135
|
+
// we are full synced
|
136
|
+
// console.log('we are full synced')
|
137
|
+
this.status = 'full synced'
|
125
138
|
this.peer.send(JSON.stringify({ ok: true }))
|
126
139
|
// this.pushBacklogResolve({ ok: true })
|
127
140
|
} else {
|
128
|
-
|
141
|
+
console.log('do send', carBlock.bytes.length)
|
142
|
+
this.status = 'sending diff car'
|
129
143
|
this.peer.send(carBlock.bytes)
|
130
144
|
// this.pushBacklogResolve({ ok: true })
|
131
145
|
}
|
@@ -133,10 +147,18 @@ export class Sync {
|
|
133
147
|
}
|
134
148
|
}
|
135
149
|
|
150
|
+
destroy () {
|
151
|
+
this.database.blocks.syncs.delete(this)
|
152
|
+
this.status = 'destroyed'
|
153
|
+
this.peer.destroy()
|
154
|
+
}
|
155
|
+
|
136
156
|
async sendUpdate (blockstore) {
|
157
|
+
if (!this.peer) return
|
137
158
|
// console.log('send update from', this.database.instanceId)
|
138
159
|
// todo should send updates since last sync
|
139
160
|
const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore)
|
161
|
+
this.status = 'sending update car'
|
140
162
|
this.peer.send(newCar.bytes)
|
141
163
|
}
|
142
164
|
|
@@ -149,6 +171,7 @@ export class Sync {
|
|
149
171
|
cids: allCIDs.map(cid => cid.toString())
|
150
172
|
}
|
151
173
|
// console.log('send diff', reqCidDiff)
|
174
|
+
this.status = 'sending cid diff'
|
152
175
|
this.peer.send(JSON.stringify(reqCidDiff))
|
153
176
|
}
|
154
177
|
|