From 44e0dd94951fc9596c2187f6d792cbc485611ec2 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Wed, 24 Nov 2021 12:38:08 -0500 Subject: [PATCH] PROPOSAL - Use v1 Cids throughout the codebase --- README.md | 10 +++++----- api/http/api.go | 2 +- db/fetcher/dag.go | 2 +- db/tests/query/all_commits/simple_test.go | 10 +++++----- db/tests/query/commit/simple_test.go | 4 ++-- db/tests/query/latest_commits/simple_test.go | 6 +++--- db/tests/query/simple/with_version_test.go | 6 +++--- merkle/clock/heads.go | 2 +- merkle/clock/heads_test.go | 2 +- merkle/clock/ipld.go | 9 +++++++++ query/graphql/parser/query_test.go | 4 ++-- store/blockstore.go | 2 +- 12 files changed, 34 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 2d2f02aa7d..9d0e1ab34d 100644 --- a/README.md +++ b/README.md @@ -163,24 +163,24 @@ This will return a structure similar to the following, which contains the update { "data": [ { - "cid": "QmPqCtcCPNHoWkHLFvG4aKqDkLLzhVDAVEDSzEs38GHxoo", + "cid": "bafkreibbbtps3cki27zs5w24djizgjfuydqsdgtrz773sfpqechiih5ose", "delta": "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=", "height": 1, "links": [ { - "cid": "QmSom35RYVzYTE7nGsudvomv1pi9ffjEfSFsPZgQRM92v1", + "cid": "bafybeiccmckwnoe3ib3ebybcb6fdyawrji4z4wg2trwswjbfmkeyhdkd4y", "name": "age" }, { - "cid": "QmYJrCcfMmfFp4JcbChLfMLCv8TSHjGwRVHUBgPazWxPga", + "cid": "bafybeieud2zx7evz47vtujk4pyhexcaankozyfhkd4tyjoyp73eeq2xak4", "name": "name" }, { - "cid": "QmXLuVB5CCGqWcdQitingkfRxoVRLKh2jNcnX4UbYnW6Mk", + "cid": "bafybeiefzhsaukwg6dazgyztulstewns2f6a6c6hku4egso2cyowxrxcd4", "name": "points" }, { - "cid": "QmNRQwWjTBTDfAFUHkG8yuKmtbprYQtGs4jYxGJ5fCfXtn", + "cid": "bafybeiabhbk6omcebl52bwxjxbcabpe2ajc22yxrav7mq5f26v7ds76j2m", "name": "verified" } ] diff --git a/api/http/api.go b/api/http/api.go index 212e39e522..f7f39d553f 100644 --- a/api/http/api.go +++ b/api/http/api.go @@ -117,7 +117,7 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) return } - c = cid.NewCidV0(hash) + c = cid.NewCidV1(cid.Raw, hash) } block, err := s.db.GetBlock(ctx, c) diff --git a/db/fetcher/dag.go b/db/fetcher/dag.go index 3608358b6f..591cc59b6d 100644 --- a/db/fetcher/dag.go +++ b/db/fetcher/dag.go @@ -115,7 +115,7 @@ func (hf *HeadFetcher) processKV(kv *core.KeyValue) error { if err != nil { return err } - headCid := cid.NewCidV0(hash) + headCid := cid.NewCidV1(cid.Raw, hash) hf.cid = &headCid return nil } diff --git a/db/tests/query/all_commits/simple_test.go b/db/tests/query/all_commits/simple_test.go index 16910a5fbe..6c52ce526d 100644 --- a/db/tests/query/all_commits/simple_test.go +++ b/db/tests/query/all_commits/simple_test.go @@ -36,14 +36,14 @@ func TestQueryAllCommitsSingleDAG(t *testing.T) { }, Results: []map[string]interface{}{ { - "cid": "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV", + "cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m", "links": []map[string]interface{}{ { - "cid": "QmPaY2DNmd7LtRDpReswc5UTGoU5Q32Py1aEVG7Shq6Np1", + "cid": "bafybeiasnjaz6bohhhqopk77ksivqed5wgbog7575wunleaq57nar6otui", "name": "Age", }, { - "cid": "Qmag2zKKGGQwVSss9pQn3hjTu9opdF5mkJXUR9rt2A651h", + "cid": "bafybeifxin4fbdnc4hrn5tyimnzy53jj6oxtu5kpgohzv5y5wsrpjoih6a", "name": "Name", }, }, @@ -78,11 +78,11 @@ func TestQueryAllCommitsMultipleDAG(t *testing.T) { }, Results: []map[string]interface{}{ { - "cid": "QmQQgYgC3PLFCTwsSgMHHFvFbPEeWDKkbsnvYJwuLP3R8t", + "cid": "bafkreicewiwopwgdrnrdnbh4qnv45yk6vhlmdvdmeri6rue34zpbouyxsq", "height": int64(2), }, { - "cid": "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV", + "cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m", "height": int64(1), }, }, diff --git a/db/tests/query/commit/simple_test.go b/db/tests/query/commit/simple_test.go index ac5e4fa531..790ba31b06 100644 --- a/db/tests/query/commit/simple_test.go +++ b/db/tests/query/commit/simple_test.go @@ -19,7 +19,7 @@ func TestQueryOneCommit(t *testing.T) { test := testUtils.QueryTestCase{ Description: "query for a single block by CID", Query: `query { - commit(cid: "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV") { + commit(cid: "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m") { cid height delta @@ -34,7 +34,7 @@ func TestQueryOneCommit(t *testing.T) { }, Results: []map[string]interface{}{ { - "cid": "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV", + "cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m", "height": int64(1), // cbor encoded delta "delta": []uint8{0xa2, 0x63, 0x41, 0x67, 0x65, 0x15, 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x64, 0x4a, 0x6f, 0x68, 0x6e}, diff --git a/db/tests/query/latest_commits/simple_test.go b/db/tests/query/latest_commits/simple_test.go index 4ae0a6c65e..eeb8cae309 100644 --- a/db/tests/query/latest_commits/simple_test.go +++ b/db/tests/query/latest_commits/simple_test.go @@ -36,14 +36,14 @@ func TestQueryLatestCommits(t *testing.T) { }, Results: []map[string]interface{}{ { - "cid": "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV", + "cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m", "links": []map[string]interface{}{ { - "cid": "QmPaY2DNmd7LtRDpReswc5UTGoU5Q32Py1aEVG7Shq6Np1", + "cid": "bafybeiasnjaz6bohhhqopk77ksivqed5wgbog7575wunleaq57nar6otui", "name": "Age", }, { - "cid": "Qmag2zKKGGQwVSss9pQn3hjTu9opdF5mkJXUR9rt2A651h", + "cid": "bafybeifxin4fbdnc4hrn5tyimnzy53jj6oxtu5kpgohzv5y5wsrpjoih6a", "name": "Name", }, }, diff --git a/db/tests/query/simple/with_version_test.go b/db/tests/query/simple/with_version_test.go index 170c11abf8..1317912d4c 100644 --- a/db/tests/query/simple/with_version_test.go +++ b/db/tests/query/simple/with_version_test.go @@ -44,14 +44,14 @@ func TestQuerySimpleWithEmbeddedLatestCommit(t *testing.T) { "Age": uint64(21), "_version": []map[string]interface{}{ { - "cid": "QmaXdKKsc5GRWXtMytZj4PEf5hFgFxjZaKToQpDY8cAocV", + "cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m", "links": []map[string]interface{}{ { - "cid": "QmPaY2DNmd7LtRDpReswc5UTGoU5Q32Py1aEVG7Shq6Np1", + "cid": "bafybeiasnjaz6bohhhqopk77ksivqed5wgbog7575wunleaq57nar6otui", "name": "Age", }, { - "cid": "Qmag2zKKGGQwVSss9pQn3hjTu9opdF5mkJXUR9rt2A651h", + "cid": "bafybeifxin4fbdnc4hrn5tyimnzy53jj6oxtu5kpgohzv5y5wsrpjoih6a", "name": "Name", }, }, diff --git a/merkle/clock/heads.go b/merkle/clock/heads.go index b91d6a3989..559cf3b637 100644 --- a/merkle/clock/heads.go +++ b/merkle/clock/heads.go @@ -152,7 +152,7 @@ func (hh *heads) List(ctx context.Context) ([]cid.Cid, uint64, error) { if err != nil { return nil, 0, fmt.Errorf("Failed to get CID from key : %w", err) } - headCid := cid.NewCidV0(hash) + headCid := cid.NewCidV1(cid.Raw, hash) height, n := binary.Uvarint(r.Value) if n <= 0 { return nil, 0, errors.New("error decocding height") diff --git a/merkle/clock/heads_test.go b/merkle/clock/heads_test.go index 24df6c8ad3..6ce5072f4b 100644 --- a/merkle/clock/heads_test.go +++ b/merkle/clock/heads_test.go @@ -25,7 +25,7 @@ import ( func newRandomCID() cid.Cid { pref := cid.Prefix{ - Version: 0, + Version: 1, Codec: cid.Raw, MhType: mh.SHA2_256, MhLength: -1, // default length diff --git a/merkle/clock/ipld.go b/merkle/clock/ipld.go index 52792be5d8..b5d89a2d38 100644 --- a/merkle/clock/ipld.go +++ b/merkle/clock/ipld.go @@ -14,6 +14,7 @@ import ( "github.com/sourcenetwork/defradb/core" + mh "github.com/multiformats/go-multihash" // pb "github.com/ipfs/go-ds-crdt/pb" cid "github.com/ipfs/go-cid" ipld "github.com/ipfs/go-ipld-format" @@ -120,6 +121,14 @@ func makeNode(delta core.Delta, heads []cid.Cid) (ipld.Node, error) { } nd := dag.NodeWithData(data) + // The cid builder defaults to v0, we want to be using v1 Cids + nd.SetCidBuilder( + cid.V1Builder{ + Codec: cid.Raw, + MhType: mh.SHA2_256, + MhLength: -1, + }) + // add heads for _, h := range heads { if err = nd.AddRawLink("_head", &ipld.Link{Cid: h}); err != nil { diff --git a/query/graphql/parser/query_test.go b/query/graphql/parser/query_test.go index 19cfd90327..207428d14b 100644 --- a/query/graphql/parser/query_test.go +++ b/query/graphql/parser/query_test.go @@ -111,7 +111,7 @@ func TestQueryParse_Limit_Offset(t *testing.T) { func TestQueryParse_Commit_Latest(t *testing.T) { var query = (` query { - latestCommits(dockey: "Qm123") { + latestCommits(dockey: "baf123") { cid } }`) @@ -128,6 +128,6 @@ func TestQueryParse_Commit_Latest(t *testing.T) { assert.NoError(t, err) commit := q.Queries[0].Selections[0].(*CommitSelect) - assert.Equal(t, commit.DocKey, "Qm123") + assert.Equal(t, commit.DocKey, "baf123") assert.Len(t, commit.Fields, 1) } diff --git a/store/blockstore.go b/store/blockstore.go index 60e15b2041..49c8696526 100644 --- a/store/blockstore.go +++ b/store/blockstore.go @@ -166,7 +166,7 @@ func (bs *bstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { log.Warningf("error parsing key from binary: %s", err) continue } - k := cid.NewCidV0(hash) + k := cid.NewCidV1(cid.Raw, hash) select { case <-ctx.Done(): return