Skip to content

Commit 77848c4

Browse files
authored
Merge pull request #9342 from ellemouton/slogProtofsm
protofsm: update GR Manager usage and start using structured logging
2 parents dd25e6e + 42ce9d6 commit 77848c4

File tree

8 files changed

+142
-126
lines changed

8 files changed

+142
-126
lines changed

build/version.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,5 @@ func WithBuildInfo(ctx context.Context, cfg *LogConfig) (context.Context,
122122
return nil, fmt.Errorf("unable to decode commit hash: %w", err)
123123
}
124124

125-
// Include the first 3 bytes of the commit hash in the context as an
126-
// slog attribute.
127-
if len(commitHash) > 3 {
128-
commitHash = commitHash[:3]
129-
}
130-
131-
return btclog.WithCtx(ctx, btclog.Hex("rev", commitHash)), nil
125+
return btclog.WithCtx(ctx, btclog.Hex3("rev", commitHash)), nil
132126
}

docs/release-notes/release-notes-0.19.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ The underlying functionality between those two options remain the same.
251251
[GoroutineManager](https://github.com/lightningnetwork/lnd/pull/9141) so that
252252
its constructor does not take a context.
253253

254+
* [Update protofsm
255+
StateMachine](https://github.com/lightningnetwork/lnd/pull/9342) to use the
256+
new GoroutineManager API along with structured logging.
257+
254258
## Tooling and Documentation
255259

256260
* [Improved `lncli create` command help text](https://github.com/lightningnetwork/lnd/pull/9077)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/btcsuite/btcd/btcutil/psbt v1.1.8
1111
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
1212
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c
13-
github.com/btcsuite/btclog/v2 v2.0.0
13+
github.com/btcsuite/btclog/v2 v2.0.1-0.20250110154127-3ae4bf1cb318
1414
github.com/btcsuite/btcwallet v0.16.10-0.20241127094224-93c858b2ad63
1515
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.5
1616
github.com/btcsuite/btcwallet/wallet/txrules v1.2.2
@@ -36,7 +36,7 @@ require (
3636
github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb
3737
github.com/lightningnetwork/lnd/cert v1.2.2
3838
github.com/lightningnetwork/lnd/clock v1.1.1
39-
github.com/lightningnetwork/lnd/fn/v2 v2.0.2
39+
github.com/lightningnetwork/lnd/fn/v2 v2.0.4
4040
github.com/lightningnetwork/lnd/healthcheck v1.2.6
4141
github.com/lightningnetwork/lnd/kvdb v1.4.12
4242
github.com/lightningnetwork/lnd/queue v1.1.1

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtyd
9292
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
9393
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c h1:4HxD1lBUGUddhzgaNgrCPsFWd7cGYNpeFUgd9ZIgyM0=
9494
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c/go.mod h1:w7xnGOhwT3lmrS4H3b/D1XAXxvh+tbhUm8xeHN2y3TQ=
95-
github.com/btcsuite/btclog/v2 v2.0.0 h1:ZfOBItEeLWfU0voi88K72j8vtxP4/dHhxRFf2bxZkVo=
96-
github.com/btcsuite/btclog/v2 v2.0.0/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE=
95+
github.com/btcsuite/btclog/v2 v2.0.1-0.20250110154127-3ae4bf1cb318 h1:oCjIcinPt7XQ644MP/22JcjYEC84qRc3bRBH0d7Hhd4=
96+
github.com/btcsuite/btclog/v2 v2.0.1-0.20250110154127-3ae4bf1cb318/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE=
9797
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
9898
github.com/btcsuite/btcwallet v0.16.10-0.20241127094224-93c858b2ad63 h1:YN+PekOLlLoGxE3P5RJaGgodZD5DDJSU8eXQZVwwCxM=
9999
github.com/btcsuite/btcwallet v0.16.10-0.20241127094224-93c858b2ad63/go.mod h1:1HJXYbjJzgumlnxOC2+ViR1U+gnHWoOn7WeK5OfY1eU=
@@ -456,8 +456,8 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf
456456
github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U=
457457
github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
458458
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ=
459-
github.com/lightningnetwork/lnd/fn/v2 v2.0.2 h1:M7o2lYrh/zCp+lntPB3WP/rWTu5U+4ssyHW+kqNJ0fs=
460-
github.com/lightningnetwork/lnd/fn/v2 v2.0.2/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
459+
github.com/lightningnetwork/lnd/fn/v2 v2.0.4 h1:DiC/AEa7DhnY4qOEQBISu1cp+1+51LjbVDzNLVBwNjI=
460+
github.com/lightningnetwork/lnd/fn/v2 v2.0.4/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
461461
github.com/lightningnetwork/lnd/healthcheck v1.2.6 h1:1sWhqr93GdkWy4+6U7JxBfcyZIE78MhIHTJZfPx7qqI=
462462
github.com/lightningnetwork/lnd/healthcheck v1.2.6/go.mod h1:Mu02um4CWY/zdTOvFje7WJgJcHyX2zq/FG3MhOAiGaQ=
463463
github.com/lightningnetwork/lnd/kvdb v1.4.12 h1:Y0WY5Tbjyjn6eCYh068qkWur5oFtioJlfxc8w5SlJeQ=

lnwallet/chancloser/rbf_coop_test.go

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chancloser
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/hex"
67
"errors"
78
"fmt"
@@ -144,7 +145,9 @@ func assertUnknownEventFail(t *testing.T, startingState ProtocolState) {
144145

145146
closeHarness.expectFailure(ErrInvalidStateTransition)
146147

147-
closeHarness.chanCloser.SendEvent(&unknownEvent{})
148+
closeHarness.chanCloser.SendEvent(
149+
context.Background(), &unknownEvent{},
150+
)
148151

149152
// There should be no further state transitions.
150153
closeHarness.assertNoStateTransitions()
@@ -481,6 +484,7 @@ func (r *rbfCloserTestHarness) expectHalfSignerIteration(
481484
initEvent ProtocolEvent, balanceAfterClose, absoluteFee btcutil.Amount,
482485
dustExpect dustExpectation) {
483486

487+
ctx := context.Background()
484488
numFeeCalls := 2
485489

486490
// If we're using the SendOfferEvent as a trigger, we only need to call
@@ -527,7 +531,7 @@ func (r *rbfCloserTestHarness) expectHalfSignerIteration(
527531
})
528532
r.expectMsgSent(msgExpect)
529533

530-
r.chanCloser.SendEvent(initEvent)
534+
r.chanCloser.SendEvent(ctx, initEvent)
531535

532536
// Based on the init event, we'll either just go to the closing
533537
// negotiation state, or go through the channel flushing state first.
@@ -582,6 +586,8 @@ func (r *rbfCloserTestHarness) assertSingleRbfIteration(
582586
initEvent ProtocolEvent, balanceAfterClose, absoluteFee btcutil.Amount,
583587
dustExpect dustExpectation) {
584588

589+
ctx := context.Background()
590+
585591
// We'll now send in the send offer event, which should trigger 1/2 of
586592
// the RBF loop, ending us in the LocalOfferSent state.
587593
r.expectHalfSignerIteration(
@@ -607,7 +613,7 @@ func (r *rbfCloserTestHarness) assertSingleRbfIteration(
607613
balanceAfterClose, true,
608614
)
609615

610-
r.chanCloser.SendEvent(localSigEvent)
616+
r.chanCloser.SendEvent(ctx, localSigEvent)
611617

612618
// We should transition to the pending closing state now.
613619
r.assertLocalClosePending()
@@ -617,6 +623,8 @@ func (r *rbfCloserTestHarness) assertSingleRemoteRbfIteration(
617623
initEvent ProtocolEvent, balanceAfterClose, absoluteFee btcutil.Amount,
618624
sequence uint32, iteration bool) {
619625

626+
ctx := context.Background()
627+
620628
// If this is an iteration, then we expect some intermediate states,
621629
// before we enter the main RBF/sign loop.
622630
if iteration {
@@ -635,7 +643,7 @@ func (r *rbfCloserTestHarness) assertSingleRemoteRbfIteration(
635643
absoluteFee, balanceAfterClose, false,
636644
)
637645

638-
r.chanCloser.SendEvent(initEvent)
646+
r.chanCloser.SendEvent(ctx, initEvent)
639647

640648
// Our outer state should transition to ClosingNegotiation state.
641649
r.assertStateTransitions(&ClosingNegotiation{})
@@ -668,6 +676,8 @@ func assertStateT[T ProtocolState](h *rbfCloserTestHarness) T {
668676
func newRbfCloserTestHarness(t *testing.T,
669677
cfg *harnessCfg) *rbfCloserTestHarness {
670678

679+
ctx := context.Background()
680+
671681
startingHeight := 200
672682

673683
chanPoint := randOutPoint(t)
@@ -747,7 +757,7 @@ func newRbfCloserTestHarness(t *testing.T,
747757
).Return(nil)
748758

749759
chanCloser := protofsm.NewStateMachine(protoCfg)
750-
chanCloser.Start()
760+
chanCloser.Start(ctx)
751761

752762
harness.stateSub = chanCloser.RegisterStateEvents()
753763

@@ -769,6 +779,7 @@ func newCloser(t *testing.T, cfg *harnessCfg) *rbfCloserTestHarness {
769779
// TestRbfChannelActiveTransitions tests the transitions of from the
770780
// ChannelActive state.
771781
func TestRbfChannelActiveTransitions(t *testing.T) {
782+
ctx := context.Background()
772783
localAddr := lnwire.DeliveryAddress(bytes.Repeat([]byte{0x01}, 20))
773784
remoteAddr := lnwire.DeliveryAddress(bytes.Repeat([]byte{0x02}, 20))
774785

@@ -782,7 +793,7 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
782793
})
783794
defer closeHarness.stopAndAssert()
784795

785-
closeHarness.chanCloser.SendEvent(&SpendEvent{})
796+
closeHarness.chanCloser.SendEvent(ctx, &SpendEvent{})
786797

787798
closeHarness.assertStateTransitions(&CloseFin{})
788799
})
@@ -799,7 +810,7 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
799810
// We don't specify an upfront shutdown addr, and don't specify
800811
// on here in the vent, so we should call new addr, but then
801812
// fail.
802-
closeHarness.chanCloser.SendEvent(&SendShutdown{})
813+
closeHarness.chanCloser.SendEvent(ctx, &SendShutdown{})
803814

804815
// We shouldn't have transitioned to a new state.
805816
closeHarness.assertNoStateTransitions()
@@ -824,9 +835,9 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
824835

825836
// If we send the shutdown event, we should transition to the
826837
// shutdown pending state.
827-
closeHarness.chanCloser.SendEvent(&SendShutdown{
828-
IdealFeeRate: feeRate,
829-
})
838+
closeHarness.chanCloser.SendEvent(
839+
ctx, &SendShutdown{IdealFeeRate: feeRate},
840+
)
830841
closeHarness.assertStateTransitions(&ShutdownPending{})
831842

832843
// If we examine the internal state, it should be consistent
@@ -869,9 +880,9 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
869880

870881
// Next, we'll emit the recv event, with the addr of the remote
871882
// party.
872-
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
873-
ShutdownScript: remoteAddr,
874-
})
883+
closeHarness.chanCloser.SendEvent(
884+
ctx, &ShutdownReceived{ShutdownScript: remoteAddr},
885+
)
875886

876887
// We should transition to the shutdown pending state.
877888
closeHarness.assertStateTransitions(&ShutdownPending{})
@@ -899,6 +910,7 @@ func TestRbfChannelActiveTransitions(t *testing.T) {
899910
// shutdown ourselves.
900911
func TestRbfShutdownPendingTransitions(t *testing.T) {
901912
t.Parallel()
913+
ctx := context.Background()
902914

903915
startingState := &ShutdownPending{}
904916

@@ -913,7 +925,7 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
913925
})
914926
defer closeHarness.stopAndAssert()
915927

916-
closeHarness.chanCloser.SendEvent(&SpendEvent{})
928+
closeHarness.chanCloser.SendEvent(ctx, &SpendEvent{})
917929

918930
closeHarness.assertStateTransitions(&CloseFin{})
919931
})
@@ -936,7 +948,7 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
936948
// We'll now send in a ShutdownReceived event, but with a
937949
// different address provided in the shutdown message. This
938950
// should result in an error.
939-
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
951+
closeHarness.chanCloser.SendEvent(ctx, &ShutdownReceived{
940952
ShutdownScript: localAddr,
941953
})
942954

@@ -972,9 +984,9 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
972984

973985
// We'll send in a shutdown received event, with the expected
974986
// co-op close addr.
975-
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
976-
ShutdownScript: remoteAddr,
977-
})
987+
closeHarness.chanCloser.SendEvent(
988+
ctx, &ShutdownReceived{ShutdownScript: remoteAddr},
989+
)
978990

979991
// We should transition to the channel flushing state.
980992
closeHarness.assertStateTransitions(&ChannelFlushing{})
@@ -1015,7 +1027,7 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
10151027
closeHarness.expectFinalBalances(fn.None[ShutdownBalances]())
10161028

10171029
// We'll send in a shutdown received event.
1018-
closeHarness.chanCloser.SendEvent(&ShutdownComplete{})
1030+
closeHarness.chanCloser.SendEvent(ctx, &ShutdownComplete{})
10191031

10201032
// We should transition to the channel flushing state.
10211033
closeHarness.assertStateTransitions(&ChannelFlushing{})
@@ -1030,6 +1042,7 @@ func TestRbfShutdownPendingTransitions(t *testing.T) {
10301042
// transition to the negotiation state.
10311043
func TestRbfChannelFlushingTransitions(t *testing.T) {
10321044
t.Parallel()
1045+
ctx := context.Background()
10331046

10341047
localBalance := lnwire.NewMSatFromSatoshis(10_000)
10351048
remoteBalance := lnwire.NewMSatFromSatoshis(50_000)
@@ -1082,7 +1095,9 @@ func TestRbfChannelFlushingTransitions(t *testing.T) {
10821095

10831096
// We'll now send in the event which should trigger
10841097
// this code path.
1085-
closeHarness.chanCloser.SendEvent(&chanFlushedEvent)
1098+
closeHarness.chanCloser.SendEvent(
1099+
ctx, &chanFlushedEvent,
1100+
)
10861101

10871102
// With the event sent, we should now transition
10881103
// straight to the ClosingNegotiation state, with no
@@ -1149,6 +1164,7 @@ func TestRbfChannelFlushingTransitions(t *testing.T) {
11491164
// rate.
11501165
func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
11511166
t.Parallel()
1167+
ctx := context.Background()
11521168

11531169
localBalance := lnwire.NewMSatFromSatoshis(40_000)
11541170
remoteBalance := lnwire.NewMSatFromSatoshis(50_000)
@@ -1232,7 +1248,7 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
12321248

12331249
// We should fail as the remote party sent us more than one
12341250
// signature.
1235-
closeHarness.chanCloser.SendEvent(localSigEvent)
1251+
closeHarness.chanCloser.SendEvent(ctx, localSigEvent)
12361252
})
12371253

12381254
// Next, we'll verify that if the balance of the remote party is dust,
@@ -1333,7 +1349,7 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
13331349
singleMsgMatcher[*lnwire.Shutdown](nil),
13341350
)
13351351

1336-
closeHarness.chanCloser.SendEvent(sendShutdown)
1352+
closeHarness.chanCloser.SendEvent(ctx, sendShutdown)
13371353

13381354
// We should first transition to the Channel Active state
13391355
// momentarily, before transitioning to the shutdown pending
@@ -1367,6 +1383,7 @@ func TestRbfCloseClosingNegotiationLocal(t *testing.T) {
13671383
// party.
13681384
func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
13691385
t.Parallel()
1386+
ctx := context.Background()
13701387

13711388
localBalance := lnwire.NewMSatFromSatoshis(40_000)
13721389
remoteBalance := lnwire.NewMSatFromSatoshis(50_000)
@@ -1416,7 +1433,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
14161433
FeeSatoshis: absoluteFee * 10,
14171434
},
14181435
}
1419-
closeHarness.chanCloser.SendEvent(feeOffer)
1436+
closeHarness.chanCloser.SendEvent(ctx, feeOffer)
14201437

14211438
// We shouldn't have transitioned to a new state.
14221439
closeHarness.assertNoStateTransitions()
@@ -1460,7 +1477,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
14601477
},
14611478
},
14621479
}
1463-
closeHarness.chanCloser.SendEvent(feeOffer)
1480+
closeHarness.chanCloser.SendEvent(ctx, feeOffer)
14641481

14651482
// We shouldn't have transitioned to a new state.
14661483
closeHarness.assertNoStateTransitions()
@@ -1489,7 +1506,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
14891506
},
14901507
},
14911508
}
1492-
closeHarness.chanCloser.SendEvent(feeOffer)
1509+
closeHarness.chanCloser.SendEvent(ctx, feeOffer)
14931510

14941511
// We shouldn't have transitioned to a new state.
14951512
closeHarness.assertNoStateTransitions()
@@ -1561,9 +1578,9 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
15611578
// We'll now simulate the start of the RBF loop, by receiving a
15621579
// new Shutdown message from the remote party. This signals
15631580
// that they want to obtain a new commit sig.
1564-
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
1565-
ShutdownScript: remoteAddr,
1566-
})
1581+
closeHarness.chanCloser.SendEvent(
1582+
ctx, &ShutdownReceived{ShutdownScript: remoteAddr},
1583+
)
15671584

15681585
// Next, we'll receive an offer from the remote party, and
15691586
// drive another RBF iteration. This time, we'll increase the

protofsm/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package protofsm
22

33
import (
4-
"github.com/btcsuite/btclog"
4+
"github.com/btcsuite/btclog/v2"
55
"github.com/lightningnetwork/lnd/build"
66
)
77

0 commit comments

Comments
 (0)