Skip to content

Commit 5d91834

Browse files
committed
supplyverifier: respect disable output watch flag in manager
Update the supply verifier manager to respect the new config flag. When `universe.disable-supply-verifier-chain-watch` is enabled, no state machines are created for watching on-chain outputs. This allows universe servers to remain lightweight by avoiding the overhead of running potentially many verifier state machines.
1 parent 01b0dbd commit 5d91834

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

tapcfg/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
549549

550550
// Set up the supply verifier, which validates supply commitment leaves
551551
// published by asset issuers.
552+
//
553+
// nolint: lll
552554
supplyVerifyManager, err := supplyverifier.NewManager(
553555
supplyverifier.ManagerCfg{
554556
Chain: chainBridge,
@@ -560,6 +562,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
560562
GroupFetcher: assetMintingStore,
561563
IssuanceSubscriptions: universeSyncer,
562564
DaemonAdapters: lndFsmDaemonAdapters,
565+
DisableChainWatch: cfg.Universe.DisableSupplyVerifierChainWatch,
563566
},
564567
)
565568
if err != nil {

universe/supplyverifier/manager.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ type ManagerCfg struct {
9494

9595
// ErrChan is the channel that is used to send errors to the caller.
9696
ErrChan chan<- error
97+
98+
// DisableChainWatch, when true, prevents the supply verifier from
99+
// starting state machines to watch on-chain outputs for spends. This
100+
// option is intended for universe servers, where supply verification
101+
// should only occur for commitments submitted by peers, not via
102+
// on-chain spend detection.
103+
DisableChainWatch bool
97104
}
98105

99106
// Validate validates the ManagerCfg.
@@ -227,9 +234,19 @@ func (m *Manager) Start() error {
227234
m.startOnce.Do(func() {
228235
log.Infof("Starting supply verifier manager")
229236

230-
// Initialize the state machine cache.
237+
// Initialize the state machine cache unconditionally to prevent
238+
// potential nil pointer dereferences, even if it ends up
239+
// unused.
231240
m.smCache = newStateMachineCache()
232241

242+
// If chain watching is disabled, return early. We won't start
243+
// any state machines in this mode.
244+
if m.cfg.DisableChainWatch {
245+
log.Infof("Supply verifier chain watch disabled, " +
246+
"skip state machine initialization")
247+
return
248+
}
249+
233250
// Initialize state machines for each asset group that supports
234251
// supply commitments.
235252
err := m.InitStateMachines()
@@ -400,10 +417,18 @@ func (m *Manager) Stop() error {
400417
}
401418

402419
// startAssetSM creates and starts a new supply commitment state machine for the
403-
// given asset specifier.
420+
// given asset specifier. If DisableChainWatch is true, an error is returned.
404421
func (m *Manager) startAssetSM(ctx context.Context,
405422
assetSpec asset.Specifier) (*StateMachine, error) {
406423

424+
// If chain watching is disabled, return an error.
425+
if m.cfg.DisableChainWatch {
426+
log.Debugf("Supply verifier chain watch disabled, not "+
427+
"starting state machine (asset=%s)", assetSpec.String())
428+
return nil, fmt.Errorf("supply verifier chain watch is " +
429+
"disabled")
430+
}
431+
407432
log.Infof("Starting supply verifier state machine (asset=%s)",
408433
assetSpec.String())
409434

@@ -455,10 +480,18 @@ func (m *Manager) startAssetSM(ctx context.Context,
455480

456481
// fetchStateMachine retrieves a state machine from the cache or creates a
457482
// new one if it doesn't exist. If a new state machine is created, it is also
458-
// started.
483+
// started. If DisableChainWatch is true, an error is returned.
459484
func (m *Manager) fetchStateMachine(assetSpec asset.Specifier) (*StateMachine,
460485
error) {
461486

487+
// If chain watching is disabled, return an error.
488+
if m.cfg.DisableChainWatch {
489+
log.Debugf("Supply verifier chain watch disabled, not "+
490+
"fetching state machine (asset=%s)", assetSpec.String())
491+
return nil, fmt.Errorf("supply verifier chain watch is " +
492+
"disabled")
493+
}
494+
462495
groupKey, err := assetSpec.UnwrapGroupKeyOrErr()
463496
if err != nil {
464497
return nil, fmt.Errorf("asset specifier missing group key: %w",
@@ -894,6 +927,10 @@ func newStateMachineCache() *stateMachineCache {
894927

895928
// StopAll stops all state machines in the cache.
896929
func (c *stateMachineCache) StopAll() {
930+
if c == nil {
931+
return
932+
}
933+
897934
c.mu.RLock()
898935
defer c.mu.RUnlock()
899936

0 commit comments

Comments
 (0)