Skip to content

Commit 11c7e20

Browse files
authored
Merge pull request #8953 from ProofOfKeags/refactor/lnwallet-channel-channel-constraints-partition
[MICRO]: multi: break ChannelConstraints into two sub-structures
2 parents 6e63dcd + 22e3470 commit 11c7e20

File tree

15 files changed

+338
-242
lines changed

15 files changed

+338
-242
lines changed

chanbackup/single_test.go

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,64 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
126126

127127
chanType := channeldb.ChannelType(rand.Intn(8))
128128

129+
localCfg := channeldb.ChannelConfig{
130+
ChannelStateBounds: channeldb.ChannelStateBounds{},
131+
CommitmentParams: channeldb.CommitmentParams{
132+
CsvDelay: uint16(rand.Int63()),
133+
},
134+
MultiSigKey: keychain.KeyDescriptor{
135+
KeyLocator: keychain.KeyLocator{
136+
Family: keychain.KeyFamily(rand.Int63()),
137+
Index: uint32(rand.Int63()),
138+
},
139+
},
140+
RevocationBasePoint: keychain.KeyDescriptor{
141+
KeyLocator: keychain.KeyLocator{
142+
Family: keychain.KeyFamily(rand.Int63()),
143+
Index: uint32(rand.Int63()),
144+
},
145+
},
146+
PaymentBasePoint: keychain.KeyDescriptor{
147+
KeyLocator: keychain.KeyLocator{
148+
Family: keychain.KeyFamily(rand.Int63()),
149+
Index: uint32(rand.Int63()),
150+
},
151+
},
152+
DelayBasePoint: keychain.KeyDescriptor{
153+
KeyLocator: keychain.KeyLocator{
154+
Family: keychain.KeyFamily(rand.Int63()),
155+
Index: uint32(rand.Int63()),
156+
},
157+
},
158+
HtlcBasePoint: keychain.KeyDescriptor{
159+
KeyLocator: keychain.KeyLocator{
160+
Family: keychain.KeyFamily(rand.Int63()),
161+
Index: uint32(rand.Int63()),
162+
},
163+
},
164+
}
165+
166+
remoteCfg := channeldb.ChannelConfig{
167+
CommitmentParams: channeldb.CommitmentParams{
168+
CsvDelay: uint16(rand.Int63()),
169+
},
170+
MultiSigKey: keychain.KeyDescriptor{
171+
PubKey: pub,
172+
},
173+
RevocationBasePoint: keychain.KeyDescriptor{
174+
PubKey: pub,
175+
},
176+
PaymentBasePoint: keychain.KeyDescriptor{
177+
PubKey: pub,
178+
},
179+
DelayBasePoint: keychain.KeyDescriptor{
180+
PubKey: pub,
181+
},
182+
HtlcBasePoint: keychain.KeyDescriptor{
183+
PubKey: pub,
184+
},
185+
}
186+
129187
return &channeldb.OpenChannel{
130188
ChainHash: chainHash,
131189
ChanType: chanType,
@@ -134,63 +192,10 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
134192
ShortChannelID: lnwire.NewShortChanIDFromInt(
135193
uint64(rand.Int63()),
136194
),
137-
ThawHeight: rand.Uint32(),
138-
IdentityPub: pub,
139-
LocalChanCfg: channeldb.ChannelConfig{
140-
ChannelConstraints: channeldb.ChannelConstraints{
141-
CsvDelay: uint16(rand.Int63()),
142-
},
143-
MultiSigKey: keychain.KeyDescriptor{
144-
KeyLocator: keychain.KeyLocator{
145-
Family: keychain.KeyFamily(rand.Int63()),
146-
Index: uint32(rand.Int63()),
147-
},
148-
},
149-
RevocationBasePoint: keychain.KeyDescriptor{
150-
KeyLocator: keychain.KeyLocator{
151-
Family: keychain.KeyFamily(rand.Int63()),
152-
Index: uint32(rand.Int63()),
153-
},
154-
},
155-
PaymentBasePoint: keychain.KeyDescriptor{
156-
KeyLocator: keychain.KeyLocator{
157-
Family: keychain.KeyFamily(rand.Int63()),
158-
Index: uint32(rand.Int63()),
159-
},
160-
},
161-
DelayBasePoint: keychain.KeyDescriptor{
162-
KeyLocator: keychain.KeyLocator{
163-
Family: keychain.KeyFamily(rand.Int63()),
164-
Index: uint32(rand.Int63()),
165-
},
166-
},
167-
HtlcBasePoint: keychain.KeyDescriptor{
168-
KeyLocator: keychain.KeyLocator{
169-
Family: keychain.KeyFamily(rand.Int63()),
170-
Index: uint32(rand.Int63()),
171-
},
172-
},
173-
},
174-
RemoteChanCfg: channeldb.ChannelConfig{
175-
ChannelConstraints: channeldb.ChannelConstraints{
176-
CsvDelay: uint16(rand.Int63()),
177-
},
178-
MultiSigKey: keychain.KeyDescriptor{
179-
PubKey: pub,
180-
},
181-
RevocationBasePoint: keychain.KeyDescriptor{
182-
PubKey: pub,
183-
},
184-
PaymentBasePoint: keychain.KeyDescriptor{
185-
PubKey: pub,
186-
},
187-
DelayBasePoint: keychain.KeyDescriptor{
188-
PubKey: pub,
189-
},
190-
HtlcBasePoint: keychain.KeyDescriptor{
191-
PubKey: pub,
192-
},
193-
},
195+
ThawHeight: rand.Uint32(),
196+
IdentityPub: pub,
197+
LocalChanCfg: localCfg,
198+
RemoteChanCfg: remoteCfg,
194199
RevocationProducer: shaChainProducer,
195200
}, nil
196201
}

channeldb/channel.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,11 @@ func (c ChannelType) IsTaproot() bool {
395395
return c&SimpleTaprootFeatureBit == SimpleTaprootFeatureBit
396396
}
397397

398-
// ChannelConstraints represents a set of constraints meant to allow a node to
399-
// limit their exposure, enact flow control and ensure that all HTLCs are
400-
// economically relevant. This struct will be mirrored for both sides of the
401-
// channel, as each side will enforce various constraints that MUST be adhered
402-
// to for the life time of the channel. The parameters for each of these
403-
// constraints are static for the duration of the channel, meaning the channel
404-
// must be torn down for them to change.
405-
type ChannelConstraints struct {
406-
// DustLimit is the threshold (in satoshis) below which any outputs
407-
// should be trimmed. When an output is trimmed, it isn't materialized
408-
// as an actual output, but is instead burned to miner's fees.
409-
DustLimit btcutil.Amount
410-
398+
// ChannelStateBounds are the parameters from OpenChannel and AcceptChannel
399+
// that are responsible for providing bounds on the state space of the abstract
400+
// channel state. These values must be remembered for normal channel operation
401+
// but they do not impact how we compute the commitment transactions themselves.
402+
type ChannelStateBounds struct {
411403
// ChanReserve is an absolute reservation on the channel for the
412404
// owner of this set of constraints. This means that the current
413405
// settled balance for this node CANNOT dip below the reservation
@@ -433,6 +425,19 @@ type ChannelConstraints struct {
433425
// acted upon in the case of a unilateral channel closure or a contract
434426
// breach.
435427
MaxAcceptedHtlcs uint16
428+
}
429+
430+
// CommitmentParams are the parameters from OpenChannel and
431+
// AcceptChannel that are required to render an abstract channel state to a
432+
// concrete commitment transaction. These values are necessary to (re)compute
433+
// the commitment transaction. We treat these differently than the state space
434+
// bounds because their history needs to be stored in order to properly handle
435+
// chain resolution.
436+
type CommitmentParams struct {
437+
// DustLimit is the threshold (in satoshis) below which any outputs
438+
// should be trimmed. When an output is trimmed, it isn't materialized
439+
// as an actual output, but is instead burned to miner's fees.
440+
DustLimit btcutil.Amount
436441

437442
// CsvDelay is the relative time lock delay expressed in blocks. Any
438443
// settled outputs that pay to the owner of this channel configuration
@@ -448,12 +453,17 @@ type ChannelConstraints struct {
448453
// nature of HTLC's allotted, the keys to be used for delivery, and relative
449454
// time lock parameters.
450455
type ChannelConfig struct {
451-
// ChannelConstraints is the set of constraints that must be upheld for
452-
// the duration of the channel for the owner of this channel
456+
// ChannelStateBounds is the set of constraints that must be
457+
// upheld for the duration of the channel for the owner of this channel
453458
// configuration. Constraints govern a number of flow control related
454459
// parameters, also including the smallest HTLC that will be accepted
455460
// by a participant.
456-
ChannelConstraints
461+
ChannelStateBounds
462+
463+
// CommitmentParams is an embedding of the parameters
464+
// required to render an abstract channel state into a concrete
465+
// commitment transaction.
466+
CommitmentParams
457467

458468
// MultiSigKey is the key to be used within the 2-of-2 output script
459469
// for the owner of this channel config.

channeldb/channel_test.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,21 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
235235
}
236236
}
237237

238+
localStateBounds := ChannelStateBounds{
239+
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
240+
ChanReserve: btcutil.Amount(rand.Int63()),
241+
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
242+
MaxAcceptedHtlcs: uint16(rand.Int31()),
243+
}
244+
245+
localRenderingParams := CommitmentParams{
246+
DustLimit: btcutil.Amount(rand.Int63()),
247+
CsvDelay: uint16(rand.Int31()),
248+
}
249+
238250
localCfg := ChannelConfig{
239-
ChannelConstraints: ChannelConstraints{
240-
DustLimit: btcutil.Amount(rand.Int63()),
241-
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
242-
ChanReserve: btcutil.Amount(rand.Int63()),
243-
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
244-
MaxAcceptedHtlcs: uint16(rand.Int31()),
245-
CsvDelay: uint16(rand.Int31()),
246-
},
251+
ChannelStateBounds: localStateBounds,
252+
CommitmentParams: localRenderingParams,
247253
MultiSigKey: keychain.KeyDescriptor{
248254
PubKey: privKey.PubKey(),
249255
},
@@ -260,15 +266,22 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
260266
PubKey: privKey.PubKey(),
261267
},
262268
}
269+
270+
remoteStateBounds := ChannelStateBounds{
271+
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
272+
ChanReserve: btcutil.Amount(rand.Int63()),
273+
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
274+
MaxAcceptedHtlcs: uint16(rand.Int31()),
275+
}
276+
277+
remoteRenderingParams := CommitmentParams{
278+
DustLimit: btcutil.Amount(rand.Int63()),
279+
CsvDelay: uint16(rand.Int31()),
280+
}
281+
263282
remoteCfg := ChannelConfig{
264-
ChannelConstraints: ChannelConstraints{
265-
DustLimit: btcutil.Amount(rand.Int63()),
266-
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
267-
ChanReserve: btcutil.Amount(rand.Int63()),
268-
MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
269-
MaxAcceptedHtlcs: uint16(rand.Int31()),
270-
CsvDelay: uint16(rand.Int31()),
271-
},
283+
ChannelStateBounds: remoteStateBounds,
284+
CommitmentParams: remoteRenderingParams,
272285
MultiSigKey: keychain.KeyDescriptor{
273286
PubKey: privKey.PubKey(),
274287
KeyLocator: keychain.KeyLocator{

channeldb/db_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func genRandomChannelShell() (*ChannelShell, error) {
292292
}
293293
shaChainProducer := shachain.NewRevocationProducer(*revRoot)
294294

295+
commitParams := CommitmentParams{
296+
CsvDelay: uint16(rand.Int63()),
297+
}
298+
295299
return &ChannelShell{
296300
NodeAddrs: []net.Addr{&net.TCPAddr{
297301
IP: net.ParseIP("127.0.0.1"),
@@ -306,9 +310,7 @@ func genRandomChannelShell() (*ChannelShell, error) {
306310
),
307311
IdentityPub: pub,
308312
LocalChanCfg: ChannelConfig{
309-
ChannelConstraints: ChannelConstraints{
310-
CsvDelay: uint16(rand.Int63()),
311-
},
313+
CommitmentParams: commitParams,
312314
PaymentBasePoint: keychain.KeyDescriptor{
313315
KeyLocator: keychain.KeyLocator{
314316
Family: keychain.KeyFamily(rand.Int63()),

contractcourt/breach_arbitrator_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,13 +2178,15 @@ func createInitChannels(t *testing.T) (
21782178
fundingTxIn := wire.NewTxIn(prevOut, nil, nil)
21792179

21802180
aliceCfg := channeldb.ChannelConfig{
2181-
ChannelConstraints: channeldb.ChannelConstraints{
2182-
DustLimit: aliceDustLimit,
2181+
ChannelStateBounds: channeldb.ChannelStateBounds{
21832182
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
21842183
ChanReserve: 0,
21852184
MinHTLC: 0,
21862185
MaxAcceptedHtlcs: uint16(rand.Int31()),
2187-
CsvDelay: uint16(csvTimeoutAlice),
2186+
},
2187+
CommitmentParams: channeldb.CommitmentParams{
2188+
DustLimit: aliceDustLimit,
2189+
CsvDelay: uint16(csvTimeoutAlice),
21882190
},
21892191
MultiSigKey: keychain.KeyDescriptor{
21902192
PubKey: aliceKeyPub,
@@ -2203,13 +2205,15 @@ func createInitChannels(t *testing.T) (
22032205
},
22042206
}
22052207
bobCfg := channeldb.ChannelConfig{
2206-
ChannelConstraints: channeldb.ChannelConstraints{
2207-
DustLimit: bobDustLimit,
2208+
ChannelStateBounds: channeldb.ChannelStateBounds{
22082209
MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
22092210
ChanReserve: 0,
22102211
MinHTLC: 0,
22112212
MaxAcceptedHtlcs: uint16(rand.Int31()),
2212-
CsvDelay: uint16(csvTimeoutBob),
2213+
},
2214+
CommitmentParams: channeldb.CommitmentParams{
2215+
DustLimit: bobDustLimit,
2216+
CsvDelay: uint16(csvTimeoutBob),
22132217
},
22142218
MultiSigKey: keychain.KeyDescriptor{
22152219
PubKey: bobKeyPub,

0 commit comments

Comments
 (0)