2017-03-19 21:40:25 +03:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2017-03-27 20:25:44 +03:00
|
|
|
"bytes"
|
2018-09-05 07:45:57 +03:00
|
|
|
"errors"
|
2017-08-22 09:40:02 +03:00
|
|
|
"fmt"
|
2017-03-19 21:40:25 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-07-18 02:43:06 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2019-04-17 23:25:56 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2017-04-01 15:33:17 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-06-08 06:11:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2017-08-22 09:40:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-01-23 18:26:15 +03:00
|
|
|
"github.com/lightningnetwork/lnd/multimutex"
|
2020-03-18 02:23:01 +03:00
|
|
|
"github.com/lightningnetwork/lnd/netann"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-03-23 05:56:33 +03:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2017-03-19 21:40:25 +03:00
|
|
|
)
|
|
|
|
|
2017-11-18 06:21:50 +03:00
|
|
|
var (
|
2018-08-20 15:28:09 +03:00
|
|
|
// ErrGossiperShuttingDown is an error that is returned if the gossiper
|
|
|
|
// is in the process of being shut down.
|
|
|
|
ErrGossiperShuttingDown = errors.New("gossiper is shutting down")
|
2018-09-05 03:31:57 +03:00
|
|
|
|
|
|
|
// ErrGossipSyncerNotFound signals that we were unable to find an active
|
|
|
|
// gossip syncer corresponding to a gossip query message received from
|
|
|
|
// the remote peer.
|
|
|
|
ErrGossipSyncerNotFound = errors.New("gossip syncer not found")
|
2017-11-18 06:21:50 +03:00
|
|
|
)
|
|
|
|
|
2019-04-17 23:25:56 +03:00
|
|
|
// optionalMsgFields is a set of optional message fields that external callers
|
|
|
|
// can provide that serve useful when processing a specific network
|
|
|
|
// announcement.
|
|
|
|
type optionalMsgFields struct {
|
|
|
|
capacity *btcutil.Amount
|
|
|
|
channelPoint *wire.OutPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply applies the optional fields within the functional options.
|
|
|
|
func (f *optionalMsgFields) apply(optionalMsgFields ...OptionalMsgField) {
|
|
|
|
for _, optionalMsgField := range optionalMsgFields {
|
|
|
|
optionalMsgField(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionalMsgField is a functional option parameter that can be used to provide
|
|
|
|
// external information that is not included within a network message but serves
|
|
|
|
// useful when processing it.
|
|
|
|
type OptionalMsgField func(*optionalMsgFields)
|
|
|
|
|
|
|
|
// ChannelCapacity is an optional field that lets the gossiper know of the
|
|
|
|
// capacity of a channel.
|
|
|
|
func ChannelCapacity(capacity btcutil.Amount) OptionalMsgField {
|
|
|
|
return func(f *optionalMsgFields) {
|
|
|
|
f.capacity = &capacity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelPoint is an optional field that lets the gossiper know of the outpoint
|
|
|
|
// of a channel.
|
|
|
|
func ChannelPoint(op wire.OutPoint) OptionalMsgField {
|
|
|
|
return func(f *optionalMsgFields) {
|
|
|
|
f.channelPoint = &op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// networkMsg couples a routing related wire message with the peer that
|
|
|
|
// originally sent it.
|
|
|
|
type networkMsg struct {
|
2019-04-17 23:25:56 +03:00
|
|
|
peer lnpeer.Peer
|
|
|
|
source *btcec.PublicKey
|
|
|
|
msg lnwire.Message
|
|
|
|
optionalMsgFields *optionalMsgFields
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
isRemote bool
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
err chan error
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:56:11 +03:00
|
|
|
// chanPolicyUpdateRequest is a request that is sent to the server when a caller
|
2019-09-19 12:02:46 +03:00
|
|
|
// wishes to update a particular set of channels. New ChannelUpdate messages
|
|
|
|
// will be crafted to be sent out during the next broadcast epoch and the fee
|
|
|
|
// updates committed to the lower layer.
|
2017-12-15 23:56:11 +03:00
|
|
|
type chanPolicyUpdateRequest struct {
|
2019-09-19 12:02:46 +03:00
|
|
|
edgesToUpdate []EdgeWithInfo
|
|
|
|
errChan chan error
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:13:38 +03:00
|
|
|
// PinnedSyncers is a set of node pubkeys for which we will maintain an active
|
|
|
|
// syncer at all times.
|
|
|
|
type PinnedSyncers map[route.Vertex]struct{}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// Config defines the configuration for the service. ALL elements within the
|
|
|
|
// configuration MUST be non-nil for the service to carry out its duties.
|
|
|
|
type Config struct {
|
2017-09-04 02:41:01 +03:00
|
|
|
// ChainHash is a hash that indicates which resident chain of the
|
|
|
|
// AuthenticatedGossiper. Any announcements that don't match this
|
|
|
|
// chain hash will be ignored.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): eventually make into map so can de-multiplex
|
|
|
|
// incoming announcements
|
|
|
|
// * also need to do same for Notifier
|
|
|
|
ChainHash chainhash.Hash
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// Router is the subsystem which is responsible for managing the
|
2017-04-01 15:33:17 +03:00
|
|
|
// topology of lightning network. After incoming channel, node, channel
|
|
|
|
// updates announcements are validated they are sent to the router in
|
|
|
|
// order to be included in the LN graph.
|
2017-03-19 21:40:25 +03:00
|
|
|
Router routing.ChannelGraphSource
|
|
|
|
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// ChanSeries is an interfaces that provides access to a time series
|
2019-03-23 05:54:46 +03:00
|
|
|
// view of the current known channel graph. Each GossipSyncer enabled
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// peer will utilize this in order to create and respond to channel
|
|
|
|
// graph time series queries.
|
|
|
|
ChanSeries ChannelGraphTimeSeries
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// Notifier is used for receiving notifications of incoming blocks.
|
|
|
|
// With each new incoming block found we process previously premature
|
|
|
|
// announcements.
|
2017-04-01 15:33:17 +03:00
|
|
|
//
|
2017-03-19 21:40:25 +03:00
|
|
|
// TODO(roasbeef): could possibly just replace this with an epoch
|
|
|
|
// channel.
|
|
|
|
Notifier chainntnfs.ChainNotifier
|
|
|
|
|
|
|
|
// Broadcast broadcasts a particular set of announcements to all peers
|
|
|
|
// that the daemon is connected to. If supplied, the exclude parameter
|
2017-04-01 15:33:17 +03:00
|
|
|
// indicates that the target peer should be excluded from the
|
|
|
|
// broadcast.
|
2019-04-05 18:36:11 +03:00
|
|
|
Broadcast func(skips map[route.Vertex]struct{},
|
2017-12-26 18:26:28 +03:00
|
|
|
msg ...lnwire.Message) error
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2017-11-18 06:21:50 +03:00
|
|
|
// NotifyWhenOnline is a function that allows the gossiper to be
|
|
|
|
// notified when a certain peer comes online, allowing it to
|
|
|
|
// retry sending a peer message.
|
2018-07-06 04:19:35 +03:00
|
|
|
//
|
|
|
|
// NOTE: The peerChan channel must be buffered.
|
2019-05-31 00:37:30 +03:00
|
|
|
NotifyWhenOnline func(peerPubKey [33]byte, peerChan chan<- lnpeer.Peer)
|
2017-11-18 06:21:50 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// NotifyWhenOffline is a function that allows the gossiper to be
|
|
|
|
// notified when a certain peer disconnects, allowing it to request a
|
|
|
|
// notification for when it reconnects.
|
|
|
|
NotifyWhenOffline func(peerPubKey [33]byte) <-chan struct{}
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
// SelfNodeAnnouncement is a function that fetches our own current node
|
|
|
|
// announcement, for use when determining whether we should update our
|
|
|
|
// peers about our presence on the network. If the refresh is true, a
|
|
|
|
// new and updated announcement will be returned.
|
|
|
|
SelfNodeAnnouncement func(refresh bool) (lnwire.NodeAnnouncement, error)
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// ProofMatureDelta the number of confirmations which is needed before
|
|
|
|
// exchange the channel announcement proofs.
|
2017-03-28 22:08:14 +03:00
|
|
|
ProofMatureDelta uint32
|
|
|
|
|
2017-09-14 02:33:46 +03:00
|
|
|
// TrickleDelay the period of trickle timer which flushes to the
|
2017-03-28 22:08:14 +03:00
|
|
|
// network the pending batch of new announcements we've received since
|
|
|
|
// the last trickle tick.
|
|
|
|
TrickleDelay time.Duration
|
2017-05-05 20:17:31 +03:00
|
|
|
|
2019-09-10 15:37:17 +03:00
|
|
|
// RetransmitTicker is a ticker that ticks with a period which
|
|
|
|
// indicates that we should check if we need re-broadcast any of our
|
|
|
|
// personal channels.
|
|
|
|
RetransmitTicker ticker.Ticker
|
2017-09-25 04:47:48 +03:00
|
|
|
|
2019-09-10 15:37:17 +03:00
|
|
|
// RebroadcastInterval is the maximum time we wait between sending out
|
|
|
|
// channel updates for our active channels and our own node
|
|
|
|
// announcement. We do this to ensure our active presence on the
|
|
|
|
// network is known, and we are not being considered a zombie node or
|
|
|
|
// having zombie channels.
|
|
|
|
RebroadcastInterval time.Duration
|
|
|
|
|
2019-02-06 04:18:34 +03:00
|
|
|
// WaitingProofStore is a persistent storage of partial channel proof
|
|
|
|
// announcement messages. We use it to buffer half of the material
|
|
|
|
// needed to reconstruct a full authenticated channel announcement.
|
|
|
|
// Once we receive the other half the channel proof, we'll be able to
|
|
|
|
// properly validate it and re-broadcast it out to the network.
|
|
|
|
//
|
|
|
|
// TODO(wilmer): make interface to prevent channeldb dependency.
|
|
|
|
WaitingProofStore *channeldb.WaitingProofStore
|
2017-05-05 20:17:31 +03:00
|
|
|
|
2019-02-06 04:18:27 +03:00
|
|
|
// MessageStore is a persistent storage of gossip messages which we will
|
|
|
|
// use to determine which messages need to be resent for a given peer.
|
|
|
|
MessageStore GossipMessageStore
|
|
|
|
|
2017-08-22 09:40:02 +03:00
|
|
|
// AnnSigner is an instance of the MessageSigner interface which will
|
|
|
|
// be used to manually sign any outgoing channel updates. The signer
|
|
|
|
// implementation should be backed by the public key of the backing
|
|
|
|
// Lightning node.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): extract ann crafting + sign from fundingMgr into
|
|
|
|
// here?
|
|
|
|
AnnSigner lnwallet.MessageSigner
|
2019-03-23 05:56:33 +03:00
|
|
|
|
|
|
|
// NumActiveSyncers is the number of peers for which we should have
|
|
|
|
// active syncers with. After reaching NumActiveSyncers, any future
|
|
|
|
// gossip syncers will be passive.
|
|
|
|
NumActiveSyncers int
|
|
|
|
|
|
|
|
// RotateTicker is a ticker responsible for notifying the SyncManager
|
|
|
|
// when it should rotate its active syncers. A single active syncer with
|
|
|
|
// a chansSynced state will be exchanged for a passive syncer in order
|
|
|
|
// to ensure we don't keep syncing with the same peers.
|
|
|
|
RotateTicker ticker.Ticker
|
|
|
|
|
|
|
|
// HistoricalSyncTicker is a ticker responsible for notifying the
|
|
|
|
// syncManager when it should attempt a historical sync with a gossip
|
|
|
|
// sync peer.
|
|
|
|
HistoricalSyncTicker ticker.Ticker
|
|
|
|
|
|
|
|
// ActiveSyncerTimeoutTicker is a ticker responsible for notifying the
|
|
|
|
// syncManager when it should attempt to start the next pending
|
|
|
|
// activeSyncer due to the current one not completing its state machine
|
|
|
|
// within the timeout.
|
|
|
|
ActiveSyncerTimeoutTicker ticker.Ticker
|
2019-04-30 10:28:02 +03:00
|
|
|
|
|
|
|
// MinimumBatchSize is minimum size of a sub batch of announcement
|
|
|
|
// messages.
|
|
|
|
MinimumBatchSize int
|
|
|
|
|
|
|
|
// SubBatchDelay is the delay between sending sub batches of
|
|
|
|
// gossip messages.
|
|
|
|
SubBatchDelay time.Duration
|
2019-07-31 03:25:47 +03:00
|
|
|
|
|
|
|
// IgnoreHistoricalFilters will prevent syncers from replying with
|
|
|
|
// historical data when the remote peer sets a gossip_timestamp_range.
|
|
|
|
// This prevents ranges with old start times from causing us to dump the
|
|
|
|
// graph on connect.
|
|
|
|
IgnoreHistoricalFilters bool
|
2020-11-30 03:13:40 +03:00
|
|
|
|
|
|
|
// GossipUpdateThrottle if true, then the gossiper will throttle
|
|
|
|
// gossip updates to once per RebroadcastInterval for any keep-alive
|
|
|
|
// updates, and once per block for other types of updates.
|
|
|
|
GossipUpdateThrottle bool
|
2021-01-29 11:14:21 +03:00
|
|
|
|
|
|
|
// PinnedSyncers is a set of peers that will always transition to
|
|
|
|
// ActiveSync upon connection. These peers will never transition to
|
|
|
|
// PassiveSync.
|
|
|
|
PinnedSyncers PinnedSyncers
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// AuthenticatedGossiper is a subsystem which is responsible for receiving
|
2017-09-08 04:25:43 +03:00
|
|
|
// announcements, validating them and applying the changes to router, syncing
|
2017-04-01 15:33:17 +03:00
|
|
|
// lightning network with newly connected nodes, broadcasting announcements
|
|
|
|
// after validation, negotiating the channel announcement proofs exchange and
|
|
|
|
// handling the premature announcements. All outgoing announcements are
|
|
|
|
// expected to be properly signed as dictated in BOLT#7, additionally, all
|
|
|
|
// incoming message are expected to be well formed and signed. Invalid messages
|
|
|
|
// will be rejected by this struct.
|
|
|
|
type AuthenticatedGossiper struct {
|
|
|
|
// Parameters which are needed to properly handle the start and stop of
|
2019-06-06 20:15:11 +03:00
|
|
|
// the service.
|
|
|
|
started sync.Once
|
|
|
|
stopped sync.Once
|
2018-06-01 01:41:41 +03:00
|
|
|
|
|
|
|
// bestHeight is the height of the block at the tip of the main chain
|
2020-04-16 13:20:56 +03:00
|
|
|
// as we know it. Accesses *MUST* be done with the gossiper's lock
|
|
|
|
// held.
|
2018-06-01 01:41:41 +03:00
|
|
|
bestHeight uint32
|
|
|
|
|
2018-06-08 06:11:27 +03:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2017-05-05 20:17:31 +03:00
|
|
|
// cfg is a copy of the configuration struct that the gossiper service
|
2017-03-19 21:40:25 +03:00
|
|
|
// was initialized with.
|
|
|
|
cfg *Config
|
|
|
|
|
2018-06-12 03:37:17 +03:00
|
|
|
// blockEpochs encapsulates a stream of block epochs that are sent at
|
|
|
|
// every new block height.
|
|
|
|
blockEpochs *chainntnfs.BlockEpochEvent
|
2017-03-19 21:40:25 +03:00
|
|
|
|
|
|
|
// networkMsgs is a channel that carries new network broadcasted
|
2017-05-05 20:17:31 +03:00
|
|
|
// message from outside the gossiper service to be processed by the
|
2017-03-19 21:40:25 +03:00
|
|
|
// networkHandler.
|
|
|
|
networkMsgs chan *networkMsg
|
|
|
|
|
2018-01-31 07:23:14 +03:00
|
|
|
// chanPolicyUpdates is a channel that requests to update the
|
|
|
|
// forwarding policy of a set of channels is sent over.
|
2017-12-15 23:56:11 +03:00
|
|
|
chanPolicyUpdates chan *chanPolicyUpdateRequest
|
2017-08-22 09:40:02 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// selfKey is the identity public key of the backing Lightning node.
|
2017-08-22 09:40:02 +03:00
|
|
|
selfKey *btcec.PublicKey
|
2017-11-30 03:45:08 +03:00
|
|
|
|
2018-01-23 18:26:15 +03:00
|
|
|
// channelMtx is used to restrict the database access to one
|
|
|
|
// goroutine per channel ID. This is done to ensure that when
|
|
|
|
// the gossiper is handling an announcement, the db state stays
|
2018-02-07 06:13:07 +03:00
|
|
|
// consistent between when the DB is first read until it's written.
|
2018-01-23 18:26:15 +03:00
|
|
|
channelMtx *multimutex.Mutex
|
|
|
|
|
2018-01-31 07:41:27 +03:00
|
|
|
rejectMtx sync.RWMutex
|
|
|
|
recentRejects map[uint64]struct{}
|
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
// syncMgr is a subsystem responsible for managing the gossip syncers
|
|
|
|
// for peers currently connected. When a new peer is connected, the
|
|
|
|
// manager will create its accompanying gossip syncer and determine
|
|
|
|
// whether it should have an activeSync or passiveSync sync type based
|
|
|
|
// on how many other gossip syncers are currently active. Any activeSync
|
|
|
|
// gossip syncers are started in a round-robin manner to ensure we're
|
|
|
|
// not syncing with multiple peers at the same time.
|
|
|
|
syncMgr *SyncManager
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// reliableSender is a subsystem responsible for handling reliable
|
2019-03-13 23:36:21 +03:00
|
|
|
// message send requests to peers. This should only be used for channels
|
|
|
|
// that are unadvertised at the time of handling the message since if it
|
|
|
|
// is advertised, then peers should be able to get the message from the
|
|
|
|
// network.
|
2019-02-06 04:18:56 +03:00
|
|
|
reliableSender *reliableSender
|
|
|
|
|
2020-11-20 04:31:58 +03:00
|
|
|
// heightForLastChanUpdate keeps track of the height at which we
|
|
|
|
// processed the latest channel update for a specific direction.
|
|
|
|
//
|
|
|
|
// NOTE: This map must be synchronized with the main
|
|
|
|
// AuthenticatedGossiper lock.
|
|
|
|
heightForLastChanUpdate map[uint64][2]uint32
|
|
|
|
|
2017-11-30 03:45:08 +03:00
|
|
|
sync.Mutex
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new AuthenticatedGossiper instance, initialized with the
|
|
|
|
// passed configuration parameters.
|
2019-02-06 04:18:34 +03:00
|
|
|
func New(cfg Config, selfKey *btcec.PublicKey) *AuthenticatedGossiper {
|
2019-02-06 04:18:56 +03:00
|
|
|
gossiper := &AuthenticatedGossiper{
|
2017-12-14 19:52:41 +03:00
|
|
|
selfKey: selfKey,
|
|
|
|
cfg: &cfg,
|
|
|
|
networkMsgs: make(chan *networkMsg),
|
|
|
|
quit: make(chan struct{}),
|
2017-12-15 23:56:11 +03:00
|
|
|
chanPolicyUpdates: make(chan *chanPolicyUpdateRequest),
|
2018-01-23 18:26:15 +03:00
|
|
|
channelMtx: multimutex.NewMutex(),
|
2018-01-31 07:41:27 +03:00
|
|
|
recentRejects: make(map[uint64]struct{}),
|
2020-11-20 04:31:58 +03:00
|
|
|
heightForLastChanUpdate: make(map[uint64][2]uint32),
|
2019-02-06 04:18:34 +03:00
|
|
|
}
|
2019-02-06 04:18:56 +03:00
|
|
|
|
2020-12-03 02:15:35 +03:00
|
|
|
gossiper.syncMgr = newSyncManager(&SyncManagerCfg{
|
|
|
|
ChainHash: cfg.ChainHash,
|
|
|
|
ChanSeries: cfg.ChanSeries,
|
|
|
|
RotateTicker: cfg.RotateTicker,
|
|
|
|
HistoricalSyncTicker: cfg.HistoricalSyncTicker,
|
|
|
|
NumActiveSyncers: cfg.NumActiveSyncers,
|
|
|
|
IgnoreHistoricalFilters: cfg.IgnoreHistoricalFilters,
|
|
|
|
BestHeight: gossiper.latestHeight,
|
2021-01-29 11:14:21 +03:00
|
|
|
PinnedSyncers: cfg.PinnedSyncers,
|
2020-12-03 02:15:35 +03:00
|
|
|
})
|
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
gossiper.reliableSender = newReliableSender(&reliableSenderCfg{
|
|
|
|
NotifyWhenOnline: cfg.NotifyWhenOnline,
|
|
|
|
NotifyWhenOffline: cfg.NotifyWhenOffline,
|
|
|
|
MessageStore: cfg.MessageStore,
|
|
|
|
IsMsgStale: gossiper.isMsgStale,
|
|
|
|
})
|
|
|
|
|
|
|
|
return gossiper
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 12:02:46 +03:00
|
|
|
// EdgeWithInfo contains the information that is required to update an edge.
|
|
|
|
type EdgeWithInfo struct {
|
|
|
|
// Info describes the channel.
|
|
|
|
Info *channeldb.ChannelEdgeInfo
|
|
|
|
|
|
|
|
// Edge describes the policy in one direction of the channel.
|
|
|
|
Edge *channeldb.ChannelEdgePolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropagateChanPolicyUpdate signals the AuthenticatedGossiper to perform the
|
|
|
|
// specified edge updates. Updates are done in two stages: first, the
|
2018-02-07 06:11:11 +03:00
|
|
|
// AuthenticatedGossiper ensures the update has been committed by dependent
|
2019-08-30 03:54:08 +03:00
|
|
|
// sub-systems, then it signs and broadcasts new updates to the network. A
|
|
|
|
// mapping between outpoints and updated channel policies is returned, which is
|
|
|
|
// used to update the forwarding policies of the underlying links.
|
2017-12-15 23:56:11 +03:00
|
|
|
func (d *AuthenticatedGossiper) PropagateChanPolicyUpdate(
|
2019-09-19 12:02:46 +03:00
|
|
|
edgesToUpdate []EdgeWithInfo) error {
|
2017-08-22 09:40:02 +03:00
|
|
|
|
2019-09-19 12:02:46 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-12-15 23:56:11 +03:00
|
|
|
policyUpdate := &chanPolicyUpdateRequest{
|
2019-09-19 12:02:46 +03:00
|
|
|
edgesToUpdate: edgesToUpdate,
|
|
|
|
errChan: errChan,
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-12-15 23:56:11 +03:00
|
|
|
case d.chanPolicyUpdates <- policyUpdate:
|
2019-09-19 12:02:46 +03:00
|
|
|
err := <-errChan
|
|
|
|
return err
|
2017-08-22 09:40:02 +03:00
|
|
|
case <-d.quit:
|
2019-09-19 12:02:46 +03:00
|
|
|
return fmt.Errorf("AuthenticatedGossiper shutting down")
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// Start spawns network messages handler goroutine and registers on new block
|
|
|
|
// notifications in order to properly handle the premature announcements.
|
2017-04-01 15:33:17 +03:00
|
|
|
func (d *AuthenticatedGossiper) Start() error {
|
2019-06-06 20:15:11 +03:00
|
|
|
var err error
|
|
|
|
d.started.Do(func() {
|
|
|
|
err = d.start()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2019-06-06 20:15:11 +03:00
|
|
|
func (d *AuthenticatedGossiper) start() error {
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Info("Authenticated Gossiper is starting")
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// First we register for new notifications of newly discovered blocks.
|
|
|
|
// We do this immediately so we'll later be able to consume any/all
|
|
|
|
// blocks which were discovered.
|
2018-08-09 10:05:27 +03:00
|
|
|
blockEpochs, err := d.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
2017-03-19 21:40:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-12 03:37:17 +03:00
|
|
|
d.blockEpochs = blockEpochs
|
2017-03-19 21:40:25 +03:00
|
|
|
|
|
|
|
height, err := d.cfg.Router.CurrentBlockHeight()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.bestHeight = height
|
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// Start the reliable sender. In case we had any pending messages ready
|
|
|
|
// to be sent when the gossiper was last shut down, we must continue on
|
|
|
|
// our quest to deliver them to their respective peers.
|
|
|
|
if err := d.reliableSender.Start(); err != nil {
|
2017-11-18 06:21:50 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
d.syncMgr.Start()
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
d.wg.Add(1)
|
|
|
|
go d.networkHandler()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals any active goroutines for a graceful closure.
|
2017-04-01 15:33:17 +03:00
|
|
|
func (d *AuthenticatedGossiper) Stop() {
|
2019-06-06 20:15:11 +03:00
|
|
|
d.stopped.Do(d.stop)
|
|
|
|
}
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2019-06-06 20:15:11 +03:00
|
|
|
func (d *AuthenticatedGossiper) stop() {
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Info("Authenticated Gossiper is stopping")
|
|
|
|
|
2018-06-12 03:37:17 +03:00
|
|
|
d.blockEpochs.Cancel()
|
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
d.syncMgr.Stop()
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
close(d.quit)
|
|
|
|
d.wg.Wait()
|
2019-02-06 04:18:56 +03:00
|
|
|
|
|
|
|
// We'll stop our reliable sender after all of the gossiper's goroutines
|
|
|
|
// have exited to ensure nothing can cause it to continue executing.
|
|
|
|
d.reliableSender.Stop()
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:09:11 +03:00
|
|
|
// TODO(roasbeef): need method to get current gossip timestamp?
|
|
|
|
// * using mtx, check time rotate forward is needed?
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// ProcessRemoteAnnouncement sends a new remote announcement message along with
|
|
|
|
// the peer that sent the routing message. The announcement will be processed
|
|
|
|
// then added to a queue for batched trickled announcement to all connected
|
|
|
|
// peers. Remote channel announcements should contain the announcement proof
|
|
|
|
// and be fully validated.
|
|
|
|
func (d *AuthenticatedGossiper) ProcessRemoteAnnouncement(msg lnwire.Message,
|
2018-06-08 06:11:27 +03:00
|
|
|
peer lnpeer.Peer) chan error {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2018-11-02 03:13:13 +03:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
// For messages in the known set of channel series queries, we'll
|
2019-03-23 05:54:46 +03:00
|
|
|
// dispatch the message directly to the GossipSyncer, and skip the main
|
2018-11-02 03:13:13 +03:00
|
|
|
// processing loop.
|
|
|
|
switch m := msg.(type) {
|
|
|
|
case *lnwire.QueryShortChanIDs,
|
|
|
|
*lnwire.QueryChannelRange,
|
|
|
|
*lnwire.ReplyChannelRange,
|
|
|
|
*lnwire.ReplyShortChanIDsEnd:
|
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
syncer, ok := d.syncMgr.GossipSyncer(peer.PubKey())
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("Gossip syncer for peer=%x not found",
|
|
|
|
peer.PubKey())
|
2018-11-02 03:13:13 +03:00
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
errChan <- ErrGossipSyncerNotFound
|
2018-11-02 03:13:13 +03:00
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found the message target, then we'll dispatch the
|
|
|
|
// message directly to it.
|
2018-11-02 03:35:55 +03:00
|
|
|
syncer.ProcessQueryMsg(m, peer.QuitSignal())
|
2018-11-02 03:13:13 +03:00
|
|
|
|
|
|
|
errChan <- nil
|
|
|
|
return errChan
|
|
|
|
|
|
|
|
// If a peer is updating its current update horizon, then we'll dispatch
|
2019-03-23 05:54:46 +03:00
|
|
|
// that directly to the proper GossipSyncer.
|
2018-11-02 03:13:13 +03:00
|
|
|
case *lnwire.GossipTimestampRange:
|
2019-03-23 05:56:33 +03:00
|
|
|
syncer, ok := d.syncMgr.GossipSyncer(peer.PubKey())
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("Gossip syncer for peer=%x not found",
|
|
|
|
peer.PubKey())
|
2018-11-02 03:13:13 +03:00
|
|
|
|
2019-03-23 05:56:33 +03:00
|
|
|
errChan <- ErrGossipSyncerNotFound
|
2018-11-02 03:13:13 +03:00
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found the message target, then we'll dispatch the
|
|
|
|
// message directly to it.
|
2019-03-23 05:56:33 +03:00
|
|
|
if err := syncer.ApplyGossipFilter(m); err != nil {
|
|
|
|
log.Warnf("Unable to apply gossip filter for peer=%x: "+
|
|
|
|
"%v", peer.PubKey(), err)
|
2018-11-02 03:13:13 +03:00
|
|
|
|
|
|
|
errChan <- err
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan <- nil
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
nMsg := &networkMsg{
|
|
|
|
msg: msg,
|
|
|
|
isRemote: true,
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: peer,
|
|
|
|
source: peer.IdentityKey(),
|
2018-11-02 03:13:13 +03:00
|
|
|
err: errChan,
|
2017-04-01 15:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case d.networkMsgs <- nMsg:
|
2018-08-26 03:16:03 +03:00
|
|
|
|
|
|
|
// If the peer that sent us this error is quitting, then we don't need
|
|
|
|
// to send back an error and can return immediately.
|
|
|
|
case <-peer.QuitSignal():
|
|
|
|
return nil
|
2017-04-01 15:33:17 +03:00
|
|
|
case <-d.quit:
|
2018-08-20 15:28:09 +03:00
|
|
|
nMsg.err <- ErrGossiperShuttingDown
|
2017-04-01 15:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nMsg.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessLocalAnnouncement sends a new remote announcement message along with
|
|
|
|
// the peer that sent the routing message. The announcement will be processed
|
|
|
|
// then added to a queue for batched trickled announcement to all connected
|
|
|
|
// peers. Local channel announcements don't contain the announcement proof and
|
|
|
|
// will not be fully validated. Once the channel proofs are received, the
|
|
|
|
// entire channel announcement and update messages will be re-constructed and
|
|
|
|
// broadcast to the rest of the network.
|
|
|
|
func (d *AuthenticatedGossiper) ProcessLocalAnnouncement(msg lnwire.Message,
|
2019-04-17 23:25:56 +03:00
|
|
|
source *btcec.PublicKey, optionalFields ...OptionalMsgField) chan error {
|
|
|
|
|
|
|
|
optionalMsgFields := &optionalMsgFields{}
|
|
|
|
optionalMsgFields.apply(optionalFields...)
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
nMsg := &networkMsg{
|
2019-04-17 23:25:56 +03:00
|
|
|
msg: msg,
|
|
|
|
optionalMsgFields: optionalMsgFields,
|
|
|
|
isRemote: false,
|
|
|
|
source: source,
|
|
|
|
err: make(chan error, 1),
|
2017-04-01 15:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case d.networkMsgs <- nMsg:
|
|
|
|
case <-d.quit:
|
2018-08-20 15:28:09 +03:00
|
|
|
nMsg.err <- ErrGossiperShuttingDown
|
2017-04-01 15:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nMsg.err
|
|
|
|
}
|
|
|
|
|
2017-09-14 02:33:46 +03:00
|
|
|
// channelUpdateID is a unique identifier for ChannelUpdate messages, as
|
2019-01-12 20:59:43 +03:00
|
|
|
// channel updates can be identified by the (ShortChannelID, ChannelFlags)
|
2017-09-08 04:25:43 +03:00
|
|
|
// tuple.
|
2017-09-14 02:33:46 +03:00
|
|
|
type channelUpdateID struct {
|
2017-09-08 04:25:43 +03:00
|
|
|
// channelID represents the set of data which is needed to
|
|
|
|
// retrieve all necessary data to validate the channel existence.
|
|
|
|
channelID lnwire.ShortChannelID
|
|
|
|
|
|
|
|
// Flags least-significant bit must be set to 0 if the creating node
|
|
|
|
// corresponds to the first node in the previously sent channel
|
|
|
|
// announcement and 1 otherwise.
|
2019-01-12 20:59:43 +03:00
|
|
|
flags lnwire.ChanUpdateChanFlags
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-12-26 18:18:56 +03:00
|
|
|
// msgWithSenders is a wrapper struct around a message, and the set of peers
|
2018-01-08 20:16:36 +03:00
|
|
|
// that originally sent us this message. Using this struct, we can ensure that
|
2017-12-26 18:18:56 +03:00
|
|
|
// we don't re-send a message to the peer that sent it to us in the first
|
|
|
|
// place.
|
|
|
|
type msgWithSenders struct {
|
|
|
|
// msg is the wire message itself.
|
|
|
|
msg lnwire.Message
|
|
|
|
|
|
|
|
// sender is the set of peers that sent us this message.
|
2019-04-05 18:36:11 +03:00
|
|
|
senders map[route.Vertex]struct{}
|
2017-12-26 18:18:56 +03:00
|
|
|
}
|
|
|
|
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// mergeSyncerMap is used to merge the set of senders of a particular message
|
2019-03-23 05:54:46 +03:00
|
|
|
// with peers that we have an active GossipSyncer with. We do this to ensure
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// that we don't broadcast messages to any peers that we have active gossip
|
|
|
|
// syncers for.
|
2019-04-05 18:36:11 +03:00
|
|
|
func (m *msgWithSenders) mergeSyncerMap(syncers map[route.Vertex]*GossipSyncer) {
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
for peerPub := range syncers {
|
|
|
|
m.senders[peerPub] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:21:08 +03:00
|
|
|
// deDupedAnnouncements de-duplicates announcements that have been added to the
|
|
|
|
// batch. Internally, announcements are stored in three maps
|
2017-09-08 04:25:43 +03:00
|
|
|
// (one each for channel announcements, channel updates, and node
|
2017-11-30 03:21:08 +03:00
|
|
|
// announcements). These maps keep track of unique announcements and ensure no
|
2018-01-07 22:10:02 +03:00
|
|
|
// announcements are duplicated. We keep the three message types separate, such
|
|
|
|
// that we can send channel announcements first, then channel updates, and
|
|
|
|
// finally node announcements when it's time to broadcast them.
|
2017-09-08 04:25:43 +03:00
|
|
|
type deDupedAnnouncements struct {
|
|
|
|
// channelAnnouncements are identified by the short channel id field.
|
2017-12-26 18:18:56 +03:00
|
|
|
channelAnnouncements map[lnwire.ShortChannelID]msgWithSenders
|
2017-09-08 04:25:43 +03:00
|
|
|
|
|
|
|
// channelUpdates are identified by the channel update id field.
|
2017-12-26 18:18:56 +03:00
|
|
|
channelUpdates map[channelUpdateID]msgWithSenders
|
2017-09-08 04:25:43 +03:00
|
|
|
|
2017-09-14 02:33:46 +03:00
|
|
|
// nodeAnnouncements are identified by the Vertex field.
|
2019-04-05 18:36:11 +03:00
|
|
|
nodeAnnouncements map[route.Vertex]msgWithSenders
|
2017-11-30 03:21:08 +03:00
|
|
|
|
|
|
|
sync.Mutex
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-11-30 03:21:08 +03:00
|
|
|
// Reset operates on deDupedAnnouncements to reset the storage of
|
|
|
|
// announcements.
|
2017-09-08 04:25:43 +03:00
|
|
|
func (d *deDupedAnnouncements) Reset() {
|
2017-11-30 03:21:08 +03:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
d.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset is the private version of the Reset method. We have this so we can
|
|
|
|
// call this method within method that are already holding the lock.
|
|
|
|
func (d *deDupedAnnouncements) reset() {
|
2018-02-07 06:11:11 +03:00
|
|
|
// Storage of each type of announcement (channel announcements, channel
|
2017-09-08 04:25:43 +03:00
|
|
|
// updates, node announcements) is set to an empty map where the
|
2017-11-30 03:21:08 +03:00
|
|
|
// appropriate key points to the corresponding lnwire.Message.
|
2017-12-26 18:18:56 +03:00
|
|
|
d.channelAnnouncements = make(map[lnwire.ShortChannelID]msgWithSenders)
|
|
|
|
d.channelUpdates = make(map[channelUpdateID]msgWithSenders)
|
2019-04-05 18:36:11 +03:00
|
|
|
d.nodeAnnouncements = make(map[route.Vertex]msgWithSenders)
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-12-26 18:18:56 +03:00
|
|
|
// addMsg adds a new message to the current batch. If the message is already
|
2018-02-07 06:11:11 +03:00
|
|
|
// present in the current batch, then this new instance replaces the latter,
|
2017-12-26 18:18:56 +03:00
|
|
|
// and the set of senders is updated to reflect which node sent us this
|
|
|
|
// message.
|
|
|
|
func (d *deDupedAnnouncements) addMsg(message networkMsg) {
|
2017-11-30 03:21:08 +03:00
|
|
|
// Depending on the message type (channel announcement, channel update,
|
|
|
|
// or node announcement), the message is added to the corresponding map
|
|
|
|
// in deDupedAnnouncements. Because each identifying key can have at
|
|
|
|
// most one value, the announcements are de-duplicated, with newer ones
|
|
|
|
// replacing older ones.
|
2017-12-26 18:18:56 +03:00
|
|
|
switch msg := message.msg.(type) {
|
2017-11-30 03:21:08 +03:00
|
|
|
|
|
|
|
// Channel announcements are identified by the short channel id field.
|
2017-09-08 04:25:43 +03:00
|
|
|
case *lnwire.ChannelAnnouncement:
|
2017-12-26 18:18:56 +03:00
|
|
|
deDupKey := msg.ShortChannelID
|
2019-04-05 18:36:11 +03:00
|
|
|
sender := route.NewVertex(message.source)
|
2017-12-26 18:18:56 +03:00
|
|
|
|
|
|
|
mws, ok := d.channelAnnouncements[deDupKey]
|
|
|
|
if !ok {
|
|
|
|
mws = msgWithSenders{
|
|
|
|
msg: msg,
|
2019-04-05 18:36:11 +03:00
|
|
|
senders: make(map[route.Vertex]struct{}),
|
2017-12-26 18:18:56 +03:00
|
|
|
}
|
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
|
|
|
|
d.channelAnnouncements[deDupKey] = mws
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mws.msg = msg
|
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
d.channelAnnouncements[deDupKey] = mws
|
2017-11-30 03:21:08 +03:00
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// Channel updates are identified by the (short channel id,
|
|
|
|
// channelflags) tuple.
|
2017-09-08 04:25:43 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2019-04-05 18:36:11 +03:00
|
|
|
sender := route.NewVertex(message.source)
|
2017-12-26 18:18:56 +03:00
|
|
|
deDupKey := channelUpdateID{
|
2017-09-08 04:25:43 +03:00
|
|
|
msg.ShortChannelID,
|
2019-01-12 20:59:43 +03:00
|
|
|
msg.ChannelFlags,
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:02 +03:00
|
|
|
oldTimestamp := uint32(0)
|
2017-12-26 18:18:56 +03:00
|
|
|
mws, ok := d.channelUpdates[deDupKey]
|
2018-01-07 22:10:02 +03:00
|
|
|
if ok {
|
|
|
|
// If we already have seen this message, record its
|
|
|
|
// timestamp.
|
|
|
|
oldTimestamp = mws.msg.(*lnwire.ChannelUpdate).Timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already had this message with a strictly newer
|
|
|
|
// timestamp, then we'll just discard the message we got.
|
|
|
|
if oldTimestamp > msg.Timestamp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the message we just got is newer than what we previously
|
|
|
|
// have seen, or this is the first time we see it, then we'll
|
|
|
|
// add it to our map of announcements.
|
|
|
|
if oldTimestamp < msg.Timestamp {
|
2017-12-26 18:18:56 +03:00
|
|
|
mws = msgWithSenders{
|
|
|
|
msg: msg,
|
2019-04-05 18:36:11 +03:00
|
|
|
senders: make(map[route.Vertex]struct{}),
|
2017-12-26 18:18:56 +03:00
|
|
|
}
|
2018-01-07 22:10:02 +03:00
|
|
|
|
|
|
|
// We'll mark the sender of the message in the
|
|
|
|
// senders map.
|
2017-12-26 18:18:56 +03:00
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
|
|
|
|
d.channelUpdates[deDupKey] = mws
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:02 +03:00
|
|
|
// Lastly, if we had seen this exact message from before, with
|
|
|
|
// the same timestamp, we'll add the sender to the map of
|
|
|
|
// senders, such that we can skip sending this message back in
|
|
|
|
// the next batch.
|
2017-12-26 18:18:56 +03:00
|
|
|
mws.msg = msg
|
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
d.channelUpdates[deDupKey] = mws
|
2017-11-30 03:21:08 +03:00
|
|
|
|
|
|
|
// Node announcements are identified by the Vertex field. Use the
|
|
|
|
// NodeID to create the corresponding Vertex.
|
2017-09-08 04:25:43 +03:00
|
|
|
case *lnwire.NodeAnnouncement:
|
2019-04-05 18:36:11 +03:00
|
|
|
sender := route.NewVertex(message.source)
|
|
|
|
deDupKey := route.Vertex(msg.NodeID)
|
2017-12-26 18:18:56 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// We do the same for node announcements as we did for channel
|
2018-01-07 22:10:02 +03:00
|
|
|
// updates, as they also carry a timestamp.
|
|
|
|
oldTimestamp := uint32(0)
|
2017-12-26 18:18:56 +03:00
|
|
|
mws, ok := d.nodeAnnouncements[deDupKey]
|
2018-01-07 22:10:02 +03:00
|
|
|
if ok {
|
|
|
|
oldTimestamp = mws.msg.(*lnwire.NodeAnnouncement).Timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard the message if it's old.
|
|
|
|
if oldTimestamp > msg.Timestamp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace if it's newer.
|
|
|
|
if oldTimestamp < msg.Timestamp {
|
2017-12-26 18:18:56 +03:00
|
|
|
mws = msgWithSenders{
|
|
|
|
msg: msg,
|
2019-04-05 18:36:11 +03:00
|
|
|
senders: make(map[route.Vertex]struct{}),
|
2017-12-26 18:18:56 +03:00
|
|
|
}
|
2018-01-07 22:10:02 +03:00
|
|
|
|
2017-12-26 18:18:56 +03:00
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
|
|
|
|
d.nodeAnnouncements[deDupKey] = mws
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:02 +03:00
|
|
|
// Add to senders map if it's the same as we had.
|
2017-12-26 18:18:56 +03:00
|
|
|
mws.msg = msg
|
|
|
|
mws.senders[sender] = struct{}{}
|
|
|
|
d.nodeAnnouncements[deDupKey] = mws
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:21:08 +03:00
|
|
|
// AddMsgs is a helper method to add multiple messages to the announcement
|
|
|
|
// batch.
|
2017-12-26 18:18:56 +03:00
|
|
|
func (d *deDupedAnnouncements) AddMsgs(msgs ...networkMsg) {
|
2017-11-30 03:21:08 +03:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
for _, msg := range msgs {
|
2017-11-30 03:21:08 +03:00
|
|
|
d.addMsg(msg)
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:21:08 +03:00
|
|
|
// Emit returns the set of de-duplicated announcements to be sent out during
|
|
|
|
// the next announcement epoch, in the order of channel announcements, channel
|
2017-12-26 18:18:56 +03:00
|
|
|
// updates, and node announcements. Each message emitted, contains the set of
|
|
|
|
// peers that sent us the message. This way, we can ensure that we don't waste
|
|
|
|
// bandwidth by re-sending a message to the peer that sent it to us in the
|
|
|
|
// first place. Additionally, the set of stored messages are reset.
|
|
|
|
func (d *deDupedAnnouncements) Emit() []msgWithSenders {
|
2017-11-30 03:21:08 +03:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// Get the total number of announcements.
|
|
|
|
numAnnouncements := len(d.channelAnnouncements) + len(d.channelUpdates) +
|
|
|
|
len(d.nodeAnnouncements)
|
|
|
|
|
|
|
|
// Create an empty array of lnwire.Messages with a length equal to
|
|
|
|
// the total number of announcements.
|
2017-12-26 18:18:56 +03:00
|
|
|
msgs := make([]msgWithSenders, 0, numAnnouncements)
|
2017-09-08 04:25:43 +03:00
|
|
|
|
|
|
|
// Add the channel announcements to the array first.
|
|
|
|
for _, message := range d.channelAnnouncements {
|
2017-12-26 18:18:56 +03:00
|
|
|
msgs = append(msgs, message)
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then add the channel updates.
|
|
|
|
for _, message := range d.channelUpdates {
|
2017-12-26 18:18:56 +03:00
|
|
|
msgs = append(msgs, message)
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally add the node announcements.
|
|
|
|
for _, message := range d.nodeAnnouncements {
|
2017-12-26 18:18:56 +03:00
|
|
|
msgs = append(msgs, message)
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-11-30 03:21:08 +03:00
|
|
|
d.reset()
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// Return the array of lnwire.messages.
|
2017-12-26 18:18:56 +03:00
|
|
|
return msgs
|
2017-09-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 10:28:02 +03:00
|
|
|
// calculateSubBatchSize is a helper function that calculates the size to break
|
|
|
|
// down the batchSize into.
|
|
|
|
func calculateSubBatchSize(totalDelay, subBatchDelay time.Duration,
|
|
|
|
minimumBatchSize, batchSize int) int {
|
|
|
|
if subBatchDelay > totalDelay {
|
|
|
|
return batchSize
|
|
|
|
}
|
|
|
|
|
|
|
|
subBatchSize := (int(batchSize)*int(subBatchDelay) + int(totalDelay) - 1) /
|
|
|
|
int(totalDelay)
|
|
|
|
|
|
|
|
if subBatchSize < minimumBatchSize {
|
|
|
|
return minimumBatchSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return subBatchSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitAnnouncementBatches takes an exiting list of announcements and
|
|
|
|
// decomposes it into sub batches controlled by the `subBatchSize`.
|
|
|
|
func splitAnnouncementBatches(subBatchSize int,
|
|
|
|
announcementBatch []msgWithSenders) [][]msgWithSenders {
|
|
|
|
var splitAnnouncementBatch [][]msgWithSenders
|
|
|
|
|
|
|
|
for subBatchSize < len(announcementBatch) {
|
|
|
|
// For slicing with minimal allocation
|
|
|
|
// https://github.com/golang/go/wiki/SliceTricks
|
|
|
|
announcementBatch, splitAnnouncementBatch =
|
|
|
|
announcementBatch[subBatchSize:],
|
|
|
|
append(splitAnnouncementBatch,
|
|
|
|
announcementBatch[0:subBatchSize:subBatchSize])
|
|
|
|
}
|
|
|
|
splitAnnouncementBatch = append(splitAnnouncementBatch, announcementBatch)
|
|
|
|
|
|
|
|
return splitAnnouncementBatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendBatch broadcasts a list of announcements to our peers.
|
|
|
|
func (d *AuthenticatedGossiper) sendBatch(announcementBatch []msgWithSenders) {
|
|
|
|
syncerPeers := d.syncMgr.GossipSyncers()
|
|
|
|
|
|
|
|
// We'll first attempt to filter out this new message
|
|
|
|
// for all peers that have active gossip syncers
|
|
|
|
// active.
|
|
|
|
for _, syncer := range syncerPeers {
|
|
|
|
syncer.FilterGossipMsgs(announcementBatch...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msgChunk := range announcementBatch {
|
|
|
|
// With the syncers taken care of, we'll merge
|
|
|
|
// the sender map with the set of syncers, so
|
|
|
|
// we don't send out duplicate messages.
|
|
|
|
msgChunk.mergeSyncerMap(syncerPeers)
|
|
|
|
|
|
|
|
err := d.cfg.Broadcast(
|
|
|
|
msgChunk.senders, msgChunk.msg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to send batch "+
|
|
|
|
"announcements: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// networkHandler is the primary goroutine that drives this service. The roles
|
|
|
|
// of this goroutine includes answering queries related to the state of the
|
|
|
|
// network, syncing up newly connected peers, and also periodically
|
|
|
|
// broadcasting our latest topology state to all connected peers.
|
2017-03-19 21:40:25 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
2017-04-01 15:33:17 +03:00
|
|
|
func (d *AuthenticatedGossiper) networkHandler() {
|
2017-03-19 21:40:25 +03:00
|
|
|
defer d.wg.Done()
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// Initialize empty deDupedAnnouncements to store announcement batch.
|
|
|
|
announcements := deDupedAnnouncements{}
|
|
|
|
announcements.Reset()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2019-09-10 15:37:17 +03:00
|
|
|
d.cfg.RetransmitTicker.Resume()
|
|
|
|
defer d.cfg.RetransmitTicker.Stop()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
trickleTimer := time.NewTicker(d.cfg.TrickleDelay)
|
2017-03-19 21:40:25 +03:00
|
|
|
defer trickleTimer.Stop()
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
// To start, we'll first check to see if there are any stale channel or
|
|
|
|
// node announcements that we need to re-transmit.
|
|
|
|
if err := d.retransmitStaleAnns(time.Now()); err != nil {
|
|
|
|
log.Errorf("Unable to rebroadcast stale announcements: %v", err)
|
2017-10-05 06:07:54 +03:00
|
|
|
}
|
|
|
|
|
2017-11-30 03:45:08 +03:00
|
|
|
// We'll use this validation to ensure that we process jobs in their
|
|
|
|
// dependency order during parallel validation.
|
2020-11-25 03:39:47 +03:00
|
|
|
validationBarrier := routing.NewValidationBarrier(1000, d.quit)
|
2017-11-30 03:45:08 +03:00
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
for {
|
|
|
|
select {
|
2017-12-15 23:56:11 +03:00
|
|
|
// A new policy update has arrived. We'll commit it to the
|
2017-08-22 09:40:02 +03:00
|
|
|
// sub-systems below us, then craft, sign, and broadcast a new
|
|
|
|
// ChannelUpdate for the set of affected clients.
|
2017-12-15 23:56:11 +03:00
|
|
|
case policyUpdate := <-d.chanPolicyUpdates:
|
2017-08-22 09:40:02 +03:00
|
|
|
// First, we'll now create new fully signed updates for
|
|
|
|
// the affected channels and also update the underlying
|
|
|
|
// graph with the new state.
|
2019-09-19 12:02:46 +03:00
|
|
|
newChanUpdates, err := d.processChanPolicyUpdate(
|
|
|
|
policyUpdate.edgesToUpdate,
|
2018-08-20 15:28:10 +03:00
|
|
|
)
|
2019-09-19 12:02:46 +03:00
|
|
|
policyUpdate.errChan <- err
|
2017-08-22 09:40:02 +03:00
|
|
|
if err != nil {
|
2017-12-15 23:56:11 +03:00
|
|
|
log.Errorf("Unable to craft policy updates: %v",
|
|
|
|
err)
|
2017-08-22 09:40:02 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, with the updates committed, we'll now add
|
|
|
|
// them to the announcement batch to be flushed at the
|
|
|
|
// start of the next epoch.
|
2017-11-30 03:45:08 +03:00
|
|
|
announcements.AddMsgs(newChanUpdates...)
|
2017-08-22 09:40:02 +03:00
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
case announcement := <-d.networkMsgs:
|
2019-07-31 02:25:36 +03:00
|
|
|
// We should only broadcast this message forward if it
|
|
|
|
// originated from us or it wasn't received as part of
|
|
|
|
// our initial historical sync.
|
|
|
|
shouldBroadcast := !announcement.isRemote ||
|
|
|
|
d.syncMgr.IsGraphSynced()
|
|
|
|
|
2018-11-02 03:13:13 +03:00
|
|
|
switch announcement.msg.(type) {
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// Channel announcement signatures are amongst the only
|
|
|
|
// messages that we'll process serially.
|
|
|
|
case *lnwire.AnnounceSignatures:
|
2017-11-30 04:53:16 +03:00
|
|
|
emittedAnnouncements := d.processNetworkAnnouncement(
|
|
|
|
announcement,
|
|
|
|
)
|
|
|
|
if emittedAnnouncements != nil {
|
|
|
|
announcements.AddMsgs(
|
|
|
|
emittedAnnouncements...,
|
|
|
|
)
|
|
|
|
}
|
2017-11-30 05:35:11 +03:00
|
|
|
continue
|
2017-11-30 04:53:16 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 07:41:27 +03:00
|
|
|
// If this message was recently rejected, then we won't
|
|
|
|
// attempt to re-process it.
|
|
|
|
if d.isRecentlyRejectedMsg(announcement.msg) {
|
2018-08-20 15:28:10 +03:00
|
|
|
announcement.err <- fmt.Errorf("recently " +
|
|
|
|
"rejected")
|
2018-01-31 07:41:27 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// We'll set up any dependent, and wait until a free
|
2017-11-30 03:45:08 +03:00
|
|
|
// slot for this job opens up, this allow us to not
|
|
|
|
// have thousands of goroutines active.
|
2018-02-07 06:11:11 +03:00
|
|
|
validationBarrier.InitJobDependencies(announcement.msg)
|
2017-11-30 03:45:08 +03:00
|
|
|
|
2018-05-08 05:10:44 +03:00
|
|
|
d.wg.Add(1)
|
2017-11-30 03:45:08 +03:00
|
|
|
go func() {
|
2018-05-08 05:10:44 +03:00
|
|
|
defer d.wg.Done()
|
2017-11-30 03:45:08 +03:00
|
|
|
defer validationBarrier.CompleteJob()
|
|
|
|
|
|
|
|
// If this message has an existing dependency,
|
|
|
|
// then we'll wait until that has been fully
|
|
|
|
// validated before we proceed.
|
2018-05-08 05:10:44 +03:00
|
|
|
err := validationBarrier.WaitForDependants(
|
|
|
|
announcement.msg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != routing.ErrVBarrierShuttingDown {
|
|
|
|
log.Warnf("unexpected error "+
|
|
|
|
"during validation "+
|
|
|
|
"barrier shutdown: %v",
|
|
|
|
err)
|
|
|
|
}
|
2018-08-20 15:28:10 +03:00
|
|
|
announcement.err <- err
|
2018-05-08 05:10:44 +03:00
|
|
|
return
|
|
|
|
}
|
2017-11-30 03:45:08 +03:00
|
|
|
|
2018-08-20 15:28:10 +03:00
|
|
|
// Process the network announcement to
|
|
|
|
// determine if this is either a new
|
|
|
|
// announcement from our PoV or an edges to a
|
|
|
|
// prior vertex/edge we previously proceeded.
|
2017-11-30 03:45:08 +03:00
|
|
|
emittedAnnouncements := d.processNetworkAnnouncement(
|
|
|
|
announcement,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If this message had any dependencies, then
|
|
|
|
// we can now signal them to continue.
|
2018-08-20 15:28:10 +03:00
|
|
|
validationBarrier.SignalDependants(
|
|
|
|
announcement.msg,
|
|
|
|
)
|
2017-11-30 03:45:08 +03:00
|
|
|
|
2018-08-20 15:28:10 +03:00
|
|
|
// If the announcement was accepted, then add
|
|
|
|
// the emitted announcements to our announce
|
|
|
|
// batch to be broadcast once the trickle timer
|
|
|
|
// ticks gain.
|
2019-07-31 02:25:36 +03:00
|
|
|
if emittedAnnouncements != nil && shouldBroadcast {
|
2018-08-20 15:28:10 +03:00
|
|
|
// TODO(roasbeef): exclude peer that
|
|
|
|
// sent.
|
2017-11-30 03:45:08 +03:00
|
|
|
announcements.AddMsgs(
|
|
|
|
emittedAnnouncements...,
|
|
|
|
)
|
2019-07-31 02:25:36 +03:00
|
|
|
} else if emittedAnnouncements != nil {
|
|
|
|
log.Trace("Skipping broadcast of " +
|
|
|
|
"announcements received " +
|
|
|
|
"during initial graph sync")
|
2017-11-30 03:45:08 +03:00
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-11-30 03:45:08 +03:00
|
|
|
}()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2021-01-05 04:58:45 +03:00
|
|
|
// A new block has arrived, update our best height.
|
2018-06-12 03:37:17 +03:00
|
|
|
case newBlock, ok := <-d.blockEpochs.Epochs:
|
2017-03-19 21:40:25 +03:00
|
|
|
// If the channel has been closed, then this indicates
|
|
|
|
// the daemon is shutting down, so we exit ourselves.
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-16 13:20:56 +03:00
|
|
|
// Once a new block arrives, we update our running
|
2017-03-19 21:40:25 +03:00
|
|
|
// track of the height of the chain tip.
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Lock()
|
2017-03-19 21:40:25 +03:00
|
|
|
blockHeight := uint32(newBlock.Height)
|
2020-04-16 13:20:56 +03:00
|
|
|
d.bestHeight = blockHeight
|
2020-12-03 02:15:50 +03:00
|
|
|
d.Unlock()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2020-04-16 13:20:56 +03:00
|
|
|
log.Debugf("New block: height=%d, hash=%s", blockHeight,
|
2020-04-10 13:00:49 +03:00
|
|
|
newBlock.Hash)
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// The trickle timer has ticked, which indicates we should
|
|
|
|
// flush to the network the pending batch of new announcements
|
|
|
|
// we've received since the last trickle tick.
|
|
|
|
case <-trickleTimer.C:
|
2017-11-30 03:45:08 +03:00
|
|
|
// Emit the current batch of announcements from
|
|
|
|
// deDupedAnnouncements.
|
|
|
|
announcementBatch := announcements.Emit()
|
2017-09-08 04:25:43 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
// If the current announcements batch is nil, then we
|
2017-03-19 21:40:25 +03:00
|
|
|
// have no further work here.
|
|
|
|
if len(announcementBatch) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// Next, If we have new things to announce then
|
|
|
|
// broadcast them to all our immediately connected
|
|
|
|
// peers.
|
2019-04-30 10:28:02 +03:00
|
|
|
subBatchSize := calculateSubBatchSize(
|
|
|
|
d.cfg.TrickleDelay, d.cfg.SubBatchDelay, d.cfg.MinimumBatchSize,
|
|
|
|
len(announcementBatch),
|
|
|
|
)
|
|
|
|
|
|
|
|
splitAnnouncementBatch := splitAnnouncementBatches(
|
|
|
|
subBatchSize, announcementBatch,
|
|
|
|
)
|
|
|
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer d.wg.Done()
|
|
|
|
log.Infof("Broadcasting %v new announcements in %d sub batches",
|
|
|
|
len(announcementBatch), len(splitAnnouncementBatch))
|
|
|
|
|
|
|
|
for _, announcementBatch := range splitAnnouncementBatch {
|
|
|
|
d.sendBatch(announcementBatch)
|
|
|
|
select {
|
|
|
|
case <-time.After(d.cfg.SubBatchDelay):
|
|
|
|
case <-d.quit:
|
|
|
|
return
|
|
|
|
}
|
2017-12-26 18:26:28 +03:00
|
|
|
}
|
2019-04-30 10:28:02 +03:00
|
|
|
}()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
|
|
|
// The retransmission timer has ticked which indicates that we
|
2017-09-25 04:47:48 +03:00
|
|
|
// should check if we need to prune or re-broadcast any of our
|
2019-09-10 15:37:18 +03:00
|
|
|
// personal channels or node announcement. This addresses the
|
|
|
|
// case of "zombie" channels and channel advertisements that
|
|
|
|
// have been dropped, or not properly propagated through the
|
|
|
|
// network.
|
2019-09-10 15:37:17 +03:00
|
|
|
case tick := <-d.cfg.RetransmitTicker.Ticks():
|
2019-09-10 15:37:18 +03:00
|
|
|
if err := d.retransmitStaleAnns(tick); err != nil {
|
2017-10-05 06:07:54 +03:00
|
|
|
log.Errorf("unable to rebroadcast stale "+
|
2019-09-10 15:37:18 +03:00
|
|
|
"announcements: %v", err)
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 20:17:31 +03:00
|
|
|
// The gossiper has been signalled to exit, to we exit our
|
2017-04-01 15:33:17 +03:00
|
|
|
// main loop so the wait group can be decremented.
|
2017-03-19 21:40:25 +03:00
|
|
|
case <-d.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:29:12 +03:00
|
|
|
// TODO(roasbeef): d/c peers that send updates not on our chain
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
|
2018-07-31 11:29:12 +03:00
|
|
|
// InitSyncState is called by outside sub-systems when a connection is
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
// established to a new peer that understands how to perform channel range
|
|
|
|
// queries. We'll allocate a new gossip syncer for it, and start any goroutines
|
2019-03-23 05:56:33 +03:00
|
|
|
// needed to handle new queries.
|
|
|
|
func (d *AuthenticatedGossiper) InitSyncState(syncPeer lnpeer.Peer) {
|
|
|
|
d.syncMgr.InitSyncState(syncPeer)
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PruneSyncState is called by outside sub-systems once a peer that we were
|
|
|
|
// previously connected to has been disconnected. In this case we can stop the
|
2019-03-23 05:54:46 +03:00
|
|
|
// existing GossipSyncer assigned to the peer and free up resources.
|
2019-04-05 18:36:11 +03:00
|
|
|
func (d *AuthenticatedGossiper) PruneSyncState(peer route.Vertex) {
|
2019-03-23 05:56:33 +03:00
|
|
|
d.syncMgr.PruneSyncState(peer)
|
discovery: update AuthenticatedGossiper to be aware of new gossipSyncers
In this commit, we update the logic in the AuthenticatedGossiper to
ensure that can properly create, manage, and dispatch messages to any
gossipSyncer instances created by the server.
With this set of changes, the gossip now has complete knowledge of the
current set of peers we're conneted to that support the new range
queries. Upon initial connect, InitSyncState will be called by the
server if the new peer understands the set of gossip queries. This will
then create a new spot in the peerSyncers map for the new syncer. For
each new gossip query message, we'll then attempt to dispatch the
message directly to the gossip syncer. When the peer has disconnected,
we then expect the server to call the PruneSyncState method which will
allow us to free up the resources.
Finally, when we go to broadcast messages, we'll send the messages
directly to the peers that have gossipSyncer instances active, so they
can properly be filtered out. For those that don't we'll broadcast
directly, ensuring we skip *all* peers that have an active gossip
syncer.
2018-04-17 05:00:00 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 07:41:27 +03:00
|
|
|
// isRecentlyRejectedMsg returns true if we recently rejected a message, and
|
|
|
|
// false otherwise, This avoids expensive reprocessing of the message.
|
|
|
|
func (d *AuthenticatedGossiper) isRecentlyRejectedMsg(msg lnwire.Message) bool {
|
|
|
|
d.rejectMtx.RLock()
|
|
|
|
defer d.rejectMtx.RUnlock()
|
|
|
|
|
|
|
|
switch m := msg.(type) {
|
|
|
|
case *lnwire.ChannelUpdate:
|
|
|
|
_, ok := d.recentRejects[m.ShortChannelID.ToUint64()]
|
|
|
|
return ok
|
|
|
|
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
_, ok := d.recentRejects[m.ShortChannelID.ToUint64()]
|
|
|
|
return ok
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
// retransmitStaleAnns examines all outgoing channels that the source node is
|
|
|
|
// known to maintain to check to see if any of them are "stale". A channel is
|
|
|
|
// stale iff, the last timestamp of its rebroadcast is older than the
|
|
|
|
// RebroadcastInterval. We also check if a refreshed node announcement should
|
|
|
|
// be resent.
|
|
|
|
func (d *AuthenticatedGossiper) retransmitStaleAnns(now time.Time) error {
|
2017-10-05 06:07:54 +03:00
|
|
|
// Iterate over all of our channels and check if any of them fall
|
|
|
|
// within the prune interval or re-broadcast interval.
|
|
|
|
type updateTuple struct {
|
|
|
|
info *channeldb.ChannelEdgeInfo
|
|
|
|
edge *channeldb.ChannelEdgePolicy
|
|
|
|
}
|
2019-09-10 15:37:18 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
havePublicChannels bool
|
|
|
|
edgesToUpdate []updateTuple
|
|
|
|
)
|
2017-10-05 06:07:54 +03:00
|
|
|
err := d.cfg.Router.ForAllOutgoingChannels(func(
|
|
|
|
info *channeldb.ChannelEdgeInfo,
|
|
|
|
edge *channeldb.ChannelEdgePolicy) error {
|
|
|
|
|
2018-01-17 07:05:07 +03:00
|
|
|
// If there's no auth proof attached to this edge, it means
|
|
|
|
// that it is a private channel not meant to be announced to
|
|
|
|
// the greater network, so avoid sending channel updates for
|
|
|
|
// this channel to not leak its
|
2017-11-14 04:12:57 +03:00
|
|
|
// existence.
|
|
|
|
if info.AuthProof == nil {
|
|
|
|
log.Debugf("Skipping retransmission of channel "+
|
2018-01-19 01:07:56 +03:00
|
|
|
"without AuthProof: %v", info.ChannelID)
|
2017-11-14 04:12:57 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
// We make a note that we have at least one public channel. We
|
|
|
|
// use this to determine whether we should send a node
|
|
|
|
// announcement below.
|
|
|
|
havePublicChannels = true
|
|
|
|
|
2019-01-12 20:59:45 +03:00
|
|
|
// If this edge has a ChannelUpdate that was created before the
|
|
|
|
// introduction of the MaxHTLC field, then we'll update this
|
|
|
|
// edge to propagate this information in the network.
|
2019-01-16 14:43:46 +03:00
|
|
|
if !edge.MessageFlags.HasMaxHtlc() {
|
2019-09-20 12:26:45 +03:00
|
|
|
// We'll make sure we support the new max_htlc field if
|
|
|
|
// not already present.
|
|
|
|
edge.MessageFlags |= lnwire.ChanUpdateOptionMaxHtlc
|
|
|
|
edge.MaxHTLC = lnwire.NewMSatFromSatoshis(info.Capacity)
|
|
|
|
|
2019-01-12 20:59:45 +03:00
|
|
|
edgesToUpdate = append(edgesToUpdate, updateTuple{
|
|
|
|
info: info,
|
|
|
|
edge: edge,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:17 +03:00
|
|
|
timeElapsed := now.Sub(edge.LastUpdate)
|
2017-10-05 06:07:54 +03:00
|
|
|
|
2019-09-10 15:37:17 +03:00
|
|
|
// If it's been longer than RebroadcastInterval since we've
|
|
|
|
// re-broadcasted the channel, add the channel to the set of
|
|
|
|
// edges we need to update.
|
|
|
|
if timeElapsed >= d.cfg.RebroadcastInterval {
|
2017-10-05 06:07:54 +03:00
|
|
|
edgesToUpdate = append(edgesToUpdate, updateTuple{
|
|
|
|
info: info,
|
|
|
|
edge: edge,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2018-08-29 04:35:24 +03:00
|
|
|
if err != nil && err != channeldb.ErrGraphNoEdgesFound {
|
|
|
|
return fmt.Errorf("unable to retrieve outgoing channels: %v",
|
|
|
|
err)
|
2017-10-05 06:07:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var signedUpdates []lnwire.Message
|
|
|
|
for _, chanToUpdate := range edgesToUpdate {
|
|
|
|
// Re-sign and update the channel on disk and retrieve our
|
|
|
|
// ChannelUpdate to broadcast.
|
2018-04-04 05:46:06 +03:00
|
|
|
chanAnn, chanUpdate, err := d.updateChannel(
|
|
|
|
chanToUpdate.info, chanToUpdate.edge,
|
|
|
|
)
|
2017-10-05 06:07:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to update channel: %v", err)
|
|
|
|
}
|
2017-10-05 06:33:03 +03:00
|
|
|
|
|
|
|
// If we have a valid announcement to transmit, then we'll send
|
|
|
|
// that along with the update.
|
|
|
|
if chanAnn != nil {
|
|
|
|
signedUpdates = append(signedUpdates, chanAnn)
|
|
|
|
}
|
|
|
|
|
2017-10-05 06:07:54 +03:00
|
|
|
signedUpdates = append(signedUpdates, chanUpdate)
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
// If we don't have any public channels, we return as we don't want to
|
|
|
|
// broadcast anything that would reveal our existence.
|
|
|
|
if !havePublicChannels {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also check that our NodeAnnouncement is not too old.
|
|
|
|
currentNodeAnn, err := d.cfg.SelfNodeAnnouncement(false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get current node announment: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp := time.Unix(int64(currentNodeAnn.Timestamp), 0)
|
|
|
|
timeElapsed := now.Sub(timestamp)
|
|
|
|
|
|
|
|
// If it's been a full day since we've re-broadcasted the
|
|
|
|
// node announcement, refresh it and resend it.
|
|
|
|
nodeAnnStr := ""
|
|
|
|
if timeElapsed >= d.cfg.RebroadcastInterval {
|
|
|
|
newNodeAnn, err := d.cfg.SelfNodeAnnouncement(true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get refreshed node "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signedUpdates = append(signedUpdates, &newNodeAnn)
|
|
|
|
nodeAnnStr = " and our refreshed node announcement"
|
|
|
|
|
|
|
|
// Before broadcasting the refreshed node announcement, add it
|
|
|
|
// to our own graph.
|
|
|
|
if err := d.addNode(&newNodeAnn); err != nil {
|
|
|
|
log.Errorf("Unable to add refreshed node announcement "+
|
|
|
|
"to graph: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have any updates to re-broadcast, then we'll exit
|
2017-10-05 06:07:54 +03:00
|
|
|
// early.
|
|
|
|
if len(signedUpdates) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:18 +03:00
|
|
|
log.Infof("Retransmitting %v outgoing channels%v",
|
|
|
|
len(edgesToUpdate), nodeAnnStr)
|
2017-10-05 06:07:54 +03:00
|
|
|
|
|
|
|
// With all the wire announcements properly crafted, we'll broadcast
|
|
|
|
// our known outgoing channels to all our immediate peers.
|
|
|
|
if err := d.cfg.Broadcast(nil, signedUpdates...); err != nil {
|
|
|
|
return fmt.Errorf("unable to re-broadcast channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 12:02:46 +03:00
|
|
|
// processChanPolicyUpdate generates a new set of channel updates for the
|
|
|
|
// provided list of edges and updates the backing ChannelGraphSource.
|
2017-12-15 23:56:11 +03:00
|
|
|
func (d *AuthenticatedGossiper) processChanPolicyUpdate(
|
2019-09-19 12:02:46 +03:00
|
|
|
edgesToUpdate []EdgeWithInfo) ([]networkMsg, error) {
|
2019-09-20 12:26:45 +03:00
|
|
|
|
2018-04-04 05:46:06 +03:00
|
|
|
var chanUpdates []networkMsg
|
|
|
|
for _, edgeInfo := range edgesToUpdate {
|
|
|
|
// Now that we've collected all the channels we need to update,
|
2019-09-19 12:02:46 +03:00
|
|
|
// we'll re-sign and update the backing ChannelGraphSource, and
|
2017-10-05 05:30:54 +03:00
|
|
|
// retrieve our ChannelUpdate to broadcast.
|
2018-04-04 05:46:06 +03:00
|
|
|
_, chanUpdate, err := d.updateChannel(
|
2019-09-19 12:02:46 +03:00
|
|
|
edgeInfo.Info, edgeInfo.Edge,
|
2018-04-04 05:46:06 +03:00
|
|
|
)
|
2017-08-22 09:40:02 +03:00
|
|
|
if err != nil {
|
2019-09-19 12:02:46 +03:00
|
|
|
return nil, err
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 03:54:58 +03:00
|
|
|
// We'll avoid broadcasting any updates for private channels to
|
2019-04-10 03:44:45 +03:00
|
|
|
// avoid directly giving away their existence. Instead, we'll
|
|
|
|
// send the update directly to the remote party.
|
2019-09-19 12:02:46 +03:00
|
|
|
if edgeInfo.Info.AuthProof == nil {
|
2019-04-10 03:44:45 +03:00
|
|
|
remotePubKey := remotePubFromChanInfo(
|
2019-09-19 12:02:46 +03:00
|
|
|
edgeInfo.Info, chanUpdate.ChannelFlags,
|
2019-04-10 03:44:45 +03:00
|
|
|
)
|
|
|
|
err := d.reliableSender.sendMessage(
|
|
|
|
chanUpdate, remotePubKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to reliably send %v for "+
|
|
|
|
"channel=%v to peer=%x: %v",
|
|
|
|
chanUpdate.MsgType(),
|
|
|
|
chanUpdate.ShortChannelID,
|
|
|
|
remotePubKey, err)
|
|
|
|
}
|
2019-04-09 03:54:58 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:23:05 +03:00
|
|
|
// We set ourselves as the source of this message to indicate
|
|
|
|
// that we shouldn't skip any peers when sending this message.
|
|
|
|
chanUpdates = append(chanUpdates, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
source: d.selfKey,
|
|
|
|
msg: chanUpdate,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
2019-09-19 12:02:46 +03:00
|
|
|
return chanUpdates, nil
|
2017-08-22 09:40:02 +03:00
|
|
|
}
|
|
|
|
|
2020-03-18 02:24:10 +03:00
|
|
|
// remotePubFromChanInfo returns the public key of the remote peer given a
|
|
|
|
// ChannelEdgeInfo that describe a channel we have with them.
|
|
|
|
func remotePubFromChanInfo(chanInfo *channeldb.ChannelEdgeInfo,
|
|
|
|
chanFlags lnwire.ChanUpdateChanFlags) [33]byte {
|
|
|
|
|
|
|
|
var remotePubKey [33]byte
|
|
|
|
switch {
|
|
|
|
case chanFlags&lnwire.ChanUpdateDirection == 0:
|
|
|
|
remotePubKey = chanInfo.NodeKey2Bytes
|
|
|
|
case chanFlags&lnwire.ChanUpdateDirection == 1:
|
|
|
|
remotePubKey = chanInfo.NodeKey1Bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return remotePubKey
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// processRejectedEdge examines a rejected edge to see if we can extract any
|
2017-12-26 18:28:17 +03:00
|
|
|
// new announcements from it. An edge will get rejected if we already added
|
|
|
|
// the same edge without AuthProof to the graph. If the received announcement
|
|
|
|
// contains a proof, we can add this proof to our edge. We can end up in this
|
2018-02-07 06:11:11 +03:00
|
|
|
// situation in the case where we create a channel, but for some reason fail
|
2017-12-26 18:28:17 +03:00
|
|
|
// to receive the remote peer's proof, while the remote peer is able to fully
|
|
|
|
// assemble the proof and craft the ChannelAnnouncement.
|
2018-08-20 15:28:10 +03:00
|
|
|
func (d *AuthenticatedGossiper) processRejectedEdge(
|
|
|
|
chanAnnMsg *lnwire.ChannelAnnouncement,
|
2017-12-26 18:28:17 +03:00
|
|
|
proof *channeldb.ChannelAuthProof) ([]networkMsg, error) {
|
|
|
|
|
|
|
|
// First, we'll fetch the state of the channel as we know if from the
|
|
|
|
// database.
|
|
|
|
chanInfo, e1, e2, err := d.cfg.Router.GetChannelByID(
|
|
|
|
chanAnnMsg.ShortChannelID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The edge is in the graph, and has a proof attached, then we'll just
|
|
|
|
// reject it as normal.
|
|
|
|
if chanInfo.AuthProof != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this means that the edge is within the graph, but it
|
2018-01-02 11:57:01 +03:00
|
|
|
// doesn't yet have a proper proof attached. If we did not receive
|
|
|
|
// the proof such that we now can add it, there's nothing more we
|
|
|
|
// can do.
|
|
|
|
if proof == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-26 18:28:17 +03:00
|
|
|
|
|
|
|
// We'll then create then validate the new fully assembled
|
|
|
|
// announcement.
|
2020-03-18 02:23:54 +03:00
|
|
|
chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
|
2017-12-26 18:28:17 +03:00
|
|
|
proof, chanInfo, e1, e2,
|
|
|
|
)
|
2018-01-31 07:23:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-15 16:51:01 +03:00
|
|
|
err = routing.ValidateChannelAnn(chanAnn)
|
2017-12-26 18:28:17 +03:00
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("assembled channel announcement proof "+
|
2017-12-26 18:28:17 +03:00
|
|
|
"for shortChanID=%v isn't valid: %v",
|
|
|
|
chanAnnMsg.ShortChannelID, err)
|
|
|
|
log.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If everything checks out, then we'll add the fully assembled proof
|
|
|
|
// to the database.
|
|
|
|
err = d.cfg.Router.AddProof(chanAnnMsg.ShortChannelID, proof)
|
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable add proof to shortChanID=%v: %v",
|
2017-12-26 18:28:17 +03:00
|
|
|
chanAnnMsg.ShortChannelID, err)
|
|
|
|
log.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// As we now have a complete channel announcement for this channel,
|
|
|
|
// we'll construct the announcement so they can be broadcast out to all
|
|
|
|
// our peers.
|
|
|
|
announcements := make([]networkMsg, 0, 3)
|
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
source: d.selfKey,
|
|
|
|
msg: chanAnn,
|
2017-12-26 18:28:17 +03:00
|
|
|
})
|
|
|
|
if e1Ann != nil {
|
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
source: d.selfKey,
|
|
|
|
msg: e1Ann,
|
2017-12-26 18:28:17 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if e2Ann != nil {
|
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
source: d.selfKey,
|
|
|
|
msg: e2Ann,
|
2017-12-26 18:28:17 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return announcements, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:37:16 +03:00
|
|
|
// addNode processes the given node announcement, and adds it to our channel
|
|
|
|
// graph.
|
|
|
|
func (d *AuthenticatedGossiper) addNode(msg *lnwire.NodeAnnouncement) error {
|
|
|
|
if err := routing.ValidateNodeAnn(msg); err != nil {
|
|
|
|
return fmt.Errorf("unable to validate node announcement: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp := time.Unix(int64(msg.Timestamp), 0)
|
2019-11-08 16:32:00 +03:00
|
|
|
features := lnwire.NewFeatureVector(msg.Features, lnwire.Features)
|
2019-09-10 15:37:16 +03:00
|
|
|
node := &channeldb.LightningNode{
|
|
|
|
HaveNodeAnnouncement: true,
|
|
|
|
LastUpdate: timestamp,
|
|
|
|
Addresses: msg.Addresses,
|
|
|
|
PubKeyBytes: msg.NodeID,
|
|
|
|
Alias: msg.Alias.String(),
|
|
|
|
AuthSigBytes: msg.Signature.ToSignatureBytes(),
|
|
|
|
Features: features,
|
|
|
|
Color: msg.RGBColor,
|
|
|
|
ExtraOpaqueData: msg.ExtraOpaqueData,
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.cfg.Router.AddNode(node)
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// processNetworkAnnouncement processes a new network relate authenticated
|
2017-03-28 22:08:14 +03:00
|
|
|
// channel or node announcement or announcements proofs. If the announcement
|
|
|
|
// didn't affect the internal state due to either being out of date, invalid,
|
2017-04-01 15:33:17 +03:00
|
|
|
// or redundant, then nil is returned. Otherwise, the set of announcements will
|
|
|
|
// be returned which should be broadcasted to the rest of the network.
|
2018-08-20 15:28:10 +03:00
|
|
|
func (d *AuthenticatedGossiper) processNetworkAnnouncement(
|
|
|
|
nMsg *networkMsg) []networkMsg {
|
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
isPremature := func(chanID lnwire.ShortChannelID, delta uint32) bool {
|
2017-08-22 09:40:02 +03:00
|
|
|
// TODO(roasbeef) make height delta 6
|
|
|
|
// * or configurable
|
2020-04-16 13:20:56 +03:00
|
|
|
return chanID.BlockHeight+delta > d.bestHeight
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-12-26 18:28:17 +03:00
|
|
|
var announcements []networkMsg
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
switch msg := nMsg.msg.(type) {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-08-05 04:32:25 +03:00
|
|
|
// A new node announcement has arrived which either presents new
|
|
|
|
// information about a node in one of the channels we know about, or a
|
|
|
|
// updating previously advertised information.
|
2017-03-19 21:40:25 +03:00
|
|
|
case *lnwire.NodeAnnouncement:
|
2018-02-25 06:35:58 +03:00
|
|
|
timestamp := time.Unix(int64(msg.Timestamp), 0)
|
|
|
|
|
2018-10-10 16:05:21 +03:00
|
|
|
// We'll quickly ask the router if it already has a
|
|
|
|
// newer update for this node so we can skip validating
|
|
|
|
// signatures if not required.
|
|
|
|
if d.cfg.Router.IsStaleNode(msg.NodeID, timestamp) {
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-25 06:35:58 +03:00
|
|
|
|
2019-09-10 15:37:16 +03:00
|
|
|
if err := d.addNode(msg); err != nil {
|
2017-08-05 04:32:25 +03:00
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
|
|
|
|
2017-04-14 00:30:41 +03:00
|
|
|
log.Debug(err)
|
2017-03-28 22:08:14 +03:00
|
|
|
} else {
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Error(err)
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
nMsg.err <- err
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:49:14 +03:00
|
|
|
// In order to ensure we don't leak unadvertised nodes, we'll
|
|
|
|
// make a quick check to ensure this node intends to publicly
|
|
|
|
// advertise itself to the network.
|
2019-09-10 15:37:16 +03:00
|
|
|
isPublic, err := d.cfg.Router.IsPublicNode(msg.NodeID)
|
2018-10-18 01:49:14 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to determine if node %x is "+
|
2019-09-10 15:37:16 +03:00
|
|
|
"advertised: %v", msg.NodeID, err)
|
2018-10-18 01:49:14 +03:00
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it does, we'll add their announcement to our batch so that
|
|
|
|
// it can be broadcast to the rest of our peers.
|
|
|
|
if isPublic {
|
|
|
|
announcements = append(announcements, networkMsg{
|
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nMsg.source,
|
|
|
|
msg: msg,
|
|
|
|
})
|
2018-11-05 09:57:24 +03:00
|
|
|
} else {
|
|
|
|
log.Tracef("Skipping broadcasting node announcement "+
|
|
|
|
"for %x due to being unadvertised", msg.NodeID)
|
2018-10-18 01:49:14 +03:00
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
|
|
|
nMsg.err <- nil
|
2017-04-01 15:33:17 +03:00
|
|
|
// TODO(roasbeef): get rid of the above
|
2017-03-28 22:08:14 +03:00
|
|
|
return announcements
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// A new channel announcement has arrived, this indicates the
|
|
|
|
// *creation* of a new channel within the network. This only advertises
|
|
|
|
// the existence of a channel and not yet the routing policies in
|
|
|
|
// either direction of the channel.
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
2017-09-04 02:41:01 +03:00
|
|
|
// We'll ignore any channel announcements that target any chain
|
|
|
|
// other than the set of chains we know of.
|
|
|
|
if !bytes.Equal(msg.ChainHash[:], d.cfg.ChainHash[:]) {
|
2019-09-25 21:42:38 +03:00
|
|
|
err := fmt.Errorf("ignoring ChannelAnnouncement from "+
|
2017-09-04 02:41:01 +03:00
|
|
|
"chain=%v, gossiper on chain=%v", msg.ChainHash,
|
|
|
|
d.cfg.ChainHash)
|
2018-08-20 15:28:09 +03:00
|
|
|
log.Errorf(err.Error())
|
|
|
|
|
2018-01-31 07:41:27 +03:00
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
2018-08-20 15:28:09 +03:00
|
|
|
|
|
|
|
nMsg.err <- err
|
2017-09-04 02:41:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
// If the advertised inclusionary block is beyond our knowledge
|
|
|
|
// of the chain tip, then we'll put the announcement in limbo
|
|
|
|
// to be fully verified once we advance forward in the chain.
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Lock()
|
2017-10-18 05:24:04 +03:00
|
|
|
if nMsg.isRemote && isPremature(msg.ShortChannelID, 0) {
|
2018-08-20 15:28:10 +03:00
|
|
|
log.Infof("Announcement for chan_id=(%v), is "+
|
|
|
|
"premature: advertises height %v, only "+
|
|
|
|
"height %v is known",
|
2017-04-24 05:06:33 +03:00
|
|
|
msg.ShortChannelID.ToUint64(),
|
2017-11-30 03:45:08 +03:00
|
|
|
msg.ShortChannelID.BlockHeight,
|
2020-04-16 13:20:56 +03:00
|
|
|
d.bestHeight)
|
2017-11-30 03:45:08 +03:00
|
|
|
d.Unlock()
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Unlock()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2019-03-27 23:08:03 +03:00
|
|
|
// At this point, we'll now ask the router if this is a
|
|
|
|
// zombie/known edge. If so we can skip all the processing
|
|
|
|
// below.
|
2018-02-25 06:35:58 +03:00
|
|
|
if d.cfg.Router.IsKnownEdge(msg.ShortChannelID) {
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-05 04:32:25 +03:00
|
|
|
// If this is a remote channel announcement, then we'll validate
|
|
|
|
// all the signatures within the proof as it should be well
|
2017-04-01 15:33:17 +03:00
|
|
|
// formed.
|
2017-03-19 21:40:25 +03:00
|
|
|
var proof *channeldb.ChannelAuthProof
|
2017-03-28 22:08:14 +03:00
|
|
|
if nMsg.isRemote {
|
2018-08-15 16:51:01 +03:00
|
|
|
if err := routing.ValidateChannelAnn(msg); err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable to validate "+
|
2017-03-27 20:25:44 +03:00
|
|
|
"announcement: %v", err)
|
2018-01-31 07:41:27 +03:00
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-03-27 20:25:44 +03:00
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// If the proof checks out, then we'll save the proof
|
|
|
|
// itself to the database so we can fetch it later when
|
|
|
|
// gossiping with other nodes.
|
2017-03-28 22:08:14 +03:00
|
|
|
proof = &channeldb.ChannelAuthProof{
|
2018-01-31 07:23:14 +03:00
|
|
|
NodeSig1Bytes: msg.NodeSig1.ToSignatureBytes(),
|
|
|
|
NodeSig2Bytes: msg.NodeSig2.ToSignatureBytes(),
|
|
|
|
BitcoinSig1Bytes: msg.BitcoinSig1.ToSignatureBytes(),
|
|
|
|
BitcoinSig2Bytes: msg.BitcoinSig2.ToSignatureBytes(),
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// With the proof validate (if necessary), we can now store it
|
|
|
|
// within the database for our path finding and syncing needs.
|
2017-08-22 09:41:19 +03:00
|
|
|
var featureBuf bytes.Buffer
|
|
|
|
if err := msg.Features.Encode(&featureBuf); err != nil {
|
2017-08-22 11:00:07 +03:00
|
|
|
log.Errorf("unable to encode features: %v", err)
|
2017-08-22 09:41:19 +03:00
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
edge := &channeldb.ChannelEdgeInfo{
|
2018-01-31 07:23:14 +03:00
|
|
|
ChannelID: msg.ShortChannelID.ToUint64(),
|
|
|
|
ChainHash: msg.ChainHash,
|
|
|
|
NodeKey1Bytes: msg.NodeID1,
|
|
|
|
NodeKey2Bytes: msg.NodeID2,
|
|
|
|
BitcoinKey1Bytes: msg.BitcoinKey1,
|
|
|
|
BitcoinKey2Bytes: msg.BitcoinKey2,
|
|
|
|
AuthProof: proof,
|
|
|
|
Features: featureBuf.Bytes(),
|
2018-09-01 06:13:54 +03:00
|
|
|
ExtraOpaqueData: msg.ExtraOpaqueData,
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
2017-07-14 22:41:46 +03:00
|
|
|
|
2019-04-17 23:25:56 +03:00
|
|
|
// If there were any optional message fields provided, we'll
|
|
|
|
// include them in its serialized disk representation now.
|
|
|
|
if nMsg.optionalMsgFields != nil {
|
|
|
|
if nMsg.optionalMsgFields.capacity != nil {
|
|
|
|
edge.Capacity = *nMsg.optionalMsgFields.capacity
|
|
|
|
}
|
|
|
|
if nMsg.optionalMsgFields.channelPoint != nil {
|
|
|
|
edge.ChannelPoint = *nMsg.optionalMsgFields.channelPoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:41:46 +03:00
|
|
|
// We will add the edge to the channel router. If the nodes
|
|
|
|
// present in this channel are not present in the database, a
|
|
|
|
// partial node will be added to represent each node while we
|
|
|
|
// wait for a node announcement.
|
2018-01-23 18:26:15 +03:00
|
|
|
//
|
|
|
|
// Before we add the edge to the database, we obtain
|
|
|
|
// the mutex for this channel ID. We do this to ensure
|
|
|
|
// no other goroutine has read the database and is now
|
|
|
|
// making decisions based on this DB state, before it
|
|
|
|
// writes to the DB.
|
|
|
|
d.channelMtx.Lock(msg.ShortChannelID.ToUint64())
|
|
|
|
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
|
2017-03-19 21:40:25 +03:00
|
|
|
if err := d.cfg.Router.AddEdge(edge); err != nil {
|
2017-12-26 18:28:17 +03:00
|
|
|
// If the edge was rejected due to already being known,
|
|
|
|
// then it may be that case that this new message has a
|
2018-02-25 06:35:58 +03:00
|
|
|
// fresh channel proof, so we'll check.
|
2020-11-25 03:38:14 +03:00
|
|
|
if routing.IsError(err, routing.ErrIgnored) {
|
2017-12-26 18:28:17 +03:00
|
|
|
// Attempt to process the rejected message to
|
|
|
|
// see if we get any new announcements.
|
|
|
|
anns, rErr := d.processRejectedEdge(msg, proof)
|
2018-01-02 11:57:01 +03:00
|
|
|
if rErr != nil {
|
2018-01-31 07:41:27 +03:00
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
2017-12-26 18:28:17 +03:00
|
|
|
nMsg.err <- rErr
|
2017-11-18 06:21:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:28:17 +03:00
|
|
|
// If while processing this rejected edge, we
|
|
|
|
// realized there's a set of announcements we
|
|
|
|
// could extract, then we'll return those
|
|
|
|
// directly.
|
|
|
|
if len(anns) != 0 {
|
2017-11-18 06:21:50 +03:00
|
|
|
nMsg.err <- nil
|
2017-12-26 18:28:17 +03:00
|
|
|
return anns
|
2017-11-18 06:21:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-24 01:50:50 +03:00
|
|
|
// Otherwise, this is just a regular rejected
|
|
|
|
// edge.
|
2017-12-26 18:28:17 +03:00
|
|
|
log.Debugf("Router rejected channel "+
|
|
|
|
"edge: %v", err)
|
2017-03-19 21:40:25 +03:00
|
|
|
} else {
|
2018-03-24 01:50:50 +03:00
|
|
|
log.Tracef("Router rejected channel "+
|
2017-12-26 18:28:17 +03:00
|
|
|
"edge: %v", err)
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
nMsg.err <- err
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
// Channel announcement was successfully proceeded and know it
|
2017-04-01 15:33:17 +03:00
|
|
|
// might be broadcast to other connected nodes if it was
|
2017-03-28 22:08:14 +03:00
|
|
|
// announcement with proof (remote).
|
|
|
|
if proof != nil {
|
2017-12-26 18:23:05 +03:00
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nMsg.source,
|
|
|
|
msg: msg,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
nMsg.err <- nil
|
|
|
|
return announcements
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// A new authenticated channel edge update has arrived. This indicates
|
2017-03-19 21:40:25 +03:00
|
|
|
// that the directional information for an already known channel has
|
|
|
|
// been updated.
|
2017-04-20 02:20:46 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2017-09-04 02:41:01 +03:00
|
|
|
// We'll ignore any channel announcements that target any chain
|
|
|
|
// other than the set of chains we know of.
|
|
|
|
if !bytes.Equal(msg.ChainHash[:], d.cfg.ChainHash[:]) {
|
2019-09-25 21:42:38 +03:00
|
|
|
err := fmt.Errorf("ignoring ChannelUpdate from "+
|
2017-09-04 02:41:01 +03:00
|
|
|
"chain=%v, gossiper on chain=%v", msg.ChainHash,
|
|
|
|
d.cfg.ChainHash)
|
2018-08-20 15:28:09 +03:00
|
|
|
log.Errorf(err.Error())
|
|
|
|
|
2018-01-31 07:41:27 +03:00
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
2018-08-20 15:28:09 +03:00
|
|
|
|
|
|
|
nMsg.err <- err
|
2017-09-04 02:41:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
blockHeight := msg.ShortChannelID.BlockHeight
|
|
|
|
shortChanID := msg.ShortChannelID.ToUint64()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
|
|
|
// If the advertised inclusionary block is beyond our knowledge
|
2021-01-05 04:58:45 +03:00
|
|
|
// of the chain tip, then we'll ignore it.
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Lock()
|
2017-10-18 05:24:04 +03:00
|
|
|
if nMsg.isRemote && isPremature(msg.ShortChannelID, 0) {
|
2017-03-28 22:08:14 +03:00
|
|
|
log.Infof("Update announcement for "+
|
2017-04-01 15:33:17 +03:00
|
|
|
"short_chan_id(%v), is premature: advertises "+
|
2017-03-28 22:08:14 +03:00
|
|
|
"height %v, only height %v is known",
|
2017-11-30 03:45:08 +03:00
|
|
|
shortChanID, blockHeight,
|
2020-04-16 13:20:56 +03:00
|
|
|
d.bestHeight)
|
2017-11-30 03:45:08 +03:00
|
|
|
d.Unlock()
|
2021-01-05 04:58:45 +03:00
|
|
|
|
|
|
|
nMsg.err <- nil
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Unlock()
|
2017-03-19 21:40:25 +03:00
|
|
|
|
2018-02-25 06:35:58 +03:00
|
|
|
// Before we perform any of the expensive checks below, we'll
|
2019-03-27 23:08:03 +03:00
|
|
|
// check whether this update is stale or is for a zombie
|
|
|
|
// channel in order to quickly reject it.
|
2018-02-25 06:35:58 +03:00
|
|
|
timestamp := time.Unix(int64(msg.Timestamp), 0)
|
|
|
|
if d.cfg.Router.IsStaleEdgePolicy(
|
2019-01-12 20:59:43 +03:00
|
|
|
msg.ShortChannelID, timestamp, msg.ChannelFlags,
|
2018-02-25 06:35:58 +03:00
|
|
|
) {
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Get the node pub key as far as we don't have it in channel
|
|
|
|
// update announcement message. We'll need this to properly
|
|
|
|
// verify message signature.
|
2018-01-23 18:26:15 +03:00
|
|
|
//
|
|
|
|
// We make sure to obtain the mutex for this channel ID
|
2018-02-07 11:30:09 +03:00
|
|
|
// before we access the database. This ensures the state
|
2018-01-23 18:26:15 +03:00
|
|
|
// we read from the database has not changed between this
|
|
|
|
// point and when we call UpdateEdge() later.
|
|
|
|
d.channelMtx.Lock(msg.ShortChannelID.ToUint64())
|
|
|
|
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
|
2020-11-20 04:31:58 +03:00
|
|
|
chanInfo, edge1, edge2, err := d.cfg.Router.GetChannelByID(msg.ShortChannelID)
|
2019-03-27 23:08:03 +03:00
|
|
|
switch err {
|
|
|
|
// No error, break.
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case channeldb.ErrZombieEdge:
|
|
|
|
// Since we've deemed the update as not stale above,
|
|
|
|
// before marking it live, we'll make sure it has been
|
|
|
|
// signed by the correct party. The least-significant
|
|
|
|
// bit in the flag on the channel update tells us which
|
|
|
|
// edge is being updated.
|
|
|
|
var pubKey *btcec.PublicKey
|
|
|
|
switch {
|
|
|
|
case msg.ChannelFlags&lnwire.ChanUpdateDirection == 0:
|
|
|
|
pubKey, _ = chanInfo.NodeKey1()
|
|
|
|
case msg.ChannelFlags&lnwire.ChanUpdateDirection == 1:
|
|
|
|
pubKey, _ = chanInfo.NodeKey2()
|
|
|
|
}
|
2018-04-28 02:31:15 +03:00
|
|
|
|
2019-03-27 23:08:03 +03:00
|
|
|
err := routing.VerifyChannelUpdateSignature(msg, pubKey)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("unable to verify channel "+
|
|
|
|
"update signature: %v", err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
2017-12-14 19:52:41 +03:00
|
|
|
return nil
|
2019-03-27 23:08:03 +03:00
|
|
|
}
|
2018-08-20 15:28:10 +03:00
|
|
|
|
2019-03-27 23:08:03 +03:00
|
|
|
// With the signature valid, we'll proceed to mark the
|
|
|
|
// edge as live and wait for the channel announcement to
|
|
|
|
// come through again.
|
|
|
|
err = d.cfg.Router.MarkEdgeLive(msg.ShortChannelID)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("unable to remove edge with "+
|
|
|
|
"chan_id=%v from zombie index: %v",
|
|
|
|
msg.ShortChannelID, err)
|
2017-12-14 19:52:41 +03:00
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 23:08:03 +03:00
|
|
|
|
|
|
|
log.Debugf("Removed edge with chan_id=%v from zombie "+
|
|
|
|
"index", msg.ShortChannelID)
|
|
|
|
|
2021-01-05 04:58:45 +03:00
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
|
2019-03-27 23:08:03 +03:00
|
|
|
case channeldb.ErrGraphNotFound:
|
|
|
|
fallthrough
|
|
|
|
case channeldb.ErrGraphNoEdgesFound:
|
|
|
|
fallthrough
|
|
|
|
case channeldb.ErrEdgeNotFound:
|
|
|
|
log.Debugf("Got ChannelUpdate for edge not found in "+
|
2021-01-05 04:58:45 +03:00
|
|
|
"graph(shortChanID=%v)", shortChanID)
|
2019-03-27 23:08:03 +03:00
|
|
|
|
2021-01-05 04:58:45 +03:00
|
|
|
// TODO: Add to recentRejects like below? Return error?
|
|
|
|
nMsg.err <- nil
|
2019-03-27 23:08:03 +03:00
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("unable to validate channel update "+
|
|
|
|
"short_chan_id=%v: %v", shortChanID, err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
|
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
|
|
|
return nil
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 09:37:16 +03:00
|
|
|
// The least-significant bit in the flag on the channel update
|
|
|
|
// announcement tells us "which" side of the channels directed
|
|
|
|
// edge is being updated.
|
2020-11-20 04:31:58 +03:00
|
|
|
var (
|
|
|
|
pubKey *btcec.PublicKey
|
|
|
|
edgeToUpdate *channeldb.ChannelEdgePolicy
|
|
|
|
)
|
|
|
|
direction := msg.ChannelFlags & lnwire.ChanUpdateDirection
|
|
|
|
switch direction {
|
|
|
|
case 0:
|
2018-01-31 07:23:14 +03:00
|
|
|
pubKey, _ = chanInfo.NodeKey1()
|
2020-11-20 04:31:58 +03:00
|
|
|
edgeToUpdate = edge1
|
|
|
|
case 1:
|
2018-01-31 07:23:14 +03:00
|
|
|
pubKey, _ = chanInfo.NodeKey2()
|
2020-11-20 04:31:58 +03:00
|
|
|
edgeToUpdate = edge2
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a previous version of the edge being updated,
|
|
|
|
// we'll want to rate limit its updates to prevent spam
|
2020-11-30 03:13:40 +03:00
|
|
|
// throughout the network if we're currently throttling such
|
|
|
|
// updates.
|
|
|
|
if d.cfg.GossipUpdateThrottle && nMsg.isRemote && edgeToUpdate != nil {
|
2020-11-20 04:31:58 +03:00
|
|
|
// If it's a keep-alive update, we'll only propagate one
|
|
|
|
// if it's been a day since the previous. This follows
|
|
|
|
// our own heuristic of sending keep-alive updates after
|
|
|
|
// the same duration (see retransmitStaleAnns).
|
|
|
|
timeSinceLastUpdate := timestamp.Sub(edgeToUpdate.LastUpdate)
|
|
|
|
if IsKeepAliveUpdate(msg, edgeToUpdate) {
|
|
|
|
if timeSinceLastUpdate < d.cfg.RebroadcastInterval {
|
|
|
|
log.Debugf("Ignoring keep alive update "+
|
|
|
|
"not within %v period for "+
|
|
|
|
"channel %v",
|
|
|
|
d.cfg.RebroadcastInterval,
|
|
|
|
shortChanID)
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If it's not, we'll only allow a single update
|
|
|
|
// for this channel per block.
|
|
|
|
d.Lock()
|
|
|
|
lastUpdateHeight := d.heightForLastChanUpdate[shortChanID]
|
|
|
|
if lastUpdateHeight[direction] == d.bestHeight {
|
|
|
|
log.Debugf("Ignoring update for "+
|
|
|
|
"channel %v due to previous "+
|
|
|
|
"update occurring within the "+
|
|
|
|
"same block %v", shortChanID,
|
|
|
|
d.bestHeight)
|
|
|
|
d.Unlock()
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
d.Unlock()
|
|
|
|
}
|
2017-03-27 20:25:44 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
// Validate the channel announcement with the expected public key and
|
|
|
|
// channel capacity. In the case of an invalid channel update, we'll
|
|
|
|
// return an error to the caller and exit early.
|
|
|
|
err = routing.ValidateChannelUpdateAnn(pubKey, chanInfo.Capacity, msg)
|
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
rErr := fmt.Errorf("unable to validate channel "+
|
2017-04-01 15:33:17 +03:00
|
|
|
"update announcement for short_chan_id=%v: %v",
|
|
|
|
spew.Sdump(msg.ShortChannelID), err)
|
|
|
|
|
2017-08-23 06:12:43 +03:00
|
|
|
log.Error(rErr)
|
|
|
|
nMsg.err <- rErr
|
2017-03-27 20:25:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-03-19 21:40:25 +03:00
|
|
|
update := &channeldb.ChannelEdgePolicy{
|
2018-01-31 07:23:14 +03:00
|
|
|
SigBytes: msg.Signature.ToSignatureBytes(),
|
2017-03-28 22:08:14 +03:00
|
|
|
ChannelID: shortChanID,
|
2018-02-25 06:35:58 +03:00
|
|
|
LastUpdate: timestamp,
|
2019-01-12 20:59:43 +03:00
|
|
|
MessageFlags: msg.MessageFlags,
|
|
|
|
ChannelFlags: msg.ChannelFlags,
|
2017-03-19 21:40:25 +03:00
|
|
|
TimeLockDelta: msg.TimeLockDelta,
|
2017-08-22 09:40:02 +03:00
|
|
|
MinHTLC: msg.HtlcMinimumMsat,
|
2019-01-12 20:59:44 +03:00
|
|
|
MaxHTLC: msg.HtlcMaximumMsat,
|
2017-08-22 09:40:02 +03:00
|
|
|
FeeBaseMSat: lnwire.MilliSatoshi(msg.BaseFee),
|
|
|
|
FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
|
2018-09-01 06:13:54 +03:00
|
|
|
ExtraOpaqueData: msg.ExtraOpaqueData,
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.cfg.Router.UpdateEdge(update); err != nil {
|
2018-08-20 15:28:10 +03:00
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
2017-04-14 00:30:41 +03:00
|
|
|
log.Debug(err)
|
2017-03-28 22:08:14 +03:00
|
|
|
} else {
|
2018-01-31 07:41:27 +03:00
|
|
|
d.rejectMtx.Lock()
|
|
|
|
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{}
|
|
|
|
d.rejectMtx.Unlock()
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Error(err)
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
nMsg.err <- err
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:31:58 +03:00
|
|
|
// With the edge successfully updated on disk, we'll note the
|
|
|
|
// current height so that we're able to rate limit any future
|
|
|
|
// updates for the same channel.
|
|
|
|
d.Lock()
|
|
|
|
lastUpdateHeight := d.heightForLastChanUpdate[shortChanID]
|
|
|
|
lastUpdateHeight[direction] = d.bestHeight
|
|
|
|
d.heightForLastChanUpdate[shortChanID] = lastUpdateHeight
|
|
|
|
d.Unlock()
|
|
|
|
|
2017-12-14 19:52:41 +03:00
|
|
|
// If this is a local ChannelUpdate without an AuthProof, it
|
|
|
|
// means it is an update to a channel that is not (yet)
|
|
|
|
// supposed to be announced to the greater network. However,
|
|
|
|
// our channel counter party will need to be given the update,
|
|
|
|
// so we'll try sending the update directly to the remote peer.
|
2017-11-14 04:12:57 +03:00
|
|
|
if !nMsg.isRemote && chanInfo.AuthProof == nil {
|
|
|
|
// Get our peer's public key.
|
2019-04-10 03:44:45 +03:00
|
|
|
remotePubKey := remotePubFromChanInfo(
|
|
|
|
chanInfo, msg.ChannelFlags,
|
|
|
|
)
|
2017-11-14 02:51:56 +03:00
|
|
|
|
2019-02-06 04:19:04 +03:00
|
|
|
// Now, we'll attempt to send the channel update message
|
|
|
|
// reliably to the remote peer in the background, so
|
|
|
|
// that we don't block if the peer happens to be offline
|
|
|
|
// at the moment.
|
|
|
|
err := d.reliableSender.sendMessage(msg, remotePubKey)
|
2018-06-08 06:11:27 +03:00
|
|
|
if err != nil {
|
2019-02-06 04:19:04 +03:00
|
|
|
err := fmt.Errorf("unable to reliably send %v "+
|
|
|
|
"for channel=%v to peer=%x: %v",
|
|
|
|
msg.MsgType(), msg.ShortChannelID,
|
|
|
|
remotePubKey, err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
2017-11-14 02:51:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Channel update announcement was successfully processed and
|
|
|
|
// now it can be broadcast to the rest of the network. However,
|
|
|
|
// we'll only broadcast the channel update announcement if it
|
|
|
|
// has an attached authentication proof.
|
2017-03-28 22:08:14 +03:00
|
|
|
if chanInfo.AuthProof != nil {
|
2017-12-26 18:23:05 +03:00
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nMsg.source,
|
|
|
|
msg: msg,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nMsg.err <- nil
|
|
|
|
return announcements
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// A new signature announcement has been received. This indicates
|
|
|
|
// willingness of nodes involved in the funding of a channel to
|
|
|
|
// announce this new channel to the rest of the world.
|
2017-03-28 22:08:14 +03:00
|
|
|
case *lnwire.AnnounceSignatures:
|
2018-08-20 15:28:10 +03:00
|
|
|
needBlockHeight := msg.ShortChannelID.BlockHeight +
|
|
|
|
d.cfg.ProofMatureDelta
|
2017-03-28 22:08:14 +03:00
|
|
|
shortChanID := msg.ShortChannelID.ToUint64()
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
prefix := "local"
|
|
|
|
if nMsg.isRemote {
|
|
|
|
prefix = "remote"
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-16 00:20:53 +03:00
|
|
|
log.Infof("Received new %v channel announcement for %v", prefix,
|
|
|
|
msg.ShortChannelID)
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
|
|
// By the specification, channel announcement proofs should be
|
|
|
|
// sent after some number of confirmations after channel was
|
|
|
|
// registered in bitcoin blockchain. Therefore, we check if the
|
|
|
|
// proof is premature. If so we'll halt processing until the
|
|
|
|
// expected announcement height. This allows us to be tolerant
|
|
|
|
// to other clients if this constraint was changed.
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Lock()
|
2017-03-28 22:08:14 +03:00
|
|
|
if isPremature(msg.ShortChannelID, d.cfg.ProofMatureDelta) {
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Infof("Premature proof announcement, "+
|
2017-03-28 22:08:14 +03:00
|
|
|
"current block height lower than needed: %v <"+
|
|
|
|
" %v, add announcement to reprocessing batch",
|
2020-04-16 13:20:56 +03:00
|
|
|
d.bestHeight, needBlockHeight)
|
|
|
|
d.Unlock()
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-16 13:20:56 +03:00
|
|
|
d.Unlock()
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Ensure that we know of a channel with the target channel ID
|
|
|
|
// before proceeding further.
|
2018-01-23 18:26:15 +03:00
|
|
|
//
|
|
|
|
// We must acquire the mutex for this channel ID before getting
|
|
|
|
// the channel from the database, to ensure what we read does
|
|
|
|
// not change before we call AddProof() later.
|
|
|
|
d.channelMtx.Lock(msg.ShortChannelID.ToUint64())
|
|
|
|
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
|
2018-08-20 15:28:10 +03:00
|
|
|
|
2017-11-18 06:21:50 +03:00
|
|
|
chanInfo, e1, e2, err := d.cfg.Router.GetChannelByID(
|
|
|
|
msg.ShortChannelID)
|
2017-03-28 22:08:14 +03:00
|
|
|
if err != nil {
|
2017-04-26 05:04:53 +03:00
|
|
|
// TODO(andrew.shvv) this is dangerous because remote
|
|
|
|
// node might rewrite the waiting proof.
|
2017-05-05 20:17:31 +03:00
|
|
|
proof := channeldb.NewWaitingProof(nMsg.isRemote, msg)
|
2019-02-06 04:18:34 +03:00
|
|
|
err := d.cfg.WaitingProofStore.Add(proof)
|
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable to store "+
|
2017-05-05 20:17:31 +03:00
|
|
|
"the proof for short_chan_id=%v: %v",
|
|
|
|
shortChanID, err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-26 05:04:53 +03:00
|
|
|
|
|
|
|
log.Infof("Orphan %v proof announcement with "+
|
2019-10-15 14:16:03 +03:00
|
|
|
"short_chan_id=%v, adding "+
|
2017-04-26 05:04:53 +03:00
|
|
|
"to waiting batch", prefix, shortChanID)
|
|
|
|
nMsg.err <- nil
|
2017-03-28 22:08:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-08 06:11:27 +03:00
|
|
|
nodeID := nMsg.source.SerializeCompressed()
|
|
|
|
isFirstNode := bytes.Equal(nodeID, chanInfo.NodeKey1Bytes[:])
|
|
|
|
isSecondNode := bytes.Equal(nodeID, chanInfo.NodeKey2Bytes[:])
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Ensure that channel that was retrieved belongs to the peer
|
|
|
|
// which sent the proof announcement.
|
2017-03-28 22:08:14 +03:00
|
|
|
if !(isFirstNode || isSecondNode) {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("channel that was received not "+
|
2017-03-28 22:08:14 +03:00
|
|
|
"belongs to the peer which sent the proof, "+
|
2017-04-01 15:33:17 +03:00
|
|
|
"short_chan_id=%v", shortChanID)
|
2017-03-28 22:08:14 +03:00
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-18 06:21:50 +03:00
|
|
|
// If proof was sent by a local sub-system, then we'll
|
|
|
|
// send the announcement signature to the remote node
|
|
|
|
// so they can also reconstruct the full channel
|
|
|
|
// announcement.
|
|
|
|
if !nMsg.isRemote {
|
2019-02-06 04:18:56 +03:00
|
|
|
var remotePubKey [33]byte
|
2017-11-18 06:21:50 +03:00
|
|
|
if isFirstNode {
|
2019-02-06 04:18:56 +03:00
|
|
|
remotePubKey = chanInfo.NodeKey2Bytes
|
2017-11-18 06:21:50 +03:00
|
|
|
} else {
|
2019-02-06 04:18:56 +03:00
|
|
|
remotePubKey = chanInfo.NodeKey1Bytes
|
2017-11-18 06:21:50 +03:00
|
|
|
}
|
|
|
|
// Since the remote peer might not be online
|
|
|
|
// we'll call a method that will attempt to
|
|
|
|
// deliver the proof when it comes online.
|
2019-02-06 04:18:56 +03:00
|
|
|
err := d.reliableSender.sendMessage(msg, remotePubKey)
|
2018-08-20 15:28:10 +03:00
|
|
|
if err != nil {
|
2019-02-06 04:18:56 +03:00
|
|
|
err := fmt.Errorf("unable to reliably send %v "+
|
|
|
|
"for channel=%v to peer=%x: %v",
|
|
|
|
msg.MsgType(), msg.ShortChannelID,
|
|
|
|
remotePubKey, err)
|
2017-11-18 06:21:50 +03:00
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we already have the full proof for this channel.
|
|
|
|
if chanInfo.AuthProof != nil {
|
|
|
|
// If we already have the fully assembled proof, then
|
|
|
|
// the peer sending us their proof has probably not
|
|
|
|
// received our local proof yet. So be kind and send
|
|
|
|
// them the full proof.
|
|
|
|
if nMsg.isRemote {
|
2018-06-08 06:11:27 +03:00
|
|
|
peerID := nMsg.source.SerializeCompressed()
|
2017-11-18 06:21:50 +03:00
|
|
|
log.Debugf("Got AnnounceSignatures for " +
|
|
|
|
"channel with full proof.")
|
|
|
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer d.wg.Done()
|
|
|
|
log.Debugf("Received half proof for "+
|
|
|
|
"channel %v with existing "+
|
|
|
|
"full proof. Sending full "+
|
|
|
|
"proof to peer=%x",
|
|
|
|
msg.ChannelID,
|
|
|
|
peerID)
|
|
|
|
|
2020-03-18 02:23:54 +03:00
|
|
|
chanAnn, _, _, err := netann.CreateChanAnnouncement(
|
2018-08-20 15:28:10 +03:00
|
|
|
chanInfo.AuthProof, chanInfo,
|
|
|
|
e1, e2,
|
2018-01-31 07:23:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2018-08-20 15:28:10 +03:00
|
|
|
log.Errorf("unable to gen "+
|
|
|
|
"ann: %v", err)
|
2018-01-31 07:23:14 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-20 15:28:10 +03:00
|
|
|
err = nMsg.peer.SendMessage(
|
|
|
|
false, chanAnn,
|
|
|
|
)
|
2017-11-18 06:21:50 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed sending "+
|
|
|
|
"full proof to "+
|
|
|
|
"peer=%x: %v",
|
|
|
|
peerID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debugf("Full proof sent to peer=%x"+
|
2018-08-20 15:28:10 +03:00
|
|
|
" for chanID=%v", peerID,
|
|
|
|
msg.ChannelID)
|
2017-11-18 06:21:50 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Already have proof for channel "+
|
|
|
|
"with chanID=%v", msg.ChannelID)
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Check that we received the opposite proof. If so, then we're
|
|
|
|
// now able to construct the full proof, and create the channel
|
2017-03-28 22:08:14 +03:00
|
|
|
// announcement. If we didn't receive the opposite half of the
|
2017-04-01 15:33:17 +03:00
|
|
|
// proof than we should store it this one, and wait for
|
|
|
|
// opposite to be received.
|
2017-05-05 20:17:31 +03:00
|
|
|
proof := channeldb.NewWaitingProof(nMsg.isRemote, msg)
|
2019-02-06 04:18:34 +03:00
|
|
|
oppositeProof, err := d.cfg.WaitingProofStore.Get(
|
|
|
|
proof.OppositeKey(),
|
|
|
|
)
|
2017-05-05 20:17:31 +03:00
|
|
|
if err != nil && err != channeldb.ErrWaitingProofNotFound {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable to get "+
|
2017-05-05 20:17:31 +03:00
|
|
|
"the opposite proof for short_chan_id=%v: %v",
|
|
|
|
shortChanID, err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == channeldb.ErrWaitingProofNotFound {
|
2019-02-06 04:18:34 +03:00
|
|
|
err := d.cfg.WaitingProofStore.Add(proof)
|
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable to store "+
|
2017-05-05 20:17:31 +03:00
|
|
|
"the proof for short_chan_id=%v: %v",
|
|
|
|
shortChanID, err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Infof("1/2 of channel ann proof received for "+
|
|
|
|
"short_chan_id=%v, waiting for other half",
|
|
|
|
shortChanID)
|
2017-03-28 22:08:14 +03:00
|
|
|
|
|
|
|
nMsg.err <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-18 06:21:50 +03:00
|
|
|
// We now have both halves of the channel announcement proof,
|
|
|
|
// then we'll reconstruct the initial announcement so we can
|
|
|
|
// validate it shortly below.
|
2017-03-28 22:08:14 +03:00
|
|
|
var dbProof channeldb.ChannelAuthProof
|
|
|
|
if isFirstNode {
|
2018-01-31 07:23:14 +03:00
|
|
|
dbProof.NodeSig1Bytes = msg.NodeSignature.ToSignatureBytes()
|
|
|
|
dbProof.NodeSig2Bytes = oppositeProof.NodeSignature.ToSignatureBytes()
|
|
|
|
dbProof.BitcoinSig1Bytes = msg.BitcoinSignature.ToSignatureBytes()
|
|
|
|
dbProof.BitcoinSig2Bytes = oppositeProof.BitcoinSignature.ToSignatureBytes()
|
2017-03-28 22:08:14 +03:00
|
|
|
} else {
|
2018-01-31 07:23:14 +03:00
|
|
|
dbProof.NodeSig1Bytes = oppositeProof.NodeSignature.ToSignatureBytes()
|
|
|
|
dbProof.NodeSig2Bytes = msg.NodeSignature.ToSignatureBytes()
|
|
|
|
dbProof.BitcoinSig1Bytes = oppositeProof.BitcoinSignature.ToSignatureBytes()
|
|
|
|
dbProof.BitcoinSig2Bytes = msg.BitcoinSignature.ToSignatureBytes()
|
|
|
|
}
|
2020-03-18 02:23:54 +03:00
|
|
|
chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
|
2018-08-20 15:28:10 +03:00
|
|
|
&dbProof, chanInfo, e1, e2,
|
|
|
|
)
|
2018-01-31 07:23:14 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// With all the necessary components assembled validate the
|
|
|
|
// full channel announcement proof.
|
2018-08-15 16:51:01 +03:00
|
|
|
if err := routing.ValidateChannelAnn(chanAnn); err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("channel announcement proof "+
|
2017-04-01 15:33:17 +03:00
|
|
|
"for short_chan_id=%v isn't valid: %v",
|
2017-03-27 20:25:44 +03:00
|
|
|
shortChanID, err)
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-03-27 20:25:44 +03:00
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
|
|
|
// If the channel was returned by the router it means that
|
|
|
|
// existence of funding point and inclusion of nodes bitcoin
|
2017-04-01 15:33:17 +03:00
|
|
|
// keys in it already checked by the router. In this stage we
|
|
|
|
// should check that node keys are attest to the bitcoin keys
|
|
|
|
// by validating the signatures of announcement. If proof is
|
|
|
|
// valid then we'll populate the channel edge with it, so we
|
|
|
|
// can announce it on peer connect.
|
2017-03-28 22:08:14 +03:00
|
|
|
err = d.cfg.Router.AddProof(msg.ShortChannelID, &dbProof)
|
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable add proof to the "+
|
2017-03-28 22:08:14 +03:00
|
|
|
"channel chanID=%v: %v", msg.ChannelID, err)
|
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-05 20:17:31 +03:00
|
|
|
|
2019-02-06 04:18:34 +03:00
|
|
|
err = d.cfg.WaitingProofStore.Remove(proof.OppositeKey())
|
2018-08-20 15:28:10 +03:00
|
|
|
if err != nil {
|
2018-09-05 07:45:57 +03:00
|
|
|
err := fmt.Errorf("unable remove opposite proof "+
|
2018-08-20 15:28:10 +03:00
|
|
|
"for the channel with chanID=%v: %v",
|
|
|
|
msg.ChannelID, err)
|
2017-05-05 20:17:31 +03:00
|
|
|
log.Error(err)
|
|
|
|
nMsg.err <- err
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 22:08:14 +03:00
|
|
|
|
|
|
|
// Proof was successfully created and now can announce the
|
|
|
|
// channel to the remain network.
|
2017-04-01 15:33:17 +03:00
|
|
|
log.Infof("Fully valid channel proof for short_chan_id=%v "+
|
|
|
|
"constructed, adding to next ann batch",
|
|
|
|
shortChanID)
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Assemble the necessary announcements to add to the next
|
|
|
|
// broadcasting batch.
|
2017-12-26 18:23:05 +03:00
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nMsg.source,
|
|
|
|
msg: chanAnn,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2021-01-06 23:51:22 +03:00
|
|
|
if src, err := chanInfo.NodeKey1(); err == nil && e1Ann != nil {
|
2017-12-26 18:23:05 +03:00
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: nMsg.peer,
|
2021-01-06 23:51:22 +03:00
|
|
|
source: src,
|
2018-06-08 06:11:27 +03:00
|
|
|
msg: e1Ann,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2021-01-06 23:51:22 +03:00
|
|
|
if src, err := chanInfo.NodeKey2(); err == nil && e2Ann != nil {
|
2017-12-26 18:23:05 +03:00
|
|
|
announcements = append(announcements, networkMsg{
|
2018-06-08 06:11:27 +03:00
|
|
|
peer: nMsg.peer,
|
2021-01-06 23:51:22 +03:00
|
|
|
source: src,
|
2018-06-08 06:11:27 +03:00
|
|
|
msg: e2Ann,
|
2017-12-26 18:23:05 +03:00
|
|
|
})
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-11-05 09:59:38 +03:00
|
|
|
// We'll also send along the node announcements for each channel
|
2019-06-19 03:32:56 +03:00
|
|
|
// participant if we know of them. To ensure our node
|
|
|
|
// announcement propagates to our channel counterparty, we'll
|
|
|
|
// set the source for each announcement to the node it belongs
|
|
|
|
// to, otherwise we won't send it since the source gets skipped.
|
|
|
|
// This isn't necessary for channel updates and announcement
|
|
|
|
// signatures since we send those directly to our channel
|
|
|
|
// counterparty through the gossiper's reliable sender.
|
2018-11-05 09:59:38 +03:00
|
|
|
node1Ann, err := d.fetchNodeAnn(chanInfo.NodeKey1Bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Unable to fetch node announcement for "+
|
|
|
|
"%x: %v", chanInfo.NodeKey1Bytes, err)
|
|
|
|
} else {
|
2019-06-19 03:32:56 +03:00
|
|
|
if nodeKey1, err := chanInfo.NodeKey1(); err == nil {
|
|
|
|
announcements = append(announcements, networkMsg{
|
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nodeKey1,
|
|
|
|
msg: node1Ann,
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 09:59:38 +03:00
|
|
|
}
|
|
|
|
node2Ann, err := d.fetchNodeAnn(chanInfo.NodeKey2Bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Unable to fetch node announcement for "+
|
|
|
|
"%x: %v", chanInfo.NodeKey2Bytes, err)
|
|
|
|
} else {
|
2019-06-19 03:32:56 +03:00
|
|
|
if nodeKey2, err := chanInfo.NodeKey2(); err == nil {
|
|
|
|
announcements = append(announcements, networkMsg{
|
|
|
|
peer: nMsg.peer,
|
|
|
|
source: nodeKey2,
|
|
|
|
msg: node2Ann,
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 09:59:38 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
nMsg.err <- nil
|
|
|
|
return announcements
|
|
|
|
|
|
|
|
default:
|
|
|
|
nMsg.err <- errors.New("wrong type of the announcement")
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-19 21:40:25 +03:00
|
|
|
}
|
|
|
|
|
2018-11-05 09:59:38 +03:00
|
|
|
// fetchNodeAnn fetches the latest signed node announcement from our point of
|
|
|
|
// view for the node with the given public key.
|
|
|
|
func (d *AuthenticatedGossiper) fetchNodeAnn(
|
|
|
|
pubKey [33]byte) (*lnwire.NodeAnnouncement, error) {
|
|
|
|
|
|
|
|
node, err := d.cfg.Router.FetchLightningNode(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.NodeAnnouncement(true)
|
|
|
|
}
|
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// isMsgStale determines whether a message retrieved from the backing
|
|
|
|
// MessageStore is seen as stale by the current graph.
|
|
|
|
func (d *AuthenticatedGossiper) isMsgStale(msg lnwire.Message) bool {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
chanInfo, _, _, err := d.cfg.Router.GetChannelByID(
|
|
|
|
msg.ShortChannelID,
|
|
|
|
)
|
2017-11-18 06:21:50 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// If the channel cannot be found, it is most likely a leftover
|
|
|
|
// message for a channel that was closed, so we can consider it
|
|
|
|
// stale.
|
|
|
|
if err == channeldb.ErrEdgeNotFound {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Unable to retrieve channel=%v from graph: "+
|
|
|
|
"%v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-11-18 06:21:50 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
// If the proof exists in the graph, then we have successfully
|
|
|
|
// received the remote proof and assembled the full proof, so we
|
|
|
|
// can safely delete the local proof from the database.
|
|
|
|
return chanInfo.AuthProof != nil
|
2018-07-18 02:43:06 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2019-03-13 23:36:21 +03:00
|
|
|
_, p1, p2, err := d.cfg.Router.GetChannelByID(msg.ShortChannelID)
|
|
|
|
|
|
|
|
// If the channel cannot be found, it is most likely a leftover
|
|
|
|
// message for a channel that was closed, so we can consider it
|
|
|
|
// stale.
|
|
|
|
if err == channeldb.ErrEdgeNotFound {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-02-06 04:18:56 +03:00
|
|
|
log.Debugf("Unable to retrieve channel=%v from graph: "+
|
2019-03-13 23:36:21 +03:00
|
|
|
"%v", msg.ShortChannelID, err)
|
|
|
|
return false
|
2017-11-18 06:21:50 +03:00
|
|
|
}
|
2019-03-13 23:36:21 +03:00
|
|
|
|
|
|
|
// Otherwise, we'll retrieve the correct policy that we
|
|
|
|
// currently have stored within our graph to check if this
|
|
|
|
// message is stale by comparing its timestamp.
|
|
|
|
var p *channeldb.ChannelEdgePolicy
|
|
|
|
if msg.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
|
|
|
|
p = p1
|
|
|
|
} else {
|
|
|
|
p = p2
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:49:34 +03:00
|
|
|
// If the policy is still unknown, then we can consider this
|
|
|
|
// policy fresh.
|
|
|
|
if p == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-13 23:36:21 +03:00
|
|
|
timestamp := time.Unix(int64(msg.Timestamp), 0)
|
|
|
|
return p.LastUpdate.After(timestamp)
|
2017-11-18 06:21:50 +03:00
|
|
|
|
2019-02-06 04:18:56 +03:00
|
|
|
default:
|
|
|
|
// We'll make sure to not mark any unsupported messages as stale
|
|
|
|
// to ensure they are not removed.
|
|
|
|
return false
|
|
|
|
}
|
2017-11-18 06:21:50 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 05:30:54 +03:00
|
|
|
// updateChannel creates a new fully signed update for the channel, and updates
|
|
|
|
// the underlying graph with the new state.
|
|
|
|
func (d *AuthenticatedGossiper) updateChannel(info *channeldb.ChannelEdgeInfo,
|
2018-08-20 15:28:10 +03:00
|
|
|
edge *channeldb.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement,
|
|
|
|
*lnwire.ChannelUpdate, error) {
|
2017-09-25 04:47:48 +03:00
|
|
|
|
2020-03-19 23:43:39 +03:00
|
|
|
// Parse the unsigned edge into a channel update.
|
|
|
|
chanUpdate := netann.UnsignedChannelUpdateFromEdge(info, edge)
|
2019-01-12 20:59:45 +03:00
|
|
|
|
2020-03-19 23:43:39 +03:00
|
|
|
// We'll generate a new signature over a digest of the channel
|
|
|
|
// announcement itself and update the timestamp to ensure it propagate.
|
|
|
|
err := netann.SignChannelUpdate(
|
2020-03-18 02:23:01 +03:00
|
|
|
d.cfg.AnnSigner, d.selfKey, chanUpdate,
|
2020-03-19 23:43:39 +03:00
|
|
|
netann.ChanUpdSetTimestamp,
|
2020-03-18 02:23:01 +03:00
|
|
|
)
|
2017-09-25 04:47:48 +03:00
|
|
|
if err != nil {
|
2017-10-05 06:31:24 +03:00
|
|
|
return nil, nil, err
|
2017-09-25 04:47:48 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 05:30:54 +03:00
|
|
|
// Next, we'll set the new signature in place, and update the reference
|
|
|
|
// in the backing slice.
|
2020-03-19 23:43:39 +03:00
|
|
|
edge.LastUpdate = time.Unix(int64(chanUpdate.Timestamp), 0)
|
|
|
|
edge.SigBytes = chanUpdate.Signature.ToSignatureBytes()
|
2017-09-25 04:47:48 +03:00
|
|
|
|
2017-10-05 05:30:54 +03:00
|
|
|
// To ensure that our signature is valid, we'll verify it ourself
|
|
|
|
// before committing it to the slice returned.
|
2019-01-12 20:59:43 +03:00
|
|
|
err = routing.ValidateChannelUpdateAnn(d.selfKey, info.Capacity, chanUpdate)
|
2017-09-25 04:47:48 +03:00
|
|
|
if err != nil {
|
2017-10-05 06:31:24 +03:00
|
|
|
return nil, nil, fmt.Errorf("generated invalid channel "+
|
|
|
|
"update sig: %v", err)
|
2017-09-25 04:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll write the new edge policy to disk.
|
|
|
|
if err := d.cfg.Router.UpdateEdge(edge); err != nil {
|
2017-10-05 06:31:24 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also create the original channel announcement so the two can
|
|
|
|
// be broadcast along side each other (if necessary), but only if we
|
|
|
|
// have a full channel announcement for this channel.
|
|
|
|
var chanAnn *lnwire.ChannelAnnouncement
|
|
|
|
if info.AuthProof != nil {
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(info.ChannelID)
|
|
|
|
chanAnn = &lnwire.ChannelAnnouncement{
|
2018-09-01 06:13:54 +03:00
|
|
|
ShortChannelID: chanID,
|
|
|
|
NodeID1: info.NodeKey1Bytes,
|
|
|
|
NodeID2: info.NodeKey2Bytes,
|
|
|
|
ChainHash: info.ChainHash,
|
|
|
|
BitcoinKey1: info.BitcoinKey1Bytes,
|
|
|
|
Features: lnwire.NewRawFeatureVector(),
|
|
|
|
BitcoinKey2: info.BitcoinKey2Bytes,
|
|
|
|
ExtraOpaqueData: edge.ExtraOpaqueData,
|
2018-01-31 07:23:14 +03:00
|
|
|
}
|
|
|
|
chanAnn.NodeSig1, err = lnwire.NewSigFromRawSignature(
|
|
|
|
info.AuthProof.NodeSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.NodeSig2, err = lnwire.NewSigFromRawSignature(
|
|
|
|
info.AuthProof.NodeSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.BitcoinSig1, err = lnwire.NewSigFromRawSignature(
|
|
|
|
info.AuthProof.BitcoinSig1Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
chanAnn.BitcoinSig2, err = lnwire.NewSigFromRawSignature(
|
|
|
|
info.AuthProof.BitcoinSig2Bytes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-10-05 06:31:24 +03:00
|
|
|
}
|
2017-09-25 04:47:48 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 06:31:24 +03:00
|
|
|
return chanAnn, chanUpdate, err
|
2017-09-25 04:47:48 +03:00
|
|
|
}
|
2019-03-23 05:56:33 +03:00
|
|
|
|
|
|
|
// SyncManager returns the gossiper's SyncManager instance.
|
|
|
|
func (d *AuthenticatedGossiper) SyncManager() *SyncManager {
|
|
|
|
return d.syncMgr
|
|
|
|
}
|
2020-11-20 04:31:58 +03:00
|
|
|
|
|
|
|
// IsKeepAliveUpdate determines whether this channel update is considered a
|
|
|
|
// keep-alive update based on the previous channel update processed for the same
|
|
|
|
// direction.
|
|
|
|
func IsKeepAliveUpdate(update *lnwire.ChannelUpdate,
|
|
|
|
prev *channeldb.ChannelEdgePolicy) bool {
|
|
|
|
|
|
|
|
// Both updates should be from the same direction.
|
|
|
|
if update.ChannelFlags&lnwire.ChanUpdateDirection !=
|
|
|
|
prev.ChannelFlags&lnwire.ChanUpdateDirection {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The timestamp should always increase for a keep-alive update.
|
|
|
|
timestamp := time.Unix(int64(update.Timestamp), 0)
|
|
|
|
if !timestamp.After(prev.LastUpdate) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the remaining fields should change for a keep-alive update.
|
|
|
|
if update.ChannelFlags.IsDisabled() != prev.ChannelFlags.IsDisabled() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if lnwire.MilliSatoshi(update.BaseFee) != prev.FeeBaseMSat {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if lnwire.MilliSatoshi(update.FeeRate) != prev.FeeProportionalMillionths {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if update.TimeLockDelta != prev.TimeLockDelta {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if update.HtlcMinimumMsat != prev.MinHTLC {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if update.MessageFlags.HasMaxHtlc() && !prev.MessageFlags.HasMaxHtlc() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if update.HtlcMaximumMsat != prev.MaxHTLC {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !bytes.Equal(update.ExtraOpaqueData, prev.ExtraOpaqueData) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2020-12-03 02:15:35 +03:00
|
|
|
|
|
|
|
// latestHeight returns the gossiper's latest height known of the chain.
|
|
|
|
func (d *AuthenticatedGossiper) latestHeight() uint32 {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
return d.bestHeight
|
|
|
|
}
|