Skip to content

Commit

Permalink
Merge pull request #129 from ssbc/createNewEpoch
Browse files Browse the repository at this point in the history
extract createNewEpoch
  • Loading branch information
Powersource authored Jul 4, 2023
2 parents 565e6ae + da27eca commit 470736a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 63 deletions.
89 changes: 27 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ const {
addMember: isAddMember,
content: isContent,
excludeMember: isExcludeMember,
initEpoch: isInitEpoch,
},
},
keySchemes,
} = require('private-group-spec')
const { SecretKey } = require('ssb-private-group-keys')
const { fromMessageSigil, isBendyButtV1FeedSSBURI } = require('ssb-uri2')

const startListeners = require('./listeners')
Expand All @@ -39,7 +37,7 @@ const publishAndPrune = require('./lib/prune-publish')
const MetaFeedHelpers = require('./lib/meta-feed-helpers')
const Epochs = require('./lib/epochs')
const { groupRecp } = require('./lib/operators')
const { reAddMembers } = require('./lib/exclude')
const { createNewEpoch } = require('./lib/exclude')

module.exports = {
name: 'tribes2',
Expand Down Expand Up @@ -224,68 +222,35 @@ module.exports = {
if (cb === undefined)
return promisify(excludeMembers)(groupId, feedIds, opts)

ssb.metafeeds.findOrCreate(function gotRoot(err, myRoot) {
const excludeContent = {
type: 'group/exclude-member',
excludes: feedIds,
recps: [groupId],
}
const excludeOpts = {
tangles: ['members'],
isValid: isExcludeMember,
}
publish(excludeContent, excludeOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get own root when excluding members"))

const excludeContent = {
type: 'group/exclude-member',
excludes: feedIds,
recps: [groupId],
}
const excludeOpts = {
tangles: ['members'],
isValid: isExcludeMember,
}
publish(excludeContent, excludeOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to publish exclude msg'))
if (err) return cb(clarify(err, 'Failed to publish exclude msg'))

const newSecret = new SecretKey()
const addInfo = { key: newSecret.toBuffer() }

ssb.box2.addGroupInfo(groupId, addInfo, (err) => {
// prettier-ignore
if (opts?._newEpochCrash) return cb(new Error('Intentional crash before creating new epoch'))

createNewEpoch(
ssb,
groupId,
{
_reAddCrash: opts?._reAddCrash,
_reAddSkipMember: opts?._reAddSkipMember,
},
(err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't store new key when excluding members"))

const newKey = {
key: newSecret.toBuffer(),
scheme: keySchemes.private_group,
}
ssb.box2.pickGroupWriteKey(groupId, newKey, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't switch to new key for writing when excluding members"))

const newEpochContent = {
type: 'group/init',
version: 'v2',
secret: newSecret.toString('base64'),
tangles: {
members: { root: null, previous: null },
},
recps: [groupId, myRoot.id],
}
const newTangleOpts = {
tangles: ['epoch'],
isValid: isInitEpoch,
}
publish(newEpochContent, newTangleOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't post init msg on new epoch when excluding members"))

// prettier-ignore
if (opts?._reAddCrash) return cb(new Error('Intentional crash before re-adding members'))

reAddMembers(
ssb,
groupId,
{ _reAddSkipMember: opts?._reAddSkipMember },
cb
)
})
})
})
})
if (err) return cb(clarify(err, "Couldn't create new epoch and/or re-add members when excluding members"))
cb()
}
)
})
}

Expand Down
66 changes: 66 additions & 0 deletions lib/exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,74 @@

const chunk = require('lodash.chunk')
const pull = require('pull-stream')
const { SecretKey } = require('ssb-private-group-keys')
const clarify = require('clarify-error')
const {
validator: {
group: { initEpoch: isInitEpoch },
},
keySchemes,
} = require('private-group-spec')
const Epochs = require('./epochs')

function createNewEpoch(ssb, groupId, opts, cb) {
ssb.metafeeds.findOrCreate(function gotRoot(err, myRoot) {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't get own root when excluding members"))

const newSecret = new SecretKey()
const addInfo = { key: newSecret.toBuffer() }

ssb.box2.addGroupInfo(groupId, addInfo, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't store new key when excluding members"))

const newKey = {
key: newSecret.toBuffer(),
scheme: keySchemes.private_group,
}
ssb.box2.pickGroupWriteKey(groupId, newKey, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't switch to new key for writing when excluding members"))

const newEpochContent = {
type: 'group/init',
version: 'v2',
secret: newSecret.toString('base64'),
tangles: {
members: { root: null, previous: null },
},
recps: [groupId, myRoot.id],
}
const newTangleOpts = {
tangles: ['epoch'],
isValid: isInitEpoch,
}

ssb.tribes2.publish(newEpochContent, newTangleOpts, (err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't post init msg on new epoch when excluding members"))

// prettier-ignore
if (opts?._reAddCrash) return cb(new Error('Intentional crash before re-adding members'))

reAddMembers(
ssb,
groupId,
{ _reAddSkipMember: opts?._reAddSkipMember },
(err) => {
// prettier-ignore
if (err) return cb(clarify(err, "Couldn't re-add members when creating new epoch"))
cb()
}
)
})
})
})
})
}

/** idempotent */
function reAddMembers(ssb, groupId, opts, cb) {
const epochs = Epochs(ssb)

Expand Down Expand Up @@ -49,5 +114,6 @@ function reAddMembers(ssb, groupId, opts, cb) {
}

module.exports = {
createNewEpoch,
reAddMembers,
}
2 changes: 1 addition & 1 deletion test/exclude-members.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ test('On exclusion, recover if we fail to re-add anyone at all', async (t) => {
.catch((err) =>
t.equal(
err.message,
'Intentional crash before re-adding members',
"Couldn't create new epoch and/or re-add members when excluding members",
'alice excludes bob but crashes before re-adding herself and carol'
)
)
Expand Down

0 comments on commit 470736a

Please sign in to comment.