Skip to content

Commit b985182

Browse files
committed
staticaddr: high fee versions of the htlc tx
1 parent ba5eaba commit b985182

File tree

5 files changed

+488
-273
lines changed

5 files changed

+488
-273
lines changed

staticaddr/loopin/actions.go

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,53 @@ func (f *FSM) InitHtlcAction(_ fsm.EventContext) fsm.EventType {
118118
}
119119

120120
f.loopIn.HtlcCltvExpiry = loopInResp.HtlcExpiry
121+
122+
// Retrieve htlc tx server nonces.
121123
f.htlcServerNonces, err = toNonces(loopInResp.HtlcServerNonces)
122124
if err != nil {
123125
return f.HandleError(err)
124126
}
127+
f.htlcServerNoncesHighFee, err = toNonces(
128+
loopInResp.HtlcServerNoncesHighFee,
129+
)
130+
if err != nil {
131+
return f.HandleError(err)
132+
}
133+
f.htlcServerNoncesExtremelyHighFee, err = toNonces(
134+
loopInResp.HtlcServerNoncesExtremelyHighFee,
135+
)
136+
if err != nil {
137+
return f.HandleError(err)
138+
}
125139

126140
// We need to defend against the server setting high fees for the htlc
127141
// tx since we might have to sweep the timeout path. We maximally allow
128142
// 20% of the swap value to be spent on fees.
129-
f.loopIn.HtlcTxFeeRate = chainfee.SatPerKWeight(loopInResp.HtlcFeeRate)
130-
fee := f.loopIn.HtlcTxFeeRate.FeeForWeight(f.loopIn.htlcWeight())
143+
feeRate := chainfee.SatPerKWeight(loopInResp.HtlcFeeRate)
144+
fee := feeRate.FeeForWeight(f.loopIn.htlcWeight())
145+
if fee > f.loopIn.TotalDepositAmount()/5 {
146+
return f.HandleError(errors.New("fee is higher than 20% of " +
147+
"sweep value"))
148+
}
149+
f.loopIn.HtlcTxFeeRate = feeRate
131150

151+
highFeeRate := chainfee.SatPerKWeight(loopInResp.HighHtlcFeeRate)
152+
fee = highFeeRate.FeeForWeight(f.loopIn.htlcWeight())
132153
if fee > f.loopIn.TotalDepositAmount()/5 {
133154
return f.HandleError(errors.New("fee is higher than 20% of " +
134155
"sweep value"))
135156
}
157+
f.loopIn.HtlcTxHighFeeRate = highFeeRate
158+
159+
extremelyHighFeeRate := chainfee.SatPerKWeight(
160+
loopInResp.ExtremelyHighHtlcFeeRate,
161+
)
162+
fee = extremelyHighFeeRate.FeeForWeight(f.loopIn.htlcWeight())
163+
if fee > f.loopIn.TotalDepositAmount()/5 {
164+
return f.HandleError(errors.New("fee is higher than 20% of " +
165+
"sweep value"))
166+
}
167+
f.loopIn.HtlcTxExtremelyHighFeeRate = extremelyHighFeeRate
136168

137169
// Derive the sweep address for the htlc timeout sweep tx.
138170
sweepAddress, err := f.cfg.WalletKit.NextAddr(
@@ -171,38 +203,78 @@ func (f *FSM) SignHtlcTxAction(_ fsm.EventContext) fsm.EventType {
171203
return f.HandleError(err)
172204
}
173205

174-
// Create a musig2 session for each deposit.
206+
// Create a musig2 session for each deposit and different htlc tx fee
207+
// rates.
175208
htlcSessions, clientHtlcNonces, err := f.loopIn.createMusig2Sessions(
176209
f.ctx, f.cfg.Signer,
177210
)
178211
if err != nil {
179212
return f.HandleError(err)
180213
}
181-
f.htlcMusig2Sessions = htlcSessions
182-
f.htlcClientNonces, err = toNonces(clientHtlcNonces)
214+
htlcSessionsHighFee, highFeeNonces, err := f.loopIn.createMusig2Sessions(
215+
f.ctx, f.cfg.Signer,
216+
)
217+
if err != nil {
218+
return f.HandleError(err)
219+
}
220+
htlcSessionsExtremelyHighFee, extremelyHighNonces, err := f.loopIn.createMusig2Sessions( //nolint:lll
221+
f.ctx, f.cfg.Signer,
222+
)
183223
if err != nil {
184224
return f.HandleError(err)
185225
}
186226

187-
htlcTx, err := f.loopIn.createHtlcTx(f.cfg.ChainParams)
227+
// Create the htlc txns for different fee rates.
228+
htlcTx, err := f.loopIn.createHtlcTx(
229+
f.cfg.ChainParams, f.loopIn.HtlcTxFeeRate,
230+
)
231+
if err != nil {
232+
return f.HandleError(err)
233+
}
234+
htlcTxHighFee, err := f.loopIn.createHtlcTx(
235+
f.cfg.ChainParams, f.loopIn.HtlcTxHighFeeRate,
236+
)
237+
if err != nil {
238+
return f.HandleError(err)
239+
}
240+
htlcTxExtremelyHighFee, err := f.loopIn.createHtlcTx(
241+
f.cfg.ChainParams, f.loopIn.HtlcTxExtremelyHighFeeRate,
242+
)
188243
if err != nil {
189244
return f.HandleError(err)
190245
}
191246

192-
// Next we'll get our htlc tx signatures.
247+
// Next we'll get our htlc tx signatures for different fee rates.
193248
htlcSigs, err := f.loopIn.signMusig2Tx(
194249
f.ctx, htlcTx, f.cfg.Signer, htlcSessions, f.htlcServerNonces,
195250
)
196251
if err != nil {
197252
return f.HandleError(err)
198253
}
199-
f.htlcClientSigs = htlcSigs
254+
htlcSigsHighFee, err := f.loopIn.signMusig2Tx(
255+
f.ctx, htlcTxHighFee, f.cfg.Signer, htlcSessionsHighFee,
256+
f.htlcServerNoncesHighFee,
257+
)
258+
if err != nil {
259+
return f.HandleError(err)
260+
}
261+
htlcSigsExtremelyHighFee, err := f.loopIn.signMusig2Tx(
262+
f.ctx, htlcTxExtremelyHighFee, f.cfg.Signer,
263+
htlcSessionsExtremelyHighFee, f.htlcServerNoncesExtremelyHighFee,
264+
)
265+
if err != nil {
266+
return f.HandleError(err)
267+
}
200268

201269
// Push htlc tx sigs to server.
202270
pushHtlcReq := &looprpc.PushStaticAddressHtlcSigsRequest{
203-
SwapHash: f.loopIn.SwapHash[:],
204-
HtlcClientNonces: clientHtlcNonces,
205-
HtlcClientSigs: htlcSigs,
271+
SwapHash: f.loopIn.SwapHash[:],
272+
HtlcClientNonces: clientHtlcNonces,
273+
HtlcClientNoncesHighFee: highFeeNonces,
274+
HtlcClientNoncesExtremelyHighFee: extremelyHighNonces,
275+
HtlcClientSigs: htlcSigs,
276+
HtlcClientSigsHighFee: htlcSigsHighFee,
277+
HtlcClientSigsExtremelyHighFee: htlcSigsExtremelyHighFee,
206278
}
207279
sigPushResp, err := f.cfg.StaticAddressServerClient.PushStaticAddressHtlcSigs( //nolint:lll
208280
f.ctx, pushHtlcReq,

staticaddr/loopin/fsm.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,17 @@ type FSM struct {
2626
// MuSig2 data must not be re-used across restarts, hence it is not
2727
// persisted.
2828
//
29-
// htlcMusig2Sessions contains all the loop-in musig2 sessions that will
30-
// be used for the htlc transaction.
31-
htlcMusig2Sessions []*input.MuSig2SessionInfo
32-
33-
// htlcClientNonces contains all the nonces that the client sent for
34-
// the htlc musig2 sessions.
35-
htlcClientNonces [][musig2.PubNonceSize]byte
36-
3729
// htlcServerNonces contains all the nonces that the server generated
3830
// for the htlc musig2 sessions.
3931
htlcServerNonces [][musig2.PubNonceSize]byte
4032

41-
// htlcClientSigs contains all the signatures that the client generated
33+
// htlcServerNonces contains all the nonces that the server generated
34+
// for the htlc musig2 sessions.
35+
htlcServerNoncesHighFee [][musig2.PubNonceSize]byte
36+
37+
// htlcServerNonces contains all the nonces that the server generated
4238
// for the htlc musig2 sessions.
43-
htlcClientSigs [][]byte
39+
htlcServerNoncesExtremelyHighFee [][musig2.PubNonceSize]byte
4440

4541
// sweeplessMusig2Sessions contains all the loop-in musig2 sessions that
4642
// will be used for signing the sweepless sweep transaction.

staticaddr/loopin/loopin.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ type StaticAddressLoopIn struct {
115115
// HtlcTxFeeRate is the fee rate that is used for the htlc transaction.
116116
HtlcTxFeeRate chainfee.SatPerKWeight
117117

118+
// HtlcTxHighFeeRate is the fee rate that is used for the htlc
119+
// transaction.
120+
HtlcTxHighFeeRate chainfee.SatPerKWeight
121+
122+
// HtlcTxExtremelyHighFeeRate is the fee rate that is used for the htlc
123+
// transaction.
124+
HtlcTxExtremelyHighFeeRate chainfee.SatPerKWeight
125+
118126
// HtlcTimeoutSweepTxHash is the hash of the htlc timeout sweep tx.
119127
HtlcTimeoutSweepTxHash *chainhash.Hash
120128

@@ -309,8 +317,8 @@ func (l *StaticAddressLoopIn) signMusig2Tx(ctx context.Context,
309317

310318
// createHtlcTx creates the transaction that spend the deposit outpoints into
311319
// a htlc outpoint.
312-
func (l *StaticAddressLoopIn) createHtlcTx(chainParams *chaincfg.Params) (
313-
*wire.MsgTx, error) {
320+
func (l *StaticAddressLoopIn) createHtlcTx(chainParams *chaincfg.Params,
321+
feeRate chainfee.SatPerKWeight) (*wire.MsgTx, error) {
314322

315323
// First Create the tx.
316324
msgTx := wire.NewMsgTx(2)
@@ -326,7 +334,7 @@ func (l *StaticAddressLoopIn) createHtlcTx(chainParams *chaincfg.Params) (
326334

327335
// Calculate htlc tx fee for server provided fee rate.
328336
weight := l.htlcWeight()
329-
fee := l.HtlcTxFeeRate.FeeForWeight(weight)
337+
fee := feeRate.FeeForWeight(weight)
330338

331339
htlc, err := l.getHtlc(chainParams)
332340
if err != nil {

0 commit comments

Comments
 (0)