-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fundingmanager: send messages directly to peers #1505
fundingmanager: send messages directly to peers #1505
Conversation
f97bf6e
to
08ea215
Compare
discovery/gossiper.go
Outdated
@@ -2399,11 +2399,11 @@ func (d *AuthenticatedGossiper) sendAnnSigReliably( | |||
"to peer(%x): %v. Will retry when online.", | |||
remotePeer.SerializeCompressed(), err) | |||
|
|||
connected := make(chan struct{}) | |||
d.cfg.NotifyWhenOnline(remotePeer, connected) | |||
peerChan := make(chan lnpeer.Peer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer this as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager_test.go
Outdated
f.cfg.NotifyWhenOnline = func(peer *btcec.PublicKey, | ||
connectedChan chan<- lnpeer.Peer) { | ||
|
||
go func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will these tests pass w/o the goroutine now that we're buffering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Fixed.
peer.go
Outdated
|
||
// AddNewChannel adds a new channel to the peer. The channel should fail to be | ||
// added if the fundingQuit channel is closed. | ||
func (p *peer) AddNewChannel(channel *lnwallet.LightningChannel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
discovery/gossiper.go
Outdated
@@ -107,7 +107,7 @@ type Config struct { | |||
// 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. | |||
NotifyWhenOnline func(peer *btcec.PublicKey, connectedChan chan<- struct{}) | |||
NotifyWhenOnline func(peer *btcec.PublicKey, peerChan chan<- lnpeer.Peer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps make a note in the godoc that peerChan
MUST be buffered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
08ea215
to
4a7b000
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Fairly familiar with this PR as spent a good amount of time working through it w/ @wpaulino. LGTM, but would appreciate feedback from others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a first pass, and like what I see! I dig the commit format you have been using lately (small, independent commits), makes reviewing a joy!
Main request I have is to try to simplify the logic sending to the peer when it might not be online, avoiding the nil
parameter.
peer.go
Outdated
p.server.fundingMgr.processFundingError(msg, p.addr) | ||
case p.server.fundingMgr.IsPendingChannel( | ||
msg.ChanID, p.addr.IdentityKey, | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, weird format? Maybe just do key := addr.IdentityKey
to keep on one line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager.go
Outdated
lnpeer.Peer | ||
|
||
// AddNewChannel adds a new channel to the peer. The channel should fail | ||
// to be added if the fundingQuit channel is closed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cancel
would be a better name for the chan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
peer.go
Outdated
@@ -223,6 +223,9 @@ func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server, | |||
return p, nil | |||
} | |||
|
|||
// A compile-time check to ensure that peer satisfies the FundingPeer interface. | |||
var _ FundingPeer = (*peer)(nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this has the unfortunate effect of keeping the dependency cycle peer<->fundingmanager
. Why not make AddNewChannel
part of the regular Peer
interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager.go
Outdated
@@ -630,7 +633,9 @@ func (f *fundingManager) Start() error { | |||
go func(dbChan *channeldb.OpenChannel) { | |||
defer f.wg.Done() | |||
|
|||
err := f.handleFundingConfirmation(dbChan, shortChanID) | |||
err := f.handleFundingConfirmation( | |||
nil, dbChan, shortChanID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if passing nil
here is the best option, can make it easy to make errors down the line. Instead, can you fetch the peer (NotifyWhenOnline
) right before it is used, or make sure that it will never be passed in as nil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it so that we fetch the peer before handleFundingConfirmation
.
fundingmanager.go
Outdated
@@ -337,7 +337,7 @@ type fundingConfig struct { | |||
// the channel to the ChainArbitrator so it can watch for any on-chain | |||
// events related to the channel. We also provide the address of the | |||
// node we're establishing a channel with for reconnection purposes. | |||
WatchNewChannel func(*channeldb.OpenChannel, *lnwire.NetAddress) error | |||
WatchNewChannel func(*channeldb.OpenChannel, *btcec.PublicKey) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: update comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
discovery/gossiper.go
Outdated
@@ -9,7 +9,7 @@ import ( | |||
"sync/atomic" | |||
"time" | |||
|
|||
"github.com/coreos/bbolt" | |||
bolt "github.com/coreos/bbolt" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add it because it wouldn't compile otherwise. Although it no longer seems to be a problem...
fundingmanager.go
Outdated
fndgLog.Warnf("Unable to send fundingLocked "+ | ||
"to peer %x: %v", | ||
peerKey.SerializeCompressed(), err) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can peer
be nil
here? If not, this can be written
err = sendMessage(..)
if err == nil {
break
}
print(err)
... go on ...
EDIT: see other comment about nil
peer
4a7b000
to
a8a9e26
Compare
peer.go
Outdated
// before we close the channel barrier corresponding to the channel. | ||
select { | ||
case <-newChanDone: | ||
case <-cancel: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if cancelling at this point will actually stop the channel from being added to the peer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not much we can do after the request has already been sent. Just ended up removing this instead as this wasn't being handled previously anyway.
peer.go
Outdated
// handle the error. | ||
case p.server.fundingMgr.IsPendingChannel(msg.ChanID, p.addr): | ||
p.server.fundingMgr.processFundingError(msg, p.addr) | ||
// If the channel ID for the error message corresponds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager.go
Outdated
f.cfg.NotifyWhenOnline(ch.IdentityPub, peerChan) | ||
|
||
select { | ||
case peer := <-peerChan: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do
var peer lnpeer.Peer
select {
case peer = <-peerChan:
case <-f.quit:
return
}
err := f.handleFundingConfirmation ....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager.go
Outdated
f.cfg.NotifyWhenOnline(dbChan.IdentityPub, peerChan) | ||
|
||
select { | ||
case peer := <-peerChan: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
fundingmanager.go
Outdated
return | ||
case <-newChanDone: // Fallthrough if we're not quitting. | ||
} | ||
fmsg.peer.AddNewChannel(channel, f.quit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to let this return an error that we can log here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
// sender. Since the fundingManager now has a reference to peers itself, | ||
// alice.sendMessage will be triggered when Bob's funding manager | ||
// attempts to send a message to Alice and vice versa. | ||
alice.remotePeer = bob |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
a8a9e26
to
6f618f0
Compare
needs rebase |
In this commit, we modify the existing message sending functionality within the fundingmanager. Due to each mesage send requiring to hold the server's lock to retrieve the peer, we might run into a case where the lock is held for a larger than usual amount of time and would therefore block on sending the message within the fundingmanager. We remedy this by taking a similar approach to some recent changes within the gossiper. We now keep track of each peer within the internal fundingmanager messages and send messages directly to them.
callbacks The FindPeer and SendToPeer callbacks are no longer needed within the fundingManager due to the previous commit allowing us to send messages to peers directly.
6f618f0
to
d54d41e
Compare
Rebased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🏌🏾
Really dig the incremental step towards removing all references to *peer
within the main package all together!
In this PR, we introduce a similar optimization to the one done in #1345. Currently, each call to
server.SendToPeer
acquires the server's mutex to obtain the active peer. We work around this by passing thepeer
struct itself within the internalfundingManager
messages.Fixes #1495.