From 94ba7246e524a54e75baef6dcffc3092081ca8ee Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 7 Apr 2023 15:27:30 +0300 Subject: [PATCH] remove opts.tips as a FeedV1 API --- lib/feed-v1/index.js | 12 +++++------- lib/plugin.js | 21 +-------------------- test/add.test.js | 1 - test/create.test.js | 1 - test/feed-v1-encode.test.js | 2 -- test/feed-v1-invalid-prev.test.js | 11 ----------- test/feed-v1-invalid-type.test.js | 5 ----- test/feed-v1-lipmaa.test.js | 15 --------------- test/feed-v1-validate.test.js | 12 ------------ 9 files changed, 6 insertions(+), 74 deletions(-) diff --git a/lib/feed-v1/index.js b/lib/feed-v1/index.js index fec0ec9..5115c0e 100644 --- a/lib/feed-v1/index.js +++ b/lib/feed-v1/index.js @@ -40,7 +40,6 @@ const { * @property {string} keys.id * @property {string} keys.private * @property {Iterator & {values: () => Iterator}} existing - * @property {Iterator & {values: () => Iterator}} tips */ /** @@ -74,11 +73,11 @@ function toPlaintextBuffer(opts) { return Buffer.from(stringify(opts.content), 'utf8') } -function calculateDepth(tips) { +function calculateDepth(existing) { let max = -1 - for (const p of tips.values()) { - if (p.metadata.depth > max) { - max = p.metadata.depth + for (const msg of existing.values()) { + if (msg.metadata.depth > max) { + max = msg.metadata.depth } } return max + 1 @@ -147,10 +146,9 @@ function create(opts) { let err if ((err = validateType(opts.type))) throw err prevalidatePrevious(opts.existing, 'existing') - prevalidatePrevious(opts.tips, 'tips') const [proof, size] = representContent(opts.content) - const depth = calculateDepth(opts.tips) + const depth = calculateDepth(opts.existing) const lipmaaDepth = lipmaa(depth + 1) - 1 const prev = calculatePrev(opts.existing, depth, lipmaaDepth) const msg = { diff --git a/lib/plugin.js b/lib/plugin.js index f2b0362..e91ed7e 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -50,19 +50,12 @@ exports.init = function initDB(peer, config) { const msgsPerFeed = { _mapAll: new Map(), // who => Set - _mapTips: new Map(), // who => Set _byHash: new Map(), // msgId => Msg // TODO: optimize space usage of this?? update(msg, msgId) { const msgHash = FeedV1.getMsgHash(msgId ?? msg) const feedId = FeedV1.getFeedId(msg) const setAll = this._mapAll.get(feedId) ?? new Set() - const setTips = this._mapTips.get(feedId) ?? new Set() - for (const p of msg.metadata.prev) { - setTips.delete(p) - } setAll.add(msgHash) - setTips.add(msgHash) - this._mapTips.set(feedId, setTips) this._mapAll.set(feedId, setAll) this._byHash.set(msgHash, msg) }, @@ -74,21 +67,11 @@ exports.init = function initDB(peer, config) { } return map }, - getTips(feedId) { - const map = new Map() - for (const msgHash of this._mapTips.get(feedId) ?? []) { - const msg = this._byHash.get(msgHash) - if (msg) map.set(msgHash, msg) - } - return map - }, deleteMsg(msg) { const feedId = FeedV1.getFeedId(msg) const msgHash = FeedV1.getMsgHash(msg) const setAll = this._mapAll.get(feedId) setAll.delete(msgHash) - const setTips = this._mapTips.get(feedId) - setTips.delete(msgHash) this._byHash.delete(msgHash) }, } @@ -238,7 +221,6 @@ exports.init = function initDB(peer, config) { when: Date.now(), ...opts, existing: [], - tips: [], keys, }) } catch (err) { @@ -246,8 +228,7 @@ exports.init = function initDB(peer, config) { } const feedId = FeedV1.getFeedId(tempMsg) const existing = msgsPerFeed.getAll(feedId) - const tips = msgsPerFeed.getTips(feedId) - const fullOpts = { when: Date.now(), ...opts, existing, tips, keys } + const fullOpts = { when: Date.now(), ...opts, existing, keys } // If opts ask for encryption, encrypt and put ciphertext in opts.content const recps = fullOpts.content.recps diff --git a/test/add.test.js b/test/add.test.js index 1a04e5e..b1b9dfc 100644 --- a/test/add.test.js +++ b/test/add.test.js @@ -26,7 +26,6 @@ test('add()', async (t) => { type: 'post', content: { text: 'This is the first post!' }, existing: [], - tips: [], }) const rec = await p(peer.db.add)(inputMsg) diff --git a/test/create.test.js b/test/create.test.js index 0c04d0b..44f5883 100644 --- a/test/create.test.js +++ b/test/create.test.js @@ -49,7 +49,6 @@ test('add() forked then create() merged', async (t) => { type: 'post', content: { text: '3rd post forked from 1st' }, existing: [rec1.msg], - tips: [rec1.msg], }) const rec3 = await p(peer.db.add)(msg3) diff --git a/test/feed-v1-encode.test.js b/test/feed-v1-encode.test.js index 065a2b8..38c908a 100644 --- a/test/feed-v1-encode.test.js +++ b/test/feed-v1-encode.test.js @@ -12,7 +12,6 @@ tape('encode/decode works', (t) => { content, type: 'post', existing: [], - tips: [], when, }) t.deepEquals( @@ -51,7 +50,6 @@ tape('encode/decode works', (t) => { content: content2, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: when + 1, }) t.deepEquals( diff --git a/test/feed-v1-invalid-prev.test.js b/test/feed-v1-invalid-prev.test.js index 04d2d2d..523bf75 100644 --- a/test/feed-v1-invalid-prev.test.js +++ b/test/feed-v1-invalid-prev.test.js @@ -11,7 +11,6 @@ tape('invalid 1st msg with non-empty prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), - tips: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), when: 1652030001000, }) @@ -34,7 +33,6 @@ tape('invalid 1st msg with non-array prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) msg.metadata.prev = null @@ -54,7 +52,6 @@ tape('invalid msg with non-array prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -64,7 +61,6 @@ tape('invalid msg with non-array prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), - tips: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), when: 1652030002000, }) msg2.metadata.prev = null @@ -90,7 +86,6 @@ tape('invalid msg with bad prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -100,7 +95,6 @@ tape('invalid msg with bad prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), - tips: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), when: 1652030002000, }) msg2.metadata.prev = [1234] @@ -126,7 +120,6 @@ tape('invalid msg with URI in prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -136,7 +129,6 @@ tape('invalid msg with URI in prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), - tips: new Map([['1234', { metadata: { depth: 10 }, sig: 'fake' }]]), when: 1652030002000, }) const randBuf = Buffer.alloc(16).fill(16) @@ -164,7 +156,6 @@ tape('invalid msg with unknown prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -174,7 +165,6 @@ tape('invalid msg with unknown prev', (t) => { content: { text: 'Alien' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const unknownMsgHash = FeedV1.getMsgHash(unknownMsg) @@ -184,7 +174,6 @@ tape('invalid msg with unknown prev', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[unknownMsgHash, unknownMsg]]), - tips: new Map([[unknownMsgHash, unknownMsg]]), when: 1652030002000, }) diff --git a/test/feed-v1-invalid-type.test.js b/test/feed-v1-invalid-type.test.js index 648f41c..f366fbf 100644 --- a/test/feed-v1-invalid-type.test.js +++ b/test/feed-v1-invalid-type.test.js @@ -13,7 +13,6 @@ tape('invalid type not a string', (t) => { when: 1652037377204, type: 123, existing: new Map(), - tips: new Map(), }) }, /type is not a string/, @@ -33,7 +32,6 @@ tape('invalid type with "/" character', (t) => { when: 1652037377204, type: 'group/init', existing: new Map(), - tips: new Map(), }) }, /invalid type/, @@ -53,7 +51,6 @@ tape('invalid type with "*" character', (t) => { when: 1652037377204, type: 'star*', existing: new Map(), - tips: new Map(), }) }, /invalid type/, @@ -73,7 +70,6 @@ tape('invalid type too short', (t) => { when: 1652037377204, type: 'xy', existing: new Map(), - tips: new Map(), }) }, /shorter than 3/, @@ -93,7 +89,6 @@ tape('invalid type too long', (t) => { when: 1652037377204, type: 'a'.repeat(120), existing: new Map(), - tips: new Map(), }) }, /100\+ characters long/, diff --git a/test/feed-v1-lipmaa.test.js b/test/feed-v1-lipmaa.test.js index 9b9787b..f5d3bb4 100644 --- a/test/feed-v1-lipmaa.test.js +++ b/test/feed-v1-lipmaa.test.js @@ -7,19 +7,16 @@ tape('lipmaa prevs', (t) => { const content = { text: 'Hello world!' } const when = 1652037377204 const existing = new Map() - const tips = new Map() const msg1 = FeedV1.create({ keys, content, type: 'post', existing: new Map(), - tips: new Map(), when: when + 1, }) const msgHash1 = FeedV1.getMsgHash(msg1) existing.set(msgHash1, msg1) - tips.set(msgHash1, msg1) t.deepEquals(msg1.metadata.prev, [], 'msg1.prev is empty') const msg2 = FeedV1.create({ @@ -27,13 +24,10 @@ tape('lipmaa prevs', (t) => { content, type: 'post', existing, - tips, when: when + 2, }) const msgHash2 = FeedV1.getMsgHash(msg2) existing.set(msgHash2, msg2) - tips.set(msgHash2, msg2) - tips.delete(msgHash1) t.deepEquals(msg2.metadata.prev, [msgHash1], 'msg2.prev is msg1') const msg3 = FeedV1.create({ @@ -41,13 +35,10 @@ tape('lipmaa prevs', (t) => { content, type: 'post', existing, - tips, when: when + 3, }) const msgHash3 = FeedV1.getMsgHash(msg3) existing.set(msgHash3, msg3) - tips.set(msgHash3, msg3) - tips.delete(msgHash2) t.deepEquals(msg3.metadata.prev, [msgHash2], 'msg3.prev is msg2') const msg4 = FeedV1.create({ @@ -55,13 +46,10 @@ tape('lipmaa prevs', (t) => { content, type: 'post', existing, - tips, when: when + 4, }) const msgHash4 = FeedV1.getMsgHash(msg4) existing.set(msgHash4, msg4) - tips.set(msgHash4, msg4) - tips.delete(msgHash3) t.deepEquals( msg4.metadata.prev, [msgHash1, msgHash3], @@ -73,13 +61,10 @@ tape('lipmaa prevs', (t) => { content, type: 'post', existing, - tips, when: when + 5, }) const msgHash5 = FeedV1.getMsgHash(msg5) existing.set(msgHash5, msg5) - tips.set(msgHash5, msg5) - tips.delete(msgHash4) t.deepEquals(msg5.metadata.prev, [msgHash4], 'msg5.prev is msg4') t.end() diff --git a/test/feed-v1-validate.test.js b/test/feed-v1-validate.test.js index 9e56285..ff36551 100644 --- a/test/feed-v1-validate.test.js +++ b/test/feed-v1-validate.test.js @@ -11,7 +11,6 @@ tape('validate 1st msg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) @@ -30,7 +29,6 @@ tape('validate 2nd msg with existing nativeMsg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -40,7 +38,6 @@ tape('validate 2nd msg with existing nativeMsg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030002000, }) @@ -62,7 +59,6 @@ tape('validate 2nd msg with existing msgId', (t) => { type: 'post', prev: [], existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -72,7 +68,6 @@ tape('validate 2nd msg with existing msgId', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030002000, }) @@ -93,7 +88,6 @@ tape('validate 2nd msg with existing KVT', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -103,7 +97,6 @@ tape('validate 2nd msg with existing KVT', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030002000, }) @@ -124,7 +117,6 @@ tape('validate 2nd forked msg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -134,7 +126,6 @@ tape('validate 2nd forked msg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030002000, }) const msgHash2A = FeedV1.getMsgHash(msg2A) @@ -144,7 +135,6 @@ tape('validate 2nd forked msg', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030003000, }) @@ -166,7 +156,6 @@ tape('invalid msg with unknown previous', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map(), - tips: new Map(), when: 1652030001000, }) const msgHash1 = FeedV1.getMsgHash(msg1) @@ -178,7 +167,6 @@ tape('invalid msg with unknown previous', (t) => { content: { text: 'Hello world!' }, type: 'post', existing: new Map([[msgHash1, msg1]]), - tips: new Map([[msgHash1, msg1]]), when: 1652030002000, }) msg2.metadata.prev = [fakeMsgKey1]