Skip to content

Commit e89177f

Browse files
feat: implemented cid and removed redundant encoding
1 parent 4acda9a commit e89177f

File tree

8 files changed

+186
-135
lines changed

8 files changed

+186
-135
lines changed

dag/dag.go

+23-40
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
cbor "github.com/fxamacker/cbor/v2"
14-
"github.com/multiformats/go-multibase"
1514
)
1615

1716
type fileInfoDirEntry struct {
@@ -42,15 +41,7 @@ func newDirEntry(path string) (fs.DirEntry, error) {
4241
return fileInfoDirEntry{fileInfo: fileInfo}, nil
4342
}
4443

45-
func CreateDag(path string, encoding ...multibase.Encoding) (*Dag, error) {
46-
var e multibase.Encoding
47-
if len(encoding) > 0 {
48-
e = encoding[0]
49-
} else {
50-
e = multibase.Base64
51-
}
52-
encoder := multibase.MustNewEncoder(e)
53-
44+
func CreateDag(path string) (*Dag, error) {
5445
dag := CreateDagBuilder()
5546

5647
fileInfo, err := os.Stat(path)
@@ -68,29 +59,29 @@ func CreateDag(path string, encoding ...multibase.Encoding) (*Dag, error) {
6859
var leaf *DagLeaf
6960

7061
if fileInfo.IsDir() {
71-
leaf, err = processDirectory(dirEntry, &parentPath, dag, encoder, true)
62+
leaf, err = processDirectory(dirEntry, &parentPath, dag, true)
7263
} else {
73-
leaf, err = processFile(dirEntry, &parentPath, dag, encoder, true)
64+
leaf, err = processFile(dirEntry, &parentPath, dag, true)
7465
}
7566

7667
if err != nil {
7768
return nil, err
7869
}
7970

80-
dag.AddLeaf(leaf, encoder, nil)
71+
dag.AddLeaf(leaf, nil)
8172

8273
rootHash := leaf.Hash
8374
return dag.BuildDag(rootHash), nil
8475
}
8576

86-
func processEntry(entry fs.DirEntry, path *string, dag *DagBuilder, encoder multibase.Encoder) (*DagLeaf, error) {
77+
func processEntry(entry fs.DirEntry, path *string, dag *DagBuilder) (*DagLeaf, error) {
8778
var result *DagLeaf
8879
var err error
8980

9081
if entry.IsDir() {
91-
result, err = processDirectory(entry, path, dag, encoder, false)
82+
result, err = processDirectory(entry, path, dag, false)
9283
} else {
93-
result, err = processFile(entry, path, dag, encoder, false)
84+
result, err = processFile(entry, path, dag, false)
9485
}
9586

9687
if err != nil {
@@ -100,7 +91,7 @@ func processEntry(entry fs.DirEntry, path *string, dag *DagBuilder, encoder mult
10091
return result, nil
10192
}
10293

103-
func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, encoder multibase.Encoder, isRoot bool) (*DagLeaf, error) {
94+
func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, isRoot bool) (*DagLeaf, error) {
10495
entryPath := filepath.Join(*path, entry.Name())
10596

10697
relPath, err := filepath.Rel(*path, entryPath)
@@ -120,21 +111,21 @@ func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, encoder
120111
var result *DagLeaf
121112

122113
for _, entry := range entries {
123-
leaf, err := processEntry(entry, &entryPath, dag, encoder)
114+
leaf, err := processEntry(entry, &entryPath, dag)
124115
if err != nil {
125116
return nil, err
126117
}
127118

128119
label := dag.GetNextAvailableLabel()
129120
builder.AddLink(label, leaf.Hash)
130121
leaf.SetLabel(label)
131-
dag.AddLeaf(leaf, encoder, nil)
122+
dag.AddLeaf(leaf, nil)
132123
}
133124

134125
if isRoot {
135-
result, err = builder.BuildRootLeaf(dag, encoder)
126+
result, err = builder.BuildRootLeaf(dag)
136127
} else {
137-
result, err = builder.BuildLeaf(encoder)
128+
result, err = builder.BuildLeaf()
138129
}
139130

140131
if err != nil {
@@ -144,7 +135,7 @@ func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, encoder
144135
return result, nil
145136
}
146137

147-
func processFile(entry fs.DirEntry, path *string, dag *DagBuilder, encoder multibase.Encoder, isRoot bool) (*DagLeaf, error) {
138+
func processFile(entry fs.DirEntry, path *string, dag *DagBuilder, isRoot bool) (*DagLeaf, error) {
148139
entryPath := filepath.Join(*path, entry.Name())
149140

150141
relPath, err := filepath.Rel(*path, entryPath)
@@ -177,22 +168,22 @@ func processFile(entry fs.DirEntry, path *string, dag *DagBuilder, encoder multi
177168
chunkBuilder.SetType(ChunkLeafType)
178169
chunkBuilder.SetData(chunk)
179170

180-
chunkLeaf, err := chunkBuilder.BuildLeaf(encoder)
171+
chunkLeaf, err := chunkBuilder.BuildLeaf()
181172
if err != nil {
182173
return nil, err
183174
}
184175

185176
label := dag.GetNextAvailableLabel()
186177
builder.AddLink(label, chunkLeaf.Hash)
187178
chunkLeaf.SetLabel(label)
188-
dag.AddLeaf(chunkLeaf, encoder, nil)
179+
dag.AddLeaf(chunkLeaf, nil)
189180
}
190181
}
191182

192183
if isRoot {
193-
result, err = builder.BuildRootLeaf(dag, encoder)
184+
result, err = builder.BuildRootLeaf(dag)
194185
} else {
195-
result, err = builder.BuildLeaf(encoder)
186+
result, err = builder.BuildLeaf()
196187
}
197188

198189
if err != nil {
@@ -223,7 +214,7 @@ func CreateDagBuilder() *DagBuilder {
223214
}
224215
}
225216

226-
func (b *DagBuilder) AddLeaf(leaf *DagLeaf, encoder multibase.Encoder, parentLeaf *DagLeaf) error {
217+
func (b *DagBuilder) AddLeaf(leaf *DagLeaf, parentLeaf *DagLeaf) error {
227218
if parentLeaf != nil {
228219
label := GetLabel(leaf.Hash)
229220
_, exists := parentLeaf.Links[label]
@@ -244,27 +235,19 @@ func (b *DagBuilder) BuildDag(root string) *Dag {
244235
}
245236
}
246237

247-
func (dag *Dag) Verify(encoder multibase.Encoder) error {
238+
func (dag *Dag) Verify() error {
248239
err := dag.IterateDag(func(leaf *DagLeaf, parent *DagLeaf) error {
249240
if leaf.Hash == dag.Root {
250-
leafResult, err := leaf.VerifyRootLeaf(encoder)
241+
err := leaf.VerifyRootLeaf()
251242
if err != nil {
252243
return err
253244
}
254-
255-
if !leafResult {
256-
return fmt.Errorf("root leaf %s failed to verify", leaf.Hash)
257-
}
258245
} else {
259-
leafResult, err := leaf.VerifyLeaf(encoder)
246+
err := leaf.VerifyLeaf()
260247
if err != nil {
261248
return err
262249
}
263250

264-
if !leafResult {
265-
return fmt.Errorf("leaf %s failed to verify", leaf.Hash)
266-
}
267-
268251
if !parent.HasLink(leaf.Hash) {
269252
return fmt.Errorf("parent %s does not contain link to child %s", parent.Hash, leaf.Hash)
270253
}
@@ -280,11 +263,11 @@ func (dag *Dag) Verify(encoder multibase.Encoder) error {
280263
return nil
281264
}
282265

283-
func (dag *Dag) CreateDirectory(path string, encoder multibase.Encoder) error {
266+
func (dag *Dag) CreateDirectory(path string) error {
284267
rootHash := dag.Root
285268
rootLeaf := dag.Leafs[rootHash]
286269

287-
err := rootLeaf.CreateDirectoryLeaf(path, dag, encoder)
270+
err := rootLeaf.CreateDirectoryLeaf(path, dag)
288271
if err != nil {
289272
return err
290273
}

dag/dag_test.go

+12-36
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"os"
66
"path/filepath"
77
"testing"
8-
9-
"github.com/multiformats/go-multibase"
108
)
119

1210
func TestFull(t *testing.T) {
@@ -27,18 +25,17 @@ func TestFull(t *testing.T) {
2725

2826
SetChunkSize(4096)
2927

30-
dag, err := CreateDag(input, multibase.Base64)
28+
dag, err := CreateDag(input)
3129
if err != nil {
3230
t.Fatalf("Error: %s", err)
3331
}
3432

35-
encoder := multibase.MustNewEncoder(multibase.Base64)
36-
err = dag.Verify(encoder)
33+
err = dag.Verify()
3734
if err != nil {
3835
t.Fatalf("Error: %s", err)
3936
}
4037

41-
err = dag.CreateDirectory(output, encoder)
38+
err = dag.CreateDirectory(output)
4239
if err != nil {
4340
t.Fatalf("Error: %s", err)
4441
}
@@ -62,39 +59,26 @@ func TestPartial(t *testing.T) {
6259

6360
SetChunkSize(4096)
6461

65-
dag, err := CreateDag(input, multibase.Base64)
62+
dag, err := CreateDag(input)
6663
if err != nil {
6764
t.Fatal("Error: ", err)
6865
}
6966

70-
// Figure out what encoder was used by decoding the root hash
71-
encoding, _, err := multibase.Decode(dag.Root)
72-
if err != nil {
73-
t.Fatal("Failed to decode root hash")
74-
}
75-
76-
// Create encoder from encoding
77-
encoder := multibase.MustNewEncoder(encoding)
78-
7967
// Retrieve the root leaf as this is what you will always start with
8068
parentLeaf := dag.Leafs[dag.Root].Clone()
8169

8270
// Remove the links as the leaves probably wouldn't have them
8371
parentLeaf.Links = map[string]string{}
8472

8573
// Verify the root leaf
86-
result, err := parentLeaf.VerifyRootLeaf(encoder)
74+
err = parentLeaf.VerifyRootLeaf()
8775
if err != nil {
8876
t.Fatal("Failed to verify branch for random leaf")
8977
}
9078

91-
if !result {
92-
t.Fatal("Root leaf verified correctly")
93-
}
94-
9579
// Create a new dag builder and add the root leaf
9680
dagBuilder := CreateDagBuilder()
97-
dagBuilder.AddLeaf(parentLeaf, encoder, nil)
81+
dagBuilder.AddLeaf(parentLeaf, nil)
9882

9983
for ok := true; ok; ok = parentLeaf.Type == DirectoryLeafType {
10084
originalParentLeaf := dag.Leafs[parentLeaf.Hash]
@@ -104,22 +88,18 @@ func TestPartial(t *testing.T) {
10488
}
10589

10690
// Now retrieve a random child of the parent leaf from the original dag to simulate branch verification
107-
randomLeaf := FindRandomChild(originalParentLeaf, dag.Leafs, encoder)
91+
randomLeaf := FindRandomChild(originalParentLeaf, dag.Leafs)
10892
randomLeaf = randomLeaf.Clone()
10993

11094
// Remove the links as the leaf probably wouldn't have them
11195
randomLeaf.Links = map[string]string{}
11296

11397
// Verify the random leaf
114-
result, err := randomLeaf.VerifyLeaf(encoder)
98+
err := randomLeaf.VerifyLeaf()
11599
if err != nil {
116100
t.Fatal("Failed to verify branch for random leaf")
117101
}
118102

119-
if !result {
120-
t.Fatal("Random leaf verified incorrectly")
121-
}
122-
123103
// Retrieve the branch for random child
124104
branch, err := originalParentLeaf.GetBranch(GetLabel(randomLeaf.Hash)) //index
125105
if err != nil {
@@ -128,18 +108,14 @@ func TestPartial(t *testing.T) {
128108

129109
if branch != nil {
130110
// Verify the branch before adding the leaf to the dag
131-
result, err = parentLeaf.VerifyBranch(branch)
111+
err = parentLeaf.VerifyBranch(branch)
132112
if err != nil {
133113
t.Fatal("Failed to verify branch for random leaf")
134114
}
135-
136-
if !result {
137-
t.Fatal("Branch verified correctly")
138-
}
139115
}
140116

141117
// Add the leaf to the dag builder
142-
dagBuilder.AddLeaf(randomLeaf, encoder, parentLeaf)
118+
dagBuilder.AddLeaf(randomLeaf, parentLeaf)
143119

144120
// Set the parent leaf to the new leaf so we can find the next random child if it's a directory
145121
parentLeaf = randomLeaf
@@ -149,13 +125,13 @@ func TestPartial(t *testing.T) {
149125
dag = dagBuilder.BuildDag(dag.Root)
150126

151127
// Verify the dag
152-
err = dag.Verify(encoder)
128+
err = dag.Verify()
153129
if err != nil {
154130
t.Fatal("Error: ", err)
155131
}
156132

157133
// Re-create the directory from the dag
158-
err = dag.CreateDirectory(output, encoder)
134+
err = dag.CreateDirectory(output)
159135
if err != nil {
160136
t.Fatal("Error: ", err)
161137
}

0 commit comments

Comments
 (0)