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,16 @@ func createCommitmentTxLabel(assetSpec asset.Specifier,
66
67
func (d * DefaultState ) ProcessEvent (event Event ,
67
68
env * Environment ) (* StateTransition , error ) {
68
69
70
+ prefixedLog := env .Logger ()
71
+
69
72
// In this state, we'll receive one of three events: a new burn, a new
70
73
// mint, or a new ignore. For all three types, we'll emit this as an
71
74
// internal event as we transition to the UpdatePendingState.
72
75
switch supplyEvent := event .(type ) {
73
76
case SyncSupplyUpdateEvent :
77
+ prefixedLog .Infof ("Received new supply update event: %T" ,
78
+ supplyEvent )
79
+
74
80
// Before we transition to the next state, we'll add this event
75
81
// to our update log. This ensures that we'll remember to
76
82
// process from this state after a restart.
@@ -120,10 +126,15 @@ func (d *DefaultState) ProcessEvent(event Event,
120
126
func (u * UpdatesPendingState ) ProcessEvent (event Event , env * Environment ) (
121
127
* StateTransition , error ) {
122
128
129
+ prefixedLog := env .Logger ()
130
+
123
131
switch newEvent := event .(type ) {
124
132
// We've received a new update event, we'll stage this in our local
125
133
// state, and do a self transition.
126
134
case SyncSupplyUpdateEvent :
135
+ prefixedLog .Infof ("Received new supply update event: %T" ,
136
+ newEvent )
137
+
127
138
// We just got a new pending update in addition to the one that
128
139
// made us transition to this state. This ensures that we'll
129
140
// remember to process from this state after a restart.
@@ -163,6 +174,9 @@ func (u *UpdatesPendingState) ProcessEvent(event Event, env *Environment) (
163
174
"pending transition: %w" , err )
164
175
}
165
176
177
+ prefixedLog .Infof ("Received tick event, committing %d " +
178
+ "supply updates" , len (u .pendingUpdates ))
179
+
166
180
return & StateTransition {
167
181
NextState : & CommitTreeCreateState {},
168
182
NewEvents : lfn .Some (FsmEvent {
@@ -254,13 +268,18 @@ func applyTreeUpdates(supplyTrees SupplyTrees,
254
268
func (c * CommitTreeCreateState ) ProcessEvent (event Event ,
255
269
env * Environment ) (* StateTransition , error ) {
256
270
271
+ prefixedLog := env .Logger ()
272
+
257
273
// TODO(roasbeef): cache attemps to add new elements? or main state
258
274
// driver still single threaded?
259
275
260
276
switch newEvent := event .(type ) {
261
277
// If we get a supply update event while we're creating the tree,
262
278
// we'll just insert it as a dangling update and do a self-transition.
263
279
case SyncSupplyUpdateEvent :
280
+ prefixedLog .Infof ("Received new supply update event: %T" ,
281
+ newEvent )
282
+
264
283
ctx := context .Background ()
265
284
err := env .StateLog .InsertPendingUpdate (
266
285
ctx , env .AssetSpec , newEvent ,
@@ -291,6 +310,10 @@ func (c *CommitTreeCreateState) ProcessEvent(event Event,
291
310
case * CreateTreeEvent :
292
311
pendingUpdates := newEvent .updatesToCommit
293
312
313
+ prefixedLog .Infof ("Creating new supply trees " +
314
+ "with %d pending updates" ,
315
+ len (pendingUpdates ))
316
+
294
317
// TODO(ffranr): Pass in context?
295
318
ctx := context .Background ()
296
319
@@ -396,7 +419,13 @@ func newRootCommitment(ctx context.Context,
396
419
oldCommitment lfn.Option [RootCommitment ],
397
420
unspentPreCommits []PreCommitment , newSupplyRoot * mssmt.BranchNode ,
398
421
wallet Wallet , keyRing KeyRing ,
399
- chainParams chaincfg.Params ) (* RootCommitment , * psbt.Packet , error ) {
422
+ chainParams chaincfg.Params ,
423
+ logger lfn.Option [btclog.Logger ]) (* RootCommitment , * psbt.Packet , error ) {
424
+
425
+ logger .WhenSome (func (l btclog.Logger ) {
426
+ l .Infof ("Creating new root commitment, spending %v " +
427
+ "pre-commits" , len (unspentPreCommits ))
428
+ })
400
429
401
430
newCommitTx := wire .NewMsgTx (2 )
402
431
@@ -435,6 +464,11 @@ func newRootCommitment(ctx context.Context,
435
464
// on mint transactions where as the old commitment is the last
436
465
// commitment that was broadcast.
437
466
oldCommitment .WhenSome (func (r RootCommitment ) {
467
+ logger .WhenSome (func (l btclog.Logger ) {
468
+ l .Infof ("Re-using prior commitment as outpoint=%v: %v" ,
469
+ r .CommitPoint (), limitSpewer .Sdump (r ))
470
+ })
471
+
438
472
newCommitTx .AddTxIn (r .TxIn ())
439
473
440
474
bip32Derivation , trBip32Derivation :=
@@ -459,11 +493,12 @@ func newRootCommitment(ctx context.Context,
459
493
})
460
494
})
461
495
496
+ // TODO(roasbef): do CreateTaprootSignature instead?
462
497
// With the inputs available, derive the supply commitment output.
463
498
//
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.
499
+ // Determine the internal key to use for this output. If a prior root
500
+ // commitment exists, reuse its internal key; otherwise, generate a new
501
+ // one.
467
502
iKeyOpt := lfn .MapOption (func (r RootCommitment ) keychain.KeyDescriptor {
468
503
return r .InternalKey
469
504
})(oldCommitment )
@@ -534,6 +569,11 @@ func newRootCommitment(ctx context.Context,
534
569
SupplyRoot : newSupplyRoot ,
535
570
}
536
571
572
+ logger .WhenSome (func (l btclog.Logger ) {
573
+ l .Infof ("Created new root commitment: %v" ,
574
+ limitSpewer .Sdump (newSupplyCommit ))
575
+ })
576
+
537
577
commitPkt , err := psbt .NewFromUnsignedTx (newCommitTx )
538
578
if err != nil {
539
579
return nil , nil , fmt .Errorf ("unable to create PSBT: %w" , err )
@@ -608,10 +648,15 @@ func fundSupplyCommitTx(ctx context.Context, supplyCommit *RootCommitment,
608
648
func (c * CommitTxCreateState ) ProcessEvent (event Event ,
609
649
env * Environment ) (* StateTransition , error ) {
610
650
651
+ prefixedLog := env .Logger ()
652
+
611
653
switch newEvent := event .(type ) {
612
654
// If we get a supply update event while we're creating the commit tx,
613
655
// we'll just insert it as a dangling update and do a self-transition.
614
656
case SyncSupplyUpdateEvent :
657
+ prefixedLog .Infof ("Received new supply update event: %T" ,
658
+ newEvent )
659
+
615
660
ctx := context .Background ()
616
661
err := env .StateLog .InsertPendingUpdate (
617
662
ctx , env .AssetSpec , newEvent ,
@@ -635,6 +680,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
635
680
case * CreateTxEvent :
636
681
ctx := context .Background ()
637
682
683
+ prefixedLog .Infof ("Creating new supply commitment tx" )
684
+
638
685
// To create the new commitment, we'll fetch the unspent pre
639
686
// commitments, the current supply root (which may not exist),
640
687
// and the new supply root.
@@ -659,6 +706,7 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
659
706
newSupplyCommit , commitPkt , err := newRootCommitment (
660
707
ctx , oldCommitment , preCommits , newSupplyRoot ,
661
708
env .Wallet , env .KeyRing , env .ChainParams ,
709
+ lfn .Some (prefixedLog ),
662
710
)
663
711
if err != nil {
664
712
return nil , fmt .Errorf ("unable to create " +
@@ -711,6 +759,8 @@ func (c *CommitTxCreateState) ProcessEvent(event Event,
711
759
func (s * CommitTxSignState ) ProcessEvent (event Event ,
712
760
env * Environment ) (* StateTransition , error ) {
713
761
762
+ prefixedLog := env .Logger ()
763
+
714
764
switch newEvent := event .(type ) {
715
765
// If we get a supply update event while we're signing the commit tx,
716
766
// we'll just insert it as a dangling update and do a self-transition.
@@ -728,6 +778,9 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
728
778
729
779
newEvent .SignalDone (nil )
730
780
781
+ prefixedLog .Infof ("Received new supply update " +
782
+ "event: %T" , newEvent )
783
+
731
784
return & StateTransition {
732
785
NextState : s ,
733
786
}, nil
@@ -741,7 +794,7 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
741
794
stateTransition := s .SupplyTransition
742
795
743
796
// After some initial validation, we'll now sign the PSBT.
744
- log .Debug ("Signing supply commitment PSBT" )
797
+ prefixedLog .Debug ("Signing supply commitment PSBT" )
745
798
signedPsbt , err := env .Wallet .SignPsbt (
746
799
ctx , newEvent .CommitPkt .Pkt ,
747
800
)
@@ -750,10 +803,13 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
750
803
"commitment tx: %w" , err )
751
804
}
752
805
806
+ prefixedLog .Infof ("Signed supply " +
807
+ "commitment txn: %v" , limitSpewer .Sdump (signedPsbt ))
808
+
753
809
err = psbt .MaybeFinalizeAll (signedPsbt )
754
810
if err != nil {
755
- return nil , fmt .Errorf ("unable to finalize psbt: %w" ,
756
- err )
811
+ return nil , fmt .Errorf ("unable to finalize " +
812
+ "psbt: %w" , err )
757
813
}
758
814
759
815
commitTx , err := psbt .Extract (signedPsbt )
@@ -805,11 +861,17 @@ func (s *CommitTxSignState) ProcessEvent(event Event,
805
861
func (c * CommitBroadcastState ) ProcessEvent (event Event ,
806
862
env * Environment ) (* StateTransition , error ) {
807
863
864
+ prefixedLog := env .Logger ()
865
+
808
866
switch newEvent := event .(type ) {
809
867
// If we get a supply update event while we're broadcasting the commit
810
868
// tx, we'll just insert it as a dangling update and do a
811
869
// self-transition.
812
870
case SyncSupplyUpdateEvent :
871
+ prefixedLog .Infof ("Received new supply update %T while " +
872
+ "finalizing prior commitment, inserting as dangling update" ,
873
+ newEvent )
874
+
813
875
ctx := context .Background ()
814
876
err := env .StateLog .InsertPendingUpdate (
815
877
ctx , env .AssetSpec , newEvent ,
@@ -835,6 +897,11 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
835
897
return nil , fmt .Errorf ("commitment transaction is nil" )
836
898
}
837
899
900
+ commitTxid := c .SupplyTransition .NewCommitment .Txn .TxHash ()
901
+ prefixedLog .Infof ("Broadcasting supply commitment " +
902
+ "txn (txid=%v): %v" , commitTxid ,
903
+ limitSpewer .Sdump (c .SupplyTransition .NewCommitment .Txn ))
904
+
838
905
commitTx := c .SupplyTransition .NewCommitment .Txn
839
906
840
907
// Construct a detailed label for the broadcast request using
@@ -912,18 +979,20 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
912
979
"proof: %w" , err )
913
980
}
914
981
982
+ // Now that the transaction has been confirmed, we'll construct
983
+ // a merkle proof for the commitment transaction. This'll be
984
+ // used to prove that the supply commit is canonical.
915
985
stateTransition .ChainProof = lfn .Some (ChainProof {
916
986
Header : newEvent .Block .Header ,
917
987
BlockHeight : newEvent .BlockHeight ,
918
988
MerkleProof : * merkleProof ,
919
989
TxIndex : newEvent .TxIndex ,
920
990
})
921
991
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
992
+ prefixedLog .Infof ("Supply commitment txn confirmed " +
993
+ "in block %d (hash=%v): %v" ,
994
+ newEvent .BlockHeight , newEvent .Block .Header .BlockHash (),
995
+ limitSpewer .Sdump (c .SupplyTransition .NewCommitment .Txn ))
927
996
928
997
// The commitment has been confirmed, so we'll transition to the
929
998
// finalize state, but also log on disk that we no longer need
@@ -960,10 +1029,16 @@ func (c *CommitBroadcastState) ProcessEvent(event Event,
960
1029
func (c * CommitFinalizeState ) ProcessEvent (event Event ,
961
1030
env * Environment ) (* StateTransition , error ) {
962
1031
1032
+ prefixedLog := env .Logger ()
1033
+
963
1034
switch newEvent := event .(type ) {
964
1035
// If we get a supply update event while we're finalizing the commit,
965
1036
// we'll just insert it as a dangling update and do a self-transition.
966
1037
case SyncSupplyUpdateEvent :
1038
+ prefixedLog .Infof ("Received new supply update %T while " +
1039
+ "finalizing prior commitment, inserting as dangling " +
1040
+ "update" , newEvent )
1041
+
967
1042
ctx := context .Background ()
968
1043
err := env .StateLog .InsertPendingUpdate (
969
1044
ctx , env .AssetSpec , newEvent ,
@@ -987,6 +1062,8 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
987
1062
case * FinalizeEvent :
988
1063
ctx := context .Background ()
989
1064
1065
+ prefixedLog .Infof ("Finalizing supply commitment transition" )
1066
+
990
1067
// At this point, the commitment has been confirmed on disk, so
991
1068
// we can update: the state machine state on disk, and swap in
992
1069
// all the new supply tree information.
@@ -1021,6 +1098,9 @@ func (c *CommitFinalizeState) ProcessEvent(event Event,
1021
1098
}, nil
1022
1099
}
1023
1100
1101
+ prefixedLog .Infof ("Dangling updates found: %d" ,
1102
+ len (danglingUpdates ))
1103
+
1024
1104
// Otherwise, we have more work to do! We'll kick off a new
1025
1105
// commitment cycle right away by transitioning to the tree
1026
1106
// creation state.
0 commit comments