Skip to content

Commit

Permalink
Merge branch 'master' into tangle-prune
Browse files Browse the repository at this point in the history
  • Loading branch information
Powersource authored Oct 12, 2022
2 parents 8077af0 + c4dcfd1 commit dc4e7bf
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 112 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ module.exports = {
ecmaVersion: 2019,
sourceType: 'module',
},
rules: {},
rules: {
'no-console': ['error', { allow: ['error'] }],
},
}
37 changes: 16 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,11 @@ module.exports = {
addGroupTangle(content, (err, content) => {
if (err) return cb(err)

ssb.db.create(
{ content, recps, encryptionFormat: 'box2' },
(err, msg) => {
if (err) return cb(err)
ssb.db.create({ content, encryptionFormat: 'box2' }, (err, msg) => {
if (err) return cb(err)

cb(null, msg)
}
)
cb(null, msg)
})
})
}

Expand All @@ -196,7 +193,7 @@ module.exports = {
live({ old: true }),
toPullStream()
),
pull.drain((msg) => {
pull.asyncMap((msg, cb) => {
// TODO: call ssb-db2 reindexEncrypted

if (lodashGet(msg, 'value.content.recps', []).includes(ssb.id)) {
Expand All @@ -205,7 +202,12 @@ module.exports = {
const groupId = lodashGet(msg, 'value.content.recps[0]')

ssb.box2.getGroupKeyInfo(groupId, (err, info) => {
if (err) throw err
if (err) {
return console.error(
'Error when finding group invite for me:',
err
)
}

if (!info) {
// we're not already in the group
Expand All @@ -214,21 +216,14 @@ module.exports = {
root: groupRoot,
})
}
return cb()
})
} else {
return cb()
}
})
}),
pull.drain(() => {})
)

//pull(
// ssb.db.query(live({ old: true }), toPullStream()),
// pull.drain((msg) => {
// console.log('i got any kind of message', {
// author: msg.value.author,
// seq: msg.value.sequence,
// myId: ssb.id,
// })
// })
//)
}

return {
Expand Down
6 changes: 0 additions & 6 deletions lib/get-group-tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ module.exports = function GetGroupTangle(server) {
previous: Object.keys(reduce.state),
})
})
//)
})
}
}
Expand All @@ -60,9 +59,6 @@ const B_ROOT = Buffer.from('root')

function GetUpdates(ssb) {
const { seekKey } = require('bipf')
//const { spec } = crut
//const isUpdate = (msg) =>
// crut.isUpdate(getCanonicalContent(msg, spec.getTransformation))
const B_TANGLE = Buffer.from('group')

function tangleRoot(value) {
Expand All @@ -87,8 +83,6 @@ function GetUpdates(ssb) {
const { where, and, toPullStream } = ssb.db.operators
pull(
ssb.db.query(where(and(tangleRoot(id))), toPullStream()),
//TODO: do we want this?
//pull.filter(isUpdate),
pull.collect(cb)
)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"secret-stack": "^6.4.1",
"ssb-caps": "^1.1.0",
"ssb-db2": "^6.2.3",
"ssb-ebt": "^9.1.0",
"ssb-ebt": "^9.1.2",
"tap-arc": "^0.3.4",
"tape": "^5.5.3"
},
Expand Down
24 changes: 16 additions & 8 deletions test/add-member.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ test('add member', async (t) => {

const authorIds = [newPerson.id, ssbKeys.generate().id]

let invite = await kaitiaki.tribes2.addMembers(group.id, authorIds, {
text: 'welcome friends',
})
const encryptedInvite = await kaitiaki.tribes2.addMembers(
group.id,
authorIds,
{
text: 'welcome friends',
}
)

invite = await p(kaitiaki.db.get)(invite.key)
const invite = await p(kaitiaki.db.get)(encryptedInvite.key)

const expected = {
type: 'group/add-member',
Expand All @@ -72,12 +76,14 @@ test('add member', async (t) => {
recps: [group.id, ...authorIds],

tangles: {
group: { root: group.root },
group: {
root: group.root,
// we don't know the key of the last message, that was the admin adding themselves
previous: invite.content.tangles.group.previous,
},
members: { root: group.root, previous: [group.root] },
},
}
// we don't know the key of the last message, that was the admin adding themselves
expected.tangles.group.previous = invite.content.tangles.group.previous
t.deepEqual(invite.content, expected, 'kaitiaki sent invite')

/* kaitiaki posts to group, new person can read */
Expand Down Expand Up @@ -113,5 +119,7 @@ test('add member', async (t) => {
t.fail(err)
}

kaitiaki.close(true, () => newPerson.close(true))
await new Promise((resolve) => {
kaitiaki.close(true, () => newPerson.close(true, resolve))
})
})
15 changes: 4 additions & 11 deletions test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,17 @@ test('create more', (t) => {

// this is more of an integration test over the api
server.tribes2.create({}, (err, data) => {
if (err) throw err
t.error(err, 'no error')

const { id, secret, root } = data
t.true(ref.isCloakedMsg(id), 'returns group identifier - groupId')
t.true(
Buffer.isBuffer(secret) && secret.length === 32,
'returns group symmetric key - groupKey'
)
//TODO: can we get the root msg unencrypted?
//t.match(
// groupInitMsg.value.content,
// /^[a-zA-Z0-9/+]+=*\.box2$/,
// 'encrypted init msg'
//)

server.db.get(root, (err, value) => {
if (err) throw err
t.error(err, 'no error')

t.deepEqual(
value.content,
Expand All @@ -81,7 +75,7 @@ test('create more', (t) => {
server.db.query(where(author(server.id)), descending(), toPullStream()),
pull.map((msg) => msg.value.content),
pull.collect((err, msgContents) => {
if (err) throw err
t.error(err, 'no error')

t.deepEqual(
msgContents[0], // contents of the latest message
Expand All @@ -104,8 +98,7 @@ test('create more', (t) => {
},
'The admin was was also added to the group'
)
server.close()
t.end()
server.close(true, t.end)
})
)
})
Expand Down
96 changes: 39 additions & 57 deletions test/get-group-tangle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ test('get-group-tangle unit test', (t) => {
const server = Testbot({ name })

server.tribes2.create(null, (err, group) => {
if (err) throw err
t.error(err, 'no error')

const getGroupTangle = GetGroupTangle(server)

getGroupTangle(group.id, (err, groupTangle) => {
if (err) throw err
t.error(err, 'no error')

const { root, previous } = groupTangle
const rootKey = group.root
Expand All @@ -31,7 +31,7 @@ test('get-group-tangle unit test', (t) => {
pull.map((m) => m.key),
pull.take(1),
pull.collect((err, keys) => {
if (err) throw err
t.error(err, 'no error')

t.deepEqual(
{ root, previous },
Expand All @@ -48,21 +48,21 @@ test('get-group-tangle unit test', (t) => {
}

server.tribes2.publish(content, (err, msg) => {
if (err) throw err
t.error(err, 'no error')

getGroupTangle(group.id, (err, { root, previous }) => {
if (err) throw err
t.error(err, 'no error')
t.deepEqual(
{ root, previous },
{ root: rootKey, previous: [msg.key] },
'adding message to root'
)

server.tribes2.publish(content, (err, msg) => {
if (err) throw err
t.error(err, 'no error')

getGroupTangle(group.id, (err, { root, previous }) => {
if (err) throw err
t.error(err, 'no error')
t.deepEqual(
{ root, previous },
{ root: rootKey, previous: [msg.key] },
Expand All @@ -85,7 +85,7 @@ test(`get-group-tangle-${n}-publishes`, (t) => {
const server = Testbot()
let count = 0
server.tribes2.create(null, (err, data) => {
if (err) throw err
t.error(err, 'no error creating group')

const groupId = data.id
pull(
Expand All @@ -101,9 +101,8 @@ test(`get-group-tangle-${n}-publishes`, (t) => {
count += m.value.content.tangles.group.previous.length
},
(err) => {
t.error(err, 'no error')
t.error(err, 'no error publishing')

// t.equal(count, n, 'We expect there to be no branches in our groupTangle')
t.true(
count < n * 8,
'We expect bounded branching with fast publishing'
Expand All @@ -117,56 +116,39 @@ test(`get-group-tangle-${n}-publishes`, (t) => {
})

test('get-group-tangle', (t) => {
const tests = [
{
plan: 4,
test: (t) => {
const DESCRIPTION = 'auto adds group tangle'
// this is an integration test, as get-group-tangle is used in ssb.tribes2.publish
const ssb = Testbot()
// this is an integration test, as get-group-tangle is used in ssb.tribes2.publish
const ssb = Testbot()

ssb.tribes2.create(null, (err, data) => {
t.error(err, 'create group')
ssb.tribes2.create(null, (err, data) => {
t.error(err, 'create group')

const groupRoot = data.root
const groupId = data.id
const groupRoot = data.root
const groupId = data.id

const content = {
type: 'yep',
recps: [groupId],
}
const content = {
type: 'yep',
recps: [groupId],
}

ssb.tribes2.publish(content, (err, msg) => {
t.error(err, 'publish a message')
ssb.db.onMsgAdded((lastMsgAfterCreate) => {
ssb.tribes2.publish(content, (err, msg) => {
t.error(err, 'publish a message')

ssb.db.get(msg.key, (err, A) => {
t.error(err, 'get that message back')
ssb.db.get(msg.key, (err, A) => {
t.error(err, 'get that message back')

//TODO: this is confusing, why is previous supposed to be groupRoot? because right after creating the group we add ourselves to the group, which creates another message, so that message should be the previous
//t.deepEqual(
// A.content.tangles.group, // actual
// { root: groupRoot, previous: [groupRoot] }, // expected
// DESCRIPTION + ' (auto added tangles.group)'
//)
//TODO: remove
t.equal(1, 1)
t.deepEqual(
A.content.tangles.group, // actual
// last message is the admin adding themselves to the group they just created i.e. not the root msg
{ root: groupRoot, previous: [lastMsgAfterCreate.kvt.key] }, // expected
'auto adds group tangle (auto added tangles.group)'
)

ssb.close()
})
})
ssb.close(true, t.end)
})
},
},
]

const toRun = tests.reduce((acc, round) => {
acc += round.plan || 1
return acc
}, 0)

t.plan(toRun)

tests.forEach((round) => round.test(t))
})
})
})
})

test('get-group-tangle with branch', (t) => {
Expand All @@ -177,7 +159,7 @@ test('get-group-tangle with branch', (t) => {

// Alice creates a group
alice.tribes2.create(null, (err, group) => {
if (err) throw err
t.error(err, 'no error')

const getAliceGroupTangle = GetGroupTangle(alice)
const getBobGroupTangle = GetGroupTangle(bob)
Expand All @@ -194,9 +176,9 @@ test('get-group-tangle with branch', (t) => {

// Both servers should see the same group tangle
getAliceGroupTangle(group.id, (err, aliceTangle) => {
if (err) throw err
t.error(err, 'no error')
getBobGroupTangle(group.id, (err, bobTangle) => {
if (err) throw err
t.error(err, 'no error')
t.deepEqual(aliceTangle, bobTangle, 'tangles should match')
t.deepEqual(aliceTangle.root, group.root, 'the root is the groupId')
t.deepEqual(
Expand All @@ -216,12 +198,12 @@ test('get-group-tangle with branch', (t) => {
t.error(err, 'alice publishes a new message')

bob.tribes2.publish(content(), async (err) => {
if (err) throw err
t.error(err, 'no error')
// Then Bob shares his message with Alice
await replicate(bob, alice)
// There should now be a branch in Alice's group tangle
getAliceGroupTangle(group.id, (err, aliceTangle) => {
if (err) throw err
t.error(err, 'no error')

t.deepEqual(
aliceTangle.previous.length,
Expand Down
Loading

0 comments on commit dc4e7bf

Please sign in to comment.