Skip to content

Commit ec94b4e

Browse files
committed
Fixup: Fix minor stuff
1 parent c7fd7bd commit ec94b4e

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

go/oasis-node/cmd/storage/checkpoint.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func newImportCmd() *cobra.Command {
187187
for _, entry := range entries {
188188
var ns common.Namespace
189189
if err := ns.UnmarshalHex(entry.Name()); err != nil {
190-
return fmt.Errorf("malformed source runtime ID: %q: %w", entry.Name(), err)
190+
return fmt.Errorf("malformed source runtime ID: %s: %w", entry.Name(), err)
191191
}
192192
rtCpsDir := filepath.Join(runtimeInputDir, entry.Name())
193193

@@ -226,14 +226,14 @@ func createCheckpoints(ctx context.Context, ndb api.NodeDB, ns common.Namespace,
226226

227227
roots, err := ndb.GetRootsForVersion(version)
228228
if err != nil {
229-
return fmt.Errorf("failed to get finalized roots %w", err)
229+
return fmt.Errorf("failed to get roots %w", err)
230230
}
231231
if len(roots) == 0 {
232232
// Empty roots are implicit in NodeDB and therefore not returned by GetRootsForVersion.
233233
// If at this height state DB has an empty state, create a checkpoint for the empty state root,
234234
// so that importing it will still finalize this version (making NodeDB non-empty).
235235
//
236-
// In case of state DB with with multiple empty roots (e.g. state and IO root both empty)
236+
// In case of state DB with multiple empty roots (e.g. state and IO root both empty)
237237
// this will create only one empty checkpoint (duplicate hash), which is safe as we only need
238238
// it to finalize version with implicitly empty state and IO root.
239239
stateRoot := node.Root{
@@ -247,7 +247,7 @@ func createCheckpoints(ctx context.Context, ndb api.NodeDB, ns common.Namespace,
247247

248248
creator, err := checkpoint.NewFileCreator(outputDir, ndb)
249249
if err != nil {
250-
return fmt.Errorf("failed to create checkpoint file creator: %w", err)
250+
return fmt.Errorf("failed to create file creator: %w", err)
251251
}
252252

253253
for _, root := range roots {
@@ -263,10 +263,10 @@ func createCheckpoints(ctx context.Context, ndb api.NodeDB, ns common.Namespace,
263263
func restoreCheckpoints(ctx context.Context, ndb api.NodeDB, ns common.Namespace, inputDir string) error {
264264
isEmpty, err := isEmptyDir(inputDir)
265265
if err != nil {
266-
return fmt.Errorf("aborting checkpoint restoration: failed to inspect input directory: %w", err)
266+
return fmt.Errorf("failed to inspect input directory: %w", err)
267267
}
268268
if isEmpty {
269-
return fmt.Errorf("aborting checkpoint restoration: input directory is empty: %s", inputDir)
269+
return fmt.Errorf("input directory is empty: %s", inputDir)
270270
}
271271
if _, ok := ndb.GetLatestVersion(); ok {
272272
return fmt.Errorf("aborting checkpoint restoration: state db not empty")
@@ -550,21 +550,21 @@ func state(height uint64, stateStore cmtState.Store, blockStore *store.BlockStor
550550
}
551551

552552
func isEmptyDir(dir string) (bool, error) {
553-
// Over-engineering?
553+
// Avoid using os.ReadDir as it reads the whole dir.
554554
f, err := os.Open(dir)
555555
if err != nil {
556556
return false, fmt.Errorf("failed to open directory %q: %w", dir, err)
557557
}
558558
defer f.Close()
559559

560-
_, err = f.Readdir(1)
561-
if errors.Is(err, io.EOF) {
562-
return true, nil // empty
563-
}
564-
if err == nil {
560+
switch _, err = f.Readdir(1); {
561+
case err == nil:
565562
return false, nil
563+
case errors.Is(err, io.EOF):
564+
default:
565+
return false, fmt.Errorf("failed to read directory %q: %w", dir, err)
566566
}
567-
return false, fmt.Errorf("failed to read directory %q: %w", dir, err)
567+
return true, nil
568568
}
569569

570570
func ensureEmptyDir(dir string) error {

go/oasis-node/cmd/storage/checkpoint_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func newTestNodeDB(t *testing.T, ns common.Namespace) (dbAPI.NodeDB, error) {
286286
t.Helper()
287287

288288
return pathbadger.New(&dbAPI.Config{
289-
DB: filepath.Join(t.TempDir(), "db"),
289+
DB: filepath.Join(t.TempDir()),
290290
Namespace: ns,
291291
MemoryOnly: true,
292292
})
@@ -307,7 +307,8 @@ func commitRoot(
307307
defer tree.Close()
308308

309309
for k, v := range data {
310-
require.NoError(t, tree.Insert(ctx, []byte(k), []byte(v)))
310+
err := tree.Insert(ctx, []byte(k), []byte(v))
311+
require.NoError(t, err)
311312
}
312313

313314
_, rootHash, err := tree.Commit(ctx, ns, version)

0 commit comments

Comments
 (0)