multi: fix fmt.Errorf error wrapping

Refactor fmt.Errorf usage to correctly wrap errors instead of using
non-wrapping format verbs.
This commit is contained in:
ffranr committed 2024-02-27 11:13:40 +00:00
1 parent 581c16d72f
commit cd566eb097
103 files changed
+426 -390

No files matched your search

+20 -13
View File
@@ -1015,7 +1015,7 @@ func (c *OpenChannel) Refresh() error {
// We'll re-populating the in-memory channel with the info
// fetched from disk.
if err := fetchChanInfo(chanBucket, c); err != nil {
return fmt.Errorf("unable to fetch chan info: %v", err)
return fmt.Errorf("unable to fetch chan info: %w", err)
}
// Also populate the channel's commitment states for both sides
@@ -1869,13 +1869,13 @@ func putOpenChannel(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
// First, we'll write out all the relatively static fields, that are
// decided upon initial channel creation.
if err := putChanInfo(chanBucket, channel); err != nil {
return fmt.Errorf("unable to store chan info: %v", err)
return fmt.Errorf("unable to store chan info: %w", err)
}
// With the static channel info written out, we'll now write out the
// current commitment state for both parties.
if err := putChanCommitments(chanBucket, channel); err != nil {
return fmt.Errorf("unable to store chan commitments: %v", err)
return fmt.Errorf("unable to store chan commitments: %w", err)
}
// Next, if this is a frozen channel, we'll add in the axillary
@@ -1885,14 +1885,15 @@ func putOpenChannel(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
chanBucket, channel.ThawHeight,
)
if err != nil {
return fmt.Errorf("unable to store thaw height: %v", err)
return fmt.Errorf("unable to store thaw height: %w",
err)
}
}
// Finally, we'll write out the revocation state for both parties
// within a distinct key space.
if err := putChanRevocationState(chanBucket, channel); err != nil {
return fmt.Errorf("unable to store chan revocations: %v", err)
return fmt.Errorf("unable to store chan revocations: %w", err)
}
return nil
@@ -1910,13 +1911,14 @@ func fetchOpenChannel(chanBucket kvdb.RBucket,
// First, we'll read all the static information that changes less
// frequently from disk.
if err := fetchChanInfo(chanBucket, channel); err != nil {
return nil, fmt.Errorf("unable to fetch chan info: %v", err)
return nil, fmt.Errorf("unable to fetch chan info: %w", err)
}
// With the static information read, we'll now read the current
// commitment state for both sides of the channel.
if err := fetchChanCommitments(chanBucket, channel); err != nil {
return nil, fmt.Errorf("unable to fetch chan commitments: %v", err)
return nil, fmt.Errorf("unable to fetch chan commitments: %w",
err)
}
// Next, if this is a frozen channel, we'll add in the axillary
@@ -1934,7 +1936,8 @@ func fetchOpenChannel(chanBucket kvdb.RBucket,
// Finally, we'll retrieve the current revocation state so we can
// properly
if err := fetchChanRevocationState(chanBucket, channel); err != nil {
return nil, fmt.Errorf("unable to fetch chan revocations: %v", err)
return nil, fmt.Errorf("unable to fetch chan revocations: %w",
err)
}
channel.Packager = NewChannelPackager(channel.ShortChannelID)
@@ -2043,7 +2046,7 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment,
}
if err = putChanInfo(chanBucket, c); err != nil {
return fmt.Errorf("unable to store chan info: %v", err)
return fmt.Errorf("unable to store chan info: %w", err)
}
// With the proper bucket fetched, we'll now write the latest
@@ -2130,12 +2133,14 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment,
var b3 bytes.Buffer
err = serializeLogUpdates(&b3, unsignedUpdates)
if err != nil {
return fmt.Errorf("unable to serialize log updates: %v", err)
return fmt.Errorf("unable to serialize log updates: %w",
err)
}
err = chanBucket.Put(remoteUnsignedLocalUpdatesKey, b3.Bytes())
if err != nil {
return fmt.Errorf("unable to restore chanbucket: %v", err)
return fmt.Errorf("unable to restore chanbucket: %w",
err)
}
return nil
@@ -3003,12 +3008,14 @@ func (c *OpenChannel) AdvanceCommitChainTail(fwdPkg *FwdPkg,
var b bytes.Buffer
err = serializeLogUpdates(&b, validUpdates)
if err != nil {
return fmt.Errorf("unable to serialize log updates: %v", err)
return fmt.Errorf("unable to serialize log updates: %w",
err)
}
err = chanBucket.Put(unsignedAckedUpdatesKey, b.Bytes())
if err != nil {
return fmt.Errorf("unable to store under unsignedAckedUpdatesKey: %v", err)
return fmt.Errorf("unable to store under "+
"unsignedAckedUpdatesKey: %w", err)
}
// Persist the local updates the peer hasn't yet signed so they
+1 -1
View File
@@ -497,7 +497,7 @@ func initChannelDB(db kvdb.Backend) error {
return putMeta(meta, tx)
}, func() {})
if err != nil {
return fmt.Errorf("unable to create new channeldb: %v", err)
return fmt.Errorf("unable to create new channeldb: %w", err)
}
return nil
+1 -1
View File
@@ -385,7 +385,7 @@ func initChannelGraph(db kvdb.Backend) error {
return err
}, func() {})
if err != nil {
return fmt.Errorf("unable to create new channel graph: %v", err)
return fmt.Errorf("unable to create new channel graph: %w", err)
}
return nil
+3 -3
View File
@@ -1212,7 +1212,7 @@ func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) {
// is a 2 byte length followed by the payload itself.
var failureLength uint16
if err := ReadElement(r, &failureLength); err != nil {
return nil, fmt.Errorf("unable to read error len: %v", err)
return nil, fmt.Errorf("unable to read error len: %w", err)
}
if failureLength > FailureMessageLength {
return nil, fmt.Errorf("failure message is too "+
@@ -1236,7 +1236,7 @@ func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) {
// the first two bytes of the buffer.
var codeBytes [2]byte
if _, err := io.ReadFull(r, codeBytes[:]); err != nil {
return nil, fmt.Errorf("unable to read failure code: %v", err)
return nil, fmt.Errorf("unable to read failure code: %w", err)
}
failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:]))
@@ -1244,7 +1244,7 @@ func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) {
// additional data if needed.
failure, err := makeEmptyOnionError(failCode)
if err != nil {
return nil, fmt.Errorf("unable to make empty error: %v", err)
return nil, fmt.Errorf("unable to make empty error: %w", err)
}
// Finally, if this failure has a payload, then we'll read that now as
@@ -223,7 +223,8 @@ func decodeShortChanIDs(r io.Reader) (ShortChanIDEncoding, []ShortChannelID, err
N: maxZlibBufSize,
})
if err != nil {
return 0, nil, fmt.Errorf("unable to create zlib reader: %v", err)
return 0, nil, fmt.Errorf("unable to create zlib "+
"reader: %w", err)
}
var (
+3 -3
View File
@@ -86,7 +86,7 @@ func findOpenChannels(openChanBucket kvdb.RBucket) ([]*OpenChannel, error) {
}
// Get the old channel info.
if err := fetchChanInfo(chanBucket, c, true); err != nil {
return fmt.Errorf("unable to fetch chan info: %v", err)
return fmt.Errorf("unable to fetch chan info: %w", err)
}
// Fetch the channel commitments, which are useful for freshly
@@ -154,7 +154,7 @@ func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error {
// Get the initial balances.
localAmt, remoteAmt, err := c.balancesAtHeight(chanBucket, 0)
if err != nil {
return fmt.Errorf("unable to get initial balances: %v", err)
return fmt.Errorf("unable to get initial balances: %w", err)
}
c.InitialLocalBalance = localAmt
@@ -162,7 +162,7 @@ func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error {
// Update the channel info.
if err := putChanInfo(chanBucket, c, false); err != nil {
return fmt.Errorf("unable to put chan info: %v", err)
return fmt.Errorf("unable to put chan info: %w", err)
}
return nil
+2 -2
View File
@@ -76,7 +76,7 @@ func findOpenChannels(openChanBucket kvdb.RBucket) ([]*OpenChannel, error) {
// Get the old channel info.
if err := FetchChanInfo(chanBucket, c, true); err != nil {
return fmt.Errorf("unable to fetch chan info: %v", err)
return fmt.Errorf("unable to fetch chan info: %w", err)
}
channels = append(channels, c)
@@ -139,7 +139,7 @@ func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error {
// `c.InitialRemoteBalance` then create the new tlv stream as
// requested.
if err := PutChanInfo(chanBucket, c, false); err != nil {
return fmt.Errorf("unable to put chan info: %v", err)
return fmt.Errorf("unable to put chan info: %w", err)
}
return nil
+8 -8
View File
@@ -82,7 +82,7 @@ func FetchChanInfo(chanBucket kvdb.RBucket, c *OpenChannel, legacy bool) error {
&c.IdentityPub, &c.Capacity, &c.TotalMSatSent,
&c.TotalMSatReceived,
); err != nil {
return fmt.Errorf("ReadElements got: %v", err)
return fmt.Errorf("ReadElements got: %w", err)
}
c.ChanType = mig25.ChannelType(chanType)
@@ -92,15 +92,15 @@ func FetchChanInfo(chanBucket kvdb.RBucket, c *OpenChannel, legacy bool) error {
// transaction to, read the funding txn.
if c.FundingTxPresent() {
if err := mig.ReadElement(r, &c.FundingTxn); err != nil {
return fmt.Errorf("read FundingTxn got: %v", err)
return fmt.Errorf("read FundingTxn got: %w", err)
}
}
if err := mig.ReadChanConfig(r, &c.LocalChanCfg); err != nil {
return fmt.Errorf("read LocalChanCfg got: %v", err)
return fmt.Errorf("read LocalChanCfg got: %w", err)
}
if err := mig.ReadChanConfig(r, &c.RemoteChanCfg); err != nil {
return fmt.Errorf("read RemoteChanCfg got: %v", err)
return fmt.Errorf("read RemoteChanCfg got: %w", err)
}
// Retrieve the boolean stored under lastWasRevokeKey.
@@ -114,7 +114,7 @@ func FetchChanInfo(chanBucket kvdb.RBucket, c *OpenChannel, legacy bool) error {
revokeReader := bytes.NewReader(lastWasRevokeBytes)
err := mig.ReadElements(revokeReader, &c.LastWasRevoke)
if err != nil {
return fmt.Errorf("read LastWasRevoke got: %v", err)
return fmt.Errorf("read LastWasRevoke got: %w", err)
}
}
@@ -147,11 +147,11 @@ func FetchChanInfo(chanBucket kvdb.RBucket, c *OpenChannel, legacy bool) error {
)
}
if err != nil {
return fmt.Errorf("create tlv stream got: %v", err)
return fmt.Errorf("create tlv stream got: %w", err)
}
if err := ts.Decode(r); err != nil {
return fmt.Errorf("decode tlv stream got: %v", err)
return fmt.Errorf("decode tlv stream got: %w", err)
}
// For the new format, attach the balance fields.
@@ -164,7 +164,7 @@ func FetchChanInfo(chanBucket kvdb.RBucket, c *OpenChannel, legacy bool) error {
if err := mig25.GetOptionalUpfrontShutdownScript(
chanBucket, localUpfrontShutdownKey, &c.LocalShutdownScript,
); err != nil {
return fmt.Errorf("local shutdown script got: %v", err)
return fmt.Errorf("local shutdown script got: %w", err)
}
return mig25.GetOptionalUpfrontShutdownScript(
+2 -2
View File
@@ -65,7 +65,7 @@ func findHistoricalChannels(historicalBucket kvdb.RBucket) ([]*OpenChannel,
chanPointBuf := bytes.NewBuffer(cp)
err := mig.ReadOutpoint(chanPointBuf, &c.FundingOutpoint)
if err != nil {
return fmt.Errorf("read funding outpoint got: %v", err)
return fmt.Errorf("read funding outpoint got: %w", err)
}
// Read the sub-bucket.
@@ -141,7 +141,7 @@ func migrateBalances(rootBucket kvdb.RwBucket, c *OpenChannel) error {
// Update the channel info.
if err := PutChanInfo(chanBucket, c, false); err != nil {
return fmt.Errorf("unable to put chan info: %v", err)
return fmt.Errorf("unable to put chan info: %w", err)
}
return nil
+8 -8
View File
@@ -117,7 +117,7 @@ func MigrateRevocationLog(db kvdb.Backend, cfg MigrateRevLogConfig) error {
// make sure the logs are migrated as expected.
err = kvdb.Update(db, validateMigration, func() {})
if err != nil {
return fmt.Errorf("validate migration failed: %v", err)
return fmt.Errorf("validate migration failed: %w", err)
}
log.Info("Migration check passed, now deleting the old logs...")
@@ -125,7 +125,7 @@ func MigrateRevocationLog(db kvdb.Backend, cfg MigrateRevLogConfig) error {
// Once the migration completes, we can now safety delete the old
// revocation logs.
if err := deleteOldBuckets(db); err != nil {
return fmt.Errorf("deleteOldBuckets err: %v", err)
return fmt.Errorf("deleteOldBuckets err: %w", err)
}
log.Info("Old revocation log buckets removed!")
@@ -146,7 +146,7 @@ func processMigration(tx kvdb.RwTx, cfg MigrateRevLogConfig) (bool, error) {
// Locate the next migration height.
locator, err := locateNextUpdateNum(openChanBucket)
if err != nil {
return false, fmt.Errorf("locator got error: %v", err)
return false, fmt.Errorf("locator got error: %w", err)
}
// If the returned locator is nil, we've done migrating the logs.
@@ -157,7 +157,7 @@ func processMigration(tx kvdb.RwTx, cfg MigrateRevLogConfig) (bool, error) {
// Read a list of old revocation logs.
entryMap, err := readOldRevocationLogs(openChanBucket, locator, cfg)
if err != nil {
return false, fmt.Errorf("read old logs err: %v", err)
return false, fmt.Errorf("read old logs err: %w", err)
}
// Migrate the revocation logs.
@@ -232,7 +232,7 @@ func writeRevocationLogs(openChanBucket kvdb.RwBucket,
// Find the channel bucket.
chanBucket, err := locator.locateChanBucket(openChanBucket)
if err != nil {
return fmt.Errorf("locateChanBucket err: %v", err)
return fmt.Errorf("locateChanBucket err: %w", err)
}
// Create the new log bucket.
@@ -240,7 +240,7 @@ func writeRevocationLogs(openChanBucket kvdb.RwBucket,
revocationLogBucket,
)
if err != nil {
return fmt.Errorf("create log bucket err: %v", err)
return fmt.Errorf("create log bucket err: %w", err)
}
// Write the new logs.
@@ -345,7 +345,7 @@ func fetchLogStats(tx kvdb.RwTx) (uint64, uint64, error) {
// Locate the next migration height.
locator, err := locateNextUpdateNum(openChanBucket)
if err != nil {
return 0, 0, fmt.Errorf("locator got error: %v", err)
return 0, 0, fmt.Errorf("locator got error: %w", err)
}
// If the returned locator is not nil, we still have un-migrated
@@ -491,7 +491,7 @@ func readOldRevocationLogs(openChanBucket kvdb.RwBucket,
c := &mig26.OpenChannel{}
err := mig26.FetchChanInfo(chanBucket, c, false)
if err != nil {
return fmt.Errorf("unable to fetch chan info: %v", err)
return fmt.Errorf("unable to fetch chan info: %w", err)
}
err = fetchChanRevocationState(chanBucket, c)
+3 -3
View File
@@ -334,18 +334,18 @@ func setupTestLogs(db kvdb.Backend, c *mig26.OpenChannel,
// Save channel info.
if err := mig26.PutChanInfo(chanBucket, c, false); err != nil {
return fmt.Errorf("PutChanInfo got %v", err)
return fmt.Errorf("PutChanInfo got %w", err)
}
// Save revocation state.
if err := putChanRevocationState(chanBucket, c); err != nil {
return fmt.Errorf("putChanRevocationState got %v", err)
return fmt.Errorf("putChanRevocationState got %w", err)
}
// Create old logs.
err = writeOldRevocationLogs(chanBucket, oldLogs)
if err != nil {
return fmt.Errorf("write old logs: %v", err)
return fmt.Errorf("write old logs: %w", err)
}
// Create new logs.
@@ -156,7 +156,7 @@ func signDigestCompact(hash []byte) ([]byte, error) {
// ecdsa.SignCompact returns a pubkey-recoverable signature
sig, err := ecdsa.SignCompact(privKey, hash, isCompressedKey)
if err != nil {
return nil, fmt.Errorf("can't sign the hash: %v", err)
return nil, fmt.Errorf("can't sign the hash: %w", err)
}
return sig, nil
+11 -10
View File
@@ -22,13 +22,13 @@ func MigrateNodeAndEdgeUpdateIndex(tx kvdb.RwTx) error {
// where these items will be housed.
nodes, err := tx.CreateTopLevelBucket(nodeBucket)
if err != nil {
return fmt.Errorf("unable to create node bucket: %v", err)
return fmt.Errorf("unable to create node bucket: %w", err)
}
nodeUpdateIndex, err := nodes.CreateBucketIfNotExists(
nodeUpdateIndexBucket,
)
if err != nil {
return fmt.Errorf("unable to create node update index: %v", err)
return fmt.Errorf("unable to create node update index: %w", err)
}
log.Infof("Populating new node update index bucket")
@@ -57,7 +57,7 @@ func MigrateNodeAndEdgeUpdateIndex(tx kvdb.RwTx) error {
return nodeUpdateIndex.Put(indexKey[:], nil)
})
if err != nil {
return fmt.Errorf("unable to update node indexes: %v", err)
return fmt.Errorf("unable to update node indexes: %w", err)
}
log.Infof("Populating new edge update index bucket")
@@ -66,13 +66,13 @@ func MigrateNodeAndEdgeUpdateIndex(tx kvdb.RwTx) error {
// corresponding entry in the edge update index.
edges, err := tx.CreateTopLevelBucket(edgeBucket)
if err != nil {
return fmt.Errorf("unable to create edge bucket: %v", err)
return fmt.Errorf("unable to create edge bucket: %w", err)
}
edgeUpdateIndex, err := edges.CreateBucketIfNotExists(
edgeUpdateIndexBucket,
)
if err != nil {
return fmt.Errorf("unable to create edge update index: %v", err)
return fmt.Errorf("unable to create edge update index: %w", err)
}
// We'll now run through each edge policy in the database, and update
@@ -109,7 +109,7 @@ func MigrateNodeAndEdgeUpdateIndex(tx kvdb.RwTx) error {
return edgeUpdateIndex.Put(indexKey[:], nil)
})
if err != nil {
return fmt.Errorf("unable to update edge indexes: %v", err)
return fmt.Errorf("unable to update edge indexes: %w", err)
}
log.Infof("Migration to node and edge update indexes complete!")
@@ -169,7 +169,7 @@ func MigrateInvoiceTimeSeries(tx kvdb.RwTx) error {
invoiceReader := bytes.NewReader(invoiceBytesCopy)
invoice, err := deserializeInvoiceLegacy(invoiceReader)
if err != nil {
return fmt.Errorf("unable to decode invoice: %v", err)
return fmt.Errorf("unable to decode invoice: %w", err)
}
// Now that we have the fully decoded invoice, we can update
@@ -307,7 +307,8 @@ func MigrateInvoiceTimeSeriesOutgoingPayments(tx kvdb.RwTx) error {
paymentReader := bytes.NewReader(paymentCopy)
_, err := deserializeOutgoingPayment(paymentReader)
if err != nil {
return fmt.Errorf("unable to deserialize payment: %v", err)
return fmt.Errorf("unable to deserialize payment: %w",
err)
}
// Now that we know the modifications was successful, we'll
@@ -400,7 +401,7 @@ func MigrateEdgePolicies(tx kvdb.RwTx) error {
})
if err != nil {
return fmt.Errorf("unable to update edge policies: %v", err)
return fmt.Errorf("unable to update edge policies: %w", err)
}
log.Infof("Migration of edge policies complete!")
@@ -649,7 +650,7 @@ func MigrateOptionalChannelCloseSummaryFields(tx kvdb.RwTx) error {
return nil
})
if err != nil {
return fmt.Errorf("unable to update closed channels: %v", err)
return fmt.Errorf("unable to update closed channels: %w", err)
}
// Now put the new format back into the DB.