-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ssbc/prune-yolo
Detect too big group tangle previous array by failing to publish, and prune it
- Loading branch information
Showing
6 changed files
with
109 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros <contact@staltz.com> | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
module.exports = function prunePublish(ssb, content, cb) { | ||
const group = content.tangles.group | ||
|
||
ssb.db.create({ content, encryptionFormat: 'box2' }, (err, msg) => { | ||
if (err) { | ||
if (group.previous.length > 1) { | ||
const halfLength = Math.ceil(group.previous.length / 2) | ||
group.previous = group.previous.slice(0, halfLength) | ||
|
||
return prunePublish(ssb, content, cb) | ||
} else { | ||
return cb(err) | ||
} | ||
} | ||
|
||
cb(null, msg) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-FileCopyrightText: 2022 Mix Irving | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
const test = require('tape') | ||
const Testbot = require('./helpers/testbot') | ||
const prunePublish = require('../lib/prune-publish') | ||
|
||
test('prune a message with way too big `previous`', (t) => { | ||
const ssb = Testbot() | ||
const ssbId = ssb.id | ||
|
||
ssb.tribes2.create(null, (err, group) => { | ||
t.error(err, 'no err on group create') | ||
|
||
const msgId = '%RDORgMCjmL6vs51nR4bn0LWNe6wkBfbRJulSdOJsmwg=.sha256' | ||
const content = (prevCount, numRecps) => ({ | ||
type: 'post', | ||
text: 'hello!', | ||
recps: [group.id, new Array(numRecps - 1).fill(ssbId)], | ||
tangles: { | ||
group: { | ||
root: msgId, | ||
previous: new Array(prevCount).fill(msgId), | ||
}, | ||
}, | ||
}) | ||
|
||
//console.time('prune') | ||
prunePublish(ssb, content(4000, 16), (err, encMsg16) => { | ||
//console.timeEnd('prune') | ||
ssb.db.get(encMsg16.key, (err, msg16) => { | ||
const msg16len = msg16.content.tangles.group.previous.length | ||
|
||
t.true( | ||
msg16len < 4000, | ||
`pruned ${4000 - msg16len} from 'previous', ${msg16len} remaining` | ||
) | ||
|
||
t.true(msg16len > 10, 'there are some previouses left') | ||
|
||
ssb.close(true, t.end) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test('publish many messages that might need pruning', (t) => { | ||
const n = 5000 | ||
const ssb = Testbot() | ||
|
||
const publishArray = new Array(n).fill().map((item, i) => i) | ||
|
||
ssb.tribes2.create(null, (err, group) => { | ||
const publishes = publishArray.map( | ||
(value) => | ||
new Promise((res, rej) => { | ||
ssb.tribes2.publish( | ||
{ type: 'potato', content: value, recps: [group.id] }, | ||
(err, msg) => { | ||
if (err) return rej(err) | ||
return res(msg) | ||
} | ||
) | ||
}) | ||
) | ||
|
||
//console.time('publish') | ||
Promise.all(publishes) | ||
.then(async () => { | ||
//console.timeEnd('publish') | ||
|
||
ssb.close(true, t.end) | ||
}) | ||
.catch(t.error) | ||
}) | ||
}) |