remove metadata.when from FeedV1

This commit is contained in:
Andre Staltz 2023-04-17 16:45:52 +03:00
parent d2023b5bc0
commit faa7b13c92
13 changed files with 10 additions and 70 deletions

View File

@ -44,7 +44,6 @@ function isEmptyObject(obj) {
* @property {Record<string, TangleMetadata>} metadata.tangles
* @property {string} metadata.type
* @property {string} metadata.who
* @property {number} metadata.when
* @property {string} sig
*/
@ -58,7 +57,6 @@ function isEmptyObject(obj) {
* @typedef {Object} CreateOpts
* @property {*} content
* @property {string} type
* @property {number} when
* @property {Keys} keys
* @property {Record<string, Tangle>} tangles
*/
@ -143,7 +141,6 @@ function create(opts) {
tangles,
type: opts.type,
who: stripAuthor(opts.keys.id),
when: +opts.when,
},
sig: '',
}
@ -176,7 +173,6 @@ function createRoot(keys, type) {
tangles: {},
type,
who: stripAuthor(keys.id),
when: 0,
},
sig: '',
}

View File

@ -142,13 +142,6 @@ function validateTangleRoot(msg, tangleId) {
}
}
function validateWhen(msg) {
if (msg.metadata.when && typeof msg.metadata.when !== 'number') {
// prettier-ignore
return new Error('invalid message: `when` is not a number, on feed: ' + msg.metadata.who);
}
}
function validateType(type) {
if (!type || typeof type !== 'string') {
// prettier-ignore
@ -190,7 +183,6 @@ function validateSync(msg, tangle, msgHash, rootHash) {
let err
if ((err = validateShape(msg))) return err
if ((err = validateWho(msg))) return err
if ((err = validateWhen(msg))) return err
if (msgHash === rootHash) {
if ((err = validateTangleRoot(msg))) return err
} else {

View File

@ -231,7 +231,7 @@ exports.init = function initDB(peer, config) {
const tangleTemplates = opts.tangles ?? []
tangleTemplates.push(feedRootHash)
const tangles = populateTangles(tangleTemplates)
const fullOpts = { when: Date.now(), ...opts, tangles, keys }
const fullOpts = { ...opts, tangles, keys }
// If opts ask for encryption, encrypt and put ciphertext in opts.content
const recps = fullOpts.content.recps

View File

@ -24,13 +24,12 @@ test('add()', async (t) => {
const rootHash = FeedV1.getMsgHash(rootMsg)
const recRoot = await p(peer.db.add)(rootMsg, rootHash)
t.equals(recRoot.msg.metadata.when, 0, 'root msg added')
t.equals(recRoot.msg.metadata.size, 0, 'root msg added')
const tangle = new FeedV1.Tangle(rootHash)
tangle.add(recRoot.hash, recRoot.msg)
const inputMsg = FeedV1.create({
keys,
when: 1514517067954,
type: 'post',
content: { text: 'This is the first post!' },
tangles: {

View File

@ -72,7 +72,6 @@ test('add() forked then create() merged', async (t) => {
const msg3 = FeedV1.create({
keys,
when: Date.now(),
type: 'post',
content: { text: '3rd post forked from 1st' },
tangles: {

View File

@ -20,7 +20,7 @@ test('erase', async (t) => {
await peer.db.loaded()
const rootHash = 'PpkBfa8C4sB8wHrqiNmHqe'
const rootHash = 'PGwQiuwFnB7EySQHBit2mA'
const msgHashes = []
for (let i = 0; i < 5; i++) {
const rec = await p(peer.db.create)({

View File

@ -12,18 +12,16 @@ tape('FeedV1.createRoot()', (t) => {
t.equals(rootMsg.metadata.size, 0, 'size')
t.equals(rootMsg.metadata.type, 'post', 'type')
t.equals(rootMsg.metadata.who, FeedV1.stripAuthor(keys.id), 'who')
t.equals(rootMsg.metadata.when, 0, 'when')
t.deepEquals(rootMsg.metadata.tangles, {}, 'tangles')
rootHash = FeedV1.getMsgHash(rootMsg)
t.equals(rootHash, 'PpkBfa8C4sB8wHrqiNmHqe', 'root hash')
t.equals(rootHash, 'PGwQiuwFnB7EySQHBit2mA', 'root hash')
t.end()
})
tape('FeedV1.create()', (t) => {
const keys = generateKeypair('alice')
const content = { text: 'Hello world!' }
const when = 1652037377204
const tangle1 = new FeedV1.Tangle(rootHash)
tangle1.add(rootHash, rootMsg)
@ -35,11 +33,10 @@ tape('FeedV1.create()', (t) => {
tangles: {
[rootHash]: tangle1,
},
when,
})
t.deepEquals(
Object.keys(msg1.metadata),
['proof', 'size', 'tangles', 'type', 'who', 'when'],
['proof', 'size', 'tangles', 'type', 'who'],
'metadata fields'
)
t.equals(
@ -53,12 +50,11 @@ tape('FeedV1.create()', (t) => {
t.equals(msg1.metadata.tangles[rootHash].depth, 1, 'tangle depth')
t.deepEquals(msg1.metadata.tangles[rootHash].prev, [rootHash], 'tangle prev')
t.deepEquals(msg1.metadata.size, 23, 'metadata.size')
t.equals(typeof msg1.metadata.when, 'number', 'metadata.when')
t.deepEqual(msg1.content, content, 'content is correct')
console.log(msg1)
const msgHash1 = 'YWbEeMtcU4eNwF6uJVTrKE'
const msgHash1 = 'M31mLeV2wNDwp9ZRkkF8pL'
t.equals(
FeedV1.getMsgId(msg1),
@ -80,11 +76,10 @@ tape('FeedV1.create()', (t) => {
tangles: {
[rootHash]: tangle2,
},
when: when + 1,
})
t.deepEquals(
Object.keys(msg2.metadata),
['proof', 'size', 'tangles', 'type', 'who', 'when'],
['proof', 'size', 'tangles', 'type', 'who'],
'metadata keys'
)
t.equals(
@ -98,14 +93,13 @@ tape('FeedV1.create()', (t) => {
t.deepEquals(msg2.metadata.tangles[rootHash].prev, [msgHash1], 'tangle prev')
t.deepEquals(msg2.metadata.proof, 'XuZEzH1Dhy1yuRMcviBBcN', 'metadata.proof')
t.deepEquals(msg2.metadata.size, 21, 'metadata.size')
t.equals(typeof msg2.metadata.when, 'number', 'metadata.when')
t.deepEqual(msg2.content, content2, 'content is correct')
console.log(msg2)
t.deepEqual(
FeedV1.getMsgId(msg2),
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/R9XRXBL1ntSKRrrk86bhn8',
'ppppp:message/v1/4mjQ5aJu378cEu6TksRG3uXAiKFiwGjYQtWAjfVjDAJW/post/MHLPVrHFzCLXVeXUkY1W4a',
'getMsgId'
)
@ -114,7 +108,6 @@ tape('FeedV1.create()', (t) => {
tape('create() handles DAG tips correctly', (t) => {
const keys = generateKeypair('alice')
const when = 1652037377204
const tangle = new FeedV1.Tangle(rootHash)
tangle.add(rootHash, rootMsg)
@ -125,12 +118,11 @@ tape('create() handles DAG tips correctly', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 1,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
t.deepEquals(
msg1.metadata.tangles[rootHash].prev,
['PpkBfa8C4sB8wHrqiNmHqe'],
['PGwQiuwFnB7EySQHBit2mA'],
'msg1.prev is root'
)
@ -143,7 +135,6 @@ tape('create() handles DAG tips correctly', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 2,
})
t.deepEquals(
msg2A.metadata.tangles[rootHash].prev,
@ -158,7 +149,6 @@ tape('create() handles DAG tips correctly', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 2,
})
const msgHash2B = FeedV1.getMsgHash(msg2B)
t.deepEquals(
@ -176,7 +166,6 @@ tape('create() handles DAG tips correctly', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 3,
})
const msgHash3 = FeedV1.getMsgHash(msg3)
t.deepEquals(
@ -197,7 +186,6 @@ tape('create() handles DAG tips correctly', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 4,
})
t.deepEquals(
msg4.metadata.tangles[rootHash].prev,

View File

@ -19,7 +19,6 @@ tape('invalid msg with non-array prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
msg.metadata.tangles[rootHash].prev = null
const msgHash = FeedV1.getMsgHash(msg)
@ -47,7 +46,6 @@ tape('invalid msg with bad prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangle.add(msgHash1, msg1)
@ -59,7 +57,6 @@ tape('invalid msg with bad prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030002000,
})
msg2.metadata.tangles[rootHash].depth = 1
msg2.metadata.tangles[rootHash].prev = [1234]
@ -92,7 +89,6 @@ tape('invalid msg with URI in prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangle.add(msgHash1, msg1)
@ -104,7 +100,6 @@ tape('invalid msg with URI in prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030002000,
})
const msgHash2 = FeedV1.getMsgHash(msg2)
const randBuf = Buffer.alloc(16).fill(16)
@ -139,7 +134,6 @@ tape('invalid msg with unknown prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangle.add(msgHash1, msg1)
@ -151,7 +145,6 @@ tape('invalid msg with unknown prev', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const unknownMsgHash = FeedV1.getMsgHash(unknownMsg)
@ -166,7 +159,6 @@ tape('invalid msg with unknown prev', (t) => {
tangles: {
[rootHash]: tangle2
},
when: 1652030002000,
})
const msgHash2 = FeedV1.getMsgHash(msg2)
@ -197,7 +189,6 @@ tape('invalid feed msg with a different who', (t) => {
tangles: {
[rootHash]: feedTangle,
},
when: 1652030002000,
})
const msgHash = FeedV1.getMsgHash(msg)
@ -222,7 +213,6 @@ tape('invalid feed msg with a different type', (t) => {
tangles: {
[rootHash]: feedTangle,
},
when: 1652030002000,
})
const msgHash = FeedV1.getMsgHash(msg)

View File

@ -10,7 +10,6 @@ tape('invalid type not a string', (t) => {
FeedV1.create({
keys,
content: { text: 'Hello world!' },
when: 1652037377204,
type: 123,
})
},
@ -28,7 +27,6 @@ tape('invalid type with "/" character', (t) => {
FeedV1.create({
keys,
content: { text: 'Hello world!' },
when: 1652037377204,
type: 'group/init',
})
},
@ -46,7 +44,6 @@ tape('invalid type with "*" character', (t) => {
FeedV1.create({
keys,
content: { text: 'Hello world!' },
when: 1652037377204,
type: 'star*',
})
},
@ -64,7 +61,6 @@ tape('invalid type too short', (t) => {
FeedV1.create({
keys,
content: { text: 'Hello world!' },
when: 1652037377204,
type: 'xy',
})
},
@ -82,7 +78,6 @@ tape('invalid type too long', (t) => {
FeedV1.create({
keys,
content: { text: 'Hello world!' },
when: 1652037377204,
type: 'a'.repeat(120),
})
},

View File

@ -5,7 +5,6 @@ const { generateKeypair } = require('./util')
tape('lipmaa prevs', (t) => {
const keys = generateKeypair('alice')
const content = { text: 'Hello world!' }
const when = 1652037377204
const rootMsg = FeedV1.createRoot(keys, 'post')
const rootHash = FeedV1.getMsgHash(rootMsg)
@ -19,7 +18,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 1,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangle.add(msgHash1, msg1)
@ -33,7 +31,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 2,
})
const msgHash2 = FeedV1.getMsgHash(msg2)
tangle.add(msgHash2, msg2)
@ -47,7 +44,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 3,
})
const msgHash3 = FeedV1.getMsgHash(msg3)
tangle.add(msgHash3, msg3)
@ -65,7 +61,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 4,
})
const msgHash4 = FeedV1.getMsgHash(msg4)
tangle.add(msgHash4, msg4)
@ -79,7 +74,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 5,
})
const msgHash5 = FeedV1.getMsgHash(msg5)
tangle.add(msgHash5, msg5)
@ -93,7 +87,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 6,
})
const msgHash6 = FeedV1.getMsgHash(msg6)
tangle.add(msgHash6, msg6)
@ -107,7 +100,6 @@ tape('lipmaa prevs', (t) => {
tangles: {
[rootHash]: tangle,
},
when: when + 7,
})
const msgHash7 = FeedV1.getMsgHash(msg7)
tangle.add(msgHash7, msg7)

View File

@ -23,7 +23,6 @@ tape('simple multi-author tangle', (t) => {
tangles: {
[rootHashA]: tangleA,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
t.deepEquals(
@ -43,7 +42,6 @@ tape('simple multi-author tangle', (t) => {
[rootHashB]: tangleB,
[msgHash1]: tangleX,
},
when: 1652030002000,
})
t.deepEquals(
@ -73,7 +71,6 @@ tape('lipmaa in multi-author tangle', (t) => {
const keysB = generateKeypair('bob')
const content = { text: 'Hello world!' }
const when = 1652037377204
const rootMsgA = FeedV1.createRoot(keysA, 'post')
const rootHashA = FeedV1.getMsgHash(rootMsgA)
@ -92,7 +89,6 @@ tape('lipmaa in multi-author tangle', (t) => {
tangles: {
[rootHashA]: tangleA,
},
when: when + 1,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangleA.add(msgHash1, msg1)
@ -113,7 +109,6 @@ tape('lipmaa in multi-author tangle', (t) => {
[rootHashB]: tangleB,
[msgHash1]: tangleThread,
},
when: when + 2,
})
const msgHash2 = FeedV1.getMsgHash(msg2)
tangleB.add(msgHash2, msg2)
@ -133,7 +128,6 @@ tape('lipmaa in multi-author tangle', (t) => {
[rootHashB]: tangleB,
[msgHash1]: tangleThread,
},
when: when + 3,
})
const msgHash3 = FeedV1.getMsgHash(msg3)
tangleB.add(msgHash3, msg3)
@ -153,7 +147,6 @@ tape('lipmaa in multi-author tangle', (t) => {
[rootHashA]: tangleA,
[msgHash1]: tangleThread,
},
when: when + 4,
})
const msgHash4 = FeedV1.getMsgHash(msg4)
tangleB.add(msgHash4, msg4)

View File

@ -33,7 +33,6 @@ tape('validate 2nd msg with existing root', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)
tangle.add(msgHash1, msg1)
@ -61,7 +60,6 @@ tape('validate 2nd forked msg', (t) => {
[rootHash]: tangle,
},
existing: new Map(),
when: 1652030001000,
})
const msgHash1A = FeedV1.getMsgHash(msg1A)
@ -72,7 +70,6 @@ tape('validate 2nd forked msg', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030002000,
})
const msgHash1B = FeedV1.getMsgHash(msg1B)
@ -100,7 +97,6 @@ tape('invalid msg with unknown previous', (t) => {
tangles: {
[rootHash]: tangle,
},
when: 1652030001000,
})
const msgHash1 = FeedV1.getMsgHash(msg1)

View File

@ -33,7 +33,7 @@ test('onRecordAdded', async (t) => {
t.equal(listened.length, 2)
t.deepEquals(listened[0].msg.content, null, 'root')
t.deepEquals(listened[0].msg.metadata.when, 0, 'root')
t.deepEquals(listened[0].msg.metadata.size, 0, 'root')
t.deepEquals(listened[1], rec1, 'actual record')
remove()