Skip to content

Commit 7c0a2f0

Browse files
committed
looprpc: expose deposit interfaces to withdrawals
1 parent 7daf1d3 commit 7c0a2f0

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

staticaddr/deposit/actions.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,19 @@ func (f *FSM) SweptExpiredDepositAction(_ fsm.EventContext) fsm.EventType {
138138

139139
return fsm.NoOp
140140
}
141+
142+
// WithdrawnDepositAction is the final action after a withdrawal. It signals to
143+
// the manager that the deposit has been swept and the FSM can be removed. It
144+
// also ends the state machine main loop by cancelling its context.
145+
func (f *FSM) WithdrawnDepositAction(_ fsm.EventContext) fsm.EventType {
146+
select {
147+
case <-f.ctx.Done():
148+
return fsm.OnError
149+
150+
default:
151+
f.finalizedDepositChan <- f.deposit.OutPoint
152+
f.ctx.Done()
153+
}
154+
155+
return fsm.NoOp
156+
}

staticaddr/deposit/deposit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ func (d *Deposit) IsPending() bool {
5555

5656
// IsFinal returns true if the deposit is final.
5757
func (d *Deposit) IsFinal() bool {
58-
return d.State == Expired || d.State == Failed
58+
return d.State == Withdrawn || d.State == Expired ||
59+
d.State == Failed
5960
}
6061

6162
// GetRandomDepositID generates a random deposit ID.

staticaddr/deposit/fsm.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var (
2929
var (
3030
Deposited = fsm.StateType("Deposited")
3131

32+
Withdrawing = fsm.StateType("Withdrawing")
33+
34+
Withdrawn = fsm.StateType("Withdrawn")
35+
3236
PublishExpiredDeposit = fsm.StateType("PublishExpiredDeposit")
3337

3438
WaitForExpirySweep = fsm.StateType("WaitForExpirySweep")
@@ -41,6 +45,8 @@ var (
4145
// Events.
4246
var (
4347
OnStart = fsm.EventType("OnStart")
48+
OnWithdraw = fsm.EventType("OnWithdraw")
49+
OnWithdrawn = fsm.EventType("OnWithdrawn")
4450
OnExpiry = fsm.EventType("OnExpiry")
4551
OnExpiryPublished = fsm.EventType("OnExpiryPublished")
4652
OnExpirySwept = fsm.EventType("OnExpirySwept")
@@ -172,8 +178,18 @@ func (f *FSM) DepositStatesV0() fsm.States {
172178
},
173179
Deposited: fsm.State{
174180
Transitions: fsm.Transitions{
175-
OnExpiry: PublishExpiredDeposit,
176-
OnRecover: Deposited,
181+
OnWithdraw: Withdrawing,
182+
OnExpiry: PublishExpiredDeposit,
183+
OnRecover: Deposited,
184+
},
185+
Action: fsm.NoOpAction,
186+
},
187+
Withdrawing: fsm.State{
188+
Transitions: fsm.Transitions{
189+
OnWithdrawn: Withdrawn,
190+
OnRecover: Deposited,
191+
OnExpiry: PublishExpiredDeposit,
192+
fsm.OnError: Deposited,
177193
},
178194
Action: fsm.NoOpAction,
179195
},
@@ -207,6 +223,9 @@ func (f *FSM) DepositStatesV0() fsm.States {
207223
},
208224
Action: f.SweptExpiredDepositAction,
209225
},
226+
Withdrawn: fsm.State{
227+
Action: f.WithdrawnDepositAction,
228+
},
210229
Failed: fsm.State{
211230
Action: fsm.NoOpAction,
212231
},

staticaddr/deposit/manager.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/btcsuite/btcd/wire"
1212
"github.com/lightninglabs/lndclient"
1313
"github.com/lightninglabs/loop"
14+
"github.com/lightninglabs/loop/fsm"
1415
staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
1516
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
1617
"github.com/lightningnetwork/lnd/lnwallet"
@@ -419,3 +420,69 @@ func (m *Manager) finalizeDeposit(outpoint wire.OutPoint) {
419420
delete(m.deposits, outpoint)
420421
m.Unlock()
421422
}
423+
424+
// GetActiveOutpoints returns all active outpoints.
425+
func (m *Manager) GetActiveOutpoints() ([]wire.OutPoint, error) {
426+
m.Lock()
427+
defer m.Unlock()
428+
429+
var outpoints []wire.OutPoint
430+
for o, _ := range m.activeDeposits {
431+
outpoints = append(outpoints, o)
432+
}
433+
434+
return outpoints, nil
435+
}
436+
437+
// AllDepositsActive checks if all deposits referenced by the outpoints are
438+
// deposited and active and returns them if so.
439+
func (m *Manager) AllDepositsActive(outpoints []wire.OutPoint) ([]*Deposit,
440+
bool) {
441+
442+
m.Lock()
443+
defer m.Unlock()
444+
445+
var deposits []*Deposit
446+
for _, o := range outpoints {
447+
if _, ok := m.activeDeposits[o]; !ok {
448+
return nil, false
449+
}
450+
451+
deposit := m.deposits[o]
452+
if deposit.State != Deposited {
453+
return nil, false
454+
}
455+
456+
deposits = append(deposits, m.deposits[o])
457+
}
458+
459+
return deposits, true
460+
}
461+
462+
// TransitionDeposits allows a caller to transition a set of deposits to a new
463+
// state.
464+
func (m *Manager) TransitionDeposits(outpoints []wire.OutPoint,
465+
event fsm.EventType, expectedFinalState fsm.StateType) error {
466+
467+
for _, o := range outpoints {
468+
m.Lock()
469+
sm, ok := m.activeDeposits[o]
470+
m.Unlock()
471+
if !ok {
472+
return fmt.Errorf("deposit not found")
473+
}
474+
475+
err := sm.SendEvent(event, nil)
476+
if err != nil {
477+
return err
478+
}
479+
err = sm.DefaultObserver.WaitForState(
480+
m.runCtx, time.Minute, expectedFinalState,
481+
)
482+
if err != nil {
483+
return err
484+
}
485+
}
486+
487+
return nil
488+
}

0 commit comments

Comments
 (0)