Skip to content

Commit 01be6ee

Browse files
committed
server+tapchannel: make aux signer partially stateless
We allow any stateless methods of the aux leaf signer to be called without the server needing to be fully started, so reduce the number of potential startup wait dependencies there could be.
1 parent c912e82 commit 01be6ee

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

server.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,9 @@ func (s *Server) PackSigs(
863863

864864
srvrLog.Debugf("PackSigs called")
865865

866-
if err := s.waitForReady(); err != nil {
867-
return lfn.None[tlv.Blob](), err
868-
}
869-
870-
return s.cfg.AuxLeafSigner.PackSigs(blob)
866+
// We don't need to wait for the server to be ready here, as the
867+
// PackSigs method is fully stateless.
868+
return tapchannel.PackSigs(blob)
871869
}
872870

873871
// UnpackSigs takes a packed blob of signatures and returns the original
@@ -879,11 +877,9 @@ func (s *Server) UnpackSigs(blob lfn.Option[tlv.Blob]) ([]lfn.Option[tlv.Blob],
879877

880878
srvrLog.Debugf("UnpackSigs called")
881879

882-
if err := s.waitForReady(); err != nil {
883-
return nil, err
884-
}
885-
886-
return s.cfg.AuxLeafSigner.UnpackSigs(blob)
880+
// We don't need to wait for the server to be ready here, as the
881+
// UnpackSigs method is fully stateless.
882+
return tapchannel.UnpackSigs(blob)
887883
}
888884

889885
// VerifySecondLevelSigs attempts to synchronously verify a batch of aux sig
@@ -895,12 +891,10 @@ func (s *Server) VerifySecondLevelSigs(chanState *channeldb.OpenChannel,
895891

896892
srvrLog.Debugf("VerifySecondLevelSigs called")
897893

898-
if err := s.waitForReady(); err != nil {
899-
return err
900-
}
901-
902-
return s.cfg.AuxLeafSigner.VerifySecondLevelSigs(
903-
chanState, commitTx, verifyJob,
894+
// We don't need to wait for the server to be ready here, as the
895+
// VerifySecondLevelSigs method is fully stateless.
896+
return tapchannel.VerifySecondLevelSigs(
897+
s.chainParams, chanState, commitTx, verifyJob,
904898
)
905899
}
906900

@@ -1089,8 +1083,8 @@ func (s *Server) FinalizeClose(desc chancloser.AuxCloseDesc,
10891083

10901084
// ResolveContract attempts to obtain a resolution blob for the specified
10911085
// contract.
1092-
func (s *Server) ResolveContract(req lnwallet.ResolutionReq,
1093-
) lfn.Result[tlv.Blob] {
1086+
func (s *Server) ResolveContract(
1087+
req lnwallet.ResolutionReq) lfn.Result[tlv.Blob] {
10941088

10951089
srvrLog.Infof("ResolveContract called, req=%v", spew.Sdump(req))
10961090

tapchannel/aux_leaf_signer.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ func (s *AuxLeafSigner) Stop() error {
9494
return stopErr
9595
}
9696

97-
// A compile-time check to ensure that AuxLeafSigner fully implements the
98-
// lnwallet.AuxSigner interface.
99-
var _ lnwallet.AuxSigner = (*AuxLeafSigner)(nil)
100-
10197
// SubmitSecondLevelSigBatch takes a batch of aux sign jobs and processes them
10298
// asynchronously.
10399
func (s *AuxLeafSigner) SubmitSecondLevelSigBatch(
@@ -112,9 +108,7 @@ func (s *AuxLeafSigner) SubmitSecondLevelSigBatch(
112108

113109
// PackSigs takes a series of aux signatures and packs them into a single blob
114110
// that can be sent alongside the CommitSig messages.
115-
func (s *AuxLeafSigner) PackSigs(
116-
sigBlob []lfn.Option[tlv.Blob]) (lfn.Option[tlv.Blob], error) {
117-
111+
func PackSigs(sigBlob []lfn.Option[tlv.Blob]) (lfn.Option[tlv.Blob], error) {
118112
htlcSigs := make([][]*cmsg.AssetSig, len(sigBlob))
119113
for idx := range sigBlob {
120114
err := lfn.MapOptionZ(
@@ -150,9 +144,7 @@ func (s *AuxLeafSigner) PackSigs(
150144

151145
// UnpackSigs takes a packed blob of signatures and returns the original
152146
// signatures for each HTLC, keyed by HTLC index.
153-
func (s *AuxLeafSigner) UnpackSigs(
154-
blob lfn.Option[tlv.Blob]) ([]lfn.Option[tlv.Blob], error) {
155-
147+
func UnpackSigs(blob lfn.Option[tlv.Blob]) ([]lfn.Option[tlv.Blob], error) {
156148
if blob.IsNone() {
157149
return nil, nil
158150
}
@@ -173,8 +165,9 @@ func (s *AuxLeafSigner) UnpackSigs(
173165

174166
// VerifySecondLevelSigs attempts to synchronously verify a batch of aux sig
175167
// jobs.
176-
func (s *AuxLeafSigner) VerifySecondLevelSigs(chanState *channeldb.OpenChannel,
177-
commitTx *wire.MsgTx, verifyJobs []lnwallet.AuxVerifyJob) error {
168+
func VerifySecondLevelSigs(chainParams *address.ChainParams,
169+
chanState *channeldb.OpenChannel, commitTx *wire.MsgTx,
170+
verifyJobs []lnwallet.AuxVerifyJob) error {
178171

179172
for idx := range verifyJobs {
180173
verifyJob := verifyJobs[idx]
@@ -225,9 +218,10 @@ func (s *AuxLeafSigner) VerifySecondLevelSigs(chanState *channeldb.OpenChannel,
225218
continue
226219
}
227220

228-
err = s.verifyHtlcSignature(
229-
chanState, commitTx, verifyJobs[idx].KeyRing,
230-
assetSigs.Sigs, htlcOutputs, verifyJobs[idx].BaseAuxJob,
221+
err = verifyHtlcSignature(
222+
chainParams, chanState, commitTx,
223+
verifyJobs[idx].KeyRing, assetSigs.Sigs, htlcOutputs,
224+
verifyJobs[idx].BaseAuxJob,
231225
)
232226
if err != nil {
233227
return fmt.Errorf("error verifying second level sig: "+
@@ -330,13 +324,14 @@ func (s *AuxLeafSigner) processAuxSigBatch(chanState *channeldb.OpenChannel,
330324

331325
// verifyHtlcSignature verifies the HTLC signature in the commitment transaction
332326
// described by the sign job.
333-
func (s *AuxLeafSigner) verifyHtlcSignature(chanState *channeldb.OpenChannel,
334-
commitTx *wire.MsgTx, keyRing lnwallet.CommitmentKeyRing,
335-
sigs []*cmsg.AssetSig, htlcOutputs []*cmsg.AssetOutput,
336-
baseJob lnwallet.BaseAuxJob) error {
327+
func verifyHtlcSignature(chainParams *address.ChainParams,
328+
chanState *channeldb.OpenChannel, commitTx *wire.MsgTx,
329+
keyRing lnwallet.CommitmentKeyRing, sigs []*cmsg.AssetSig,
330+
htlcOutputs []*cmsg.AssetOutput, baseJob lnwallet.BaseAuxJob) error {
337331

338-
vPackets, err := s.htlcSecondLevelPacketsFromCommit(
339-
chanState, commitTx, baseJob.KeyRing, htlcOutputs, baseJob,
332+
vPackets, err := htlcSecondLevelPacketsFromCommit(
333+
chainParams, chanState, commitTx, baseJob.KeyRing, htlcOutputs,
334+
baseJob,
340335
)
341336
if err != nil {
342337
return fmt.Errorf("error generating second level packets: %w",
@@ -470,8 +465,9 @@ func (s *AuxLeafSigner) generateHtlcSignature(chanState *channeldb.OpenChannel,
470465
signDesc input.SignDescriptor,
471466
baseJob lnwallet.BaseAuxJob) (lnwallet.AuxSigJobResp, error) {
472467

473-
vPackets, err := s.htlcSecondLevelPacketsFromCommit(
474-
chanState, commitTx, baseJob.KeyRing, htlcOutputs, baseJob,
468+
vPackets, err := htlcSecondLevelPacketsFromCommit(
469+
s.cfg.ChainParams, chanState, commitTx, baseJob.KeyRing,
470+
htlcOutputs, baseJob,
475471
)
476472
if err != nil {
477473
return lnwallet.AuxSigJobResp{}, fmt.Errorf("error generating "+
@@ -556,14 +552,14 @@ func (s *AuxLeafSigner) generateHtlcSignature(chanState *channeldb.OpenChannel,
556552
// htlcSecondLevelPacketsFromCommit generates the HTLC second level packets from
557553
// the commitment transaction. A bool is returned indicating if the HTLC was
558554
// incoming or outgoing.
559-
func (s *AuxLeafSigner) htlcSecondLevelPacketsFromCommit(
555+
func htlcSecondLevelPacketsFromCommit(chainParams *address.ChainParams,
560556
chanState *channeldb.OpenChannel, commitTx *wire.MsgTx,
561557
keyRing lnwallet.CommitmentKeyRing, htlcOutputs []*cmsg.AssetOutput,
562558
baseJob lnwallet.BaseAuxJob) ([]*tappsbt.VPacket, error) {
563559

564560
packets, _, err := CreateSecondLevelHtlcPackets(
565561
chanState, commitTx, baseJob.HTLC.Amount.ToSatoshis(),
566-
keyRing, s.cfg.ChainParams, htlcOutputs,
562+
keyRing, chainParams, htlcOutputs,
567563
)
568564
if err != nil {
569565
return nil, fmt.Errorf("error creating second level HTLC "+

0 commit comments

Comments
 (0)