-
Notifications
You must be signed in to change notification settings - Fork 3
chore: upgrade to new multiformats module #98
Changes from 5 commits
f98d385
e22501b
17248ac
155bc90
d4d09e7
ef6c211
3546fb9
6c38340
9b5a552
c9d01ea
a2a7a59
a4ce9c6
90a0e78
f402825
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
const dagpb = require('ipld-dag-pb') | ||
const { CID } = require('multiformats/cid') | ||
const dagPb = require('@ipld/dag-pb') | ||
const cbor = require('cborg') | ||
const multicodec = require('multicodec') | ||
const multibase = require('multibase') | ||
const pinset = require('./pin-set') | ||
const { createStore } = require('../../src/utils') | ||
const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils') | ||
const length = require('it-length') | ||
const { sha256 } = require('multiformats/hashes/sha2') | ||
const mhd = require('multiformats/hashes/digest') | ||
const { base32 } = require('multiformats/bases/base32') | ||
|
||
/** | ||
* @typedef {import('../../src/types').Migration} Migration | ||
* @typedef {import('../../src/types').MigrationProgressCallback} MigrationProgressCallback | ||
* @typedef {import('interface-datastore').Datastore} Datastore | ||
* @typedef {import('multicodec').CodecCode} CodecCode | ||
* @typedef {import('multiformats/cid').CIDVersion} CIDVersion | ||
*/ | ||
|
||
/** | ||
|
@@ -29,9 +30,9 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) { | |
} | ||
|
||
const mh = await datastore.get(PIN_DS_KEY) | ||
const cid = new CID(mh) | ||
const cid = CID.decode(mh) | ||
const pinRootBuf = await blockstore.get(cidToKey(cid)) | ||
const pinRoot = dagpb.util.deserialize(pinRootBuf) | ||
const pinRoot = dagPb.decode(pinRootBuf) | ||
let counter = 0 | ||
let pinCount | ||
|
||
|
@@ -40,7 +41,7 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) { | |
for await (const cid of pinset.loadSet(blockstore, pinRoot, PinTypes.recursive)) { | ||
counter++ | ||
|
||
/** @type {{ depth: number, version?: CID.CIDVersion, codec?: CodecCode }} */ | ||
/** @type {{ depth: number, version?: CIDVersion, codec?: number }} */ | ||
const pin = { | ||
depth: Infinity | ||
} | ||
|
@@ -49,8 +50,8 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) { | |
pin.version = cid.version | ||
} | ||
|
||
if (cid.codec !== 'dag-pb') { | ||
pin.codec = multicodec.getNumber(cid.codec) | ||
if (cid.code !== dagPb.code) { | ||
pin.codec = cid.code | ||
} | ||
|
||
await pinstore.put(cidToKey(cid), cbor.encode(pin)) | ||
|
@@ -61,7 +62,7 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) { | |
for await (const cid of pinset.loadSet(blockstore, pinRoot, PinTypes.direct)) { | ||
counter++ | ||
|
||
/** @type {{ depth: number, version?: CID.CIDVersion, codec?: CodecCode }} */ | ||
/** @type {{ depth: number, version?: CIDVersion, codec?: number }} */ | ||
const pin = { | ||
depth: 0 | ||
} | ||
|
@@ -70,8 +71,8 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) { | |
pin.version = cid.version | ||
} | ||
|
||
if (cid.codec !== 'dag-pb') { | ||
pin.codec = multicodec.getNumber(cid.codec) | ||
if (cid.code !== dagPb.code) { | ||
pin.codec = cid.code | ||
} | ||
|
||
await pinstore.put(cidToKey(cid), cbor.encode(pin)) | ||
|
@@ -98,7 +99,11 @@ async function pinsToDAG (blockstore, datastore, pinstore, onProgress) { | |
for await (const { key, value } of pinstore.query({})) { | ||
counter++ | ||
const pin = cbor.decode(value) | ||
const cid = new CID(pin.version || 0, pin.codec && multicodec.getName(pin.codec) || 'dag-pb', multibase.decode('b' + key.toString().split('/').pop())) | ||
const cid = CID.create( | ||
pin.version || 0, | ||
pin.codec || dagPb.code, | ||
mhd.decode(base32.decode('b' + key.toString().toLowerCase().split('/').pop())) | ||
) | ||
|
||
if (pin.depth === 0) { | ||
onProgress((counter / pinCount) * 100, `Reverted direct pin ${cid}`) | ||
|
@@ -112,16 +117,18 @@ async function pinsToDAG (blockstore, datastore, pinstore, onProgress) { | |
} | ||
|
||
onProgress(100, 'Updating pin root') | ||
const pinRoot = new dagpb.DAGNode(new Uint8Array(), [ | ||
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins), | ||
await pinset.storeSet(blockstore, PinTypes.direct, directPins) | ||
]) | ||
const buf = pinRoot.serialize() | ||
const cid = await dagpb.util.cid(buf, { | ||
cidVersion: 0 | ||
}) | ||
const pinRoot = { | ||
Links: [ | ||
await pinset.storeSet(blockstore, PinTypes.direct, directPins), | ||
rvagg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins) | ||
] | ||
} | ||
const buf = dagPb.encode(pinRoot) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could just pull the multihash bytes out of the v1 CID, I guess.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think Block.encode will save that much, and probably it's best to not add support for v0 there if we eventually want to break from v0. I would personally leave it as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This repo will always have to handle v0 as it needs to be able to migrate repos from before v1 was a thing. |
||
const digest = await sha256.digest(buf) | ||
const cid = CID.createV0(digest) | ||
|
||
await blockstore.put(cidToKey(cid), buf) | ||
await datastore.put(PIN_DS_KEY, cid.multihash) | ||
await datastore.put(PIN_DS_KEY, cid.bytes) | ||
} | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.