From 75bf4e020aee139e67d3502ea0a968bdd5f7034d Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 14 Aug 2020 17:21:58 +0200 Subject: [PATCH] rpcserver+order: remove funding shim on subsequent prepare Because we have to always expect an OrderMatchPrepare to be sent multiple times, we need to make sure we clean up our funding shims from the previous pending batch. --- order/manager.go | 4 +++- rpcserver.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/order/manager.go b/order/manager.go index d80c017..2261f27 100644 --- a/order/manager.go +++ b/order/manager.go @@ -275,13 +275,15 @@ func (m *Manager) OrderMatchValidate(batch *Batch) error { // it in case it ends up being the final version. err := m.batchVerifier.Verify(batch) if err != nil { + // This error will lead to us sending an OrderMatchReject + // message and canceling all funding shims we might already have + // set up. return fmt.Errorf("error validating batch: %v", err) } m.pendingBatch = batch atomic.StoreUint32(&m.hasPendingBatch, 1) - // TODO: cancel funding shim of previous pending batch if not nil return nil } diff --git a/rpcserver.go b/rpcserver.go index e3ef71f..aba69dc 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -753,6 +753,30 @@ func (s *rpcServer) handleServerMessage(rpcMsg *clmrpc.ServerAuctionMessage) err rpcLog.Infof("Received PrepareMsg for batch=%x, num_orders=%v", batch.ID[:], len(batch.MatchedOrders)) + // The prepare message can be sent over and over again if the + // batch needs adjustment. Clear all previous shims. + if s.orderManager.HasPendingBatch() { + pendingBatch := s.orderManager.PendingBatch() + err := pendingBatch.CancelPendingFundingShims( + s.lndClient, + func(o order.Nonce) (order.Order, error) { + return s.server.db.GetOrder(o) + }, + ) + if err != nil { + // We can't accept the batch, something went + // wrong. + rpcLog.Errorf("Error clearing previous batch: "+ + "%v", err) + return s.sendRejectBatch(batch, err) + } + + // TODO(guggero): 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. + } + // Do an in-depth verification of the batch. err = s.orderManager.OrderMatchValidate(batch) if err != nil {