Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peer: more interfaces, refactor unit tests #5067

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions funding/interfaces.go

This file was deleted.

5 changes: 4 additions & 1 deletion htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type ChannelLinkConfig struct {
// TODO(conner): remove after refactoring htlcswitch testing framework.
Switch *Switch

// BestHeight returns the best known height.
BestHeight func() uint32

// ForwardPackets attempts to forward the batch of htlcs through the
// switch. The function returns and error in case it fails to send one or
// more packets. The link's quit signal should be provided to allow
Expand Down Expand Up @@ -2674,7 +2677,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg,
continue
}

heightNow := l.cfg.Switch.BestHeight()
heightNow := l.cfg.BestHeight()

pld, err := chanIterator.HopPayload()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions htlcswitch/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
FwrdingPolicy: globalPolicy,
Peer: alicePeer,
Switch: aliceSwitch,
BestHeight: aliceSwitch.BestHeight,
Circuits: aliceSwitch.CircuitModifier(),
ForwardPackets: aliceSwitch.ForwardPackets,
DecodeHopIterators: decoder.DecodeHopIterators,
Expand Down Expand Up @@ -4454,6 +4455,7 @@ func (h *persistentLinkHarness) restartLink(
FwrdingPolicy: globalPolicy,
Peer: alicePeer,
Switch: aliceSwitch,
BestHeight: aliceSwitch.BestHeight,
Circuits: aliceSwitch.CircuitModifier(),
ForwardPackets: aliceSwitch.ForwardPackets,
DecodeHopIterators: decoder.DecodeHopIterators,
Expand Down
1 change: 1 addition & 0 deletions htlcswitch/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ func (h *hopNetwork) createChannelLink(server, peer *mockServer,
link := NewChannelLink(
ChannelLinkConfig{
Switch: server.htlcSwitch,
BestHeight: server.htlcSwitch.BestHeight,
FwrdingPolicy: h.globalPolicy,
Peer: peer,
Circuits: server.htlcSwitch.CircuitModifier(),
Expand Down
18 changes: 14 additions & 4 deletions lntest/mock/walletcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ func (w *WalletController) ConfirmedBalance(confs int32,
func (w *WalletController) NewAddress(addrType lnwallet.AddressType,
change bool, _ string) (btcutil.Address, error) {

addr, _ := btcutil.NewAddressPubKey(
w.RootKey.PubKey().SerializeCompressed(), &chaincfg.MainNetParams,
)
return addr, nil
key := w.RootKey.PubKey().SerializeCompressed()

if addrType == lnwallet.WitnessPubKey {
witProgram := btcutil.Hash160(key)

addr, err := btcutil.NewAddressWitnessPubKeyHash(
witProgram, &chaincfg.MainNetParams,
)

return addr, err
}

addr, err := btcutil.NewAddressPubKey(key, &chaincfg.MainNetParams)
return addr, err
}

// LastUnusedAddress currently returns dummy values.
Expand Down
111 changes: 73 additions & 38 deletions peer/brontide.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channelnotifier"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/discovery"
"github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/htlcswitch/hodl"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnpeer"
Expand Down Expand Up @@ -121,6 +118,56 @@ type TimestampedError struct {
Timestamp time.Time
}

// ChannelSwitch is an implementation of the MessageSwitch interface that wraps
// htlcswitch.Switch.
type ChannelSwitch struct {
*htlcswitch.Switch
}

// NewChannelSwitch initializes a ChannelSwitch given a raw *htlcswitch.Switch
// pointer.
func NewChannelSwitch(innerSwitch *htlcswitch.Switch) *ChannelSwitch {
return &ChannelSwitch{innerSwitch}
}

// BestHeight returns innerSwitch's BestHeight.
func (s *ChannelSwitch) BestHeight() uint32 {
return s.Switch.BestHeight()
}

// CircuitModifier returns the innerSwitch's CircuitModifier.
func (s *ChannelSwitch) CircuitModifier() htlcswitch.CircuitModifier {
return s.Switch.CircuitModifier()
}

// GetLink retrieves a MessageLink given a ChannelID from the inner Switch.
func (s *ChannelSwitch) GetLink(cid lnwire.ChannelID) (MessageLink, error) {
return s.Switch.GetLink(cid)
}

// InitLink initializes a ChannelLink in the Switch.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MessageLink or ChannelLink?

Copy link
Collaborator Author

@Crypt-iQ Crypt-iQ Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using the underlying htlcswitch method AddLink and NewChannelLink, so I'd say a ChannelLink

func (s *ChannelSwitch) InitLink(linkCfg htlcswitch.ChannelLinkConfig,
lnChan *lnwallet.LightningChannel) error {

link := htlcswitch.NewChannelLink(linkCfg, lnChan)

// Before adding our new link, purge the Switch of any pending or live
// links going by the same channel id. If one is found, we'll shut it
// down to ensure that the mailboxes are only ever under the control of
// one link.
s.RemoveLink(link.ChanID())

// With the ChannelLink created, we'll now notify the Switch so this
// channel can be used to dispatch local payments and also passively
// forward payments.
return s.AddLink(link)
}

// RemoveLink removes a ChannelLink from the Switch.
func (s *ChannelSwitch) RemoveLink(cid lnwire.ChannelID) {
s.Switch.RemoveLink(cid)
}

// Config defines configuration fields that are necessary for a peer object
// to function.
type Config struct {
Expand Down Expand Up @@ -175,9 +222,9 @@ type Config struct {
// ReadPool is the task pool that manages reuse of read buffers.
ReadPool *pool.Read

// Switch is a pointer to the htlcswitch. It is used to setup, get, and
// tear-down ChannelLinks.
Switch *htlcswitch.Switch
// Switch is an implementation of MessageSwitch. It is used to setup,
// get, and tear-down MessageLinks.
Switch MessageSwitch
Crypt-iQ marked this conversation as resolved.
Show resolved Hide resolved

// InterceptSwitch is a pointer to the InterceptableSwitch, a wrapper around
// the regular Switch. We only export it here to pass ForwardPackets to the
Expand All @@ -187,21 +234,21 @@ type Config struct {
// ChannelDB is used to fetch opened channels, and closed channels.
ChannelDB *channeldb.DB

// ChannelGraph is a pointer to the channel graph which is used to
// query information about the set of known active channels.
ChannelGraph *channeldb.ChannelGraph
// ChannelGraph is an implementation of the ChannelGraph interface and
// is used to query information about the set of known active channels.
ChannelGraph ChannelGraph

// ChainArb is used to subscribe to channel events, update contract signals,
// and force close channels.
ChainArb *contractcourt.ChainArbitrator
// ChainArb is used to subscribe to channel events, update contract
// signals, and force close channels.
ChainArb ChainArbitrator

// AuthGossiper is needed so that the Brontide impl can register with the
// gossiper and process remote channel announcements.
AuthGossiper *discovery.AuthenticatedGossiper
// AuthGossiper is needed so that the Brontide impl can register with
// the gossiper and process remote channel announcements.
AuthGossiper Gossiper

// ChanStatusMgr is used to set or un-set the disabled bit in channel
// updates.
ChanStatusMgr *netann.ChanStatusManager
ChanStatusMgr StatusManager

// ChainIO is used to retrieve the best block.
ChainIO lnwallet.BlockChainIO
Expand All @@ -228,9 +275,9 @@ type Config struct {
// the Brontide.
RoutingPolicy htlcswitch.ForwardingPolicy

// Sphinx is used when setting up ChannelLinks so they can decode sphinx
// onion blobs.
Sphinx *hop.OnionProcessor
// Sphinx is used when setting up ChannelLinks so they can decode
// sphinx onion blobs.
Sphinx Sphinx

// WitnessBeacon is used when setting up ChannelLinks so they can add any
// preimages that they learn.
Expand Down Expand Up @@ -273,8 +320,8 @@ type Config struct {
FetchLastChanUpdate func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate,
error)

// FundingManager is an implementation of the funding.Controller interface.
FundingManager funding.Controller
// FundingManager handles state relating to the funding process.
FundingManager Funding

// Hodl is used when creating ChannelLinks to specify HodlFlags as
// breakpoints in dev builds.
Expand Down Expand Up @@ -813,7 +860,7 @@ func (p *Brontide) addLink(chanPoint *wire.OutPoint,
FetchLastChannelUpdate: p.cfg.FetchLastChanUpdate,
HodlMask: p.cfg.Hodl.Mask(),
Registry: p.cfg.Invoices,
Switch: p.cfg.Switch,
BestHeight: p.cfg.Switch.BestHeight,
Crypt-iQ marked this conversation as resolved.
Show resolved Hide resolved
Circuits: p.cfg.Switch.CircuitModifier(),
ForwardPackets: p.cfg.InterceptSwitch.ForwardPackets,
FwrdingPolicy: *forwardingPolicy,
Expand Down Expand Up @@ -841,18 +888,7 @@ func (p *Brontide) addLink(chanPoint *wire.OutPoint,
HtlcNotifier: p.cfg.HtlcNotifier,
}

link := htlcswitch.NewChannelLink(linkCfg, lnChan)

// Before adding our new link, purge the switch of any pending or live
// links going by the same channel id. If one is found, we'll shut it
// down to ensure that the mailboxes are only ever under the control of
// one link.
p.cfg.Switch.RemoveLink(link.ChanID())

// With the channel link created, we'll now notify the htlc switch so
// this channel can be used to dispatch local payments and also
// passively forward payments.
return p.cfg.Switch.AddLink(link)
return p.cfg.Switch.InitLink(linkCfg, lnChan)
}

// maybeSendNodeAnn sends our node announcement to the remote peer if at least
Expand Down Expand Up @@ -1139,10 +1175,9 @@ func (ms *msgStream) AddMsg(msg lnwire.Message) {
}

// waitUntilLinkActive waits until the target link is active and returns a
// ChannelLink to pass messages to. It accomplishes this by subscribing to
// MessageLink to pass messages to. It accomplishes this by subscribing to
// an ActiveLinkEvent which is emitted by the link when it first starts up.
func waitUntilLinkActive(p *Brontide,
cid lnwire.ChannelID) htlcswitch.ChannelLink {
func waitUntilLinkActive(p *Brontide, cid lnwire.ChannelID) MessageLink {

// Subscribe to receive channel events.
//
Expand Down Expand Up @@ -1211,7 +1246,7 @@ func waitUntilLinkActive(p *Brontide,
// lookups.
func newChanMsgStream(p *Brontide, cid lnwire.ChannelID) *msgStream {

var chanLink htlcswitch.ChannelLink
var chanLink MessageLink

apply := func(msg lnwire.Message) {
// This check is fine because if the link no longer exists, it will
Expand Down
Loading