Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit f98d385

Browse files
committed
chore: upgrade to new multiformats module
BREAKING CHANGE: Uses new CID class
1 parent c27da0d commit f98d385

File tree

8 files changed

+123
-94
lines changed

8 files changed

+123
-94
lines changed

.aegir.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = {
2727
}
2828
},
2929
build: {
30+
bundlesizeMax: '45kB',
3031
config: esbuild
3132
}
3233
}

migrations/migration-8/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3-
const CID = require('cids')
3+
const { CID } = require('multiformats')
44
const Key = require('interface-datastore').Key
5-
const mb = require('multibase')
65
const log = require('debug')('ipfs:repo:migrator:migration-8')
7-
const uint8ArrayToString = require('uint8arrays/to-string')
86
const { createStore } = require('../../src/utils')
97
const length = require('it-length')
8+
const { base32 } = require('multiformats/bases/base32')
9+
const raw = require('multiformats/codecs/raw')
10+
const mhd = require('multiformats/hashes/digest')
1011

1112
/**
1213
* @typedef {import('../../src/types').Migration} Migration
@@ -16,16 +17,14 @@ const length = require('it-length')
1617
* @param {Key} key
1718
*/
1819
function keyToMultihash (key) {
19-
const buf = mb.decode(`b${key.toString().slice(1)}`)
20+
const buf = base32.decode(`b${key.toString().toLowerCase().slice(1)}`)
2021

2122
// Extract multihash from CID
22-
let multihash = new CID(buf).multihash
23+
let multihash = CID.decode(buf).multihash.bytes
2324

2425
// Encode and slice off multibase codec
25-
multihash = mb.encode('base32', multihash).slice(1)
26-
2726
// Should be uppercase for interop with go
28-
const multihashStr = uint8ArrayToString(multihash).toUpperCase()
27+
const multihashStr = base32.encode(multihash).slice(1).toUpperCase()
2928

3029
return new Key(`/${multihashStr}`, false)
3130
}
@@ -34,12 +33,13 @@ function keyToMultihash (key) {
3433
* @param {Key} key
3534
*/
3635
function keyToCid (key) {
37-
const buf = mb.decode(`b${key.toString().slice(1)}`)
36+
const buf = base32.decode(`b${key.toString().toLowerCase().slice(1)}`)
37+
const digest = mhd.decode(buf)
3838

3939
// CID to Key
40-
const multihash = mb.encode('base32', new CID(1, 'raw', buf).bytes).slice(1)
40+
const multihash = base32.encode(CID.createV1(raw.code, digest).bytes).slice(1)
4141

42-
return new Key(`/${uint8ArrayToString(multihash)}`.toUpperCase(), false)
42+
return new Key(`/${multihash.toUpperCase()}`, false)
4343
}
4444

4545
/**

migrations/migration-9/index.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict'
22

3-
const CID = require('cids')
4-
const dagpb = require('ipld-dag-pb')
3+
const { CID } = require('multiformats')
4+
const dagPb = require('@ipld/dag-pb')
55
const cbor = require('cborg')
6-
const multicodec = require('multicodec')
7-
const multibase = require('multibase')
86
const pinset = require('./pin-set')
97
const { createStore } = require('../../src/utils')
108
const { cidToKey, PIN_DS_KEY, PinTypes } = require('./utils')
119
const length = require('it-length')
10+
const { sha256 } = require('multiformats/hashes/sha2')
11+
const mhd = require('multiformats/hashes/digest')
12+
const { base32 } = require('multiformats/bases/base32')
1213

1314
/**
1415
* @typedef {import('../../src/types').Migration} Migration
1516
* @typedef {import('../../src/types').MigrationProgressCallback} MigrationProgressCallback
1617
* @typedef {import('interface-datastore').Datastore} Datastore
17-
* @typedef {import('multicodec').CodecCode} CodecCode
1818
*/
1919

2020
/**
@@ -29,9 +29,9 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
2929
}
3030

3131
const mh = await datastore.get(PIN_DS_KEY)
32-
const cid = new CID(mh)
32+
const cid = CID.decode(mh)
3333
const pinRootBuf = await blockstore.get(cidToKey(cid))
34-
const pinRoot = dagpb.util.deserialize(pinRootBuf)
34+
const pinRoot = dagPb.decode(pinRootBuf)
3535
let counter = 0
3636
let pinCount
3737

@@ -40,7 +40,7 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
4040
for await (const cid of pinset.loadSet(blockstore, pinRoot, PinTypes.recursive)) {
4141
counter++
4242

43-
/** @type {{ depth: number, version?: CID.CIDVersion, codec?: CodecCode }} */
43+
/** @type {{ depth: number, version?: 0 | 1, codec?: number }} */
4444
const pin = {
4545
depth: Infinity
4646
}
@@ -49,8 +49,8 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
4949
pin.version = cid.version
5050
}
5151

52-
if (cid.codec !== 'dag-pb') {
53-
pin.codec = multicodec.getNumber(cid.codec)
52+
if (cid.code !== dagPb.code) {
53+
pin.codec = cid.code
5454
}
5555

5656
await pinstore.put(cidToKey(cid), cbor.encode(pin))
@@ -61,7 +61,7 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
6161
for await (const cid of pinset.loadSet(blockstore, pinRoot, PinTypes.direct)) {
6262
counter++
6363

64-
/** @type {{ depth: number, version?: CID.CIDVersion, codec?: CodecCode }} */
64+
/** @type {{ depth: number, version?: 0 | 1, codec?: number }} */
6565
const pin = {
6666
depth: 0
6767
}
@@ -70,8 +70,8 @@ async function pinsToDatastore (blockstore, datastore, pinstore, onProgress) {
7070
pin.version = cid.version
7171
}
7272

73-
if (cid.codec !== 'dag-pb') {
74-
pin.codec = multicodec.getNumber(cid.codec)
73+
if (cid.code !== dagPb.code) {
74+
pin.codec = cid.code
7575
}
7676

7777
await pinstore.put(cidToKey(cid), cbor.encode(pin))
@@ -98,7 +98,11 @@ async function pinsToDAG (blockstore, datastore, pinstore, onProgress) {
9898
for await (const { key, value } of pinstore.query({})) {
9999
counter++
100100
const pin = cbor.decode(value)
101-
const cid = new CID(pin.version || 0, pin.codec && multicodec.getName(pin.codec) || 'dag-pb', multibase.decode('b' + key.toString().split('/').pop()))
101+
const cid = CID.create(
102+
pin.version || 0,
103+
pin.codec || dagPb.code,
104+
mhd.decode(base32.decode('b' + key.toString().toLowerCase().split('/').pop()))
105+
)
102106

103107
if (pin.depth === 0) {
104108
onProgress((counter / pinCount) * 100, `Reverted direct pin ${cid}`)
@@ -112,16 +116,18 @@ async function pinsToDAG (blockstore, datastore, pinstore, onProgress) {
112116
}
113117

114118
onProgress(100, 'Updating pin root')
115-
const pinRoot = new dagpb.DAGNode(new Uint8Array(), [
116-
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins),
117-
await pinset.storeSet(blockstore, PinTypes.direct, directPins)
118-
])
119-
const buf = pinRoot.serialize()
120-
const cid = await dagpb.util.cid(buf, {
121-
cidVersion: 0
122-
})
119+
const pinRoot = {
120+
Links: [
121+
await pinset.storeSet(blockstore, PinTypes.direct, directPins),
122+
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins)
123+
]
124+
}
125+
const buf = dagPb.encode(pinRoot)
126+
const digest = await sha256.digest(buf)
127+
const cid = CID.createV0(digest)
128+
123129
await blockstore.put(cidToKey(cid), buf)
124-
await datastore.put(PIN_DS_KEY, cid.multihash)
130+
await datastore.put(PIN_DS_KEY, cid.bytes)
125131
}
126132

127133
/**

migrations/migration-9/pin-set.js

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const CID = require('cids')
3+
const { CID } = require('multiformats')
44
const {
55
ipfs: {
66
pin: {
@@ -12,39 +12,35 @@ const {
1212
// @ts-ignore
1313
const fnv1a = require('fnv1a')
1414
const varint = require('varint')
15-
const dagpb = require('ipld-dag-pb')
16-
const DAGNode = require('ipld-dag-pb/src/dag-node/dagNode')
17-
const DAGLink = require('ipld-dag-pb/src/dag-link/dagLink')
18-
const multihash = require('multihashing-async').multihash
15+
const dagPb = require('@ipld/dag-pb')
1916
const { cidToKey, DEFAULT_FANOUT, MAX_ITEMS, EMPTY_KEY } = require('./utils')
2017
const uint8ArrayConcat = require('uint8arrays/concat')
2118
const uint8ArrayCompare = require('uint8arrays/compare')
2219
const uint8ArrayToString = require('uint8arrays/to-string')
2320
const uint8ArrayFromString = require('uint8arrays/from-string')
24-
const uint8ArrayEquals = require('uint8arrays/equals')
21+
const { sha256 } = require('multiformats/hashes/sha2')
2522

2623
/**
2724
* @typedef {import('interface-datastore').Datastore} Datastore
25+
* @typedef {import('@ipld/dag-pb').PBNode} PBNode
2826
*
2927
* @typedef {object} Pin
3028
* @property {CID} key
3129
* @property {Uint8Array} [data]
3230
*/
3331

3432
/**
35-
* @param {Uint8Array | CID} hash
36-
*/
37-
function toB58String (hash) {
38-
return new CID(hash).toBaseEncodedString()
39-
}
40-
41-
/**
42-
* @param {DAGNode} rootNode
33+
* @param {PBNode} rootNode
4334
*/
4435
function readHeader (rootNode) {
4536
// rootNode.data should be a buffer of the format:
4637
// < varint(headerLength) | header | itemData... >
4738
const rootData = rootNode.Data
39+
40+
if (!rootData) {
41+
throw new Error('No data present')
42+
}
43+
4844
const hdrLength = varint.decode(rootData)
4945
const vBytes = varint.decode.bytes
5046

@@ -86,15 +82,15 @@ function hash (seed, key) {
8682
const buffer = new Uint8Array(4)
8783
const dataView = new DataView(buffer.buffer)
8884
dataView.setUint32(0, seed, true)
89-
const encodedKey = uint8ArrayFromString(toB58String(key))
85+
const encodedKey = uint8ArrayFromString(key.toString())
9086
const data = uint8ArrayConcat([buffer, encodedKey], buffer.byteLength + encodedKey.byteLength)
9187

9288
return fnv1a(uint8ArrayToString(data))
9389
}
9490

9591
/**
9692
* @param {Datastore} blockstore
97-
* @param {DAGNode} node
93+
* @param {PBNode} node
9894
* @returns {AsyncGenerator<CID, void, undefined>}
9995
*/
10096
async function * walkItems (blockstore, node) {
@@ -107,10 +103,10 @@ async function * walkItems (blockstore, node) {
107103
// if a fanout bin is not 'empty', dig into and walk its DAGLinks
108104
const linkHash = link.Hash
109105

110-
if (!uint8ArrayEquals(EMPTY_KEY, linkHash.bytes)) {
106+
if (!EMPTY_KEY.equals(linkHash)) {
111107
// walk the links of this fanout bin
112108
const buf = await blockstore.get(cidToKey(linkHash))
113-
const node = dagpb.util.deserialize(buf)
109+
const node = dagPb.decode(buf)
114110

115111
yield * walkItems(blockstore, node)
116112
}
@@ -125,7 +121,7 @@ async function * walkItems (blockstore, node) {
125121

126122
/**
127123
* @param {Datastore} blockstore
128-
* @param {DAGNode} rootNode
124+
* @param {PBNode} rootNode
129125
* @param {string} name
130126
*/
131127
async function * loadSet (blockstore, rootNode, name) {
@@ -136,7 +132,7 @@ async function * loadSet (blockstore, rootNode, name) {
136132
}
137133

138134
const buf = await blockstore.get(cidToKey(link.Hash))
139-
const node = dagpb.util.deserialize(buf)
135+
const node = dagPb.decode(buf)
140136

141137
yield * walkItems(blockstore, node)
142138
}
@@ -164,26 +160,38 @@ function storeItems (blockstore, items) {
164160
const fanoutLinks = []
165161

166162
for (let i = 0; i < DEFAULT_FANOUT; i++) {
167-
fanoutLinks.push(new DAGLink('', 1, EMPTY_KEY))
163+
fanoutLinks.push({
164+
Name: '',
165+
Tsize: 1,
166+
Hash: EMPTY_KEY
167+
})
168168
}
169169

170170
if (pins.length <= MAX_ITEMS) {
171171
const nodes = pins
172172
.map(item => {
173173
return ({
174-
link: new DAGLink('', 1, item.key),
174+
link: {
175+
Name: '',
176+
Tsize: 1,
177+
Hash: item.key
178+
},
175179
data: item.data || new Uint8Array()
176180
})
177181
})
178182
// sorting makes any ordering of `pins` produce the same DAGNode
179183
.sort((a, b) => {
184+
//return a.link.Name.localeCompare(b.link.Name)
180185
return uint8ArrayCompare(a.link.Hash.bytes, b.link.Hash.bytes)
181186
})
182187

183188
const rootLinks = fanoutLinks.concat(nodes.map(item => item.link))
184189
const rootData = uint8ArrayConcat([headerBuf, ...nodes.map(item => item.data)])
185190

186-
return new DAGNode(rootData, rootLinks)
191+
return {
192+
Data: rootData,
193+
Links: rootLinks
194+
}
187195
} else {
188196
// If the array of pins is > MAX_ITEMS, we:
189197
// - distribute the pins among `DEFAULT_FANOUT` bins
@@ -212,22 +220,30 @@ function storeItems (blockstore, items) {
212220
idx++
213221
}
214222

215-
return new DAGNode(headerBuf, fanoutLinks)
223+
return {
224+
Data: headerBuf,
225+
Links: fanoutLinks
226+
}
216227
}
217228

218229
/**
219-
* @param {DAGNode} child
230+
* @param {PBNode} child
220231
* @param {number} binIdx
221232
*/
222233
async function storeChild (child, binIdx) {
223-
const buf = dagpb.util.serialize(child)
224-
const cid = await dagpb.util.cid(buf, {
225-
cidVersion: 0,
226-
hashAlg: multihash.names['sha2-256']
227-
})
234+
const buf = dagPb.encode(child)
235+
const digest = await sha256.digest(buf)
236+
const cid = CID.createV0(digest)
237+
228238
await blockstore.put(cidToKey(cid), buf)
229239

230-
fanoutLinks[binIdx] = new DAGLink('', child.size, cid)
240+
let size = child.Links.reduce((acc, curr) => acc + (curr?.Tsize || 0), 0) + buf.length
241+
242+
fanoutLinks[binIdx] = {
243+
Name: '',
244+
Tsize: size,
245+
Hash: cid
246+
}
231247
}
232248
}
233249
}
@@ -243,15 +259,19 @@ async function storeSet (blockstore, type, cids) {
243259
key: cid
244260
}
245261
}))
246-
const buf = rootNode.serialize()
247-
const cid = await dagpb.util.cid(buf, {
248-
cidVersion: 0,
249-
hashAlg: multihash.names['sha2-256']
250-
})
262+
const buf = dagPb.encode(rootNode)
263+
const digest = await sha256.digest(buf)
264+
const cid = CID.createV0(digest)
251265

252266
await blockstore.put(cidToKey(cid), buf)
253267

254-
return new DAGLink(type, rootNode.size, cid)
268+
let size = rootNode.Links.reduce((acc, curr) => acc + curr.Tsize, 0) + buf.length
269+
270+
return {
271+
Name: type,
272+
Tsize: size,
273+
Hash: cid
274+
}
255275
}
256276

257277
module.exports = {

0 commit comments

Comments
 (0)