diff --git a/asserter/block.go b/asserter/block.go index 59547cc27..dc9d0b754 100644 --- a/asserter/block.go +++ b/asserter/block.go @@ -486,7 +486,11 @@ func (a *Asserter) Transaction( // invalid transaction identifiers, or a direction not defined by the enum. func (a *Asserter) RelatedTransactions(relatedTransactions []*types.RelatedTransaction) error { if dup := DuplicateRelatedTransaction(relatedTransactions); dup != nil { - return fmt.Errorf("%w: %v", ErrDuplicateRelatedTransaction, dup) + return fmt.Errorf( + "related transaction %s is invalid: %w", + types.PrintStruct(dup), + ErrDuplicateRelatedTransaction, + ) } for i, relatedTransaction := range relatedTransactions { diff --git a/constructor/worker/populator.go b/constructor/worker/populator.go index 41894263a..ef225f0a4 100644 --- a/constructor/worker/populator.go +++ b/constructor/worker/populator.go @@ -43,7 +43,7 @@ func PopulateInput(state string, input string) (string, error) { return value.Raw }) if err != nil { - return "", fmt.Errorf("%w: unable to insert variables", err) + return "", fmt.Errorf("unable to insert variables: %w", err) } if !gjson.Valid(input) { diff --git a/constructor/worker/worker.go b/constructor/worker/worker.go index c61f6988a..9e25107fe 100644 --- a/constructor/worker/worker.go +++ b/constructor/worker/worker.go @@ -85,7 +85,7 @@ func (w *Worker) invokeWorker( case job.GetBlob: return w.GetBlobWorker(ctx, dbTx, input) default: - return "", fmt.Errorf("%w: %s", ErrInvalidActionType, action) + return "", ErrInvalidActionType } } @@ -237,7 +237,7 @@ func GenerateKeyWorker(rawInput string) (string, error) { var input job.GenerateKeyInput err := job.UnmarshalInput([]byte(rawInput), &input) if err != nil { - return "", fmt.Errorf("%w: %s", ErrInvalidInput, err.Error()) + return "", fmt.Errorf("failed to unmarshal input: %w", err) } kp, err := keys.GenerateKeypair(input.CurveType) @@ -303,7 +303,7 @@ func MathWorker(rawInput string) (string, error) { var input job.MathInput err := job.UnmarshalInput([]byte(rawInput), &input) if err != nil { - return "", fmt.Errorf("%w: %s", ErrInvalidInput, err.Error()) + return "", fmt.Errorf("failed to unmarshal input: %w", err) } var result string @@ -880,10 +880,10 @@ func HTTPRequestWorker(rawInput string) (string, error) { if resp.StatusCode != http.StatusOK { return "", fmt.Errorf( - "%w: status code %d with body %s", - ErrActionFailed, + "status code %d with body %s: %w", resp.StatusCode, body, + ErrActionFailed, ) } diff --git a/constructor/worker/worker_test.go b/constructor/worker/worker_test.go index e6315a0d7..0c8d5a7a9 100644 --- a/constructor/worker/worker_test.go +++ b/constructor/worker/worker_test.go @@ -1474,7 +1474,7 @@ func TestJob_Failures(t *testing.T) { OutputPath: "key", }, ProcessedInput: `{"curve_typ": "secp256k1"}`, - Err: ErrInvalidInput, + Err: fmt.Errorf("unknown field \"curve_typ\""), }, helper: &mocks.Helper{}, }, diff --git a/fetcher/utils.go b/fetcher/utils.go index 3a9762cf6..4346c9ff5 100644 --- a/fetcher/utils.go +++ b/fetcher/utils.go @@ -90,9 +90,9 @@ func tryAgain(fetchMsg string, thisBackoff *Backoff, err *Error) *Error { if nextBackoff == backoff.Stop { return &Error{ Err: fmt.Errorf( - "%w: %s", - ErrExhaustedRetries, + "fetch message %s: %w", fetchMsg, + ErrExhaustedRetries, ), } } diff --git a/parser/intent.go b/parser/intent.go index 64ef81a3c..96b19822a 100644 --- a/parser/intent.go +++ b/parser/intent.go @@ -109,9 +109,9 @@ func (p *Parser) ExpectedOperations( if !foundMatch && errExtra { return fmt.Errorf( - "%w: %s", + "operation %s: %w", + types.PrintStruct(obs), ErrExpectedOperationsExtraOperation, - types.PrettyPrintStruct(obs), ) } } diff --git a/storage/database/badger_database.go b/storage/database/badger_database.go index a8b1b29bc..401d09f7d 100644 --- a/storage/database/badger_database.go +++ b/storage/database/badger_database.go @@ -262,13 +262,13 @@ func NewBadgerDatabase( db, err := badger.Open(b.badgerOptions) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrDatabaseOpenFailed, err) + return nil, fmt.Errorf("unable to open database: %w", err) } b.db = db encoder, err := encoder.NewEncoder(b.compressorEntries, b.pool, b.compress) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrCompressorLoadFailed, err) + return nil, fmt.Errorf("unable to load compressor: %w", err) } b.encoder = encoder @@ -286,7 +286,7 @@ func (b *BadgerDatabase) Close(ctx context.Context) error { close(b.closed) if err := b.db.Close(); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrDBCloseFailed, err) + return fmt.Errorf("unable to close badger database: %w", err) } return nil @@ -436,7 +436,7 @@ func (b *BadgerTransaction) Commit(context.Context) error { b.releaseLocks() if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrCommitFailed, err) + return fmt.Errorf("unable to commit transaction: %w", err) } return nil @@ -494,7 +494,7 @@ func (b *BadgerTransaction) Get( if err == badger.ErrKeyNotFound { return false, nil, nil } else if err != nil { - return false, nil, err + return false, nil, fmt.Errorf("unable to get the item of key %s within a transaction: %w", string(key), err) } err = item.Value(func(v []byte) error { @@ -502,7 +502,11 @@ func (b *BadgerTransaction) Get( return err }) if err != nil { - return false, nil, err + return false, nil, fmt.Errorf( + "unable to get the value from the item for key %s: %w", + string(key), + err, + ) } return true, value.Bytes(), nil @@ -539,13 +543,17 @@ func (b *BadgerTransaction) Scan( k := item.Key() err := item.Value(func(v []byte) error { if err := worker(k, v); err != nil { - return fmt.Errorf("%w: worker failed for key %s", err, string(k)) + return fmt.Errorf("worker failed for key %s: %w", string(k), err) } return nil }) if err != nil { - return -1, fmt.Errorf("%w: unable to get value for key %s", err, string(k)) + return -1, fmt.Errorf( + "unable to get the value from the item for key %s: %w", + string(k), + err, + ) } entries++ @@ -568,7 +576,12 @@ func decompressAndSave( // encoded using dictionary compression. decompressed, err := encoder.DecodeRaw(namespace, v) if err != nil { - return -1, -1, fmt.Errorf("%w %s: %v", storageErrs.ErrDecompressFailed, string(k), err) + return -1, -1, fmt.Errorf( + "unable to decompress for namespace %s and input %s: %w", + namespace, + string(v), + err, + ) } err = ioutil.WriteFile( @@ -577,7 +590,11 @@ func decompressAndSave( os.FileMode(utils.DefaultFilePermissions), ) if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrDecompressSaveUnsuccessful, err) + return -1, -1, fmt.Errorf( + "unable to write decompress file %s: %w", + path.Join(tmpDir, types.Hash(string(k))), + err, + ) } return float64(len(decompressed)), float64(len(v)), nil @@ -591,8 +608,7 @@ func decompressAndEncode( decompressed, err := ioutil.ReadFile(path) // #nosec G304 if err != nil { return -1, -1, -1, fmt.Errorf( - "%w for file %s: %v", - storageErrs.ErrLoadFileUnsuccessful, + "unable to read decompress file %s: %w", path, err, ) @@ -600,18 +616,18 @@ func decompressAndEncode( normalCompress, err := encoder.EncodeRaw("", decompressed) if err != nil { - return -1, -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrCompressNormalFailed, err) + return -1, -1, -1, fmt.Errorf("unable to compress normal: %w", err) } dictCompress, err := encoder.EncodeRaw(namespace, decompressed) if err != nil { - return -1, -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrCompressWithDictFailed, err) + return -1, -1, -1, fmt.Errorf("unable to compress with dictionary: %w", err) } // Ensure dict works decompressedDict, err := encoder.DecodeRaw(namespace, dictCompress) if err != nil { - return -1, -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrDecompressWithDictFailed, err) + return -1, -1, -1, fmt.Errorf("unable to decompress with dictionary: %w", err) } if types.Hash(decompressed) != types.Hash(decompressedDict) { @@ -644,12 +660,17 @@ func recompress( func(k []byte, v []byte) error { decompressed, err := badgerDb.Encoder().DecodeRaw(namespace, v) if err != nil { - return fmt.Errorf("%w %s: %v", storageErrs.ErrDecompressFailed, string(k), err) + return fmt.Errorf( + "unable to decompress for namespace %s and input %s: %w", + namespace, + string(v), + err, + ) } newCompressed, err := newCompressor.EncodeRaw(namespace, decompressed) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrCompressWithDictFailed, err) + return fmt.Errorf("unable to compress with dictionary: %w", err) } onDiskSize += float64(len(v)) newSize += float64(len(newCompressed)) @@ -660,7 +681,7 @@ func recompress( false, ) if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrRecompressFailed, err) + return -1, -1, fmt.Errorf("unable to recompress: %w", err) } // Negative savings here means that the new dictionary @@ -691,14 +712,14 @@ func BadgerTrain( WithCompressorEntries(compressorEntries), ) if err != nil { - return -1, -1, fmt.Errorf("%w: unable to load database", err) + return -1, -1, fmt.Errorf("unable to load database: %w", err) } defer badgerDb.Close(ctx) // Create directory to store uncompressed files for training tmpDir, err := utils.CreateTempDir() if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrCreateTempDirectoryFailed, err) + return -1, -1, fmt.Errorf("unable to create temporary directory: %w", err) } defer utils.RemoveTempDir(tmpDir) @@ -724,7 +745,7 @@ func BadgerTrain( v, ) if err != nil { - return fmt.Errorf("%w: unable to decompress and save", err) + return fmt.Errorf("unable to decompress and save: %w", err) } totalUncompressedSize += decompressedSize @@ -774,11 +795,11 @@ func BadgerTrain( dictPath, ) // #nosec G204 if err := cmd.Start(); err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrInvokeZSTDFailed, err) + return -1, -1, fmt.Errorf("unable to start zstd: %w", err) } if err := cmd.Wait(); err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrTrainZSTDFailed, err) + return -1, -1, fmt.Errorf("unable to train zstd: %w", err) } encoder, err := encoder.NewEncoder([]*encoder.CompressorEntry{ @@ -788,7 +809,7 @@ func BadgerTrain( }, }, encoder.NewBufferPool(), true) if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrCompressorLoadFailed, err) + return -1, -1, fmt.Errorf("unable to load compressor: %w", err) } sizeUncompressed := float64(0) @@ -809,7 +830,7 @@ func BadgerTrain( encoder, ) if err != nil { - return fmt.Errorf("%w: unable to decompress and encode", err) + return fmt.Errorf("unable to decompress and encode: %w", err) } sizeUncompressed += decompressed @@ -819,7 +840,7 @@ func BadgerTrain( return nil }) if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrWalkFilesFailed, err) + return -1, -1, fmt.Errorf("unable to walk files: %w", err) } log.Printf( diff --git a/storage/encoder/encoder.go b/storage/encoder/encoder.go index d8470c628..4dccff0f2 100644 --- a/storage/encoder/encoder.go +++ b/storage/encoder/encoder.go @@ -69,8 +69,7 @@ func NewEncoder( b, err := ioutil.ReadFile(path.Clean(entry.DictionaryPath)) if err != nil { return nil, fmt.Errorf( - "%w from %s: %v", - errors.ErrLoadDictFailed, + "unable to load dictionary %s: %w", entry.DictionaryPath, err, ) @@ -100,7 +99,7 @@ func (e *Encoder) Encode(namespace string, object interface{}) ([]byte, error) { buf := e.pool.Get() err := getEncoder(buf).Encode(object) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrObjectEncodeFailed, err) + return nil, fmt.Errorf("unable to encode object: %w", err) } if !e.compress { @@ -109,7 +108,7 @@ func (e *Encoder) Encode(namespace string, object interface{}) ([]byte, error) { output, err := e.EncodeRaw(namespace, buf.Bytes()) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrRawCompressFailed, err) + return nil, fmt.Errorf("unable to compress raw bytes: %w", err) } e.pool.Put(buf) @@ -140,17 +139,17 @@ func (e *Encoder) Decode( if e.compress { decompressed, err := e.DecodeRaw(namespace, input) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrRawDecompressFailed, err) + return fmt.Errorf("unable to decompress raw bytes: %w", err) } if err := getDecoder(bytes.NewReader(decompressed)).Decode(&object); err != nil { - return fmt.Errorf("%w: %v", errors.ErrRawDecodeFailed, err) + return fmt.Errorf("unable to decode object: %w", err) } e.pool.PutByteSlice(decompressed) } else { // nolint:gocritic if err := getDecoder(bytes.NewReader(input)).Decode(&object); err != nil { - return fmt.Errorf("%w: %v", errors.ErrRawDecodeFailed, err) + return fmt.Errorf("unable to decode object: %w", err) } } @@ -176,11 +175,11 @@ func (e *Encoder) encode(input []byte, zstdDict []byte) ([]byte, error) { writer = zstd.NewWriter(buf) } if _, err := writer.Write(input); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBufferWriteFailed, err) + return nil, fmt.Errorf("unable to write to buffer: %w", err) } if err := writer.Close(); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrWriterCloseFailed, err) + return nil, fmt.Errorf("unable to close writer: %w", err) } return buf.Bytes(), nil @@ -196,11 +195,11 @@ func (e *Encoder) decode(b []byte, zstdDict []byte) ([]byte, error) { } if _, err := buf.ReadFrom(reader); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrObjectDecodeFailed, err) + return nil, fmt.Errorf("unable to decode object: %w", err) } if err := reader.Close(); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrReaderCloseFailed, err) + return nil, fmt.Errorf("unable to close reader: %w", err) } return buf.Bytes(), nil @@ -211,7 +210,7 @@ func (e *Encoder) decode(b []byte, zstdDict []byte) ([]byte, error) { func CopyStruct(input interface{}, output interface{}) error { inputString := types.PrintStruct(input) if err := json.Unmarshal([]byte(inputString), &output); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCopyBlockFailed, err) + return fmt.Errorf("unable to copy block: %w", err) } return nil @@ -225,11 +224,11 @@ func (e *Encoder) encodeAndWrite(output *bytes.Buffer, object interface{}) error buf := e.pool.Get() err := getEncoder(buf).Encode(object) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrObjectEncodeFailed, err) + return fmt.Errorf("unable to encode object: %w", err) } if _, err := output.Write(buf.Bytes()); err != nil { - return fmt.Errorf("%w: %v", errors.ErrObjectEncodeFailed, err) + return fmt.Errorf("unable to write buffer: %w", err) } e.pool.Put(buf) @@ -239,7 +238,7 @@ func (e *Encoder) encodeAndWrite(output *bytes.Buffer, object interface{}) error func (e *Encoder) decodeMap(input []byte) (map[string]interface{}, error) { var m map[string]interface{} if err := getDecoder(bytes.NewReader(input)).Decode(&m); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrRawDecodeFailed, err) + return nil, fmt.Errorf("unable to decode object: %w", err) } return m, nil @@ -260,33 +259,39 @@ func (e *Encoder) EncodeAccountCoin( // nolint:gocognit ) ([]byte, error) { output := e.pool.Get() if _, err := output.WriteString(accountCoin.Account.Address); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write account coin account address to buffer: %w", err) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString(accountCoin.Coin.CoinIdentifier.Identifier); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write account oin coin identifier to buffer: %w", err) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString(accountCoin.Coin.Amount.Value); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write account coin amount value to buffer: %w", err) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString(accountCoin.Coin.Amount.Currency.Symbol); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account coin amount currency symbol to buffer: %w", + err, + ) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString( strconv.FormatInt(int64(accountCoin.Coin.Amount.Currency.Decimals), 10), ); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account coin amount currency decimals to buffer: %w", + err, + ) } // Exit early if we don't have any complex data to record (this helps @@ -299,46 +304,61 @@ func (e *Encoder) EncodeAccountCoin( // nolint:gocognit } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCoin.Account.Metadata != nil { if err := e.encodeAndWrite(output, accountCoin.Account.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account coin account metadata to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCoin.Account.SubAccount != nil { if _, err := output.WriteString(accountCoin.Account.SubAccount.Address); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account coin account sub account address: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCoin.Account.SubAccount != nil && accountCoin.Account.SubAccount.Metadata != nil { if err := e.encodeAndWrite(output, accountCoin.Account.SubAccount.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account coin account sub account metadata to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCoin.Coin.Amount.Metadata != nil { if err := e.encodeAndWrite(output, accountCoin.Coin.Amount.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account coin amount metadata to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCoin.Coin.Amount.Currency.Metadata != nil { if err := e.encodeAndWrite(output, accountCoin.Coin.Amount.Currency.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account coin amount currency metadata: %w", + err, + ) } } @@ -374,7 +394,7 @@ func (e *Encoder) DecodeAccountCoin( // nolint:gocognit nextRune := bytes.IndexRune(currentBytes, unicodeRecordSeparator) if nextRune == -1 { if count != amountCurrencyDecimals && count != currencyMetadata { - return fmt.Errorf("%w: next rune is -1 at %d", errors.ErrRawDecodeFailed, count) + return fmt.Errorf("next rune is -1 at %d: %w", count, errors.ErrRawDecodeFailed) } nextRune = len(currentBytes) @@ -407,14 +427,14 @@ func (e *Encoder) DecodeAccountCoin( // nolint:gocognit case amountCurrencyDecimals: i, err := strconv.ParseInt(string(val), 10, 32) if err != nil { - return fmt.Errorf("%w: %s", errors.ErrRawDecodeFailed, err.Error()) + return fmt.Errorf("unable to parse int: %w", err) } accountCoin.Coin.Amount.Currency.Decimals = int32(i) case accountMetadata: m, err := e.decodeMap(val) if err != nil { - return fmt.Errorf("%w: account metadata %s", errors.ErrRawDecodeFailed, err.Error()) + return fmt.Errorf("unable to decode map: %w", err) } accountCoin.Account.Metadata = m @@ -430,9 +450,8 @@ func (e *Encoder) DecodeAccountCoin( // nolint:gocognit m, err := e.decodeMap(val) if err != nil { return fmt.Errorf( - "%w: subaccount metadata %s", - errors.ErrRawDecodeFailed, - err.Error(), + "unable to decode map: %w", + err, ) } @@ -440,7 +459,7 @@ func (e *Encoder) DecodeAccountCoin( // nolint:gocognit case amountMetadata: m, err := e.decodeMap(val) if err != nil { - return fmt.Errorf("%w: amount metadata %s", errors.ErrRawDecodeFailed, err.Error()) + return fmt.Errorf("unable to decode map: %w", err) } accountCoin.Coin.Amount.Metadata = m @@ -448,15 +467,14 @@ func (e *Encoder) DecodeAccountCoin( // nolint:gocognit m, err := e.decodeMap(val) if err != nil { return fmt.Errorf( - "%w: currency metadata %s", - errors.ErrRawDecodeFailed, - err.Error(), + "unable to decode map: %w", + err, ) } accountCoin.Coin.Amount.Currency.Metadata = m default: - return fmt.Errorf("%w: count %d > end", errors.ErrRawDecodeFailed, count) + return fmt.Errorf("count %d > end: %w", count, errors.ErrRawDecodeFailed) } handleNext: @@ -489,21 +507,30 @@ func (e *Encoder) EncodeAccountCurrency( // nolint:gocognit ) ([]byte, error) { output := e.pool.Get() if _, err := output.WriteString(accountCurrency.Account.Address); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account currency account address to buffer: %w", + err, + ) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString(accountCurrency.Currency.Symbol); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account currency currency symbol to buffer: %w", + err, + ) } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if _, err := output.WriteString( strconv.FormatInt(int64(accountCurrency.Currency.Decimals), 10), ); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account currency currency decimals to buffer: %w", + err, + ) } // Exit early if we don't have any complex data to record (this helps @@ -515,39 +542,51 @@ func (e *Encoder) EncodeAccountCurrency( // nolint:gocognit } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCurrency.Account.Metadata != nil { if err := e.encodeAndWrite(output, accountCurrency.Account.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account currency account metadata to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCurrency.Account.SubAccount != nil { if _, err := output.WriteString(accountCurrency.Account.SubAccount.Address); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account currency account sub account to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCurrency.Account.SubAccount != nil && accountCurrency.Account.SubAccount.Metadata != nil { if err := e.encodeAndWrite(output, accountCurrency.Account.SubAccount.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to encode and write account currency account sub account metadata to buffer: %w", + err, + ) } } if _, err := output.WriteRune(unicodeRecordSeparator); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf("unable to write unicode record separator to buffer: %w", err) } if accountCurrency.Currency.Metadata != nil { if err := e.encodeAndWrite(output, accountCurrency.Currency.Metadata); err != nil { - return nil, fmt.Errorf("%w: %s", errors.ErrObjectEncodeFailed, err.Error()) + return nil, fmt.Errorf( + "unable to write account currency currency metadata to buffer: %w", + err, + ) } } @@ -580,7 +619,7 @@ func (e *Encoder) DecodeAccountCurrency( // nolint:gocognit nextRune := bytes.IndexRune(currentBytes, unicodeRecordSeparator) if nextRune == -1 { if count != currencyDecimals && count != currencyMetadata { - return fmt.Errorf("%w: next rune is -1 at %d", errors.ErrRawDecodeFailed, count) + return fmt.Errorf("next rune is -1 at %d: %w", count, errors.ErrRawDecodeFailed) } nextRune = len(currentBytes) @@ -603,14 +642,14 @@ func (e *Encoder) DecodeAccountCurrency( // nolint:gocognit case currencyDecimals: i, err := strconv.ParseInt(string(val), 10, 32) if err != nil { - return fmt.Errorf("%w: %s", errors.ErrRawDecodeFailed, err.Error()) + return fmt.Errorf("unable to parse int for %s: %w", string(val), err) } accountCurrency.Currency.Decimals = int32(i) case accountMetadata: m, err := e.decodeMap(val) if err != nil { - return fmt.Errorf("%w: account metadata %s", errors.ErrRawDecodeFailed, err.Error()) + return fmt.Errorf("unable to decode map: %w", err) } accountCurrency.Account.Metadata = m @@ -625,27 +664,19 @@ func (e *Encoder) DecodeAccountCurrency( // nolint:gocognit m, err := e.decodeMap(val) if err != nil { - return fmt.Errorf( - "%w: subaccount metadata %s", - errors.ErrRawDecodeFailed, - err.Error(), - ) + return fmt.Errorf("unable to decode map: %w", err) } accountCurrency.Account.SubAccount.Metadata = m case currencyMetadata: m, err := e.decodeMap(val) if err != nil { - return fmt.Errorf( - "%w: currency metadata %s", - errors.ErrRawDecodeFailed, - err.Error(), - ) + return fmt.Errorf("unable to decode map: %w", err) } accountCurrency.Currency.Metadata = m default: - return fmt.Errorf("%w: count %d > end", errors.ErrRawDecodeFailed, count) + return fmt.Errorf("count %d > end: %w", count, errors.ErrRawDecodeFailed) } handleNext: diff --git a/storage/errors/errors.go b/storage/errors/errors.go index a0d9666c3..1ed9cc5eb 100644 --- a/storage/errors/errors.go +++ b/storage/errors/errors.go @@ -22,236 +22,77 @@ import ( // Badger Storage Errors var ( - ErrDatabaseOpenFailed = errors.New("unable to open database") - ErrCompressorLoadFailed = errors.New("unable to load compressor") - ErrDBCloseFailed = errors.New("unable to close database") - ErrCommitFailed = errors.New("unable to commit transaction") - ErrScanGetValueFailed = errors.New("unable to get value for key") - ErrScanWorkerFailed = errors.New("worker failed") - ErrDecompressFailed = errors.New("unable to decompress") - ErrDecompressSaveUnsuccessful = errors.New("unable to store decompressed file") - ErrLoadFileUnsuccessful = errors.New("unable to load file") - ErrCompressNormalFailed = errors.New("unable to compress normal") - ErrCompressWithDictFailed = errors.New("unable to compress with dictionary") - ErrDecompressWithDictFailed = errors.New("unable to decompress with dictionary") - ErrDecompressOutputMismatch = errors.New("decompressed dictionary output does not match") - ErrRecompressFailed = errors.New("unable to recompress") - ErrCreateTempDirectoryFailed = errors.New("unable to create temporary directory") - ErrMaxEntries = errors.New("max entries reached") - ErrScanFailed = errors.New("unable to scan") - ErrNoEntriesFoundInNamespace = errors.New("found 0 entries for namespace") - ErrInvokeZSTDFailed = errors.New("unable to start zstd") - ErrTrainZSTDFailed = errors.New("unable to train zstd") - ErrWalkFilesFailed = errors.New("unable to walk files") + ErrDBCloseFailed = errors.New("unable to close database") + ErrScanGetValueFailed = errors.New("unable to get value for key") + ErrScanWorkerFailed = errors.New("worker failed") + ErrDecompressOutputMismatch = errors.New("decompressed dictionary output does not match") + ErrMaxEntries = errors.New("max entries reached") + ErrScanFailed = errors.New("unable to scan") + ErrNoEntriesFoundInNamespace = errors.New("found 0 entries for namespace") BadgerStorageErrs = []error{ - ErrDatabaseOpenFailed, - ErrCompressorLoadFailed, - ErrDBCloseFailed, - ErrCommitFailed, ErrScanGetValueFailed, ErrScanWorkerFailed, - ErrDecompressFailed, - ErrDecompressSaveUnsuccessful, - ErrLoadFileUnsuccessful, - ErrCompressNormalFailed, - ErrCompressWithDictFailed, - ErrDecompressWithDictFailed, ErrDecompressOutputMismatch, - ErrRecompressFailed, - ErrCreateTempDirectoryFailed, ErrMaxEntries, ErrScanFailed, ErrNoEntriesFoundInNamespace, - ErrInvokeZSTDFailed, - ErrTrainZSTDFailed, - ErrWalkFilesFailed, } ) // Broadcast Storage Errors var ( - ErrBroadcastTxStale = errors.New("unable to handle stale transaction") - ErrBroadcastTxConfirmed = errors.New( - "unable to handle confirmed transaction", - ) - ErrBroadcastFindTxFailed = errors.New( - "unable to determine if transaction was seen", - ) - ErrBroadcastEncodeUpdateFailed = errors.New("unable to encode updated broadcast") - ErrBroadcastUpdateFailed = errors.New("unable to update broadcast") - ErrBroadcastDeleteConfirmedTxFailed = errors.New("unable to delete confirmed broadcast") - ErrBroadcastInvokeBlockHandlersFailed = errors.New("unable to handle block") - ErrBroadcastFailed = errors.New( - "unable to broadcast pending transactions", - ) - ErrBroadcastDBGetFailed = errors.New( - "unable to determine if already broadcasting transaction", - ) ErrBroadcastAlreadyExists = errors.New("already broadcasting transaction") - ErrBroadcastEncodeFailed = errors.New("unable to encode broadcast") - ErrBroadcastSetFailed = errors.New("unable to set broadcast") - ErrBroadcastScanFailed = errors.New("unable to scan for all broadcasts") - ErrBroadcastDecodeFailed = errors.New("unable to decode broadcast") ErrBroadcastCommitUpdateFailed = errors.New("unable to commit broadcast update") ErrBroadcastIdentifierMismatch = errors.New( "unexpected transaction hash returned by broadcast", ) - ErrBroadcastGetCurrentBlockIdentifierFailed = errors.New( - "unable to get current block identifier", - ) - ErrBroadcastAtTipFailed = errors.New("unable to determine if at tip") - ErrBroadcastGetAllFailed = errors.New("unable to get all broadcasts") - ErrBroadcastDeleteFailed = errors.New("unable to delete broadcast") - ErrBroadcastHandleFailureUnsuccessful = errors.New("unable to handle broadcast failure") - ErrBroadcastCommitDeleteFailed = errors.New("unable to commit broadcast delete") - ErrBroadcastPerformFailed = errors.New("unable to perform broadcast") BroadcastStorageErrs = []error{ - ErrBroadcastTxStale, - ErrBroadcastTxConfirmed, - ErrBroadcastFindTxFailed, - ErrBroadcastEncodeUpdateFailed, - ErrBroadcastUpdateFailed, - ErrBroadcastDeleteConfirmedTxFailed, - ErrBroadcastInvokeBlockHandlersFailed, - ErrBroadcastFailed, - ErrBroadcastDBGetFailed, ErrBroadcastAlreadyExists, - ErrBroadcastEncodeFailed, - ErrBroadcastSetFailed, - ErrBroadcastScanFailed, - ErrBroadcastDecodeFailed, ErrBroadcastCommitUpdateFailed, ErrBroadcastIdentifierMismatch, - ErrBroadcastGetCurrentBlockIdentifierFailed, - ErrBroadcastAtTipFailed, - ErrBroadcastGetAllFailed, - ErrBroadcastDeleteFailed, - ErrBroadcastHandleFailureUnsuccessful, - ErrBroadcastCommitDeleteFailed, - ErrBroadcastPerformFailed, } ) // Coin Storage Errors var ( - ErrCoinQueryFailed = errors.New("unable to query for coin") - ErrCoinDecodeFailed = errors.New("unable to decode coin") - ErrCoinGetFailed = errors.New("unable to get coin") - ErrCoinAddFailed = errors.New("unable to add coin") - ErrReconciliationUpdateCommitFailed = errors.New("unable to commit last reconciliation update") - ErrCoinDataEncodeFailed = errors.New("unable to encode coin data") - ErrCoinStoreFailed = errors.New("unable to store coin") - ErrAccountCoinStoreFailed = errors.New("unable to store account coin") - ErrAccountCoinQueryFailed = errors.New("unable to query coins for account") - ErrCoinDeleteFailed = errors.New("unable to delete coin") - ErrOperationParseFailed = errors.New("unable to parse operation success") - ErrUnableToDetermineIfSkipOperation = errors.New( - "unable to to determine if should skip operation", - ) - ErrDuplicateCoinFound = errors.New("duplicate coin found") - ErrCoinRemoveFailed = errors.New("unable to remove coin") - ErrAccountIdentifierQueryFailed = errors.New("unable to query account identifier") - ErrCurrentBlockGetFailed = errors.New("unable to get current block identifier") - ErrCoinLookupFailed = errors.New("unable to lookup coin") - ErrUTXOBalanceGetFailed = errors.New("unable to get utxo balance") - ErrCoinParseFailed = errors.New("unable to parse amount for coin") - ErrCoinImportFailed = errors.New("unable to import coins") - ErrCoinNotFound = errors.New("coin not found") + ErrDuplicateCoinFound = errors.New("duplicate coin found") + ErrCoinParseFailed = errors.New("unable to parse amount for coin") + ErrCoinNotFound = errors.New("coin not found") CoinStorageErrs = []error{ - ErrCoinQueryFailed, - ErrCoinDecodeFailed, - ErrCoinGetFailed, - ErrCoinAddFailed, - ErrReconciliationUpdateCommitFailed, - ErrCoinDataEncodeFailed, - ErrCoinStoreFailed, - ErrAccountCoinStoreFailed, - ErrAccountCoinQueryFailed, - ErrCoinDeleteFailed, - ErrOperationParseFailed, - ErrUnableToDetermineIfSkipOperation, ErrDuplicateCoinFound, - ErrCoinRemoveFailed, - ErrAccountIdentifierQueryFailed, - ErrCurrentBlockGetFailed, - ErrCoinLookupFailed, - ErrUTXOBalanceGetFailed, ErrCoinParseFailed, - ErrCoinImportFailed, ErrCoinNotFound, } ) // Compressor Errors var ( - ErrLoadDictFailed = errors.New("unable to load dictionary") - ErrObjectEncodeFailed = errors.New("unable to encode object") - ErrRawCompressFailed = errors.New("unable to compress raw bytes") - ErrRawDecompressFailed = errors.New("unable to decompress raw bytes") - ErrRawDecodeFailed = errors.New("unable to decode bytes") - ErrBufferWriteFailed = errors.New("unable to write to buffer") - ErrWriterCloseFailed = errors.New("unable to close writer") - ErrObjectDecodeFailed = errors.New("unable to decode object") - ErrReaderCloseFailed = errors.New("unable to close reader") - ErrCopyBlockFailed = errors.New("unable to copy block") + ErrWriterCloseFailed = errors.New("unable to close writer") + ErrObjectDecodeFailed = errors.New("unable to decode object") + ErrCopyBlockFailed = errors.New("unable to copy block") + ErrRawDecodeFailed = errors.New("unable to decode raw bytes") CompressorErrs = []error{ - ErrLoadDictFailed, - ErrObjectEncodeFailed, - ErrRawCompressFailed, - ErrRawDecompressFailed, - ErrRawDecodeFailed, - ErrBufferWriteFailed, ErrWriterCloseFailed, ErrObjectDecodeFailed, - ErrReaderCloseFailed, ErrCopyBlockFailed, + ErrRawDecodeFailed, } ) // Job Storage Errors var ( - ErrJobsGetAllFailed = errors.New("unable to get all jobs") - ErrJobIdentifierDecodeFailed = errors.New("unable to decode existing identifier") - ErrJobGetFailed = errors.New("unable to get job") - ErrJobIdentifierEncodeFailed = errors.New("unable to encode job identifier") - ErrJobIdentifierUpdateFailed = errors.New("unable to update job identifier") - ErrJobIdentifiersEncodeAllFailed = errors.New("unable to encode identifiers") - ErrJobIdentifiersSetAllFailed = errors.New("unable to set identifiers") - ErrJobIdentifierRemoveFailed = errors.New("unable to remove identifier") - ErrJobIdentifierNotFound = errors.New("identifier not found") - ErrJobRemoveFailed = errors.New("unable to remove job") - ErrJobAddFailed = errors.New("unable to add job") - ErrJobIdentifierGetFailed = errors.New("unable to get next identifier") - ErrJobUpdateOldFailed = errors.New("unable to update terminal job") - ErrJobEncodeFailed = errors.New("unable to encode job") - ErrJobUpdateFailed = errors.New("unable to update job") - ErrJobMetadataUpdateFailed = errors.New("unable to update metadata") - ErrJobDoesNotExist = errors.New("job does not exist") - ErrJobDecodeFailed = errors.New("unable to decode job") + ErrJobIdentifierNotFound = errors.New("identifier not found") + ErrJobUpdateOldFailed = errors.New("unable to update terminal job") + ErrJobDoesNotExist = errors.New("job does not exist") JobStorageErrs = []error{ - ErrJobsGetAllFailed, - ErrJobIdentifierDecodeFailed, - ErrJobGetFailed, - ErrJobIdentifierEncodeFailed, - ErrJobIdentifierUpdateFailed, - ErrJobIdentifiersEncodeAllFailed, - ErrJobIdentifiersSetAllFailed, - ErrJobIdentifierRemoveFailed, ErrJobIdentifierNotFound, - ErrJobRemoveFailed, - ErrJobAddFailed, - ErrJobIdentifierGetFailed, ErrJobUpdateOldFailed, - ErrJobEncodeFailed, - ErrJobUpdateFailed, - ErrJobMetadataUpdateFailed, ErrJobDoesNotExist, - ErrJobDecodeFailed, } ) @@ -259,47 +100,18 @@ var ( var ( // ErrAddrExists is returned when key storage already // contains an address. - ErrAddrExists = errors.New("address already exists") - - ErrAddrCheckIfExistsFailed = errors.New("unable to check if address exists") - ErrSerializeKeyFailed = errors.New("unable to serialize key") - ErrStoreKeyFailed = errors.New("unable to store key") - ErrCommitKeyFailed = errors.New("unable to commit new key to db") - ErrAddrGetFailed = errors.New("unable to get address") - ErrAddrNotFound = errors.New("address not found") - ErrParseSavedKeyFailed = errors.New("unable to parse saved key") - ErrKeyScanFailed = errors.New("database scan for keys failed") - ErrParseKeyPairFailed = errors.New("unable to parse key pair") - ErrKeyGetFailed = errors.New("unable to get key") - ErrSignerCreateFailed = errors.New("unable to create signer") - ErrDetermineSigTypeFailed = errors.New("cannot determine signature type for payload") - ErrSignPayloadFailed = errors.New("unable to to sign payload") - ErrAddrsGetAllFailed = errors.New("unable to get addresses") - ErrNoAddrAvailable = errors.New("no addresses available") - ErrAddrImportFailed = errors.New("unable to import prefunded account") - ErrPrefundedAcctStoreFailed = errors.New("unable to store prefunded account") - ErrRandomAddress = errors.New("cannot select random address") + ErrAddrExists = errors.New("key already exists") + ErrAddrNotFound = errors.New("address not found") + ErrParseKeyPairFailed = errors.New("unable to parse key pair") + ErrDetermineSigTypeFailed = errors.New("cannot determine signature type for payload") + ErrNoAddrAvailable = errors.New("no addresses available") KeyStorageErrs = []error{ ErrAddrExists, - ErrAddrCheckIfExistsFailed, - ErrSerializeKeyFailed, - ErrStoreKeyFailed, - ErrCommitKeyFailed, - ErrAddrGetFailed, ErrAddrNotFound, - ErrParseSavedKeyFailed, - ErrKeyScanFailed, ErrParseKeyPairFailed, - ErrKeyGetFailed, - ErrSignerCreateFailed, ErrDetermineSigTypeFailed, - ErrSignPayloadFailed, - ErrAddrsGetAllFailed, ErrNoAddrAvailable, - ErrAddrImportFailed, - ErrPrefundedAcctStoreFailed, - ErrRandomAddress, } ) @@ -336,6 +148,8 @@ var ( ErrHelperHandlerMissing = errors.New("balance storage helper or handler is missing") + ErrInvalidCurrency = errors.New("invalid currency") + BalanceStorageErrs = []error{ ErrNegativeBalance, ErrInvalidLiveBalance, @@ -366,82 +180,42 @@ var ( // hash cannot be stored because it is a duplicate. ErrDuplicateTransactionHash = errors.New("duplicate transaction hash") - ErrBlockGetFailed = errors.New("unable to get block") - ErrTransactionGetFailed = errors.New("could not get transaction") - ErrBlockEncodeFailed = errors.New("unable to encode block") - ErrBlockStoreFailed = errors.New("unable to store block") - ErrBlockIndexStoreFailed = errors.New("unable to store block index") - ErrBlockIdentifierUpdateFailed = errors.New("unable to update head block identifier") - ErrBlockCopyFailed = errors.New("unable to copy block") - ErrTransactionHashStoreFailed = errors.New("unable to store transaction hash") - ErrBlockDeleteFailed = errors.New("unable to delete block") - ErrBlockIndexDeleteFailed = errors.New("unable to delete block index") - ErrHeadBlockIdentifierUpdateFailed = errors.New("unable to update head block identifier") ErrLastProcessedBlockPrecedesStart = errors.New( "last processed block is less than start index", ) ErrTransactionHashContentsDecodeFailed = errors.New( "could not decode transaction hash contents", ) - ErrTransactionDataEncodeFailed = errors.New("unable to encode transaction data") - ErrTransactionDeleteFailed = errors.New("could not remove transaction") - ErrTransactionHashNotFound = errors.New( + ErrTransactionDeleteFailed = errors.New("could not remove transaction") + ErrTransactionHashNotFound = errors.New( "saved blocks at transaction does not contain transaction hash", ) - ErrTransactionDBQueryFailed = errors.New("unable to query database for transaction") - ErrBlockDataDecodeFailed = errors.New( + ErrBlockDataDecodeFailed = errors.New( "unable to decode block data for transaction", ) ErrTransactionNotFound = errors.New("unable to find transaction") ErrTransactionDoesNotExistInBlock = errors.New("transaction does not exist in block") - ErrHeadBlockGetFailed = errors.New("unable to get head block") - ErrOldestIndexUpdateFailed = errors.New("oldest index update failed") ErrOldestIndexMissing = errors.New("oldest index missing") - ErrOldestIndexRead = errors.New("cannot read oldest index") ErrCannotRemoveOldest = errors.New("cannot remove oldest index") ErrCannotAccessPrunedData = errors.New("cannot access pruned data") ErrNothingToPrune = errors.New("nothing to prune") - ErrPruningFailed = errors.New("pruning failed") - ErrCannotPruneTransaction = errors.New("cannot prune transaction") - ErrCannotStoreBackwardRelation = errors.New("cannot store backward relation") - ErrCannotRemoveBackwardRelation = errors.New("cannot remove backward relation") BlockStorageErrs = []error{ ErrHeadBlockNotFound, ErrBlockNotFound, ErrDuplicateKey, ErrDuplicateTransactionHash, - ErrBlockGetFailed, - ErrTransactionGetFailed, - ErrBlockEncodeFailed, - ErrBlockStoreFailed, - ErrBlockIndexStoreFailed, - ErrBlockIdentifierUpdateFailed, - ErrBlockCopyFailed, - ErrTransactionHashStoreFailed, - ErrBlockDeleteFailed, - ErrBlockIndexDeleteFailed, - ErrHeadBlockIdentifierUpdateFailed, ErrLastProcessedBlockPrecedesStart, ErrTransactionHashContentsDecodeFailed, - ErrTransactionDataEncodeFailed, ErrTransactionDeleteFailed, ErrTransactionHashNotFound, - ErrTransactionDBQueryFailed, ErrBlockDataDecodeFailed, ErrTransactionNotFound, ErrTransactionDoesNotExistInBlock, - ErrHeadBlockGetFailed, - ErrOldestIndexUpdateFailed, ErrOldestIndexMissing, - ErrOldestIndexRead, ErrCannotRemoveOldest, ErrCannotAccessPrunedData, ErrNothingToPrune, - ErrPruningFailed, - ErrCannotPruneTransaction, - ErrCannotStoreBackwardRelation, - ErrCannotRemoveBackwardRelation, } ) diff --git a/storage/errors/errors_test.go b/storage/errors/errors_test.go index 0ff7e1729..e32c38839 100644 --- a/storage/errors/errors_test.go +++ b/storage/errors/errors_test.go @@ -37,31 +37,6 @@ func TestErr(t *testing.T) { is: true, source: "block storage error", }, - "coin storage error": { - err: ErrCoinQueryFailed, - is: true, - source: "coin storage error", - }, - "badger storage error": { - err: ErrDatabaseOpenFailed, - is: true, - source: "badger storage error", - }, - "broadcast storage error": { - err: ErrBroadcastTxStale, - is: true, - source: "broadcast storage error", - }, - "compressor error": { - err: ErrLoadDictFailed, - is: true, - source: "compressor error", - }, - "job storage error": { - err: ErrJobsGetAllFailed, - is: true, - source: "job storage error", - }, "key storage error": { err: ErrAddrExists, is: true, diff --git a/storage/modules/balance_storage.go b/storage/modules/balance_storage.go index 122bcadd0..8e07d89bb 100644 --- a/storage/modules/balance_storage.go +++ b/storage/modules/balance_storage.go @@ -194,7 +194,7 @@ func (b *BalanceStorage) AddingBlock( changes, err := b.parser.BalanceChanges(ctx, block, false) if err != nil { - return nil, fmt.Errorf("%w: unable to calculate balance changes", err) + return nil, fmt.Errorf("unable to calculate balance changes: %w", err) } // Keep track of how many new accounts have been seen so that the counter @@ -212,7 +212,7 @@ func (b *BalanceStorage) AddingBlock( block.ParentBlockIdentifier, ) if err != nil { - return err + return fmt.Errorf("unable to update balance: %w", err) } if !newAccount { @@ -232,7 +232,10 @@ func (b *BalanceStorage) AddingBlock( if pending > 0 { if err := b.handler.AccountsReconciled(ctx, transaction, pending); err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to update the total accounts reconciled by count: %w", + err, + ) } } @@ -254,7 +257,11 @@ func (b *BalanceStorage) RemovingBlock( changes, err := b.parser.BalanceChanges(ctx, block, true) if err != nil { - return nil, fmt.Errorf("%w: unable to calculate balance changes", err) + return nil, fmt.Errorf( + "unable to calculate balance changes for block %s: %w", + types.PrintStruct(block), + err, + ) } // staleAccounts should be removed because the orphaned @@ -295,7 +302,7 @@ func (b *BalanceStorage) RemovingBlock( return func(ctx context.Context) error { if err := b.handler.BlockRemoved(ctx, block, changes); err != nil { - return err + return fmt.Errorf("unable to remove block %s: %w", types.PrintStruct(block), err) } if len(staleAccounts) == 0 { @@ -335,12 +342,16 @@ func (b *BalanceStorage) SetBalance( account, amount.Currency, ); err != nil { - return err + return fmt.Errorf( + "unable to delete account records for account %s: %w", + types.PrintStruct(account), + err, + ) } // Mark as new account seen if err := b.handler.AccountsSeen(ctx, dbTransaction, 1); err != nil { - return err + return fmt.Errorf("unable to update the total accounts seen by count: %w", err) } // Serialize account entry @@ -349,13 +360,18 @@ func (b *BalanceStorage) SetBalance( Currency: amount.Currency, }) if err != nil { - return err + return fmt.Errorf( + "unable to encode account currency for currency %s of account %s: %w", + types.PrintStruct(amount.Currency), + types.PrintStruct(account), + err, + ) } // Set account key := GetAccountKey(accountNamespace, account, amount.Currency) if err := dbTransaction.Set(ctx, key, serialAcc, true); err != nil { - return err + return fmt.Errorf("unable to set account: %w", err) } // Set current balance @@ -367,13 +383,18 @@ func (b *BalanceStorage) SetBalance( valueBytes := value.Bytes() if err := dbTransaction.Set(ctx, key, valueBytes, false); err != nil { - return err + return fmt.Errorf("unable to set current balance: %w", err) } // Set historical balance key = GetHistoricalBalanceKey(account, amount.Currency, block.Index) if err := dbTransaction.Set(ctx, key, valueBytes, true); err != nil { - return err + return fmt.Errorf( + "unable to set historical balance for currency %s of account %s: %w", + types.PrintStruct(amount.Currency), + types.PrintStruct(account), + err, + ) } return nil @@ -397,7 +418,7 @@ func (b *BalanceStorage) Reconciled( acctKey := GetAccountKey(accountNamespace, account, currency) acctExists, _, err := dbTx.Get(ctx, acctKey) if err != nil { - return err + return fmt.Errorf("unable to get account: %w", err) } if !acctExists { return nil @@ -405,7 +426,7 @@ func (b *BalanceStorage) Reconciled( exists, lastReconciled, err := BigIntGet(ctx, key, dbTx) if err != nil { - return err + return fmt.Errorf("unable to get reconciliation: %w", err) } if exists { @@ -421,11 +442,11 @@ func (b *BalanceStorage) Reconciled( } if err := dbTx.Set(ctx, key, new(big.Int).SetInt64(block.Index).Bytes(), true); err != nil { - return err + return fmt.Errorf("unable to set reconciliation: %w", err) } if err := dbTx.Commit(ctx); err != nil { - return fmt.Errorf("%w: unable to commit last reconciliation update", err) + return fmt.Errorf("unable to commit last reconciliation update: %w", err) } return nil @@ -445,12 +466,12 @@ func (b *BalanceStorage) EstimatedReconciliationCoverage(ctx context.Context) (f reconciled, err := b.helper.AccountsReconciled(ctx, dbTx) if err != nil { - return -1, err + return -1, fmt.Errorf("unable to update the total accounts reconciled by count: %w", err) } accounts, err := b.helper.AccountsSeen(ctx, dbTx) if err != nil { - return -1, err + return -1, fmt.Errorf("unable to update the total accounts seen by count: %w", err) } if accounts.Sign() == 0 { @@ -477,7 +498,7 @@ func (b *BalanceStorage) ReconciliationCoverage( key := GetAccountKey(reconciliationNamepace, entry.Account, entry.Currency) exists, lastReconciled, err := BigIntGet(ctx, key, txn) if err != nil { - return err + return fmt.Errorf("unable to get reconciliation: %w", err) } if !exists { @@ -492,7 +513,7 @@ func (b *BalanceStorage) ReconciliationCoverage( }, ) if err != nil { - return -1, fmt.Errorf("%w: unable to get all account entries", err) + return -1, fmt.Errorf("unable to get all account entries: %w", err) } if seen == 0 { @@ -540,11 +561,11 @@ func (b *BalanceStorage) existingValue( ) if err != nil { return "", fmt.Errorf( - "%w: unable to get previous account balance for %s %s at %s", - err, + "unable to get previous account balance for %s %s at %s: %w", types.PrintStruct(change.Account), types.PrintStruct(change.Currency), types.PrintStruct(parentBlock), + err, ) } @@ -586,11 +607,11 @@ func (b *BalanceStorage) applyExemptions( ) if err != nil { return "", fmt.Errorf( - "%w: unable to get current account balance for %s %s at %s", - err, + "unable to get current account balance for %s %s at %s: %w", types.PrintStruct(change.Account), types.PrintStruct(change.Currency), types.PrintStruct(change.Block), + err, ) } @@ -599,7 +620,9 @@ func (b *BalanceStorage) applyExemptions( difference, err := types.SubtractValues(liveAmount.Value, newVal) if err != nil { return "", fmt.Errorf( - "%w: unable to calculate difference between live and computed balances", + "unable to calculate difference between live balance %s and computed balance %s: %w", + liveAmount.Value, + newVal, err, ) } @@ -610,11 +633,11 @@ func (b *BalanceStorage) applyExemptions( ) if exemption == nil { return "", fmt.Errorf( - "%w: account %s balance difference (live - computed) %s at %s is not allowed by any balance exemption", - storageErrs.ErrInvalidLiveBalance, + "account %s balance difference (live - computed) %s at %s is not allowed by any balance exemption: %w", types.PrintStruct(change.Account), difference, types.PrintStruct(change.Block), + storageErrs.ErrInvalidLiveBalance, ) } @@ -647,7 +670,7 @@ func (b *BalanceStorage) deleteAccountRecords( -1, true, // We want everything >= -1 ); err != nil { - return err + return fmt.Errorf("unable to remove historical balances: %w", err) } // Remove single key records @@ -664,12 +687,12 @@ func (b *BalanceStorage) deleteAccountRecords( if namespace == accountNamespace { exists, _, err := dbTx.Get(ctx, key) if err != nil { - return err + return fmt.Errorf("unable to get account: %w", err) } if exists { if err := b.handler.AccountsSeen(ctx, dbTx, -1); err != nil { - return err + return fmt.Errorf("unable to update the total accounts seen by count: %w", err) } } } @@ -679,18 +702,21 @@ func (b *BalanceStorage) deleteAccountRecords( if namespace == reconciliationNamepace { exists, _, err := dbTx.Get(ctx, key) if err != nil { - return err + return fmt.Errorf("unable to get reconciliation: %w", err) } if exists { if err := b.handler.AccountsReconciled(ctx, dbTx, -1); err != nil { - return err + return fmt.Errorf( + "unable to update the total accounts reconciled by count: %w", + err, + ) } } } if err := dbTx.Delete(ctx, key); err != nil { - return err + return fmt.Errorf("unable to delete transaction: %w", err) } } @@ -714,7 +740,7 @@ func (b *BalanceStorage) OrphanBalance( true, ) if err != nil { - return false, err + return false, fmt.Errorf("unable to remove historical balances: %w", err) } // Check if we should remove the account record @@ -730,14 +756,19 @@ func (b *BalanceStorage) OrphanBalance( case errors.Is(err, storageErrs.ErrAccountMissing): return true, nil case err != nil: - return false, err + return false, fmt.Errorf( + "unable to get historical balance for currency %s of account %s: %w", + types.PrintStruct(change.Currency), + types.PrintStruct(change.Account), + err, + ) } // Update current balance key := GetAccountKey(balanceNamespace, change.Account, change.Currency) exists, lastBalance, err := BigIntGet(ctx, key, dbTransaction) if err != nil { - return false, err + return false, fmt.Errorf("unable to get balance: %w", err) } if !exists { @@ -751,7 +782,7 @@ func (b *BalanceStorage) OrphanBalance( newBalance := new(big.Int).Add(lastBalance, difference) if err := dbTransaction.Set(ctx, key, newBalance.Bytes(), true); err != nil { - return false, err + return false, fmt.Errorf("unable to set current balance: %w", err) } return false, nil @@ -780,11 +811,11 @@ func (b *BalanceStorage) PruneBalances( false, ) if err != nil { - return fmt.Errorf("%w: unable to remove historical balances", err) + return fmt.Errorf("unable to remove historical balances: %w", err) } if err := dbTx.Commit(ctx); err != nil { - return fmt.Errorf("%w: unable to commit historical balance removal", err) + return fmt.Errorf("unable to commit historical balance removal: %w", err) } return nil @@ -800,7 +831,7 @@ func (b *BalanceStorage) UpdateBalance( parentBlock *types.BlockIdentifier, ) (bool, error) { if change.Currency == nil { - return false, errors.New("invalid currency") + return false, storageErrs.ErrInvalidCurrency } // If the balance key does not exist, the account @@ -808,7 +839,7 @@ func (b *BalanceStorage) UpdateBalance( key := GetAccountKey(balanceNamespace, change.Account, change.Currency) exists, currentBalance, err := BigIntGet(ctx, key, dbTransaction) if err != nil { - return false, err + return false, fmt.Errorf("unable to get balance: %w", err) } // Find account existing value whether the account is new, has an @@ -825,7 +856,12 @@ func (b *BalanceStorage) UpdateBalance( currentBalance.String(), ) if err != nil { - return false, err + return false, fmt.Errorf( + "unable to find account existing value for currency %s of account %s: %w", + types.PrintStruct(change.Currency), + types.PrintStruct(change.Account), + err, + ) } newVal, err := types.AddValues(change.Difference, existingValue) @@ -838,7 +874,7 @@ func (b *BalanceStorage) UpdateBalance( // and *types.Currency. newVal, err = b.applyExemptions(ctx, change, newVal) if err != nil { - return false, err + return false, fmt.Errorf("unable to apply exemptions: %w", err) } bigNewVal, ok := new(big.Int).SetString(newVal, 10) @@ -848,12 +884,9 @@ func (b *BalanceStorage) UpdateBalance( if bigNewVal.Sign() == -1 { return false, fmt.Errorf( - "%w %s:%+v for %+v at %+v", - storageErrs.ErrNegativeBalance, + "%s is invalid: %w", newVal, - change.Currency, - change.Account, - change.Block, + storageErrs.ErrNegativeBalance, ) } @@ -867,16 +900,21 @@ func (b *BalanceStorage) UpdateBalance( Currency: change.Currency, }) if err != nil { - return false, err + return false, fmt.Errorf( + "unable to encode account currency %s of account %s: %w", + types.PrintStruct(change.Currency), + types.PrintStruct(change.Account), + err, + ) } if err := dbTransaction.Set(ctx, key, serialAcc, true); err != nil { - return false, err + return false, fmt.Errorf("unable to set account: %w", err) } } // Update current balance if err := dbTransaction.Set(ctx, key, bigNewVal.Bytes(), true); err != nil { - return false, err + return false, fmt.Errorf("unable to set current balance: %w", err) } // Add a new historical record for the balance. @@ -886,7 +924,12 @@ func (b *BalanceStorage) UpdateBalance( change.Block.Index, ) if err := dbTransaction.Set(ctx, historicalKey, bigNewVal.Bytes(), true); err != nil { - return false, err + return false, fmt.Errorf( + "unable to set historical balance for currency %s of account %s: %w", + types.PrintStruct(change.Currency), + types.PrintStruct(change.Account), + err, + ) } return newAccount, nil @@ -911,7 +954,7 @@ func (b *BalanceStorage) GetBalance( index, ) if err != nil { - return nil, fmt.Errorf("%w: unable to get balance", err) + return nil, fmt.Errorf("unable to get balance: %w", err) } return amount, nil @@ -929,7 +972,7 @@ func (b *BalanceStorage) GetBalanceTransactional( key := GetAccountKey(balanceNamespace, account, currency) exists, _, err := dbTx.Get(ctx, key) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to get balance: %w", err) } if !exists { @@ -939,15 +982,15 @@ func (b *BalanceStorage) GetBalanceTransactional( key = GetAccountKey(pruneNamespace, account, currency) exists, lastPruned, err := BigIntGet(ctx, key, dbTx) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to get prune: %w", err) } if exists && lastPruned.Int64() >= index { return nil, fmt.Errorf( - "%w: desired %d last pruned %d", - storageErrs.ErrBalancePruned, + "desired %d last pruned %d: %w", index, lastPruned.Int64(), + storageErrs.ErrBalancePruned, ) } @@ -970,7 +1013,12 @@ func (b *BalanceStorage) GetBalanceTransactional( }, nil } if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to get historical balance for currency %s of account %s: %w", + types.PrintStruct(currency), + types.PrintStruct(account), + err, + ) } return amount, nil @@ -985,7 +1033,12 @@ func (b *BalanceStorage) fetchAndSetBalance( ) (*types.Amount, error) { amount, err := b.helper.AccountBalance(ctx, account, currency, block) if err != nil { - return nil, fmt.Errorf("%w: unable to get account balance from helper", err) + return nil, fmt.Errorf( + "unable to get account balance for currency %s of account %s: %w", + types.PrintStruct(currency), + types.PrintStruct(account), + err, + ) } err = b.SetBalance( @@ -996,7 +1049,11 @@ func (b *BalanceStorage) fetchAndSetBalance( block, ) if err != nil { - return nil, fmt.Errorf("%w: unable to set account balance", err) + return nil, fmt.Errorf( + "unable to set account balance of account %s: %w", + types.PrintStruct(account), + err, + ) } return amount, nil @@ -1022,12 +1079,17 @@ func (b *BalanceStorage) GetOrSetBalance( block, ) if err != nil { - return nil, fmt.Errorf("%w: unable to get balance", err) + return nil, fmt.Errorf( + "unable to get balance for currency %s of account %s: %w", + types.PrintStruct(currency), + types.PrintStruct(account), + err, + ) } // We commit any changes made during the balance lookup. if err := dbTx.Commit(ctx); err != nil { - return nil, fmt.Errorf("%w: unable to commit account balance transaction", err) + return nil, fmt.Errorf("unable to commit account balance transaction: %w", err) } return amount, nil @@ -1057,13 +1119,23 @@ func (b *BalanceStorage) GetOrSetBalanceTransactional( if errors.Is(err, storageErrs.ErrAccountMissing) { amount, err = b.fetchAndSetBalance(ctx, dbTx, account, currency, block) if err != nil { - return nil, fmt.Errorf("%w: unable to set balance", err) + return nil, fmt.Errorf( + "unable to set balance for currency %s of account %s: %w", + types.PrintStruct(currency), + types.PrintStruct(account), + err, + ) } return amount, nil } if err != nil { - return nil, fmt.Errorf("%w: unable to get balance", err) + return nil, fmt.Errorf( + "unable to get balance for currency %s of account %s: %w", + types.PrintStruct(currency), + types.PrintStruct(account), + err, + ) } return amount, nil @@ -1090,7 +1162,7 @@ func (b *BalanceStorage) BootstrapBalances( // Read bootstrap file balances := []*BootstrapBalance{} if err := utils.LoadAndParse(bootstrapBalancesFile, &balances); err != nil { - return err + return fmt.Errorf("unable to load and parse %s: %w", bootstrapBalancesFile, err) } // Update balances in database @@ -1103,7 +1175,7 @@ func (b *BalanceStorage) BootstrapBalances( // contains huge number of accounts. if i != 0 && i%utils.MaxEntrySizePerTxn == 0 { if err := dbTransaction.Commit(ctx); err != nil { - return err + return fmt.Errorf("unable to commit bootstrap balance update batch") } dbTransaction = b.db.Transaction(ctx) } @@ -1135,12 +1207,17 @@ func (b *BalanceStorage) BootstrapBalances( genesisBlockIdentifier, ) if err != nil { - return err + return fmt.Errorf( + "unable to set balance for currency %s of account %s: %w", + types.PrintStruct(balance.Currency), + types.PrintStruct(balance.Account), + err, + ) } } if err := dbTransaction.Commit(ctx); err != nil { - return err + return fmt.Errorf("unable to commit bootstrap balance") } log.Printf("%d Balances Bootstrapped\n", len(balances)) @@ -1163,9 +1240,9 @@ func (b *BalanceStorage) getAllAccountEntries( err := b.db.Encoder().DecodeAccountCurrency(v, &accCurrency, false) if err != nil { return fmt.Errorf( - "%w: unable to parse balance entry for %s", - err, + "unable to parse balance entry for %s: %w", string(v), + err, ) } @@ -1175,7 +1252,7 @@ func (b *BalanceStorage) getAllAccountEntries( false, ) if err != nil { - return fmt.Errorf("%w: database scan failed", err) + return fmt.Errorf("database scan failed: %w", err) } return nil @@ -1194,7 +1271,7 @@ func (b *BalanceStorage) GetAllAccountCurrency( accounts = append(accounts, account) return nil }); err != nil { - return nil, fmt.Errorf("%w: unable to get all balance entries", err) + return nil, fmt.Errorf("unable to get all balance entries: %w", err) } return accounts, nil @@ -1228,12 +1305,17 @@ func (b *BalanceStorage) SetBalanceImported( accountBalance.Block, ) if err != nil { - return err + return fmt.Errorf( + "unable to set balance for currency %s of account %s: %w", + types.PrintStruct(accountBalance.Amount.Currency), + types.PrintStruct(accountBalance.Account), + err, + ) } } if err := transaction.Commit(ctx); err != nil { - return err + return fmt.Errorf("unable to commit balance imported") } log.Printf("%d Balances Updated\n", len(accountBalances)) @@ -1268,7 +1350,7 @@ func (b *BalanceStorage) getHistoricalBalance( }, nil } if err != nil { - return nil, fmt.Errorf("%w: database scan failed", err) + return nil, fmt.Errorf("database scan failed: %w", err) } return nil, storageErrs.ErrAccountMissing @@ -1312,12 +1394,12 @@ func (b *BalanceStorage) removeHistoricalBalances( !orphan, ) if err != nil && !errors.Is(err, errTooManyKeys) { - return fmt.Errorf("%w: database scan failed", err) + return fmt.Errorf("database scan failed: %w", err) } for _, k := range foundKeys { if err := dbTx.Delete(ctx, k); err != nil { - return err + return fmt.Errorf("unable to delete transaction: %w", err) } } @@ -1331,7 +1413,7 @@ func (b *BalanceStorage) removeHistoricalBalances( key := GetAccountKey(pruneNamespace, account, currency) exists, lastPruned, err := BigIntGet(ctx, key, dbTx) if err != nil { - return err + return fmt.Errorf("unable to get prune: %w", err) } if exists && lastPruned.Int64() > index { diff --git a/storage/modules/block_storage.go b/storage/modules/block_storage.go index 1ff32ba28..7e738d910 100644 --- a/storage/modules/block_storage.go +++ b/storage/modules/block_storage.go @@ -179,7 +179,7 @@ func (b *BlockStorage) setOldestBlockIndex( value := []byte(strconv.FormatInt(index, 10)) if update { if err := dbTx.Set(ctx, key, value, true); err != nil { - return err + return fmt.Errorf("unable to set oldest block index: %w", err) } return nil @@ -190,7 +190,7 @@ func (b *BlockStorage) setOldestBlockIndex( return nil } - return err + return fmt.Errorf("unable to store block index for %s: %w", string(key), err) } // GetOldestBlockIndexTransactional returns the oldest block index @@ -201,7 +201,7 @@ func (b *BlockStorage) GetOldestBlockIndexTransactional( ) (int64, error) { exists, rawIndex, err := dbTx.Get(ctx, getOldestBlockIndexKey()) if err != nil { - return -1, err + return -1, fmt.Errorf("unable to get oldest block index: %w", err) } if !exists { @@ -210,7 +210,7 @@ func (b *BlockStorage) GetOldestBlockIndexTransactional( index, err := strconv.ParseInt(string(rawIndex), 10, 64) if err != nil { - return -1, err + return -1, fmt.Errorf("unable to parse int for raw index: %w", err) } return index, nil @@ -243,7 +243,7 @@ func (b *BlockStorage) pruneBlock( oldestIndex, err := b.GetOldestBlockIndexTransactional(ctx, dbTx) if err != nil { - return -1, fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexRead, err) + return -1, fmt.Errorf("unable to get oldest block index: %w", err) } if index < oldestIndex { @@ -252,7 +252,7 @@ func (b *BlockStorage) pruneBlock( head, err := b.GetHeadBlockIdentifierTransactional(ctx, dbTx) if err != nil { - return -1, fmt.Errorf("%w: cannot get head block identifier", err) + return -1, fmt.Errorf("unable to get head block identifier: %w", err) } // Ensure we are only pruning blocks that could not be @@ -267,7 +267,7 @@ func (b *BlockStorage) pruneBlock( dbTx, ) if err != nil && !errors.Is(err, storageErrs.ErrBlockNotFound) { - return -1, err + return -1, fmt.Errorf("unable to get block: %w", err) } // If there is an omitted block, we will have a non-nil error. When @@ -285,7 +285,7 @@ func (b *BlockStorage) pruneBlock( tx := blockResponse.OtherTransactions[i] g.Go(func() error { if err := b.pruneTransaction(gctx, dbTx, blockIdentifier, tx); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrCannotPruneTransaction, err) + return fmt.Errorf("unable to prune transaction: %w", err) } return nil @@ -298,18 +298,18 @@ func (b *BlockStorage) pruneBlock( _, blockKey := getBlockHashKey(blockIdentifier.Hash) if err := dbTx.Set(ctx, blockKey, []byte(""), true); err != nil { - return -1, err + return -1, fmt.Errorf("unable to get block hash %s: %w", blockIdentifier.Hash, err) } } // Update prune index if err := b.setOldestBlockIndex(ctx, dbTx, true, oldestIndex+1); err != nil { - return -1, fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexUpdateFailed, err) + return -1, fmt.Errorf("unable to set oldest block index: %w", err) } // Commit tx if err := dbTx.Commit(ctx); err != nil { - return -1, err + return -1, fmt.Errorf("unable to commit transaction: %w", err) } return oldestIndex, nil @@ -339,7 +339,7 @@ func (b *BlockStorage) Prune( return firstPruned, lastPruned, nil } if err != nil { - return -1, -1, fmt.Errorf("%w: %v", storageErrs.ErrPruningFailed, err) + return -1, -1, fmt.Errorf("unable to prune block %d: %w", index, err) } if firstPruned == -1 { @@ -373,7 +373,7 @@ func (b *BlockStorage) GetHeadBlockIdentifierTransactional( ) (*types.BlockIdentifier, error) { exists, block, err := transaction.Get(ctx, getHeadBlockKey()) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to get head block: %w", err) } if !exists { @@ -383,7 +383,7 @@ func (b *BlockStorage) GetHeadBlockIdentifierTransactional( var blockIdentifier types.BlockIdentifier err = b.db.Encoder().Decode("", block, &blockIdentifier, true) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to decode head block identifier: %w", err) } return &blockIdentifier, nil @@ -398,11 +398,11 @@ func (b *BlockStorage) StoreHeadBlockIdentifier( ) error { buf, err := b.db.Encoder().Encode("", blockIdentifier) if err != nil { - return err + return fmt.Errorf("unable to encode head block identifier: %w", err) } if err := transaction.Set(ctx, getHeadBlockKey(), buf, true); err != nil { - return err + return fmt.Errorf("unable to set head block: %w", err) } return nil @@ -428,7 +428,7 @@ func (b *BlockStorage) GetBlockLazyTransactional( var head *types.BlockIdentifier head, err = b.GetHeadBlockIdentifierTransactional(ctx, transaction) if err != nil { - return nil, fmt.Errorf("%w: cannot get head block identifier", err) + return nil, fmt.Errorf("unable to get head block identifier: %w", err) } namespace, key = getBlockHashKey(head.Hash) @@ -447,14 +447,14 @@ func (b *BlockStorage) GetBlockLazyTransactional( } } if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrBlockGetFailed, err) + return nil, fmt.Errorf("unable to get block: %w", err) } if !exists { return nil, fmt.Errorf( - "%w: %s", - storageErrs.ErrBlockNotFound, + "block %s is invalid: %w", types.PrintStruct(blockIdentifier), + storageErrs.ErrBlockNotFound, ) } @@ -465,7 +465,7 @@ func (b *BlockStorage) GetBlockLazyTransactional( var rosettaBlockResponse types.BlockResponse err = b.db.Encoder().Decode(namespace, blockResponse, &rosettaBlockResponse, true) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to decode block response: %w", err) } return &rosettaBlockResponse, nil @@ -520,7 +520,11 @@ func (b *BlockStorage) CanonicalBlockTransactional( return false, nil } if err != nil { - return false, err + return false, fmt.Errorf( + "unable to get block %s: %w", + types.PrintStruct(blockIdentifier), + err, + ) } if block == nil { @@ -539,7 +543,11 @@ func (b *BlockStorage) GetBlockTransactional( ) (*types.Block, error) { blockResponse, err := b.GetBlockLazyTransactional(ctx, blockIdentifier, dbTx) if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to get block %s: %w", + types.PrintStruct(blockIdentifier), + err, + ) } if len(blockResponse.OtherTransactions) == 0 { @@ -558,8 +566,7 @@ func (b *BlockStorage) GetBlockTransactional( ) if err != nil { return nil, fmt.Errorf( - "%w %s: %v", - storageErrs.ErrTransactionGetFailed, + "unable to get transaction %s: %w", transactionIdentifier.Hash, err, ) @@ -595,12 +602,12 @@ func (b *BlockStorage) seeBlock( namespace, key := getBlockHashKey(blockIdentifier.Hash) buf, err := b.db.Encoder().Encode(namespace, blockResponse) if err != nil { - return false, fmt.Errorf("%w: %v", storageErrs.ErrBlockEncodeFailed, err) + return false, fmt.Errorf("unable to encode block: %w", err) } exists, val, err := transaction.Get(ctx, key) if err != nil { - return false, err + return false, fmt.Errorf("unable to get block hash %s: %w", blockIdentifier.Hash, err) } if !exists { @@ -610,7 +617,7 @@ func (b *BlockStorage) seeBlock( var rosettaBlockResponse types.BlockResponse err = b.db.Encoder().Decode(namespace, val, &rosettaBlockResponse, true) if err != nil { - return false, err + return false, fmt.Errorf("unable to decode block response: %w", err) } // Exit early if block already exists! @@ -620,9 +627,9 @@ func (b *BlockStorage) seeBlock( } return false, fmt.Errorf( - "%w: duplicate key %s found", - storageErrs.ErrDuplicateKey, + "key %s is invalid: %w", string(key), + storageErrs.ErrDuplicateKey, ) } @@ -640,15 +647,19 @@ func (b *BlockStorage) storeBlock( key, false, ); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockIndexStoreFailed, err) + return fmt.Errorf("unable to store block index: %w", err) } if err := b.StoreHeadBlockIdentifier(ctx, transaction, blockIdentifier); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockIdentifierUpdateFailed, err) + return fmt.Errorf( + "unable to update head block identifier %s: %w", + types.PrintStruct(blockIdentifier), + err, + ) } if err := b.setOldestBlockIndex(ctx, transaction, false, blockIdentifier.Index); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexUpdateFailed, err) + return fmt.Errorf("unable to set oldest block index %d: %s", blockIdentifier.Index, err) } return nil @@ -669,11 +680,11 @@ func (b *BlockStorage) SeeBlock( for i, txn := range block.Transactions { if _, exists := identiferSet[txn.TransactionIdentifier.Hash]; exists { return fmt.Errorf( - "%w: duplicate transaction %s found in block %s:%d", - storageErrs.ErrDuplicateTransactionHash, + "duplicate transaction %s found in block index %s: hash %d: %w", txn.TransactionIdentifier.Hash, block.BlockIdentifier.Hash, block.BlockIdentifier.Index, + storageErrs.ErrDuplicateTransactionHash, ) } @@ -684,7 +695,7 @@ func (b *BlockStorage) SeeBlock( // Make copy of block and remove all transactions var copyBlock types.Block if err := encoder.CopyStruct(block, ©Block); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockCopyFailed, err) + return fmt.Errorf("unable to copy block: %w", err) } copyBlock.Transactions = nil @@ -698,7 +709,7 @@ func (b *BlockStorage) SeeBlock( // Store block exists, err := b.seeBlock(ctx, transaction, blockWithoutTransactions) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockStoreFailed, err) + return fmt.Errorf("unable to store block: %w", err) } if exists { @@ -719,7 +730,7 @@ func (b *BlockStorage) SeeBlock( txn, ) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrTransactionHashStoreFailed, err) + return fmt.Errorf("unable to store transaction hash: %w", err) } return nil @@ -743,7 +754,11 @@ func (b *BlockStorage) AddBlock( // Store block err := b.storeBlock(ctx, transaction, block.BlockIdentifier) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockStoreFailed, err) + return fmt.Errorf( + "unable to store block %s: %w", + types.PrintStruct(block.BlockIdentifier), + err, + ) } return b.callWorkersAndCommit(ctx, block, transaction, true) @@ -761,7 +776,7 @@ func (b *BlockStorage) deleteBlock( // further block removals would involve decoding pruned blocks. oldestIndex, err := b.GetOldestBlockIndexTransactional(ctx, transaction) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexRead, err) + return fmt.Errorf("cannot read oldest index: %w", err) } if blockIdentifier.Index <= oldestIndex { @@ -770,15 +785,15 @@ func (b *BlockStorage) deleteBlock( _, key := getBlockHashKey(blockIdentifier.Hash) if err := transaction.Delete(ctx, key); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockDeleteFailed, err) + return fmt.Errorf("unable to delete block: %w", err) } if err := transaction.Delete(ctx, getBlockIndexKey(blockIdentifier.Index)); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockIndexDeleteFailed, err) + return fmt.Errorf("unable to delete block index: %w", err) } if err := b.StoreHeadBlockIdentifier(ctx, transaction, block.ParentBlockIdentifier); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrHeadBlockIdentifierUpdateFailed, err) + return fmt.Errorf("unable to update head block identifier: %w", err) } return nil @@ -801,7 +816,7 @@ func (b *BlockStorage) RemoveBlock( types.ConstructPartialBlockIdentifier(blockIdentifier), ) if err != nil { - return err + return fmt.Errorf("unable to get block: %w", err) } // Remove all transaction hashes @@ -826,7 +841,7 @@ func (b *BlockStorage) RemoveBlock( // Delete block if err := b.deleteBlock(ctx, transaction, block); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrBlockDeleteFailed, err) + return fmt.Errorf("unable to delete block: %w", err) } return b.callWorkersAndCommit(ctx, block, transaction, false) @@ -853,7 +868,7 @@ func (b *BlockStorage) callWorkersAndCommit( cw, err = w.RemovingBlock(gctx, g, block, txn) } if err != nil { - return err + return fmt.Errorf("unable to adding/removing block: %w", err) } commitWorkers[i] = cw @@ -896,10 +911,10 @@ func (b *BlockStorage) SetNewStartIndex( if head.Index < startIndex { return fmt.Errorf( - "%w: block index is %d but start index is %d", - storageErrs.ErrLastProcessedBlockPrecedesStart, + "block index is %d but start index is %d: %w", head.Index, startIndex, + storageErrs.ErrLastProcessedBlockPrecedesStart, ) } @@ -909,15 +924,15 @@ func (b *BlockStorage) SetNewStartIndex( oldestIndex, err := b.GetOldestBlockIndexTransactional(ctx, dbTx) dbTx.Discard(ctx) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexRead, err) + return fmt.Errorf("unable to read oldest block index: %w", err) } if oldestIndex > startIndex { return fmt.Errorf( - "%w: oldest block index is %d but start index is %d", - storageErrs.ErrCannotAccessPrunedData, + "oldest block index is %d but start index is %d: %w", oldestIndex, startIndex, + storageErrs.ErrCannotAccessPrunedData, ) } @@ -930,7 +945,7 @@ func (b *BlockStorage) SetNewStartIndex( } if err := b.RemoveBlock(ctx, block.BlockIdentifier); err != nil { - return err + return fmt.Errorf("unable to remove block: %w", err) } currBlock = block.ParentBlockIdentifier @@ -976,7 +991,7 @@ func (b *BlockStorage) storeTransaction( ) error { err := b.storeBackwardRelations(ctx, transaction, tx) if err != nil { - return err + return fmt.Errorf("unable to store backward relations: %w", err) } namespace, hashKey := getTransactionKey(blockIdentifier, tx.TransactionIdentifier) @@ -987,7 +1002,7 @@ func (b *BlockStorage) storeTransaction( encodedResult, err := b.db.Encoder().Encode(namespace, bt) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrTransactionDataEncodeFailed, err) + return fmt.Errorf("unable to encode transaction data: %w", err) } return storeUniqueKey(ctx, transaction, hashKey, encodedResult, true) @@ -1001,7 +1016,7 @@ func (b *BlockStorage) storeBackwardRelations( fn := func(ctx context.Context, transaction database.Transaction, key []byte) error { err := transaction.Set(ctx, key, []byte{}, true) if err != nil { - return fmt.Errorf("%v: %w", storageErrs.ErrCannotStoreBackwardRelation, err) + return fmt.Errorf("unable to set backward relations: %w", err) } return nil @@ -1018,7 +1033,7 @@ func (b *BlockStorage) removeBackwardRelations( fn := func(ctx context.Context, transaction database.Transaction, key []byte) error { err := transaction.Delete(ctx, key) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrCannotRemoveBackwardRelation, err) + return fmt.Errorf("unable to remove backward relations: %w", err) } return nil @@ -1046,7 +1061,11 @@ func (b *BlockStorage) modifyBackwardRelations( // skip if related block not found block, _, err := b.FindTransaction(ctx, relatedTx.TransactionIdentifier, transaction) if err != nil { - return fmt.Errorf("%v: %w", storageErrs.ErrCannotStoreBackwardRelation, err) + return fmt.Errorf( + "unable to find backward relation transaction %s: %w", + types.PrintStruct(relatedTx.TransactionIdentifier), + err, + ) } if block == nil { continue @@ -1081,7 +1100,7 @@ func (b *BlockStorage) pruneTransaction( encodedResult, err := b.db.Encoder().Encode(namespace, bt) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrTransactionDataEncodeFailed, err) + return fmt.Errorf("unable to encode transaction data: %w", err) } return transaction.Set(ctx, hashKey, encodedResult, true) @@ -1095,7 +1114,7 @@ func (b *BlockStorage) removeTransaction( ) error { err := b.removeBackwardRelations(ctx, transaction, tx) if err != nil { - return err + return fmt.Errorf("unable to remove backward relations: %w", err) } _, hashKey := getTransactionKey(blockIdentifier, tx.TransactionIdentifier) @@ -1116,7 +1135,7 @@ func (b *BlockStorage) getAllTransactionsByIdentifier( // Decode blockTransaction var bt blockTransaction if err := b.db.Encoder().Decode(transactionNamespace, v, &bt, false); err != nil { - return fmt.Errorf("%w: unable to decode block data for transaction", err) + return fmt.Errorf("unable to decode block transaction: %w", err) } // Extract hash from key @@ -1136,7 +1155,7 @@ func (b *BlockStorage) getAllTransactionsByIdentifier( false, ) if err != nil { - return nil, err + return nil, fmt.Errorf("database scan failed: %w", err) } return blockTransactions, nil @@ -1151,7 +1170,7 @@ func (b *BlockStorage) FindTransaction( ) (*types.BlockIdentifier, *types.Transaction, error) { blockTransactions, err := b.getAllTransactionsByIdentifier(ctx, transactionIdentifier, txn) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", storageErrs.ErrTransactionDBQueryFailed, err) + return nil, nil, fmt.Errorf("unable to query database for transaction: %w", err) } if len(blockTransactions) == 0 { @@ -1160,7 +1179,7 @@ func (b *BlockStorage) FindTransaction( head, err := b.GetHeadBlockIdentifierTransactional(ctx, txn) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("unable to get head block identifier: %w", err) } var newestBlock *types.BlockIdentifier @@ -1236,7 +1255,7 @@ func (b *BlockStorage) FindRelatedTransactions( childBlock, childTx, err := b.FindTransaction(ctx, childID, db) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, fmt.Errorf("unable to find related transaction: %w", err) } if childBlock == nil { @@ -1250,7 +1269,7 @@ func (b *BlockStorage) FindRelatedTransactions( newChildren, err := b.getForwardRelatedTransactions(ctx, childTx, db) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, fmt.Errorf("unable to find forward related transactions: %w", err) } childIds = append(childIds, newChildren...) } @@ -1293,7 +1312,7 @@ func (b *BlockStorage) getForwardRelatedTransactions( ) if err != nil { - return nil, err + return nil, fmt.Errorf("database scan failed: %w", err) } return children, nil @@ -1307,7 +1326,7 @@ func (b *BlockStorage) findBlockTransaction( ) (*types.Transaction, error) { oldestIndex, err := b.GetOldestBlockIndexTransactional(ctx, txn) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrOldestIndexRead, err) + return nil, fmt.Errorf("unable to get oldest block index: %w", err) } if blockIdentifier.Index < oldestIndex { @@ -1317,7 +1336,7 @@ func (b *BlockStorage) findBlockTransaction( namespace, key := getTransactionKey(blockIdentifier, transactionIdentifier) txExists, tx, err := txn.Get(ctx, key) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrTransactionDBQueryFailed, err) + return nil, fmt.Errorf("unable to query database for transaction: %w", err) } if !txExists { @@ -1330,7 +1349,7 @@ func (b *BlockStorage) findBlockTransaction( var bt blockTransaction if err := b.db.Encoder().Decode(namespace, tx, &bt, true); err != nil { - return nil, fmt.Errorf("%w: unable to decode block data for transaction", err) + return nil, fmt.Errorf("unable to decode block transaction: %w", err) } return bt.Transaction, nil @@ -1363,7 +1382,7 @@ func (b *BlockStorage) AtTipTransactional( return false, nil, nil } if err != nil { - return false, nil, fmt.Errorf("%w: %v", storageErrs.ErrHeadBlockGetFailed, err) + return false, nil, fmt.Errorf("unable to get head block: %w", err) } block := blockResponse.Block @@ -1405,7 +1424,7 @@ func (b *BlockStorage) IndexAtTip( return false, nil } if err != nil { - return false, err + return false, fmt.Errorf("unable to get block: %w", err) } // Check if index is greater than headBlock and if headBlock @@ -1423,7 +1442,7 @@ func (b *BlockStorage) IndexAtTip( transaction, ) if err != nil { - return false, err + return false, fmt.Errorf("unable to get block %d: %w", index, err) } block := blockResponse.Block diff --git a/storage/modules/block_storage_test.go b/storage/modules/block_storage_test.go index 9d5caad1f..fc8739c92 100644 --- a/storage/modules/block_storage_test.go +++ b/storage/modules/block_storage_test.go @@ -345,7 +345,7 @@ func TestBlock(t *testing.T) { firstPruned, lastPruned, err := storage.Prune(ctx, 2, minPruningDepth) assert.Equal(t, int64(-1), firstPruned) assert.Equal(t, int64(-1), lastPruned) - assert.True(t, errors.Is(err, storageErrs.ErrPruningFailed)) + assert.True(t, errors.Is(err, storageErrs.ErrOldestIndexMissing)) }) t.Run("Set genesis block", func(t *testing.T) { @@ -366,7 +366,6 @@ func TestBlock(t *testing.T) { // Ensure we error if trying to remove genesis err = storage.RemoveBlock(ctx, genesisBlock.BlockIdentifier) assert.Contains(t, err.Error(), storageErrs.ErrCannotRemoveOldest.Error()) - assert.True(t, errors.Is(err, storageErrs.ErrBlockDeleteFailed)) }) t.Run("Set and get block", func(t *testing.T) { diff --git a/storage/modules/broadcast_storage.go b/storage/modules/broadcast_storage.go index 8831714ac..94578e412 100644 --- a/storage/modules/broadcast_storage.go +++ b/storage/modules/broadcast_storage.go @@ -184,8 +184,7 @@ func (b *BroadcastStorage) invokeAddBlockHandlers( for _, stale := range staleBroadcasts { if err := b.handler.TransactionStale(ctx, dbTx, stale.Identifier, stale.TransactionIdentifier); err != nil { return fmt.Errorf( - "%w %s: %v", - errors.ErrBroadcastTxStale, + "unable to handle stale transaction %s: %w", stale.TransactionIdentifier.Hash, err, ) @@ -204,8 +203,7 @@ func (b *BroadcastStorage) invokeAddBlockHandlers( ) if err != nil { return fmt.Errorf( - "%w %s: %v", - errors.ErrBroadcastTxConfirmed, + "unable to handle confirmed transaction %s: %w", broadcast.TransactionIdentifier.Hash, err, ) @@ -224,7 +222,7 @@ func (b *BroadcastStorage) AddingBlock( ) (database.CommitWorker, error) { broadcasts, err := b.GetAllBroadcasts(ctx) if err != nil { - return nil, fmt.Errorf("%w: unable to get all broadcasts", err) + return nil, fmt.Errorf("unable to get all broadcasts: %w", err) } staleBroadcasts := []*Broadcast{} @@ -248,7 +246,11 @@ func (b *BroadcastStorage) AddingBlock( transaction, ) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastFindTxFailed, err) + return nil, fmt.Errorf( + "unable to find broadcast transaction %s: %w", + types.PrintStruct(broadcast.TransactionIdentifier), + err, + ) } // Check if we should mark the broadcast as stale @@ -258,11 +260,15 @@ func (b *BroadcastStorage) AddingBlock( broadcast.LastBroadcast = nil bytes, err := b.db.Encoder().Encode(namespace, broadcast) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastEncodeUpdateFailed, err) + return nil, fmt.Errorf( + "unable to encode updated broadcast %s: %w", + types.PrintStruct(broadcast), + err, + ) } if err := transaction.Set(ctx, key, bytes, true); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastUpdateFailed, err) + return nil, fmt.Errorf("unable to set broadcast: %w", err) } continue @@ -280,7 +286,7 @@ func (b *BroadcastStorage) AddingBlock( foundBlocks = append(foundBlocks, foundBlock) if err := transaction.Delete(ctx, key); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastDeleteConfirmedTxFailed, err) + return nil, fmt.Errorf("unable to delete confirmed broadcast: %w", err) } } } @@ -293,12 +299,12 @@ func (b *BroadcastStorage) AddingBlock( foundBlocks, foundTransactions, ); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastInvokeBlockHandlersFailed, err) + return nil, fmt.Errorf("unable to handle block add: %w", err) } return func(ctx context.Context) error { if err := b.BroadcastAll(ctx, true); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastFailed, err) + return fmt.Errorf("unable to broadcast pending transactions: %w", err) } return nil @@ -333,11 +339,15 @@ func (b *BroadcastStorage) Broadcast( exists, _, err := dbTx.Get(ctx, broadcastKey) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastDBGetFailed, err) + return fmt.Errorf("unable get broadcast: %w", err) } if exists { - return fmt.Errorf("%w %s", errors.ErrBroadcastAlreadyExists, transactionIdentifier.Hash) + return fmt.Errorf( + "broadcast is invalid with broadcast transaction %s: %w", + transactionIdentifier.Hash, + errors.ErrBroadcastAlreadyExists, + ) } bytes, err := b.db.Encoder().Encode(namespace, Broadcast{ @@ -351,11 +361,11 @@ func (b *BroadcastStorage) Broadcast( TransactionMetadata: transactionMetadata, }) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastEncodeFailed, err) + return fmt.Errorf("unable to encode broadcast: %w", err) } if err := dbTx.Set(ctx, broadcastKey, bytes, true); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastSetFailed, err) + return fmt.Errorf("unable to set broadcast: %w", err) } return nil @@ -375,7 +385,7 @@ func (b *BroadcastStorage) getAllBroadcasts( var broadcast Broadcast // We should not reclaim memory during a scan!! if err := b.db.Encoder().Decode(namespace, v, &broadcast, false); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastDecodeFailed, err) + return fmt.Errorf("unable to decode broadcast: %w", err) } broadcasts = append(broadcasts, &broadcast) @@ -385,7 +395,7 @@ func (b *BroadcastStorage) getAllBroadcasts( false, ) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastScanFailed, err) + return nil, fmt.Errorf("unable to scan for all broadcasts: %w", err) } return broadcasts, nil @@ -407,18 +417,18 @@ func (b *BroadcastStorage) performBroadcast( namespace, key := getBroadcastKey(broadcast.TransactionIdentifier) bytes, err := b.db.Encoder().Encode(namespace, broadcast) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastEncodeFailed, err) + return fmt.Errorf("unable to encode broadcast: %w", err) } txn := b.db.Transaction(ctx) defer txn.Discard(ctx) if err := txn.Set(ctx, key, bytes, true); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastUpdateFailed, err) + return fmt.Errorf("unable to set broadcast: %w", err) } if err := txn.Commit(ctx); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastCommitUpdateFailed, err) + return fmt.Errorf("unable to commit broadcast: %w", err) } if !onlyEligible { @@ -443,10 +453,10 @@ func (b *BroadcastStorage) performBroadcast( if types.Hash(broadcastIdentifier) != types.Hash(broadcast.TransactionIdentifier) { return fmt.Errorf( - "%w: expected %s but got %s", - errors.ErrBroadcastIdentifierMismatch, + "expected %s but got %s: %w", broadcast.TransactionIdentifier.Hash, broadcastIdentifier.Hash, + errors.ErrBroadcastIdentifierMismatch, ) } @@ -463,7 +473,7 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) currBlock, err := b.helper.CurrentBlockIdentifier(ctx) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastGetCurrentBlockIdentifierFailed, err) + return fmt.Errorf("unable to get current block identifier: %w", err) } // We have not yet synced a block and should wait to broadcast @@ -475,7 +485,7 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) // Wait to broadcast transaction until close to tip atTip, err := b.helper.AtTip(ctx, b.tipDelay) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastAtTipFailed, err) + return fmt.Errorf("unable to determine if at tip: %w", err) } if (!atTip && !b.broadcastBehindTip) && onlyEligible { @@ -484,7 +494,7 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) broadcasts, err := b.GetAllBroadcasts(ctx) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastGetAllFailed, err) + return fmt.Errorf("unable to get all broadcasts: %w", err) } attemptedBroadcasts := 0 @@ -501,7 +511,7 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) _, key := getBroadcastKey(broadcast.TransactionIdentifier) if err := txn.Delete(ctx, key); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastDeleteFailed, err) + return fmt.Errorf("unable to delete broadcast: %w", err) } if err := b.handler.BroadcastFailed( @@ -511,11 +521,15 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) broadcast.TransactionIdentifier, broadcast.Intent, ); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastHandleFailureUnsuccessful, err) + return fmt.Errorf( + "unable to handle broadcast failure for broadcast %s: %w", + broadcast.Identifier, + err, + ) } if err := txn.Commit(ctx); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastCommitDeleteFailed, err) + return fmt.Errorf("unable to commit broadcast: %w", err) } continue @@ -536,7 +550,7 @@ func (b *BroadcastStorage) BroadcastAll(ctx context.Context, onlyEligible bool) broadcast.Broadcasts++ if err := b.performBroadcast(ctx, broadcast, onlyEligible); err != nil { - return fmt.Errorf("%w: %v", errors.ErrBroadcastPerformFailed, err) + return fmt.Errorf("unable to perform broadcast: %w", err) } } @@ -552,7 +566,7 @@ func (b *BroadcastStorage) LockedAccounts( ) ([]*types.AccountIdentifier, error) { broadcasts, err := b.getAllBroadcasts(ctx, dbTx) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastGetAllFailed, err) + return nil, fmt.Errorf("unable to get all broadcasts: %w", err) } // De-duplicate accounts present in broadcast storage. @@ -581,7 +595,7 @@ func (b *BroadcastStorage) LockedAccounts( func (b *BroadcastStorage) ClearBroadcasts(ctx context.Context) ([]*Broadcast, error) { broadcasts, err := b.GetAllBroadcasts(ctx) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastGetAllFailed, err) + return nil, fmt.Errorf("unable to get all broadcasts: %w", err) } txn := b.db.Transaction(ctx) @@ -589,9 +603,7 @@ func (b *BroadcastStorage) ClearBroadcasts(ctx context.Context) ([]*Broadcast, e _, key := getBroadcastKey(broadcast.TransactionIdentifier) if err := txn.Delete(ctx, key); err != nil { return nil, fmt.Errorf( - "%w %s: %v", - errors.ErrBroadcastDeleteFailed, - broadcast.Identifier, + "unable to delete broadcast: %w", err, ) } @@ -606,8 +618,7 @@ func (b *BroadcastStorage) ClearBroadcasts(ctx context.Context) ([]*Broadcast, e broadcast.Intent, ); err != nil { return nil, fmt.Errorf( - "%w %s: %v", - errors.ErrBroadcastHandleFailureUnsuccessful, + "unable to handle broadcast failure for broadcast %s: %w", broadcast.Identifier, err, ) @@ -615,7 +626,7 @@ func (b *BroadcastStorage) ClearBroadcasts(ctx context.Context) ([]*Broadcast, e } if err := txn.Commit(ctx); err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrBroadcastCommitDeleteFailed, err) + return nil, fmt.Errorf("unable to commit broadcast: %w", err) } return broadcasts, nil diff --git a/storage/modules/coin_storage.go b/storage/modules/coin_storage.go index 20cf69fca..cd9255c07 100644 --- a/storage/modules/coin_storage.go +++ b/storage/modules/coin_storage.go @@ -98,7 +98,7 @@ func (c *CoinStorage) getAndDecodeCoin( key := getCoinKey(coinIdentifier) exists, val, err := transaction.Get(ctx, key) if err != nil { - return false, nil, nil, fmt.Errorf("%w: %v", errors.ErrCoinQueryFailed, err) + return false, nil, nil, fmt.Errorf("unable to get coin: %w", err) } if !exists { // this could occur if coin was created before we started syncing @@ -107,7 +107,7 @@ func (c *CoinStorage) getAndDecodeCoin( var accountCoin types.AccountCoin if err := c.db.Encoder().DecodeAccountCoin(val, &accountCoin, true); err != nil { - return false, nil, nil, fmt.Errorf("%w: %v", errors.ErrCoinDecodeFailed, err) + return false, nil, nil, fmt.Errorf("unable to decode coin: %w", err) } return true, accountCoin.Coin, accountCoin.Account, nil @@ -125,7 +125,7 @@ func (c *CoinStorage) AddCoins( for _, accountCoin := range accountCoins { exists, _, _, err := c.getAndDecodeCoin(ctx, dbTransaction, accountCoin.Coin.CoinIdentifier) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinGetFailed, err) + return fmt.Errorf("unable to get and decode coin: %w", err) } if exists { @@ -134,12 +134,16 @@ func (c *CoinStorage) AddCoins( err = c.addCoin(ctx, accountCoin.Account, accountCoin.Coin, dbTransaction) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinAddFailed, err) + return fmt.Errorf( + "unable to add coin for account %s: %w", + types.PrintStruct(accountCoin.Account), + err, + ) } } if err := dbTransaction.Commit(ctx); err != nil { - return fmt.Errorf("%w: %v", errors.ErrReconciliationUpdateCommitFailed, err) + return fmt.Errorf("unable to commit last reconciliation update: %w", err) } return nil @@ -157,11 +161,11 @@ func (c *CoinStorage) addCoin( Coin: coin, }) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinDataEncodeFailed, err) + return fmt.Errorf("unable to encode coin: %w", err) } if err := storeUniqueKey(ctx, transaction, key, encodedResult, true); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinStoreFailed, err) + return fmt.Errorf("unable to store coin: %w", err) } if err := storeUniqueKey( @@ -171,7 +175,7 @@ func (c *CoinStorage) addCoin( []byte(""), false, ); err != nil { - return fmt.Errorf("%w: %v", errors.ErrAccountCoinStoreFailed, err) + return fmt.Errorf("unable to store account coin: %w", err) } return nil @@ -197,7 +201,7 @@ func getAndDecodeCoins( false, ) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrAccountCoinQueryFailed, err) + return nil, fmt.Errorf("unable to query coins for account: %w", err) } return coins, nil @@ -212,7 +216,7 @@ func (c *CoinStorage) removeCoin( key := getCoinKey(coinIdentifier) exists, _, err := transaction.Get(ctx, key) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinQueryFailed, err) + return fmt.Errorf("unable to get coin: %w", err) } if !exists { // this could occur if coin was created before we started syncing @@ -220,11 +224,11 @@ func (c *CoinStorage) removeCoin( } if err := transaction.Delete(ctx, key); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinDeleteFailed, err) + return fmt.Errorf("unable to delete coin: %w", err) } if err := transaction.Delete(ctx, getCoinAccountCoin(account, coinIdentifier)); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinDeleteFailed, err) + return fmt.Errorf("unable to delete account coin: %w", err) } return nil @@ -243,7 +247,11 @@ func (c *CoinStorage) skipOperation( success, err := c.asserter.OperationSuccessful(operation) if err != nil { - return false, fmt.Errorf("%w: %v", errors.ErrOperationParseFailed, err) + return false, fmt.Errorf( + "unable to successfully parse operation %s: %w", + types.PrintStruct(operation), + err, + ) } if !success { @@ -277,7 +285,11 @@ func (c *CoinStorage) updateCoins( // nolint:gocognit for _, operation := range txn.Operations { skip, err := c.skipOperation(operation) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrUnableToDetermineIfSkipOperation, err) + return fmt.Errorf( + "unable to skip operation %s: %w", + types.PrintStruct(operation), + err, + ) } if skip { continue @@ -292,7 +304,11 @@ func (c *CoinStorage) updateCoins( // nolint:gocognit } if _, ok := coinDict[identifier]; ok { - return fmt.Errorf("%w %s", errors.ErrDuplicateCoinFound, identifier) + return fmt.Errorf( + "coin identifier %s is invalid: %w", + identifier, + errors.ErrDuplicateCoinFound, + ) } coinDict[identifier] = operation @@ -318,7 +334,11 @@ func (c *CoinStorage) updateCoins( // nolint:gocognit }, dbTx, ); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinAddFailed, err) + return fmt.Errorf( + "unable to add coin for account %s: %w", + types.PrintStruct(op.Account), + err, + ) } return nil @@ -341,7 +361,11 @@ func (c *CoinStorage) updateCoins( // nolint:gocognit op.CoinChange.CoinIdentifier, dbTx, ); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinRemoveFailed, err) + return fmt.Errorf( + "unable to remove coin for account %s: %w", + types.PrintStruct(op.Account), + err, + ) } return nil @@ -379,12 +403,16 @@ func (c *CoinStorage) GetCoinsTransactional( ) ([]*types.Coin, *types.BlockIdentifier, error) { coins, err := getAndDecodeCoins(ctx, dbTx, accountIdentifier) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", errors.ErrAccountIdentifierQueryFailed, err) + return nil, nil, fmt.Errorf( + "unable to get and decode coins for account %s: %w", + types.PrintStruct(accountIdentifier), + err, + ) } headBlockIdentifier, err := c.helper.CurrentBlockIdentifier(ctx, dbTx) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", errors.ErrCurrentBlockGetFailed, err) + return nil, nil, fmt.Errorf("unable to get current block: %w", err) } coinArr := []*types.Coin{} @@ -395,11 +423,15 @@ func (c *CoinStorage) GetCoinsTransactional( &types.CoinIdentifier{Identifier: coinIdentifier}, ) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", errors.ErrCoinQueryFailed, err) + return nil, nil, fmt.Errorf( + "unable to get and decode coin %s: %w", + types.PrintStruct(coinIdentifier), + err, + ) } if !exists { - return nil, nil, fmt.Errorf("%w %s: %v", errors.ErrCoinGetFailed, coinIdentifier, err) + return nil, nil, errors.ErrCoinNotFound } coinArr = append(coinArr, coin) @@ -428,7 +460,11 @@ func (c *CoinStorage) GetCoinTransactional( ) (*types.Coin, *types.AccountIdentifier, error) { exists, coin, owner, err := c.getAndDecodeCoin(ctx, dbTx, coinIdentifier) if err != nil { - return nil, nil, fmt.Errorf("%w: %v", errors.ErrCoinLookupFailed, err) + return nil, nil, fmt.Errorf( + "unable to get and decode coin %s: %w", + types.PrintStruct(coinIdentifier), + err, + ) } if !exists { @@ -460,8 +496,8 @@ func (c *CoinStorage) GetLargestCoin( coins, blockIdentifier, err := c.GetCoins(ctx, accountIdentifier) if err != nil { return nil, nil, nil, fmt.Errorf( - "%w for %s: %v", - errors.ErrUTXOBalanceGetFailed, + "unable to get utxo balance for account %s: %w", + accountIdentifier.Address, err, ) @@ -481,9 +517,9 @@ func (c *CoinStorage) GetLargestCoin( val, ok := new(big.Int).SetString(coin.Amount.Value, 10) if !ok { return nil, nil, nil, fmt.Errorf( - "%w %s", - errors.ErrCoinParseFailed, + "coin %s is invalid: %w", coin.CoinIdentifier.Identifier, + errors.ErrCoinParseFailed, ) } @@ -507,7 +543,11 @@ func (c *CoinStorage) SetCoinsImported( // Request array length should always equal response array length. // But we still check it for sure. if len(accts) != len(acctCoinsResp) { - return errors.ErrCoinImportFailed + return fmt.Errorf( + "the length of coin request %d and coin response %d are not equal", + len(accts), + len(acctCoinsResp), + ) } var acctCoins []*types.AccountCoin @@ -523,7 +563,7 @@ func (c *CoinStorage) SetCoinsImported( } if err := c.AddCoins(ctx, acctCoins); err != nil { - return fmt.Errorf("%w: %v", errors.ErrCoinImportFailed, err) + return fmt.Errorf("unable to add coins: %w", err) } return nil diff --git a/storage/modules/counter_storage.go b/storage/modules/counter_storage.go index a5412a90b..67a1d505e 100644 --- a/storage/modules/counter_storage.go +++ b/storage/modules/counter_storage.go @@ -121,7 +121,7 @@ func BigIntGet( ) (bool, *big.Int, error) { exists, val, err := txn.Get(ctx, key) if err != nil { - return false, nil, err + return false, nil, fmt.Errorf("unable to get the value for key %s: %w", string(key), err) } if !exists { @@ -147,13 +147,13 @@ func (c *CounterStorage) UpdateTransactional( _, val, err := BigIntGet(ctx, getCounterKey(counter), dbTx) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to get counter: %w", err) } newVal := new(big.Int).Add(val, amount) if err := dbTx.Set(ctx, getCounterKey(counter), newVal.Bytes(), false); err != nil { - return nil, err + return nil, fmt.Errorf("unable to set counter: %w", err) } return newVal, nil @@ -170,11 +170,11 @@ func (c *CounterStorage) Update( newVal, err := c.UpdateTransactional(ctx, dbTx, counter, amount) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to update counter: %w", err) } if err := dbTx.Commit(ctx); err != nil { - return nil, err + return nil, fmt.Errorf("unable to commit counter update: %w", err) } return newVal, nil @@ -213,7 +213,7 @@ func (c *CounterStorage) AddingBlock( big.NewInt(1), ) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to update block counter: %w", err) } _, err = c.UpdateTransactional( @@ -223,7 +223,7 @@ func (c *CounterStorage) AddingBlock( big.NewInt(int64(len(block.Transactions))), ) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to update transaction counter: %w", err) } opCount := int64(0) @@ -237,7 +237,7 @@ func (c *CounterStorage) AddingBlock( big.NewInt(opCount), ) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to update operation counter: %w", err) } return nil, nil diff --git a/storage/modules/job_storage.go b/storage/modules/job_storage.go index 290b73dd2..0cb34cdd7 100644 --- a/storage/modules/job_storage.go +++ b/storage/modules/job_storage.go @@ -77,7 +77,7 @@ func (j *JobStorage) getAllJobs( ) ([]*job.Job, error) { exists, v, err := dbTx.Get(ctx, k) if err != nil { - return nil, fmt.Errorf("%w by %s: %v", errors.ErrJobsGetAllFailed, string(k), err) + return nil, fmt.Errorf("unable to get all jobs: %w", err) } jobs := []*job.Job{} @@ -88,13 +88,13 @@ func (j *JobStorage) getAllJobs( var identifiers map[string]struct{} err = j.db.Encoder().Decode("", v, &identifiers, true) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrJobIdentifierDecodeFailed, err) + return nil, fmt.Errorf("unable to decode jobs: %w", err) } for identifier := range identifiers { v, err := j.Get(ctx, dbTx, identifier) if err != nil { - return nil, fmt.Errorf("%w %s: %v", errors.ErrJobGetFailed, identifier, err) + return nil, fmt.Errorf("unable to get job %s: %w", identifier, err) } jobs = append(jobs, v) @@ -172,7 +172,7 @@ func (j *JobStorage) getNextIdentifier( k := getJobMetadataKey("identifier") exists, v, err := dbTx.Get(ctx, k) if err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobGetFailed, err) + return "", fmt.Errorf("unable to get job: %w", err) } // Get existing identifier @@ -180,7 +180,7 @@ func (j *JobStorage) getNextIdentifier( if exists { err = j.db.Encoder().Decode("", v, &nextIdentifier, true) if err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobIdentifierDecodeFailed, err) + return "", fmt.Errorf("unable to decode job: %w", err) } } else { nextIdentifier = 0 @@ -189,11 +189,11 @@ func (j *JobStorage) getNextIdentifier( // Increment and save encoded, err := j.db.Encoder().Encode("", nextIdentifier+1) if err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobIdentifierEncodeFailed, err) + return "", fmt.Errorf("unable to encode job: %w", err) } if err := dbTx.Set(ctx, k, encoded, true); err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobIdentifierUpdateFailed, err) + return "", fmt.Errorf("unable to set job: %w", err) } return strconv.Itoa(nextIdentifier), nil @@ -207,11 +207,11 @@ func (j *JobStorage) updateIdentifiers( ) error { encoded, err := j.db.Encoder().Encode("", identifiers) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrJobIdentifiersEncodeAllFailed, err) + return fmt.Errorf("unable to encode identifier: %w", err) } if err := dbTx.Set(ctx, k, encoded, true); err != nil { - return fmt.Errorf("%w: %v", errors.ErrJobIdentifiersSetAllFailed, err) + return fmt.Errorf("unable to set identifiers: %w", err) } return nil @@ -225,14 +225,14 @@ func (j *JobStorage) addJob( ) error { exists, v, err := dbTx.Get(ctx, k) if err != nil { - return fmt.Errorf("%w by %s: %v", errors.ErrJobsGetAllFailed, string(k), err) + return fmt.Errorf("unable to add job: %w", err) } var identifiers map[string]struct{} if exists { err = j.db.Encoder().Decode("", v, &identifiers, true) if err != nil { - return fmt.Errorf("%w: %v", errors.ErrJobIdentifierDecodeFailed, err) + return fmt.Errorf("unable to decode job: %w", err) } } else { identifiers = map[string]struct{}{} @@ -251,31 +251,21 @@ func (j *JobStorage) removeJob( ) error { exists, v, err := dbTx.Get(ctx, k) if err != nil { - return fmt.Errorf("%w by %s: %v", errors.ErrJobsGetAllFailed, string(k), err) + return fmt.Errorf("unable to get job: %w", err) } var identifiers map[string]struct{} if !exists { - return fmt.Errorf( - "%w %s from %s", - errors.ErrJobIdentifierRemoveFailed, - identifier, - string(k), - ) + return errors.ErrJobDoesNotExist } err = j.db.Encoder().Decode("", v, &identifiers, true) if err != nil { - return fmt.Errorf("%w: unable to decode existing identifier", err) + return fmt.Errorf("unable to decode job: %w", err) } if _, ok := identifiers[identifier]; !ok { - return fmt.Errorf( - "%w: %s is not in %s", - errors.ErrJobIdentifierNotFound, - identifier, - string(k), - ) + return errors.ErrJobIdentifierNotFound } delete(identifiers, identifier) @@ -321,14 +311,14 @@ func (j *JobStorage) updateMetadata( removedKeys := getAssociatedKeys(oldJob) for _, key := range removedKeys { if err := j.removeJob(ctx, dbTx, key, oldJob.Identifier); err != nil { - return fmt.Errorf("%w %s: %v", errors.ErrJobRemoveFailed, oldJob.Identifier, err) + return fmt.Errorf("unable to remove job: %w", err) } } addedKeys := getAssociatedKeys(newJob) for _, key := range addedKeys { if err := j.addJob(ctx, dbTx, key, newJob.Identifier); err != nil { - return fmt.Errorf("%w %s: %v", errors.ErrJobAddFailed, newJob.Identifier, err) + return fmt.Errorf("unable to add job: %w", err) } } @@ -345,7 +335,7 @@ func (j *JobStorage) Update( if len(v.Identifier) == 0 { newIdentifier, err := j.getNextIdentifier(ctx, dbTx) if err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobIdentifierGetFailed, err) + return "", fmt.Errorf("unable to get next job: %w", err) } v.Identifier = newIdentifier @@ -353,26 +343,26 @@ func (j *JobStorage) Update( var err error oldJob, err = j.Get(ctx, dbTx, v.Identifier) if err != nil { - return "", fmt.Errorf("%w %s: %v", errors.ErrJobGetFailed, v.Identifier, err) + return "", fmt.Errorf("unable to get job: %w", err) } } if oldJob != nil && (oldJob.Status == job.Completed || oldJob.Status == job.Failed) { - return "", fmt.Errorf("%w %s", errors.ErrJobUpdateOldFailed, v.Identifier) + return "", fmt.Errorf("job %s is invalid: %w", v.Identifier, errors.ErrJobUpdateOldFailed) } k := getJobKey(v.Identifier) encoded, err := j.db.Encoder().Encode("", v) if err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobEncodeFailed, err) + return "", fmt.Errorf("unable to encode job: %w", err) } if err := dbTx.Set(ctx, k, encoded, true); err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobUpdateFailed, err) + return "", fmt.Errorf("unable to set job: %w", err) } if err := j.updateMetadata(ctx, dbTx, oldJob, v); err != nil { - return "", fmt.Errorf("%w: %v", errors.ErrJobMetadataUpdateFailed, err) + return "", fmt.Errorf("unable to update job metadata: %w", err) } return v.Identifier, nil @@ -388,16 +378,16 @@ func (j *JobStorage) Get( exists, v, err := dbTx.Get(ctx, k) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrJobGetFailed, err) + return nil, fmt.Errorf("unable to get job: %w", err) } if !exists { - return nil, fmt.Errorf("%w: %v", errors.ErrJobDoesNotExist, err) + return nil, errors.ErrJobDoesNotExist } var output job.Job err = j.db.Encoder().Decode("", v, &output, true) if err != nil { - return nil, fmt.Errorf("%w: %v", errors.ErrJobDecodeFailed, err) + return nil, fmt.Errorf("unable to decode job: %w", err) } return &output, nil diff --git a/storage/modules/key_storage.go b/storage/modules/key_storage.go index 139dd683c..a9b7d5623 100644 --- a/storage/modules/key_storage.go +++ b/storage/modules/key_storage.go @@ -81,19 +81,13 @@ func (k *KeyStorage) StoreTransactional( exists, _, err := dbTx.Get(ctx, getAccountKey(account)) if err != nil { return fmt.Errorf( - "%w: %s %v", - storageErrs.ErrAddrCheckIfExistsFailed, - types.PrintStruct(account), + "unable to get key: %w", err, ) } if exists { - return fmt.Errorf( - "%w: account %s already exists", - storageErrs.ErrAddrExists, - types.PrintStruct(account), - ) + return storageErrs.ErrAddrExists } val, err := k.db.Encoder().Encode("", &Key{ @@ -101,12 +95,12 @@ func (k *KeyStorage) StoreTransactional( KeyPair: keyPair, }) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrSerializeKeyFailed, err) + return fmt.Errorf("unable to encode key: %w", err) } err = dbTx.Set(ctx, getAccountKey(account), val, true) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrStoreKeyFailed, err) + return fmt.Errorf("unable to set key: %w", err) } return nil @@ -123,11 +117,11 @@ func (k *KeyStorage) Store( defer dbTx.Discard(ctx) if err := k.StoreTransactional(ctx, account, keyPair, dbTx); err != nil { - return fmt.Errorf("%w: unable to store key", err) + return fmt.Errorf("unable to store key for account %s: %w", types.PrintStruct(keyPair), err) } if err := dbTx.Commit(ctx); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrCommitKeyFailed, err) + return fmt.Errorf("unable to commit new key: %w", err) } return nil @@ -143,20 +137,18 @@ func (k *KeyStorage) GetTransactional( exists, rawKey, err := dbTx.Get(ctx, getAccountKey(account)) if err != nil { return nil, fmt.Errorf( - "%w: %s %v", - storageErrs.ErrAddrGetFailed, - types.PrintStruct(account), + "unable to get key: %w", err, ) } if !exists { - return nil, fmt.Errorf("%w: %s", storageErrs.ErrAddrNotFound, types.PrintStruct(account)) + return nil, storageErrs.ErrAddrNotFound } var kp Key if err := k.db.Encoder().Decode("", rawKey, &kp, true); err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrParseSavedKeyFailed, err) + return nil, fmt.Errorf("unable to decode key: %w", err) } return kp.KeyPair, nil @@ -187,7 +179,7 @@ func (k *KeyStorage) GetAllAccountsTransactional( var kp Key // We should not reclaim memory during a scan!! if err := k.db.Encoder().Decode("", v, &kp, false); err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrKeyScanFailed, err) + return fmt.Errorf("unable to decode key: %w", err) } accounts = append(accounts, kp.Account) @@ -197,7 +189,7 @@ func (k *KeyStorage) GetAllAccountsTransactional( false, ) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrKeyScanFailed, err) + return nil, fmt.Errorf("database scan failed: %w", err) } return accounts, nil @@ -221,8 +213,7 @@ func (k *KeyStorage) Sign( keyPair, err := k.Get(ctx, payload.AccountIdentifier) if err != nil { return nil, fmt.Errorf( - "%w for %s: %v", - storageErrs.ErrKeyGetFailed, + "unable to get key for account %s: %w", types.PrintStruct(payload.AccountIdentifier), err, ) @@ -230,16 +221,16 @@ func (k *KeyStorage) Sign( signer, err := keyPair.Signer() if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrSignerCreateFailed, err) + return nil, fmt.Errorf("unable to create signer: %w", err) } if len(payload.SignatureType) == 0 { - return nil, fmt.Errorf("%w %d", storageErrs.ErrDetermineSigTypeFailed, i) + return nil, storageErrs.ErrDetermineSigTypeFailed } signature, err := signer.Sign(payload, payload.SignatureType) if err != nil { - return nil, fmt.Errorf("%w for %d: %v", storageErrs.ErrSignPayloadFailed, i, err) + return nil, fmt.Errorf("unable to to sign payload: %w", err) } signatures[i] = signature @@ -252,7 +243,7 @@ func (k *KeyStorage) Sign( func (k *KeyStorage) RandomAccount(ctx context.Context) (*types.AccountIdentifier, error) { accounts, err := k.GetAllAccounts(ctx) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrAddrsGetAllFailed, err) + return nil, fmt.Errorf("unable to get all accounts: %w", err) } if len(accounts) == 0 { @@ -261,7 +252,7 @@ func (k *KeyStorage) RandomAccount(ctx context.Context) (*types.AccountIdentifie randomNumber, err := utils.RandomNumber(big.NewInt(0), big.NewInt(int64(len(accounts)))) if err != nil { - return nil, fmt.Errorf("%w: %v", storageErrs.ErrRandomAddress, err) + return nil, fmt.Errorf("unable to generate random account: %w", err) } return accounts[randomNumber.Int64()], nil @@ -272,7 +263,7 @@ func (k *KeyStorage) ImportAccounts(ctx context.Context, accounts []*PrefundedAc for _, acc := range accounts { keyPair, err := keys.ImportPrivateKey(acc.PrivateKeyHex, acc.CurveType) if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrAddrImportFailed, err) + return fmt.Errorf("unable to import prefunded account: %w", err) } // Skip if key already exists @@ -281,7 +272,7 @@ func (k *KeyStorage) ImportAccounts(ctx context.Context, accounts []*PrefundedAc continue } if err != nil { - return fmt.Errorf("%w: %v", storageErrs.ErrPrefundedAcctStoreFailed, err) + return fmt.Errorf("unable to store prefunded account: %w", err) } } return nil diff --git a/storage/modules/utils.go b/storage/modules/utils.go index 15f0c9fb4..e7ac275d6 100644 --- a/storage/modules/utils.go +++ b/storage/modules/utils.go @@ -36,9 +36,9 @@ func storeUniqueKey( if exists { return fmt.Errorf( - "%w: duplicate key %s found", - errors.ErrDuplicateKey, + "key %s is invalid: %w", string(key), + errors.ErrDuplicateKey, ) } diff --git a/types/utils.go b/types/utils.go index 75b69a279..06ae588bc 100644 --- a/types/utils.go +++ b/types/utils.go @@ -45,7 +45,7 @@ func hashBytes(data []byte) string { h := sha256.New() _, err := h.Write(data) if err != nil { - log.Fatal(fmt.Errorf("%w: unable to hash data %s", err, string(data))) + log.Fatal(fmt.Errorf("unable to hash data %s: %w", string(data), err)) } return fmt.Sprintf("%x", h.Sum(nil)) @@ -65,19 +65,19 @@ func Hash(i interface{}) string { // contains json.RawMessage) a, err := json.Marshal(i) if err != nil { - log.Fatal(fmt.Errorf("%w: unable to marshal %+v", err, i)) + log.Fatal(fmt.Errorf("unable to marshal %+v: %w", i, err)) } // Convert JSON object to interface (all json.RawMessage converted to go types) var b interface{} if err := json.Unmarshal(a, &b); err != nil { - log.Fatal(fmt.Errorf("%w: unable to unmarshal %+v", err, a)) + log.Fatal(fmt.Errorf("unable to unmarshal %+v: %w", a, err)) } // Convert interface to JSON object (all map keys ordered) c, err := json.Marshal(b) if err != nil { - log.Fatal(fmt.Errorf("%w: unable to marshal %+v", err, b)) + log.Fatal(fmt.Errorf("unable to marshal %+v: %w", b, err)) } return hashBytes(c) diff --git a/utils/utils.go b/utils/utils.go index db51a664f..2121aa104 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -88,7 +88,7 @@ var ( func CreateTempDir() (string, error) { storageDir, err := ioutil.TempDir("", "") if err != nil { - return "", err + return "", fmt.Errorf("failed to create temporary directory: %w", err) } color.Cyan("Using temporary directory %s", storageDir) @@ -108,7 +108,7 @@ func RemoveTempDir(dir string) { // a path if they do not exist. func EnsurePathExists(path string) error { if err := os.MkdirAll(path, os.FileMode(AllFilePermissions)); err != nil { - return fmt.Errorf("%w: unable to create data and network directory", err) + return fmt.Errorf("unable to create data and network directory: %w", err) } return nil @@ -129,7 +129,7 @@ func SerializeAndWrite(filePath string, object interface{}) error { os.FileMode(DefaultFilePermissions), ) if err != nil { - return fmt.Errorf("%w: unable to write to file path %s", err, filePath) + return fmt.Errorf("unable to write to file path %s: %w", filePath, err) } return nil @@ -140,7 +140,7 @@ func SerializeAndWrite(filePath string, object interface{}) error { func LoadAndParse(filePath string, output interface{}) error { b, err := ioutil.ReadFile(path.Clean(filePath)) if err != nil { - return fmt.Errorf("%w: unable to load file %s", err, filePath) + return fmt.Errorf("unable to load file %s: %w", filePath, err) } // To prevent silent erroring, we explicitly @@ -149,7 +149,7 @@ func LoadAndParse(filePath string, output interface{}) error { dec.DisallowUnknownFields() if err := dec.Decode(&output); err != nil { - return fmt.Errorf("%w: unable to unmarshal", err) + return fmt.Errorf("unable to unmarshal: %w", err) } return nil @@ -166,7 +166,7 @@ func CreateCommandPath( ) (string, error) { dataPath := path.Join(dataDirectory, cmd, types.Hash(network)) if err := EnsurePathExists(dataPath); err != nil { - return "", fmt.Errorf("%w: cannot populate path", err) + return "", fmt.Errorf("failed to create path %s: %w", dataPath, err) } return dataPath, nil @@ -229,7 +229,11 @@ func CheckNetworkTip(ctx context.Context, // NetworkStatusRetry call. status, fetchErr := f.NetworkStatusRetry(ctx, network, nil) if fetchErr != nil { - return false, nil, fmt.Errorf("%w: unable to fetch network status", fetchErr.Err) + return false, nil, fmt.Errorf( + "unable to fetch network status of network %s: %w", + types.PrintStruct(network), + fetchErr.Err, + ) } // if the block timestamp is within tip delay of current time, @@ -280,7 +284,7 @@ func CheckStorageTip(ctx context.Context, if fetchErr != nil { return false, nil, - fmt.Errorf("%w: unable to fetch network status", fetchErr) + fmt.Errorf("unable to check network tip: %w", fetchErr) } if networkAtTip && types.Hash(tipBlock) == types.Hash(currentStorageBlock.BlockIdentifier) { @@ -299,7 +303,7 @@ func CheckNetworkSupported( ) (*types.NetworkStatusResponse, error) { networks, fetchErr := helper.NetworkList(ctx, nil) if fetchErr != nil { - return nil, fmt.Errorf("%w: unable to fetch network list", fetchErr.Err) + return nil, fmt.Errorf("unable to fetch network list: %w", fetchErr.Err) } networkMatched, supportedNetworks := fetcher.CheckNetworkListForNetwork( @@ -309,9 +313,9 @@ func CheckNetworkSupported( if !networkMatched { color.Yellow("Supported networks: %s", types.PrettyPrintStruct(supportedNetworks)) return nil, fmt.Errorf( - "%w: %s is not available", + "network %s is invalid: %w", + types.PrintStruct(networkIdentifier), ErrNetworkNotSupported, - types.PrettyPrintStruct(networkIdentifier), ) } @@ -321,7 +325,11 @@ func CheckNetworkSupported( nil, ) if fetchErr != nil { - return nil, fmt.Errorf("%w: unable to get network status", fetchErr.Err) + return nil, fmt.Errorf( + "unable to fetch network status of network %s: %w", + types.PrintStruct(networkIdentifier), + fetchErr.Err, + ) } return status, nil @@ -360,7 +368,7 @@ func RandomNumber(minimum *big.Int, maximum *big.Int) (*big.Int, error) { addition, err := rand.Int(rand.Reader, transformed) if err != nil { - return nil, fmt.Errorf("cannot get random number: %v", err) + return nil, fmt.Errorf("failed to generate random number: %w", err) } return new(big.Int).Add(minimum, addition), nil @@ -440,7 +448,12 @@ func CurrencyBalance( []*types.Currency{currency}, ) if fetchErr != nil { - return nil, nil, fetchErr.Err + return nil, nil, fmt.Errorf( + "unable to fetch account balance for currency %s of account %s: %w", + types.PrintStruct([]*types.Currency{currency}), + types.PrintStruct(account), + fetchErr.Err, + ) } liveAmount := types.ExtractAmount(liveBalances, currency) @@ -473,7 +486,11 @@ func AllCurrencyBalance( nil, ) if fetchErr != nil { - return nil, nil, fetchErr.Err + return nil, nil, fmt.Errorf( + "unable to fetch account balance for all currencies of account %s: %w", + types.PrintStruct(account), + fetchErr.Err, + ) } return liveBalances, liveBlock, nil @@ -516,7 +533,7 @@ func GetAccountBalances( -1, ) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get currency balance: %w", err) } accountBalance := &AccountBalance{ @@ -534,7 +551,7 @@ func GetAccountBalances( -1, ) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get all currencies balance: %w", err) } for _, amount := range amounts { @@ -585,7 +602,12 @@ func GetAccountCoins( ) if err != nil { - return nil, err.Err + return nil, fmt.Errorf( + "unable to fetch account coin for currency %s of account %s: %w", + types.PrintStruct(req.Currencies), + types.PrintStruct(req.Account), + err.Err, + ) } resp := &AccountCoinsResponse{