git ssb

2+

mixmix / ticktack



Commit 2266bb60f9e3162b169687e66a63363af534ece5

successful working backup restore using remote peers to confirm seq!

mix irving committed on 5/24/2018, 3:12:46 AM
Parent: 69fecb32f29fb953037e1e0b67db4d89bb1cab8b

Files changed

ftu/app.jschanged
package-lock.jsonchanged
package.jsonchanged
ssb-server-ticktack.jschanged
ftu/app.jsView
@@ -4,20 +4,25 @@
44 const fs = require('fs')
55 const { remote } = require('electron')
66 const insertCss = require('insert-css')
77 const values = require('lodash/values')
8+const get = require('lodash/get')
89 const electron = require('electron')
910 const { dialog } = require('electron').remote
1011 const os = require('os')
12+const progress = require('progress-string')
13+
1114 const appName = process.env.SSB_APPNAME || 'ssb'
1215 const configFolder = path.join(os.homedir(), `.${appName}`)
1316
1417 var isBusy = Value(false)
1518 var isPresentingOptions = Value(true)
19+var checkerTimeout
1620
1721 // these initial values are overwritten by the identity file.
1822 var state = Struct({
1923 latestSequence: 0,
24+ confirmedRemotely: false,
2025 currentSequence: -1
2126 })
2227
2328 exports.gives = nest('ftu.app')
@@ -49,19 +54,30 @@
4954 when(isBusy, busyMessage, actionButtons)
5055 ])
5156 ])
5257
53- var importProcess = h('Page -ftu', [
58+ var importProgress = h('Page -ftu', [
5459 h('div.content', [
5560 h('h1', strings.backup.import.header),
56- h('p', [strings.backup.import.synchronizeMessage, state.currentSequence, '/', state.latestSequence])
61+ h('p', [strings.backup.import.synchronizeMessage]),
62+ h('pre', computed(state, s => {
63+ return progress({
64+ width: 42,
65+ total: s.latestSequence,
66+ style: function (complete, incomplete) {
67+ // add an arrow at the head of the completed part
68+ return `${complete}>${incomplete} (${s.currentSequence}/ ${s.latestSequence})`
69+ }
70+ })(s.currentSequence)
71+ }))
5772 ])
5873 ])
5974
6075 // This watcher is responsible for switching from FTU to Ticktack main app
6176 watch(throttle(state, 500), s => {
62- if (s.currentSequence >= s.latestSequence) {
77+ if (s.currentSequence >= s.latestSequence && s.confirmedRemotely) {
6378 console.log('all imported')
79+ clearTimeout(checkerTimeout)
6480 electron.ipcRenderer.send('import-completed')
6581 }
6682 })
6783
@@ -87,9 +103,9 @@
87103 var app = h('App', [
88104 h('Header', [
89105 windowControls()
90106 ]),
91- when(isPresentingOptions, initialOptions, importProcess)
107+ when(isPresentingOptions, initialOptions, importProgress)
92108 ])
93109
94110 return app
95111 }
@@ -209,81 +225,74 @@
209225 function observeSequence () {
210226 const pull = require('pull-stream')
211227 const Client = require('ssb-client')
212228 const config = require('../config').create().config.sync.load()
213- const _ = require('lodash')
214229
215230 Client(config.keys, config, (err, ssbServer) => {
216- if (err) {
217- console.error('problem starting client', err)
218- } else {
219- console.log('> sbot running!!!!')
231+ if (err) return console.error('problem starting client', err)
220232
221- ssbServer.gossip.peers((err, data) => {
222- console.log('PEERS', err, data)
233+ console.log('> sbot running!!!!')
223234
224- data.forEach(peer => {
225- ssbServer.gossip.connect({
226- 'host': peer.host,
227- 'port': peer.port,
228- 'key': peer.key
229- }, function (err, v) {
230- console.log('connected to ', peer.host)
231- })
232- })
235+ ssbServer.gossip.peers((err, peers) => {
236+ if (err) return console.error(err)
237+
238+ connectToPeers(peers)
239+ checkPeers()
240+ })
241+
242+ // start listening to the my seq, and update the state
243+ pull(
244+ ssbServer.createUserStream({ live: true, id: ssbServer.id }),
245+ pull.drain((msg) => {
246+ let seq = get(msg, 'value.sequence', false)
247+ if (seq) {
248+ state.currentSequence.set(seq)
249+ }
233250 })
251+ )
234252
235- // ssbServer.gossip.connect({
236- // 'host': '128.199.76.241',
237- // 'port': 8008,
238- // 'key': '@7xMrWP8708+LDvaJrRMRQJEixWYp4Oipa9ohqY7+NyQ=.ed25519'
239- // }, function (err, v) {
240- // console.log('connected to ticktack 1', err, v)
241- // })
253+ function connectToPeers (peers) {
254+ if (peers.length > 10) {
255+ const lessPeers = peers.filter(p => !p.error)
256+ if (lessPeers.length > 10) peers = lessPeers
257+ }
242258
243- // ssbServer.gossip.connect({
244- // 'host': '138.68.27.255',
245- // 'port': 8008,
246- // 'key': '@MflVZCcOBOUe6BLrm/8TyirkTu9/JtdnIJALcd8v5bc=.ed25519'
247- // }, function (err, v) {
248- // console.log('connected to ticktack 2', err, v)
249- // })
259+ peers.forEach(({ host, port, key }) => {
260+ if (host && port && key) {
261+ ssbServer.gossip.connect({ host, port, key }, (err, v) => {
262+ if (err) console.log('error connecting to ', host, err)
263+ else console.log('connected to ', host)
264+ })
265+ }
266+ })
267+ }
268+ function checkPeers () {
269+ ssbServer.ebt.peerStatus(ssbServer.id, (err, data) => {
270+ if (err) {
271+ checkerTimeout = setTimeout(checkPeers, 5000)
272+ return
273+ }
250274
251- // ssbServer.gossip.connect({
252- // host: 'one.butt.nz',
253- // key: '@VJM7w1W19ZsKmG2KnfaoKIM66BRoreEkzaVm/J//wl8=.ed25519',
254- // port: 8008
255- // }, function (err, v) {
256- // console.log('connected to one.butt.nz', err, v)
257- // checkPeers()
258- // })
275+ const latest = resolve(state.latestSequence)
259276
260- function checkPeers () {
261- ssbServer.ebt.peerStatus(ssbServer.id, (err, data) => {
262- console.log('PEER STATUS:')
263- console.log(data.seq)
264- console.log(data.peers)
265- console.log('-------')
266- })
277+ const remoteSeqs = Object.keys(data.peers)
278+ .map(p => data.peers[p].seq) // get my seq reported by each peer
279+ .filter(s => s >= latest) // only keep remote seq that confirm or update backup seq
280+ .sort((a, b) => a > b ? -1 : 1) // order them
267281
268- setTimeout(checkPeers, 5000)
269- }
282+ console.log(remoteSeqs)
270283
271- var feedSource = ssbServer.createUserStream({
272- live: true,
273- id: ssbServer.id
274- })
284+ const newLatest = remoteSeqs[0]
285+ if (newLatest) {
286+ state.latestSequence.set(newLatest)
275287
276- var valueLogger = pull.drain((msg) => {
277- let seq = _.get(msg, 'value.sequence', false)
278- if (seq) {
279- state.currentSequence.set(seq)
288+ // if this value is confirmed remotely twice, assume safe
289+ if (remoteSeqs.filter(s => s === newLatest).length >= 2) {
290+ state.confirmedRemotely.set(true)
291+ }
280292 }
293+
294+ checkerTimeout = setTimeout(checkPeers, 5000)
281295 })
282-
283- pull(
284- feedSource,
285- valueLogger
286- )
287296 }
288297 })
289298 }
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 289973 bytes
New file size: 290240 bytes
package.jsonView
@@ -49,8 +49,9 @@
4949 "patch-profile": "^1.0.4",
5050 "patch-settings": "^1.1.2",
5151 "patch-suggest": "^1.1.0",
5252 "patchcore": "^1.23.3",
53+ "progress-string": "^1.2.2",
5354 "pull-defer": "^0.2.2",
5455 "pull-next": "^1.0.1",
5556 "pull-next-step": "^1.0.0",
5657 "pull-obv": "^1.3.0",
ssb-server-ticktack.jsView
@@ -191,9 +191,9 @@
191191 delete opts.limit
192192 // have to remove limit from the query otherwise Next stalls out if it doesn't get a new result
193193
194194 const _source = pull(
195- server.messagesByType(opts),
195+ server.messagesByType(opts), // TODO - check/ note why I didn't use e.g. readComments
196196 pull.filter(makeFilter(blogIds)),
197197 limit ? pull.take(limit) : true
198198 )
199199

Built with git-ssb-web