Skip to content

Commit 6684727

Browse files
authored
Merge pull request #41 from joostjager/monitor-crash
loopd: fix nil ptr derefence for monitor
2 parents 806db5b + 7f9ab31 commit 6684727

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

client.go

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,61 @@ func NewClient(dbDir string, serverAddress string, insecure bool,
113113
return client, cleanup, nil
114114
}
115115

116-
// FetchLoopOutSwaps returns a list of all swaps currently in the database.
117-
func (s *Client) FetchLoopOutSwaps() ([]*loopdb.LoopOut, error) {
118-
return s.Store.FetchLoopOutSwaps()
119-
}
116+
// FetchSwaps returns all loop in and out swaps currently in the database.
117+
func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
118+
loopOutSwaps, err := s.Store.FetchLoopOutSwaps()
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
loopInSwaps, err := s.Store.FetchLoopInSwaps()
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
swaps := make([]*SwapInfo, 0, len(loopInSwaps)+len(loopOutSwaps))
129+
130+
for _, swp := range loopOutSwaps {
131+
htlc, err := swap.NewHtlc(
132+
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
133+
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
134+
s.lndServices.ChainParams,
135+
)
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
swaps = append(swaps, &SwapInfo{
141+
SwapType: TypeOut,
142+
SwapContract: swp.Contract.SwapContract,
143+
State: swp.State(),
144+
SwapHash: swp.Hash,
145+
LastUpdate: swp.LastUpdateTime(),
146+
HtlcAddress: htlc.Address,
147+
})
148+
}
149+
150+
for _, swp := range loopInSwaps {
151+
htlc, err := swap.NewHtlc(
152+
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
153+
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH,
154+
s.lndServices.ChainParams,
155+
)
156+
if err != nil {
157+
return nil, err
158+
}
159+
160+
swaps = append(swaps, &SwapInfo{
161+
SwapType: TypeIn,
162+
SwapContract: swp.Contract.SwapContract,
163+
State: swp.State(),
164+
SwapHash: swp.Hash,
165+
LastUpdate: swp.LastUpdateTime(),
166+
HtlcAddress: htlc.Address,
167+
})
168+
}
120169

121-
// FetchLoopInSwaps returns a list of all swaps currently in the database.
122-
func (s *Client) FetchLoopInSwaps() ([]*loopdb.LoopIn, error) {
123-
return s.Store.FetchLoopInSwaps()
170+
return swaps, nil
124171
}
125172

126173
// Run is a blocking call that executes all swaps. Any pending swaps are

cmd/loopd/daemon.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func daemon(config *config) error {
3232
config.SwapServer = testnetServer
3333
}
3434

35+
// Create an instance of the loop client library.
3536
swapClient, cleanup, err := getClient(
3637
config.Network, config.SwapServer, config.Insecure, &lnd.LndServices,
3738
)
@@ -40,35 +41,14 @@ func daemon(config *config) error {
4041
}
4142
defer cleanup()
4243

43-
// Before starting the client, build an in-memory view of all swaps.
44-
// This view is used to update newly connected clients with the most
45-
// recent swaps.
46-
loopOutSwaps, err := swapClient.FetchLoopOutSwaps()
44+
// Retrieve all currently existing swaps from the database.
45+
swapsList, err := swapClient.FetchSwaps()
4746
if err != nil {
4847
return err
4948
}
50-
for _, swap := range loopOutSwaps {
51-
swaps[swap.Hash] = loop.SwapInfo{
52-
SwapType: loop.TypeOut,
53-
SwapContract: swap.Contract.SwapContract,
54-
State: swap.State(),
55-
SwapHash: swap.Hash,
56-
LastUpdate: swap.LastUpdateTime(),
57-
}
58-
}
5949

60-
loopInSwaps, err := swapClient.FetchLoopInSwaps()
61-
if err != nil {
62-
return err
63-
}
64-
for _, swap := range loopInSwaps {
65-
swaps[swap.Hash] = loop.SwapInfo{
66-
SwapType: loop.TypeIn,
67-
SwapContract: swap.Contract.SwapContract,
68-
State: swap.State(),
69-
SwapHash: swap.Hash,
70-
LastUpdate: swap.LastUpdateTime(),
71-
}
50+
for _, s := range swapsList {
51+
swaps[s.SwapHash] = *s
7252
}
7353

7454
// Instantiate the loopd gRPC server.

cmd/loopd/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func view(config *config) error {
4242
}
4343

4444
func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
45-
swaps, err := swapClient.FetchLoopOutSwaps()
45+
swaps, err := swapClient.Store.FetchLoopOutSwaps()
4646
if err != nil {
4747
return err
4848
}
@@ -88,7 +88,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
8888
}
8989

9090
func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
91-
swaps, err := swapClient.FetchLoopInSwaps()
91+
swaps, err := swapClient.Store.FetchLoopInSwaps()
9292
if err != nil {
9393
return err
9494
}

0 commit comments

Comments
 (0)