Skip to content

Commit

Permalink
blockchain: Add ldbErr to error description.
Browse files Browse the repository at this point in the history
Add the reason for bad inserts, updates, and deletes to the error
description.
  • Loading branch information
JoeGruffins authored and davecgh committed Feb 8, 2022
1 parent 4481196 commit 1f187c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions blockchain/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5061,7 +5061,7 @@ func migrateUtxoDbBuckets(ctx context.Context, utxoBackend UtxoBackend) error {
numMigrated++
}
if iterErr := iter.Error(); iterErr != nil {
return false, convertLdbErr(iterErr, iterErr.Error())
return false, convertLdbErr(iterErr, "failed to run batch")
}
isFullyDone := err == nil
if (isFullyDone || logProgress) && numMigrated > 0 {
Expand Down Expand Up @@ -5202,7 +5202,7 @@ func moveUtxoDatabase(ctx context.Context, oldPath string, newPath string) error
// Open the database at the old path.
oldDb, err := leveldb.OpenFile(oldPath, &opts)
if err != nil {
str := fmt.Sprintf("failed to open UTXO database at old path: %v", err)
str := "failed to open UTXO database at old path"
return convertLdbErr(err, str)
}

Expand All @@ -5212,7 +5212,7 @@ func moveUtxoDatabase(ctx context.Context, oldPath string, newPath string) error
if err := oldDb.Close(); err != nil {
return convertLdbErr(err, "failed to close UTXO database at old path")
}
str := fmt.Sprintf("failed to open UTXO database at new path: %v", err)
str := "failed to open UTXO database at new path"
return convertLdbErr(err, str)
}

Expand Down Expand Up @@ -5265,7 +5265,7 @@ func moveUtxoDatabase(ctx context.Context, oldPath string, newPath string) error
numMigrated++
}
if iterErr := iter.Error(); iterErr != nil {
return false, convertLdbErr(iterErr, iterErr.Error())
return false, convertLdbErr(iterErr, "failed to run batch")
}
isFullyDone := err == nil
if (isFullyDone || logProgress) && numMigrated > 0 {
Expand Down
10 changes: 6 additions & 4 deletions blockchain/utxobackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ var _ UtxoBackend = (*levelDbUtxoBackend)(nil)

// convertLdbErr converts the passed leveldb error into a context error with an
// equivalent error kind and the passed description. It also sets the passed
// error as the underlying error.
// error as the underlying error and adds its error string to the description.
func convertLdbErr(ldbErr error, desc string) ContextError {
// Use the general UTXO backend error kind by default. The code below will
// update this with the converted error if it's recognized.
Expand All @@ -273,6 +273,9 @@ func convertLdbErr(ldbErr error, desc string) ContextError {
kind = ErrUtxoBackendTxClosed
}

// Include the original error in description.
desc = fmt.Sprintf("%s: %v", desc, ldbErr)

err := contextError(kind, desc)
err.RawErr = ldbErr

Expand Down Expand Up @@ -364,8 +367,7 @@ func LoadUtxoDB(ctx context.Context, params *chaincfg.Params, dataDir string) (*
}
db, err := leveldb.OpenFile(dbPath, &opts)
if err != nil {
str := fmt.Sprintf("failed to open UTXO database: %v", err)
return nil, convertLdbErr(err, str)
return nil, convertLdbErr(err, "failed to open UTXO database")
}

log.Info("UTXO database loaded")
Expand Down Expand Up @@ -573,7 +575,7 @@ func (l *levelDbUtxoBackend) FetchStats() (*UtxoStats, error) {
stats.Total += entry.amount
}
if err := iter.Error(); err != nil {
return nil, convertLdbErr(err, err.Error())
return nil, convertLdbErr(err, "failed to fetch stats")
}

stats.SerializedHash = standalone.CalcMerkleRootInPlace(leaves)
Expand Down
8 changes: 5 additions & 3 deletions blockchain/utxobackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package blockchain

import (
"errors"
"fmt"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestConvertLdbErr(t *testing.T) {
want: ErrUtxoBackend,
}, {
name: "Corruption error",
ldbErr: &ldberrors.ErrCorrupted{},
ldbErr: &ldberrors.ErrCorrupted{Err: errors.New("corrupted")},
desc: "Some corruption error occurred",
want: ErrUtxoBackendCorruption,
}, {
Expand Down Expand Up @@ -78,10 +79,11 @@ func TestConvertLdbErr(t *testing.T) {
continue
}

wantDesc := fmt.Sprintf("%s: %v", test.desc, test.ldbErr)
// Validate the error description.
if gotErr.Description != test.desc {
if gotErr.Description != wantDesc {
t.Errorf("%q: mismatched error description:\nwant: %v\n got: %v\n",
test.name, test.desc, gotErr.Description)
test.name, wantDesc, gotErr.Description)
continue
}

Expand Down

0 comments on commit 1f187c2

Please sign in to comment.