Skip to content

Commit 324958d

Browse files
committed
factor in
1 parent 7d1521c commit 324958d

1 file changed

Lines changed: 16 additions & 122 deletions

File tree

cmd/proofgen/main.go

Lines changed: 16 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ type inclusionProbe struct {
126126
WantError bool `json:"wantErr"`
127127
}
128128

129-
func inclusionProbes(rootDir string) error {
129+
func generateInclusionProbes(rootDir string, write func(string, inclusionProbe) error) error {
130130
for i, p := range inclusionProofs {
131131
dir := filepath.Join(rootDir, strconv.Itoa(i))
132132
if err := os.MkdirAll(dir, 0755); err != nil {
133133
return err
134134
}
135135

136136
leafHash := rfc6962.DefaultHasher.HashLeaf(leaves[p.leafIdx-1])
137-
if err := corruptedInclusionProbes(dir, p.leafIdx-1, p.size, p.proof, roots[p.size-1], leafHash); err != nil {
137+
if err := corruptedInclusionProbes(dir, p.leafIdx-1, p.size, p.proof, roots[p.size-1], leafHash, write); err != nil {
138138
return err
139139
}
140140
}
@@ -144,7 +144,7 @@ func inclusionProbes(rootDir string) error {
144144
return err
145145
}
146146

147-
if err := staticInclusionProbes(staticDir); err != nil {
147+
if err := staticInclusionProbes(staticDir, write); err != nil {
148148
return err
149149
}
150150

@@ -153,7 +153,7 @@ func inclusionProbes(rootDir string) error {
153153
return err
154154
}
155155

156-
if err := singleEntryInclusionProbes(singleEntryDir); err != nil {
156+
if err := singleEntryInclusionProbes(singleEntryDir, write); err != nil {
157157
return err
158158
}
159159

@@ -202,23 +202,23 @@ func invalidInclusionProof(leafIdx, treeSize uint64, proof [][]byte, root, leafH
202202
return ret
203203
}
204204

205-
func corruptedInclusionProbes(dir string, leafIdx, treeSize uint64, proof [][]byte, root, leafHash []byte) error {
205+
func corruptedInclusionProbes(dir string, leafIdx, treeSize uint64, proof [][]byte, root, leafHash []byte, write func(string, inclusionProbe) error) error {
206206
happyPath := inclusionProbe{leafIdx, treeSize, root, leafHash, proof, "happy path", false}
207-
if err := writeInclusionProbe(dir, happyPath); err != nil {
208-
return nil
207+
if err := write(dir, happyPath); err != nil {
208+
return err
209209
}
210210

211211
probes := invalidInclusionProof(leafIdx, treeSize, proof, root, leafHash)
212212
for _, p := range probes {
213-
if err := writeInclusionProbe(dir, p); err != nil {
213+
if err := write(dir, p); err != nil {
214214
return err
215215
}
216216
}
217217

218218
return nil
219219
}
220220

221-
func singleEntryInclusionProbes(dir string) error {
221+
func singleEntryInclusionProbes(dir string, write func(string, inclusionProbe) error) error {
222222
data := []byte("data")
223223
// Root and leaf hash for 1-entry tree are the same.
224224
hash := rfc6962.DefaultHasher.HashLeaf(data)
@@ -239,15 +239,15 @@ func singleEntryInclusionProbes(dir string) error {
239239
} {
240240
probe := inclusionProbe{0, 1, p.root, p.leaf, proof, p.desc, p.wantErr}
241241

242-
if err := writeInclusionProbe(dir, probe); err != nil {
242+
if err := write(dir, probe); err != nil {
243243
return err
244244
}
245245
}
246246

247247
return nil
248248
}
249249

250-
func staticInclusionProbes(rootDir string) error {
250+
func staticInclusionProbes(rootDir string, write func(string, inclusionProbe) error) error {
251251
proof := [][]byte{}
252252

253253
probes := []struct {
@@ -261,17 +261,17 @@ func staticInclusionProbes(rootDir string) error {
261261
}
262262

263263
randomLeaf := inclusionProbe{p.index, p.size, []byte{}, sha256SomeHash, proof, "random leaf", true}
264-
if err := writeInclusionProbe(dir, randomLeaf); err != nil {
264+
if err := write(dir, randomLeaf); err != nil {
265265
return err
266266
}
267267

268268
emptyRoot := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, []byte{}, proof, "empty root", true}
269-
if err := writeInclusionProbe(dir, emptyRoot); err != nil {
269+
if err := write(dir, emptyRoot); err != nil {
270270
return err
271271
}
272272

273273
emptyRootRandomLeaf := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, sha256SomeHash, proof, "empty root and random leaf", true}
274-
if err := writeInclusionProbe(dir, emptyRootRandomLeaf); err != nil {
274+
if err := write(dir, emptyRootRandomLeaf); err != nil {
275275
return err
276276
}
277277
}
@@ -351,113 +351,7 @@ func shiftSubtreeInclusionProbe(p subtreeInclusionProbe) subtreeInclusionProbe {
351351
}
352352
}
353353

354-
func subtreeInclusionProbes(rootDir string) error {
355-
for i, p := range inclusionProofs {
356-
dir := filepath.Join(rootDir, strconv.Itoa(i))
357-
if err := os.MkdirAll(dir, 0755); err != nil {
358-
return err
359-
}
360-
361-
leafHash := rfc6962.DefaultHasher.HashLeaf(leaves[p.leafIdx-1])
362-
if err := corruptedSubtreeInclusionProbes(dir, p.leafIdx-1, p.size, p.proof, roots[p.size-1], leafHash); err != nil {
363-
return err
364-
}
365-
}
366-
367-
staticDir := filepath.Join(rootDir, "additional")
368-
if err := os.MkdirAll(staticDir, 0755); err != nil {
369-
return err
370-
}
371-
372-
if err := staticSubtreeInclusionProbes(staticDir); err != nil {
373-
return err
374-
}
375-
376-
singleEntryDir := filepath.Join(rootDir, "single-entry")
377-
if err := os.MkdirAll(singleEntryDir, 0755); err != nil {
378-
return err
379-
}
380-
381-
if err := singleEntrySubtreeInclusionProbes(singleEntryDir); err != nil {
382-
return err
383-
}
384-
385-
return nil
386-
}
387-
388-
func corruptedSubtreeInclusionProbes(dir string, leafIdx, treeSize uint64, proof [][]byte, root, leafHash []byte) error {
389-
happyPath := inclusionProbe{leafIdx, treeSize, root, leafHash, proof, "happy path", false}
390-
if err := convertToSubtreeInclusionProbesAndWrite(dir, happyPath); err != nil {
391-
return err
392-
}
393-
394-
probes := invalidInclusionProof(leafIdx, treeSize, proof, root, leafHash)
395-
for _, p := range probes {
396-
if err := convertToSubtreeInclusionProbesAndWrite(dir, p); err != nil {
397-
return err
398-
}
399-
}
400-
401-
return nil
402-
}
403-
404-
func singleEntrySubtreeInclusionProbes(dir string) error {
405-
data := []byte("data")
406-
hash := rfc6962.DefaultHasher.HashLeaf(data)
407-
proof := [][]byte{}
408-
emptyHash := []byte{}
409-
410-
for _, p := range []struct {
411-
root []byte
412-
leaf []byte
413-
desc string
414-
wantErr bool
415-
}{
416-
{hash, hash, "matching root and leaf", false},
417-
{hash, emptyHash, "empty leaf", true},
418-
{emptyHash, hash, "empty root", true},
419-
{emptyHash, emptyHash, "empty root and leaf", true},
420-
} {
421-
probe := inclusionProbe{0, 1, p.root, p.leaf, proof, p.desc, p.wantErr}
422-
if err := convertToSubtreeInclusionProbesAndWrite(dir, probe); err != nil {
423-
return err
424-
}
425-
}
426-
427-
return nil
428-
}
429-
430-
func staticSubtreeInclusionProbes(rootDir string) error {
431-
proof := [][]byte{}
432354

433-
probes := []struct {
434-
index, size uint64
435-
}{{0, 0}, {0, 1}, {1, 0}, {2, 1}}
436-
for i, p := range probes {
437-
dir := filepath.Join(rootDir, strconv.Itoa(i))
438-
err := os.MkdirAll(dir, 0755)
439-
if err != nil {
440-
return err
441-
}
442-
443-
randomLeaf := inclusionProbe{p.index, p.size, []byte{}, sha256SomeHash, proof, "random leaf", true}
444-
if err := convertToSubtreeInclusionProbesAndWrite(dir, randomLeaf); err != nil {
445-
return err
446-
}
447-
448-
emptyRoot := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, []byte{}, proof, "empty root", true}
449-
if err := convertToSubtreeInclusionProbesAndWrite(dir, emptyRoot); err != nil {
450-
return err
451-
}
452-
453-
emptyRootRandomLeaf := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, sha256SomeHash, proof, "empty root and random leaf", true}
454-
if err := convertToSubtreeInclusionProbesAndWrite(dir, emptyRootRandomLeaf); err != nil {
455-
return err
456-
}
457-
}
458-
459-
return nil
460-
}
461355

462356
func errorSubtreeInclusionProbes(rootDir string) error {
463357
dir := filepath.Join(rootDir, "errors")
@@ -789,12 +683,12 @@ func fileName(n string) string {
789683

790684
func main() {
791685
inclusionDir := "testdata/inclusion"
792-
if err := inclusionProbes(inclusionDir); err != nil {
686+
if err := generateInclusionProbes(inclusionDir, writeInclusionProbe); err != nil {
793687
log.Fatalf("writing inclusion test data: %s", err)
794688
}
795689

796690
subtreeInclusionDir := "testdata/subtreeinclusion"
797-
if err := subtreeInclusionProbes(subtreeInclusionDir); err != nil {
691+
if err := generateInclusionProbes(subtreeInclusionDir, convertToSubtreeInclusionProbesAndWrite); err != nil {
798692
log.Fatalf("writing subtree inclusion test data: %s", err)
799693
}
800694
if err := errorSubtreeInclusionProbes(subtreeInclusionDir); err != nil {

0 commit comments

Comments
 (0)