new API minGhostDepth()

This commit is contained in:
Andre Staltz 2023-10-25 14:02:06 +03:00
parent 3385901b2d
commit a2304bc24f
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 29 additions and 25 deletions

View File

@ -265,7 +265,7 @@ function initRecord(peer, config) {
const mootID = MsgV3.getMootID(accountID, fromSubdomain(subdomain)) const mootID = MsgV3.getMootID(accountID, fromSubdomain(subdomain))
const tangle = peer.db.getTangle(mootID) const tangle = peer.db.getTangle(mootID)
const maxDepth = tangle.maxDepth const maxDepth = tangle.maxDepth
const fieldRoots = getFieldRoots(subdomain) const fieldRoots = _getFieldRoots(subdomain)
let minDepth = Infinity let minDepth = Infinity
for (const field in fieldRoots) { for (const field in fieldRoots) {
for (const msgID of fieldRoots[field]) { for (const msgID of fieldRoots[field]) {
@ -335,16 +335,17 @@ function initRecord(peer, config) {
/** /**
* @param {string} subdomain * @param {string} subdomain
*/ */
function getFieldRoots(subdomain) { function _getFieldRoots(subdomain) {
if (!accountID) throw new Error('Cannot getFieldRoots() before loading') if (!accountID) throw new Error('Cannot getFieldRoots() before loading')
return fieldRoots.getAll(subdomain) return fieldRoots.getAll(subdomain)
} }
/** /**
* @public
* @param {string} tangleID * @param {string} tangleID
* @returns {number} * @returns {number}
*/ */
function getMinRequiredDepth(tangleID) { function minRequiredDepth(tangleID) {
assertDBPlugin(peer) assertDBPlugin(peer)
const rootMsg = peer.db.get(tangleID) const rootMsg = peer.db.get(tangleID)
@ -378,6 +379,15 @@ function initRecord(peer, config) {
return minDepth return minDepth
} }
/**
* @public
* @param {string} tangleID
* @returns {number}
*/
function minGhostDepth(tangleID) {
return Math.max(0, minRequiredDepth(tangleID) - ghostSpan)
}
/** /**
* @public * @public
* @param {string} id * @param {string} id
@ -522,10 +532,11 @@ function initRecord(peer, config) {
read, read,
getFeedID, getFeedID,
isGhostable, isGhostable,
getFieldRoots, minGhostDepth,
getMinRequiredDepth, minRequiredDepth,
squeeze, squeeze,
_getFieldRoots,
_squeezePotential, _squeezePotential,
} }
} }

View File

@ -12,7 +12,8 @@
"main": "index.js", "main": "index.js",
"files": [ "files": [
"*.js", "*.js",
"lib/*.js" "lib/*.js",
"lib/*.d.ts"
], ],
"exports": { "exports": {
".": { ".": {

View File

@ -42,7 +42,7 @@ test('Record update() and get()', async (t) => {
'get' 'get'
) )
const fieldRoots1 = peer.record.getFieldRoots('profile') const fieldRoots1 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots1, fieldRoots1,
{ name: ['PbwnLbJS4oninQ1RPCdgRn'] }, { name: ['PbwnLbJS4oninQ1RPCdgRn'] },
@ -56,7 +56,7 @@ test('Record update() and get()', async (t) => {
'get' 'get'
) )
const fieldRoots2 = peer.record.getFieldRoots('profile') const fieldRoots2 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots2, fieldRoots2,
{ name: ['PbwnLbJS4oninQ1RPCdgRn'], age: ['9iTTqNabtnXmw4AiZxNMRq'] }, { name: ['PbwnLbJS4oninQ1RPCdgRn'], age: ['9iTTqNabtnXmw4AiZxNMRq'] },
@ -85,7 +85,7 @@ test('Record update() and get()', async (t) => {
'get' 'get'
) )
const fieldRoots3 = peer.record.getFieldRoots('profile') const fieldRoots3 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots3, fieldRoots3,
{ age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] }, { age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] },
@ -98,7 +98,7 @@ test('Record squeeze', async (t) => {
assert(await p(peer.record.update)('profile', { age: 22 }), 'update .age') assert(await p(peer.record.update)('profile', { age: 22 }), 'update .age')
assert(await p(peer.record.update)('profile', { age: 23 }), 'update .age') assert(await p(peer.record.update)('profile', { age: 23 }), 'update .age')
const fieldRoots4 = peer.record.getFieldRoots('profile') const fieldRoots4 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots4, fieldRoots4,
{ name: ['M2JhM7TE2KX5T5rfnxBh6M'], age: ['S3xiydrT6Y34Bp1vg6wN7P'] }, { name: ['M2JhM7TE2KX5T5rfnxBh6M'], age: ['S3xiydrT6Y34Bp1vg6wN7P'] },
@ -112,7 +112,7 @@ test('Record squeeze', async (t) => {
) )
assert.equal(await p(peer.record.squeeze)('profile'), true, 'squeezed') assert.equal(await p(peer.record.squeeze)('profile'), true, 'squeezed')
const fieldRoots5 = peer.record.getFieldRoots('profile') const fieldRoots5 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots5, fieldRoots5,
{ name: ['Y4JkpPCHN8Avtz4VALaAmK'], age: ['Y4JkpPCHN8Avtz4VALaAmK'] }, { name: ['Y4JkpPCHN8Avtz4VALaAmK'], age: ['Y4JkpPCHN8Avtz4VALaAmK'] },
@ -130,7 +130,7 @@ test('Record squeeze', async (t) => {
'squeeze idempotent' 'squeeze idempotent'
) )
const fieldRoots6 = peer.record.getFieldRoots('profile') const fieldRoots6 = peer.record._getFieldRoots('profile')
assert.deepEqual(fieldRoots6, fieldRoots5, 'fieldRoots') assert.deepEqual(fieldRoots6, fieldRoots5, 'fieldRoots')
}) })
@ -143,11 +143,11 @@ test('Record isGhostable', (t) => {
const tangle = peer.db.getTangle(mootID) const tangle = peer.db.getTangle(mootID)
const msgIDs = tangle.topoSort() const msgIDs = tangle.topoSort()
const fieldRoots = peer.record.getFieldRoots('profile') const fieldRoots = peer.record._getFieldRoots('profile')
assert.deepEqual(fieldRoots.age, [msgIDs[7]]) assert.deepEqual(fieldRoots.age, [msgIDs[7]])
// Remember from the setup, that ghostSpan=4 // Remember from the setup, that ghostSpan=4
assert.equal(msgIDs.length, 8); assert.equal(msgIDs.length, 8)
assert.equal(peer.record.isGhostable(msgIDs[0], mootID), false) // moot assert.equal(peer.record.isGhostable(msgIDs[0], mootID), false) // moot
assert.equal(peer.record.isGhostable(msgIDs[1], mootID), false) assert.equal(peer.record.isGhostable(msgIDs[1], mootID), false)
assert.equal(peer.record.isGhostable(msgIDs[2], mootID), false) assert.equal(peer.record.isGhostable(msgIDs[2], mootID), false)
@ -162,11 +162,7 @@ test('Record receives old branched update', async (t) => {
const moot = MsgV3.createMoot(aliceID, 'record_v1__profile', aliceKeypair) const moot = MsgV3.createMoot(aliceID, 'record_v1__profile', aliceKeypair)
const mootID = MsgV3.getMsgID(moot) const mootID = MsgV3.getMsgID(moot)
assert.equal( assert.equal(peer.record.minRequiredDepth(mootID), 7, 'minRequiredDepth')
peer.record.getMinRequiredDepth(mootID),
7,
'getMinRequiredDepth'
)
const tangle = new MsgV3.Tangle(mootID) const tangle = new MsgV3.Tangle(mootID)
tangle.add(mootID, moot) tangle.add(mootID, moot)
@ -185,7 +181,7 @@ test('Record receives old branched update', async (t) => {
const rec = await p(peer.db.add)(msg, mootID) const rec = await p(peer.db.add)(msg, mootID)
assert.equal(rec.id, 'XZWr3DZFG253awsWXgSkS2', 'msg ID') assert.equal(rec.id, 'XZWr3DZFG253awsWXgSkS2', 'msg ID')
const fieldRoots7 = peer.record.getFieldRoots('profile') const fieldRoots7 = peer.record._getFieldRoots('profile')
assert.deepEqual( assert.deepEqual(
fieldRoots7, fieldRoots7,
{ {
@ -195,11 +191,7 @@ test('Record receives old branched update', async (t) => {
'fieldRoots' 'fieldRoots'
) )
assert.equal( assert.equal(peer.record.minRequiredDepth(mootID), 1, 'minRequiredDepth')
peer.record.getMinRequiredDepth(mootID),
1,
'getMinRequiredDepth'
)
assert.equal( assert.equal(
peer.record._squeezePotential('profile'), peer.record._squeezePotential('profile'),