Skip to content

Commit 715dec4

Browse files
committed
looprpc: expose deposit interfaces to withdrawals
1 parent ceb62aa commit 715dec4

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-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 == SweptExpiredDeposit || d.State == Failed
58+
return d.State == Withdrawn || d.State == SweptExpiredDeposit ||
59+
d.State == Failed
5960
}
6061

6162
// GetRandomDepositID generates a random deposit ID.

staticaddr/deposit/fsm.go

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

32+
Withdrawal = fsm.StateType("Withdrawal")
33+
Withdrawn = fsm.StateType("Withdrawn")
34+
3235
PublishExpiredDeposit = fsm.StateType("PublishExpiredDeposit")
3336
WaitForExpirySweep = fsm.StateType("WaitForExpirySweep")
3437

@@ -40,6 +43,8 @@ var (
4043
// Events.
4144
var (
4245
OnStart = fsm.EventType("OnStart")
46+
OnWithdraw = fsm.EventType("OnWithdraw")
47+
OnWithdrawn = fsm.EventType("OnWithdrawn")
4348
OnExpiry = fsm.EventType("OnExpiry")
4449
OnExpiryPublished = fsm.EventType("OnExpiryPublished")
4550
OnExpirySwept = fsm.EventType("OnExpirySwept")
@@ -171,8 +176,18 @@ func (f *FSM) DepositStatesV0() fsm.States {
171176
},
172177
Deposited: fsm.State{
173178
Transitions: fsm.Transitions{
174-
OnExpiry: PublishExpiredDeposit,
175-
OnRecover: Deposited,
179+
OnWithdraw: Withdrawal,
180+
OnExpiry: PublishExpiredDeposit,
181+
OnRecover: Deposited,
182+
},
183+
Action: fsm.NoOpAction,
184+
},
185+
Withdrawal: fsm.State{
186+
Transitions: fsm.Transitions{
187+
OnWithdrawn: Withdrawn,
188+
OnRecover: Deposited,
189+
OnExpiry: PublishExpiredDeposit,
190+
fsm.OnError: Deposited,
176191
},
177192
Action: fsm.NoOpAction,
178193
},
@@ -200,6 +215,9 @@ func (f *FSM) DepositStatesV0() fsm.States {
200215
},
201216
Action: f.WaitForExpirySweepAction,
202217
},
218+
Withdrawn: fsm.State{
219+
Action: f.WithdrawnDepositAction,
220+
},
203221
SweptExpiredDeposit: fsm.State{
204222
Transitions: fsm.Transitions{
205223
OnExpiry: SweptExpiredDeposit,

staticaddr/deposit/manager.go

Lines changed: 65 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,67 @@ 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+
sm, ok := m.activeDeposits[o]
469+
if !ok {
470+
return fmt.Errorf("deposit not found")
471+
}
472+
473+
err := sm.SendEvent(event, nil)
474+
if err != nil {
475+
return err
476+
}
477+
err = sm.DefaultObserver.WaitForState(
478+
m.runCtx, time.Minute, expectedFinalState,
479+
)
480+
if err != nil {
481+
return err
482+
}
483+
}
484+
485+
return nil
486+
}

0 commit comments

Comments
 (0)