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,11 +67,19 @@ 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 (
72
+ fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()),
73
+ )
74
+
69
75
// In this state, we'll receive one of three events: a new burn, a new
70
76
// mint, or a new ignore. For all three types, we'll emit this as an
71
77
// internal event as we transition to the UpdatePendingState.
72
78
switch supplyEvent := event .(type ) {
73
79
case SyncSupplyUpdateEvent :
80
+ prefixedLog .Infof ("Received new supply update event: %T" ,
81
+ supplyEvent )
82
+
74
83
// Before we transition to the next state, we'll add this event
75
84
// to our update log. This ensures that we'll remember to
76
85
// process from this state after a restart.
@@ -120,10 +129,18 @@ func (d *DefaultState) ProcessEvent(event Event,
120
129
func (u * UpdatesPendingState ) ProcessEvent (event Event , env * Environment ) (
121
130
* StateTransition , error ) {
122
131
132
+ // Create a prefixed logger for this supply commit.
133
+ prefixedLog := log .WithPrefix (
134
+ fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()),
135
+ )
136
+
123
137
switch newEvent := event .(type ) {
124
138
// We've received a new update event, we'll stage this in our local
125
139
// state, and do a self transition.
126
140
case SyncSupplyUpdateEvent :
141
+ prefixedLog .Infof ("Received new supply update event: %T" ,
142
+ newEvent )
143
+
127
144
// We just got a new pending update in addition to the one that
128
145
// made us transition to this state. This ensures that we'll
129
146
// remember to process from this state after a restart.
@@ -163,6 +180,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
163
180
"pending transition: %w" , err )
164
181
}
165
182
183
+ prefixedLog .Infof ("Received tick event, committing %d " +
184
+ "supply updates" , len (u .pendingUpdates ))
185
+
166
186
return & StateTransition {
167
187
NextState : & CommitTreeCreateState {},
168
188
NewEvents : lfn .Some (FsmEvent {
@@ -254,13 +274,23 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
254
274
func (c * CommitTreeCreateState ) ProcessEvent (event Event ,
255
275
env * Environment ) (* StateTransition , error ) {
256
276
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
+
257
284
// TODO(roasbeef): cache attemps to add new elements? or main state
258
285
// driver still single threaded?
259
286
260
287
switch newEvent := event .(type ) {
261
288
// If we get a supply update event while we're creating the tree,
262
289
// we'll just insert it as a dangling update and do a self-transition.
263
290
case SyncSupplyUpdateEvent :
291
+ prefixedLog .Infof ("Received new supply update event: %T" ,
292
+ newEvent )
293
+
264
294
ctx := context .Background ()
265
295
err := env .StateLog .InsertPendingUpdate (
266
296
ctx , env .AssetSpec , newEvent ,
@@ -291,6 +321,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
291
321
case * CreateTreeEvent :
292
322
pendingUpdates := newEvent .updatesToCommit
293
323
324
+ prefixedLog .Infof ("Creating new supply trees " +
325
+ "with %d pending updates" ,
326
+ len (pendingUpdates ))
327
+
294
328
// TODO(ffranr): Pass in context?
295
329
ctx := context .Background ()
296
330
@@ -395,8 +429,14 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
395
429
func newRootCommitment (ctx context.Context ,
396
430
oldCommitment lfn.Option [RootCommitment ],
397
431
unspentPreCommits []PreCommitment , newSupplyRoot * mssmt.BranchNode ,
398
- wallet Wallet , keyRing KeyRing ,
399
- chainParams chaincfg.Params ) (* RootCommitment , * psbt.Packet , error ) {
432
+ wallet Wallet , keyRing KeyRing , chainParams chaincfg.Params ,
433
+ logger lfn.Option [btclog.Logger ]) (* RootCommitment , * psbt.Packet ,
434
+ error ) {
435
+
436
+ logger .WhenSome (func (l btclog.Logger ) {
437
+ l .Infof ("Creating new root commitment, spending %v " +
438
+ "pre-commits" , len (unspentPreCommits ))
439
+ })
400
440
401
441
newCommitTx := wire .NewMsgTx (2 )
402
442
@@ -435,6 +475,11 @@ func newRootCommitment(ctx context.Context,
435
475
// on mint transactions where as the old commitment is the last
436
476
// commitment that was broadcast.
437
477
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
+
438
483
newCommitTx .AddTxIn (r .TxIn ())
439
484
440
485
bip32Derivation , trBip32Derivation :=
@@ -459,11 +504,12 @@ func newRootCommitment(ctx context.Context,
459
504
})
460
505
})
461
506
507
+ // TODO(roasbef): do CreateTaprootSignature instead?
462
508
// With the inputs available, derive the supply commitment output.
463
509
//
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.
467
513
iKeyOpt := lfn .MapOption (func (r RootCommitment ) keychain.KeyDescriptor {
468
514
return r .InternalKey
469
515
})(oldCommitment )
@@ -534,6 +580,11 @@ func newRootCommitment(ctx context.Context,
534
580
SupplyRoot : newSupplyRoot ,
535
581
}
536
582
583
+ logger .WhenSome (func (l btclog.Logger ) {
584
+ l .Infof ("Created new root commitment: %v" ,
585
+ limitSpewer .Sdump (newSupplyCommit ))
586
+ })
587
+
537
588
commitPkt , err := psbt .NewFromUnsignedTx (newCommitTx )
538
589
if err != nil {
539
590
return nil , nil , fmt .Errorf ("unable to create PSBT: %w" , err )
@@ -608,10 +659,18 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
608
659
func (c * CommitTxCreateState ) ProcessEvent (event Event ,
609
660
env * Environment ) (* StateTransition , error ) {
610
661
662
+ // Create a prefixed logger for this supply commit.
663
+ prefixedLog := log .WithPrefix (fmt .Sprintf (
664
+ "SupplyCommit(%v): " , env .AssetSpec .String ()),
665
+ )
666
+
611
667
switch newEvent := event .(type ) {
612
668
// If we get a supply update event while we're creating the commit tx,
613
669
// we'll just insert it as a dangling update and do a self-transition.
614
670
case SyncSupplyUpdateEvent :
671
+ prefixedLog .Infof ("Received new supply update event: %T" ,
672
+ newEvent )
673
+
615
674
ctx := context .Background ()
616
675
err := env .StateLog .InsertPendingUpdate (
617
676
ctx , env .AssetSpec , newEvent ,
@@ -635,6 +694,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
635
694
case * CreateTxEvent :
636
695
ctx := context .Background ()
637
696
697
+ prefixedLog .Infof ("Creating new supply commitment tx" )
698
+
638
699
// To create the new commitment, we'll fetch the unspent pre
639
700
// commitments, the current supply root (which may not exist),
640
701
// and the new supply root.
@@ -659,6 +720,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
659
720
newSupplyCommit , commitPkt , err := newRootCommitment (
660
721
ctx , oldCommitment , preCommits , newSupplyRoot ,
661
722
env .Wallet , env .KeyRing , env .ChainParams ,
723
+ lfn .Some (prefixedLog ),
662
724
)
663
725
if err != nil {
664
726
return nil , fmt .Errorf ("unable to create " +
@@ -711,6 +773,11 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
711
773
func (s * CommitTxSignState ) ProcessEvent (event Event ,
712
774
env * Environment ) (* StateTransition , error ) {
713
775
776
+ // Create a prefixed logger for this supply commit.
777
+ prefixedLog := log .WithPrefix (
778
+ fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()),
779
+ )
780
+
714
781
switch newEvent := event .(type ) {
715
782
// If we get a supply update event while we're signing the commit tx,
716
783
// we'll just insert it as a dangling update and do a self-transition.
@@ -728,6 +795,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
728
795
729
796
newEvent .SignalDone (nil )
730
797
798
+ prefixedLog .Infof ("Received new supply update " +
799
+ "event: %T" , newEvent )
800
+
731
801
return & StateTransition {
732
802
NextState : s ,
733
803
}, nil
@@ -741,7 +811,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
741
811
stateTransition := s .SupplyTransition
742
812
743
813
// After some initial validation, we'll now sign the PSBT.
744
- log .Debug ("Signing supply commitment PSBT" )
814
+ prefixedLog .Debug ("Signing supply commitment PSBT" )
745
815
signedPsbt , err := env .Wallet .SignPsbt (
746
816
ctx , newEvent .CommitPkt .Pkt ,
747
817
)
@@ -750,10 +820,13 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
750
820
"commitment tx: %w" , err )
751
821
}
752
822
823
+ prefixedLog .Infof ("Signed supply " +
824
+ "commitment txn: %v" , limitSpewer .Sdump (signedPsbt ))
825
+
753
826
err = psbt .MaybeFinalizeAll (signedPsbt )
754
827
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 )
757
830
}
758
831
759
832
commitTx , err := psbt .Extract (signedPsbt )
@@ -805,11 +878,20 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
805
878
func (c * CommitBroadcastState ) ProcessEvent (event Event ,
806
879
env * Environment ) (* StateTransition , error ) {
807
880
881
+ // Create a prefixed logger for this supply commit.
882
+ prefixedLog := log .WithPrefix (
883
+ fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()),
884
+ )
885
+
808
886
switch newEvent := event .(type ) {
809
887
// If we get a supply update event while we're broadcasting the commit
810
888
// tx, we'll just insert it as a dangling update and do a
811
889
// self-transition.
812
890
case SyncSupplyUpdateEvent :
891
+ prefixedLog .Infof ("Received new supply update %T while " +
892
+ "finalizing prior commitment, inserting as dangling " +
893
+ "update" , newEvent )
894
+
813
895
ctx := context .Background ()
814
896
err := env .StateLog .InsertPendingUpdate (
815
897
ctx , env .AssetSpec , newEvent ,
@@ -835,6 +917,11 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
835
917
return nil , fmt .Errorf ("commitment transaction is nil" )
836
918
}
837
919
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
+
838
925
commitTx := c .SupplyTransition .NewCommitment .Txn
839
926
840
927
// Construct a detailed label for the broadcast request using
@@ -912,18 +999,20 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
912
999
"proof: %w" , err )
913
1000
}
914
1001
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.
915
1005
stateTransition .ChainProof = lfn .Some (ChainProof {
916
1006
Header : newEvent .Block .Header ,
917
1007
BlockHeight : newEvent .BlockHeight ,
918
1008
MerkleProof : * merkleProof ,
919
1009
TxIndex : newEvent .TxIndex ,
920
1010
})
921
1011
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 ))
927
1016
928
1017
// The commitment has been confirmed, so we'll transition to the
929
1018
// finalize state, but also log on disk that we no longer need
@@ -960,10 +1049,19 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
960
1049
func (c * CommitFinalizeState ) ProcessEvent (event Event ,
961
1050
env * Environment ) (* StateTransition , error ) {
962
1051
1052
+ // Create a prefixed logger for this supply commit.
1053
+ prefixedLog := log .WithPrefix (
1054
+ fmt .Sprintf ("SupplyCommit(%v): " , env .AssetSpec .String ()),
1055
+ )
1056
+
963
1057
switch newEvent := event .(type ) {
964
1058
// If we get a supply update event while we're finalizing the commit,
965
1059
// we'll just insert it as a dangling update and do a self-transition.
966
1060
case SyncSupplyUpdateEvent :
1061
+ prefixedLog .Infof ("Received new supply update %T while " +
1062
+ "finalizing prior commitment, inserting as dangling " +
1063
+ "update" , newEvent )
1064
+
967
1065
ctx := context .Background ()
968
1066
err := env .StateLog .InsertPendingUpdate (
969
1067
ctx , env .AssetSpec , newEvent ,
@@ -987,6 +1085,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
987
1085
case * FinalizeEvent :
988
1086
ctx := context .Background ()
989
1087
1088
+ prefixedLog .Infof ("Finalizing supply commitment transition" )
1089
+
990
1090
// At this point, the commitment has been confirmed on disk, so
991
1091
// we can update: the state machine state on disk, and swap in
992
1092
// all the new supply tree information.
@@ -1021,6 +1121,9 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
1021
1121
}, nil
1022
1122
}
1023
1123
1124
+ prefixedLog .Infof ("Dangling updates found: %d" ,
1125
+ len (danglingUpdates ))
1126
+
1024
1127
// Otherwise, we have more work to do! We'll kick off a new
1025
1128
// commitment cycle right away by transitioning to the tree
1026
1129
// creation state.
0 commit comments