Skip to content

Commit 15f7297

Browse files
committed
universe/supplycommit: add additional logging
1 parent a9cedbb commit 15f7297

File tree

2 files changed

+104
-14
lines changed

2 files changed

+104
-14
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: 96 additions & 14 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,12 +67,14 @@ 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(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
72+
6973
// In this state, we'll receive one of three events: a new burn, a new
7074
// mint, or a new ignore. For all three types, we'll emit this as an
7175
// internal event as we transition to the UpdatePendingState.
7276
switch supplyEvent := event.(type) {
7377
case SupplyUpdateEvent:
74-
7578
// Before we transition to the next state, we'll add this event
7679
// to our update log. This ensures that we'll remember to
7780
// process from this state after a restart.
@@ -86,6 +89,8 @@ func (d *DefaultState) ProcessEvent(event Event,
8689
"pending update: %w", err)
8790
}
8891

92+
prefixedLog.Infof("Received new supply update event: %T", supplyEvent)
93+
8994
// We'll go to the next state, caching the pendingUpdate we just
9095
// received.
9196
return &StateTransition{
@@ -117,11 +122,13 @@ func (d *DefaultState) ProcessEvent(event Event,
117122
func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
118123
*StateTransition, error) {
119124

125+
// Create a prefixed logger for this supply commit.
126+
prefixedLog := log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
127+
120128
switch newEvent := event.(type) {
121129
// We've received a new update event, we'll stage this in our local
122130
// state, and do a self transition.
123131
case SupplyUpdateEvent:
124-
125132
// We just got a new pending update in addition to the one that
126133
// made us transition to this state. This ensures that we'll
127134
// remember to process from this state after a restart.
@@ -134,6 +141,8 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
134141
"pending update: %w", err)
135142
}
136143

144+
prefixedLog.Infof("Received new supply update event: %T", newEvent)
145+
137146
// We'll go to the next state, caching the pendingUpdate we just
138147
// received.
139148
currentUpdates := append(u.pendingUpdates, newEvent)
@@ -157,6 +166,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
157166
"pending transition: %w", err)
158167
}
159168

169+
prefixedLog.Infof("Received tick event, committing %d supply updates",
170+
len(u.pendingUpdates))
171+
160172
return &StateTransition{
161173
NextState: &CommitTreeCreateState{},
162174
NewEvents: lfn.Some(FsmEvent{
@@ -248,12 +260,17 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
248260
func (c *CommitTreeCreateState) ProcessEvent(event Event,
249261
env *Environment) (*StateTransition, error) {
250262

263+
// Create a prefixed logger for this supply commit.
264+
//
265+
// TODO(roasbeef): put this in the env?
266+
prefixedLog := log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
267+
251268
// TODO(roasbeef): cache attemps to add new elements? or main state
252269
// driver still single threaded?
253270

254271
switch newEvent := event.(type) {
255-
// If we get a supply update event while we're creating the tree,
256-
// we'll just insert it as a dangling update and do a self-transition.
272+
// If we get a supply update event while we're creating the tree, we'll
273+
// just insert it as a dangling update and do a self-transition.
257274
case SupplyUpdateEvent:
258275
ctx := context.Background()
259276
err := env.StateLog.InsertPendingUpdate(
@@ -264,6 +281,8 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
264281
"pending update: %w", err)
265282
}
266283

284+
prefixedLog.Infof("Received new supply update event: %T", newEvent)
285+
267286
return &StateTransition{
268287
NextState: c,
269288
}, nil
@@ -281,6 +300,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
281300
case *CreateTreeEvent:
282301
pendingUpdates := newEvent.updatesToCommit
283302

303+
prefixedLog.Infof("Creating new supply trees "+
304+
"with %d pending updates",
305+
len(pendingUpdates))
306+
284307
// TODO(ffranr): Pass in context?
285308
ctx := context.Background()
286309

@@ -386,7 +409,13 @@ func newRootCommitment(ctx context.Context,
386409
oldCommitment lfn.Option[RootCommitment],
387410
unspentPreCommits []PreCommitment, newSupplyRoot *mssmt.BranchNode,
388411
wallet Wallet, keyRing KeyRing,
389-
chainParams chaincfg.Params) (*RootCommitment, *psbt.Packet, error) {
412+
chainParams chaincfg.Params,
413+
logger lfn.Option[btclog.Logger]) (*RootCommitment, *psbt.Packet, error) {
414+
415+
logger.WhenSome(func(l btclog.Logger) {
416+
l.Infof("Creating new root commitment, spending %v "+
417+
"pre-commits", len(unspentPreCommits))
418+
})
390419

391420
newCommitTx := wire.NewMsgTx(2)
392421

@@ -425,6 +454,11 @@ func newRootCommitment(ctx context.Context,
425454
// on mint transactions where as the old commitment is the last
426455
// commitment that was broadcast.
427456
oldCommitment.WhenSome(func(r RootCommitment) {
457+
logger.WhenSome(func(l btclog.Logger) {
458+
l.Infof("Re-using prior commitment as outpoint=%v: %v",
459+
r.CommitPoint(), limitSpewer.Sdump(r))
460+
})
461+
428462
newCommitTx.AddTxIn(r.TxIn())
429463

430464
bip32Derivation, trBip32Derivation :=
@@ -449,11 +483,12 @@ func newRootCommitment(ctx context.Context,
449483
})
450484
})
451485

486+
// TODO(roasbef): do CreateTaprootSignature instead?
452487
// With the inputs available, derive the supply commitment output.
453488
//
454-
// Determine the internal key to use for this output.
455-
// If a prior root commitment exists, reuse its internal key;
456-
// otherwise, generate a new one.
489+
// Determine the internal key to use for this output. If a prior root
490+
// commitment exists, reuse its internal key; otherwise, generate a new
491+
// one.
457492
iKeyOpt := lfn.MapOption(func(r RootCommitment) keychain.KeyDescriptor {
458493
return r.InternalKey
459494
})(oldCommitment)
@@ -524,6 +559,11 @@ func newRootCommitment(ctx context.Context,
524559
SupplyRoot: newSupplyRoot,
525560
}
526561

562+
logger.WhenSome(func(l btclog.Logger) {
563+
l.Infof("Created new root commitment: %v",
564+
limitSpewer.Sdump(newSupplyCommit))
565+
})
566+
527567
commitPkt, err := psbt.NewFromUnsignedTx(newCommitTx)
528568
if err != nil {
529569
return nil, nil, fmt.Errorf("unable to create PSBT: %w", err)
@@ -598,6 +638,11 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
598638
func (c *CommitTxCreateState) ProcessEvent(event Event,
599639
env *Environment) (*StateTransition, error) {
600640

641+
// Create a prefixed logger for this supply commit.
642+
prefixedLog := log.WithPrefix(fmt.Sprintf(
643+
"SupplyCommit(%v): ", env.AssetSpec.String()),
644+
)
645+
601646
switch newEvent := event.(type) {
602647
// If we get a supply update event while we're creating the commit tx,
603648
// we'll just insert it as a dangling update and do a self-transition.
@@ -611,6 +656,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
611656
"pending update: %w", err)
612657
}
613658

659+
prefixedLog.Infof("Received new supply update event: %T", newEvent)
660+
614661
return &StateTransition{
615662
NextState: c,
616663
}, nil
@@ -621,6 +668,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
621668
case *CreateTxEvent:
622669
ctx := context.Background()
623670

671+
prefixedLog.Infof("Creating new supply commitment tx")
672+
624673
// To create the new commitment, we'll fetch the unspent pre
625674
// commitments, the current supply root (which may not exist),
626675
// and the new supply root.
@@ -645,6 +694,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
645694
newSupplyCommit, commitPkt, err := newRootCommitment(
646695
ctx, oldCommitment, preCommits, newSupplyRoot,
647696
env.Wallet, env.KeyRing, env.ChainParams,
697+
lfn.Some(prefixedLog),
648698
)
649699
if err != nil {
650700
return nil, fmt.Errorf("unable to create "+
@@ -697,6 +747,9 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
697747
func (s *CommitTxSignState) ProcessEvent(event Event,
698748
env *Environment) (*StateTransition, error) {
699749

750+
// Create a prefixed logger for this supply commit.
751+
prefixedLog := log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
752+
700753
switch newEvent := event.(type) {
701754
// If we get a supply update event while we're signing the commit tx,
702755
// we'll just insert it as a dangling update and do a self-transition.
@@ -710,6 +763,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
710763
"pending update: %w", err)
711764
}
712765

766+
prefixedLog.Infof("Received new supply update "+
767+
"event: %T", newEvent)
768+
713769
return &StateTransition{
714770
NextState: s,
715771
}, nil
@@ -723,7 +779,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
723779
stateTransition := s.SupplyTransition
724780

725781
// After some initial validation, we'll now sign the PSBT.
726-
log.Debug("Signing supply commitment PSBT")
782+
prefixedLog.Debug("Signing supply commitment PSBT")
727783
signedPsbt, err := env.Wallet.SignPsbt(
728784
ctx, newEvent.CommitPkt.Pkt,
729785
)
@@ -732,6 +788,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
732788
"commitment tx: %w", err)
733789
}
734790

791+
prefixedLog.Infof("Signed supply "+
792+
"commitment txn: %v", limitSpewer.Sdump(signedPsbt))
793+
735794
err = psbt.MaybeFinalizeAll(signedPsbt)
736795
if err != nil {
737796
return nil, fmt.Errorf("unable to finalize psbt: %w",
@@ -787,11 +846,18 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
787846
func (c *CommitBroadcastState) ProcessEvent(event Event,
788847
env *Environment) (*StateTransition, error) {
789848

849+
// Create a prefixed logger for this supply commit.
850+
prefixedLog := log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
851+
790852
switch newEvent := event.(type) {
791853
// If we get a supply update event while we're broadcasting the commit
792854
// tx, we'll just insert it as a dangling update and do a
793855
// self-transition.
794856
case SupplyUpdateEvent:
857+
prefixedLog.Infof("Received new supply update %T while "+
858+
"finalizing prior commitment, inserting as dangling update",
859+
newEvent)
860+
795861
ctx := context.Background()
796862
err := env.StateLog.InsertPendingUpdate(
797863
ctx, env.AssetSpec, newEvent,
@@ -813,6 +879,10 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
813879
return nil, fmt.Errorf("commitment transaction is nil")
814880
}
815881

882+
commitTxid := c.SupplyTransition.NewCommitment.Txn.TxHash()
883+
prefixedLog.Infof("Broadcasting supply commitment txn (txid=%v): %v",
884+
commitTxid, limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
885+
816886
commitTx := c.SupplyTransition.NewCommitment.Txn
817887

818888
// Construct a detailed label for the broadcast request using
@@ -890,18 +960,19 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
890960
"proof: %w", err)
891961
}
892962

963+
// Now that the transaction has been confirmed, we'll construct
964+
// a merkle proof for the commitment transaction. This'll be
965+
// used to prove that the supply commit is canonical.
893966
stateTransition.ChainProof = lfn.Some(ChainProof{
894967
Header: newEvent.Block.Header,
895968
BlockHeight: newEvent.BlockHeight,
896969
MerkleProof: *merkleProof,
897970
TxIndex: newEvent.TxIndex,
898971
})
899972

900-
// Now that the transaction has been confirmed, we'll construct
901-
// a merkle proof for the commitment transaction. This'll be
902-
// used to prove that the supply commit is canonical.
903-
//
904-
// TODO(roasbeef): need header and txindex, etc to construct
973+
prefixedLog.Infof("Supply commitment txn confirmed in block %d (hash=%v): %v",
974+
newEvent.BlockHeight, newEvent.Block.Header.BlockHash(),
975+
limitSpewer.Sdump(c.SupplyTransition.NewCommitment.Txn))
905976

906977
// The commitment has been confirmed, so we'll transition to the
907978
// finalize state, but also log on disk that we no longer need
@@ -938,10 +1009,17 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
9381009
func (c *CommitFinalizeState) ProcessEvent(event Event,
9391010
env *Environment) (*StateTransition, error) {
9401011

1012+
// Create a prefixed logger for this supply commit.
1013+
prefixedLog := log.WithPrefix(fmt.Sprintf("SupplyCommit(%v): ", env.AssetSpec.String()))
1014+
9411015
switch newEvent := event.(type) {
9421016
// If we get a supply update event while we're finalizing the commit,
9431017
// we'll just insert it as a dangling update and do a self-transition.
9441018
case SupplyUpdateEvent:
1019+
prefixedLog.Infof("Received new supply update %T while "+
1020+
"finalizing prior commitment, inserting as dangling update",
1021+
newEvent)
1022+
9451023
ctx := context.Background()
9461024
err := env.StateLog.InsertPendingUpdate(
9471025
ctx, env.AssetSpec, newEvent,
@@ -961,6 +1039,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
9611039
case *FinalizeEvent:
9621040
ctx := context.Background()
9631041

1042+
prefixedLog.Infof("Finalizing supply commitment transition")
1043+
9641044
// At this point, the commitment has been confirmed on disk, so
9651045
// we can update: the state machine state on disk, and swap in
9661046
// all the new supply tree information.
@@ -995,6 +1075,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
9951075
}, nil
9961076
}
9971077

1078+
prefixedLog.Infof("Dangling updates found: %d", len(danglingUpdates))
1079+
9981080
// Otherwise, we have more work to do! We'll kick off a new
9991081
// commitment cycle right away by transitioning to the tree
10001082
// creation state.

0 commit comments

Comments
 (0)