Skip to content

Commit c912e82

Browse files
committed
server+tapchannel: make aux leaf creator stateless
This fixes a startup dependency issue between lnd and tapd where both wait for each other to become ready. Because the aux leaf creator doesn't depend on any other components that need to be started first, and also doesn't carry any state by itself, we can declare it to be fully stateless and not block on server startup before being able to use it.
1 parent 58d5508 commit c912e82

File tree

4 files changed

+33
-109
lines changed

4 files changed

+33
-109
lines changed

config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ type Config struct {
197197

198198
UniverseStats universe.Telemetry
199199

200-
AuxLeafCreator *tapchannel.AuxLeafCreator
201-
202200
AuxLeafSigner *tapchannel.AuxLeafSigner
203201

204202
AuxFundingController *tapchannel.FundingController

server.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,7 @@ func (s *Server) initialize(interceptorChain *rpcperms.InterceptorChain) error {
196196
return fmt.Errorf("unable to start RFQ manager: %w", err)
197197
}
198198

199-
// Start the auxiliary leaf creator and signer.
200-
if err := s.cfg.AuxLeafCreator.Start(); err != nil {
201-
return fmt.Errorf("unable to start aux leaf creator: %w", err)
202-
}
199+
// Start the auxiliary components.
203200
if err := s.cfg.AuxLeafSigner.Start(); err != nil {
204201
return fmt.Errorf("unable to start aux leaf signer: %w", err)
205202
}
@@ -657,9 +654,6 @@ func (s *Server) Stop() error {
657654
return err
658655
}
659656

660-
if err := s.cfg.AuxLeafCreator.Stop(); err != nil {
661-
return err
662-
}
663657
if err := s.cfg.AuxLeafSigner.Stop(); err != nil {
664658
return err
665659
}
@@ -733,13 +727,11 @@ func (s *Server) FetchLeavesFromView(chanState *channeldb.OpenChannel,
733727
"numTheirUpdates=%d", isOurCommit, ourBalance, theirBalance,
734728
len(view.OurUpdates), len(view.TheirUpdates))
735729

736-
if err := s.waitForReady(); err != nil {
737-
return lfn.None[lnwallet.CommitAuxLeaves](), nil, err
738-
}
739-
740-
return s.cfg.AuxLeafCreator.FetchLeavesFromView(
741-
chanState, prevBlob, view, isOurCommit, ourBalance,
742-
theirBalance, keys,
730+
// The aux leaf creator is fully stateless, and we don't need to wait
731+
// for the server to be started before being able to use it.
732+
return tapchannel.FetchLeavesFromView(
733+
s.chainParams, chanState, prevBlob, view, isOurCommit,
734+
ourBalance, theirBalance, keys,
743735
)
744736
}
745737

@@ -757,11 +749,11 @@ func (s *Server) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
757749
"theirBalance=%v, numHtlcs=%d", com.LocalBalance,
758750
com.RemoteBalance, len(com.Htlcs))
759751

760-
if err := s.waitForReady(); err != nil {
761-
return lfn.None[lnwallet.CommitAuxLeaves](), err
762-
}
763-
764-
return s.cfg.AuxLeafCreator.FetchLeavesFromCommit(chanState, com, keys)
752+
// The aux leaf creator is fully stateless, and we don't need to wait
753+
// for the server to be started before being able to use it.
754+
return tapchannel.FetchLeavesFromCommit(
755+
s.chainParams, chanState, com, keys,
756+
)
765757
}
766758

767759
// FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
@@ -776,11 +768,9 @@ func (s *Server) FetchLeavesFromRevocation(
776768
"teirBalance=%v, numHtlcs=%d", rev.OurBalance, rev.TheirBalance,
777769
len(rev.HTLCEntries))
778770

779-
if err := s.waitForReady(); err != nil {
780-
return lfn.None[lnwallet.CommitAuxLeaves](), err
781-
}
782-
783-
return s.cfg.AuxLeafCreator.FetchLeavesFromRevocation(rev)
771+
// The aux leaf creator is fully stateless, and we don't need to wait
772+
// for the server to be started before being able to use it.
773+
return tapchannel.FetchLeavesFromRevocation(rev)
784774
}
785775

786776
// ApplyHtlcView serves as the state transition function for the custom
@@ -798,13 +788,11 @@ func (s *Server) ApplyHtlcView(chanState *channeldb.OpenChannel,
798788
"numTheirUpdates=%d", isOurCommit, ourBalance, theirBalance,
799789
len(originalView.OurUpdates), len(originalView.TheirUpdates))
800790

801-
if err := s.waitForReady(); err != nil {
802-
return lfn.None[tlv.Blob](), err
803-
}
804-
805-
return s.cfg.AuxLeafCreator.ApplyHtlcView(
806-
chanState, prevBlob, originalView, isOurCommit, ourBalance,
807-
theirBalance, keys,
791+
// The aux leaf creator is fully stateless, and we don't need to wait
792+
// for the server to be started before being able to use it.
793+
return tapchannel.ApplyHtlcView(
794+
s.chainParams, chanState, prevBlob, originalView, isOurCommit,
795+
ourBalance, theirBalance, keys,
808796
)
809797
}
810798

tapcfg/server.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,6 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
406406
},
407407
)
408408

409-
auxLeafCreator := tapchannel.NewAuxLeafCreator(
410-
&tapchannel.LeafCreatorConfig{
411-
ChainParams: &tapChainParams,
412-
},
413-
)
414409
auxLeafSigner := tapchannel.NewAuxLeafSigner(
415410
&tapchannel.LeafSignerConfig{
416411
ChainParams: &tapChainParams,
@@ -554,7 +549,6 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
554549
UniverseQueriesPerSecond: cfg.Universe.UniverseQueriesPerSecond,
555550
UniverseQueriesBurst: cfg.Universe.UniverseQueriesBurst,
556551
RfqManager: rfqManager,
557-
AuxLeafCreator: auxLeafCreator,
558552
AuxLeafSigner: auxLeafSigner,
559553
AuxFundingController: auxFundingController,
560554
AuxChanCloser: auxChanCloser,

tapchannel/aux_leaf_creator.go

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package tapchannel
33
import (
44
"bytes"
55
"fmt"
6-
"sync"
76
"time"
87

98
"github.com/btcsuite/btcd/txscript"
109
"github.com/btcsuite/btcd/wire"
1110
"github.com/lightninglabs/taproot-assets/address"
12-
"github.com/lightninglabs/taproot-assets/fn"
1311
cmsg "github.com/lightninglabs/taproot-assets/tapchannelmsg"
1412
"github.com/lightningnetwork/lnd/channeldb"
1513
lfn "github.com/lightningnetwork/lnd/fn"
@@ -25,67 +23,12 @@ const (
2523
DefaultTimeout = 30 * time.Second
2624
)
2725

28-
// LeafCreatorConfig defines the configuration for the auxiliary leaf creator.
29-
type LeafCreatorConfig struct {
30-
ChainParams *address.ChainParams
31-
}
32-
33-
// AuxLeafCreator is a Taproot Asset auxiliary leaf creator that can be used to
34-
// create auxiliary leaves for Taproot Asset channels.
35-
type AuxLeafCreator struct {
36-
startOnce sync.Once
37-
stopOnce sync.Once
38-
39-
cfg *LeafCreatorConfig
40-
41-
// ContextGuard provides a wait group and main quit channel that can be
42-
// used to create guarded contexts.
43-
*fn.ContextGuard
44-
}
45-
46-
// NewAuxLeafCreator creates a new Taproot Asset auxiliary leaf creator based on
47-
// the passed config.
48-
func NewAuxLeafCreator(cfg *LeafCreatorConfig) *AuxLeafCreator {
49-
return &AuxLeafCreator{
50-
cfg: cfg,
51-
ContextGuard: &fn.ContextGuard{
52-
DefaultTimeout: DefaultTimeout,
53-
Quit: make(chan struct{}),
54-
},
55-
}
56-
}
57-
58-
// Start attempts to start a new aux leaf creator.
59-
func (c *AuxLeafCreator) Start() error {
60-
var startErr error
61-
c.startOnce.Do(func() {
62-
log.Info("Starting aux leaf creator")
63-
})
64-
return startErr
65-
}
66-
67-
// Stop signals for a custodian to gracefully exit.
68-
func (c *AuxLeafCreator) Stop() error {
69-
var stopErr error
70-
c.stopOnce.Do(func() {
71-
log.Info("Stopping aux leaf creator")
72-
73-
close(c.Quit)
74-
c.Wg.Wait()
75-
})
76-
77-
return stopErr
78-
}
79-
80-
// A compile-time check to ensure that AuxLeafCreator fully implements the
81-
// lnwallet.AuxLeafStore interface.
82-
var _ lnwallet.AuxLeafStore = (*AuxLeafCreator)(nil)
83-
8426
// FetchLeavesFromView attempts to fetch the auxiliary leaves that correspond to
8527
// the passed aux blob, and pending fully evaluated HTLC view.
86-
func (c *AuxLeafCreator) FetchLeavesFromView(chanState *channeldb.OpenChannel,
87-
prevBlob tlv.Blob, originalView *lnwallet.HtlcView, isOurCommit bool,
88-
ourBalance, theirBalance lnwire.MilliSatoshi,
28+
func FetchLeavesFromView(chainParams *address.ChainParams,
29+
chanState *channeldb.OpenChannel, prevBlob tlv.Blob,
30+
originalView *lnwallet.HtlcView, isOurCommit bool, ourBalance,
31+
theirBalance lnwire.MilliSatoshi,
8932
keys lnwallet.CommitmentKeyRing) (lfn.Option[lnwallet.CommitAuxLeaves],
9033
lnwallet.CommitSortFunc, error) {
9134

@@ -112,7 +55,7 @@ func (c *AuxLeafCreator) FetchLeavesFromView(chanState *channeldb.OpenChannel,
11255

11356
allocations, newCommitment, err := GenerateCommitmentAllocations(
11457
prevState, chanState, chanAssetState, isOurCommit, ourBalance,
115-
theirBalance, originalView, c.cfg.ChainParams, keys,
58+
theirBalance, originalView, chainParams, keys,
11659
)
11760
if err != nil {
11861
return none, nil, fmt.Errorf("unable to generate allocations: "+
@@ -132,8 +75,8 @@ func (c *AuxLeafCreator) FetchLeavesFromView(chanState *channeldb.OpenChannel,
13275

13376
// FetchLeavesFromCommit attempts to fetch the auxiliary leaves that correspond
13477
// to the passed aux blob, and an existing channel commitment.
135-
func (c *AuxLeafCreator) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
136-
com channeldb.ChannelCommitment,
78+
func FetchLeavesFromCommit(chainParams *address.ChainParams,
79+
chanState *channeldb.OpenChannel, com channeldb.ChannelCommitment,
13780
keys lnwallet.CommitmentKeyRing) (lfn.Option[lnwallet.CommitAuxLeaves],
13881
error) {
13982

@@ -173,7 +116,7 @@ func (c *AuxLeafCreator) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
173116

174117
leaf, err := CreateSecondLevelHtlcTx(
175118
chanState, com.CommitTx, htlc.Amt.ToSatoshis(),
176-
keys, c.cfg.ChainParams, htlcOutputs,
119+
keys, chainParams, htlcOutputs,
177120
)
178121
if err != nil {
179122
return none, fmt.Errorf("unable to create "+
@@ -204,7 +147,7 @@ func (c *AuxLeafCreator) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
204147

205148
leaf, err := CreateSecondLevelHtlcTx(
206149
chanState, com.CommitTx, htlc.Amt.ToSatoshis(),
207-
keys, c.cfg.ChainParams, htlcOutputs,
150+
keys, chainParams, htlcOutputs,
208151
)
209152
if err != nil {
210153
return none, fmt.Errorf("unable to create "+
@@ -231,7 +174,7 @@ func (c *AuxLeafCreator) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
231174

232175
// FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
233176
// from a channel revocation that stores balance + blob information.
234-
func (c *AuxLeafCreator) FetchLeavesFromRevocation(
177+
func FetchLeavesFromRevocation(
235178
rev *channeldb.RevocationLog) (lfn.Option[lnwallet.CommitAuxLeaves],
236179
error) {
237180

@@ -255,8 +198,9 @@ func (c *AuxLeafCreator) FetchLeavesFromRevocation(
255198
// ApplyHtlcView serves as the state transition function for the custom
256199
// channel's blob. Given the old blob, and an HTLC view, then a new
257200
// blob should be returned that reflects the pending updates.
258-
func (c *AuxLeafCreator) ApplyHtlcView(chanState *channeldb.OpenChannel,
259-
prevBlob tlv.Blob, originalView *lnwallet.HtlcView, isOurCommit bool,
201+
func ApplyHtlcView(chainParams *address.ChainParams,
202+
chanState *channeldb.OpenChannel, prevBlob tlv.Blob,
203+
originalView *lnwallet.HtlcView, isOurCommit bool,
260204
ourBalance, theirBalance lnwire.MilliSatoshi,
261205
keys lnwallet.CommitmentKeyRing) (lfn.Option[tlv.Blob], error) {
262206

@@ -283,7 +227,7 @@ func (c *AuxLeafCreator) ApplyHtlcView(chanState *channeldb.OpenChannel,
283227

284228
_, newCommitment, err := GenerateCommitmentAllocations(
285229
prevState, chanState, chanAssetState, isOurCommit, ourBalance,
286-
theirBalance, originalView, c.cfg.ChainParams, keys,
230+
theirBalance, originalView, chainParams, keys,
287231
)
288232
if err != nil {
289233
return none, fmt.Errorf("unable to generate allocations: %w",

0 commit comments

Comments
 (0)