Skip to content

Commit d003bfc

Browse files
committed
universe/supplycommit: add additional logging
1 parent 81f33ea commit d003bfc

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
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"
@@ -523,6 +524,9 @@ type Environment struct {
523524
// IgnoreCheckerCache is used to invalidate the ignore cache when a new
524525
// supply commitment is created.
525526
IgnoreCheckerCache IgnoreCheckerCache
527+
528+
// Log is the prefixed logger for this supply commitment state machine.
529+
Log btclog.Logger
526530
}
527531

528532
// SupplyCommitTxn encapsulates the details of the transaction that creates a
@@ -548,3 +552,14 @@ type SupplyCommitTxn struct {
548552
func (e *Environment) Name() string {
549553
return fmt.Sprintf("universe_supply_commit(%v)", e.AssetSpec)
550554
}
555+
556+
// Logger returns the logger for this environment. If a logger was provided in
557+
// the environment configuration, it returns that logger. Otherwise, it returns
558+
// the package-level logger with an asset-specific prefix.
559+
func (e *Environment) Logger() btclog.Logger {
560+
if e.Log != nil {
561+
return e.Log
562+
}
563+
564+
return NewAssetLogger(e.AssetSpec.String())
565+
}

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: 93 additions & 13 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/fn"
1314
"github.com/lightninglabs/taproot-assets/mssmt"
@@ -67,11 +68,16 @@ func createCommitmentTxLabel(assetSpec asset.Specifier,
6768
func (d *DefaultState) ProcessEvent(event Event,
6869
env *Environment) (*StateTransition, error) {
6970

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

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

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

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

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

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

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

402431
newCommitTx := wire.NewMsgTx(2)
403432

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

441475
bip32Derivation, trBip32Derivation :=
@@ -460,11 +494,12 @@ func newRootCommitment(ctx context.Context,
460494
})
461495
})
462496

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

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

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

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

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

730780
newEvent.SignalDone(nil)
731781

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

744797
// After some initial validation, we'll now sign the PSBT.
745-
log.Debug("Signing supply commitment PSBT")
798+
prefixedLog.Debug("Signing supply commitment PSBT")
746799
signedPsbt, err := env.Wallet.SignPsbt(
747800
ctx, newEvent.CommitPkt.Pkt,
748801
)
@@ -751,10 +804,13 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
751804
"commitment tx: %w", err)
752805
}
753806

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

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

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

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

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

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

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

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

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

1066+
prefixedLog.Infof("Finalizing supply commitment transition")
1067+
9911068
// At this point, the commitment has been confirmed on disk, so
9921069
// we can update: the state machine state on disk, and swap in
9931070
// all the new supply tree information.
@@ -1041,6 +1118,9 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
10411118
}, nil
10421119
}
10431120

1121+
prefixedLog.Infof("Dangling updates found: %d",
1122+
len(danglingUpdates))
1123+
10441124
// Otherwise, we have more work to do! We'll kick off a new
10451125
// commitment cycle right away by transitioning to the tree
10461126
// creation state.

0 commit comments

Comments
 (0)