Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore(dag): replace custom dag walk with multiformats/traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 29, 2021
1 parent 6178708 commit 58aee9e
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"libp2p-crypto": "^0.19.7",
"libp2p-websockets": "^0.16.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"nanoid": "^3.1.23",
"native-abort-controller": "^1.0.3",
"p-map": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"mafmt": "^10.0.0",
"multiaddr": "^10.0.0",
"multiaddr-to-uri": "^8.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"parse-duration": "^1.0.0",
"pretty-bytes": "^5.4.1",
"progress": "^2.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"interface-datastore": "^6.0.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.13"
"multiformats": "^9.5.0"
},
"devDependencies": {
"aegir": "^36.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"merge-options": "^3.0.4",
"multiaddr": "^10.0.0",
"multiaddr-to-uri": "^8.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"nanoid": "^3.1.23",
"parse-duration": "^1.0.0",
"timeout-abort-controller": "^1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"mortice": "^2.0.0",
"multiaddr": "^10.0.0",
"multiaddr-to-uri": "^8.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"native-abort-controller": "^1.0.3",
"pako": "^1.0.2",
"parse-duration": "^1.0.0",
Expand Down
76 changes: 27 additions & 49 deletions packages/ipfs-core/src/components/dag/export.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CID } from 'multiformats/cid'
import { createUnsafe } from 'multiformats/block'
import { base58btc } from 'multiformats/bases/base58'
import { CarWriter } from '@ipld/car/writer'
import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
import debug from 'debug'
import * as raw from 'multiformats/codecs/raw'
import * as json from 'multiformats/codecs/json'
import { walk } from 'multiformats/traversal'

const log = debug('ipfs:components:dag:import')

Expand All @@ -23,6 +23,11 @@ const NO_LINKS_CODECS = [
* @typedef {import('ipfs-core-types/src/utils').AbortOptions} AbortOptions
*/

/**
* @template T
* @typedef {import('multiformats/block').Block<T>} Block
*/

/**
* @param {Object} config
* @param {IPFSRepo} config.repo
Expand Down Expand Up @@ -53,13 +58,9 @@ export function createExport ({ repo, preload, codecs }) {
let err = null
;(async () => {
try {
await traverseWrite(
repo,
{ signal: options.signal, timeout: options.timeout },
cid,
writer,
codecs)
writer.close()
const load = makeLoader(repo, writer, { signal: options.signal, timeout: options.timeout }, codecs)
await walk({ cid, load })
await writer.close()
} catch (/** @type {any} */ e) {
err = e
}
Expand All @@ -80,52 +81,29 @@ export function createExport ({ repo, preload, codecs }) {
}

/**
* @template T
* @param {IPFSRepo} repo
* @param {AbortOptions} options
* @param {CID} cid
* @param {BlockWriter} writer
* @param {import('ipfs-core-utils/multicodecs').Multicodecs} codecs
* @param {Set<string>} seen
* @returns {Promise<void>}
*/
async function traverseWrite (repo, options, cid, writer, codecs, seen = new Set()) {
const b58Cid = cid.toString(base58btc)
if (seen.has(b58Cid)) {
return
}

const block = await getBlock(repo, options, cid, codecs)

log(`Adding block ${cid} to car`)
await writer.put(block)
seen.add(b58Cid)

// recursive traversal of all links
for (const link of block.links) {
await traverseWrite(repo, options, link, writer, codecs, seen)
}
}

/**
* @param {IPFSRepo} repo
* @param {AbortOptions} options
* @param {CID} cid
* @param {import('ipfs-core-utils/multicodecs').Multicodecs} codecs
* @returns {Promise<{cid:CID, bytes:Uint8Array, links:CID[]}>}
* @returns {(cid:CID)=>Promise<Block<T>>}
*/
async function getBlock (repo, options, cid, codecs) {
const bytes = await repo.blocks.get(cid, options)

/** @type {CID[]} */
let links = []
const codec = await codecs.getCodec(cid.code)
function makeLoader (repo, writer, options, codecs) {
return async (cid) => {
const bytes = await repo.blocks.get(cid, options)
const codec = await codecs.getCodec(cid.code)

let block
if (codec) {
block = createUnsafe({ bytes, cid, codec })
} else if (NO_LINKS_CODECS.includes(cid.code)) {
return null // skip this block
} else {
throw new Error(`Can't decode links in block with codec 0x${cid.code.toString(16)} to form complete DAG`)
}

if (codec) {
const block = createUnsafe({ bytes, cid, codec })
links = [...block.links()].map((l) => l[1])
} else if (!NO_LINKS_CODECS.includes(cid.code)) {
throw new Error(`Can't decode links in block with codec 0x${cid.code.toString(16)} to form complete DAG`)
log(`Adding block ${cid} to car`)
await writer.put(block)
return block
}

return { cid, bytes, links }
}
2 changes: 1 addition & 1 deletion packages/ipfs-grpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"it-first": "^1.0.4",
"it-pushable": "^1.4.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"p-defer": "^3.0.0",
"protobufjs": "^6.10.2",
"wherearewe": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"it-last": "^1.0.4",
"merge-options": "^3.0.4",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"native-abort-controller": "^1.0.3",
"parse-duration": "^1.0.0",
"stream-to-it": "^0.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"it-last": "^1.0.4",
"it-to-stream": "^1.0.0",
"joi": "^17.2.1",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"uint8arrays": "^3.0.0",
"uri-to-multiaddr": "^6.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"joi": "^17.2.1",
"just-safe-set": "^2.2.1",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.13",
"multiformats": "^9.5.0",
"native-abort-controller": "^1.0.3",
"parse-duration": "^1.0.0",
"stream-to-it": "^0.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"ipfs-message-port-protocol": "^0.10.4",
"ipfs-unixfs": "^6.0.3",
"it-peekable": "^1.0.2",
"multiformats": "^9.4.13"
"multiformats": "^9.5.0"
},
"devDependencies": {
"aegir": "^36.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
},
"dependencies": {
"ipfs-core-types": "^0.8.4",
"multiformats": "^9.4.13"
"multiformats": "^9.5.0"
},
"devDependencies": {
"aegir": "^36.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"ipfs-core-types": "^0.8.4",
"ipfs-message-port-protocol": "^0.10.4",
"it-all": "^1.0.4",
"multiformats": "^9.4.13"
"multiformats": "^9.5.0"
},
"devDependencies": {
"aegir": "^36.0.1",
Expand Down

0 comments on commit 58aee9e

Please sign in to comment.