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 {
return fmt.Errorf("cannot find name for the blocks' CID codec: %x", prefix.Codec)
}
q.Set("format", format)
mhType, ok := multihash.Codes[prefix.MhType]
if !ok {
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("mhlen", strconv.Itoa(prefix.MhLength))
@ -894,8 +902,11 @@ func (ipfs *Connector) BlockPut(ctx context.Context, b *api.NodeWithMeta) error
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) {
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
}

View File

@ -290,12 +290,22 @@ func TestBlockPut(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown(ctx)
// CidV1
err := ipfs.BlockPut(ctx, &api.NodeWithMeta{
Data: []byte(test.Cid4Data),
Cid: test.Cid4,
})
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
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")
CidResolved, _ = cid.Decode("zb2rhiKhUepkTMw7oFfBUnChAN7ABAvg2hXUwmTBtZ6yxuabc")
// 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
query := r.URL.Query()
format := cid.Codecs[query.Get("format")]
formatStr := query.Get("format")
format := cid.Codecs[formatStr]
mhType := multihash.Names[query.Get("mhtype")]
mhLen, _ := strconv.Atoi(query.Get("mhLen"))
var builder cid.Builder
if format == cid.DagProtobuf && mhType == multihash.SHA2_256 {
if formatStr == "v0" && mhType == multihash.SHA2_256 {
builder = cid.V0Builder{}
} else {
builder = cid.V1Builder{