Skip to content

Commit 8d0cb56

Browse files
committed
universe/supplyverifier: adding basic logging
1 parent 42da7e2 commit 8d0cb56

File tree

6 files changed

+182
-5
lines changed

6 files changed

+182
-5
lines changed

universe/supplyverifier/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/btcsuite/btcd/wire"
9+
"github.com/btcsuite/btclog/v2"
910
"github.com/lightninglabs/lndclient"
1011
"github.com/lightninglabs/taproot-assets/asset"
1112
"github.com/lightninglabs/taproot-assets/fn"
@@ -96,6 +97,9 @@ type Environment struct {
9697
// that we're maintaining a supply commit for.
9798
AssetSpec asset.Specifier
9899

100+
// AssetLog is the asset-specific logger for this state machine.
101+
AssetLog btclog.Logger
102+
99103
// Chain is our access to the current main chain.
100104
Chain tapgarden.ChainBridge
101105

@@ -138,3 +142,14 @@ type Environment struct {
138142
func (e *Environment) Name() string {
139143
return fmt.Sprintf("supply_verifier(%s)", e.AssetSpec.String())
140144
}
145+
146+
// Logger returns the logger for the environment. If a logger is configured in
147+
// the environment configuration, it returns that logger. Otherwise, it returns
148+
// the package-level logger with an asset-specific prefix.
149+
func (e *Environment) Logger() btclog.Logger {
150+
if e.AssetLog != nil {
151+
return e.AssetLog
152+
}
153+
154+
return NewAssetLogger(e.AssetSpec.String())
155+
}

universe/supplyverifier/log.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package supplyverifier
22

33
import (
4+
"fmt"
5+
46
"github.com/btcsuite/btclog/v2"
57
)
68

@@ -24,3 +26,10 @@ func DisableLog() {
2426
func UseLogger(logger btclog.Logger) {
2527
log = logger
2628
}
29+
30+
// NewAssetLogger creates a new prefixed logger for a specific asset. This
31+
// logger will automatically include the asset specifier in all log messages,
32+
// using the format "SupplyVerifier(asset): message".
33+
func NewAssetLogger(assetSpec string) btclog.Logger {
34+
return log.WithPrefix(fmt.Sprintf("SupplyVerifier(%v): ", assetSpec))
35+
}

universe/supplyverifier/manager.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ func (m *Manager) InitStateMachines() error {
174174
ctx, cancel := m.WithCtxQuitNoTimeout()
175175
defer cancel()
176176

177+
log.Infof("Initializing supply verifier state machines")
178+
177179
// First, get all assets with group keys that could potentially be
178180
// involved in supply commitments. The Manager will filter these
179181
// based on delegation key ownership and other criteria.
@@ -185,6 +187,9 @@ func (m *Manager) InitStateMachines() error {
185187
err)
186188
}
187189

190+
log.Debugf("Found %d potential asset groups for supply verification",
191+
len(assetGroupKeys))
192+
188193
for idx := range assetGroupKeys {
189194
groupKey := assetGroupKeys[idx]
190195

@@ -195,6 +200,9 @@ func (m *Manager) InitStateMachines() error {
195200
// already exist.
196201
_, ok := m.smCache.Get(groupKey)
197202
if ok {
203+
log.Tracef("State machine already exists for "+
204+
"asset group: %x",
205+
groupKey.SerializeCompressed())
198206
continue
199207
}
200208

@@ -217,6 +225,8 @@ func (m *Manager) Start() error {
217225
var startErr error
218226

219227
m.startOnce.Do(func() {
228+
log.Infof("Starting supply verifier manager")
229+
220230
// Initialize the state machine cache.
221231
m.smCache = newStateMachineCache()
222232

@@ -229,13 +239,17 @@ func (m *Manager) Start() error {
229239
return
230240
}
231241

242+
log.Debugf("Starting universe syncer event monitor")
243+
232244
// Start a goroutine to handle universe syncer issuance events.
233245
m.ContextGuard.Goroutine(
234246
m.MonitorUniSyncEvents, func(err error) {
235247
log.Errorf("MonitorUniIssuanceSyncEvents: %v",
236248
err)
237249
},
238250
)
251+
252+
log.Infof("Supply verifier manager started successfully")
239253
})
240254
if startErr != nil {
241255
return fmt.Errorf("unable to start manager: %w", startErr)
@@ -273,6 +287,10 @@ func (m *Manager) handleUniSyncEvent(event fn.Event) error {
273287
return nil
274288
}
275289

290+
log.Tracef("Processing universe sync event for group key: %x, "+
291+
"new_leaf_proofs=%d", universeID.GroupKey.SerializeCompressed(),
292+
len(syncDiffEvent.SyncDiff.NewLeafProofs))
293+
276294
// Get genesis asset ID from the first synced leaf and formulate an
277295
// asset specifier.
278296
//
@@ -298,14 +316,17 @@ func (m *Manager) handleUniSyncEvent(event fn.Event) error {
298316
cancelCtx()
299317

300318
if !isSupported {
319+
log.Tracef("Asset does not support supply commitments: %s",
320+
assetSpec.String())
301321
return nil
302322
}
303323

304-
// Fetch the state machine for the asset group, creating and starting it
305-
// if it doesn't exist.
306324
log.Debugf("Ensure supply verifier state machine for asset "+
307325
"group due to universe syncer issuance event (asset=%s)",
308326
assetSpec.String())
327+
328+
// Fetch the state machine for the asset group, creating and starting it
329+
// if it doesn't exist.
309330
_, err = m.fetchStateMachine(assetSpec)
310331
if err != nil {
311332
return fmt.Errorf("unable to get or create state machine: %w",
@@ -363,12 +384,16 @@ func (m *Manager) MonitorUniSyncEvents() error {
363384
// group key specific supply verifier state machines.
364385
func (m *Manager) Stop() error {
365386
m.stopOnce.Do(func() {
387+
log.Infof("Stopping supply verifier manager")
388+
366389
// Cancel the state machine context to signal all state machines
367390
// to stop.
368391
close(m.Quit)
369392

370393
// Stop all state machines.
371394
m.smCache.StopAll()
395+
396+
log.Infof("Supply verifier manager stopped")
372397
})
373398

374399
return nil
@@ -385,6 +410,7 @@ func (m *Manager) startAssetSM(ctx context.Context,
385410
// If the state machine is not found, create a new one.
386411
env := &Environment{
387412
AssetSpec: assetSpec,
413+
AssetLog: NewAssetLogger(assetSpec.String()),
388414
Chain: m.cfg.Chain,
389415
SupplyCommitView: m.cfg.SupplyCommitView,
390416
SupplyTreeView: m.cfg.SupplyTreeView,
@@ -487,10 +513,15 @@ func (m *Manager) InsertSupplyCommit(ctx context.Context,
487513
assetSpec asset.Specifier, commitment supplycommit.RootCommitment,
488514
leaves supplycommit.SupplyLeaves) error {
489515

516+
log.Infof("Inserting supply commitment for asset: %s, "+
517+
"commitment_outpoint=%s", assetSpec.String(),
518+
commitment.CommitPoint().String())
519+
490520
// First, we verify the supply commitment to ensure it is valid and
491521
// consistent with the given supply leaves.
492522
verifier, err := NewVerifier(
493523
VerifierCfg{
524+
AssetSpec: assetSpec,
494525
ChainBridge: m.cfg.Chain,
495526
AssetLookup: m.cfg.AssetLookup,
496527
Lnd: m.cfg.Lnd,

universe/supplyverifier/syncer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func (s *SupplySyncer) pushUniServer(ctx context.Context,
9999
chainProof supplycommit.ChainProof,
100100
serverAddr universe.ServerAddr) error {
101101

102+
log.Debugf("Pushing supply commitment to server: %s, asset: %s",
103+
serverAddr.HostStr(), assetSpec.String())
104+
102105
// Create a client for the specific universe server address.
103106
client, err := s.cfg.ClientFactory(serverAddr)
104107
if err != nil {
@@ -128,6 +131,11 @@ func (s *SupplySyncer) pushUniServer(ctx context.Context,
128131
return fmt.Errorf("unable to log supply commit push: %w", err)
129132
}
130133

134+
log.Infof("Successfully pushed supply commitment to server: %s, "+
135+
"asset: %s, commitment_outpoint=%s",
136+
serverAddr.HostStr(), assetSpec.String(),
137+
commitment.CommitPoint().String())
138+
131139
return nil
132140
}
133141

@@ -183,6 +191,9 @@ func (s *SupplySyncer) PushSupplyCommitment(ctx context.Context,
183191
chainProof supplycommit.ChainProof,
184192
canonicalUniverses []url.URL) (map[string]error, error) {
185193

194+
log.Infof("Starting push of supply commitment for asset: %s to "+
195+
"%d servers", assetSpec.String(), len(canonicalUniverses))
196+
186197
targetAddrs, err := s.fetchServerAddrs(ctx, canonicalUniverses)
187198
if err != nil {
188199
// This is an internal error that prevents the operation from
@@ -245,6 +256,10 @@ func (s *SupplySyncer) pullUniServer(ctx context.Context,
245256

246257
var zero supplycommit.FetchSupplyCommitResult
247258

259+
log.Debugf("Pulling supply commitment from server: %s, asset: %s, "+
260+
"spent_outpoint=%v", serverAddr.HostStr(), assetSpec.String(),
261+
spentCommitOutpoint.IsSome())
262+
248263
// Create a client for the specific universe server address.
249264
client, err := s.cfg.ClientFactory(serverAddr)
250265
if err != nil {
@@ -268,6 +283,9 @@ func (s *SupplySyncer) pullUniServer(ctx context.Context,
268283
err)
269284
}
270285

286+
log.Infof("Successfully pulled supply commitment from server: %s, "+
287+
"asset: %s", serverAddr.HostStr(), assetSpec.String())
288+
271289
return result, nil
272290
}
273291

@@ -297,6 +315,11 @@ func (s *SupplySyncer) PullSupplyCommitment(ctx context.Context,
297315

298316
var zero SupplyCommitPullResult
299317

318+
log.Infof("Starting pull of supply commitment for asset: %s, "+
319+
"spent_outpoint=%v, canonical_universes=%d",
320+
assetSpec.String(), spentCommitOutpoint.IsSome(),
321+
len(canonicalUniverses))
322+
300323
targetAddrs, err := s.fetchServerAddrs(ctx, canonicalUniverses)
301324
if err != nil {
302325
// This is an internal error that prevents the operation from

universe/supplyverifier/transitions.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func (s *InitState) ProcessEvent(event Event,
2121

2222
switch event.(type) {
2323
case *InitEvent:
24+
log.Debugf("Processing InitEvent for asset: %s",
25+
env.AssetSpec.String())
26+
2427
ctx := context.Background()
2528

2629
// Retrieve the set of unspent pre-commitments for the asset
@@ -54,6 +57,12 @@ func (s *InitState) ProcessEvent(event Event,
5457
"verified supply commitment found")
5558
}
5659

60+
log.Infof("Transitioning from InitState to "+
61+
"WatchOutputsState for asset: %s, "+
62+
"pre_commits=%d, has_latest_commit=%v",
63+
env.AssetSpec.String(), len(preCommits),
64+
latestCommit != nil)
65+
5766
return &StateTransition{
5867
NextState: &WatchOutputsState{},
5968
NewEvents: lfn.Some(FsmEvent{
@@ -102,6 +111,9 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
102111

103112
switch e := event.(type) {
104113
case *SyncVerifyEvent:
114+
log.Debugf("Processing SyncVerifyEvent for asset: %s",
115+
env.AssetSpec.String())
116+
105117
ctx := context.Background()
106118

107119
// Check to ensure that we haven't already processed a supply
@@ -133,6 +145,10 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
133145
return nil, err
134146
}
135147

148+
log.Debugf("Supply commitment already " +
149+
"processed, transitioning to " +
150+
"WatchOutputsState")
151+
136152
watchEvent := WatchOutputsEvent{
137153
SupplyCommit: &commit,
138154
}
@@ -185,6 +201,7 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
185201

186202
verifier, err := NewVerifier(
187203
VerifierCfg{
204+
AssetSpec: env.AssetSpec,
188205
ChainBridge: env.Chain,
189206
AssetLookup: env.AssetLookup,
190207
Lnd: env.Lnd,
@@ -207,6 +224,8 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
207224
"commitment: %w", err)
208225
}
209226

227+
log.Debugf("Storing verified supply commitment")
228+
210229
// Store the verified commitment.
211230
err = env.SupplyCommitView.InsertSupplyCommit(
212231
ctx, env.AssetSpec, supplyCommit.RootCommitment,
@@ -217,6 +236,11 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
217236
"commitment: %w", err)
218237
}
219238

239+
log.Infof("Successfully synced and verified supply "+
240+
"commitment for asset: %s, "+
241+
"transitioning to WatchOutputsState",
242+
env.AssetSpec.String())
243+
220244
// After syncing, verifying, and storing the latest supply
221245
// commitment, transition to the watch state to await its spend.
222246
watchEvent := WatchOutputsEvent{
@@ -232,12 +256,18 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
232256
}, nil
233257

234258
case *SpendEvent:
259+
log.Infof("Received SpendEvent for asset: %s, spend_tx=%s",
260+
env.AssetSpec.String(),
261+
e.SpendDetail.SpendingTx.TxHash())
262+
235263
// A watched output has been spent, so transition to the sync
236264
// state to fetch the new supply commitment. Before syncing,
237265
// apply a delay to give the issuer time to publish it.
238266
switch {
239267
case e.WatchStartTimestamp.IsZero():
240268
// No watch start timestamp: wait the full sync delay.
269+
log.Debugf("Waiting full sync delay of %v before "+
270+
"syncing", env.SpendSyncDelay)
241271
time.Sleep(env.SpendSyncDelay)
242272

243273
default:
@@ -246,6 +276,10 @@ func (s *SyncVerifyState) ProcessEvent(event Event,
246276
timeSinceWatch := time.Since(e.WatchStartTimestamp)
247277
if timeSinceWatch < env.SpendSyncDelay {
248278
delay := env.SpendSyncDelay - timeSinceWatch
279+
280+
env.Logger().Debugf("Waiting remaining sync "+
281+
"delay of %v before syncing", delay)
282+
249283
time.Sleep(delay)
250284
}
251285
}
@@ -283,6 +317,8 @@ func (s *WatchOutputsState) ProcessEvent(event Event,
283317

284318
switch e := event.(type) {
285319
case *WatchOutputsEvent:
320+
env.Logger().Debugf("Processing WatchOutputsEvent")
321+
286322
preCommits := e.PreCommits
287323

288324
// If no pre-commitments were provided, then we'll query our
@@ -342,6 +378,11 @@ func (s *WatchOutputsState) ProcessEvent(event Event,
342378
Hash: e.SupplyCommit.Txn.TxHash(),
343379
Index: e.SupplyCommit.TxOutIdx,
344380
}
381+
382+
env.Logger().Debugf("Registering spend watch for "+
383+
"supply commitment outpoint: %s",
384+
outpoint.String())
385+
345386
txOutIdx := e.SupplyCommit.TxOutIdx
346387
txOut := e.SupplyCommit.Txn.TxOut[txOutIdx]
347388

@@ -365,6 +406,9 @@ func (s *WatchOutputsState) ProcessEvent(event Event,
365406
})
366407
}
367408

409+
env.Logger().Infof("Transitioning to SyncVerifyState, "+
410+
"watching %d outputs", len(events))
411+
368412
// Otherwise, we'll transition to the verify state to await
369413
// a spend of one of the outputs we're watching.
370414
return &StateTransition{

0 commit comments

Comments
 (0)