Skip to content

Commit 4eafdae

Browse files
committed
universe/supplycommit: add additional logging
1 parent d974383 commit 4eafdae

File tree

3 files changed

+124
-12
lines changed

3 files changed

+124
-12
lines changed

universe/supplycommit/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/btcsuite/btcd/chaincfg/chainhash"
1313
"github.com/btcsuite/btcd/txscript"
1414
"github.com/btcsuite/btcd/wire"
15+
"github.com/btcsuite/btclog/v2"
1516
"github.com/lightninglabs/taproot-assets/asset"
1617
"github.com/lightninglabs/taproot-assets/fn"
1718
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -509,6 +510,9 @@ type Environment struct {
509510
// ChainParams is the chain parameters for the chain that we're
510511
// operating on.
511512
ChainParams chaincfg.Params
513+
514+
// Log is the prefixed logger for this supply commitment state machine.
515+
Log btclog.Logger
512516
}
513517

514518
// SupplyCommitTxn encapsulates the details of the transaction that creates a
@@ -534,3 +538,14 @@ type SupplyCommitTxn struct {
534538
func (e *Environment) Name() string {
535539
return fmt.Sprintf("universe_supply_commit(%v)", e.AssetSpec)
536540
}
541+
542+
// Logger returns the logger for this environment. If a logger was provided in
543+
// the environment configuration, it returns that logger. Otherwise, it returns
544+
// the package-level logger with an asset-specific prefix.
545+
func (e *Environment) Logger() btclog.Logger {
546+
if e.Log != nil {
547+
return e.Log
548+
}
549+
550+
return NewAssetLogger(e.AssetSpec.String())
551+
}

universe/supplycommit/log.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package supplycommit
22

33
import (
4+
"fmt"
5+
46
"github.com/btcsuite/btclog/v2"
7+
"github.com/davecgh/go-spew/spew"
58
)
69

710
// Subsystem defines the logging code for this subsystem.
@@ -18,9 +21,23 @@ func DisableLog() {
1821
UseLogger(btclog.Disabled)
1922
}
2023

24+
// limitSpewer is a spew.ConfigState that limits the depth of the output to 4
25+
// levels, so it can safely be used for things that contain an MS-SMT tree.
26+
var limitSpewer = &spew.ConfigState{
27+
Indent: " ",
28+
MaxDepth: 4,
29+
}
30+
2131
// UseLogger uses a specified Logger to output package logging info.
2232
// This should be used in preference to SetLogWriter if the caller is also
2333
// using btclog.
2434
func UseLogger(logger btclog.Logger) {
2535
log = logger
2636
}
37+
38+
// NewAssetLogger creates a new prefixed logger for a specific asset. This
39+
// logger will automatically include the asset specifier in all log messages,
40+
// using the format "SupplyCommit(asset): message".
41+
func NewAssetLogger(assetSpec string) btclog.Logger {
42+
return log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", assetSpec))
43+
}

universe/supplycommit/transitions.go

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/btcsuite/btcd/btcutil/psbt"
99
"github.com/btcsuite/btcd/chaincfg"
1010
"github.com/btcsuite/btcd/wire"
11+
"github.com/btcsuite/btclog/v2"
1112
"github.com/lightninglabs/taproot-assets/asset"
1213
"github.com/lightninglabs/taproot-assets/mssmt"
1314
"github.com/lightninglabs/taproot-assets/proof"
@@ -66,11 +67,16 @@ func createCommitmentTxLabel(assetSpec asset.Specifier,
6667
func (d *DefaultState) ProcessEvent(event Event,
6768
env *Environment) (*StateTransition, error) {
6869

70+
prefixedLog := env.Logger()
71+
6972
// In this state, we'll receive one of three events: a new burn, a new
7073
// mint, or a new ignore. For all three types, we'll emit this as an
7174
// internal event as we transition to the UpdatePendingState.
7275
switch supplyEvent := event.(type) {
7376
case SyncSupplyUpdateEvent:
77+
prefixedLog.Infof("Received new supply update event: %T",
78+
supplyEvent)
79+
7480
// Before we transition to the next state, we'll add this event
7581
// to our update log. This ensures that we'll remember to
7682
// process from this state after a restart.
@@ -120,10 +126,15 @@ func (d *DefaultState) ProcessEvent(event Event,
120126
func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
121127
*StateTransition, error) {
122128

129+
prefixedLog := env.Logger()
130+
123131
switch newEvent := event.(type) {
124132
// We've received a new update event, we'll stage this in our local
125133
// state, and do a self transition.
126134
case SyncSupplyUpdateEvent:
135+
prefixedLog.Infof("Received new supply update event: %T",
136+
newEvent)
137+
127138
// We just got a new pending update in addition to the one that
128139
// made us transition to this state. This ensures that we'll
129140
// remember to process from this state after a restart.
@@ -163,6 +174,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
163174
"pending transition: %w", err)
164175
}
165176

177+
prefixedLog.Infof("Received tick event, committing %d "+
178+
"supply updates", len(u.pendingUpdates))
179+
166180
return &StateTransition{
167181
NextState: &CommitTreeCreateState{},
168182
NewEvents: lfn.Some(FsmEvent{
@@ -254,13 +268,18 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
254268
func (c *CommitTreeCreateState) ProcessEvent(event Event,
255269
env *Environment) (*StateTransition, error) {
256270

271+
prefixedLog := env.Logger()
272+
257273
// TODO(roasbeef): cache attemps to add new elements? or main state
258274
// driver still single threaded?
259275

260276
switch newEvent := event.(type) {
261277
// If we get a supply update event while we're creating the tree,
262278
// we'll just insert it as a dangling update and do a self-transition.
263279
case SyncSupplyUpdateEvent:
280+
prefixedLog.Infof("Received new supply update event: %T",
281+
newEvent)
282+
264283
ctx := context.Background()
265284
err := env.StateLog.InsertPendingUpdate(
266285
ctx, env.AssetSpec, newEvent,
@@ -291,6 +310,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
291310
case *CreateTreeEvent:
292311
pendingUpdates := newEvent.updatesToCommit
293312

313+
prefixedLog.Infof("Creating new supply trees "+
314+
"with %d pending updates",
315+
len(pendingUpdates))
316+
294317
// TODO(ffranr): Pass in context?
295318
ctx := context.Background()
296319

@@ -396,7 +419,13 @@ func newRootCommitment(ctx context.Context,
396419
oldCommitment lfn.Option[RootCommitment],
397420
unspentPreCommits []PreCommitment, newSupplyRoot *mssmt.BranchNode,
398421
wallet Wallet, keyRing KeyRing,
399-
chainParams chaincfg.Params) (*RootCommitment, *psbt.Packet, error) {
422+
chainParams chaincfg.Params,
423+
logger lfn.Option[btclog.Logger]) (*RootCommitment, *psbt.Packet, error) {
424+
425+
logger.WhenSome(func(l btclog.Logger) {
426+
l.Infof("Creating new root commitment, spending %v "+
427+
"pre-commits", len(unspentPreCommits))
428+
})
400429

401430
newCommitTx := wire.NewMsgTx(2)
402431

@@ -435,6 +464,11 @@ func newRootCommitment(ctx context.Context,
435464
// on mint transactions where as the old commitment is the last
436465
// commitment that was broadcast.
437466
oldCommitment.WhenSome(func(r RootCommitment) {
467+
logger.WhenSome(func(l btclog.Logger) {
468+
l.Infof("Re-using prior commitment as outpoint=%v: %v",
469+
r.CommitPoint(), limitSpewer.Sdump(r))
470+
})
471+
438472
newCommitTx.AddTxIn(r.TxIn())
439473

440474
bip32Derivation, trBip32Derivation :=
@@ -459,11 +493,12 @@ func newRootCommitment(ctx context.Context,
459493
})
460494
})
461495

496+
// TODO(roasbef): do CreateTaprootSignature instead?
462497
// With the inputs available, derive the supply commitment output.
463498
//
464-
// Determine the internal key to use for this output.
465-
// If a prior root commitment exists, reuse its internal key;
466-
// otherwise, generate a new one.
499+
// Determine the internal key to use for this output. If a prior root
500+
// commitment exists, reuse its internal key; otherwise, generate a new
501+
// one.
467502
iKeyOpt := lfn.MapOption(func(r RootCommitment) keychain.KeyDescriptor {
468503
return r.InternalKey
469504
})(oldCommitment)
@@ -534,6 +569,11 @@ func newRootCommitment(ctx context.Context,
534569
SupplyRoot: newSupplyRoot,
535570
}
536571

572+
logger.WhenSome(func(l btclog.Logger) {
573+
l.Infof("Created new root commitment: %v",
574+
limitSpewer.Sdump(newSupplyCommit))
575+
})
576+
537577
commitPkt, err := psbt.NewFromUnsignedTx(newCommitTx)
538578
if err != nil {
539579
return nil, nil, fmt.Errorf("unable to create PSBT: %w", err)
@@ -608,10 +648,15 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
608648
func (c *CommitTxCreateState) ProcessEvent(event Event,
609649
env *Environment) (*StateTransition, error) {
610650

651+
prefixedLog := env.Logger()
652+
611653
switch newEvent := event.(type) {
612654
// If we get a supply update event while we're creating the commit tx,
613655
// we'll just insert it as a dangling update and do a self-transition.
614656
case SyncSupplyUpdateEvent:
657+
prefixedLog.Infof("Received new supply update event: %T",
658+
newEvent)
659+
615660
ctx := context.Background()
616661
err := env.StateLog.InsertPendingUpdate(
617662
ctx, env.AssetSpec, newEvent,
@@ -635,6 +680,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
635680
case *CreateTxEvent:
636681
ctx := context.Background()
637682

683+
prefixedLog.Infof("Creating new supply commitment tx")
684+
638685
// To create the new commitment, we'll fetch the unspent pre
639686
// commitments, the current supply root (which may not exist),
640687
// and the new supply root.
@@ -659,6 +706,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
659706
newSupplyCommit, commitPkt, err := newRootCommitment(
660707
ctx, oldCommitment, preCommits, newSupplyRoot,
661708
env.Wallet, env.KeyRing, env.ChainParams,
709+
lfn.Some(prefixedLog),
662710
)
663711
if err != nil {
664712
return nil, fmt.Errorf("unable to create "+
@@ -711,6 +759,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
711759
func (s *CommitTxSignState) ProcessEvent(event Event,
712760
env *Environment) (*StateTransition, error) {
713761

762+
prefixedLog := env.Logger()
763+
714764
switch newEvent := event.(type) {
715765
// If we get a supply update event while we're signing the commit tx,
716766
// we'll just insert it as a dangling update and do a self-transition.
@@ -728,6 +778,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
728778

729779
newEvent.SignalDone(nil)
730780

781+
prefixedLog.Infof("Received new supply update "+
782+
"event: %T", newEvent)
783+
731784
return &StateTransition{
732785
NextState: s,
733786
}, nil
@@ -741,7 +794,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
741794
stateTransition := s.SupplyTransition
742795

743796
// After some initial validation, we'll now sign the PSBT.
744-
log.Debug("Signing supply commitment PSBT")
797+
prefixedLog.Debug("Signing supply commitment PSBT")
745798
signedPsbt, err := env.Wallet.SignPsbt(
746799
ctx, newEvent.CommitPkt.Pkt,
747800
)
@@ -750,10 +803,13 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
750803
"commitment tx: %w", err)
751804
}
752805

806+
prefixedLog.Infof("Signed supply "+
807+
"commitment txn: %v", limitSpewer.Sdump(signedPsbt))
808+
753809
err = psbt.MaybeFinalizeAll(signedPsbt)
754810
if err != nil {
755-
return nil, fmt.Errorf("unable to finalize psbt: %w",
756-
err)
811+
return nil, fmt.Errorf("unable to finalize "+
812+
"psbt: %w", err)
757813
}
758814

759815
commitTx, err := psbt.Extract(signedPsbt)
@@ -805,11 +861,17 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
805861
func (c *CommitBroadcastState) ProcessEvent(event Event,
806862
env *Environment) (*StateTransition, error) {
807863

864+
prefixedLog := env.Logger()
865+
808866
switch newEvent := event.(type) {
809867
// If we get a supply update event while we're broadcasting the commit
810868
// tx, we'll just insert it as a dangling update and do a
811869
// self-transition.
812870
case SyncSupplyUpdateEvent:
871+
prefixedLog.Infof("Received new supply update %T while "+
872+
"finalizing prior commitment, inserting as dangling update",
873+
newEvent)
874+
813875
ctx := context.Background()
814876
err := env.StateLog.InsertPendingUpdate(
815877
ctx, env.AssetSpec, newEvent,
@@ -835,6 +897,11 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
835897
return nil, fmt.Errorf("commitment transaction is nil")
836898
}
837899

900+
commitTxid := c.SupplyTransition.NewCommitment.Txn.TxHash()
901+
prefixedLog.Infof("Broadcasting supply commitment "+
902+
"txn (txid=%v): %v", commitTxid,
903+
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
904+
838905
commitTx := c.SupplyTransition.NewCommitment.Txn
839906

840907
// Construct a detailed label for the broadcast request using
@@ -912,18 +979,20 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
912979
"proof: %w", err)
913980
}
914981

982+
// Now that the transaction has been confirmed, we'll construct
983+
// a merkle proof for the commitment transaction. This'll be
984+
// used to prove that the supply commit is canonical.
915985
stateTransition.ChainProof = lfn.Some(ChainProof{
916986
Header: newEvent.Block.Header,
917987
BlockHeight: newEvent.BlockHeight,
918988
MerkleProof: *merkleProof,
919989
TxIndex: newEvent.TxIndex,
920990
})
921991

922-
// Now that the transaction has been confirmed, we'll construct
923-
// a merkle proof for the commitment transaction. This'll be
924-
// used to prove that the supply commit is canonical.
925-
//
926-
// TODO(roasbeef): need header and txindex, etc to construct
992+
prefixedLog.Infof("Supply commitment txn confirmed "+
993+
"in block %d (hash=%v): %v",
994+
newEvent.BlockHeight, newEvent.Block.Header.BlockHash(),
995+
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
927996

928997
// The commitment has been confirmed, so we'll transition to the
929998
// finalize state, but also log on disk that we no longer need
@@ -960,10 +1029,16 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
9601029
func (c *CommitFinalizeState) ProcessEvent(event Event,
9611030
env *Environment) (*StateTransition, error) {
9621031

1032+
prefixedLog := env.Logger()
1033+
9631034
switch newEvent := event.(type) {
9641035
// If we get a supply update event while we're finalizing the commit,
9651036
// we'll just insert it as a dangling update and do a self-transition.
9661037
case SyncSupplyUpdateEvent:
1038+
prefixedLog.Infof("Received new supply update %T while "+
1039+
"finalizing prior commitment, inserting as dangling "+
1040+
"update", newEvent)
1041+
9671042
ctx := context.Background()
9681043
err := env.StateLog.InsertPendingUpdate(
9691044
ctx, env.AssetSpec, newEvent,
@@ -987,6 +1062,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
9871062
case *FinalizeEvent:
9881063
ctx := context.Background()
9891064

1065+
prefixedLog.Infof("Finalizing supply commitment transition")
1066+
9901067
// At this point, the commitment has been confirmed on disk, so
9911068
// we can update: the state machine state on disk, and swap in
9921069
// all the new supply tree information.
@@ -1021,6 +1098,9 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
10211098
}, nil
10221099
}
10231100

1101+
prefixedLog.Infof("Dangling updates found: %d",
1102+
len(danglingUpdates))
1103+
10241104
// Otherwise, we have more work to do! We'll kick off a new
10251105
// commitment cycle right away by transitioning to the tree
10261106
// creation state.

0 commit comments

Comments
 (0)