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

chore: upgrade to new multiformats module #98

Merged
merged 14 commits into from
Jul 7, 2021

Conversation

achingbrain
Copy link
Member

BREAKING CHANGE: Uses new CID class

Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Let a nit and a question

migrations/migration-9/index.js Outdated Show resolved Hide resolved
migrations/migration-9/pin-set.js Outdated Show resolved Hide resolved
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins)
]
}
const buf = dagPb.encode(pinRoot)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const block = Block.encode({ value: pinRoot, codec: dagPb, hasher: sha256 }) is available to shorten this slightly, then you can pick block.cid.bytes out of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Block.encode will only ever give me a v1 CID back and we need a v0 CID here?

Copy link
Member Author

Choose a reason for hiding this comment

The 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..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, block.cid.multihash should do it. Alternatives include block.cid.toV0().bytes (which is more explicit but a little wasteful) or adding a cidVersion argument to Block.encode.

Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

cidVersion: 0,
hashAlg: multihash.names['sha2-256']
})
const buf = dagPb.encode(child)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block could help here too

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

fanoutLinks[binIdx] = new DAGLink('', child.size, cid)
let size = child.Links.reduce((acc, curr) => acc + (curr?.Tsize || 0), 0) + buf.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repetitive pattern suggests we might need to pull up a utility or two to dagPb to deal with Tsize

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and, is curr?.Tsize safe to use these days in the platforms we care about? I haven't even bothered touching it, assuming it'll be a year or two away for general use

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repetitive pattern suggests

Yes, this was previously handled by the ipld-dag-pb module, but that feature wasn't ported over so a utility would be helpful.

and, is curr?.Tsize safe to use these days in the platforms we care about?

I thought yes, but it looks like the version of acorn used by the current create-react-app release doesn't support it. A later version does but it'll require them to upgrade to webpack 5 and who knows when that will land.

It's caused a few problems with the last js-ipfs release so we should probably back out our use of optional chaining for the time being.

cidVersion: 0,
hashAlg: multihash.names['sha2-256']
})
const buf = dagPb.encode(rootNode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block

Co-authored-by: Rod Vagg <rod@vagg.org>
await pinset.storeSet(blockstore, PinTypes.recursive, recursivePins)
]
}
const buf = dagPb.encode(pinRoot)
Copy link

Choose a reason for hiding this comment

The 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.

@@ -86,15 +82,15 @@ function hash (seed, key) {
const buffer = new Uint8Array(4)
const dataView = new DataView(buffer.buffer)
dataView.setUint32(0, seed, true)
const encodedKey = uint8ArrayFromString(toB58String(key))
const encodedKey = uint8ArrayFromString(key.toString())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Is this intentional change, seems like keys for v1 CID will get encoded differently now.

@@ -164,14 +160,22 @@ function storeItems (blockstore, items) {
const fanoutLinks = []

for (let i = 0; i < DEFAULT_FANOUT; i++) {
fanoutLinks.push(new DAGLink('', 1, EMPTY_KEY))
fanoutLinks.push({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Seems like having an utility function to create links would have been still useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple factories that match the old DAGLink and DAGNode constructors would probably be good enough to make this pain decrease a little eh?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, since this one is simple I've drafted it up but would like feedback: ipld/js-dag-pb#20

These 4 lines would become fanoutLinks.push(createLink('', 1, EMPTY_KEY)). Same below, and then the return at the end would be return createNode(rootData, rootLinks).

Is this enough of an improvement to warrant API surface area increase?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I really meant is something like link({ cid: EMPTY_KEY, size: 1, name: '' }) just as a sugar.

@lidel lidel marked this pull request as draft July 2, 2021 14:17
@achingbrain achingbrain marked this pull request as ready for review July 6, 2021 17:39
@achingbrain achingbrain merged commit dad30b6 into master Jul 7, 2021
@achingbrain achingbrain deleted the chore/upgrade-multiformats branch July 7, 2021 15:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants