Skip to content

Commit

Permalink
fix: bug in trickle layout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Mar 9, 2022
1 parent 878b86e commit 8d0eecd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/file/layout/trickle.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ export const open = options => ({
*/
export const write = (state, leaves) => {
/** @type {Layout.WriteResult<Trickle>} */
let result = { layout: state, nodes: [], leaves: [] }
let result = { layout: { ...state }, nodes: [], leaves: [] }
for (const content of leaves) {
const leaf =
content.byteLength === 0
? { id: EMTPY_LEAF_ID, content }
: { id: result.layout.lastID++, content }
: { id: ++result.layout.lastID, content }

result.leaves.push(leaf)
result = addLeaf(result, result.layout.lastID)
result = addLeaf(result, leaf.id)
}

return result
Expand Down
30 changes: 30 additions & 0 deletions test/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,34 @@ describe("test file", () => {

assert.deepEqual((await blocks).length, 4)
})

it("trickle layout", async function () {
this.timeout(30000)
const content = hashrecur({
byteLength: CHUNK_SIZE * 2,
})

const { writer, ...importer } = FileImporter.createImporter(
{},
FileImporter.configure({
chunker: FixedSize.withMaxChunkSize(CHUNK_SIZE),
fileLayout: Trickle,
})
)
const collector = collect(importer.blocks)

for await (const slice of content) {
writer.write(slice)
}
const link = await writer.close()
const blocks = await collector

assert.deepEqual(link, {
cid: CID.parse(
"bafybeifovo36yvep6m53p3ghmrx6a3ig6c2ydvxp4yjmlqgg74clvudesy"
),
contentByteLength: 524288,
dagByteLength: 524424,
})
})
})

0 comments on commit 8d0eecd

Please sign in to comment.