remove opts.tips as a FeedV1 API

This commit is contained in:
Andre Staltz 2023-04-07 15:27:30 +03:00
parent b4178b7420
commit 94ba7246e5
9 changed files with 6 additions and 74 deletions

View File

@ -40,7 +40,6 @@ const {
* @property {string} keys.id
* @property {string} keys.private
* @property {Iterator<Msg> & {values: () => Iterator<Msg>}} existing
* @property {Iterator<Msg> & {values: () => Iterator<Msg>}} 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 = {

View File

@ -50,19 +50,12 @@ exports.init = function initDB(peer, config) {
const msgsPerFeed = {
_mapAll: new Map(), // who => Set<MsgHash>
_mapTips: new Map(), // who => Set<MsgHash>
_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

View File

@ -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)

View File

@ -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)

View File

@ -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(

View File

@ -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,
})

View File

@ -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/,

View File

@ -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()

View File

@ -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]