Skip to content

Commit 80595bf

Browse files
committed
staticaddr: deposit changes to be squashed
These deposit changes will be squashed into the original commits once merged into staging.
1 parent 8a920f6 commit 80595bf

File tree

5 files changed

+114
-27
lines changed

5 files changed

+114
-27
lines changed

staticaddr/deposit/actions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ func (f *FSM) SweptExpiredDepositAction(_ fsm.EventContext) fsm.EventType {
148148
return fsm.NoOp
149149
}
150150

151-
// WithdrawnDepositAction is the final action after a withdrawal. It signals to
151+
// FinalizeDepositAction is the final action after a withdrawal. It signals to
152152
// the manager that the deposit has been swept and the FSM can be removed.
153-
func (f *FSM) WithdrawnDepositAction(_ fsm.EventContext) fsm.EventType {
153+
func (f *FSM) FinalizeDepositAction(_ fsm.EventContext) fsm.EventType {
154154
select {
155155
case <-f.ctx.Done():
156156
return fsm.OnError

staticaddr/deposit/deposit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func (d *Deposit) IsInFinalState() bool {
7272
d.Lock()
7373
defer d.Unlock()
7474

75-
return d.state == Expired || d.state == Withdrawn || d.state == Failed
75+
return d.state == Expired || d.state == Withdrawn ||
76+
d.state == Failed || d.state == LoopedIn ||
77+
d.state == HtlcTimeoutSwept
7678
}
7779

7880
func (d *Deposit) IsExpired(currentHeight, expiry uint32) bool {

staticaddr/deposit/fsm.go

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ var (
3333

3434
Withdrawn = fsm.StateType("Withdrawn")
3535

36+
LoopingIn = fsm.StateType("LoopingIn")
37+
38+
LoopedIn = fsm.StateType("LoopedIn")
39+
40+
SweepHtlcTimout = fsm.StateType("SweepHtlcTimout")
41+
42+
HtlcTimeoutSwept = fsm.StateType("HtlcTimeoutSwept")
43+
3644
PublishExpiredDeposit = fsm.StateType("PublishExpiredDeposit")
3745

3846
WaitForExpirySweep = fsm.StateType("WaitForExpirySweep")
@@ -44,13 +52,17 @@ var (
4452

4553
// Events.
4654
var (
47-
OnStart = fsm.EventType("OnStart")
48-
OnWithdrawInitiated = fsm.EventType("OnWithdrawInitiated")
49-
OnWithdrawn = fsm.EventType("OnWithdrawn")
50-
OnExpiry = fsm.EventType("OnExpiry")
51-
OnExpiryPublished = fsm.EventType("OnExpiryPublished")
52-
OnExpirySwept = fsm.EventType("OnExpirySwept")
53-
OnRecover = fsm.EventType("OnRecover")
55+
OnStart = fsm.EventType("OnStart")
56+
OnWithdrawInitiated = fsm.EventType("OnWithdrawInitiated")
57+
OnWithdrawn = fsm.EventType("OnWithdrawn")
58+
OnLoopinInitiated = fsm.EventType("OnLoopinInitiated")
59+
OnSweepingHtlcTimout = fsm.EventType("OnSweepingHtlcTimout")
60+
OnHtlcTimeoutSwept = fsm.EventType("OnHtlcTimeoutSwept")
61+
OnLoopedIn = fsm.EventType("OnLoopedIn")
62+
OnExpiry = fsm.EventType("OnExpiry")
63+
OnExpiryPublished = fsm.EventType("OnExpiryPublished")
64+
OnExpirySwept = fsm.EventType("OnExpirySwept")
65+
OnRecover = fsm.EventType("OnRecover")
5466
)
5567

5668
// FSM is the state machine that handles the instant out.
@@ -169,6 +181,7 @@ func (f *FSM) DepositStatesV0() fsm.States {
169181
Transitions: fsm.Transitions{
170182
OnExpiry: PublishExpiredDeposit,
171183
OnWithdrawInitiated: Withdrawing,
184+
OnLoopinInitiated: LoopingIn,
172185
OnRecover: Deposited,
173186
},
174187
Action: fsm.NoOpAction,
@@ -232,7 +245,57 @@ func (f *FSM) DepositStatesV0() fsm.States {
232245
Transitions: fsm.Transitions{
233246
OnExpiry: Expired,
234247
},
235-
Action: f.WithdrawnDepositAction,
248+
Action: f.FinalizeDepositAction,
249+
},
250+
LoopingIn: fsm.State{
251+
Transitions: fsm.Transitions{
252+
// This event is triggered when the loop in
253+
// payment has been received. We consider the
254+
// swap to be completed and transition to a
255+
// final state.
256+
OnLoopedIn: LoopedIn,
257+
258+
// If the deposit expires while the loop in is
259+
// still pending, we publish the expiry sweep.
260+
OnExpiry: PublishExpiredDeposit,
261+
262+
// We encounter this signal if the server
263+
// published the htlc tx without paying us. We
264+
// then need to monitor for the timeout path to
265+
// open up to sweep it.
266+
OnSweepingHtlcTimout: SweepHtlcTimout,
267+
268+
OnLoopinInitiated: LoopingIn,
269+
270+
OnRecover: LoopingIn,
271+
fsm.OnError: Deposited,
272+
},
273+
Action: fsm.NoOpAction,
274+
},
275+
LoopedIn: fsm.State{
276+
Transitions: fsm.Transitions{
277+
OnExpiry: Expired,
278+
},
279+
Action: f.FinalizeDepositAction,
280+
},
281+
SweepHtlcTimout: fsm.State{
282+
Transitions: fsm.Transitions{
283+
OnHtlcTimeoutSwept: HtlcTimeoutSwept,
284+
OnRecover: SweepHtlcTimout,
285+
},
286+
Action: fsm.NoOpAction,
287+
},
288+
HtlcTimeoutSwept: fsm.State{
289+
Transitions: fsm.Transitions{
290+
OnExpiry: HtlcTimeoutSwept,
291+
},
292+
Action: f.FinalizeDepositAction,
293+
},
294+
Withdrawn: fsm.State{
295+
Transitions: fsm.Transitions{
296+
OnExpiry: Expired,
297+
},
298+
Action: f.FinalizeDepositAction,
236299
},
237300
Failed: fsm.State{
238301
Transitions: fsm.Transitions{
@@ -257,14 +320,7 @@ func (f *FSM) updateDeposit(notification fsm.Notification) {
257320

258321
f.deposit.SetState(notification.NextState)
259322

260-
// Don't update the deposit if we are in an initial state or if we
261-
// are transitioning from an initial state to a failed state.
262-
d := f.deposit
263-
if d.IsInState(fsm.EmptyState) || d.IsInState(Deposited) ||
264-
(notification.PreviousState == Deposited && d.IsInState(
265-
Failed,
266-
)) {
267-
323+
if skipUpdate(notification, f.deposit) {
268324
return
269325
}
270326

@@ -274,6 +330,23 @@ func (f *FSM) updateDeposit(notification fsm.Notification) {
274330
}
275331
}
276332

333+
// Don't update the deposit if we are in an initial state or if we are
334+
// transitioning from an initial state to a failed state.
335+
func skipUpdate(notification fsm.Notification, d *Deposit) bool {
336+
prevState := notification.PreviousState
337+
if d.IsInState(fsm.EmptyState) || d.IsInState(Deposited) &&
338+
prevState == fsm.EmptyState ||
339+
d.IsInState(prevState) ||
340+
prevState == Deposited && d.IsInState(Deposited) ||
341+
prevState == Deposited && d.IsInState(Failed) ||
342+
prevState == LoopingIn && d.IsInState(LoopingIn) {
343+
344+
return true
345+
}
346+
347+
return false
348+
}
349+
277350
// Infof logs an info message with the deposit outpoint.
278351
func (f *FSM) Infof(format string, args ...interface{}) {
279352
log.Infof(

staticaddr/deposit/manager.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ func (m *Manager) startDepositFsm(deposit *Deposit) error {
423423
func (m *Manager) finalizeDeposit(outpoint wire.OutPoint) {
424424
m.Lock()
425425
delete(m.activeDeposits, outpoint)
426-
delete(m.deposits, outpoint)
427426
m.Unlock()
428427
}
429428

@@ -456,7 +455,8 @@ func (m *Manager) GetAllDeposits() ([]*Deposit, error) {
456455
}
457456

458457
// AllOutpointsActiveDeposits checks if all deposits referenced by the outpoints
459-
// are active and in the specified state.
458+
// are active and in the specified state. If fsm.EmptyState is reference as
459+
// stateFilter all deposits are returned regardless of their state.
460460
func (m *Manager) AllOutpointsActiveDeposits(outpoints []wire.OutPoint,
461461
stateFilter fsm.StateType) ([]*Deposit, bool) {
462462

@@ -470,7 +470,8 @@ func (m *Manager) AllOutpointsActiveDeposits(outpoints []wire.OutPoint,
470470
}
471471

472472
deposit := m.deposits[o]
473-
if deposit.GetState() != stateFilter {
473+
if stateFilter != fsm.EmptyState &&
474+
deposit.GetState() != stateFilter {
474475
return nil, false
475476
}
476477

@@ -480,6 +481,22 @@ func (m *Manager) AllOutpointsActiveDeposits(outpoints []wire.OutPoint,
480481
return deposits, true
481482
}
482483

484+
func (m *Manager) AllStringOutpointsActiveDeposits(outpoints []string,
485+
stateFilter fsm.StateType) ([]*Deposit, bool) {
486+
487+
outPoints := make([]wire.OutPoint, len(outpoints))
488+
for i, o := range outpoints {
489+
op, err := wire.NewOutPointFromString(o)
490+
if err != nil {
491+
return nil, false
492+
}
493+
494+
outPoints[i] = *op
495+
}
496+
497+
return m.AllOutpointsActiveDeposits(outPoints, stateFilter)
498+
}
499+
483500
// TransitionDeposits allows a caller to transition a set of deposits to a new
484501
// state.
485502
func (m *Manager) TransitionDeposits(deposits []*Deposit, event fsm.EventType,

staticaddr/deposit/sql_store.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,3 @@ func (s *SqlStore) toDeposit(row sqlc.Deposit,
214214
WithdrawalSweepAddress: row.WithdrawalSweepAddress.String,
215215
}, nil
216216
}
217-
218-
// Close closes the database connection.
219-
func (s *SqlStore) Close() {
220-
s.baseDB.DB.Close()
221-
}

0 commit comments

Comments
 (0)