Skip to content

Commit

Permalink
server: extract startup wait into waitForReady method
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed May 28, 2024
1 parent e15249e commit d6d9dc9
Showing 1 changed file with 46 additions and 65 deletions.
111 changes: 46 additions & 65 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,22 @@ var _ protofsm.MsgEndpoint = (*Server)(nil)
var _ funding.AuxFundingController = (*Server)(nil)
var _ routing.TlvTrafficShaper = (*Server)(nil)

// waitForReady blocks until the server is ready to serve requests. If the
// server is shutting down before we ever become ready, an error is returned.
func (s *Server) waitForReady() error {
// We just need to wait for the server to be ready (but not block
// shutdown in case of a startup error). If we shut down after passing
// this part of the code, the called component will handle the quit
// signal.
select {
case <-s.ready:
return nil

case <-s.quit:
return fmt.Errorf("tapd is shutting down")
}
}

// FetchLeavesFromView attempts to fetch the auxiliary leaves that correspond to
// the passed aux blob, and pending fully evaluated HTLC view.
//
Expand All @@ -698,11 +714,8 @@ func (s *Server) FetchLeavesFromView(chanState *channeldb.OpenChannel,
"numTheirUpdates=%d", isOurCommit, ourBalance, theirBalance,
len(view.OurUpdates), len(view.TheirUpdates))

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](), nil,
fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[lnwallet.CommitAuxLeaves](), nil, err
}

return s.cfg.AuxLeafCreator.FetchLeavesFromView(
Expand All @@ -725,11 +738,8 @@ func (s *Server) FetchLeavesFromCommit(chanState *channeldb.OpenChannel,
"theirBalance=%v, numHtlcs=%d", com.LocalBalance,
com.RemoteBalance, len(com.Htlcs))

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](),
fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[lnwallet.CommitAuxLeaves](), err
}

return s.cfg.AuxLeafCreator.FetchLeavesFromCommit(chanState, com, keys)
Expand All @@ -747,11 +757,8 @@ func (s *Server) FetchLeavesFromRevocation(
"teirBalance=%v, numHtlcs=%d", rev.OurBalance, rev.TheirBalance,
len(rev.HTLCEntries))

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.CommitAuxLeaves](),
fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[lnwallet.CommitAuxLeaves](), err
}

return s.cfg.AuxLeafCreator.FetchLeavesFromRevocation(rev)
Expand All @@ -772,10 +779,8 @@ func (s *Server) ApplyHtlcView(chanState *channeldb.OpenChannel,
"numTheirUpdates=%d", isOurCommit, ourBalance, theirBalance,
len(originalView.OurUpdates), len(originalView.TheirUpdates))

select {
case <-s.ready:
case <-s.quit:
return lfn.None[tlv.Blob](), fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[tlv.Blob](), err
}

return s.cfg.AuxLeafCreator.ApplyHtlcView(
Expand Down Expand Up @@ -833,10 +838,8 @@ func (s *Server) SubmitSecondLevelSigBatch(chanState *channeldb.OpenChannel,
srvrLog.Debugf("SubmitSecondLevelSigBatch called, numSigs=%d",
len(sigJob))

select {
case <-s.ready:
case <-s.quit:
return fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return err
}

return s.cfg.AuxLeafSigner.SubmitSecondLevelSigBatch(
Expand All @@ -853,10 +856,8 @@ func (s *Server) PackSigs(

srvrLog.Debugf("PackSigs called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[tlv.Blob](), fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[tlv.Blob](), err
}

return s.cfg.AuxLeafSigner.PackSigs(blob)
Expand All @@ -871,10 +872,8 @@ func (s *Server) UnpackSigs(blob lfn.Option[tlv.Blob]) ([]lfn.Option[tlv.Blob],

srvrLog.Debugf("UnpackSigs called")

select {
case <-s.ready:
case <-s.quit:
return nil, fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return nil, err
}

return s.cfg.AuxLeafSigner.UnpackSigs(blob)
Expand All @@ -889,10 +888,8 @@ func (s *Server) VerifySecondLevelSigs(chanState *channeldb.OpenChannel,

srvrLog.Debugf("VerifySecondLevelSigs called")

select {
case <-s.ready:
case <-s.quit:
return fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return err
}

return s.cfg.AuxLeafSigner.VerifySecondLevelSigs(
Expand All @@ -912,11 +909,8 @@ func (s *Server) DescFromPendingChanID(pid funding.PendingChanID,

srvrLog.Debugf("DescFromPendingChanID called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[lnwallet.AuxFundingDesc](),
fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[lnwallet.AuxFundingDesc](), err
}

return s.cfg.AuxFundingController.DescFromPendingChanID(
Expand All @@ -934,11 +928,8 @@ func (s *Server) DeriveTapscriptRoot(

srvrLog.Debugf("DeriveTapscriptRoot called")

select {
case <-s.ready:
case <-s.quit:
return lfn.None[chainhash.Hash](),
fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return lfn.None[chainhash.Hash](), err
}

return s.cfg.AuxFundingController.DeriveTapscriptRoot(pid)
Expand All @@ -951,10 +942,8 @@ func (s *Server) DeriveTapscriptRoot(
func (s *Server) ChannelReady(openChan *channeldb.OpenChannel) error {
srvrLog.Debugf("ChannelReady called")

select {
case <-s.ready:
case <-s.quit:
return fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return err
}

return s.cfg.AuxFundingController.ChannelReady(openChan)
Expand All @@ -967,10 +956,8 @@ func (s *Server) ChannelReady(openChan *channeldb.OpenChannel) error {
func (s *Server) ChannelFinalized(pid funding.PendingChanID) error {
srvrLog.Debugf("ChannelFinalized called")

select {
case <-s.ready:
case <-s.quit:
return fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return err
}

return s.cfg.AuxFundingController.ChannelFinalized(pid)
Expand All @@ -989,10 +976,8 @@ func (s *Server) HandleTraffic(cid lnwire.ShortChannelID,
srvrLog.Debugf("HandleTraffic called, cid=%v, fundingBlob=%v", cid,
spew.Sdump(fundingBlob))

select {
case <-s.ready:
case <-s.quit:
return false, fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return false, err
}

return s.cfg.AuxTrafficShaper.HandleTraffic(cid, fundingBlob)
Expand All @@ -1012,10 +997,8 @@ func (s *Server) PaymentBandwidth(htlcBlob,
"commitmentBlob=%v", spew.Sdump(htlcBlob),
spew.Sdump(commitmentBlob))

select {
case <-s.ready:
case <-s.quit:
return 0, fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return 0, err
}

return s.cfg.AuxTrafficShaper.PaymentBandwidth(htlcBlob, commitmentBlob)
Expand All @@ -1033,10 +1016,8 @@ func (s *Server) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
srvrLog.Debugf("ProduceHtlcExtraData called, totalAmount=%d, "+
"htlcBlob=%v", totalAmount, spew.Sdump(htlcCustomRecords))

select {
case <-s.ready:
case <-s.quit:
return 0, nil, fmt.Errorf("tapd is shutting down")
if err := s.waitForReady(); err != nil {
return 0, nil, err
}

return s.cfg.AuxTrafficShaper.ProduceHtlcExtraData(
Expand Down

0 comments on commit d6d9dc9

Please sign in to comment.