Skip to content

Commit

Permalink
protofsm: use updated GoroutineManager API
Browse files Browse the repository at this point in the history
Update to use the latest version of the GoroutineManager which takes a
context via the `Go` method instead of the constructor.
  • Loading branch information
ellemouton committed Dec 12, 2024
1 parent 1a74ba1 commit 3940344
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 66 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,5 @@ go 1.22.6
retract v0.0.2

replace github.com/btcsuite/btclog/v2 => github.com/ellemouton/btclog/v2 v2.0.0-20241210110018-997ee6596623

replace github.com/lightningnetwork/lnd/fn/v2 => github.com/ellemouton/lnd/fn/v2 v2.0.0-20241212073257-37bb082f20f3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/ellemouton/btclog/v2 v2.0.0-20241210110018-997ee6596623 h1:qcTPGQ0m2YL+GkptbgDnICKcNYGfVy0Oy9WTnC9BosA=
github.com/ellemouton/btclog/v2 v2.0.0-20241210110018-997ee6596623/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE=
github.com/ellemouton/lnd/fn/v2 v2.0.0-20241212073257-37bb082f20f3 h1:jDl1yjv9yBqQq2lE0A7ht6+0IBkSBvsO68duQXBZtb8=
github.com/ellemouton/lnd/fn/v2 v2.0.0-20241212073257-37bb082f20f3/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -456,8 +458,6 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf
github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U=
github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ=
github.com/lightningnetwork/lnd/fn/v2 v2.0.2 h1:M7o2lYrh/zCp+lntPB3WP/rWTu5U+4ssyHW+kqNJ0fs=
github.com/lightningnetwork/lnd/fn/v2 v2.0.2/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
github.com/lightningnetwork/lnd/healthcheck v1.2.6 h1:1sWhqr93GdkWy4+6U7JxBfcyZIE78MhIHTJZfPx7qqI=
github.com/lightningnetwork/lnd/healthcheck v1.2.6/go.mod h1:Mu02um4CWY/zdTOvFje7WJgJcHyX2zq/FG3MhOAiGaQ=
github.com/lightningnetwork/lnd/kvdb v1.4.11 h1:fk1HMVFrsVK3xqU7q+JWHRgBltw/a2qIg1E3zazMb/8=
Expand Down
73 changes: 45 additions & 28 deletions lnwallet/chancloser/rbf_coop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chancloser

import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -144,7 +145,9 @@ func assertUnknownEventFail(t *testing.T, startingState ProtocolState) {

closeHarness.expectFailure(ErrInvalidStateTransition)

closeHarness.chanCloser.SendEvent(&unknownEvent{})
closeHarness.chanCloser.SendEvent(
context.Background(), &unknownEvent{},
)

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

ctx := context.Background()
numFeeCalls := 2

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

r.chanCloser.SendEvent(initEvent)
r.chanCloser.SendEvent(ctx, initEvent)

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

ctx := context.Background()

// We'll now send in the send offer event, which should trigger 1/2 of
// the RBF loop, ending us in the LocalOfferSent state.
r.expectHalfSignerIteration(
Expand All @@ -607,7 +613,7 @@ func (r *rbfCloserTestHarness) assertSingleRbfIteration(
balanceAfterClose, true,
)

r.chanCloser.SendEvent(localSigEvent)
r.chanCloser.SendEvent(ctx, localSigEvent)

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

ctx := context.Background()

// If this is an iteration, then we expect some intermediate states,
// before we enter the main RBF/sign loop.
if iteration {
Expand All @@ -635,7 +643,7 @@ func (r *rbfCloserTestHarness) assertSingleRemoteRbfIteration(
absoluteFee, balanceAfterClose, false,
)

r.chanCloser.SendEvent(initEvent)
r.chanCloser.SendEvent(ctx, initEvent)

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

ctx := context.Background()

startingHeight := 200

chanPoint := randOutPoint(t)
Expand Down Expand Up @@ -747,7 +757,7 @@ func newRbfCloserTestHarness(t *testing.T,
).Return(nil)

chanCloser := protofsm.NewStateMachine(protoCfg)
chanCloser.Start()
chanCloser.Start(ctx)

harness.stateSub = chanCloser.RegisterStateEvents()

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

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

closeHarness.chanCloser.SendEvent(&SpendEvent{})
closeHarness.chanCloser.SendEvent(ctx, &SpendEvent{})

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

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

// If we send the shutdown event, we should transition to the
// shutdown pending state.
closeHarness.chanCloser.SendEvent(&SendShutdown{
IdealFeeRate: feeRate,
})
closeHarness.chanCloser.SendEvent(
ctx, &SendShutdown{IdealFeeRate: feeRate},
)
closeHarness.assertStateTransitions(&ShutdownPending{})

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

// Next, we'll emit the recv event, with the addr of the remote
// party.
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
ShutdownScript: remoteAddr,
})
closeHarness.chanCloser.SendEvent(
ctx, &ShutdownReceived{ShutdownScript: remoteAddr},
)

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

startingState := &ShutdownPending{}

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

closeHarness.chanCloser.SendEvent(&SpendEvent{})
closeHarness.chanCloser.SendEvent(ctx, &SpendEvent{})

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

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

// We'll send in a shutdown received event, with the expected
// co-op close addr.
closeHarness.chanCloser.SendEvent(&ShutdownReceived{
ShutdownScript: remoteAddr,
})
closeHarness.chanCloser.SendEvent(
ctx, &ShutdownReceived{ShutdownScript: remoteAddr},
)

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

// We'll send in a shutdown received event.
closeHarness.chanCloser.SendEvent(&ShutdownComplete{})
closeHarness.chanCloser.SendEvent(ctx, &ShutdownComplete{})

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

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

// We'll now send in the event which should trigger
// this code path.
closeHarness.chanCloser.SendEvent(&chanFlushedEvent)
closeHarness.chanCloser.SendEvent(
ctx, &chanFlushedEvent,
)

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

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

// We should fail as the remote party sent us more than one
// signature.
closeHarness.chanCloser.SendEvent(localSigEvent)
closeHarness.chanCloser.SendEvent(ctx, localSigEvent)
})

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

closeHarness.chanCloser.SendEvent(sendShutdown)
closeHarness.chanCloser.SendEvent(ctx, sendShutdown)

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

localBalance := lnwire.NewMSatFromSatoshis(40_000)
remoteBalance := lnwire.NewMSatFromSatoshis(50_000)
Expand Down Expand Up @@ -1416,7 +1433,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
FeeSatoshis: absoluteFee * 10,
},
}
closeHarness.chanCloser.SendEvent(feeOffer)
closeHarness.chanCloser.SendEvent(ctx, feeOffer)

// We shouldn't have transitioned to a new state.
closeHarness.assertNoStateTransitions()
Expand Down Expand Up @@ -1460,7 +1477,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
},
},
}
closeHarness.chanCloser.SendEvent(feeOffer)
closeHarness.chanCloser.SendEvent(ctx, feeOffer)

// We shouldn't have transitioned to a new state.
closeHarness.assertNoStateTransitions()
Expand Down Expand Up @@ -1489,7 +1506,7 @@ func TestRbfCloseClosingNegotiationRemote(t *testing.T) {
},
},
}
closeHarness.chanCloser.SendEvent(feeOffer)
closeHarness.chanCloser.SendEvent(ctx, feeOffer)

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

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

0 comments on commit 3940344

Please sign in to comment.