Skip to content

Commit 59f73c7

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 9cc8fa4 commit 59f73c7

File tree

5 files changed

+300
-35
lines changed

5 files changed

+300
-35
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
@@ -1268,6 +1268,10 @@ type LightningChannel struct {
12681268
// machine.
12691269
Signer input.Signer
12701270

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

1350+
leafStore fn.Option[AuxLeafStore]
1351+
13461352
skipNonceInit bool
13471353
}
13481354

@@ -1373,6 +1379,13 @@ func WithSkipNonceInit() ChannelOpt {
13731379
}
13741380
}
13751381

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

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

2482+
// TODO(roasbeef): fetch aux leave
2483+
24662484
// Since it is the remote breach we are reconstructing, the output
24672485
// going to us will be a to-remote script with our local params.
24682486
isRemoteInitiator := !chanState.IsInitiator
24692487
ourScript, ourDelay, err := CommitScriptToRemote(
24702488
chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
2471-
leaseExpiry,
2489+
leaseExpiry, input.NoneTapLeaf(),
24722490
)
24732491
if err != nil {
24742492
return nil, err
@@ -2478,6 +2496,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
24782496
theirScript, err := CommitScriptToSelf(
24792497
chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
24802498
keyRing.RevocationKey, theirDelay, leaseExpiry,
2499+
input.NoneTapLeaf(),
24812500
)
24822501
if err != nil {
24832502
return nil, err
@@ -3025,7 +3044,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30253044
// Actually generate unsigned commitment transaction for this view.
30263045
commitTx, err := lc.commitBuilder.createUnsignedCommitmentTx(
30273046
ourBalance, theirBalance, !remoteChain, feePerKw, nextHeight,
3028-
filteredHTLCView, keyRing,
3047+
htlcView, filteredHTLCView, keyRing, commitChain.tip(),
30293048
)
30303049
if err != nil {
30313050
return nil, err
@@ -3060,6 +3079,16 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30603079
effFeeRate, spew.Sdump(commitTx))
30613080
}
30623081

3082+
// Given the custom blob of the past state, and this new HTLC view,
3083+
// we'll generate a new blob for the latest commitment.
3084+
newCommitBlob, err := updateAuxBlob(
3085+
lc.channelState, commitChain.tip().customBlob, htlcView,
3086+
!remoteChain, ourBalance, theirBalance, lc.leafStore, *keyRing,
3087+
)
3088+
if err != nil {
3089+
return nil, fmt.Errorf("unable to fetch aux leaves: %w", err)
3090+
}
3091+
30633092
// With the commitment view created, store the resulting balances and
30643093
// transaction with the other parameters for this height.
30653094
c := &commitment{
@@ -3075,6 +3104,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30753104
feePerKw: feePerKw,
30763105
dustLimit: dustLimit,
30773106
isOurs: !remoteChain,
3107+
customBlob: newCommitBlob,
30783108
}
30793109

30803110
// In order to ensure _none_ of the HTLC's associated with this new
@@ -3165,6 +3195,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
31653195
// number of satoshis we've received within the channel.
31663196
if mutateState && entry.EntryType == Settle && !remoteChain &&
31673197
entry.removeCommitHeightLocal == 0 {
3198+
31683199
lc.channelState.TotalMSatReceived += entry.Amount
31693200
}
31703201

@@ -5845,7 +5876,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
58455876
// before the change since the indexes are meant for the current,
58465877
// revoked remote commitment.
58475878
ourOutputIndex, theirOutputIndex, err := findOutputIndexesFromRemote(
5848-
revocation, lc.channelState,
5879+
revocation, lc.channelState, lc.leafStore,
58495880
)
58505881
if err != nil {
58515882
return nil, nil, nil, nil, err
@@ -6727,12 +6758,14 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Si
67276758

67286759
commitTxBroadcast := commitSpend.SpendingTx
67296760

6761+
// TODO(roasbeef): fetch aux leave
6762+
67306763
// Before we can generate the proper sign descriptor, we'll need to
67316764
// locate the output index of our non-delayed output on the commitment
67326765
// transaction.
67336766
selfScript, maturityDelay, err := CommitScriptToRemote(
67346767
chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
6735-
leaseExpiry,
6768+
leaseExpiry, input.NoneTapLeaf(),
67366769
)
67376770
if err != nil {
67386771
return nil, fmt.Errorf("unable to create self commit "+
@@ -7675,13 +7708,16 @@ func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel,
76757708
&chanState.LocalChanCfg, &chanState.RemoteChanCfg,
76767709
)
76777710

7711+
// TODO(roasbeef): fetch aux leave
7712+
76787713
var leaseExpiry uint32
76797714
if chanState.ChanType.HasLeaseExpiration() {
76807715
leaseExpiry = chanState.ThawHeight
76817716
}
76827717
toLocalScript, err := CommitScriptToSelf(
76837718
chanState.ChanType, chanState.IsInitiator, keyRing.ToLocalKey,
76847719
keyRing.RevocationKey, csvTimeout, leaseExpiry,
7720+
input.NoneTapLeaf(),
76857721
)
76867722
if err != nil {
76877723
return nil, err

0 commit comments

Comments
 (0)