Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 5 additions & 4 deletions database/ffldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func makeDbErr(kind database.ErrorKind, desc string) database.Error {

// convertErr converts the passed leveldb error into a database 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 convertErr(desc string, ldbErr error) database.Error {
// Use the driver-specific error code by default. The code below will
// update this with the converted error if it's recognized.
Expand All @@ -156,6 +156,9 @@ func convertErr(desc string, ldbErr error) database.Error {
kind = database.ErrTxClosed
}

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

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

Expand Down Expand Up @@ -2054,9 +2057,7 @@ func initDB(ldb *leveldb.DB) error {

// Write everything as a single batch.
if err := ldb.Write(batch, nil); err != nil {
str := fmt.Sprintf("failed to initialize metadata database: %v",
err)
return convertErr(str, err)
return convertErr("failed to initialize metadata database", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions database/ffldb/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (c *dbCache) commitTreaps(pendingKeys, pendingRemove TreapForEacher) error
var innerErr error
pendingKeys.ForEach(func(k, v []byte) bool {
if dbErr := ldbTx.Put(k, v, nil); dbErr != nil {
str := fmt.Sprintf("failed to put key %q to "+
str := fmt.Sprintf("failed to put key %x to "+
"ldb transaction", k)
innerErr = convertErr(str, dbErr)
return false
Expand All @@ -457,7 +457,7 @@ func (c *dbCache) commitTreaps(pendingKeys, pendingRemove TreapForEacher) error
pendingRemove.ForEach(func(k, v []byte) bool {
if dbErr := ldbTx.Delete(k, nil); dbErr != nil {
str := fmt.Sprintf("failed to delete "+
"key %q from ldb transaction",
"key %x from ldb transaction",
k)
innerErr = convertErr(str, dbErr)
return false
Expand Down