Skip to content

Commit 538c64a

Browse files
committed
universe/supplycommit: add new SendEventSync method to the multi sm manager
This'll be used by callers to make sure that they only exit once the new pending update has been written to disk.
1 parent a9001e7 commit 538c64a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

universe/supplycommit/multi_sm_manager.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,46 @@ func (m *MultiStateMachineManager) SendEvent(ctx context.Context,
225225
return nil
226226
}
227227

228+
// SendEventSync sends an event to the state machine and waits for it to be
229+
// processed and written to disk. This method provides synchronous confirmation
230+
// that the event has been durably persisted. If the event doesn't support
231+
// synchronous processing (i.e., it's not a SupplyUpdateEvent), this method will
232+
// return an error.
233+
func (m *MultiStateMachineManager) SendEventSync(ctx context.Context,
234+
assetSpec asset.Specifier, event SyncSupplyUpdateEvent) error {
235+
236+
// Only SupplyUpdateEvents can be processed synchronously.
237+
supplyEvent, ok := event.(SupplyUpdateEvent)
238+
if !ok {
239+
return fmt.Errorf("event type %T does not support "+
240+
"synchronous processing", event)
241+
}
242+
243+
// We'll use this channel to signal when the event has been processed.
244+
done := make(chan error, 1)
245+
246+
// As the event has already been created, we'll set the done channel
247+
// directly.
248+
switch e := supplyEvent.(type) {
249+
case *NewMintEvent:
250+
e.Done = done
251+
case *NewBurnEvent:
252+
e.Done = done
253+
case *NewIgnoreEvent:
254+
e.Done = done
255+
default:
256+
return fmt.Errorf("unexpected supply update event type: %T",
257+
supplyEvent)
258+
}
259+
260+
// Send the event to the state machine, the pause and wait until it
261+
// signals that the event has been fully processed.
262+
if err := m.SendEvent(ctx, assetSpec, supplyEvent); err != nil {
263+
return err
264+
}
265+
return event.WaitForDone(ctx)
266+
}
267+
228268
// CanHandle determines if the state machine associated with the given asset
229269
// specifier can handle the given message. If a state machine for the asset
230270
// group does not exist, it will be created and started.

0 commit comments

Comments
 (0)