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