Skip to content

Commit a3cbff2

Browse files
Roasbeefffranr
authored andcommitted
supplycommit: add logic to resend trigger event for select states
In this commit, we add logic to resend trigger events for select states. Along the way, we modify the way the state machine works: we can avoid having to extract the PSBT again, just by reading it from the state transition.
1 parent 7e062ed commit a3cbff2

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

universe/supplycommit/multi_sm_manager.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,15 @@ func (m *MultiStateMachineManager) fetchStateMachine(
160160
ChainParams: m.cfg.ChainParams,
161161
}
162162

163-
// TODO(ffranr): Get initial state from disk here.
164-
initialState := &DefaultState{}
163+
// Before we start the state machine, we'll need to fetch the current
164+
// state from disk, to see if we need to emit any new events.
165+
ctx, cancel := m.WithCtxQuitNoTimeout()
166+
defer cancel()
167+
168+
initialState, _, err := m.cfg.StateLog.FetchState(ctx, assetSpec)
169+
if err != nil {
170+
return nil, fmt.Errorf("unable to fetch current state: %w", err)
171+
}
165172

166173
// Create a new error reporter for the state machine.
167174
errorReporter := NewErrorReporter(assetSpec)
@@ -180,6 +187,23 @@ func (m *MultiStateMachineManager) fetchStateMachine(
180187
smCtx, _ := m.WithCtxQuitNoTimeout()
181188
newSm.Start(smCtx)
182189

190+
// If specific initial states are provided, we send the corresponding
191+
// events to the state machine to ensure it begins ticking as expected.
192+
switch initialState.(type) {
193+
// Once we write the commitment transaction to disk in CommitTxSign,
194+
// then on restart, we'll be in the broadcast state. From this point,
195+
// we'll trigger the broadcast event so we can resume the state machine.
196+
case *CommitBroadcastState:
197+
newSm.SendEvent(ctx, &BroadcastEvent{})
198+
199+
// Once we get a confirmation, then we'll transition to the
200+
// CommitFinalizeState. If we crashed right after that, then
201+
// we'll also send the finalize event so we can apply
202+
// everything, and transition back to the normal default state.
203+
case *CommitFinalizeState:
204+
newSm.SendEvent(ctx, &FinalizeEvent{})
205+
}
206+
183207
m.smCache.Set(*groupKey, &newSm)
184208

185209
return &newSm, nil

universe/supplycommit/transitions.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,7 @@ func (c *CommitTxSignState) ProcessEvent(event Event,
630630
SupplyTransition: stateTransition,
631631
},
632632
NewEvents: lfn.Some(FsmEvent{
633-
InternalEvent: []Event{&BroadcastEvent{
634-
SignedCommitPkt: signedPsbt,
635-
}},
633+
InternalEvent: []Event{&BroadcastEvent{}},
636634
}),
637635
}, nil
638636

@@ -654,14 +652,12 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
654652
// signed commit tx, then register for a confirmation for when it
655653
// confirms.
656654
case *BroadcastEvent:
657-
// First, we'll extract the final signed transaction from the
658-
// PSBT.
659-
commitTx, err := psbt.Extract(newEvent.SignedCommitPkt)
660-
if err != nil {
661-
return nil, fmt.Errorf("unable to extract "+
662-
"psbt: %w", err)
655+
if c.SupplyTransition.NewCommitment.Txn == nil {
656+
return nil, fmt.Errorf("commitment transaction is nil")
663657
}
664658

659+
commitTx := c.SupplyTransition.NewCommitment.Txn
660+
665661
// Construct a detailed label for the broadcast request using
666662
// the helper function.
667663
label := createCommitmentTxLabel(

0 commit comments

Comments
 (0)