From 8077af0f4003fd6bb435754df646063a5d6e755a Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 12 Oct 2022 15:44:53 +0200 Subject: [PATCH] Make tanglePrune work --- lib/add-group-tangle.js | 4 ++-- lib/tangle-prune.js | 5 ++--- test/tangle-prune.test.js | 13 ++++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/add-group-tangle.js b/lib/add-group-tangle.js index b3e50a3..cdb36fb 100644 --- a/lib/add-group-tangle.js +++ b/lib/add-group-tangle.js @@ -5,6 +5,7 @@ const { isCloakedMsg } = require('ssb-ref') const set = require('lodash.set') const GetGroupTangle = require('./get-group-tangle') +const tanglePrune = require('./tangle-prune') module.exports = function AddGroupTangle(server) { const getGroupTangle = GetGroupTangle(server) @@ -26,8 +27,7 @@ module.exports = function AddGroupTangle(server) { if (err) return cb(null, content) set(content, 'tangles.group', tangle) - //TODO: uncomment - //tanglePrune(content) // prune the group tangle down if needed + tanglePrune(content) // prune the group tangle down if needed cb(null, content) }) diff --git a/lib/tangle-prune.js b/lib/tangle-prune.js index aed2d03..d183c99 100644 --- a/lib/tangle-prune.js +++ b/lib/tangle-prune.js @@ -16,7 +16,8 @@ const MAX_SIZE_1_recps = 6041 module.exports = function tanglePrune(content) { const tangle = 'group' - const maxSize = content.recps > 1 ? MAX_SIZE_16_recps : MAX_SIZE_1_recps + const maxSize = + content.recps.length > 1 ? MAX_SIZE_16_recps : MAX_SIZE_1_recps if (getLength(content) <= maxSize) return content content.tangles[tangle].previous = content.tangles[tangle].previous.sort(() => @@ -36,7 +37,5 @@ module.exports = function tanglePrune(content) { } function getLength(obj) { - // stringify if on classic - //return JSON.stringify(obj).length return bipf.encodingLength(obj) } diff --git a/test/tangle-prune.test.js b/test/tangle-prune.test.js index 6e0c876..4d51601 100644 --- a/test/tangle-prune.test.js +++ b/test/tangle-prune.test.js @@ -3,13 +3,11 @@ // SPDX-License-Identifier: LGPL-3.0-only const test = require('tape') -const { promisify: p } = require('util') const bipf = require('bipf') const Testbot = require('./helpers/testbot') const tanglePrune = require('../lib/tangle-prune') const chars = 'abcABC123=+? '.split('') -//const encodedLength = (obj) => JSON.stringify(msgVal.content).length const encodedLength = (obj) => bipf.encodingLength(obj) const randomChar = () => chars.sort(() => (Math.random() < 0.5 ? -1 : +1))[0] const randomText = (length) => { @@ -18,7 +16,7 @@ const randomText = (length) => { return output } -test.only('tangle prune', async (t) => { +test('tangle prune', async (t) => { const ssb = Testbot() const ssbId = ssb.id @@ -87,6 +85,7 @@ test.only('tangle prune', async (t) => { const result = results.get(upper) || results.get(mid) || results.get(lower) t.pass(`max stringied content size for ${numberRecps} recps: ${result}`) + return result } const max16recps = await findMaxSize(16).catch(t.error) // 5546 const max1recp = await findMaxSize(1).catch(t.error) // 6041 @@ -96,7 +95,7 @@ test.only('tangle prune', async (t) => { const content = (prevCount, numRecps) => ({ type: 'post', text: 'hello!', - recps: [...new Array(numRecps).fill(ssbId)], + recps: new Array(numRecps).fill(ssbId), tangles: { group: { root: msgId, @@ -105,9 +104,9 @@ test.only('tangle prune', async (t) => { }, }) - // console.time('prune') - const result16 = tanglePrune(content(4000, 16), 'group') - // console.timeEnd('prune') + //console.time('prune') + const result16 = tanglePrune(content(4000, 16)) + //console.timeEnd('prune') t.true( encodedLength(result16) <= max16recps, `pruned ${4000 - result16.tangles.group.previous.length}`