Skip to content

Commit 258dd5c

Browse files
Roasbeefguggero
authored andcommitted
lnwallet+channeldb: add new AuxLeafStore for dynamic aux leaves
In this commit, we add a new AuxLeafStore which can be used to dynamically fetch the latest aux leaves for a given state. This is useful for custom channel types that will store some extra information in the form of a custom blob, then will use that information to derive the new leaf tapscript leaves that may be attached to reach state.
1 parent fc44ff4 commit 258dd5c

File tree

5 files changed

+303
-34
lines changed

5 files changed

+303
-34
lines changed

contractcourt/chain_watcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func (c *chainWatcher) handleUnknownLocalState(
429429
}
430430
remoteScript, _, err := lnwallet.CommitScriptToRemote(
431431
c.cfg.chanState.ChanType, c.cfg.chanState.IsInitiator,
432-
commitKeyRing.ToRemoteKey, leaseExpiry,
432+
commitKeyRing.ToRemoteKey, leaseExpiry, input.NoneTapLeaf(),
433433
)
434434
if err != nil {
435435
return false, err
@@ -442,6 +442,7 @@ func (c *chainWatcher) handleUnknownLocalState(
442442
c.cfg.chanState.ChanType, c.cfg.chanState.IsInitiator,
443443
commitKeyRing.ToLocalKey, commitKeyRing.RevocationKey,
444444
uint32(c.cfg.chanState.LocalChanCfg.CsvDelay), leaseExpiry,
445+
input.NoneTapLeaf(),
445446
)
446447
if err != nil {
447448
return false, err

lnwallet/channel.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,10 @@ type LightningChannel struct {
12691269
// machine.
12701270
Signer input.Signer
12711271

1272+
// leafStore is used to retrieve extra tapscript leaves for special
1273+
// custom channel types.
1274+
leafStore fn.Option[AuxLeafStore]
1275+
12721276
// signDesc is the primary sign descriptor that is capable of signing
12731277
// the commitment transaction that spends the multi-sig output.
12741278
signDesc *input.SignDescriptor
@@ -1344,6 +1348,8 @@ type channelOpts struct {
13441348
localNonce *musig2.Nonces
13451349
remoteNonce *musig2.Nonces
13461350

1351+
leafStore fn.Option[AuxLeafStore]
1352+
13471353
skipNonceInit bool
13481354
}
13491355

@@ -1374,6 +1380,13 @@ func WithSkipNonceInit() ChannelOpt {
13741380
}
13751381
}
13761382

1383+
// WithLeafStore is used to specify a custom leaf store for the channel.
1384+
func WithLeafStore(store AuxLeafStore) ChannelOpt {
1385+
return func(o *channelOpts) {
1386+
o.leafStore = fn.Some[AuxLeafStore](store)
1387+
}
1388+
}
1389+
13771390
// defaultChannelOpts returns the set of default options for a new channel.
13781391
func defaultChannelOpts() *channelOpts {
13791392
return &channelOpts{}
@@ -1415,13 +1428,16 @@ func NewLightningChannel(signer input.Signer,
14151428
}
14161429

14171430
lc := &LightningChannel{
1418-
Signer: signer,
1419-
sigPool: sigPool,
1420-
currentHeight: localCommit.CommitHeight,
1421-
remoteCommitChain: newCommitmentChain(),
1422-
localCommitChain: newCommitmentChain(),
1423-
channelState: state,
1424-
commitBuilder: NewCommitmentBuilder(state),
1431+
Signer: signer,
1432+
leafStore: opts.leafStore,
1433+
sigPool: sigPool,
1434+
currentHeight: localCommit.CommitHeight,
1435+
remoteCommitChain: newCommitmentChain(),
1436+
localCommitChain: newCommitmentChain(),
1437+
channelState: state,
1438+
commitBuilder: NewCommitmentBuilder(
1439+
state, opts.leafStore,
1440+
),
14251441
localUpdateLog: localUpdateLog,
14261442
remoteUpdateLog: remoteUpdateLog,
14271443
Capacity: state.Capacity,
@@ -2464,12 +2480,14 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
24642480
leaseExpiry = chanState.ThawHeight
24652481
}
24662482

2483+
// TODO(roasbeef): fetch aux leave
2484+
24672485
// Since it is the remote breach we are reconstructing, the output
24682486
// going to us will be a to-remote script with our local params.
24692487
isRemoteInitiator := !chanState.IsInitiator
24702488
ourScript, ourDelay, err := CommitScriptToRemote(
24712489
chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
2472-
leaseExpiry,
2490+
leaseExpiry, input.NoneTapLeaf(),
24732491
)
24742492
if err != nil {
24752493
return nil, err
@@ -2479,6 +2497,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
24792497
theirScript, err := CommitScriptToSelf(
24802498
chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
24812499
keyRing.RevocationKey, theirDelay, leaseExpiry,
2500+
input.NoneTapLeaf(),
24822501
)
24832502
if err != nil {
24842503
return nil, err
@@ -3033,7 +3052,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30333052
// Actually generate unsigned commitment transaction for this view.
30343053
commitTx, err := lc.commitBuilder.createUnsignedCommitmentTx(
30353054
ourBalance, theirBalance, !remoteChain, feePerKw, nextHeight,
3036-
filteredHTLCView, keyRing,
3055+
htlcView, filteredHTLCView, keyRing, commitChain.tip(),
30373056
)
30383057
if err != nil {
30393058
return nil, err
@@ -3068,6 +3087,16 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30683087
effFeeRate, spew.Sdump(commitTx))
30693088
}
30703089

3090+
// Given the custom blob of the past state, and this new HTLC view,
3091+
// we'll generate a new blob for the latest commitment.
3092+
newCommitBlob, err := updateAuxBlob(
3093+
lc.channelState, commitChain.tip().customBlob, htlcView,
3094+
!remoteChain, ourBalance, theirBalance, lc.leafStore, *keyRing,
3095+
)
3096+
if err != nil {
3097+
return nil, fmt.Errorf("unable to fetch aux leaves: %w", err)
3098+
}
3099+
30713100
// With the commitment view created, store the resulting balances and
30723101
// transaction with the other parameters for this height.
30733102
c := &commitment{
@@ -3083,6 +3112,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30833112
feePerKw: feePerKw,
30843113
dustLimit: dustLimit,
30853114
isOurs: !remoteChain,
3115+
customBlob: newCommitBlob,
30863116
}
30873117

30883118
// In order to ensure _none_ of the HTLC's associated with this new
@@ -3174,6 +3204,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
31743204
// number of satoshis we've received within the channel.
31753205
if mutateState && entry.EntryType == Settle && !remoteChain &&
31763206
entry.removeCommitHeightLocal == 0 {
3207+
31773208
lc.channelState.TotalMSatReceived += entry.Amount
31783209
}
31793210

@@ -5854,7 +5885,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
58545885
// before the change since the indexes are meant for the current,
58555886
// revoked remote commitment.
58565887
ourOutputIndex, theirOutputIndex, err := findOutputIndexesFromRemote(
5857-
revocation, lc.channelState,
5888+
revocation, lc.channelState, lc.leafStore,
58585889
)
58595890
if err != nil {
58605891
return nil, nil, nil, nil, err
@@ -6736,12 +6767,14 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
67366767

67376768
commitTxBroadcast := commitSpend.SpendingTx
67386769

6770+
// TODO(roasbeef): fetch aux leave
6771+
67396772
// Before we can generate the proper sign descriptor, we'll need to
67406773
// locate the output index of our non-delayed output on the commitment
67416774
// transaction.
67426775
selfScript, maturityDelay, err := CommitScriptToRemote(
67436776
chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
6744-
leaseExpiry,
6777+
leaseExpiry, input.NoneTapLeaf(),
67456778
)
67466779
if err != nil {
67476780
return nil, fmt.Errorf("unable to create self commit "+
@@ -7684,13 +7717,16 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel,
76847717
&chanState.LocalChanCfg, &chanState.RemoteChanCfg,
76857718
)
76867719

7720+
// TODO(roasbeef): fetch aux leave
7721+
76877722
var leaseExpiry uint32
76887723
if chanState.ChanType.HasLeaseExpiration() {
76897724
leaseExpiry = chanState.ThawHeight
76907725
}
76917726
toLocalScript, err := CommitScriptToSelf(
76927727
chanState.ChanType, chanState.IsInitiator, keyRing.ToLocalKey,
76937728
keyRing.RevocationKey, csvTimeout, leaseExpiry,
7729+
input.NoneTapLeaf(),
76947730
)
76957731
if err != nil {
76967732
return nil, err

0 commit comments

Comments
 (0)