diff --git a/funding/manager.go b/funding/manager.go index c4454b6..b35dee2 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -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, diff --git a/rpcserver.go b/rpcserver.go index c69df6c..fa92863 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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) } }