Skip to content

Commit a03dd74

Browse files
committed
Change from using hardcoded Prefixes to the more flex able cid.Format type.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent f420578 commit a03dd74

File tree

13 files changed

+48
-51
lines changed

13 files changed

+48
-51
lines changed

core/commands/add.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ You can now check what blocks have been created by:
285285
md := dagtest.Mock()
286286
emptyDirNode := ft.EmptyDirNode()
287287
// Use the same prefix for the "empty" MFS root as for the file adder.
288-
emptyDirNode.Prefix = *fileAdder.Prefix
288+
emptyDirNode.SetPrefix(fileAdder.Prefix)
289289
mr, err := mfs.NewRoot(req.Context, md, emptyDirNode, nil)
290290
if err != nil {
291291
res.SetError(err, cmdkit.ErrNormal)

core/commands/files.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ Change the cid version or hash function of the root node of a given path.
958958
},
959959
}
960960

961-
func updatePath(rt *mfs.Root, pth string, prefix *cid.Prefix, flush bool) error {
961+
func updatePath(rt *mfs.Root, pth string, prefix cid.Format, flush bool) error {
962962
if prefix == nil {
963963
return nil
964964
}
@@ -1088,7 +1088,7 @@ Remove files or directories.
10881088
},
10891089
}
10901090

1091-
func getPrefixNew(req *cmds.Request) (*cid.Prefix, error) {
1091+
func getPrefixNew(req *cmds.Request) (cid.Format, error) {
10921092
cidVer, cidVerSet := req.Options["cid-version"].(int)
10931093
hashFunStr, hashFunSet := req.Options["hash"].(string)
10941094

@@ -1117,7 +1117,7 @@ func getPrefixNew(req *cmds.Request) (*cid.Prefix, error) {
11171117
return &prefix, nil
11181118
}
11191119

1120-
func getPrefix(req oldcmds.Request) (*cid.Prefix, error) {
1120+
func getPrefix(req oldcmds.Request) (cid.Format, error) {
11211121
cidVer, cidVerSet, _ := req.Option("cid-version").Int()
11221122
hashFunStr, hashFunSet, _ := req.Option("hash").String()
11231123

@@ -1146,7 +1146,7 @@ func getPrefix(req oldcmds.Request) (*cid.Prefix, error) {
11461146
return &prefix, nil
11471147
}
11481148

1149-
func getFileHandle(r *mfs.Root, path string, create bool, prefix *cid.Prefix) (*mfs.File, error) {
1149+
func getFileHandle(r *mfs.Root, path string, create bool, prefix cid.Format) (*mfs.File, error) {
11501150
target, err := mfs.Lookup(r, path)
11511151
switch err {
11521152
case nil:

core/coreunix/add.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Adder struct {
8989
mroot *mfs.Root
9090
unlocker bstore.Unlocker
9191
tempRoot *cid.Cid
92-
Prefix *cid.Prefix
92+
Prefix cid.Format
9393
liveNodes uint64
9494
}
9595

importer/helpers/dagbuilder.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type DagBuilderHelper struct {
2626
nextData []byte // the next item to return.
2727
maxlinks int
2828
batch *ipld.Batch
29-
prefix *cid.Prefix
29+
prefix cid.Format
3030

3131
// Filestore support variables.
3232
// ----------------------------
@@ -54,7 +54,7 @@ type DagBuilderParams struct {
5454
RawLeaves bool
5555

5656
// CID Prefix to use if set
57-
Prefix *cid.Prefix
57+
Prefix cid.Format
5858

5959
// DAGService to write blocks to (required)
6060
Dagserv ipld.DAGService
@@ -146,7 +146,7 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode {
146146
}
147147

148148
// GetPrefix returns the internal `cid.Prefix` set in the builder.
149-
func (db *DagBuilderHelper) GetPrefix() *cid.Prefix {
149+
func (db *DagBuilderHelper) GetPrefix() cid.Format {
150150
return db.prefix
151151
}
152152

@@ -166,7 +166,7 @@ func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {
166166
raw: true,
167167
}, nil
168168
}
169-
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
169+
rawnode, err := dag.NewRawNodeWOpts(data, db.prefix)
170170
if err != nil {
171171
return nil, err
172172
}
@@ -197,7 +197,7 @@ func (db *DagBuilderHelper) NewLeafNode(data []byte) (ipld.Node, error) {
197197
if db.prefix == nil {
198198
return dag.NewRawNode(data), nil
199199
}
200-
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
200+
rawnode, err := dag.NewRawNodeWOpts(data, db.prefix)
201201
if err != nil {
202202
return nil, err
203203
}

importer/helpers/helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) {
6161
}
6262

6363
// SetPrefix sets the CID Prefix
64-
func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) {
64+
func (n *UnixfsNode) SetPrefix(prefix cid.Format) {
6565
n.node.SetPrefix(prefix)
6666
}
6767

merkledag/coding.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
8888
}
8989

9090
if n.cached == nil {
91-
if n.Prefix.Codec == 0 { // unset
92-
n.Prefix = v0CidPrefix
93-
}
94-
c, err := n.Prefix.Sum(n.encoded)
91+
c, err := n.Prefix().Sum(n.encoded)
9592
if err != nil {
9693
return nil, err
9794
}
@@ -129,7 +126,7 @@ func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
129126
}
130127

131128
decnd.cached = c
132-
decnd.Prefix = c.Prefix()
129+
decnd.SetPrefix(c.Prefix())
133130
return decnd, nil
134131
}
135132

merkledag/node.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type ProtoNode struct {
2828
cached *cid.Cid
2929

3030
// Prefix specifies cid version and hashing function
31-
Prefix cid.Prefix
31+
prefix cid.Format
3232
}
3333

3434
var v0CidPrefix = cid.Prefix{
@@ -63,14 +63,21 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) {
6363
}
6464
}
6565

66+
// Prefix returns the CID Prefix for this ProtoNode, it is never nil
67+
func (n *ProtoNode) Prefix() cid.Format {
68+
if n.prefix == nil {
69+
n.prefix = v0CidPrefix
70+
}
71+
return n.prefix
72+
}
73+
6674
// SetPrefix sets the CID prefix if it is non nil, if prefix is nil then
6775
// it resets it the default value
68-
func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) {
76+
func (n *ProtoNode) SetPrefix(prefix cid.Format) {
6977
if prefix == nil {
70-
n.Prefix = v0CidPrefix
78+
n.prefix = v0CidPrefix
7179
} else {
72-
n.Prefix = *prefix
73-
n.Prefix.Codec = cid.DagProtobuf
80+
n.prefix = prefix.WithCodec(cid.DagProtobuf)
7481
n.encoded = nil
7582
n.cached = nil
7683
}
@@ -191,7 +198,7 @@ func (n *ProtoNode) Copy() ipld.Node {
191198
copy(nnode.links, n.links)
192199
}
193200

194-
nnode.Prefix = n.Prefix
201+
nnode.prefix = n.prefix
195202

196203
return nnode
197204
}
@@ -301,11 +308,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
301308
return n.cached
302309
}
303310

304-
if n.Prefix.Codec == 0 {
305-
n.SetPrefix(nil)
306-
}
307-
308-
c, err := n.Prefix.Sum(n.RawData())
311+
c, err := n.Prefix().Sum(n.RawData())
309312
if err != nil {
310313
// programmer error
311314
err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)

merkledag/raw.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ var _ ipld.DecodeBlockFunc = DecodeRawBlock
3636

3737
// NewRawNodeWPrefix creates a RawNode with the hash function
3838
// specified in prefix.
39-
func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) {
40-
prefix.Codec = cid.Raw
41-
if prefix.Version == 0 {
42-
prefix.Version = 1
43-
}
39+
func NewRawNodeWOpts(data []byte, prefix cid.Format) (*RawNode, error) {
40+
prefix = prefix.WithCodec(cid.Raw)
4441
c, err := prefix.Sum(data)
4542
if err != nil {
4643
return nil, err

mfs/dir.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child
6464
}
6565

6666
// GetPrefix gets the CID prefix of the root node
67-
func (d *Directory) GetPrefix() *cid.Prefix {
67+
func (d *Directory) GetPrefix() cid.Format {
6868
return d.unixfsDir.GetPrefix()
6969
}
7070

7171
// SetPrefix sets the CID prefix
72-
func (d *Directory) SetPrefix(prefix *cid.Prefix) {
72+
func (d *Directory) SetPrefix(prefix cid.Format) {
7373
d.unixfsDir.SetPrefix(prefix)
7474
}
7575

mfs/ops.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd ipld.Node) error {
101101
type MkdirOpts struct {
102102
Mkparents bool
103103
Flush bool
104-
Prefix *cid.Prefix
104+
Prefix cid.Format
105105
}
106106

107107
// Mkdir creates a directory at 'path' under the directory 'd', creating

unixfs/hamt/hamt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Shard struct {
5252
tableSize int
5353
tableSizeLg2 int
5454

55-
prefix *cid.Prefix
55+
prefix cid.Format
5656
hashFunc uint64
5757

5858
prefixPadStr string
@@ -124,18 +124,18 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {
124124
ds.children = make([]child, len(pbnd.Links()))
125125
ds.bitfield.SetBytes(pbd.GetData())
126126
ds.hashFunc = pbd.GetHashType()
127-
ds.prefix = &ds.nd.Prefix
127+
ds.prefix = ds.nd.Prefix()
128128

129129
return ds, nil
130130
}
131131

132132
// SetPrefix sets the CID Prefix
133-
func (ds *Shard) SetPrefix(prefix *cid.Prefix) {
133+
func (ds *Shard) SetPrefix(prefix cid.Format) {
134134
ds.prefix = prefix
135135
}
136136

137137
// Prefix gets the CID Prefix, may be nil if unset
138-
func (ds *Shard) Prefix() *cid.Prefix {
138+
func (ds *Shard) Prefix() cid.Format {
139139
return ds.prefix
140140
}
141141

unixfs/io/directory.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var DefaultShardWidth = 256
3030
type Directory interface {
3131

3232
// SetPrefix sets the CID prefix of the root node.
33-
SetPrefix(*cid.Prefix)
33+
SetPrefix(cid.Format)
3434

3535
// AddChild adds a (name, key) pair to the root node.
3636
AddChild(context.Context, string, ipld.Node) error
@@ -52,7 +52,7 @@ type Directory interface {
5252
GetNode() (ipld.Node, error)
5353

5454
// GetPrefix returns the CID Prefix used.
55-
GetPrefix() *cid.Prefix
55+
GetPrefix() cid.Format
5656
}
5757

5858
// TODO: Evaluate removing `dserv` from this layer and providing it in MFS.
@@ -128,7 +128,7 @@ func NewDirectoryFromNode(dserv ipld.DAGService, node ipld.Node) (Directory, err
128128
}
129129

130130
// SetPrefix implements the `Directory` interface.
131-
func (d *BasicDirectory) SetPrefix(prefix *cid.Prefix) {
131+
func (d *BasicDirectory) SetPrefix(prefix cid.Format) {
132132
d.node.SetPrefix(prefix)
133133
}
134134

@@ -180,8 +180,8 @@ func (d *BasicDirectory) GetNode() (ipld.Node, error) {
180180
}
181181

182182
// GetPrefix implements the `Directory` interface.
183-
func (d *BasicDirectory) GetPrefix() *cid.Prefix {
184-
return &d.node.Prefix
183+
func (d *BasicDirectory) GetPrefix() cid.Format {
184+
return d.node.Prefix()
185185
}
186186

187187
// SwitchToSharding returns a HAMT implementation of this directory.
@@ -193,7 +193,7 @@ func (d *BasicDirectory) SwitchToSharding(ctx context.Context) (Directory, error
193193
if err != nil {
194194
return nil, err
195195
}
196-
shard.SetPrefix(&d.node.Prefix)
196+
shard.SetPrefix(d.node.Prefix())
197197
hamtDir.shard = shard
198198

199199
for _, lnk := range d.node.Links() {
@@ -212,7 +212,7 @@ func (d *BasicDirectory) SwitchToSharding(ctx context.Context) (Directory, error
212212
}
213213

214214
// SetPrefix implements the `Directory` interface.
215-
func (d *HAMTDirectory) SetPrefix(prefix *cid.Prefix) {
215+
func (d *HAMTDirectory) SetPrefix(prefix cid.Format) {
216216
d.shard.SetPrefix(prefix)
217217
}
218218

@@ -252,6 +252,6 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
252252
}
253253

254254
// GetPrefix implements the `Directory` interface.
255-
func (d *HAMTDirectory) GetPrefix() *cid.Prefix {
255+
func (d *HAMTDirectory) GetPrefix() cid.Format {
256256
return d.shard.Prefix()
257257
}

unixfs/mod/dagmodifier.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (*cid.Cid, error) {
256256

257257
nd := new(mdag.ProtoNode)
258258
nd.SetData(b)
259-
nd.SetPrefix(&nd0.Prefix)
259+
nd.SetPrefix(nd0.Prefix())
260260
err = dm.dagserv.Add(dm.ctx, nd)
261261
if err != nil {
262262
return nil, err
@@ -282,7 +282,7 @@ func (dm *DagModifier) modifyDag(n ipld.Node, offset uint64) (*cid.Cid, error) {
282282
copy(bytes[offsetPlusN:], origData[offsetPlusN:])
283283
}
284284

285-
nd, err := mdag.NewRawNodeWPrefix(bytes, nd0.Cid().Prefix())
285+
nd, err := mdag.NewRawNodeWOpts(bytes, nd0.Cid().Prefix())
286286
if err != nil {
287287
return nil, err
288288
}
@@ -517,7 +517,7 @@ func dagTruncate(ctx context.Context, n ipld.Node, size uint64, ds ipld.DAGServi
517517
nd.SetData(ft.WrapData(pbn.Data[:size]))
518518
return nd, nil
519519
case *mdag.RawNode:
520-
return mdag.NewRawNodeWPrefix(nd.RawData()[:size], nd.Cid().Prefix())
520+
return mdag.NewRawNodeWOpts(nd.RawData()[:size], nd.Cid().Prefix())
521521
}
522522
}
523523

0 commit comments

Comments
 (0)