funding: inform aux controller about channel ready/finalize

This commit is contained in:
Oliver Gugger 2024-05-17 12:40:22 +02:00
parent 5c854a2f53
commit 0b64b80642
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -1921,6 +1921,8 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
log.Debugf("Remote party accepted commitment rendering params: %v", log.Debugf("Remote party accepted commitment rendering params: %v",
lnutils.SpewLogClosure(params)) lnutils.SpewLogClosure(params))
reservation.SetState(lnwallet.SentAcceptChannel)
// With the initiator's contribution recorded, respond with our // With the initiator's contribution recorded, respond with our
// contribution in the next message of the workflow. // contribution in the next message of the workflow.
fundingAccept := lnwire.AcceptChannel{ fundingAccept := lnwire.AcceptChannel{
@ -1981,6 +1983,10 @@ func (f *Manager) funderProcessAcceptChannel(peer lnpeer.Peer,
// Update the timestamp once the fundingAcceptMsg has been handled. // Update the timestamp once the fundingAcceptMsg has been handled.
defer resCtx.updateTimestamp() defer resCtx.updateTimestamp()
if resCtx.reservation.State() != lnwallet.SentOpenChannel {
return
}
log.Infof("Recv'd fundingResponse for pending_id(%x)", log.Infof("Recv'd fundingResponse for pending_id(%x)",
pendingChanID[:]) pendingChanID[:])
@ -2406,6 +2412,8 @@ func (f *Manager) continueFundingAccept(resCtx *reservationWithCtx,
} }
} }
resCtx.reservation.SetState(lnwallet.SentFundingCreated)
if err := resCtx.peer.SendMessage(true, fundingCreated); err != nil { if err := resCtx.peer.SendMessage(true, fundingCreated); err != nil {
log.Errorf("Unable to send funding complete message: %v", err) log.Errorf("Unable to send funding complete message: %v", err)
f.failFundingFlow(resCtx.peer, cid, err) f.failFundingFlow(resCtx.peer, cid, err)
@ -2441,6 +2449,10 @@ func (f *Manager) fundeeProcessFundingCreated(peer lnpeer.Peer,
log.Infof("completing pending_id(%x) with ChannelPoint(%v)", log.Infof("completing pending_id(%x) with ChannelPoint(%v)",
pendingChanID[:], fundingOut) pendingChanID[:], fundingOut)
if resCtx.reservation.State() != lnwallet.SentAcceptChannel {
return
}
// Create the channel identifier without setting the active channel ID. // Create the channel identifier without setting the active channel ID.
cid := newChanIdentifier(pendingChanID) cid := newChanIdentifier(pendingChanID)
@ -2700,6 +2712,14 @@ func (f *Manager) funderProcessFundingSigned(peer lnpeer.Peer,
return return
} }
if resCtx.reservation.State() != lnwallet.SentFundingCreated {
err := fmt.Errorf("unable to find reservation for chan_id=%x",
msg.ChanID)
f.failFundingFlow(peer, cid, err)
return
}
// Create an entry in the local discovery map so we can ensure that we // Create an entry in the local discovery map so we can ensure that we
// process the channel confirmation fully before we receive a // process the channel confirmation fully before we receive a
// channel_ready message. // channel_ready message.
@ -2795,6 +2815,21 @@ func (f *Manager) funderProcessFundingSigned(peer lnpeer.Peer,
} }
} }
// Before we proceed, if we have a funding hook that wants a
// notification that it's safe to broadcast the funding transaction,
// then we'll send that now.
err = fn.MapOptionZ(
f.cfg.AuxFundingController,
func(controller AuxFundingController) error {
return controller.ChannelFinalized(cid.tempChanID)
},
)
if err != nil {
log.Errorf("Failed to inform aux funding controller about "+
"ChannelPoint(%v) being finalized: %v", fundingPoint,
err)
}
// Now that we have a finalized reservation for this funding flow, // Now that we have a finalized reservation for this funding flow,
// we'll send the to be active channel to the ChainArbitrator so it can // we'll send the to be active channel to the ChainArbitrator so it can
// watch for any on-chain actions before the channel has fully // watch for any on-chain actions before the channel has fully
@ -4043,6 +4078,26 @@ func (f *Manager) handleChannelReady(peer lnpeer.Peer, //nolint:funlen
PubNonce: remoteNonce, PubNonce: remoteNonce,
}), }),
) )
// Inform the aux funding controller that the liquidity in the
// custom channel is now ready to be advertised. We potentially
// haven't sent our own channel ready message yet, but other
// than that the channel is ready to count toward available
// liquidity.
err = fn.MapOptionZ(
f.cfg.AuxFundingController,
func(controller AuxFundingController) error {
return controller.ChannelReady(
lnwallet.NewAuxChanState(channel),
)
},
)
if err != nil {
cid := newChanIdentifier(msg.ChanID)
f.sendWarning(peer, cid, err)
return
}
} }
// The channel_ready message contains the next commitment point we'll // The channel_ready message contains the next commitment point we'll
@ -4129,6 +4184,19 @@ func (f *Manager) handleChannelReadyReceived(channel *channeldb.OpenChannel,
log.Debugf("Channel(%v) with ShortChanID %v: successfully "+ log.Debugf("Channel(%v) with ShortChanID %v: successfully "+
"added to graph", chanID, scid) "added to graph", chanID, scid)
err = fn.MapOptionZ(
f.cfg.AuxFundingController,
func(controller AuxFundingController) error {
return controller.ChannelReady(
lnwallet.NewAuxChanState(channel),
)
},
)
if err != nil {
return fmt.Errorf("failed notifying aux funding controller "+
"about channel ready: %w", err)
}
// Give the caller a final update notifying them that the channel is // Give the caller a final update notifying them that the channel is
fundingPoint := channel.FundingOutpoint fundingPoint := channel.FundingOutpoint
cp := &lnrpc.ChannelPoint{ cp := &lnrpc.ChannelPoint{
@ -4907,6 +4975,8 @@ func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
log.Infof("Starting funding workflow with %v for pending_id(%x), "+ log.Infof("Starting funding workflow with %v for pending_id(%x), "+
"committype=%v", msg.Peer.Address(), chanID, commitType) "committype=%v", msg.Peer.Address(), chanID, commitType)
reservation.SetState(lnwallet.SentOpenChannel)
fundingOpen := lnwire.OpenChannel{ fundingOpen := lnwire.OpenChannel{
ChainHash: *f.cfg.Wallet.Cfg.NetParams.GenesisHash, ChainHash: *f.cfg.Wallet.Cfg.NetParams.GenesisHash,
PendingChannelID: chanID, PendingChannelID: chanID,