Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add/publish so we wait for the state to be loaded #160

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 25 additions & 23 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,30 @@ exports.init = function (sbot, config) {
}

function rawAdd(msg, validated, cb) {
stateFeedsReady.promise.then(() => {
const id = getId(msg)
if (validated)
// ssb-validate makes sure things come in order
log.add(id, msg, cb)
else
get(id, (err, data) => {
if (data) cb(null, data)
else log.add(id, msg, cb)
})
})
const id = getId(msg)
if (validated)
// ssb-validate makes sure things come in order
log.add(id, msg, cb)
else
get(id, (err, data) => {
if (data) cb(null, data)
else log.add(id, msg, cb)
})
}

function add(msg, cb) {
const guard = guardAgainstDuplicateLogs('add()')
if (guard) return cb(guard)

try {
state = validate.append(state, hmac_key, msg)
if (state.error) return cb(state.error)
rawAdd(msg, true, cb)
} catch (ex) {
return cb(ex)
}
stateFeedsReady.promise.then(() => {
try {
state = validate.append(state, hmac_key, msg)
if (state.error) return cb(state.error)
rawAdd(msg, true, cb)
} catch (ex) {
return cb(ex)
}
})
}

function addOOO(msg, cb) {
Expand Down Expand Up @@ -196,11 +196,13 @@ exports.init = function (sbot, config) {
const guard = guardAgainstDuplicateLogs('publish()')
if (guard) return cb(guard)

state.queue = []
state = validate.appendNew(state, null, config.keys, msg, Date.now())
rawAdd(state.queue[0].value, true, (err, data) => {
post.set(data)
cb(err, data)
stateFeedsReady.promise.then(() => {
state.queue = []
state = validate.appendNew(state, null, config.keys, msg, Date.now())
rawAdd(state.queue[0].value, true, (err, data) => {
post.set(data)
cb(err, data)
})
})
}

Expand Down
52 changes: 49 additions & 3 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ mkdirp.sync(dir)

const keys = ssbKeys.loadOrCreateSync(path.join(dir, 'secret'))

const sbot = SecretStack({ appKey: caps.shs })
let sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../compat/ebt'))
.call(null, {
keys,
path: dir,
})
const db = sbot.db
let db = sbot.db

test('Base', (t) => {
const posts = []
Expand Down Expand Up @@ -157,11 +157,57 @@ test('delete all', (t) => {
(err, results) => {
t.error(err, 'no err')
t.equal(results.length, 0, 'gone')
sbot.close(t.end)
t.end()
staltz marked this conversation as resolved.
Show resolved Hide resolved
}
)
})
})
})
})
})

test('validate needs to load', (t) => {
const post = { type: 'post', text: 'Testing!' }
const post2 = { type: 'post', text: 'Testing 2!' }

db.onDrain(() => {
sbot.close(() => {
sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../compat/ebt'))
.call(null, {
keys,
path: dir,
})
db = sbot.db

// make sure we can post from cold boot
db.publish(post, (err, msg) => {
t.error(err, 'no err')

t.notEqual(msg.value.previous, null)

db.onDrain(() => {
sbot.close(() => {
// reload
sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../compat/ebt'))
.call(null, {
keys,
path: dir,
})
db = sbot.db

// make sure we have the correct previous
db.publish(post2, (err, msg2) => {
t.error(err, 'no err')
t.equal(msg.key, msg2.value.previous)
sbot.close(t.end)
})
})
})
})
})
})
})