funding+rpcserver: extract channel cleanup into funding mgr

Since it's ultimately the funding manager's job to clean up
non-finalized pending channels and/or funding shims, we move that
functionality into the manager now.
This commit is contained in:
Oliver Gugger 2020-12-07 14:23:58 +01:00
parent 4a728a864c
commit f936a70317
No known key found for this signature in database
GPG key ID: 8E4256593F177720
2 changed files with 44 additions and 25 deletions

View file

@ -607,6 +607,41 @@ func (m *Manager) BatchChannelSetup(batch *order.Batch,
return channelKeys, nil
}
// RemovePendingBatchArtifacts removes any funding shims or pending channels
// from a batch that was never finalized. Some non-terminal errors are logged
// only and not returned. Therefore if this method returns an error, it should
// be handled as terminal error.
func (m *Manager) RemovePendingBatchArtifacts(
matchedOrders map[order.Nonce][]*order.MatchedOrder,
batchTx *wire.MsgTx) error {
err := CancelPendingFundingShims(
matchedOrders, m.BaseClient, m.DB.GetOrder,
)
if err != nil {
// CancelPendingFundingShims only returns hard errors that
// justify us rejecting the batch or logging an actual error.
return fmt.Errorf("error canceling funding shims "+
"from previous pending batch: %v", err)
}
// Also abandon any channels that might still be pending
// from a previous round of the same batch or a previous
// batch that we didn't make it into the final round.
err = AbandonCanceledChannels(
matchedOrders, batchTx, m.WalletKit, m.BaseClient,
m.DB.GetOrder,
)
if err != nil {
// AbandonCanceledChannels also only returns hard errors that
// justify us rejecting the batch or logging an actual error.
return fmt.Errorf("error abandoning channels from "+
"previous pending batch: %v", err)
}
return nil
}
// connectToMatchedTrader attempts to connect to a trader that we've had an
// order matched with, on all available addresses.
func (m *Manager) connectToMatchedTrader(ctx context.Context,

View file

@ -374,35 +374,19 @@ func (s *rpcServer) handleServerMessage(rpcMsg *poolrpc.ServerAuctionMessage) er
}
// The prepare message can be sent over and over again if the
// batch needs adjustment. Clear all previous shims.
// batch needs adjustment. Clear all previous shims and channels
// that will never complete because the funding TX they refer to
// will never be published.
if s.orderManager.HasPendingBatch() {
pendingBatch := s.orderManager.PendingBatch()
orderFetcher := s.server.db.GetOrder
err := funding.CancelPendingFundingShims(
pendingBatch.MatchedOrders, s.lndClient,
orderFetcher,
err = s.fundingManager.RemovePendingBatchArtifacts(
pendingBatch.MatchedOrders, pendingBatch.BatchTX,
)
if err != nil {
// CancelPendingFundingShims only returns hard
// errors that justify us rejecting the batch.
rpcLog.Errorf("Error clearing previous batch: "+
"%v", err)
return s.sendRejectBatch(batch, err)
}
// Also abandon any channels that might still be pending
// from a previous round of the same batch or a previous
// batch that we didn't make it into the final round.
err = funding.AbandonCanceledChannels(
pendingBatch.MatchedOrders,
pendingBatch.BatchTX, s.lndServices.WalletKit,
s.lndClient, orderFetcher,
)
if err != nil {
// AbandonCanceledChannels only returns hard
// errors that justify us rejecting the batch.
rpcLog.Errorf("Error abandoning channels from "+
"last batch: %v", err)
// The above method only returns hard errors
// that justify us rejecting the batch.
rpcLog.Errorf("Error clearing previous batch "+
"artifacts: %v", err)
return s.sendRejectBatch(batch, err)
}
}