multi: move block epochs dependency from links to switch
In this commit, we move the block height dependency from the links in the switch to the switch itself. This is possible due to a recent change on the links no longer depending on the block height to update their commitment fees. We'll now only have the switch be alerted of new blocks coming in and links will retrieve the height from it atomically.
This commit is contained in:
9 files changed
+128
-171
No files matched your search
+43
-8
@@ -2,23 +2,22 @@ package htlcswitch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/coreos/bbolt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/roasbeef/btcd/btcec"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/contractcourt"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/roasbeef/btcd/btcec"
|
||||
"github.com/roasbeef/btcd/wire"
|
||||
"github.com/roasbeef/btcutil"
|
||||
)
|
||||
@@ -142,6 +141,10 @@ type Config struct {
|
||||
// provide payment senders our latest policy when sending encrypted
|
||||
// error messages.
|
||||
FetchLastChannelUpdate func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate, error)
|
||||
|
||||
// Notifier is an instance of a chain notifier that we'll use to signal
|
||||
// the switch when a new block has arrived.
|
||||
Notifier chainntnfs.ChainNotifier
|
||||
}
|
||||
|
||||
// Switch is the central messaging bus for all incoming/outgoing HTLCs.
|
||||
@@ -155,8 +158,14 @@ type Config struct {
|
||||
type Switch struct {
|
||||
started int32 // To be used atomically.
|
||||
shutdown int32 // To be used atomically.
|
||||
wg sync.WaitGroup
|
||||
quit chan struct{}
|
||||
|
||||
// bestHeight is the best known height of the main chain. The links will
|
||||
// be used this information to govern decisions based on HTLC timeouts.
|
||||
// This will be retrieved by the registered links atomically.
|
||||
bestHeight uint32
|
||||
|
||||
wg sync.WaitGroup
|
||||
quit chan struct{}
|
||||
|
||||
// cfg is a copy of the configuration struct that the htlc switch
|
||||
// service was initialized with.
|
||||
@@ -229,10 +238,15 @@ type Switch struct {
|
||||
// to the forwarding log.
|
||||
fwdEventMtx sync.Mutex
|
||||
pendingFwdingEvents []channeldb.ForwardingEvent
|
||||
|
||||
// blockEpochStream is an active block epoch event stream backed by an
|
||||
// active ChainNotifier instance. This will be used to retrieve the
|
||||
// lastest height of the chain.
|
||||
blockEpochStream *chainntnfs.BlockEpochEvent
|
||||
}
|
||||
|
||||
// New creates the new instance of htlc switch.
|
||||
func New(cfg Config) (*Switch, error) {
|
||||
func New(cfg Config, currentHeight uint32) (*Switch, error) {
|
||||
circuitMap, err := NewCircuitMap(&CircuitMapConfig{
|
||||
DB: cfg.DB,
|
||||
ExtractErrorEncrypter: cfg.ExtractErrorEncrypter,
|
||||
@@ -247,6 +261,7 @@ func New(cfg Config) (*Switch, error) {
|
||||
}
|
||||
|
||||
return &Switch{
|
||||
bestHeight: currentHeight,
|
||||
cfg: &cfg,
|
||||
circuits: circuitMap,
|
||||
paymentSequencer: sequencer,
|
||||
@@ -1339,8 +1354,10 @@ func (s *Switch) CloseLink(chanPoint *wire.OutPoint, closeType ChannelCloseType,
|
||||
func (s *Switch) htlcForwarder() {
|
||||
defer s.wg.Done()
|
||||
|
||||
// Remove all links once we've been signalled for shutdown.
|
||||
defer func() {
|
||||
s.blockEpochStream.Cancel()
|
||||
|
||||
// Remove all links once we've been signalled for shutdown.
|
||||
s.indexMtx.Lock()
|
||||
for _, link := range s.linkIndex {
|
||||
if err := s.removeLink(link.ChanID()); err != nil {
|
||||
@@ -1378,8 +1395,15 @@ func (s *Switch) htlcForwarder() {
|
||||
fwdEventTicker := time.NewTicker(15 * time.Second)
|
||||
defer fwdEventTicker.Stop()
|
||||
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
case blockEpoch, ok := <-s.blockEpochStream.Epochs:
|
||||
if !ok {
|
||||
break out
|
||||
}
|
||||
|
||||
atomic.StoreUint32(&s.bestHeight, uint32(blockEpoch.Height))
|
||||
// A local close request has arrived, we'll forward this to the
|
||||
// relevant link (if it exists) so the channel can be
|
||||
// cooperatively closed (if possible).
|
||||
@@ -1549,6 +1573,12 @@ func (s *Switch) Start() error {
|
||||
|
||||
log.Infof("Starting HTLC Switch")
|
||||
|
||||
blockEpochStream, err := s.cfg.Notifier.RegisterBlockEpochNtfn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.blockEpochStream = blockEpochStream
|
||||
|
||||
s.wg.Add(1)
|
||||
go s.htlcForwarder()
|
||||
|
||||
@@ -2033,3 +2063,8 @@ func (s *Switch) FlushForwardingEvents() error {
|
||||
// forwarding log.
|
||||
return s.cfg.FwdingLog.AddForwardingEvents(events)
|
||||
}
|
||||
|
||||
// BestHeight returns the best height known to the switch.
|
||||
func (s *Switch) BestHeight() uint32 {
|
||||
return atomic.LoadUint32(&s.bestHeight)
|
||||
}
|
||||
Reference in new issue
Block a user