@@ -25,11 +25,11 @@ func TestTryPruneDir_CachedNotExpired(t *testing.T) {
25
25
_ , sidecars := util .GenerateTestDenebBlockWithSidecar (t , [32 ]byte {}, slot , fieldparams .MaxBlobsPerBlock )
26
26
sc , err := verification .BlobSidecarNoop (sidecars [0 ])
27
27
require .NoError (t , err )
28
- root := fmt . Sprintf ( "%#x" , sc .BlockRoot ())
28
+ rootStr := rootString ( sc .BlockRoot ())
29
29
// This slot is right on the edge of what would need to be pruned, so by adding it to the cache and
30
30
// skipping any other test setup, we can be certain the hot cache path never touches the filesystem.
31
- require .NoError (t , pr .cache .ensure (root , sc .Slot (), 0 ))
32
- pruned , err := pr .tryPruneDir (root , pr .windowSize )
31
+ require .NoError (t , pr .cache .ensure (sc . BlockRoot () , sc .Slot (), 0 ))
32
+ pruned , err := pr .tryPruneDir (rootStr , pr .windowSize )
33
33
require .NoError (t , err )
34
34
require .Equal (t , 0 , pruned )
35
35
}
@@ -43,10 +43,10 @@ func TestTryPruneDir_CachedExpired(t *testing.T) {
43
43
_ , sidecars := util .GenerateTestDenebBlockWithSidecar (t , [32 ]byte {}, slot , 1 )
44
44
sc , err := verification .BlobSidecarNoop (sidecars [0 ])
45
45
require .NoError (t , err )
46
- root := fmt . Sprintf ( "%#x" , sc .BlockRoot ())
47
- require .NoError (t , fs .Mkdir (root , directoryPermissions )) // make empty directory
48
- require .NoError (t , pr .cache .ensure (root , sc .Slot (), 0 ))
49
- pruned , err := pr .tryPruneDir (root , slot + 1 )
46
+ rootStr := rootString ( sc .BlockRoot ())
47
+ require .NoError (t , fs .Mkdir (rootStr , directoryPermissions )) // make empty directory
48
+ require .NoError (t , pr .cache .ensure (sc . BlockRoot () , sc .Slot (), 0 ))
49
+ pruned , err := pr .tryPruneDir (rootStr , slot + 1 )
50
50
require .NoError (t , err )
51
51
require .Equal (t , 0 , pruned )
52
52
})
@@ -61,20 +61,21 @@ func TestTryPruneDir_CachedExpired(t *testing.T) {
61
61
require .NoError (t , bs .Save (scs [1 ]))
62
62
63
63
// check that the root->slot is cached
64
- root := fmt .Sprintf ("%#x" , scs [0 ].BlockRoot ())
65
- cs , cok := bs .pruner .cache .slot (root )
64
+ root := scs [0 ].BlockRoot ()
65
+ rootStr := rootString (root )
66
+ cs , cok := bs .pruner .cache .slot (scs [0 ].BlockRoot ())
66
67
require .Equal (t , true , cok )
67
68
require .Equal (t , slot , cs )
68
69
69
70
// ensure that we see the saved files in the filesystem
70
- files , err := listDir (fs , root )
71
+ files , err := listDir (fs , rootStr )
71
72
require .NoError (t , err )
72
73
require .Equal (t , 2 , len (files ))
73
74
74
- pruned , err := bs .pruner .tryPruneDir (root , slot + 1 )
75
+ pruned , err := bs .pruner .tryPruneDir (rootStr , slot + 1 )
75
76
require .NoError (t , err )
76
77
require .Equal (t , 2 , pruned )
77
- files , err = listDir (fs , root )
78
+ files , err = listDir (fs , rootStr )
78
79
require .ErrorIs (t , err , os .ErrNotExist )
79
80
require .Equal (t , 0 , len (files ))
80
81
})
@@ -92,7 +93,8 @@ func TestTryPruneDir_SlotFromFile(t *testing.T) {
92
93
require .NoError (t , bs .Save (scs [1 ]))
93
94
94
95
// check that the root->slot is cached
95
- root := fmt .Sprintf ("%#x" , scs [0 ].BlockRoot ())
96
+ root := scs [0 ].BlockRoot ()
97
+ rootStr := rootString (root )
96
98
cs , ok := bs .pruner .cache .slot (root )
97
99
require .Equal (t , true , ok )
98
100
require .Equal (t , slot , cs )
@@ -102,14 +104,14 @@ func TestTryPruneDir_SlotFromFile(t *testing.T) {
102
104
require .Equal (t , false , ok )
103
105
104
106
// ensure that we see the saved files in the filesystem
105
- files , err := listDir (fs , root )
107
+ files , err := listDir (fs , rootStr )
106
108
require .NoError (t , err )
107
109
require .Equal (t , 2 , len (files ))
108
110
109
- pruned , err := bs .pruner .tryPruneDir (root , slot + 1 )
111
+ pruned , err := bs .pruner .tryPruneDir (rootStr , slot + 1 )
110
112
require .NoError (t , err )
111
113
require .Equal (t , 2 , pruned )
112
- files , err = listDir (fs , root )
114
+ files , err = listDir (fs , rootStr )
113
115
require .ErrorIs (t , err , os .ErrNotExist )
114
116
require .Equal (t , 0 , len (files ))
115
117
})
@@ -125,24 +127,25 @@ func TestTryPruneDir_SlotFromFile(t *testing.T) {
125
127
require .NoError (t , bs .Save (scs [1 ]))
126
128
127
129
// Evict slot mapping from the cache so that we trigger the file read path.
128
- root := fmt .Sprintf ("%#x" , scs [0 ].BlockRoot ())
130
+ root := scs [0 ].BlockRoot ()
131
+ rootStr := rootString (root )
129
132
bs .pruner .cache .evict (root )
130
133
_ , ok := bs .pruner .cache .slot (root )
131
134
require .Equal (t , false , ok )
132
135
133
136
// Ensure that we see the saved files in the filesystem.
134
- files , err := listDir (fs , root )
137
+ files , err := listDir (fs , rootStr )
135
138
require .NoError (t , err )
136
139
require .Equal (t , 2 , len (files ))
137
140
138
141
// This should use the slotFromFile code (simulating restart).
139
142
// Setting pruneBefore == slot, so that the slot will be outside the window (at the boundary).
140
- pruned , err := bs .pruner .tryPruneDir (root , slot )
143
+ pruned , err := bs .pruner .tryPruneDir (rootStr , slot )
141
144
require .NoError (t , err )
142
145
require .Equal (t , 0 , pruned )
143
146
144
147
// Ensure files are still present.
145
- files , err = listDir (fs , root )
148
+ files , err = listDir (fs , rootStr )
146
149
require .NoError (t , err )
147
150
require .Equal (t , 2 , len (files ))
148
151
})
@@ -316,3 +319,45 @@ func TestListDir(t *testing.T) {
316
319
})
317
320
}
318
321
}
322
+
323
+ func TestRootFromDir (t * testing.T ) {
324
+ cases := []struct {
325
+ name string
326
+ dir string
327
+ err error
328
+ root [32 ]byte
329
+ }{
330
+ {
331
+ name : "happy path" ,
332
+ dir : "0xffff875e1d985c5ccb214894983f2428edb271f0f87b68ba7010e4a99df3b5cb" ,
333
+ root : [32 ]byte {255 , 255 , 135 , 94 , 29 , 152 , 92 , 92 , 203 , 33 , 72 , 148 , 152 , 63 , 36 , 40 ,
334
+ 237 , 178 , 113 , 240 , 248 , 123 , 104 , 186 , 112 , 16 , 228 , 169 , 157 , 243 , 181 , 203 },
335
+ },
336
+ {
337
+ name : "too short" ,
338
+ dir : "0xffff875e1d985c5ccb214894983f2428edb271f0f87b68ba7010e4a99df3b5c" ,
339
+ err : errInvalidRootString ,
340
+ },
341
+ {
342
+ name : "too log" ,
343
+ dir : "0xffff875e1d985c5ccb214894983f2428edb271f0f87b68ba7010e4a99df3b5cbb" ,
344
+ err : errInvalidRootString ,
345
+ },
346
+ {
347
+ name : "missing prefix" ,
348
+ dir : "ffff875e1d985c5ccb214894983f2428edb271f0f87b68ba7010e4a99df3b5cb" ,
349
+ err : errInvalidRootString ,
350
+ },
351
+ }
352
+ for _ , c := range cases {
353
+ t .Run (c .name , func (t * testing.T ) {
354
+ root , err := stringToRoot (c .dir )
355
+ if c .err != nil {
356
+ require .ErrorIs (t , err , c .err )
357
+ return
358
+ }
359
+ require .NoError (t , err )
360
+ require .Equal (t , c .root , root )
361
+ })
362
+ }
363
+ }
0 commit comments