Fix: adding CidV0 blocks results in CidV1 responses and errors

This commit is contained in:
Hector Sanjuan 2020-05-17 02:39:02 +02:00
parent 076a37cf49
commit a6f16fed05
4 changed files with 30 additions and 6 deletions

View File

@ -864,12 +864,20 @@ func (ipfs *Connector) BlockPut(ctx context.Context, b *api.NodeWithMeta) error
if !ok { if !ok {
return fmt.Errorf("cannot find name for the blocks' CID codec: %x", prefix.Codec) return fmt.Errorf("cannot find name for the blocks' CID codec: %x", prefix.Codec)
} }
q.Set("format", format)
mhType, ok := multihash.Codes[prefix.MhType] mhType, ok := multihash.Codes[prefix.MhType]
if !ok { if !ok {
return fmt.Errorf("cannot find name for the blocks' Multihash type: %x", prefix.MhType) return fmt.Errorf("cannot find name for the blocks' Multihash type: %x", prefix.MhType)
} }
// IPFS behaves differently when using v0 or protobuf which are
// actually the same.
if prefix.Version == 0 {
q.Set("format", "v0")
} else {
q.Set("format", format)
}
q.Set("mhtype", mhType) q.Set("mhtype", mhType)
q.Set("mhlen", strconv.Itoa(prefix.MhLength)) q.Set("mhlen", strconv.Itoa(prefix.MhLength))
@ -894,8 +902,11 @@ func (ipfs *Connector) BlockPut(ctx context.Context, b *api.NodeWithMeta) error
return err return err
} }
// IPFS is too brittle here. CIDv0 != CIDv1. Sending "protobuf" format
// returns CidV1. Sending "v0" format (which maps to protobuf)
// returns CidV0. Leaving this as warning.
if !respCid.Equals(b.Cid) { if !respCid.Equals(b.Cid) {
return fmt.Errorf("blockPut response CID (%s) does not match the block sent (%s)", respCid, b.Cid) logger.Warnf("blockPut response CID (%s) does not match the block sent (%s)", respCid, b.Cid)
} }
return nil return nil
} }

View File

@ -290,12 +290,22 @@ func TestBlockPut(t *testing.T) {
defer mock.Close() defer mock.Close()
defer ipfs.Shutdown(ctx) defer ipfs.Shutdown(ctx)
// CidV1
err := ipfs.BlockPut(ctx, &api.NodeWithMeta{ err := ipfs.BlockPut(ctx, &api.NodeWithMeta{
Data: []byte(test.Cid4Data), Data: []byte(test.Cid4Data),
Cid: test.Cid4, Cid: test.Cid4,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Error(err)
}
// CidV0
err = ipfs.BlockPut(ctx, &api.NodeWithMeta{
Data: []byte(test.Cid5Data),
Cid: test.Cid5,
})
if err != nil {
t.Error(err)
} }
} }

View File

@ -14,7 +14,9 @@ var (
// Cid resulting from block put using blake2b-256 and raw format // Cid resulting from block put using blake2b-256 and raw format
Cid4, _ = cid.Decode("bafk2bzaceawsyhsnrwwy5mtit2emnjfalkxsyq2p2ptd6fuliolzwwjbs42fq") Cid4, _ = cid.Decode("bafk2bzaceawsyhsnrwwy5mtit2emnjfalkxsyq2p2ptd6fuliolzwwjbs42fq")
Cid5, _ = cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmd") // Cid resulting from block put using format "v0" defaults
Cid5, _ = cid.Decode("QmbgmXgsFjxAJ7cEaziL2NDSptHAkPwkEGMmKMpfyYeFXL")
Cid5Data = "Cid5Data"
SlowCid1, _ = cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmd") SlowCid1, _ = cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmd")
CidResolved, _ = cid.Decode("zb2rhiKhUepkTMw7oFfBUnChAN7ABAvg2hXUwmTBtZ6yxuabc") CidResolved, _ = cid.Decode("zb2rhiKhUepkTMw7oFfBUnChAN7ABAvg2hXUwmTBtZ6yxuabc")
// ErrorCid is meant to be used as a Cid which causes errors. i.e. the // ErrorCid is meant to be used as a Cid which causes errors. i.e. the

View File

@ -350,12 +350,13 @@ func (m *IpfsMock) handler(w http.ResponseWriter, r *http.Request) {
} }
// Parse cid from data and format and add to mock block-store // Parse cid from data and format and add to mock block-store
query := r.URL.Query() query := r.URL.Query()
format := cid.Codecs[query.Get("format")] formatStr := query.Get("format")
format := cid.Codecs[formatStr]
mhType := multihash.Names[query.Get("mhtype")] mhType := multihash.Names[query.Get("mhtype")]
mhLen, _ := strconv.Atoi(query.Get("mhLen")) mhLen, _ := strconv.Atoi(query.Get("mhLen"))
var builder cid.Builder var builder cid.Builder
if format == cid.DagProtobuf && mhType == multihash.SHA2_256 { if formatStr == "v0" && mhType == multihash.SHA2_256 {
builder = cid.V0Builder{} builder = cid.V0Builder{}
} else { } else {
builder = cid.V1Builder{ builder = cid.V1Builder{