Skip to content

Commit

Permalink
Merge 3b508c1 into fe0a7b6
Browse files Browse the repository at this point in the history
  • Loading branch information
flaurida committed Sep 8, 2017
2 parents fe0a7b6 + 3b508c1 commit d7b8eda
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
77 changes: 74 additions & 3 deletions discovery/gossiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnwallet"
Expand Down Expand Up @@ -107,12 +108,12 @@ type Config struct {
}

// AuthenticatedGossiper is a subsystem which is responsible for receiving
// announcements validate them and apply the changes to router, syncing
// lightning network with newly connected nodes, broadcasting announcements
// announcements, validating them and applying the changes to router, syncing
// the 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
// incoming messages 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
Expand Down Expand Up @@ -319,6 +320,76 @@ func (d *AuthenticatedGossiper) ProcessLocalAnnouncement(msg lnwire.Message,
return nMsg.err
}

// ChannelUpdateID is a unique identifier for ChannelUpdate messages, as
// channel updates can be identified by the (ShortChannelID, Flags) tuple
type ChannelUpdateID struct {
// ShortChannelID 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.
Flags uint16
}

// deDupedAnnouncements de-duplicates announcements that have been
// added to the batch. Internally, announcements are stored in three maps
// (one each for channel announcements, channel updates, and node
// announcements). These maps keep track of unique announcements and
// ensure no announcements are duplicated
// TODO(flaurida): remove announcementBatch in networkHandler after replacing it
type deDupedAnnouncements struct {
// channel announcements are identified by the short channel id field
channelAnnouncements map[lnwire.ShortChannelID]lnwire.Message

// channel updates are identified by the channel update id field
channelUpdates map[ChannelUpdateID]lnwire.Message

// node announcements are identified by node id field
nodeAnnouncements map[autopilot.NodeID]lnwire.Message
}

// Reset operates on deDupedAnnouncements to reset storage of announcements
func (d *deDupedAnnouncements) Reset() {
// storage of each type of announcement (channel anouncements, channel
// updates, node announcements) is set to an empty map where the
// approprate key points to the corresponding lnwire.Message
d.channelAnnouncements = make(map[lnwire.ShortChannelID]lnwire.Message)
d.channelUpdates = make(map[ChannelUpdateID]lnwire.Message)
d.nodeAnnouncements = make(map[autopilot.NodeID]lnwire.Message)
}

// Batch returns the set of de-duplicated announcements to be sent out
// during the next announcement epoch, in the order of channel announcements,
// channel updates, and node announcements
func (d *deDupedAnnouncements) Batch() []lnwire.Message {
// 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
announcements := make([]lnwire.Message, 0, numAnnouncements)

// add the channel announcements to the array first
for _, message := range d.channelAnnouncements {
announcements = append(announcements, message)
}

// then the channel updates
for _, message := range d.channelUpdates {
announcements = append(announcements, message)
}

// then the node announcements
for _, message := range d.nodeAnnouncements {
announcements = append(announcements, message)
}

// return the array of lnwire.messages
return announcements
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion lnwire/short_channel_id.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lnwire

// ShortChannelID represent the set of data which is needed to retrieve all
// ShortChannelID represents the set of data which is needed to retrieve all
// necessary data to validate the channel existence.
type ShortChannelID struct {
// BlockHeight is the height of the block where funding transaction
Expand Down

0 comments on commit d7b8eda

Please sign in to comment.