mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-07-02 02:22:29 +00:00
commit
96bd3eab1b
@ -248,23 +248,23 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
|
|||||||
const traversed = {}
|
const traversed = {}
|
||||||
// Current entry during traversal
|
// Current entry during traversal
|
||||||
let entry
|
let entry
|
||||||
// Start traversal
|
// Start traversal and process stack until it's empty (traversed the full log)
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
// Process stack until it's empty (traversed the full log)
|
// Get the next entry from the stack
|
||||||
// or until shouldStopFn returns true
|
entry = stack.pop()
|
||||||
|
if (entry) {
|
||||||
|
const hash = entry.hash
|
||||||
|
// If we have an entry that we haven't traversed yet, process it
|
||||||
|
if (!traversed[hash]) {
|
||||||
|
// Yield the current entry
|
||||||
|
yield entry
|
||||||
|
// If we should stop traversing, stop here
|
||||||
const done = await shouldStopFn(entry)
|
const done = await shouldStopFn(entry)
|
||||||
if (done === true) {
|
if (done === true) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Get the next entry from the stack
|
|
||||||
entry = stack.pop()
|
|
||||||
const hash = entry.hash
|
|
||||||
// If we have an entry that we haven't traversed yet, process it
|
|
||||||
if (entry && !traversed[hash]) {
|
|
||||||
// Add to the hashes we've traversed
|
// Add to the hashes we've traversed
|
||||||
traversed[hash] = true
|
traversed[hash] = true
|
||||||
// Yield the current entry
|
|
||||||
yield entry
|
|
||||||
// Add hashes of next entries to the stack from entry's
|
// Add hashes of next entries to the stack from entry's
|
||||||
// causal connection (next) and references to history (refs)
|
// causal connection (next) and references to history (refs)
|
||||||
for (const nextHash of [...entry.next, ...entry.refs]) {
|
for (const nextHash of [...entry.next, ...entry.refs]) {
|
||||||
@ -281,6 +281,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Async iterator over the log entries
|
* Async iterator over the log entries
|
||||||
@ -337,12 +338,11 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
|
|||||||
const start = (lt || (lte || await heads())).filter(isDefined)
|
const start = (lt || (lte || await heads())).filter(isDefined)
|
||||||
const end = (gt || gte) ? await get(gt || gte) : null
|
const end = (gt || gte) ? await get(gt || gte) : null
|
||||||
|
|
||||||
const amountToIterate = end || amount === -1
|
const amountToIterate = (end || amount === -1) ? -1 : amount
|
||||||
? -1
|
|
||||||
: (lte || lt ? amount - 1 : amount)
|
|
||||||
|
|
||||||
let count = 0
|
let count = 0
|
||||||
const shouldStopTraversal = async (entry) => {
|
const shouldStopTraversal = async (entry) => {
|
||||||
|
count++
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -352,7 +352,6 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
|
|||||||
if (end && Entry.isEqual(entry, end)) {
|
if (end && Entry.isEqual(entry, end)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
count++
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,7 +375,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (useBuffer) {
|
if (useBuffer) {
|
||||||
const endIndex = buffer.keys.length - 1
|
const endIndex = buffer.keys.length
|
||||||
const startIndex = endIndex - amount
|
const startIndex = endIndex - amount
|
||||||
const keys = buffer.keys.slice(startIndex, endIndex)
|
const keys = buffer.keys.slice(startIndex, endIndex)
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
|
@ -29,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
ipfsd = await startIpfs(IPFS, config.daemon1)
|
ipfsd = await startIpfs(IPFS, config.daemon1)
|
||||||
ipfs = ipfsd.api
|
ipfs = ipfsd.api
|
||||||
|
|
||||||
const [identities, testIdentities] = await createTestIdentities([ipfs])
|
const [identities, testIdentities] = await createTestIdentities(ipfs)
|
||||||
identities1 = identities[0]
|
identities1 = identities[0]
|
||||||
testIdentity1 = testIdentities[0]
|
testIdentity1 = testIdentities[0]
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
ipfsd = await startIpfs(IPFS, config.daemon1)
|
ipfsd = await startIpfs(IPFS, config.daemon1)
|
||||||
ipfs = ipfsd.api
|
ipfs = ipfsd.api
|
||||||
|
|
||||||
const [identities, testIdentities] = await createTestIdentities([ipfs])
|
const [identities, testIdentities] = await createTestIdentities(ipfs)
|
||||||
identities1 = identities[0]
|
identities1 = identities[0]
|
||||||
testIdentity1 = testIdentities[0]
|
testIdentity1 = testIdentities[0]
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
strictEqual(all.length, 1)
|
strictEqual(all.length, 1)
|
||||||
deepStrictEqual(all.map(e => e.value), ['hello2'])
|
deepStrictEqual(all.map(e => e.value), ['hello1'])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns next two items greater than root', async () => {
|
it('returns next two items greater than root', async () => {
|
||||||
@ -229,7 +229,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
strictEqual(all.length, 2)
|
strictEqual(all.length, 2)
|
||||||
deepStrictEqual(all.map(e => e.value), ['hello2', 'hello3'])
|
deepStrictEqual(all.map(e => e.value), ['hello1', 'hello2'])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns first item less than head', async () => {
|
it('returns first item less than head', async () => {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { deepStrictEqual, strictEqual } from 'assert'
|
import { deepStrictEqual, strictEqual } from 'assert'
|
||||||
import rimraf from 'rimraf'
|
import rimraf from 'rimraf'
|
||||||
import { Log, Entry } from '../src/oplog/index.js'
|
import { Log, Entry } from '../../src/oplog/index.js'
|
||||||
import { Identities } from '../src/identities/index.js'
|
import { Identities } from '../../src/identities/index.js'
|
||||||
import KeyStore from '../src/key-store.js'
|
import KeyStore from '../../src/key-store.js'
|
||||||
import { Feed, Database } from '../src/db/index.js'
|
import { Feed, Database } from '../../src/db/index.js'
|
||||||
import { IPFSBlockStorage, LevelStorage } from '../src/storage/index.js'
|
import { IPFSBlockStorage, LevelStorage } from '../../src/storage/index.js'
|
||||||
|
|
||||||
// Test utils
|
// Test utils
|
||||||
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
|
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
|
||||||
import connectPeers from './utils/connect-nodes.js'
|
import connectPeers from '../utils/connect-nodes.js'
|
||||||
import waitFor from './utils/wait-for.js'
|
import waitFor from '../utils/wait-for.js'
|
||||||
import { createTestIdentities, cleanUpTestIdentities } from './fixtures/orbit-db-identity-keys.js'
|
import { createTestIdentities, cleanUpTestIdentities } from '../fixtures/orbit-db-identity-keys.js'
|
||||||
|
|
||||||
const { sync: rmrf } = rimraf
|
const { sync: rmrf } = rimraf
|
||||||
const { createIdentity } = Identities
|
const { createIdentity } = Identities
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { deepStrictEqual, strictEqual } from 'assert'
|
import { deepStrictEqual, strictEqual } from 'assert'
|
||||||
import rimraf from 'rimraf'
|
import rimraf from 'rimraf'
|
||||||
import { Log, Entry } from '../src/oplog/index.js'
|
import { Log, Entry } from '../../src/oplog/index.js'
|
||||||
import { Identities } from '../src/identities/index.js'
|
import { Identities } from '../../src/identities/index.js'
|
||||||
import KeyStore from '../src/key-store.js'
|
import KeyStore from '../../src/key-store.js'
|
||||||
import { KeyValue, KeyValuePersisted, Database } from '../src/db/index.js'
|
import { KeyValue, KeyValuePersisted, Database } from '../../src/db/index.js'
|
||||||
import { IPFSBlockStorage, LevelStorage } from '../src/storage/index.js'
|
import { IPFSBlockStorage, LevelStorage } from '../../src/storage/index.js'
|
||||||
|
|
||||||
// Test utils
|
// Test utils
|
||||||
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
|
import { config, testAPIs, getIpfsPeerId, waitForPeers, startIpfs, stopIpfs } from 'orbit-db-test-utils'
|
||||||
import connectPeers from './utils/connect-nodes.js'
|
import connectPeers from '../utils/connect-nodes.js'
|
||||||
import waitFor from './utils/wait-for.js'
|
import waitFor from '../utils/wait-for.js'
|
||||||
import { createTestIdentities, cleanUpTestIdentities } from './fixtures/orbit-db-identity-keys.js'
|
import { createTestIdentities, cleanUpTestIdentities } from '../fixtures/orbit-db-identity-keys.js'
|
||||||
|
|
||||||
const { sync: rmrf } = rimraf
|
const { sync: rmrf } = rimraf
|
||||||
const { createIdentity } = Identities
|
const { createIdentity } = Identities
|
||||||
|
@ -40,7 +40,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
peerId1 = await getIpfsPeerId(ipfs1)
|
peerId1 = await getIpfsPeerId(ipfs1)
|
||||||
peerId2 = await getIpfsPeerId(ipfs2)
|
peerId2 = await getIpfsPeerId(ipfs2)
|
||||||
|
|
||||||
const [identities, testIdentities] = await createTestIdentities([ipfs1, ipfs2])
|
const [identities, testIdentities] = await createTestIdentities(ipfs1, ipfs2)
|
||||||
identities1 = identities[0]
|
identities1 = identities[0]
|
||||||
identities2 = identities[1]
|
identities2 = identities[1]
|
||||||
testIdentity1 = testIdentities[0]
|
testIdentity1 = testIdentities[0]
|
||||||
|
@ -40,7 +40,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
peerId1 = await getIpfsPeerId(ipfs1)
|
peerId1 = await getIpfsPeerId(ipfs1)
|
||||||
peerId2 = await getIpfsPeerId(ipfs2)
|
peerId2 = await getIpfsPeerId(ipfs2)
|
||||||
|
|
||||||
const [identities, testIdentities] = await createTestIdentities([ipfs1, ipfs2])
|
const [identities, testIdentities] = await createTestIdentities(ipfs1, ipfs2)
|
||||||
identities1 = identities[0]
|
identities1 = identities[0]
|
||||||
identities2 = identities[1]
|
identities2 = identities[1]
|
||||||
testIdentity1 = testIdentities[0]
|
testIdentity1 = testIdentities[0]
|
||||||
|
17
test/fixtures/orbit-db-identity-keys.js
vendored
17
test/fixtures/orbit-db-identity-keys.js
vendored
@ -28,7 +28,7 @@ const signingKeys = {
|
|||||||
userD: userD_,
|
userD: userD_,
|
||||||
}
|
}
|
||||||
|
|
||||||
const createTestIdentities = async (ipfsInstances) => {
|
const createTestIdentities = async (ipfs1, ipfs2) => {
|
||||||
rmrf('./keys_1')
|
rmrf('./keys_1')
|
||||||
|
|
||||||
const keystore = new KeyStore('./keys_1')
|
const keystore = new KeyStore('./keys_1')
|
||||||
@ -41,16 +41,13 @@ const createTestIdentities = async (ipfsInstances) => {
|
|||||||
await keystore.addKey(key, value)
|
await keystore.addKey(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
const identities = []
|
// Create an identity for each peers
|
||||||
const testIdentities = []
|
const identities1 = await Identities({ keystore, ipfs: ipfs1 })
|
||||||
|
const identities2 = await Identities({ keystore, ipfs: ipfs2 })
|
||||||
|
const testIdentity1 = await identities1.createIdentity({ id: 'userA' })
|
||||||
|
const testIdentity2 = await identities2.createIdentity({ id: 'userB' })
|
||||||
|
|
||||||
let i = 0
|
return [[identities1, identities2], [testIdentity1, testIdentity2]]
|
||||||
for (const ipfsInstance of ipfsInstances) {
|
|
||||||
identities.push(await Identities({ keystore, ipfs: ipfsInstance }))
|
|
||||||
testIdentities.push(await identities[i].createIdentity({ id: 'user'+(++i) }))
|
|
||||||
}
|
|
||||||
|
|
||||||
return [identities, testIdentities]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cleanUpTestIdentities = async (identities) => {
|
const cleanUpTestIdentities = async (identities) => {
|
||||||
|
@ -89,6 +89,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
const result = await all(it)
|
const result = await all(it)
|
||||||
|
|
||||||
strictEqual([...result].length, 10)
|
strictEqual([...result].length, 10)
|
||||||
|
strictEqual(result[0].hash, startHash)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns entries with lte and amount', async () => {
|
it('returns entries with lte and amount', async () => {
|
||||||
@ -146,7 +147,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
for await (const entry of it) {
|
for await (const entry of it) {
|
||||||
strictEqual(entry.payload, 'entry' + (73 - i++))
|
strictEqual(entry.payload, 'entry' + (72 - i++))
|
||||||
}
|
}
|
||||||
|
|
||||||
strictEqual(i, amount)
|
strictEqual(i, amount)
|
||||||
@ -163,6 +164,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
const result = await all(it)
|
const result = await all(it)
|
||||||
|
|
||||||
strictEqual([...result].length, amount)
|
strictEqual([...result].length, amount)
|
||||||
|
strictEqual(result[result.length - 1].hash, startHash)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns entries with gte and amount', async () => {
|
it('returns entries with gte and amount', async () => {
|
||||||
@ -175,7 +177,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
for await (const entry of it) {
|
for await (const entry of it) {
|
||||||
strictEqual(entry.payload, 'entry' + (79 - i++))
|
strictEqual(entry.payload, 'entry' + (78 - i++))
|
||||||
}
|
}
|
||||||
|
|
||||||
strictEqual(i, amount)
|
strictEqual(i, amount)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user