github.com/decred/dcrlnd@v0.7.6/netann/channel_announcement.go (about)

     1  package netann
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/decred/dcrlnd/channeldb"
     7  	"github.com/decred/dcrlnd/lnwire"
     8  )
     9  
    10  // CreateChanAnnouncement is a helper function which creates all channel
    11  // announcements given the necessary channel related database items. This
    12  // function is used to transform out database structs into the corresponding wire
    13  // structs for announcing new channels to other peers, or simply syncing up a
    14  // peer's initial routing table upon connect.
    15  func CreateChanAnnouncement(chanProof *channeldb.ChannelAuthProof,
    16  	chanInfo *channeldb.ChannelEdgeInfo,
    17  	e1, e2 *channeldb.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement,
    18  	*lnwire.ChannelUpdate, *lnwire.ChannelUpdate, error) {
    19  
    20  	// First, using the parameters of the channel, along with the channel
    21  	// authentication chanProof, we'll create re-create the original
    22  	// authenticated channel announcement.
    23  	chanID := lnwire.NewShortChanIDFromInt(chanInfo.ChannelID)
    24  	chanAnn := &lnwire.ChannelAnnouncement{
    25  		ShortChannelID:  chanID,
    26  		NodeID1:         chanInfo.NodeKey1Bytes,
    27  		NodeID2:         chanInfo.NodeKey2Bytes,
    28  		ChainHash:       chanInfo.ChainHash,
    29  		DecredKey1:      chanInfo.DecredKey1Bytes,
    30  		DecredKey2:      chanInfo.DecredKey2Bytes,
    31  		Features:        lnwire.NewRawFeatureVector(),
    32  		ExtraOpaqueData: chanInfo.ExtraOpaqueData,
    33  	}
    34  
    35  	err := chanAnn.Features.Decode(bytes.NewReader(chanInfo.Features))
    36  	if err != nil {
    37  		return nil, nil, nil, err
    38  	}
    39  	chanAnn.DecredSig1, err = lnwire.NewSigFromRawSignature(
    40  		chanProof.DecredSig1Bytes,
    41  	)
    42  	if err != nil {
    43  		return nil, nil, nil, err
    44  	}
    45  	chanAnn.DecredSig2, err = lnwire.NewSigFromRawSignature(
    46  		chanProof.DecredSig2Bytes,
    47  	)
    48  	if err != nil {
    49  		return nil, nil, nil, err
    50  	}
    51  	chanAnn.NodeSig1, err = lnwire.NewSigFromRawSignature(
    52  		chanProof.NodeSig1Bytes,
    53  	)
    54  	if err != nil {
    55  		return nil, nil, nil, err
    56  	}
    57  	chanAnn.NodeSig2, err = lnwire.NewSigFromRawSignature(
    58  		chanProof.NodeSig2Bytes,
    59  	)
    60  	if err != nil {
    61  		return nil, nil, nil, err
    62  	}
    63  
    64  	// We'll unconditionally queue the channel's existence chanProof as it
    65  	// will need to be processed before either of the channel update
    66  	// networkMsgs.
    67  
    68  	// Since it's up to a node's policy as to whether they advertise the
    69  	// edge in a direction, we don't create an advertisement if the edge is
    70  	// nil.
    71  	var edge1Ann, edge2Ann *lnwire.ChannelUpdate
    72  	if e1 != nil {
    73  		edge1Ann, err = ChannelUpdateFromEdge(chanInfo, e1)
    74  		if err != nil {
    75  			return nil, nil, nil, err
    76  		}
    77  	}
    78  	if e2 != nil {
    79  		edge2Ann, err = ChannelUpdateFromEdge(chanInfo, e2)
    80  		if err != nil {
    81  			return nil, nil, nil, err
    82  		}
    83  	}
    84  
    85  	return chanAnn, edge1Ann, edge2Ann, nil
    86  }