2017-11-30 03:23:49 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2018-05-08 02:33:17 +03:00
|
|
|
"errors"
|
2017-11-30 03:23:49 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2017-11-30 03:23:49 +03:00
|
|
|
)
|
|
|
|
|
2018-05-08 02:33:17 +03:00
|
|
|
// ErrVBarrierShuttingDown signals that the barrier has been requested to
|
|
|
|
// shutdown, and that the caller should not treat the wait condition as
|
|
|
|
// fulfilled.
|
|
|
|
var ErrVBarrierShuttingDown = errors.New("validation barrier shutting down")
|
|
|
|
|
2017-11-30 03:23:49 +03:00
|
|
|
// ValidationBarrier is a barrier used to ensure proper validation order while
|
|
|
|
// concurrently validating new announcements for channel edges, and the
|
|
|
|
// attributes of channel edges. It uses this set of maps (protected by this
|
|
|
|
// mutex) to track validation dependencies. For a given channel our
|
|
|
|
// dependencies look like this: chanAnn <- chanUp <- nodeAnn. That is we must
|
|
|
|
// validate the item on the left of the arrow before that on the right.
|
|
|
|
type ValidationBarrier struct {
|
|
|
|
// validationSemaphore is a channel of structs which is used as a
|
|
|
|
// sempahore. Initially we'll fill this with a buffered channel of the
|
|
|
|
// size of the number of active requests. Each new job will consume
|
|
|
|
// from this channel, then restore the value upon completion.
|
|
|
|
validationSemaphore chan struct{}
|
|
|
|
|
|
|
|
// chanAnnFinSignal is map that keep track of all the pending
|
|
|
|
// ChannelAnnouncement like validation job going on. Once the job has
|
|
|
|
// been completed, the channel will be closed unblocking any
|
|
|
|
// dependants.
|
|
|
|
chanAnnFinSignal map[lnwire.ShortChannelID]chan struct{}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// chanEdgeDependencies tracks any channel edge updates which should
|
2017-11-30 03:23:49 +03:00
|
|
|
// wait until the completion of the ChannelAnnouncement before
|
|
|
|
// proceeding. This is a dependency, as we can't validate the update
|
|
|
|
// before we validate the announcement which creates the channel
|
|
|
|
// itself.
|
2018-02-07 06:11:11 +03:00
|
|
|
chanEdgeDependencies map[lnwire.ShortChannelID]chan struct{}
|
2017-11-30 03:23:49 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// nodeAnnDependencies tracks any pending NodeAnnouncement validation
|
2017-11-30 03:23:49 +03:00
|
|
|
// jobs which should wait until the completion of the
|
|
|
|
// ChannelAnnouncement before proceeding.
|
2019-04-05 18:36:11 +03:00
|
|
|
nodeAnnDependencies map[route.Vertex]chan struct{}
|
2017-11-30 03:23:49 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidationBarrier creates a new instance of a validation barrier given
|
|
|
|
// the total number of active requests, and a quit channel which will be used
|
2018-04-18 05:02:04 +03:00
|
|
|
// to know when to kill pending, but unfilled jobs.
|
2017-11-30 03:23:49 +03:00
|
|
|
func NewValidationBarrier(numActiveReqs int,
|
|
|
|
quitChan chan struct{}) *ValidationBarrier {
|
|
|
|
|
|
|
|
v := &ValidationBarrier{
|
|
|
|
chanAnnFinSignal: make(map[lnwire.ShortChannelID]chan struct{}),
|
2018-02-07 06:11:11 +03:00
|
|
|
chanEdgeDependencies: make(map[lnwire.ShortChannelID]chan struct{}),
|
2019-04-05 18:36:11 +03:00
|
|
|
nodeAnnDependencies: make(map[route.Vertex]chan struct{}),
|
2017-11-30 03:23:49 +03:00
|
|
|
quit: quitChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll first initialize a set of sempahores to limit our concurrency
|
|
|
|
// when validating incoming requests in parallel.
|
|
|
|
v.validationSemaphore = make(chan struct{}, numActiveReqs)
|
|
|
|
for i := 0; i < numActiveReqs; i++ {
|
|
|
|
v.validationSemaphore <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// InitJobDependencies will wait for a new job slot to become open, and then
|
|
|
|
// sets up any dependent signals/trigger for the new job
|
|
|
|
func (v *ValidationBarrier) InitJobDependencies(job interface{}) {
|
2017-11-30 03:23:49 +03:00
|
|
|
// We'll wait for either a new slot to become open, or for the quit
|
|
|
|
// channel to be closed.
|
|
|
|
select {
|
|
|
|
case <-v.validationSemaphore:
|
|
|
|
case <-v.quit:
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
|
|
|
// Once a slot is open, we'll examine the message of the job, to see if
|
2018-02-07 06:11:11 +03:00
|
|
|
// there need to be any dependent barriers set up.
|
2017-11-30 03:23:49 +03:00
|
|
|
switch msg := job.(type) {
|
|
|
|
|
|
|
|
// If this is a channel announcement, then we'll need to set up den
|
|
|
|
// tenancies, as we'll need to verify this before we verify any
|
|
|
|
// ChannelUpdates for the same channel, or NodeAnnouncements of nodes
|
|
|
|
// that are involved in this channel. This goes for both the wire
|
|
|
|
// type,s and also the types that we use within the database.
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
|
|
|
|
// We ensure that we only create a new announcement signal iff,
|
|
|
|
// one doesn't already exist, as there may be duplicate
|
|
|
|
// announcements. We'll close this signal once the
|
|
|
|
// ChannelAnnouncement has been validated. This will result in
|
2018-02-07 06:11:11 +03:00
|
|
|
// all the dependent jobs being unlocked so they can finish
|
2017-11-30 03:23:49 +03:00
|
|
|
// execution themselves.
|
|
|
|
if _, ok := v.chanAnnFinSignal[msg.ShortChannelID]; !ok {
|
|
|
|
// We'll create the channel that we close after we
|
|
|
|
// validate this announcement. All dependants will
|
|
|
|
// point to this same channel, so they'll be unblocked
|
|
|
|
// at the same time.
|
|
|
|
annFinCond := make(chan struct{})
|
|
|
|
v.chanAnnFinSignal[msg.ShortChannelID] = annFinCond
|
2018-02-07 06:11:11 +03:00
|
|
|
v.chanEdgeDependencies[msg.ShortChannelID] = annFinCond
|
2017-11-30 03:23:49 +03:00
|
|
|
|
2019-04-05 18:36:11 +03:00
|
|
|
v.nodeAnnDependencies[route.Vertex(msg.NodeID1)] = annFinCond
|
|
|
|
v.nodeAnnDependencies[route.Vertex(msg.NodeID2)] = annFinCond
|
2017-11-30 03:23:49 +03:00
|
|
|
}
|
|
|
|
case *channeldb.ChannelEdgeInfo:
|
|
|
|
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
|
|
|
|
if _, ok := v.chanAnnFinSignal[shortID]; !ok {
|
|
|
|
annFinCond := make(chan struct{})
|
|
|
|
|
|
|
|
v.chanAnnFinSignal[shortID] = annFinCond
|
2018-02-07 06:11:11 +03:00
|
|
|
v.chanEdgeDependencies[shortID] = annFinCond
|
2017-11-30 03:23:49 +03:00
|
|
|
|
2019-04-05 18:36:11 +03:00
|
|
|
v.nodeAnnDependencies[route.Vertex(msg.NodeKey1Bytes)] = annFinCond
|
|
|
|
v.nodeAnnDependencies[route.Vertex(msg.NodeKey2Bytes)] = annFinCond
|
2017-11-30 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// These other types don't have any dependants, so no further
|
|
|
|
// initialization needs to be done beyond just occupying a job slot.
|
|
|
|
case *channeldb.ChannelEdgePolicy:
|
|
|
|
return
|
|
|
|
case *lnwire.ChannelUpdate:
|
|
|
|
return
|
|
|
|
case *lnwire.NodeAnnouncement:
|
2018-01-31 07:26:26 +03:00
|
|
|
// TODO(roasbeef): node ann needs to wait on existing channel updates
|
2017-11-30 03:23:49 +03:00
|
|
|
return
|
|
|
|
case *channeldb.LightningNode:
|
|
|
|
return
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
// TODO(roasbeef): need to wait on chan ann?
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteJob returns a free slot to the set of available job slots. This
|
|
|
|
// should be called once a job has been fully completed. Otherwise, slots may
|
|
|
|
// not be returned to the internal scheduling, causing a deadlock when a new
|
|
|
|
// overflow job is attempted.
|
|
|
|
func (v *ValidationBarrier) CompleteJob() {
|
|
|
|
select {
|
|
|
|
case v.validationSemaphore <- struct{}{}:
|
|
|
|
case <-v.quit:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForDependants will block until any jobs that this job dependants on have
|
|
|
|
// finished executing. This allows us a graceful way to schedule goroutines
|
2018-02-07 06:11:11 +03:00
|
|
|
// based on any pending uncompleted dependent jobs. If this job doesn't have an
|
|
|
|
// active dependent, then this function will return immediately.
|
2018-05-08 02:33:17 +03:00
|
|
|
func (v *ValidationBarrier) WaitForDependants(job interface{}) error {
|
2017-11-30 03:23:49 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
signal chan struct{}
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
v.Lock()
|
|
|
|
switch msg := job.(type) {
|
|
|
|
|
|
|
|
// Any ChannelUpdate or NodeAnnouncement jobs will need to wait on the
|
|
|
|
// completion of any active ChannelAnnouncement jobs related to them.
|
|
|
|
case *channeldb.ChannelEdgePolicy:
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
|
2018-02-07 06:11:11 +03:00
|
|
|
signal, ok = v.chanEdgeDependencies[shortID]
|
2017-11-30 03:23:49 +03:00
|
|
|
case *channeldb.LightningNode:
|
2019-04-05 18:36:11 +03:00
|
|
|
vertex := route.Vertex(msg.PubKeyBytes)
|
2018-02-07 06:11:11 +03:00
|
|
|
signal, ok = v.nodeAnnDependencies[vertex]
|
2017-11-30 03:23:49 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2018-02-07 06:11:11 +03:00
|
|
|
signal, ok = v.chanEdgeDependencies[msg.ShortChannelID]
|
2017-11-30 03:23:49 +03:00
|
|
|
case *lnwire.NodeAnnouncement:
|
2019-04-05 18:36:11 +03:00
|
|
|
vertex := route.Vertex(msg.NodeID)
|
2018-02-07 06:11:11 +03:00
|
|
|
signal, ok = v.nodeAnnDependencies[vertex]
|
2017-11-30 03:23:49 +03:00
|
|
|
|
|
|
|
// Other types of jobs can be executed immediately, so we'll just
|
|
|
|
// return directly.
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
// TODO(roasbeef): need to wait on chan ann?
|
|
|
|
v.Unlock()
|
2018-05-08 02:33:17 +03:00
|
|
|
return nil
|
2017-11-30 03:23:49 +03:00
|
|
|
case *channeldb.ChannelEdgeInfo:
|
|
|
|
v.Unlock()
|
2018-05-08 02:33:17 +03:00
|
|
|
return nil
|
2017-11-30 03:23:49 +03:00
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
v.Unlock()
|
2018-05-08 02:33:17 +03:00
|
|
|
return nil
|
2017-11-30 03:23:49 +03:00
|
|
|
}
|
|
|
|
v.Unlock()
|
|
|
|
|
|
|
|
// If we do have an active job, then we'll wait until either the signal
|
|
|
|
// is closed, or the set of jobs exits.
|
|
|
|
if ok {
|
|
|
|
select {
|
|
|
|
case <-v.quit:
|
2018-05-08 02:33:17 +03:00
|
|
|
return ErrVBarrierShuttingDown
|
2017-11-30 03:23:49 +03:00
|
|
|
case <-signal:
|
2018-05-08 02:33:17 +03:00
|
|
|
return nil
|
2017-11-30 03:23:49 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-08 02:33:17 +03:00
|
|
|
|
|
|
|
return nil
|
2017-11-30 03:23:49 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// SignalDependants will signal any jobs that are dependent on this job that
|
2017-11-30 03:23:49 +03:00
|
|
|
// they can continue execution. If the job doesn't have any dependants, then
|
|
|
|
// this function sill exit immediately.
|
|
|
|
func (v *ValidationBarrier) SignalDependants(job interface{}) {
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
|
|
|
switch msg := job.(type) {
|
|
|
|
|
|
|
|
// If we've just finished executing a ChannelAnnouncement, then we'll
|
|
|
|
// close out the signal, and remove the signal from the map of active
|
2018-02-07 06:11:11 +03:00
|
|
|
// ones. This will allow any dependent jobs to continue execution.
|
2017-11-30 03:23:49 +03:00
|
|
|
case *channeldb.ChannelEdgeInfo:
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
|
|
|
|
finSignal, ok := v.chanAnnFinSignal[shortID]
|
|
|
|
if ok {
|
|
|
|
close(finSignal)
|
|
|
|
delete(v.chanAnnFinSignal, shortID)
|
|
|
|
}
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
|
|
|
finSignal, ok := v.chanAnnFinSignal[msg.ShortChannelID]
|
|
|
|
if ok {
|
|
|
|
close(finSignal)
|
|
|
|
delete(v.chanAnnFinSignal, msg.ShortChannelID)
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
delete(v.chanEdgeDependencies, msg.ShortChannelID)
|
2017-11-30 03:23:49 +03:00
|
|
|
|
|
|
|
// For all other job types, we'll delete the tracking entries from the
|
|
|
|
// map, as if we reach this point, then all dependants have already
|
|
|
|
// finished executing and we can proceed.
|
|
|
|
case *channeldb.LightningNode:
|
2019-04-05 18:36:11 +03:00
|
|
|
delete(v.nodeAnnDependencies, route.Vertex(msg.PubKeyBytes))
|
2017-11-30 03:23:49 +03:00
|
|
|
case *lnwire.NodeAnnouncement:
|
2019-04-05 18:36:11 +03:00
|
|
|
delete(v.nodeAnnDependencies, route.Vertex(msg.NodeID))
|
2017-11-30 03:23:49 +03:00
|
|
|
case *lnwire.ChannelUpdate:
|
2018-02-07 06:11:11 +03:00
|
|
|
delete(v.chanEdgeDependencies, msg.ShortChannelID)
|
2017-11-30 03:23:49 +03:00
|
|
|
case *channeldb.ChannelEdgePolicy:
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(msg.ChannelID)
|
2018-02-07 06:11:11 +03:00
|
|
|
delete(v.chanEdgeDependencies, shortID)
|
2017-11-30 03:23:49 +03:00
|
|
|
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|