mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-29 10:09:08 +02:00
Merge pull request #4440 from carlaKC/4165-ratelimit
chanfitness: Rate limit in memory events based on peer flap rate
This commit is contained in:
65
rpcserver.go
65
rpcserver.go
@ -2720,6 +2720,30 @@ func (r *rpcServer) ListPeers(ctx context.Context,
|
||||
rpcPeer.Errors = append(rpcPeer.Errors, rpcErr)
|
||||
}
|
||||
|
||||
// If the server has started, we can query the event store
|
||||
// for our peer's flap count. If we do so when the server has
|
||||
// not started, the request will block.
|
||||
if r.server.Started() {
|
||||
vertex, err := route.NewVertexFromBytes(nodePub[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
flap, ts, err := r.server.chanEventStore.FlapCount(
|
||||
vertex,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If our timestamp is non-nil, we have values for our
|
||||
// peer's flap count, so we set them.
|
||||
if ts != nil {
|
||||
rpcPeer.FlapCount = int32(flap)
|
||||
rpcPeer.LastFlapNs = ts.UnixNano()
|
||||
}
|
||||
}
|
||||
|
||||
resp.Peers = append(resp.Peers, rpcPeer)
|
||||
}
|
||||
|
||||
@ -3555,42 +3579,33 @@ func createRPCOpenChannel(r *rpcServer, graph *channeldb.ChannelGraph,
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
// Get the lifespan observed by the channel event store. If the channel is
|
||||
// not known to the channel event store, return early because we cannot
|
||||
// calculate any further uptime information.
|
||||
peer, err := route.NewVertexFromBytes(nodePub.SerializeCompressed())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Query the event store for additional information about the channel.
|
||||
// Do not fail if it is not available, because there is a potential
|
||||
// race between a channel being added to our node and the event store
|
||||
// being notified of it.
|
||||
outpoint := dbChannel.FundingOutpoint
|
||||
startTime, endTime, err := r.server.chanEventStore.GetLifespan(outpoint)
|
||||
info, err := r.server.chanEventStore.GetChanInfo(outpoint, peer)
|
||||
switch err {
|
||||
// If the store does not know about the channel, we just log it.
|
||||
case chanfitness.ErrChannelNotFound:
|
||||
rpcsLog.Infof("channel: %v not found by channel event store",
|
||||
outpoint)
|
||||
|
||||
return channel, nil
|
||||
// If we got our channel info, we further populate the channel.
|
||||
case nil:
|
||||
// If there is no error getting lifespan, continue to uptime
|
||||
// calculation.
|
||||
channel.Uptime = int64(info.Uptime.Seconds())
|
||||
channel.Lifetime = int64(info.Lifetime.Seconds())
|
||||
|
||||
// If we get an unexpected error, we return it.
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If endTime is zero, the channel is still open, progress endTime to
|
||||
// the present so we can calculate lifetime.
|
||||
if endTime.IsZero() {
|
||||
endTime = time.Now()
|
||||
}
|
||||
channel.Lifetime = int64(endTime.Sub(startTime).Seconds())
|
||||
|
||||
// Once we have successfully obtained channel lifespan, we know that the
|
||||
// channel is known to the event store, so we can return any non-nil error
|
||||
// that occurs.
|
||||
uptime, err := r.server.chanEventStore.GetUptime(
|
||||
outpoint, startTime, endTime,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channel.Uptime = int64(uptime.Seconds())
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user