Skip to content

Commit

Permalink
Fix adding messages with holes
Browse files Browse the repository at this point in the history
  • Loading branch information
arj03 committed Feb 14, 2020
1 parent 8ea176d commit 8dca895
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
10 changes: 8 additions & 2 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ exports.init = function (dir, ssbId) {
function validateAndAdd(msg, cb) {
const knownAuthor = msg.author in SSB.state.feeds
const earlierMessage = knownAuthor && msg.sequence < SSB.state.feeds[msg.author].sequence
const skippingMessages = knownAuthor && msg.sequence > SSB.state.feeds[msg.author].sequence + 1

if (!knownAuthor || earlierMessage)
if (!knownAuthor || earlierMessage || skippingMessages)
SSB.state = validate.appendOOO(SSB.state, hmac_key, msg)
else
SSB.state = validate.append(SSB.state, hmac_key, msg)
Expand Down Expand Up @@ -98,7 +99,12 @@ exports.init = function (dir, ssbId) {

if (ok) {
add(msg, cb)
} else {

if (skippingMessages)
store.last.setPartialLogState(msg.author, true)
}
else
{
if (updateLast)
store.last.setPartialLogState(msg.author, true)
cb()
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle-core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,31 @@ SSB.events.on('SSB: loaded', function() {
})
})

// we might get some messages from an earlier thread, and then get the latest 25 messages from the user
test('Add with holes', t => {
const validate = require('ssb-validate')
var state = validate.initial()
var keys = require('ssb-keys').generate()

state = validate.appendNew(state, null, keys, { type: 'post', text: 'test1' }, Date.now()) // ooo
state = validate.appendNew(state, null, keys, { type: 'post', text: 'test2' }, Date.now()) // missing
state = validate.appendNew(state, null, keys, { type: 'post', text: 'test3' }, Date.now()) // start

SSB.db.validateAndAdd(state.queue[0].value, (err) => {
if (err) console.error(err)

SSB.db.validateAndAdd(state.queue[2].value, (err, msg) => {
if (err) console.error(err)

t.equal(msg.value.content.text, 'test3', 'text correct')

const last = SSB.db.last.get()[keys.id]
t.equal(last.partial, true, 'is partial')
t.equal(last.sequence, 3, 'correct seq')

t.end()
})
})
})

})

0 comments on commit 8dca895

Please sign in to comment.