Skip to content

Commit

Permalink
multi: fix fmt.Errorf error wrapping
Browse files Browse the repository at this point in the history
Refactor fmt.Errorf usage to correctly wrap errors instead of using
non-wrapping format verbs.
  • Loading branch information
ffranr committed Feb 27, 2024
1 parent 581c16d commit cd566eb
Show file tree
Hide file tree
Showing 103 changed files with 426 additions and 390 deletions.
4 changes: 2 additions & 2 deletions autopilot/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func (a *Agent) openChans(availableFunds btcutil.Amount, numChans uint32,
nodes[nID] = struct{}{}
return nil
}); err != nil {
return fmt.Errorf("unable to get graph nodes: %v", err)
return fmt.Errorf("unable to get graph nodes: %w", err)
}

// Use the heuristic to calculate a score for each node in the
Expand All @@ -639,7 +639,7 @@ func (a *Agent) openChans(availableFunds btcutil.Amount, numChans uint32,
a.cfg.Graph, totalChans, chanSize, nodes,
)
if err != nil {
return fmt.Errorf("unable to calculate node scores : %v", err)
return fmt.Errorf("unable to calculate node scores : %w", err)
}

log.Debugf("Got scores for %d nodes", len(scores))
Expand Down
2 changes: 1 addition & 1 deletion brontide/noise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestWriteMessageChunking(t *testing.T) {

bytesWritten, err := localConn.Write(largeMessage)
if err != nil {
errCh <- fmt.Errorf("unable to write message: %v", err)
errCh <- fmt.Errorf("unable to write message: %w", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions build/logrotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func (r *RotatingLogWriter) InitLogRotator(logFile string, maxLogFileSize int,
logDir, _ := filepath.Split(logFile)
err := os.MkdirAll(logDir, 0700)
if err != nil {
return fmt.Errorf("failed to create log directory: %v", err)
return fmt.Errorf("failed to create log directory: %w", err)
}
r.logRotator, err = rotator.New(
logFile, int64(maxLogFileSize*1024), false, maxLogFiles,
)
if err != nil {
return fmt.Errorf("failed to create file rotator: %v", err)
return fmt.Errorf("failed to create file rotator: %w", err)
}

// Run rotator as a goroutine now but make sure we catch any errors
Expand Down
7 changes: 4 additions & 3 deletions chainntnfs/bitcoindnotify/bitcoind.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) err
// clients.
rawBlock, err := b.GetBlock(block.Hash)
if err != nil {
return fmt.Errorf("unable to get block: %v", err)
return fmt.Errorf("unable to get block: %w", err)
}
utilBlock := btcutil.NewBlock(rawBlock)

Expand All @@ -645,7 +645,7 @@ func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) err
// us.
err = b.txNotifier.ConnectTip(utilBlock, uint32(block.Height))
if err != nil {
return fmt.Errorf("unable to connect tip: %v", err)
return fmt.Errorf("unable to connect tip: %w", err)
}

chainntnfs.Log.Infof("New block: height=%v, sha=%v", block.Height,
Expand Down Expand Up @@ -719,7 +719,8 @@ func (b *BitcoindNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
pkScript, b.chainParams,
)
if err != nil {
return nil, fmt.Errorf("unable to parse script: %v", err)
return nil, fmt.Errorf("unable to parse script: %w",
err)
}
if err := b.chainConn.NotifyReceived(addrs); err != nil {
return nil, err
Expand Down
10 changes: 6 additions & 4 deletions chainntnfs/btcdnotify/btcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error {
// clients.
rawBlock, err := b.GetBlock(epoch.Hash)
if err != nil {
return fmt.Errorf("unable to get block: %v", err)
return fmt.Errorf("unable to get block: %w", err)
}
newBlock := &filteredBlock{
hash: *epoch.Hash,
Expand All @@ -706,7 +706,7 @@ func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error {
// us.
err = b.txNotifier.ConnectTip(newBlock.block, newBlock.height)
if err != nil {
return fmt.Errorf("unable to connect tip: %v", err)
return fmt.Errorf("unable to connect tip: %w", err)
}

chainntnfs.Log.Infof("New block: height=%v, sha=%v", epoch.Height,
Expand Down Expand Up @@ -785,7 +785,8 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
pkScript, b.chainParams,
)
if err != nil {
return nil, fmt.Errorf("unable to parse script: %v", err)
return nil, fmt.Errorf("unable to parse script: %w",
err)
}
if err := b.chainConn.NotifyReceived(addrs); err != nil {
return nil, err
Expand Down Expand Up @@ -823,7 +824,8 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
pkScript, b.chainParams,
)
if err != nil {
return nil, fmt.Errorf("unable to parse address: %v", err)
return nil, fmt.Errorf("unable to parse address: %w",
err)
}

asyncResult := b.chainConn.RescanAsync(startHash, addrs, nil)
Expand Down
2 changes: 1 addition & 1 deletion chainntnfs/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func GetClientMissedBlocks(chainConn ChainConn, clientBestBlock *BlockEpoch,
chainConn, startingHeight+1, notifierBestHeight+1,
)
if err != nil {
return nil, fmt.Errorf("unable to get missed blocks: %v", err)
return nil, fmt.Errorf("unable to get missed blocks: %w", err)
}

return missedBlocks, nil
Expand Down
18 changes: 10 additions & 8 deletions chainntnfs/neutrinonotify/neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ func (n *NeutrinoNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequ
key := builder.DeriveKey(blockHash)
match, err := regFilter.Match(key, confRequest.PkScript.Script())
if err != nil {
return nil, fmt.Errorf("unable to query filter: %v", err)
return nil, fmt.Errorf("unable to query filter: %w",
err)
}

// If there's no match, then we can continue forward to the
Expand All @@ -623,7 +624,8 @@ func (n *NeutrinoNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequ
// to send the proper response.
block, err := n.GetBlock(*blockHash)
if err != nil {
return nil, fmt.Errorf("unable to get block from network: %v", err)
return nil, fmt.Errorf("unable to get block from "+
"network: %w", err)
}

// For every transaction in the block, check which one matches
Expand Down Expand Up @@ -663,11 +665,11 @@ func (n *NeutrinoNotifier) handleBlockConnected(newBlock *filteredBlock) error {
// result in the items we care about being dispatched.
rawBlock, err := n.GetBlock(newBlock.hash)
if err != nil {
return fmt.Errorf("unable to get full block: %v", err)
return fmt.Errorf("unable to get full block: %w", err)
}
err = n.txNotifier.ConnectTip(rawBlock, newBlock.height)
if err != nil {
return fmt.Errorf("unable to connect tip: %v", err)
return fmt.Errorf("unable to connect tip: %w", err)
}

chainntnfs.Log.Infof("New block: height=%v, sha=%v", newBlock.height,
Expand All @@ -692,7 +694,7 @@ func (n *NeutrinoNotifier) handleBlockConnected(newBlock *filteredBlock) error {
func (n *NeutrinoNotifier) getFilteredBlock(epoch chainntnfs.BlockEpoch) (*filteredBlock, error) {
rawBlock, err := n.GetBlock(*epoch.Hash)
if err != nil {
return nil, fmt.Errorf("unable to get block: %v", err)
return nil, fmt.Errorf("unable to get block: %w", err)
}

txns := rawBlock.Transactions()
Expand Down Expand Up @@ -800,7 +802,7 @@ func (n *NeutrinoNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
return nil, chainntnfs.ErrChainNotifierShuttingDown
}
if err != nil {
return nil, fmt.Errorf("unable to update filter: %v", err)
return nil, fmt.Errorf("unable to update filter: %w", err)
}

// If the txNotifier didn't return any details to perform a historical
Expand Down Expand Up @@ -937,7 +939,7 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
params := n.p2pNode.ChainParams()
_, addrs, _, err := txscript.ExtractPkScriptAddrs(pkScript, &params)
if err != nil {
return nil, fmt.Errorf("unable to extract script: %v", err)
return nil, fmt.Errorf("unable to extract script: %w", err)
}

// We'll send the filter update request to the notifier's main event
Expand All @@ -962,7 +964,7 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
return nil, chainntnfs.ErrChainNotifierShuttingDown
}
if err != nil {
return nil, fmt.Errorf("unable to update filter: %v", err)
return nil, fmt.Errorf("unable to update filter: %w", err)
}

// If a historical rescan was not requested by the txNotifier, then we
Expand Down
3 changes: 2 additions & 1 deletion chainntnfs/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) {
pkScript, _, err := randPubKeyHashScript()
if err != nil {
return nil, nil, fmt.Errorf("unable to generate pkScript: %v", err)
return nil, nil, fmt.Errorf("unable to generate pkScript: %w",
err)
}
output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
txid, err := h.SendOutputs([]*wire.TxOut{output}, 10)
Expand Down
2 changes: 1 addition & 1 deletion chanbackup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource,
// the source to obtain any extra information that we may need.
staticChanBackup, err := assembleChanBackup(addrSource, targetChan)
if err != nil {
return nil, fmt.Errorf("unable to create chan backup: %v", err)
return nil, fmt.Errorf("unable to create chan backup: %w", err)
}

return staticChanBackup, nil
Expand Down
9 changes: 5 additions & 4 deletions chanbackup/backupfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
var err error
b.tempFile, err = os.Create(b.tempFileName)
if err != nil {
return fmt.Errorf("unable to create temp file: %v", err)
return fmt.Errorf("unable to create temp file: %w", err)
}

// With the file created, we'll write the new packed multi backup and
// remove the temporary file all together once this method exits.
_, err = b.tempFile.Write([]byte(newBackup))
if err != nil {
return fmt.Errorf("unable to write backup to temp file: %v", err)
return fmt.Errorf("unable to write backup to temp file: %w",
err)
}
if err := b.tempFile.Sync(); err != nil {
return fmt.Errorf("unable to sync temp file: %v", err)
return fmt.Errorf("unable to sync temp file: %w", err)
}
defer os.Remove(b.tempFileName)

Expand All @@ -114,7 +115,7 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
// sure to close the current file as some OSes don't support
// renaming a file that's already open (Windows).
if err := b.tempFile.Close(); err != nil {
return fmt.Errorf("unable to close file: %v", err)
return fmt.Errorf("unable to close file: %w", err)
}

// Finally, we'll attempt to atomically rename the temporary file to
Expand Down
2 changes: 1 addition & 1 deletion chanbackup/backupfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func makeFakePackedMulti() (PackedMulti, error) {
newPackedMulti := make([]byte, 50)
if _, err := rand.Read(newPackedMulti[:]); err != nil {
return nil, fmt.Errorf("unable to make test backup: %v", err)
return nil, fmt.Errorf("unable to make test backup: %w", err)
}

return PackedMulti(newPackedMulti), nil
Expand Down
4 changes: 2 additions & 2 deletions chanbackup/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (m Multi) PackToWriter(w io.Writer, keyRing keychain.KeyRing) error {
// directly to the passed writer.
e, err := lnencrypt.KeyRingEncrypter(keyRing)
if err != nil {
return fmt.Errorf("unable to generate encrypt key %v", err)
return fmt.Errorf("unable to generate encrypt key %w", err)
}

return e.EncryptPayloadToWriter(multiBackupBuffer.Bytes(), w)
Expand All @@ -107,7 +107,7 @@ func (m *Multi) UnpackFromReader(r io.Reader, keyRing keychain.KeyRing) error {
// encryption keys.
e, err := lnencrypt.KeyRingEncrypter(keyRing)
if err != nil {
return fmt.Errorf("unable to generate encrypt key %v", err)
return fmt.Errorf("unable to generate encrypt key %w", err)
}
plaintextBackup, err := e.DecryptPayloadFromReader(r)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions chanbackup/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ func (s *SubSwapper) updateBackupFile(closedChans ...wire.OutPoint) error {
var b bytes.Buffer
err = newMulti.PackToWriter(&b, s.keyRing)
if err != nil {
return fmt.Errorf("unable to pack multi backup: %v", err)
return fmt.Errorf("unable to pack multi backup: %w", err)
}

// Finally, we'll swap out the old backup for this new one in a single
// atomic step, combining the file already on-disk with this set of new
// channels.
err = s.Swapper.UpdateAndSwap(PackedMulti(b.Bytes()))
if err != nil {
return fmt.Errorf("unable to update multi backup: %v", err)
return fmt.Errorf("unable to update multi backup: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion chanbackup/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m *mockSwapper) UpdateAndSwap(newBackup PackedMulti) error {

swapState, err := newBackup.Unpack(m.keyChain)
if err != nil {
return fmt.Errorf("unable to decode on disk swaps: %v", err)
return fmt.Errorf("unable to decode on disk swaps: %w", err)
}

m.swapState = swapState
Expand Down
4 changes: 2 additions & 2 deletions chanbackup/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (s *Single) PackToWriter(w io.Writer, keyRing keychain.KeyRing) error {
// nonce that we used to the passed io.Reader.
e, err := lnencrypt.KeyRingEncrypter(keyRing)
if err != nil {
return fmt.Errorf("unable to generate encrypt key %v", err)
return fmt.Errorf("unable to generate encrypt key %w", err)
}
return e.EncryptPayloadToWriter(rawBytes.Bytes(), w)
}
Expand Down Expand Up @@ -544,7 +544,7 @@ func (s *Single) Deserialize(r io.Reader) error {
func (s *Single) UnpackFromReader(r io.Reader, keyRing keychain.KeyRing) error {
e, err := lnencrypt.KeyRingEncrypter(keyRing)
if err != nil {
return fmt.Errorf("unable to generate key decrypter %v", err)
return fmt.Errorf("unable to generate key decrypter %w", err)
}
plaintext, err := e.DecryptPayloadFromReader(r)
if err != nil {
Expand Down
Loading

0 comments on commit cd566eb

Please sign in to comment.