Skip to content

Commit

Permalink
fix: don't allow go-merkledag to reorder loaded links
Browse files Browse the repository at this point in the history
Ref: #673 (comment)

Ideally we can remove go-merkledag entirely from here because most (all?) of
the deal-making infrastructure uses go-codec-dagpb + go-ipld-prime traversals
and post-decode link-reordering is a subtle difference that impacts CAR
ordering for non-spec-compliant DAG-PB blocks.
  • Loading branch information
rvagg committed Aug 3, 2022
1 parent 12ce769 commit 6769247
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions car/car_offset_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ func (s *CarOffsetWriter) writeBlocks(ctx context.Context, w io.Writer, headerSi
return nil, fmt.Errorf("getting block %s: %w", c, err)
}

// take a copy of the links array before nd.RawData() triggers a sort
links := make([]*format.Link, len(nd.Links()))
copy(links, nd.Links())
byts := nd.RawData()

// Get the size of the block and metadata
ldsize := util.LdSize(nd.Cid().Bytes(), nd.RawData())
ldsize := util.LdSize(nd.Cid().Bytes(), byts)

// Check if the offset from which to start writing is in or before this
// block
Expand All @@ -121,7 +126,7 @@ func (s *CarOffsetWriter) writeBlocks(ctx context.Context, w io.Writer, headerSi

// Write the block data to the writer, starting at the block offset
_, err = skipWrite(w, blockWriteOffset, func(sw io.Writer) (int, error) {
return 0, util.LdWrite(sw, nd.Cid().Bytes(), nd.RawData())
return 0, util.LdWrite(sw, nd.Cid().Bytes(), byts)
})
if err != nil {
return nil, fmt.Errorf("writing CAR block %s: %w", c, err)
Expand All @@ -132,13 +137,13 @@ func (s *CarOffsetWriter) writeBlocks(ctx context.Context, w io.Writer, headerSi
s.blockInfos.Put(nd.Cid(), &BlockInfo{
offset: offset,
size: ldsize,
links: nd.Links(),
links: links,
})

offset = nextBlockOffset

// Return any links from this block to other DAG blocks
return nd.Links(), nil
return links, nil
}

seen := cid.NewSet()
Expand Down

0 comments on commit 6769247

Please sign in to comment.