-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple-kernel.js
285 lines (258 loc) · 7.76 KB
/
simple-kernel.js
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
// Kernel includes
import { Repo } from 'picorepo'
import { Store } from '@telamon/picostore'
import { Feed, getPublicKey, s2b, toU8, au8 } from 'picofeed'
// import { encode, decode } from 'cborg'
import { SimpleRPC } from './simple-rpc.js'
const KEY_SK = s2b('reg/sk')
/**
* @typedef {import('picofeed').SecretBin} SecretBin
* @typedef {import('picofeed').PublicHex} PublicHex
* @typedef {import('picorepo').BinaryLevel} BinaryLevel
*/
/* This is a simple but complete pico-kernel,
* It sets up a user-identity, store and rpc.
*
* If you need something more advanced feel free to
* fork off and hack. <3
*/
export class SimpleKernel {
ready = false
/** @type {null|SecretBin} */
_secret = null
/** @type {BinaryLevel} */
db = null
/** @type {Store} */
store = null
/** @type {SimpleRPC} */
rpc = null
/**
* @param {BinaryLevel} db Datastore
* @param {{ secret?: SecretBin }} opts Options
*/
constructor (db, opts = {}) {
// Setup store
this.db = db
this.repo = new Repo(db)
this.store = new Store(this.repo, {
strategy: this.mergeStrategy.bind(this)
})
this.store.tap(this._onstoreevent.bind(this))
// this.store.mutexTimeout = 600000000
this._secret = opts.secret ? toU8(opts.secret) : null
// Setup network
this.rpc = new SimpleRPC({
onconnect: send => this.onconnect(send),
ondisconnect: peer => this.ondisconnect(peer),
onquery: (q, reply) => this.onquery(q, reply),
onblocks: feed => this.onblocks(feed)
})
}
/**
* Returns user's public key (same thing as userId)
* @returns {PublicHex}
*/
get pk () {
return getPublicKey(this._secret)
}
/**
* Generates/Restores user _secret and initializes store
* call boot after slice registrations and before
* network connections/dispatch.
*/
async boot () {
if (this.__loading) return this.__loading
/// Master Asynchr0nos was here
this.__loading = (async () => {
// If identity wasn't provided via opts.
if (!this._secret) {
try {
// Attempt to restore existing identity
this._secret = (await this.repo.readReg(KEY_SK) || null)
} catch (err) {
if (!err.notFound) throw err
}
}
// Fallback to generate new identity
if (!this._secret) {
const { sk } = Feed.signPair()
this._secret = au8(toU8(sk), 32)
await this.repo.writeReg(KEY_SK, sk)
}
await this.store.load() // load stores
this.ready = true
})()
return this.__loading
}
/**
* PicoRepo: Default merge strategy restricts feeds
* to only allow same-author blocks to be appended.
* Override this method if you need different behavior.
* @type {import('picorepo').MergeStrategy}
*/
async mergeStrategy (block, repo) {
return false
}
/**
* Returns user's feed
* @returns {Promise<Feed>}
*/
async feed (limit = undefined) {
this._checkReady()
return this.repo.loadHead(this.pk, limit)
}
_checkReady () {
if (!this.ready) throw new Error('Kernel not ready, did you await kernel.boot()?')
}
/**
* Returns user's feed current blockheight
*/
async seq () {
return 0 // await this.feed(1).seq
/* TODO: move to formal header
const feed = await this.feed(1)
if (!feed) return -1
return decode(feed.last.body).seq
*/
}
/**
* Mutates store and reduced state
* returns {string[]} names of stores that were modified by this action
*/
async dispatch (patch, loudFail = false) {
this._checkReady()
return await this.store.dispatch(patch, loudFail)
// TODO: this.store.tap(observer) taps into merged blocks, but there are tradeoffs... let's weight them.
// Transmit accepted blocks on all wires
// if (patch?.length) this.rpc.shareBlocks(patch)
// return patch
}
/** @typedef {import('@telamon/picostore').Memory} Memory
* @type {(name: string) => Memory} */
collection (name) {
if (!(name in this.store.roots)) throw new Error(`No such collection: ${name}, did you register it?`)
return this.store.roots[name]
}
/**
* Creates a new block on parent feed and dispatches it to store
*
* @param {string} root Name of store-collection
* @param {object} payload Block contents
* @param {Feed} [branch] Target feed, defaults to user's private feed.
* @returns {Promise<Feed>} patch
*/
async createBlock (root, payload, branch) {
this._checkReady() // Abort if not ready
// Use provided branch or fetch user's feed
// if that also fails then initialize a new empty Feed.
branch = branch || (await this.feed()) || new Feed()
// TODO: Move SEQ as an optional builtin feature in picofeed
// const seq = (await this.seq()) + 1 // Increment block sequence
const patch = await this.collection(root).createBlock(branch, payload, this._secret)
return patch
}
/**
* Forwards call to RPC,
* spawns a stateless Pico-net wire
*/
spawnWire () {
return this.rpc.spawnWire()
}
/**
* RPC: on Peer disconnect handler, override for custom behaviour
*/
ondisconnect (peer) {
// Stub
}
/**
* RPC: on Peer connect handler, override for custom behaviour
* (like a handshake or something.)
* The "wire-end" received contains two useful functions:
* wire.postMessage(data, replyExpected: boolean) // => Promise
* and
* wire.close() // disconnects the peer
*/
async onconnect (node) {
// Ask peer for blocks
await this.rpc.query(node)
}
/**
* RPC: when remote Peer asks us for blocks
* override friendly.
* Expected to return a Feed or array of Feeds
*/
async onquery (params, reply) {
// We'll just send the entire repo for now *shrug*
const feeds = []
if (this.repo.allowDetached) { // listFeeds()
const res = await this.repo.listFeeds()
for (const { value: chainId } of res) {
try { au8(chainId, 64) } catch {
console.error('ChainId borked', chainId)
continue
}
const feed = await this.repo.resolveFeed(chainId)
feeds.push(feed)
}
} else { // listHeads()
const heads = await this.repo.listHeads()
for (const { key } of heads) {
const f = await this.repo.loadHead(toU8(key))
if (f) feeds.push(f)
}
}
return feeds
}
/**
* RPC: when blocks are received
* we simply dispatch them to the store.
* They're 'mutations' =)
* Returns feed to be forwarded to all connected peers
*/
async onblocks (feed) {
const loudFail = false
if (this.store.closed) {
console.warn('Dropped feed due Store closed', feed)
return
}
const patch = await this.dispatch(feed, loudFail)
return patch
}
// Handles orphaned blocks by asking network
// for a sync-up
async _networkResolveFeed (feed, loudFail) {
// Except this is not how query works atm.
// const remote = await this.rpc.query({ resolve: feed.first.parentSig })
// if (!feed) return 0 // Give up
// remote.merge(feed)
// const mutated = await this.dispatch(remote, loudFail)
// return !!mutated.length
}
$connections () { return this.rpc.$connections }
async halt () {
for (const sink of this.rpc.hub._nodes) {
this.rpc.hub.disconnect(sink)
}
await this.store.close()
}
/**
* @deprecated,
* Returns block's type as a string
static typeOfBlock (body) {
return decode(body).type
}
*/
/**
* Recieves events such as change|merged
* We simply take all 'merged' blocks and forward them onto
* the wire.
* @type {(events: {event: string, payload: any}[]) => void}
*/
_onstoreevent (events) {
if (!events.length) return
const event = events.find(({ event }) => event === 'patch-merged')
if (event) {
this.rpc.shareBlocks(event.payload.patch)
}
}
}