Skip to content

Commit d057e25

Browse files
committed
staticaddr/loopin: update fsm for server ntfn sigs
This commit updates the loop in FSM to handle the new server based pulled sweepless sweep sigs.
1 parent ac36f1d commit d057e25

File tree

2 files changed

+22
-132
lines changed

2 files changed

+22
-132
lines changed

staticaddr/loopin/actions.go

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
1313
"github.com/btcsuite/btcd/btcutil"
1414
"github.com/btcsuite/btcd/txscript"
15-
"github.com/btcsuite/btcd/wire"
1615
"github.com/btcsuite/btcwallet/chain"
1716
"github.com/lightninglabs/lndclient"
1817
"github.com/lightninglabs/loop"
@@ -777,102 +776,7 @@ func (f *FSM) PaymentReceivedAction(ctx context.Context,
777776
return f.HandleError(err)
778777
}
779778

780-
return OnFetchSignPushSweeplessSweepTx
781-
}
782-
783-
// FetchSignPushSweeplessSweepTxAction requests server nonces, fee rate and
784-
// destination address for the sweepless sweep transaction. It then creates the
785-
// sweep transaction and signs it with the server and client nonces. If signing
786-
// succeeds it pushes the signatures to the server.
787-
func (f *FSM) FetchSignPushSweeplessSweepTxAction(ctx context.Context,
788-
_ fsm.EventContext) fsm.EventType {
789-
790-
// Fetch the sweepless sweep tx details from server.
791-
fetchReq := &looprpc.FetchSweeplessSweepTxRequest{
792-
SwapHash: f.loopIn.SwapHash[:],
793-
}
794-
fetchResp, err := f.cfg.Server.FetchSweeplessSweepTx(ctx, fetchReq)
795-
if err != nil {
796-
err = fmt.Errorf("unable to fetch sweepless sweep tx: %w", err)
797-
798-
return f.HandleError(err)
799-
}
800-
801-
address, err := btcutil.DecodeAddress(
802-
fetchResp.SweepAddr, f.cfg.ChainParams,
803-
)
804-
if err != nil {
805-
f.Warnf("unable to decode sweep address: %v", err)
806-
}
807-
808-
clientResponse := make(map[uint64]*looprpc.ClientSweeplessSigningInfo)
809-
810-
var (
811-
nonces [][]byte
812-
clientNonces [][musig2.PubNonceSize]byte
813-
serverNonces [][musig2.PubNonceSize]byte
814-
sessions []*input.MuSig2SessionInfo
815-
sweepTx *wire.MsgTx
816-
sweeplessClientSigs [][]byte
817-
)
818-
for feeRate, serverInfo := range fetchResp.FeeRateToNonces {
819-
satPerKW := chainfee.SatPerKWeight(feeRate)
820-
serverNonces, err = toNonces(serverInfo.Nonces)
821-
if err != nil {
822-
err = fmt.Errorf("unable to convert server nonces: %w",
823-
err)
824-
return f.HandleError(err)
825-
}
826-
827-
sessions, nonces, err = f.loopIn.createMusig2Sessions(
828-
ctx, f.cfg.Signer,
829-
)
830-
if err != nil {
831-
return f.HandleError(err)
832-
}
833-
834-
clientNonces, err = toNonces(nonces)
835-
if err != nil {
836-
return f.HandleError(err)
837-
}
838-
839-
sweepTx, err = f.loopIn.createSweeplessSweepTx(
840-
address, satPerKW,
841-
)
842-
if err != nil {
843-
err = fmt.Errorf("unable to create sweepless sweep "+
844-
"tx: %w", err)
845-
return f.HandleError(err)
846-
}
847-
848-
sweeplessClientSigs, err = f.loopIn.signMusig2Tx(
849-
ctx, sweepTx, f.cfg.Signer, sessions, serverNonces,
850-
)
851-
if err != nil {
852-
err = fmt.Errorf("unable to sign sweepless sweep tx: "+
853-
"%w", err)
854-
return f.HandleError(err)
855-
}
856-
857-
clientResponse[feeRate] = &looprpc.ClientSweeplessSigningInfo{
858-
Nonces: fromNonces(clientNonces),
859-
Sigs: sweeplessClientSigs,
860-
}
861-
}
862-
863-
// Push sweepless sigs to the server.
864-
req := &looprpc.PushStaticAddressSweeplessSigsRequest{
865-
SwapHash: f.loopIn.SwapHash[:],
866-
SigningInfo: clientResponse,
867-
}
868-
_, err = f.cfg.Server.PushStaticAddressSweeplessSigs(ctx, req)
869-
if err != nil {
870-
err = fmt.Errorf("unable to push sweepless sweep sigs: %w", err)
871-
872-
return f.HandleError(err)
873-
}
874-
875-
return OnSweeplessSweepSigned
779+
return OnSucceeded
876780
}
877781

878782
// UnlockDepositsAction is called if the loop-in failed and its deposits should

staticaddr/loopin/fsm.go

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,13 @@ var (
111111
// transaction was sufficiently confirmed.
112112
HtlcTimeoutSwept = fsm.StateType("HtlcTimeoutSwept")
113113

114-
// FetchSignPushSweeplessSweepTx is the state where the client fetches,
115-
// signs and pushes the sweepless sweep tx signatures to the server.
116-
FetchSignPushSweeplessSweepTx = fsm.StateType("FetchSignPushSweeplessSweepTx") //nolint:lll
117-
118114
// Succeeded is the state the swap is in if it was successful.
119115
Succeeded = fsm.StateType("Succeeded")
120116

121-
// SucceededSweeplessSigFailed is the state the swap is in if the swap
122-
// payment was received but the client failed to sign the sweepless
123-
// sweep transaction. This is considered a successful case from the
124-
// client's perspective.
125-
SucceededSweeplessSigFailed = fsm.StateType("SucceededSweeplessSigFailed") //nolint:lll
117+
// SucceededTransitioningFailed is the state the swap is in if the swap
118+
// payment was received but the client was not able to transition
119+
// the deposits to the looped-in state.
120+
SucceededTransitioningFailed = fsm.StateType("SucceededTransitioningFailed") //nolint:lll
126121

127122
// UnlockDeposits is the state where the deposits are reset. This
128123
// happens when the state machine encountered an error and the swap
@@ -135,30 +130,29 @@ var (
135130

136131
var PendingStates = []fsm.StateType{
137132
InitHtlcTx, SignHtlcTx, MonitorInvoiceAndHtlcTx, PaymentReceived,
138-
SweepHtlcTimeout, MonitorHtlcTimeoutSweep, FetchSignPushSweeplessSweepTx,
133+
SweepHtlcTimeout, MonitorHtlcTimeoutSweep,
139134
UnlockDeposits,
140135
}
141136

142137
var FinalStates = []fsm.StateType{
143-
HtlcTimeoutSwept, Succeeded, SucceededSweeplessSigFailed, Failed,
138+
HtlcTimeoutSwept, Succeeded, SucceededTransitioningFailed, Failed,
144139
}
145140

146141
var AllStates = append(PendingStates, FinalStates...)
147142

148143
// Events.
149144
var (
150-
OnInitHtlc = fsm.EventType("OnInitHtlc")
151-
OnHtlcInitiated = fsm.EventType("OnHtlcInitiated")
152-
OnHtlcTxSigned = fsm.EventType("OnHtlcTxSigned")
153-
OnSweepHtlcTimeout = fsm.EventType("OnSweepHtlcTimeout")
154-
OnHtlcTimeoutSweepPublished = fsm.EventType("OnHtlcTimeoutSweepPublished")
155-
OnHtlcTimeoutSwept = fsm.EventType("OnHtlcTimeoutSwept")
156-
OnPaymentReceived = fsm.EventType("OnPaymentReceived")
157-
OnPaymentDeadlineExceeded = fsm.EventType("OnPaymentDeadlineExceeded")
158-
OnSwapTimedOut = fsm.EventType("OnSwapTimedOut")
159-
OnFetchSignPushSweeplessSweepTx = fsm.EventType("OnFetchSignPushSweeplessSweepTx")
160-
OnSweeplessSweepSigned = fsm.EventType("OnSweeplessSweepSigned")
161-
OnRecover = fsm.EventType("OnRecover")
145+
OnInitHtlc = fsm.EventType("OnInitHtlc")
146+
OnHtlcInitiated = fsm.EventType("OnHtlcInitiated")
147+
OnHtlcTxSigned = fsm.EventType("OnHtlcTxSigned")
148+
OnSweepHtlcTimeout = fsm.EventType("OnSweepHtlcTimeout")
149+
OnHtlcTimeoutSweepPublished = fsm.EventType("OnHtlcTimeoutSweepPublished")
150+
OnHtlcTimeoutSwept = fsm.EventType("OnHtlcTimeoutSwept")
151+
OnPaymentReceived = fsm.EventType("OnPaymentReceived")
152+
OnPaymentDeadlineExceeded = fsm.EventType("OnPaymentDeadlineExceeded")
153+
OnSwapTimedOut = fsm.EventType("OnSwapTimedOut")
154+
OnSucceeded = fsm.EventType("OnSucceeded")
155+
OnRecover = fsm.EventType("OnRecover")
162156
)
163157

164158
// LoopInStatesV0 returns the state and transition map for the loop-in state
@@ -215,27 +209,19 @@ func (f *FSM) LoopInStatesV0() fsm.States {
215209
},
216210
PaymentReceived: fsm.State{
217211
Transitions: fsm.Transitions{
218-
OnFetchSignPushSweeplessSweepTx: FetchSignPushSweeplessSweepTx,
219-
OnRecover: SucceededSweeplessSigFailed,
220-
fsm.OnError: SucceededSweeplessSigFailed,
212+
OnSucceeded: Succeeded,
213+
OnRecover: Succeeded,
214+
fsm.OnError: SucceededTransitioningFailed,
221215
},
222216
Action: f.PaymentReceivedAction,
223217
},
224-
FetchSignPushSweeplessSweepTx: fsm.State{
225-
Transitions: fsm.Transitions{
226-
OnSweeplessSweepSigned: Succeeded,
227-
OnRecover: SucceededSweeplessSigFailed,
228-
fsm.OnError: SucceededSweeplessSigFailed,
229-
},
230-
Action: f.FetchSignPushSweeplessSweepTxAction,
231-
},
232218
HtlcTimeoutSwept: fsm.State{
233219
Action: fsm.NoOpAction,
234220
},
235221
Succeeded: fsm.State{
236222
Action: fsm.NoOpAction,
237223
},
238-
SucceededSweeplessSigFailed: fsm.State{
224+
SucceededTransitioningFailed: fsm.State{
239225
Action: fsm.NoOpAction,
240226
},
241227
UnlockDeposits: fsm.State{

0 commit comments

Comments
 (0)