funding: create waitForPeerOnline helper

Simplifies and deduplicates the code.
This commit is contained in:
Matt Morehouse committed 2022-10-11 11:59:01 -05:00
1 parent b387e2c718
commit 768854aac9
1 file changed
+44 -42
+44 -42
View File
@@ -2458,18 +2458,22 @@ func (f *Manager) fundingTimeout(c *channeldb.OpenChannel,
go func() { go func() {
defer f.wg.Done() defer f.wg.Done()
peerChan := make(chan lnpeer.Peer, 1) peer, err := f.waitForPeerOnline(c.IdentityPub)
var peerKey [33]byte switch err {
copy(peerKey[:], c.IdentityPub.SerializeCompressed()) // We're already shutting down, so we can just return.
case ErrFundingManagerShuttingDown:
f.cfg.NotifyWhenOnline(peerKey, peerChan)
var peer lnpeer.Peer
select {
case peer = <-peerChan:
case <-f.quit:
return return
// nil error means we continue on.
case nil:
// For unexpected errors, we print the error and still try to
// fail the funding flow.
default:
log.Errorf("Unexpected error while waiting for peer "+
"to come online: %v", err)
} }
// TODO(halseth): should this send be made // TODO(halseth): should this send be made
// reliable? // reliable?
@@ -2860,14 +2864,9 @@ func (f *Manager) sendFundingLocked(completeChan *channeldb.OpenChannel,
// send fundingLocked until we succeed, or the fundingManager is shut // send fundingLocked until we succeed, or the fundingManager is shut
// down. // down.
for { for {
connected := make(chan lnpeer.Peer, 1) peer, err := f.waitForPeerOnline(completeChan.IdentityPub)
f.cfg.NotifyWhenOnline(peerKey, connected) if err != nil {
return err
var peer lnpeer.Peer
select {
case peer = <-connected:
case <-f.quit:
return ErrFundingManagerShuttingDown
} }
localAlias := peer.LocalFeatures().HasFeature( localAlias := peer.LocalFeatures().HasFeature(
@@ -2943,18 +2942,9 @@ func (f *Manager) receivedFundingLocked(node *btcec.PublicKey,
default: default:
} }
// Check whether the peer is online. If it's not, we'll wait for them // Avoid a tight loop if peer is offline.
// to come online before proceeding. This is to avoid a tight loop if if _, err := f.waitForPeerOnline(node); err != nil {
// they're offline. return false, err
connected := make(chan lnpeer.Peer, 1)
var peerKey [33]byte
copy(peerKey[:], node.SerializeCompressed())
f.cfg.NotifyWhenOnline(peerKey, connected)
select {
case <-connected:
case <-f.quit:
return false, ErrFundingManagerShuttingDown
} }
channel, err := f.cfg.FindChannel(node, chanID) channel, err := f.cfg.FindChannel(node, chanID)
@@ -3088,18 +3078,9 @@ func (f *Manager) annAfterSixConfs(completeChan *channeldb.OpenChannel,
log.Debugf("Will not announce private channel %v.", log.Debugf("Will not announce private channel %v.",
shortChanID.ToUint64()) shortChanID.ToUint64())
peerChan := make(chan lnpeer.Peer, 1) peer, err := f.waitForPeerOnline(completeChan.IdentityPub)
if err != nil {
var peerKey [33]byte return err
copy(peerKey[:], completeChan.IdentityPub.SerializeCompressed())
f.cfg.NotifyWhenOnline(peerKey, peerChan)
var peer lnpeer.Peer
select {
case peer = <-peerChan:
case <-f.quit:
return ErrFundingManagerShuttingDown
} }
nodeAnn, err := f.cfg.CurrentNodeAnnouncement() nodeAnn, err := f.cfg.CurrentNodeAnnouncement()
@@ -4512,3 +4493,24 @@ func (f *Manager) selectShutdownScript(taprootOK bool,
return txscript.PayToAddrScript(addr) return txscript.PayToAddrScript(addr)
} }
// waitForPeerOnline blocks until the peer specified by peerPubkey comes online
// and then returns the online peer.
func (f *Manager) waitForPeerOnline(peerPubkey *btcec.PublicKey) (lnpeer.Peer,
error) {
peerChan := make(chan lnpeer.Peer, 1)
var peerKey [33]byte
copy(peerKey[:], peerPubkey.SerializeCompressed())
f.cfg.NotifyWhenOnline(peerKey, peerChan)
var peer lnpeer.Peer
select {
case peer = <-peerChan:
case <-f.quit:
return peer, ErrFundingManagerShuttingDown
}
return peer, nil
}