Skip to content

Commit 5d4767d

Browse files
committed
universe/supplycommit: add additional logging
1 parent d974383 commit 5d4767d

File tree

2 files changed

+123
-12
lines changed

2 files changed

+123
-12
lines changed

universe/supplycommit/log.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package supplycommit
22

33
import (
44
"github.com/btcsuite/btclog/v2"
5+
"github.com/davecgh/go-spew/spew"
56
)
67

78
// Subsystem defines the logging code for this subsystem.
@@ -18,6 +19,13 @@ func DisableLog() {
1819
UseLogger(btclog.Disabled)
1920
}
2021

22+
// limitSpewer is a spew.ConfigState that limits the depth of the output to 4
23+
// levels, so it can safely be used for things that contain an MS-SMT tree.
24+
var limitSpewer = &spew.ConfigState{
25+
Indent: " ",
26+
MaxDepth: 4,
27+
}
28+
2129
// UseLogger uses a specified Logger to output package logging info.
2230
// This should be used in preference to SetLogWriter if the caller is also
2331
// using btclog.

universe/supplycommit/transitions.go

Lines changed: 115 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,19 @@ func createCommitmentTxLabel(assetSpec asset.Specifier,
6667
func (d *DefaultState) ProcessEvent(event Event,
6768
env *Environment) (*StateTransition, error) {
6869

70+
// Create a prefixed logger for this supply commit.
71+
prefixedLog := log.WithPrefix(
72+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
73+
)
74+
6975
// In this state, we'll receive one of three events: a new burn, a new
7076
// mint, or a new ignore. For all three types, we'll emit this as an
7177
// internal event as we transition to the UpdatePendingState.
7278
switch supplyEvent := event.(type) {
7379
case SyncSupplyUpdateEvent:
80+
prefixedLog.Infof("Received new supply update event: %T",
81+
supplyEvent)
82+
7483
// Before we transition to the next state, we'll add this event
7584
// to our update log. This ensures that we'll remember to
7685
// process from this state after a restart.
@@ -120,10 +129,18 @@ func (d *DefaultState) ProcessEvent(event Event,
120129
func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
121130
*StateTransition, error) {
122131

132+
// Create a prefixed logger for this supply commit.
133+
prefixedLog := log.WithPrefix(
134+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
135+
)
136+
123137
switch newEvent := event.(type) {
124138
// We've received a new update event, we'll stage this in our local
125139
// state, and do a self transition.
126140
case SyncSupplyUpdateEvent:
141+
prefixedLog.Infof("Received new supply update event: %T",
142+
newEvent)
143+
127144
// We just got a new pending update in addition to the one that
128145
// made us transition to this state. This ensures that we'll
129146
// remember to process from this state after a restart.
@@ -163,6 +180,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
163180
"pending transition: %w", err)
164181
}
165182

183+
prefixedLog.Infof("Received tick event, committing %d "+
184+
"supply updates", len(u.pendingUpdates))
185+
166186
return &StateTransition{
167187
NextState: &CommitTreeCreateState{},
168188
NewEvents: lfn.Some(FsmEvent{
@@ -254,13 +274,23 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
254274
func (c *CommitTreeCreateState) ProcessEvent(event Event,
255275
env *Environment) (*StateTransition, error) {
256276

277+
// Create a prefixed logger for this supply commit.
278+
//
279+
// TODO(roasbeef): put this in the env?
280+
prefixedLog := log.WithPrefix(
281+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
282+
)
283+
257284
// TODO(roasbeef): cache attemps to add new elements? or main state
258285
// driver still single threaded?
259286

260287
switch newEvent := event.(type) {
261288
// If we get a supply update event while we're creating the tree,
262289
// we'll just insert it as a dangling update and do a self-transition.
263290
case SyncSupplyUpdateEvent:
291+
prefixedLog.Infof("Received new supply update event: %T",
292+
newEvent)
293+
264294
ctx := context.Background()
265295
err := env.StateLog.InsertPendingUpdate(
266296
ctx, env.AssetSpec, newEvent,
@@ -291,6 +321,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
291321
case *CreateTreeEvent:
292322
pendingUpdates := newEvent.updatesToCommit
293323

324+
prefixedLog.Infof("Creating new supply trees "+
325+
"with %d pending updates",
326+
len(pendingUpdates))
327+
294328
// TODO(ffranr): Pass in context?
295329
ctx := context.Background()
296330

@@ -396,7 +430,13 @@ func newRootCommitment(ctx context.Context,
396430
oldCommitment lfn.Option[RootCommitment],
397431
unspentPreCommits []PreCommitment, newSupplyRoot *mssmt.BranchNode,
398432
wallet Wallet, keyRing KeyRing,
399-
chainParams chaincfg.Params) (*RootCommitment, *psbt.Packet, error) {
433+
chainParams chaincfg.Params,
434+
logger lfn.Option[btclog.Logger]) (*RootCommitment, *psbt.Packet, error) {
435+
436+
logger.WhenSome(func(l btclog.Logger) {
437+
l.Infof("Creating new root commitment, spending %v "+
438+
"pre-commits", len(unspentPreCommits))
439+
})
400440

401441
newCommitTx := wire.NewMsgTx(2)
402442

@@ -435,6 +475,11 @@ func newRootCommitment(ctx context.Context,
435475
// on mint transactions where as the old commitment is the last
436476
// commitment that was broadcast.
437477
oldCommitment.WhenSome(func(r RootCommitment) {
478+
logger.WhenSome(func(l btclog.Logger) {
479+
l.Infof("Re-using prior commitment as outpoint=%v: %v",
480+
r.CommitPoint(), limitSpewer.Sdump(r))
481+
})
482+
438483
newCommitTx.AddTxIn(r.TxIn())
439484

440485
bip32Derivation, trBip32Derivation :=
@@ -459,11 +504,12 @@ func newRootCommitment(ctx context.Context,
459504
})
460505
})
461506

507+
// TODO(roasbef): do CreateTaprootSignature instead?
462508
// With the inputs available, derive the supply commitment output.
463509
//
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.
510+
// Determine the internal key to use for this output. If a prior root
511+
// commitment exists, reuse its internal key; otherwise, generate a new
512+
// one.
467513
iKeyOpt := lfn.MapOption(func(r RootCommitment) keychain.KeyDescriptor {
468514
return r.InternalKey
469515
})(oldCommitment)
@@ -534,6 +580,11 @@ func newRootCommitment(ctx context.Context,
534580
SupplyRoot: newSupplyRoot,
535581
}
536582

583+
logger.WhenSome(func(l btclog.Logger) {
584+
l.Infof("Created new root commitment: %v",
585+
limitSpewer.Sdump(newSupplyCommit))
586+
})
587+
537588
commitPkt, err := psbt.NewFromUnsignedTx(newCommitTx)
538589
if err != nil {
539590
return nil, nil, fmt.Errorf("unable to create PSBT: %w", err)
@@ -608,10 +659,18 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
608659
func (c *CommitTxCreateState) ProcessEvent(event Event,
609660
env *Environment) (*StateTransition, error) {
610661

662+
// Create a prefixed logger for this supply commit.
663+
prefixedLog := log.WithPrefix(fmt.Sprintf(
664+
"SupplyCommit(%v): ", env.AssetSpec.String()),
665+
)
666+
611667
switch newEvent := event.(type) {
612668
// If we get a supply update event while we're creating the commit tx,
613669
// we'll just insert it as a dangling update and do a self-transition.
614670
case SyncSupplyUpdateEvent:
671+
prefixedLog.Infof("Received new supply update event: %T",
672+
newEvent)
673+
615674
ctx := context.Background()
616675
err := env.StateLog.InsertPendingUpdate(
617676
ctx, env.AssetSpec, newEvent,
@@ -635,6 +694,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
635694
case *CreateTxEvent:
636695
ctx := context.Background()
637696

697+
prefixedLog.Infof("Creating new supply commitment tx")
698+
638699
// To create the new commitment, we'll fetch the unspent pre
639700
// commitments, the current supply root (which may not exist),
640701
// and the new supply root.
@@ -659,6 +720,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
659720
newSupplyCommit, commitPkt, err := newRootCommitment(
660721
ctx, oldCommitment, preCommits, newSupplyRoot,
661722
env.Wallet, env.KeyRing, env.ChainParams,
723+
lfn.Some(prefixedLog),
662724
)
663725
if err != nil {
664726
return nil, fmt.Errorf("unable to create "+
@@ -711,6 +773,11 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
711773
func (s *CommitTxSignState) ProcessEvent(event Event,
712774
env *Environment) (*StateTransition, error) {
713775

776+
// Create a prefixed logger for this supply commit.
777+
prefixedLog := log.WithPrefix(
778+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
779+
)
780+
714781
switch newEvent := event.(type) {
715782
// If we get a supply update event while we're signing the commit tx,
716783
// we'll just insert it as a dangling update and do a self-transition.
@@ -728,6 +795,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
728795

729796
newEvent.SignalDone(nil)
730797

798+
prefixedLog.Infof("Received new supply update "+
799+
"event: %T", newEvent)
800+
731801
return &StateTransition{
732802
NextState: s,
733803
}, nil
@@ -741,7 +811,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
741811
stateTransition := s.SupplyTransition
742812

743813
// After some initial validation, we'll now sign the PSBT.
744-
log.Debug("Signing supply commitment PSBT")
814+
prefixedLog.Debug("Signing supply commitment PSBT")
745815
signedPsbt, err := env.Wallet.SignPsbt(
746816
ctx, newEvent.CommitPkt.Pkt,
747817
)
@@ -750,10 +820,13 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
750820
"commitment tx: %w", err)
751821
}
752822

823+
prefixedLog.Infof("Signed supply "+
824+
"commitment txn: %v", limitSpewer.Sdump(signedPsbt))
825+
753826
err = psbt.MaybeFinalizeAll(signedPsbt)
754827
if err != nil {
755-
return nil, fmt.Errorf("unable to finalize psbt: %w",
756-
err)
828+
return nil, fmt.Errorf("unable to finalize "+
829+
"psbt: %w", err)
757830
}
758831

759832
commitTx, err := psbt.Extract(signedPsbt)
@@ -805,11 +878,20 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
805878
func (c *CommitBroadcastState) ProcessEvent(event Event,
806879
env *Environment) (*StateTransition, error) {
807880

881+
// Create a prefixed logger for this supply commit.
882+
prefixedLog := log.WithPrefix(
883+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
884+
)
885+
808886
switch newEvent := event.(type) {
809887
// If we get a supply update event while we're broadcasting the commit
810888
// tx, we'll just insert it as a dangling update and do a
811889
// self-transition.
812890
case SyncSupplyUpdateEvent:
891+
prefixedLog.Infof("Received new supply update %T while "+
892+
"finalizing prior commitment, inserting as dangling update",
893+
newEvent)
894+
813895
ctx := context.Background()
814896
err := env.StateLog.InsertPendingUpdate(
815897
ctx, env.AssetSpec, newEvent,
@@ -835,6 +917,11 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
835917
return nil, fmt.Errorf("commitment transaction is nil")
836918
}
837919

920+
commitTxid := c.SupplyTransition.NewCommitment.Txn.TxHash()
921+
prefixedLog.Infof("Broadcasting supply commitment "+
922+
"txn (txid=%v): %v", commitTxid,
923+
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
924+
838925
commitTx := c.SupplyTransition.NewCommitment.Txn
839926

840927
// Construct a detailed label for the broadcast request using
@@ -912,18 +999,20 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
912999
"proof: %w", err)
9131000
}
9141001

1002+
// Now that the transaction has been confirmed, we'll construct
1003+
// a merkle proof for the commitment transaction. This'll be
1004+
// used to prove that the supply commit is canonical.
9151005
stateTransition.ChainProof = lfn.Some(ChainProof{
9161006
Header: newEvent.Block.Header,
9171007
BlockHeight: newEvent.BlockHeight,
9181008
MerkleProof: *merkleProof,
9191009
TxIndex: newEvent.TxIndex,
9201010
})
9211011

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
1012+
prefixedLog.Infof("Supply commitment txn confirmed "+
1013+
"in block %d (hash=%v): %v",
1014+
newEvent.BlockHeight, newEvent.Block.Header.BlockHash(),
1015+
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
9271016

9281017
// The commitment has been confirmed, so we'll transition to the
9291018
// finalize state, but also log on disk that we no longer need
@@ -960,10 +1049,19 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
9601049
func (c *CommitFinalizeState) ProcessEvent(event Event,
9611050
env *Environment) (*StateTransition, error) {
9621051

1052+
// Create a prefixed logger for this supply commit.
1053+
prefixedLog := log.WithPrefix(
1054+
fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()),
1055+
)
1056+
9631057
switch newEvent := event.(type) {
9641058
// If we get a supply update event while we're finalizing the commit,
9651059
// we'll just insert it as a dangling update and do a self-transition.
9661060
case SyncSupplyUpdateEvent:
1061+
prefixedLog.Infof("Received new supply update %T while "+
1062+
"finalizing prior commitment, inserting as dangling "+
1063+
"update", newEvent)
1064+
9671065
ctx := context.Background()
9681066
err := env.StateLog.InsertPendingUpdate(
9691067
ctx, env.AssetSpec, newEvent,
@@ -987,6 +1085,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
9871085
case *FinalizeEvent:
9881086
ctx := context.Background()
9891087

1088+
prefixedLog.Infof("Finalizing supply commitment transition")
1089+
9901090
// At this point, the commitment has been confirmed on disk, so
9911091
// we can update: the state machine state on disk, and swap in
9921092
// all the new supply tree information.
@@ -1021,6 +1121,9 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
10211121
}, nil
10221122
}
10231123

1124+
prefixedLog.Infof("Dangling updates found: %d",
1125+
len(danglingUpdates))
1126+
10241127
// Otherwise, we have more work to do! We'll kick off a new
10251128
// commitment cycle right away by transitioning to the tree
10261129
// creation state.

0 commit comments

Comments
 (0)