From 7f9ab312fb5ceaf29ca7d8024e56c718dab0d994 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 11 Apr 2019 09:06:59 +0200 Subject: [PATCH] loopd: fix nil ptr derefence for monitor Reason for the crash was an unpopulated HtlcAddress field. --- client.go | 61 +++++++++++++++++++++++++++++++++++++++------ cmd/loopd/daemon.go | 30 ++++------------------ cmd/loopd/view.go | 4 +-- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/client.go b/client.go index 82de7aa..c765637 100644 --- a/client.go +++ b/client.go @@ -113,14 +113,61 @@ func NewClient(dbDir string, serverAddress string, insecure bool, return client, cleanup, nil } -// FetchLoopOutSwaps returns a list of all swaps currently in the database. -func (s *Client) FetchLoopOutSwaps() ([]*loopdb.LoopOut, error) { - return s.Store.FetchLoopOutSwaps() -} +// FetchSwaps returns all loop in and out swaps currently in the database. +func (s *Client) FetchSwaps() ([]*SwapInfo, error) { + loopOutSwaps, err := s.Store.FetchLoopOutSwaps() + if err != nil { + return nil, err + } + + loopInSwaps, err := s.Store.FetchLoopInSwaps() + if err != nil { + return nil, err + } + + swaps := make([]*SwapInfo, 0, len(loopInSwaps)+len(loopOutSwaps)) + + for _, swp := range loopOutSwaps { + htlc, err := swap.NewHtlc( + swp.Contract.CltvExpiry, swp.Contract.SenderKey, + swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH, + s.lndServices.ChainParams, + ) + if err != nil { + return nil, err + } + + swaps = append(swaps, &SwapInfo{ + SwapType: TypeOut, + SwapContract: swp.Contract.SwapContract, + State: swp.State(), + SwapHash: swp.Hash, + LastUpdate: swp.LastUpdateTime(), + HtlcAddress: htlc.Address, + }) + } + + for _, swp := range loopInSwaps { + htlc, err := swap.NewHtlc( + swp.Contract.CltvExpiry, swp.Contract.SenderKey, + swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH, + s.lndServices.ChainParams, + ) + if err != nil { + return nil, err + } + + swaps = append(swaps, &SwapInfo{ + SwapType: TypeIn, + SwapContract: swp.Contract.SwapContract, + State: swp.State(), + SwapHash: swp.Hash, + LastUpdate: swp.LastUpdateTime(), + HtlcAddress: htlc.Address, + }) + } -// FetchLoopInSwaps returns a list of all swaps currently in the database. -func (s *Client) FetchLoopInSwaps() ([]*loopdb.LoopIn, error) { - return s.Store.FetchLoopInSwaps() + return swaps, nil } // Run is a blocking call that executes all swaps. Any pending swaps are diff --git a/cmd/loopd/daemon.go b/cmd/loopd/daemon.go index 6d66882..e9008a9 100644 --- a/cmd/loopd/daemon.go +++ b/cmd/loopd/daemon.go @@ -32,6 +32,7 @@ func daemon(config *config) error { config.SwapServer = testnetServer } + // Create an instance of the loop client library. swapClient, cleanup, err := getClient( config.Network, config.SwapServer, config.Insecure, &lnd.LndServices, ) @@ -40,35 +41,14 @@ func daemon(config *config) error { } defer cleanup() - // Before starting the client, build an in-memory view of all swaps. - // This view is used to update newly connected clients with the most - // recent swaps. - loopOutSwaps, err := swapClient.FetchLoopOutSwaps() + // Retrieve all currently existing swaps from the database. + swapsList, err := swapClient.FetchSwaps() if err != nil { return err } - for _, swap := range loopOutSwaps { - swaps[swap.Hash] = loop.SwapInfo{ - SwapType: loop.TypeOut, - SwapContract: swap.Contract.SwapContract, - State: swap.State(), - SwapHash: swap.Hash, - LastUpdate: swap.LastUpdateTime(), - } - } - loopInSwaps, err := swapClient.FetchLoopInSwaps() - if err != nil { - return err - } - for _, swap := range loopInSwaps { - swaps[swap.Hash] = loop.SwapInfo{ - SwapType: loop.TypeIn, - SwapContract: swap.Contract.SwapContract, - State: swap.State(), - SwapHash: swap.Hash, - LastUpdate: swap.LastUpdateTime(), - } + for _, s := range swapsList { + swaps[s.SwapHash] = *s } // Instantiate the loopd gRPC server. diff --git a/cmd/loopd/view.go b/cmd/loopd/view.go index e6f6b84..c2583b0 100644 --- a/cmd/loopd/view.go +++ b/cmd/loopd/view.go @@ -42,7 +42,7 @@ func view(config *config) error { } func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error { - swaps, err := swapClient.FetchLoopOutSwaps() + swaps, err := swapClient.Store.FetchLoopOutSwaps() if err != nil { return err } @@ -88,7 +88,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error { } func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error { - swaps, err := swapClient.FetchLoopInSwaps() + swaps, err := swapClient.Store.FetchLoopInSwaps() if err != nil { return err }