8
8
"github.com/btcsuite/btcd/btcutil/psbt"
9
9
"github.com/btcsuite/btcd/chaincfg"
10
10
"github.com/btcsuite/btcd/wire"
11
+ "github.com/btcsuite/btclog/v2"
11
12
"github.com/lightninglabs/taproot-assets/asset"
12
13
"github.com/lightninglabs/taproot-assets/mssmt"
13
14
"github.com/lightninglabs/taproot-assets/proof"
@@ -66,12 +67,14 @@ func createCommitmentTxLabel(assetSpec asset.Specifier,
66
67
func (d * DefaultState ) ProcessEvent (event Event ,
67
68
env * Environment ) (* StateTransition , error ) {
68
69
70
+ // Create a prefixed logger for this supply commit.
71
+ prefixedLog := log .WithPrefix (fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()))
72
+
69
73
// In this state, we'll receive one of three events: a new burn, a new
70
74
// mint, or a new ignore. For all three types, we'll emit this as an
71
75
// internal event as we transition to the UpdatePendingState.
72
76
switch supplyEvent := event .(type ) {
73
77
case SupplyUpdateEvent :
74
-
75
78
// Before we transition to the next state, we'll add this event
76
79
// to our update log. This ensures that we'll remember to
77
80
// process from this state after a restart.
@@ -86,6 +89,8 @@ func (d *DefaultState) ProcessEvent(event Event,
86
89
"pending update: %w" , err )
87
90
}
88
91
92
+ prefixedLog .Infof ("Received new supply update event: %T" , supplyEvent )
93
+
89
94
// We'll go to the next state, caching the pendingUpdate we just
90
95
// received.
91
96
return & StateTransition {
@@ -117,11 +122,13 @@ func (d *DefaultState) ProcessEvent(event Event,
117
122
func (u * UpdatesPendingState ) ProcessEvent (event Event , env * Environment ) (
118
123
* StateTransition , error ) {
119
124
125
+ // Create a prefixed logger for this supply commit.
126
+ prefixedLog := log .WithPrefix (fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()))
127
+
120
128
switch newEvent := event .(type ) {
121
129
// We've received a new update event, we'll stage this in our local
122
130
// state, and do a self transition.
123
131
case SupplyUpdateEvent :
124
-
125
132
// We just got a new pending update in addition to the one that
126
133
// made us transition to this state. This ensures that we'll
127
134
// remember to process from this state after a restart.
@@ -134,6 +141,8 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
134
141
"pending update: %w" , err )
135
142
}
136
143
144
+ prefixedLog .Infof ("Received new supply update event: %T" , newEvent )
145
+
137
146
// We'll go to the next state, caching the pendingUpdate we just
138
147
// received.
139
148
currentUpdates := append (u .pendingUpdates , newEvent )
@@ -157,6 +166,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
157
166
"pending transition: %w" , err )
158
167
}
159
168
169
+ prefixedLog .Infof ("Received tick event, committing %d supply updates" ,
170
+ len (u .pendingUpdates ))
171
+
160
172
return & StateTransition {
161
173
NextState : & CommitTreeCreateState {},
162
174
NewEvents : lfn .Some (FsmEvent {
@@ -248,12 +260,17 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
248
260
func (c * CommitTreeCreateState ) ProcessEvent (event Event ,
249
261
env * Environment ) (* StateTransition , error ) {
250
262
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
+
251
268
// TODO(roasbeef): cache attemps to add new elements? or main state
252
269
// driver still single threaded?
253
270
254
271
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.
257
274
case SupplyUpdateEvent :
258
275
ctx := context .Background ()
259
276
err := env .StateLog .InsertPendingUpdate (
@@ -264,6 +281,8 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
264
281
"pending update: %w" , err )
265
282
}
266
283
284
+ prefixedLog .Infof ("Received new supply update event: %T" , newEvent )
285
+
267
286
return & StateTransition {
268
287
NextState : c ,
269
288
}, nil
@@ -281,6 +300,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
281
300
case * CreateTreeEvent :
282
301
pendingUpdates := newEvent .updatesToCommit
283
302
303
+ prefixedLog .Infof ("Creating new supply trees " +
304
+ "with %d pending updates" ,
305
+ len (pendingUpdates ))
306
+
284
307
// TODO(ffranr): Pass in context?
285
308
ctx := context .Background ()
286
309
@@ -386,7 +409,13 @@ func newRootCommitment(ctx context.Context,
386
409
oldCommitment lfn.Option [RootCommitment ],
387
410
unspentPreCommits []PreCommitment , newSupplyRoot * mssmt.BranchNode ,
388
411
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
+ })
390
419
391
420
newCommitTx := wire .NewMsgTx (2 )
392
421
@@ -425,6 +454,11 @@ func newRootCommitment(ctx context.Context,
425
454
// on mint transactions where as the old commitment is the last
426
455
// commitment that was broadcast.
427
456
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
+
428
462
newCommitTx .AddTxIn (r .TxIn ())
429
463
430
464
bip32Derivation , trBip32Derivation :=
@@ -449,11 +483,12 @@ func newRootCommitment(ctx context.Context,
449
483
})
450
484
})
451
485
486
+ // TODO(roasbef): do CreateTaprootSignature instead?
452
487
// With the inputs available, derive the supply commitment output.
453
488
//
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.
457
492
iKeyOpt := lfn .MapOption (func (r RootCommitment ) keychain.KeyDescriptor {
458
493
return r .InternalKey
459
494
})(oldCommitment )
@@ -524,6 +559,11 @@ func newRootCommitment(ctx context.Context,
524
559
SupplyRoot : newSupplyRoot ,
525
560
}
526
561
562
+ logger .WhenSome (func (l btclog.Logger ) {
563
+ l .Infof ("Created new root commitment: %v" ,
564
+ limitSpewer .Sdump (newSupplyCommit ))
565
+ })
566
+
527
567
commitPkt , err := psbt .NewFromUnsignedTx (newCommitTx )
528
568
if err != nil {
529
569
return nil , nil , fmt .Errorf ("unable to create PSBT: %w" , err )
@@ -598,6 +638,11 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
598
638
func (c * CommitTxCreateState ) ProcessEvent (event Event ,
599
639
env * Environment ) (* StateTransition , error ) {
600
640
641
+ // Create a prefixed logger for this supply commit.
642
+ prefixedLog := log .WithPrefix (fmt .Sprintf (
643
+ "SupplyCommit(%v): " , env .AssetSpec .String ()),
644
+ )
645
+
601
646
switch newEvent := event .(type ) {
602
647
// If we get a supply update event while we're creating the commit tx,
603
648
// we'll just insert it as a dangling update and do a self-transition.
@@ -611,6 +656,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
611
656
"pending update: %w" , err )
612
657
}
613
658
659
+ prefixedLog .Infof ("Received new supply update event: %T" , newEvent )
660
+
614
661
return & StateTransition {
615
662
NextState : c ,
616
663
}, nil
@@ -621,6 +668,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
621
668
case * CreateTxEvent :
622
669
ctx := context .Background ()
623
670
671
+ prefixedLog .Infof ("Creating new supply commitment tx" )
672
+
624
673
// To create the new commitment, we'll fetch the unspent pre
625
674
// commitments, the current supply root (which may not exist),
626
675
// and the new supply root.
@@ -645,6 +694,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
645
694
newSupplyCommit , commitPkt , err := newRootCommitment (
646
695
ctx , oldCommitment , preCommits , newSupplyRoot ,
647
696
env .Wallet , env .KeyRing , env .ChainParams ,
697
+ lfn .Some (prefixedLog ),
648
698
)
649
699
if err != nil {
650
700
return nil , fmt .Errorf ("unable to create " +
@@ -697,6 +747,9 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
697
747
func (s * CommitTxSignState ) ProcessEvent (event Event ,
698
748
env * Environment ) (* StateTransition , error ) {
699
749
750
+ // Create a prefixed logger for this supply commit.
751
+ prefixedLog := log .WithPrefix (fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()))
752
+
700
753
switch newEvent := event .(type ) {
701
754
// If we get a supply update event while we're signing the commit tx,
702
755
// we'll just insert it as a dangling update and do a self-transition.
@@ -710,6 +763,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
710
763
"pending update: %w" , err )
711
764
}
712
765
766
+ prefixedLog .Infof ("Received new supply update " +
767
+ "event: %T" , newEvent )
768
+
713
769
return & StateTransition {
714
770
NextState : s ,
715
771
}, nil
@@ -723,7 +779,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
723
779
stateTransition := s .SupplyTransition
724
780
725
781
// After some initial validation, we'll now sign the PSBT.
726
- log .Debug ("Signing supply commitment PSBT" )
782
+ prefixedLog .Debug ("Signing supply commitment PSBT" )
727
783
signedPsbt , err := env .Wallet .SignPsbt (
728
784
ctx , newEvent .CommitPkt .Pkt ,
729
785
)
@@ -732,6 +788,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
732
788
"commitment tx: %w" , err )
733
789
}
734
790
791
+ prefixedLog .Infof ("Signed supply " +
792
+ "commitment txn: %v" , limitSpewer .Sdump (signedPsbt ))
793
+
735
794
err = psbt .MaybeFinalizeAll (signedPsbt )
736
795
if err != nil {
737
796
return nil , fmt .Errorf ("unable to finalize psbt: %w" ,
@@ -787,11 +846,18 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
787
846
func (c * CommitBroadcastState ) ProcessEvent (event Event ,
788
847
env * Environment ) (* StateTransition , error ) {
789
848
849
+ // Create a prefixed logger for this supply commit.
850
+ prefixedLog := log .WithPrefix (fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()))
851
+
790
852
switch newEvent := event .(type ) {
791
853
// If we get a supply update event while we're broadcasting the commit
792
854
// tx, we'll just insert it as a dangling update and do a
793
855
// self-transition.
794
856
case SupplyUpdateEvent :
857
+ prefixedLog .Infof ("Received new supply update %T while " +
858
+ "finalizing prior commitment, inserting as dangling update" ,
859
+ newEvent )
860
+
795
861
ctx := context .Background ()
796
862
err := env .StateLog .InsertPendingUpdate (
797
863
ctx , env .AssetSpec , newEvent ,
@@ -813,6 +879,10 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
813
879
return nil , fmt .Errorf ("commitment transaction is nil" )
814
880
}
815
881
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
+
816
886
commitTx := c .SupplyTransition .NewCommitment .Txn
817
887
818
888
// Construct a detailed label for the broadcast request using
@@ -890,18 +960,19 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
890
960
"proof: %w" , err )
891
961
}
892
962
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.
893
966
stateTransition .ChainProof = lfn .Some (ChainProof {
894
967
Header : newEvent .Block .Header ,
895
968
BlockHeight : newEvent .BlockHeight ,
896
969
MerkleProof : * merkleProof ,
897
970
TxIndex : newEvent .TxIndex ,
898
971
})
899
972
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 ))
905
976
906
977
// The commitment has been confirmed, so we'll transition to the
907
978
// finalize state, but also log on disk that we no longer need
@@ -938,10 +1009,17 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
938
1009
func (c * CommitFinalizeState ) ProcessEvent (event Event ,
939
1010
env * Environment ) (* StateTransition , error ) {
940
1011
1012
+ // Create a prefixed logger for this supply commit.
1013
+ prefixedLog := log .WithPrefix (fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()))
1014
+
941
1015
switch newEvent := event .(type ) {
942
1016
// If we get a supply update event while we're finalizing the commit,
943
1017
// we'll just insert it as a dangling update and do a self-transition.
944
1018
case SupplyUpdateEvent :
1019
+ prefixedLog .Infof ("Received new supply update %T while " +
1020
+ "finalizing prior commitment, inserting as dangling update" ,
1021
+ newEvent )
1022
+
945
1023
ctx := context .Background ()
946
1024
err := env .StateLog .InsertPendingUpdate (
947
1025
ctx , env .AssetSpec , newEvent ,
@@ -961,6 +1039,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
961
1039
case * FinalizeEvent :
962
1040
ctx := context .Background ()
963
1041
1042
+ prefixedLog .Infof ("Finalizing supply commitment transition" )
1043
+
964
1044
// At this point, the commitment has been confirmed on disk, so
965
1045
// we can update: the state machine state on disk, and swap in
966
1046
// all the new supply tree information.
@@ -995,6 +1075,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
995
1075
}, nil
996
1076
}
997
1077
1078
+ prefixedLog .Infof ("Dangling updates found: %d" , len (danglingUpdates ))
1079
+
998
1080
// Otherwise, we have more work to do! We'll kick off a new
999
1081
// commitment cycle right away by transitioning to the tree
1000
1082
// creation state.
0 commit comments