Skip to content

Commit b45d72f

Browse files
committed
multi: thread thru the AuxLeafStore everywhere
1 parent 2510c19 commit b45d72f

File tree

15 files changed

+194
-47
lines changed

15 files changed

+194
-47
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type DatabaseBuilder interface {
105105
type WalletConfigBuilder interface {
106106
// BuildWalletConfig is responsible for creating or unlocking and then
107107
// fully initializing a wallet.
108-
BuildWalletConfig(context.Context, *DatabaseInstances,
108+
BuildWalletConfig(context.Context, *DatabaseInstances, *AuxComponents,
109109
*rpcperms.InterceptorChain,
110110
[]*ListenerWithSignal) (*chainreg.PartialChainControl,
111111
*btcwallet.Config, func(), error)
@@ -120,14 +120,6 @@ type ChainControlBuilder interface {
120120
*btcwallet.Config) (*chainreg.ChainControl, func(), error)
121121
}
122122

123-
// AuxComponents is a set of auxiliary components that can be used by lnd for
124-
// certain custom channel types.
125-
type AuxComponents struct {
126-
// MsgRouter is an optional message router that if set will be used in
127-
// place of a new blank default message router.
128-
MsgRouter fn.Option[msgmux.Router]
129-
}
130-
131123
// ImplementationCfg is a struct that holds all configuration items for
132124
// components that can be implemented outside lnd itself.
133125
type ImplementationCfg struct {
@@ -160,6 +152,18 @@ type ImplementationCfg struct {
160152
AuxComponents
161153
}
162154

155+
// AuxComponents is a set of auxiliary components that can be used by lnd for
156+
// certain custom channel types.
157+
type AuxComponents struct {
158+
// AuxLeafStore is an optional data source that can be used by custom
159+
// channels to fetch+store various data.
160+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
161+
162+
// MsgRouter is an optional message router that if set will be used in
163+
// place of a new blank default message router.
164+
MsgRouter fn.Option[msgmux.Router]
165+
}
166+
163167
// DefaultWalletImpl is the default implementation of our normal, btcwallet
164168
// backed configuration.
165169
type DefaultWalletImpl struct {
@@ -242,7 +246,8 @@ func (d *DefaultWalletImpl) Permissions() map[string][]bakery.Op {
242246
//
243247
// NOTE: This is part of the WalletConfigBuilder interface.
244248
func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
245-
dbs *DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
249+
dbs *DatabaseInstances, aux *AuxComponents,
250+
interceptorChain *rpcperms.InterceptorChain,
246251
grpcListeners []*ListenerWithSignal) (*chainreg.PartialChainControl,
247252
*btcwallet.Config, func(), error) {
248253

@@ -562,6 +567,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
562567
HeightHintDB: dbs.HeightHintDB,
563568
ChanStateDB: dbs.ChanStateDB.ChannelStateDB(),
564569
NeutrinoCS: neutrinoCS,
570+
AuxLeafStore: aux.AuxLeafStore,
565571
ActiveNetParams: d.cfg.ActiveNetParams,
566572
FeeURL: d.cfg.FeeURL,
567573
Fee: &lncfg.Fee{
@@ -625,8 +631,9 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
625631

626632
// proxyBlockEpoch proxies a block epoch subsections to the underlying neutrino
627633
// rebroadcaster client.
628-
func proxyBlockEpoch(notifier chainntnfs.ChainNotifier,
629-
) func() (*blockntfns.Subscription, error) {
634+
func proxyBlockEpoch(
635+
notifier chainntnfs.ChainNotifier) func() (*blockntfns.Subscription,
636+
error) {
630637

631638
return func() (*blockntfns.Subscription, error) {
632639
blockEpoch, err := notifier.RegisterBlockEpochNtfn(
@@ -717,6 +724,7 @@ func (d *DefaultWalletImpl) BuildChainControl(
717724
ChainIO: walletController,
718725
NetParams: *walletConfig.NetParams,
719726
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
727+
AuxLeafStore: partialChainControl.Cfg.AuxLeafStore,
720728
}
721729

722730
// The broadcast is already always active for neutrino nodes, so we
@@ -899,6 +907,10 @@ type DatabaseInstances struct {
899907
// for native SQL queries for tables that already support it. This may
900908
// be nil if the use-native-sql flag was not set.
901909
NativeSQLStore *sqldb.BaseDB
910+
911+
// AuxLeafStore is an optional data source that can be used by custom
912+
// channels to fetch+store various data.
913+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
902914
}
903915

904916
// 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.Some[lnwallet.AuxLeafStore](&lnwallet.MockAuxLeafStore{}),
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.Some[lnwallet.AuxLeafStore](&lnwallet.MockAuxLeafStore{}),
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
@@ -193,6 +193,9 @@ type chainWatcherConfig struct {
193193
// obfuscater. This is used by the chain watcher to identify which
194194
// state was broadcast and confirmed on-chain.
195195
extractStateNumHint func(*wire.MsgTx, [lnwallet.StateHintSize]byte) uint64
196+
197+
// auxLeafStore can be used to fetch information for custom channels.
198+
auxLeafStore fn.Option[lnwallet.AuxLeafStore]
196199
}
197200

198201
// chainWatcher is a system that's assigned to every active channel. The duty
@@ -867,7 +870,7 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
867870
spendHeight := uint32(commitSpend.SpendingHeight)
868871
retribution, err := lnwallet.NewBreachRetribution(
869872
c.cfg.chanState, broadcastStateNum, spendHeight,
870-
commitSpend.SpendingTx,
873+
commitSpend.SpendingTx, c.cfg.auxLeafStore,
871874
)
872875

873876
switch {
@@ -1117,8 +1120,8 @@ func (c *chainWatcher) dispatchLocalForceClose(
11171120
"detected", c.cfg.chanState.FundingOutpoint)
11181121

11191122
forceClose, err := lnwallet.NewLocalForceCloseSummary(
1120-
c.cfg.chanState, c.cfg.signer,
1121-
commitSpend.SpendingTx, stateNum,
1123+
c.cfg.chanState, c.cfg.signer, commitSpend.SpendingTx, stateNum,
1124+
c.cfg.auxLeafStore,
11221125
)
11231126
if err != nil {
11241127
return err
@@ -1211,7 +1214,7 @@ func (c *chainWatcher) dispatchRemoteForceClose(
12111214
// channel on-chain.
12121215
uniClose, err := lnwallet.NewUnilateralCloseSummary(
12131216
c.cfg.chanState, c.cfg.signer, commitSpend,
1214-
remoteCommit, commitPoint,
1217+
remoteCommit, commitPoint, c.cfg.auxLeafStore,
12151218
)
12161219
if err != nil {
12171220
return err

funding/manager.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightningnetwork/lnd/channeldb"
2525
"github.com/lightningnetwork/lnd/channeldb/models"
2626
"github.com/lightningnetwork/lnd/discovery"
27+
"github.com/lightningnetwork/lnd/fn"
2728
"github.com/lightningnetwork/lnd/graph"
2829
"github.com/lightningnetwork/lnd/input"
2930
"github.com/lightningnetwork/lnd/keychain"
@@ -544,6 +545,10 @@ type Config struct {
544545
// backed funding flow to not use utxos still being swept by the sweeper
545546
// subsystem.
546547
IsSweeperOutpoint func(wire.OutPoint) bool
548+
549+
// AuxLeafStore is an optional store that can be used to store auxiliary
550+
// leaves for certain custom channel types.
551+
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
547552
}
548553

549554
// Manager acts as an orchestrator/bridge between the wallet's
@@ -1069,9 +1074,14 @@ func (f *Manager) advanceFundingState(channel *channeldb.OpenChannel,
10691074
}
10701075
}
10711076

1077+
var chanOpts []lnwallet.ChannelOpt
1078+
f.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
1079+
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
1080+
})
1081+
10721082
// We create the state-machine object which wraps the database state.
10731083
lnChannel, err := lnwallet.NewLightningChannel(
1074-
nil, channel, nil,
1084+
nil, channel, nil, chanOpts...,
10751085
)
10761086
if err != nil {
10771087
log.Errorf("Unable to create LightningChannel(%v): %v",

funding/manager_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/lightningnetwork/lnd/channeldb/models"
2929
"github.com/lightningnetwork/lnd/channelnotifier"
3030
"github.com/lightningnetwork/lnd/discovery"
31+
"github.com/lightningnetwork/lnd/fn"
3132
"github.com/lightningnetwork/lnd/input"
3233
"github.com/lightningnetwork/lnd/keychain"
3334
"github.com/lightningnetwork/lnd/lncfg"
@@ -563,6 +564,9 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
563564
IsSweeperOutpoint: func(wire.OutPoint) bool {
564565
return false
565566
},
567+
AuxLeafStore: fn.Some[lnwallet.AuxLeafStore](
568+
&lnwallet.MockAuxLeafStore{},
569+
),
566570
}
567571

568572
for _, op := range options {
@@ -672,6 +676,7 @@ func recreateAliceFundingManager(t *testing.T, alice *testNode) {
672676
OpenChannelPredicate: chainedAcceptor,
673677
DeleteAliasEdge: oldCfg.DeleteAliasEdge,
674678
AliasManager: oldCfg.AliasManager,
679+
AuxLeafStore: oldCfg.AuxLeafStore,
675680
})
676681
require.NoError(t, err, "failed recreating aliceFundingManager")
677682

lnd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
456456
defer cleanUp()
457457

458458
partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
459-
ctx, dbs, interceptorChain, grpcListeners,
459+
ctx, dbs, &implCfg.AuxComponents, interceptorChain,
460+
grpcListeners,
460461
)
461462
if err != nil {
462463
return mkErr("error creating wallet config: %v", err)

0 commit comments

Comments
 (0)