Skip to content

Commit 33f2db1

Browse files
committed
multi: thread thru the AuxLeafStore everywhere
1 parent 2ac360a commit 33f2db1

File tree

13 files changed

+150
-42
lines changed

13 files changed

+150
-42
lines changed

chainreg/chainregistry.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightningnetwork/lnd/chainntnfs/neutrinonotify"
2525
"github.com/lightningnetwork/lnd/channeldb"
2626
"github.com/lightningnetwork/lnd/channeldb/models"
27+
"github.com/lightningnetwork/lnd/fn"
2728
"github.com/lightningnetwork/lnd/input"
2829
"github.com/lightningnetwork/lnd/keychain"
2930
"github.com/lightningnetwork/lnd/kvdb"
@@ -63,6 +64,10 @@ type Config struct {
6364
// state.
6465
ChanStateDB *channeldb.ChannelStateDB
6566

67+
// AuxLeafStore is an optional store that can be used to store auxiliary
68+
// leaves for certain custom channel types.
69+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
70+
6671
// BlockCache is the main cache for storing block information.
6772
BlockCache *blockcache.BlockCache
6873

config_builder.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/lightningnetwork/lnd/chainreg"
3535
"github.com/lightningnetwork/lnd/channeldb"
3636
"github.com/lightningnetwork/lnd/clock"
37+
"github.com/lightningnetwork/lnd/fn"
3738
"github.com/lightningnetwork/lnd/invoices"
3839
"github.com/lightningnetwork/lnd/keychain"
3940
"github.com/lightningnetwork/lnd/kvdb"
@@ -104,7 +105,7 @@ type DatabaseBuilder interface {
104105
type WalletConfigBuilder interface {
105106
// BuildWalletConfig is responsible for creating or unlocking and then
106107
// fully initializing a wallet.
107-
BuildWalletConfig(context.Context, *DatabaseInstances,
108+
BuildWalletConfig(context.Context, *DatabaseInstances, *AuxComponents,
108109
*rpcperms.InterceptorChain,
109110
[]*ListenerWithSignal) (*chainreg.PartialChainControl,
110111
*btcwallet.Config, func(), error)
@@ -145,6 +146,17 @@ type ImplementationCfg struct {
145146
// ChainControlBuilder is a type that can provide a custom wallet
146147
// implementation.
147148
ChainControlBuilder
149+
// AuxComponents is a set of auxiliary components that can be used by
150+
// lnd for certain custom channel types.
151+
AuxComponents
152+
}
153+
154+
// AuxComponents is a set of auxiliary components that can be used by lnd for
155+
// certain custom channel types.
156+
type AuxComponents struct {
157+
// AuxLeafStore is an optional data source that can be used by custom
158+
// channels to fetch+store various data.
159+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
148160
}
149161

150162
// DefaultWalletImpl is the default implementation of our normal, btcwallet
@@ -229,7 +241,8 @@ func (d *DefaultWalletImpl) Permissions() map[string][]bakery.Op {
229241
//
230242
// NOTE: This is part of the WalletConfigBuilder interface.
231243
func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
232-
dbs *DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
244+
dbs *DatabaseInstances, aux *AuxComponents,
245+
interceptorChain *rpcperms.InterceptorChain,
233246
grpcListeners []*ListenerWithSignal) (*chainreg.PartialChainControl,
234247
*btcwallet.Config, func(), error) {
235248

@@ -549,6 +562,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
549562
HeightHintDB: dbs.HeightHintDB,
550563
ChanStateDB: dbs.ChanStateDB.ChannelStateDB(),
551564
NeutrinoCS: neutrinoCS,
565+
AuxLeafStore: aux.AuxLeafStore,
552566
ActiveNetParams: d.cfg.ActiveNetParams,
553567
FeeURL: d.cfg.FeeURL,
554568
Dialer: func(addr string) (net.Conn, error) {
@@ -607,8 +621,9 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
607621

608622
// proxyBlockEpoch proxies a block epoch subsections to the underlying neutrino
609623
// rebroadcaster client.
610-
func proxyBlockEpoch(notifier chainntnfs.ChainNotifier,
611-
) func() (*blockntfns.Subscription, error) {
624+
func proxyBlockEpoch(
625+
notifier chainntnfs.ChainNotifier) func() (*blockntfns.Subscription,
626+
error) {
612627

613628
return func() (*blockntfns.Subscription, error) {
614629
blockEpoch, err := notifier.RegisterBlockEpochNtfn(
@@ -699,6 +714,7 @@ func (d *DefaultWalletImpl) BuildChainControl(
699714
ChainIO: walletController,
700715
NetParams: *walletConfig.NetParams,
701716
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
717+
AuxLeafStore: partialChainControl.Cfg.AuxLeafStore,
702718
}
703719

704720
// The broadcast is already always active for neutrino nodes, so we
@@ -878,6 +894,10 @@ type DatabaseInstances struct {
878894
// for native SQL queries for tables that already support it. This may
879895
// be nil if the use-native-sql flag was not set.
880896
NativeSQLStore *sqldb.BaseDB
897+
898+
// AuxLeafStore is an optional data source that can be used by custom
899+
// channels to fetch+store various data.
900+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
881901
}
882902

883903
// DefaultDatabaseBuilder is a type that builds the default database backends

contractcourt/breach_arbitrator_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/go-errors/errors"
2323
"github.com/lightningnetwork/lnd/chainntnfs"
2424
"github.com/lightningnetwork/lnd/channeldb"
25+
"github.com/lightningnetwork/lnd/fn"
2526
"github.com/lightningnetwork/lnd/input"
2627
"github.com/lightningnetwork/lnd/keychain"
2728
"github.com/lightningnetwork/lnd/lntest/channels"
@@ -1590,6 +1591,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
15901591
// Notify the breach arbiter about the breach.
15911592
retribution, err := lnwallet.NewBreachRetribution(
15921593
alice.State(), height, 1, forceCloseTx,
1594+
fn.None[lnwallet.AuxLeafStore](),
15931595
)
15941596
require.NoError(t, err, "unable to create breach retribution")
15951597

@@ -1799,6 +1801,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
17991801
// Notify the breach arbiter about the breach.
18001802
retribution, err := lnwallet.NewBreachRetribution(
18011803
alice.State(), height, uint32(blockHeight), forceCloseTx,
1804+
fn.None[lnwallet.AuxLeafStore](),
18021805
)
18031806
require.NoError(t, err, "unable to create breach retribution")
18041807

contractcourt/chain_arbitrator.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ type ChainArbitratorConfig struct {
217217
// meanwhile, turn `PaymentCircuit` into an interface or bring it to a
218218
// lower package.
219219
QueryIncomingCircuit func(circuit models.CircuitKey) *models.CircuitKey
220+
221+
// AuxLeafStore is an optional store that can be used to store auxiliary
222+
// leaves for certain custom channel types.
223+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
220224
}
221225

222226
// ChainArbitrator is a sub-system that oversees the on-chain resolution of all
@@ -299,8 +303,13 @@ func (a *arbChannel) NewAnchorResolutions() (*lnwallet.AnchorResolutions,
299303
return nil, err
300304
}
301305

306+
var chanOpts []lnwallet.ChannelOpt
307+
a.c.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
308+
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
309+
})
310+
302311
chanMachine, err := lnwallet.NewLightningChannel(
303-
a.c.cfg.Signer, channel, nil,
312+
a.c.cfg.Signer, channel, nil, chanOpts...,
304313
)
305314
if err != nil {
306315
return nil, err
@@ -344,10 +353,15 @@ func (a *arbChannel) ForceCloseChan() (*lnwallet.LocalForceCloseSummary, error)
344353
return nil, err
345354
}
346355

356+
var chanOpts []lnwallet.ChannelOpt
357+
a.c.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
358+
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
359+
})
360+
347361
// Finally, we'll force close the channel completing
348362
// the force close workflow.
349363
chanMachine, err := lnwallet.NewLightningChannel(
350-
a.c.cfg.Signer, channel, nil,
364+
a.c.cfg.Signer, channel, nil, chanOpts...,
351365
)
352366
if err != nil {
353367
return nil, err

contractcourt/chain_watcher.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ type chainWatcherConfig struct {
188188
// obfuscater. This is used by the chain watcher to identify which
189189
// state was broadcast and confirmed on-chain.
190190
extractStateNumHint func(*wire.MsgTx, [lnwallet.StateHintSize]byte) uint64
191+
192+
// auxLeafStore can be used to fetch information for custom channels.
193+
auxLeafStore fn.Option[lnwallet.AuxLeafStore]
191194
}
192195

193196
// chainWatcher is a system that's assigned to every active channel. The duty
@@ -862,7 +865,7 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
862865
spendHeight := uint32(commitSpend.SpendingHeight)
863866
retribution, err := lnwallet.NewBreachRetribution(
864867
c.cfg.chanState, broadcastStateNum, spendHeight,
865-
commitSpend.SpendingTx,
868+
commitSpend.SpendingTx, c.cfg.auxLeafStore,
866869
)
867870

868871
switch {
@@ -1073,8 +1076,8 @@ func (c *chainWatcher) dispatchLocalForceClose(
10731076
"detected", c.cfg.chanState.FundingOutpoint)
10741077

10751078
forceClose, err := lnwallet.NewLocalForceCloseSummary(
1076-
c.cfg.chanState, c.cfg.signer,
1077-
commitSpend.SpendingTx, stateNum,
1079+
c.cfg.chanState, c.cfg.signer, commitSpend.SpendingTx, stateNum,
1080+
c.cfg.auxLeafStore,
10781081
)
10791082
if err != nil {
10801083
return err
@@ -1167,7 +1170,7 @@ func (c *chainWatcher) dispatchRemoteForceClose(
11671170
// channel on-chain.
11681171
uniClose, err := lnwallet.NewUnilateralCloseSummary(
11691172
c.cfg.chanState, c.cfg.signer, commitSpend,
1170-
remoteCommit, commitPoint,
1173+
remoteCommit, commitPoint, c.cfg.auxLeafStore,
11711174
)
11721175
if err != nil {
11731176
return err

funding/manager.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ type Config struct {
538538
// AliasManager is an implementation of the aliasHandler interface that
539539
// abstracts away the handling of many alias functions.
540540
AliasManager aliasHandler
541+
542+
// AuxLeafStore is an optional store that can be used to store auxiliary
543+
// leaves for certain custom channel types.
544+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
541545
}
542546

543547
// Manager acts as an orchestrator/bridge between the wallet's
@@ -1056,9 +1060,14 @@ func (f *Manager) advanceFundingState(channel *channeldb.OpenChannel,
10561060
}
10571061
}
10581062

1063+
var chanOpts []lnwallet.ChannelOpt
1064+
f.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
1065+
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
1066+
})
1067+
10591068
// We create the state-machine object which wraps the database state.
10601069
lnChannel, err := lnwallet.NewLightningChannel(
1061-
nil, channel, nil,
1070+
nil, channel, nil, chanOpts...,
10621071
)
10631072
if err != nil {
10641073
log.Errorf("Unable to create LightningChannel(%v): %v",

lnd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
437437
defer cleanUp()
438438

439439
partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
440-
ctx, dbs, interceptorChain, grpcListeners,
440+
ctx, dbs, &implCfg.AuxComponents, interceptorChain,
441+
grpcListeners,
441442
)
442443
if err != nil {
443444
return mkErr("error creating wallet config: %v", err)
@@ -580,7 +581,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
580581
server, err := newServer(
581582
cfg, cfg.Listeners, dbs, activeChainControl, &idKeyDesc,
582583
activeChainControl.Cfg.WalletUnlockParams.ChansToRestore,
583-
multiAcceptor, torController, tlsManager,
584+
multiAcceptor, torController, tlsManager, implCfg,
584585
)
585586
if err != nil {
586587
return mkErr("unable to create server: %v", err)

lnwallet/channel.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,8 @@ type BreachRetribution struct {
24622462
// required to construct the BreachRetribution. If the revocation log is missing
24632463
// the required fields then ErrRevLogDataMissing will be returned.
24642464
func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
2465-
breachHeight uint32, spendTx *wire.MsgTx) (*BreachRetribution, error) {
2465+
breachHeight uint32, spendTx *wire.MsgTx,
2466+
leafStore fn.Option[AuxLeafStore]) (*BreachRetribution, error) {
24662467

24672468
// Query the on-disk revocation log for the snapshot which was recorded
24682469
// at this particular state num. Based on whether a legacy revocation
@@ -3493,9 +3494,16 @@ func processFeeUpdate(feeUpdate *PaymentDescriptor, nextHeight uint64,
34933494
// signature can be submitted to the sigPool to generate all the signatures
34943495
// asynchronously and in parallel.
34953496
func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing,
3496-
chanType channeldb.ChannelType, isRemoteInitiator bool,
3497-
leaseExpiry uint32, localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
3498-
remoteCommitView *commitment) ([]SignJob, chan struct{}, error) {
3497+
chanState *channeldb.OpenChannel, leaseExpiry uint32,
3498+
remoteCommitView *commitment,
3499+
leafStore fn.Option[AuxLeafStore]) ([]SignJob, chan struct{}, error) {
3500+
3501+
var (
3502+
isRemoteInitiator = !chanState.IsInitiator
3503+
localChanCfg = chanState.LocalChanCfg
3504+
remoteChanCfg = chanState.RemoteChanCfg
3505+
chanType = chanState.ChanType
3506+
)
34993507

35003508
txHash := remoteCommitView.txn.TxHash()
35013509
dustLimit := remoteChanCfg.DustLimit
@@ -3661,9 +3669,9 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing,
36613669
// validate this new state. This function is called right before sending the
36623670
// new commitment to the remote party. The commit diff returned contains all
36633671
// information necessary for retransmission.
3664-
func (lc *LightningChannel) createCommitDiff(
3665-
newCommit *commitment, commitSig lnwire.Sig,
3666-
htlcSigs []lnwire.Sig) (*channeldb.CommitDiff, error) {
3672+
func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
3673+
commitSig lnwire.Sig, htlcSigs []lnwire.Sig) (*channeldb.CommitDiff,
3674+
error) {
36673675

36683676
// First, we need to convert the funding outpoint into the ID that's
36693677
// used on the wire to identify this channel. We'll use this shortly
@@ -4362,9 +4370,8 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
43624370
leaseExpiry = lc.channelState.ThawHeight
43634371
}
43644372
sigBatch, cancelChan, err := genRemoteHtlcSigJobs(
4365-
keyRing, lc.channelState.ChanType, !lc.channelState.IsInitiator,
4366-
leaseExpiry, &lc.channelState.LocalChanCfg,
4367-
&lc.channelState.RemoteChanCfg, newCommitView,
4373+
keyRing, lc.channelState, leaseExpiry, newCommitView,
4374+
lc.leafStore,
43684375
)
43694376
if err != nil {
43704377
return nil, err
@@ -4927,10 +4934,18 @@ func (lc *LightningChannel) computeView(view *HtlcView, remoteChain bool,
49274934
// meant to verify all the signatures for HTLC's attached to a newly created
49284935
// commitment state. The jobs generated are fully populated, and can be sent
49294936
// directly into the pool of workers.
4930-
func genHtlcSigValidationJobs(localCommitmentView *commitment,
4931-
keyRing *CommitmentKeyRing, htlcSigs []lnwire.Sig,
4932-
chanType channeldb.ChannelType, isLocalInitiator bool, leaseExpiry uint32,
4933-
localChanCfg, remoteChanCfg *channeldb.ChannelConfig) ([]VerifyJob, error) {
4937+
//
4938+
//nolint:funlen
4939+
func genHtlcSigValidationJobs(chanState *channeldb.OpenChannel,
4940+
localCommitmentView *commitment, keyRing *CommitmentKeyRing,
4941+
htlcSigs []lnwire.Sig, leaseExpiry uint32,
4942+
leafStore fn.Option[AuxLeafStore]) ([]VerifyJob, error) {
4943+
4944+
var (
4945+
isLocalInitiator = chanState.IsInitiator
4946+
localChanCfg = chanState.LocalChanCfg
4947+
chanType = chanState.ChanType
4948+
)
49344949

49354950
txHash := localCommitmentView.txn.TxHash()
49364951
feePerKw := localCommitmentView.feePerKw
@@ -5323,10 +5338,8 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
53235338
leaseExpiry = lc.channelState.ThawHeight
53245339
}
53255340
verifyJobs, err := genHtlcSigValidationJobs(
5326-
localCommitmentView, keyRing, commitSigs.HtlcSigs,
5327-
lc.channelState.ChanType, lc.channelState.IsInitiator,
5328-
leaseExpiry, &lc.channelState.LocalChanCfg,
5329-
&lc.channelState.RemoteChanCfg,
5341+
lc.channelState, localCommitmentView, keyRing,
5342+
commitSigs.HtlcSigs, leaseExpiry, lc.leafStore,
53305343
)
53315344
if err != nil {
53325345
return err
@@ -6773,10 +6786,10 @@ type UnilateralCloseSummary struct {
67736786
// happen in case we have lost state) it should be set to an empty struct, in
67746787
// which case we will attempt to sweep the non-HTLC output using the passed
67756788
// commitPoint.
6776-
func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer input.Signer,
6777-
commitSpend *chainntnfs.SpendDetail,
6778-
remoteCommit channeldb.ChannelCommitment,
6779-
commitPoint *btcec.PublicKey) (*UnilateralCloseSummary, error) {
6789+
func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel,
6790+
signer input.Signer, commitSpend *chainntnfs.SpendDetail,
6791+
remoteCommit channeldb.ChannelCommitment, commitPoint *btcec.PublicKey,
6792+
leafStore fn.Option[AuxLeafStore]) (*UnilateralCloseSummary, error) {
67806793

67816794
// First, we'll generate the commitment point and the revocation point
67826795
// so we can re-construct the HTLC state and also our payment key.
@@ -7717,7 +7730,7 @@ func (lc *LightningChannel) ForceClose() (*LocalForceCloseSummary, error) {
77177730
localCommitment := lc.channelState.LocalCommitment
77187731
summary, err := NewLocalForceCloseSummary(
77197732
lc.channelState, lc.Signer, commitTx,
7720-
localCommitment.CommitHeight,
7733+
localCommitment.CommitHeight, lc.leafStore,
77217734
)
77227735
if err != nil {
77237736
return nil, fmt.Errorf("unable to gen force close "+
@@ -7734,8 +7747,8 @@ func (lc *LightningChannel) ForceClose() (*LocalForceCloseSummary, error) {
77347747
// channel state. The passed commitTx must be a fully signed commitment
77357748
// transaction corresponding to localCommit.
77367749
func NewLocalForceCloseSummary(chanState *channeldb.OpenChannel,
7737-
signer input.Signer, commitTx *wire.MsgTx, stateNum uint64) (
7738-
*LocalForceCloseSummary, error) {
7750+
signer input.Signer, commitTx *wire.MsgTx, stateNum uint64,
7751+
leafStore fn.Option[AuxLeafStore]) (*LocalForceCloseSummary, error) {
77397752

77407753
// Re-derive the original pkScript for to-self output within the
77417754
// commitment transaction. We'll need this to find the corresponding

0 commit comments

Comments
 (0)