From 8cf567b948f612770ed925bb468526be07e103fb Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 30 Jun 2025 08:46:50 +0200 Subject: [PATCH 1/2] multi: use the "errors" package everywhere Replace all usages of the "github.com/go-errors/errors" and "github.com/pkg/errors" packages with the standard lib's "errors" package. This ensures that error wrapping and `errors.Is` checks will work as expected. --- channeldb/db.go | 2 +- channeldb/meta_test.go | 5 ++- channeldb/migration/lnwire21/lnwire.go | 2 +- channeldb/migration/lnwire21/onion_error.go | 3 +- channeldb/migration_01_to_11/meta_test.go | 4 +- .../migration_01_to_11/migrations_test.go | 2 +- channeldb/waitingproof.go | 2 +- channeldb/waitingproof_test.go | 2 +- contractcourt/breach_arbitrator_test.go | 2 +- discovery/gossiper_test.go | 2 +- discovery/validation_barrier.go | 2 +- funding/manager.go | 31 +++++++------- go.mod | 3 +- go.sum | 2 - graph/builder.go | 20 ++++----- graph/builder_test.go | 2 +- graph/db/notifications.go | 5 +-- graph/errors.go | 8 ++-- htlcswitch/circuit_map.go | 2 +- htlcswitch/interceptable_switch.go | 2 +- htlcswitch/linkfailure.go | 2 +- htlcswitch/mock.go | 2 +- htlcswitch/resolution_store.go | 2 +- htlcswitch/sequencer.go | 2 +- htlcswitch/switch_test.go | 2 +- htlcswitch/test_utils.go | 42 +++++++++---------- itest/lnd_channel_force_close_test.go | 2 +- itest/lnd_revocation_test.go | 2 +- itest/lnd_watchtower_test.go | 2 +- itest/lnd_zero_conf_test.go | 2 +- lntest/harness.go | 22 +++++++--- lnwire/lnwire.go | 2 +- lnwire/onion_error.go | 3 +- netann/channel_update.go | 7 ++-- netann/node_announcement.go | 4 +- routing/bandwidth_test.go | 2 +- routing/mock_test.go | 2 +- routing/payment_lifecycle_test.go | 2 +- routing/probability_bimodal.go | 2 +- routing/router.go | 2 +- routing/router_test.go | 3 +- server.go | 2 +- shachain/element_test.go | 2 +- shachain/store.go | 5 ++- 44 files changed, 114 insertions(+), 109 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index 6c467122a..793eb7018 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/binary" + "errors" "fmt" "net" "os" @@ -12,7 +13,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcwallet/walletdb" - "github.com/go-errors/errors" mig "github.com/lightningnetwork/lnd/channeldb/migration" "github.com/lightningnetwork/lnd/channeldb/migration12" "github.com/lightningnetwork/lnd/channeldb/migration13" diff --git a/channeldb/meta_test.go b/channeldb/meta_test.go index 57c549bd3..066678c1a 100644 --- a/channeldb/meta_test.go +++ b/channeldb/meta_test.go @@ -2,10 +2,11 @@ package channeldb import ( "bytes" + "errors" + "fmt" "testing" "github.com/btcsuite/btcwallet/walletdb" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/kvdb" "github.com/stretchr/testify/require" ) @@ -48,7 +49,7 @@ func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB), if dryRun && r != ErrDryRunMigrationOK { t.Fatalf("expected dry run migration OK") } - err = errors.New(r) + err = fmt.Errorf("%v", r) } if err == nil && shouldFail { diff --git a/channeldb/migration/lnwire21/lnwire.go b/channeldb/migration/lnwire21/lnwire.go index a88c5e50b..e3f96e2cd 100644 --- a/channeldb/migration/lnwire21/lnwire.go +++ b/channeldb/migration/lnwire21/lnwire.go @@ -3,6 +3,7 @@ package lnwire import ( "bytes" "encoding/binary" + "errors" "fmt" "image/color" "io" @@ -13,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/tor" ) diff --git a/channeldb/migration/lnwire21/onion_error.go b/channeldb/migration/lnwire21/onion_error.go index e9d1f8f0c..1f5628212 100644 --- a/channeldb/migration/lnwire21/onion_error.go +++ b/channeldb/migration/lnwire21/onion_error.go @@ -9,7 +9,6 @@ import ( "io" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/tlv" ) @@ -1451,7 +1450,7 @@ func makeEmptyOnionError(code FailCode) (FailureMessage, error) { return &FailInvalidBlinding{}, nil default: - return nil, errors.Errorf("unknown error code: %v", code) + return nil, fmt.Errorf("unknown error code: %v", code) } } diff --git a/channeldb/migration_01_to_11/meta_test.go b/channeldb/migration_01_to_11/meta_test.go index 513b90d56..6c0016610 100644 --- a/channeldb/migration_01_to_11/meta_test.go +++ b/channeldb/migration_01_to_11/meta_test.go @@ -1,9 +1,9 @@ package migration_01_to_11 import ( + "fmt" "testing" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/kvdb" ) @@ -33,7 +33,7 @@ func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB), defer func() { if r := recover(); r != nil { - err = errors.New(r) + err = fmt.Errorf("%v", r) } if err == nil && shouldFail { diff --git a/channeldb/migration_01_to_11/migrations_test.go b/channeldb/migration_01_to_11/migrations_test.go index 9c47bdcdf..7c28d2039 100644 --- a/channeldb/migration_01_to_11/migrations_test.go +++ b/channeldb/migration_01_to_11/migrations_test.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha256" "encoding/binary" + "errors" "fmt" "math/rand" "reflect" @@ -12,7 +13,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" lnwire "github.com/lightningnetwork/lnd/channeldb/migration/lnwire21" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lntypes" diff --git a/channeldb/waitingproof.go b/channeldb/waitingproof.go index faefc620c..0c3913f9b 100644 --- a/channeldb/waitingproof.go +++ b/channeldb/waitingproof.go @@ -3,11 +3,11 @@ package channeldb import ( "bytes" "encoding/binary" + "errors" "fmt" "io" "sync" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lnwire" ) diff --git a/channeldb/waitingproof_test.go b/channeldb/waitingproof_test.go index d7113d9e7..7155a6c99 100644 --- a/channeldb/waitingproof_test.go +++ b/channeldb/waitingproof_test.go @@ -1,11 +1,11 @@ package channeldb import ( + "errors" "reflect" "testing" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/lnwire" "github.com/stretchr/testify/require" ) diff --git a/contractcourt/breach_arbitrator_test.go b/contractcourt/breach_arbitrator_test.go index 99ed85269..ac6477692 100644 --- a/contractcourt/breach_arbitrator_test.go +++ b/contractcourt/breach_arbitrator_test.go @@ -5,6 +5,7 @@ import ( crand "crypto/rand" "crypto/sha256" "encoding/binary" + "errors" "fmt" "io" "math/rand" @@ -19,7 +20,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/fn/v2" diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index dd8eca40d..a96ea0952 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/hex" + "errors" "fmt" prand "math/rand" "net" @@ -20,7 +21,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightninglabs/neutrino/cache" "github.com/lightningnetwork/lnd/batch" "github.com/lightningnetwork/lnd/chainntnfs" diff --git a/discovery/validation_barrier.go b/discovery/validation_barrier.go index bd3bc7039..7153dd49e 100644 --- a/discovery/validation_barrier.go +++ b/discovery/validation_barrier.go @@ -1,11 +1,11 @@ package discovery import ( + "errors" "fmt" "sync" "sync/atomic" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" diff --git a/funding/manager.go b/funding/manager.go index 068773a19..c78bf8ddd 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -3,6 +3,7 @@ package funding import ( "bytes" "encoding/binary" + "errors" "fmt" "io" "sync" @@ -18,7 +19,6 @@ import ( "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chanacceptor" "github.com/lightningnetwork/lnd/channeldb" @@ -4436,8 +4436,8 @@ func (f *Manager) newChanAnnouncement(localPubKey, // channel announcement. storedFwdingPolicy, err := f.getInitialForwardingPolicy(chanID) if err != nil && !errors.Is(err, channeldb.ErrChannelNotFound) { - return nil, errors.Errorf("unable to generate channel "+ - "update announcement: %v", err) + return nil, fmt.Errorf("unable to generate channel "+ + "update announcement: %w", err) } switch { @@ -4480,13 +4480,13 @@ func (f *Manager) newChanAnnouncement(localPubKey, } sig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanUpdateMsg, true) if err != nil { - return nil, errors.Errorf("unable to generate channel "+ - "update announcement signature: %v", err) + return nil, fmt.Errorf("unable to generate channel "+ + "update announcement signature: %w", err) } chanUpdateAnn.Signature, err = lnwire.NewSigFromSignature(sig) if err != nil { - return nil, errors.Errorf("unable to generate channel "+ - "update announcement signature: %v", err) + return nil, fmt.Errorf("unable to generate channel "+ + "update announcement signature: %w", err) } // The channel existence proofs itself is currently announced in @@ -4502,15 +4502,15 @@ func (f *Manager) newChanAnnouncement(localPubKey, } nodeSig, err := f.cfg.SignMessage(f.cfg.IDKeyLoc, chanAnnMsg, true) if err != nil { - return nil, errors.Errorf("unable to generate node "+ - "signature for channel announcement: %v", err) + return nil, fmt.Errorf("unable to generate node "+ + "signature for channel announcement: %w", err) } bitcoinSig, err := f.cfg.SignMessage( localFundingKey.KeyLocator, chanAnnMsg, true, ) if err != nil { - return nil, errors.Errorf("unable to generate bitcoin "+ - "signature for node public key: %v", err) + return nil, fmt.Errorf("unable to generate bitcoin "+ + "signature for node public key: %w", err) } // Finally, we'll generate the announcement proof which we'll use to @@ -5172,13 +5172,13 @@ func (f *Manager) cancelReservationCtx(peerKey *btcec.PublicKey, nodeReservations, ok := f.activeReservations[peerIDKey] if !ok { // No reservations for this node. - return nil, errors.Errorf("no active reservations for peer(%x)", + return nil, fmt.Errorf("no active reservations for peer(%x)", peerIDKey[:]) } ctx, ok := nodeReservations[pendingChanID] if !ok { - return nil, errors.Errorf("unknown channel (id: %x) for "+ + return nil, fmt.Errorf("unknown channel (id: %x) for "+ "peer(%x)", pendingChanID[:], peerIDKey[:]) } @@ -5191,8 +5191,7 @@ func (f *Manager) cancelReservationCtx(peerKey *btcec.PublicKey, } if err := ctx.reservation.Cancel(); err != nil { - return nil, errors.Errorf("unable to cancel reservation: %v", - err) + return nil, fmt.Errorf("unable to cancel reservation: %w", err) } delete(nodeReservations, pendingChanID) @@ -5239,7 +5238,7 @@ func (f *Manager) getReservationCtx(peerKey *btcec.PublicKey, f.resMtx.RUnlock() if !ok { - return nil, errors.Errorf("unknown channel (id: %x) for "+ + return nil, fmt.Errorf("unknown channel (id: %x) for "+ "peer(%x)", pendingChanID[:], peerIDKey[:]) } diff --git a/go.mod b/go.mod index c9b552c8f..4a5beee62 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f github.com/davecgh/go-spew v1.1.1 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 - github.com/go-errors/errors v1.0.1 github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/gorilla/websocket v1.5.0 @@ -48,7 +47,7 @@ require ( github.com/lightningnetwork/lnd/tor v1.1.6 github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 github.com/miekg/dns v1.1.43 - github.com/pkg/errors v0.9.1 + github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.11.1 github.com/stretchr/testify v1.9.0 github.com/tv42/zbase32 v0.0.0-20160707012821-501572607d02 diff --git a/go.sum b/go.sum index c9b62acff..0f8782d99 100644 --- a/go.sum +++ b/go.sum @@ -156,8 +156,6 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= -github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= diff --git a/graph/builder.go b/graph/builder.go index 028e1c631..df4f3bfdf 100644 --- a/graph/builder.go +++ b/graph/builder.go @@ -2,6 +2,7 @@ package graph import ( "context" + "errors" "fmt" "sync" "sync/atomic" @@ -9,7 +10,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/batch" "github.com/lightningnetwork/lnd/chainntnfs" graphdb "github.com/lightningnetwork/lnd/graph/db" @@ -875,8 +875,8 @@ func (b *Builder) assertNodeAnnFreshness(ctx context.Context, node route.Vertex, // already have. lastUpdate, exists, err := b.cfg.Graph.HasLightningNode(ctx, node) if err != nil { - return errors.Errorf("unable to query for the "+ - "existence of node: %v", err) + return fmt.Errorf("unable to query for the "+ + "existence of node: %w", err) } if !exists { return NewErrf(ErrIgnored, "Ignoring node announcement"+ @@ -1004,8 +1004,8 @@ func (b *Builder) addNode(ctx context.Context, node *models.LightningNode, } if err := b.cfg.Graph.AddLightningNode(ctx, node, op...); err != nil { - return errors.Errorf("unable to add node %x to the "+ - "graph: %v", node.PubKeyBytes, err) + return fmt.Errorf("unable to add node %x to the "+ + "graph: %w", node.PubKeyBytes, err) } log.Tracef("Updated vertex data for node=%x", node.PubKeyBytes) @@ -1051,7 +1051,7 @@ func (b *Builder) addEdge(ctx context.Context, edge *models.ChannelEdgeInfo, edge.ChannelID, ) if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) { - return errors.Errorf("unable to check for edge existence: %v", + return fmt.Errorf("unable to check for edge existence: %w", err) } if isZombie { @@ -1109,8 +1109,7 @@ func (b *Builder) addEdge(ctx context.Context, edge *models.ChannelEdgeInfo, err = b.cfg.ChainView.UpdateFilter(filterUpdate, b.bestHeight.Load()) if err != nil { - return errors.Errorf("unable to update chain "+ - "view: %v", err) + return fmt.Errorf("unable to update chain view: %w", err) } return nil @@ -1152,8 +1151,7 @@ func (b *Builder) updateEdge(ctx context.Context, edge1Timestamp, edge2Timestamp, exists, isZombie, err := b.cfg.Graph.HasChannelEdge(policy.ChannelID) if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) { - return errors.Errorf("unable to check for edge existence: %v", - err) + return fmt.Errorf("unable to check for edge existence: %w", err) } // If the channel is marked as a zombie in our database, and @@ -1212,7 +1210,7 @@ func (b *Builder) updateEdge(ctx context.Context, // Now that we know this isn't a stale update, we'll apply the new edge // policy to the proper directional edge within the channel graph. if err = b.cfg.Graph.UpdateEdgePolicy(ctx, policy, op...); err != nil { - err := errors.Errorf("unable to add channel: %v", err) + err := fmt.Errorf("unable to add channel: %w", err) log.Error(err) return err } diff --git a/graph/builder_test.go b/graph/builder_test.go index a27605269..a7bd5ed91 100644 --- a/graph/builder_test.go +++ b/graph/builder_test.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "image/color" "math/rand" @@ -19,7 +20,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/fn/v2" graphdb "github.com/lightningnetwork/lnd/graph/db" diff --git a/graph/db/notifications.go b/graph/db/notifications.go index 4af953381..20f2f07f0 100644 --- a/graph/db/notifications.go +++ b/graph/db/notifications.go @@ -1,6 +1,7 @@ package graphdb import ( + "errors" "fmt" "image/color" "net" @@ -11,7 +12,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/lnutils" @@ -412,8 +412,7 @@ func (c *ChannelGraph) addToTopologyChange(update *TopologyChange, // being connected. edgeInfo, _, _, err := c.FetchChannelEdgesByID(m.ChannelID) if err != nil { - return errors.Errorf("unable fetch channel edge: %v", - err) + return fmt.Errorf("unable fetch channel edge: %w", err) } // If the flag is one, then the advertising node is actually diff --git a/graph/errors.go b/graph/errors.go index 729107419..5a81c9d05 100644 --- a/graph/errors.go +++ b/graph/errors.go @@ -1,6 +1,8 @@ package graph -import "github.com/go-errors/errors" +import ( + "fmt" +) // ErrorCode is used to represent the various errors that can occur within this // package. @@ -21,7 +23,7 @@ const ( // this structure carries additional information about error code in order to // be able distinguish errors outside of the current package. type Error struct { - err *errors.Error + err error code ErrorCode } @@ -39,7 +41,7 @@ var _ error = (*Error)(nil) func NewErrf(code ErrorCode, format string, a ...interface{}) *Error { return &Error{ code: code, - err: errors.Errorf(format, a...), + err: fmt.Errorf(format, a...), } } diff --git a/htlcswitch/circuit_map.go b/htlcswitch/circuit_map.go index 076d6f284..15d4b5ffc 100644 --- a/htlcswitch/circuit_map.go +++ b/htlcswitch/circuit_map.go @@ -2,10 +2,10 @@ package htlcswitch import ( "bytes" + "errors" "fmt" "sync" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/kvdb" diff --git a/htlcswitch/interceptable_switch.go b/htlcswitch/interceptable_switch.go index 6414c9f80..3d0bd90ed 100644 --- a/htlcswitch/interceptable_switch.go +++ b/htlcswitch/interceptable_switch.go @@ -2,11 +2,11 @@ package htlcswitch import ( "crypto/sha256" + "errors" "fmt" "sync" "sync/atomic" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/graph/db/models" diff --git a/htlcswitch/linkfailure.go b/htlcswitch/linkfailure.go index 495bd46fc..a2ce7305f 100644 --- a/htlcswitch/linkfailure.go +++ b/htlcswitch/linkfailure.go @@ -1,6 +1,6 @@ package htlcswitch -import "github.com/go-errors/errors" +import "errors" var ( // ErrLinkShuttingDown signals that the link is shutting down. diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 136d5c39b..70bd73c37 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -5,6 +5,7 @@ import ( "context" "crypto/sha256" "encoding/binary" + "errors" "fmt" "io" "net" @@ -18,7 +19,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" diff --git a/htlcswitch/resolution_store.go b/htlcswitch/resolution_store.go index 20ac4e5d8..e2d1b9fab 100644 --- a/htlcswitch/resolution_store.go +++ b/htlcswitch/resolution_store.go @@ -2,9 +2,9 @@ package htlcswitch import ( "bytes" + "errors" "io" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/kvdb" diff --git a/htlcswitch/sequencer.go b/htlcswitch/sequencer.go index 3f37414da..82ccc83f8 100644 --- a/htlcswitch/sequencer.go +++ b/htlcswitch/sequencer.go @@ -1,9 +1,9 @@ package htlcswitch import ( + "errors" "sync" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/kvdb" ) diff --git a/htlcswitch/switch_test.go b/htlcswitch/switch_test.go index 880932146..9a9627579 100644 --- a/htlcswitch/switch_test.go +++ b/htlcswitch/switch_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "crypto/sha256" + "errors" "fmt" "io" mrand "math/rand" @@ -13,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 43902dc33..bdb365d3c 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -7,6 +7,7 @@ import ( "crypto/sha256" "encoding/binary" "encoding/hex" + "errors" "fmt" "net" "os" @@ -21,7 +22,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" @@ -395,12 +395,12 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte, aliceStoredChannels, err = dbAlice.ChannelStateDB(). FetchOpenChannels(aliceKeyPub) if err != nil { - return nil, errors.Errorf("unable to fetch alice "+ - "channel: %v", err) + return nil, fmt.Errorf("unable to fetch alice "+ + "channel: %w", err) } default: - return nil, errors.Errorf("unable to fetch alice channel: "+ - "%v", err) + return nil, fmt.Errorf("unable to fetch alice "+ + "channel: %w", err) } var aliceStoredChannel *channeldb.OpenChannel @@ -421,8 +421,8 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte, lnwallet.WithAuxSigner(signerMock), ) if err != nil { - return nil, errors.Errorf("unable to create new channel: %v", - err) + return nil, fmt.Errorf("unable to create new "+ + "channel: %w", err) } return newAliceChannel, nil @@ -436,19 +436,19 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte, case kvdb.ErrDatabaseNotOpen: dbBob = channeldb.OpenForTesting(t, dbBob.Path()) if err != nil { - return nil, errors.Errorf("unable to reopen bob "+ - "db: %v", err) + return nil, fmt.Errorf("unable to reopen bob "+ + "db: %w", err) } bobStoredChannels, err = dbBob.ChannelStateDB(). FetchOpenChannels(bobKeyPub) if err != nil { - return nil, errors.Errorf("unable to fetch bob "+ - "channel: %v", err) + return nil, fmt.Errorf("unable to fetch bob "+ + "channel: %w", err) } default: - return nil, errors.Errorf("unable to fetch bob channel: "+ - "%v", err) + return nil, fmt.Errorf("unable to fetch bob channel: "+ + "%w", err) } var bobStoredChannel *channeldb.OpenChannel @@ -469,8 +469,8 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte, lnwallet.WithAuxSigner(signerMock), ) if err != nil { - return nil, errors.Errorf("unable to create new channel: %v", - err) + return nil, fmt.Errorf("unable to create new "+ + "channel: %w", err) } return newBobChannel, nil } @@ -881,16 +881,16 @@ func createClusterChannels(t *testing.T, aliceToBob, bobToCarol btcutil.Amount) bobPrivKey, aliceToBob, aliceToBob, 0, 0, firstChanID, ) if err != nil { - return nil, nil, errors.Errorf("unable to create "+ - "alice<->bob channel: %v", err) + return nil, nil, fmt.Errorf("unable to create "+ + "alice<->bob channel: %w", err) } secondBobChannel, carolChannel, err := createTestChannel(t, bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, 0, 0, secondChanID, ) if err != nil { - return nil, nil, errors.Errorf("unable to create "+ - "bob<->carol channel: %v", err) + return nil, nil, fmt.Errorf("unable to create "+ + "bob<->carol channel: %w", err) } restoreFromDb := func() (*clusterChannels, error) { @@ -1070,8 +1070,8 @@ func createMirroredChannel(t *testing.T, aliceToBob, aliceToBob, bobToAlice, 0, 0, firstChanID, ) if err != nil { - return nil, nil, errors.Errorf("unable to create "+ - "alice<->bob channel: %v", err) + return nil, nil, fmt.Errorf("unable to create "+ + "alice<->bob channel: %w", err) } return alice, bob, nil diff --git a/itest/lnd_channel_force_close_test.go b/itest/lnd_channel_force_close_test.go index c04036a76..b05b838ff 100644 --- a/itest/lnd_channel_force_close_test.go +++ b/itest/lnd_channel_force_close_test.go @@ -2,11 +2,11 @@ package itest import ( "bytes" + "errors" "fmt" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" diff --git a/itest/lnd_revocation_test.go b/itest/lnd_revocation_test.go index c2508b885..b975438f1 100644 --- a/itest/lnd_revocation_test.go +++ b/itest/lnd_revocation_test.go @@ -2,13 +2,13 @@ package itest import ( "bytes" + "errors" "fmt" "testing" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lntest" diff --git a/itest/lnd_watchtower_test.go b/itest/lnd_watchtower_test.go index 1b63f9063..77781ced0 100644 --- a/itest/lnd_watchtower_test.go +++ b/itest/lnd_watchtower_test.go @@ -1,12 +1,12 @@ package itest import ( + "errors" "fmt" "testing" "time" "github.com/btcsuite/btcd/btcutil" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" diff --git a/itest/lnd_zero_conf_test.go b/itest/lnd_zero_conf_test.go index 5b87846be..ac82ba0d5 100644 --- a/itest/lnd_zero_conf_test.go +++ b/itest/lnd_zero_conf_test.go @@ -1,12 +1,12 @@ package itest import ( + "errors" "fmt" "testing" "time" "github.com/btcsuite/btcd/btcutil" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/aliasmgr" "github.com/lightningnetwork/lnd/chainreg" "github.com/lightningnetwork/lnd/lnrpc" diff --git a/lntest/harness.go b/lntest/harness.go index 465c4b464..1d6aff553 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -3,6 +3,7 @@ package lntest import ( "context" "fmt" + "runtime/debug" "strings" "testing" "time" @@ -13,7 +14,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/kvdb/etcd" @@ -293,10 +293,22 @@ func (h *HarnessTest) Stop() { // represented as fatal. func (h *HarnessTest) RunTestCase(testCase *TestCase) { defer func() { - if err := recover(); err != nil { - description := errors.Wrap(err, 2).ErrorStack() - h.Fatalf("Failed: (%v) panic with: \n%v", - testCase.Name, description) + if r := recover(); r != nil { + // Wrap the recovered panic in an error. + var err error + switch v := r.(type) { + case error: + err = v + default: + err = fmt.Errorf("%v", v) + } + + // Capture and print the stack trace. + stack := debug.Stack() + + // Fail the test with panic info and stack. + h.Fatalf("Failed: (%v) panic with: %v\n%s", + testCase.Name, err, stack) } }() diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index fa43d25bf..7d6866582 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -3,6 +3,7 @@ package lnwire import ( "bytes" "encoding/binary" + "errors" "fmt" "image/color" "io" @@ -13,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/tor" ) diff --git a/lnwire/onion_error.go b/lnwire/onion_error.go index 7b65a85f4..2cbf548fa 100644 --- a/lnwire/onion_error.go +++ b/lnwire/onion_error.go @@ -9,7 +9,6 @@ import ( "io" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/tlv" ) @@ -1513,7 +1512,7 @@ func makeEmptyOnionError(code FailCode) (FailureMessage, error) { return &FailInvalidBlinding{}, nil default: - return nil, errors.Errorf("unknown error code: %v", code) + return nil, fmt.Errorf("unknown error code: %v", code) } } diff --git a/netann/channel_update.go b/netann/channel_update.go index 1bb509b08..d453b5306 100644 --- a/netann/channel_update.go +++ b/netann/channel_update.go @@ -14,7 +14,6 @@ import ( "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/tlv" - "github.com/pkg/errors" ) const ( @@ -279,13 +278,13 @@ func validateChannelUpdate1Fields(capacity btcutil.Amount, // The maxHTLC flag is mandatory. if !msg.MessageFlags.HasMaxHtlc() { - return errors.Errorf("max htlc flag not set for channel "+ + return fmt.Errorf("max htlc flag not set for channel "+ "update %v", spew.Sdump(msg)) } maxHtlc := msg.HtlcMaximumMsat if maxHtlc == 0 || maxHtlc < msg.HtlcMinimumMsat { - return errors.Errorf("invalid max htlc for channel "+ + return fmt.Errorf("invalid max htlc for channel "+ "update %v", spew.Sdump(msg)) } @@ -294,7 +293,7 @@ func validateChannelUpdate1Fields(capacity btcutil.Amount, // capacity. capacityMsat := lnwire.NewMSatFromSatoshis(capacity) if capacityMsat != 0 && maxHtlc > capacityMsat { - return errors.Errorf("max_htlc (%v) for channel update "+ + return fmt.Errorf("max_htlc (%v) for channel update "+ "greater than capacity (%v)", maxHtlc, capacityMsat) } diff --git a/netann/node_announcement.go b/netann/node_announcement.go index 3b5114c7d..712502170 100644 --- a/netann/node_announcement.go +++ b/netann/node_announcement.go @@ -2,13 +2,13 @@ package netann import ( "bytes" + "fmt" "image/color" "net" "time" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" @@ -110,7 +110,7 @@ func ValidateNodeAnn(a *lnwire.NodeAnnouncement) error { return err } - return errors.Errorf("signature on NodeAnnouncement(%x) is "+ + return fmt.Errorf("signature on NodeAnnouncement(%x) is "+ "invalid: %x", nodeKey.SerializeCompressed(), msgBuf.Bytes()) } diff --git a/routing/bandwidth_test.go b/routing/bandwidth_test.go index 7e0bca81c..4099b9608 100644 --- a/routing/bandwidth_test.go +++ b/routing/bandwidth_test.go @@ -1,10 +1,10 @@ package routing import ( + "errors" "testing" "github.com/btcsuite/btcd/btcutil" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/lnwallet" diff --git a/routing/mock_test.go b/routing/mock_test.go index a1268d0c3..d3601636c 100644 --- a/routing/mock_test.go +++ b/routing/mock_test.go @@ -1,12 +1,12 @@ package routing import ( + "errors" "fmt" "sync" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/graph/db/models" diff --git a/routing/payment_lifecycle_test.go b/routing/payment_lifecycle_test.go index 9df02ec32..6d4c2fb45 100644 --- a/routing/payment_lifecycle_test.go +++ b/routing/payment_lifecycle_test.go @@ -2,12 +2,12 @@ package routing import ( "context" + "errors" "sync/atomic" "testing" "time" "github.com/btcsuite/btcd/btcec/v2" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/htlcswitch" diff --git a/routing/probability_bimodal.go b/routing/probability_bimodal.go index 35d1f1073..748a8d1c1 100644 --- a/routing/probability_bimodal.go +++ b/routing/probability_bimodal.go @@ -1,12 +1,12 @@ package routing import ( + "errors" "fmt" "math" "time" "github.com/btcsuite/btcd/btcutil" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) diff --git a/routing/router.go b/routing/router.go index b0c6bd323..dcece2566 100644 --- a/routing/router.go +++ b/routing/router.go @@ -2,6 +2,7 @@ package routing import ( "context" + "errors" "fmt" "math" "math/big" @@ -13,7 +14,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/amp" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/clock" diff --git a/routing/router_test.go b/routing/router_test.go index 3394e2427..d6e2fe52c 100644 --- a/routing/router_test.go +++ b/routing/router_test.go @@ -20,7 +20,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" - "github.com/go-errors/errors" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/clock" @@ -189,7 +188,7 @@ func createTestNode() (*models.LightningNode, error) { priv, err := btcec.NewPrivateKey() if err != nil { - return nil, errors.Errorf("unable create private key: %v", err) + return nil, fmt.Errorf("unable create private key: %w", err) } pub := priv.PubKey().SerializeCompressed() diff --git a/server.go b/server.go index 18e23b224..5f3908d5a 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "context" "crypto/rand" "encoding/hex" + "errors" "fmt" "math/big" prand "math/rand" @@ -23,7 +24,6 @@ import ( "github.com/btcsuite/btcd/connmgr" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/go-errors/errors" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/aliasmgr" "github.com/lightningnetwork/lnd/autopilot" diff --git a/shachain/element_test.go b/shachain/element_test.go index df2b75593..3abdf7b90 100644 --- a/shachain/element_test.go +++ b/shachain/element_test.go @@ -1,10 +1,10 @@ package shachain import ( + "errors" "reflect" "testing" - "github.com/go-errors/errors" "github.com/stretchr/testify/require" ) diff --git a/shachain/store.go b/shachain/store.go index 252abdfeb..8582e62cc 100644 --- a/shachain/store.go +++ b/shachain/store.go @@ -2,10 +2,11 @@ package shachain import ( "encoding/binary" + "errors" + "fmt" "io" "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/go-errors/errors" ) // Store is an interface which serves as an abstraction over data structure @@ -117,7 +118,7 @@ func (store *RevocationStore) LookUp(v uint64) (*chainhash.Hash, error) { return &element.hash, nil } - return nil, errors.Errorf("unable to derive hash #%v", ind) + return nil, fmt.Errorf("unable to derive hash #%v", ind) } // AddNextEntry attempts to store the given hash within its internal storage in From 0862ba3f414bc45fd7221f5769575d8693bed36f Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Sun, 29 Jun 2025 17:21:13 +0200 Subject: [PATCH 2/2] server: ensure newer node announcement timestamp On startup, we currently blindly overwrite our node announcement info for our source node. This includes overwriting the existing LastUpdate timestamp. This can cause an issue with our new SQL backends in the case that the new timestamp is not greater than the previously written timestamp. So here, we first fetch the source node and make sure to use a timestamp that is greater than the previosly set one. --- server.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 5f3908d5a..7e35c33ec 100644 --- a/server.go +++ b/server.go @@ -950,9 +950,35 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr, if err != nil { return nil, err } + + // TODO(elle): All previously persisted node announcement fields (ie, + // not just LastUpdate) should be consulted here to ensure that we + // aren't overwriting any fields that may have been set during the + // last run of lnd. + nodeLastUpdate := time.Now() + srcNode, err := dbs.GraphDB.SourceNode(ctx) + switch { + // If we have a source node persisted in the DB already, then we just + // need to make sure that the new LastUpdate time is at least one + // second after the last update time. + case err == nil: + if srcNode.LastUpdate.Second() >= nodeLastUpdate.Second() { + nodeLastUpdate = srcNode.LastUpdate.Add(time.Second) + } + + // If we don't have a source node persisted in the DB, then we'll + // create a new one with the current time as the LastUpdate. + case errors.Is(err, graphdb.ErrSourceNodeNotSet): + + // If the above cases are not matched, then we have an unhandled non + // nil error. + default: + return nil, fmt.Errorf("unable to fetch source node: %w", err) + } + selfNode := &models.LightningNode{ HaveNodeAnnouncement: true, - LastUpdate: time.Now(), + LastUpdate: nodeLastUpdate, Addresses: selfAddrs, Alias: nodeAlias.String(), Features: s.featureMgr.Get(feature.SetNodeAnn),