rpcserver: calculate and populate medianChanSizeSat in NetworkInfo

This commit is contained in:
Johan T. Halseth committed 2019-03-27 15:06:30 +01:00
1 parent f96dbd2377
commit d6d66e631a
1 file changed
+12 -3
+12 -3
View File
@@ -3959,6 +3959,7 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
totalNetworkCapacity btcutil.Amount totalNetworkCapacity btcutil.Amount
minChannelSize btcutil.Amount = math.MaxInt64 minChannelSize btcutil.Amount = math.MaxInt64
maxChannelSize btcutil.Amount maxChannelSize btcutil.Amount
medianChanSize btcutil.Amount
) )
// We'll use this map to de-duplicate channels during our traversal. // We'll use this map to de-duplicate channels during our traversal.
@@ -3966,6 +3967,10 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
// edges for each channel within the graph. // edges for each channel within the graph.
seenChans := make(map[uint64]struct{}) seenChans := make(map[uint64]struct{})
// We also keep a list of all encountered capacities, in order to
// calculate the median channel size.
var allChans []btcutil.Amount
// We'll run through all the known nodes in the within our view of the // We'll run through all the known nodes in the within our view of the
// network, tallying up the total number of nodes, and also gathering // network, tallying up the total number of nodes, and also gathering
// each node so we can measure the graph diameter and degree stats // each node so we can measure the graph diameter and degree stats
@@ -4012,6 +4017,7 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
numChannels++ numChannels++
seenChans[edge.ChannelID] = struct{}{} seenChans[edge.ChannelID] = struct{}{}
allChans = append(allChans, edge.Capacity)
return nil return nil
}); err != nil { }); err != nil {
return err return err
@@ -4028,6 +4034,9 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
return nil, err return nil, err
} }
// Find the median.
medianChanSize = autopilot.Median(allChans)
// If we don't have any channels, then reset the minChannelSize to zero // If we don't have any channels, then reset the minChannelSize to zero
// to avoid outputting NaN in encoded JSON. // to avoid outputting NaN in encoded JSON.
if numChannels == 0 { if numChannels == 0 {
@@ -4037,7 +4046,6 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
// TODO(roasbeef): graph diameter // TODO(roasbeef): graph diameter
// TODO(roasbeef): also add oldest channel? // TODO(roasbeef): also add oldest channel?
// * also add median channel size
netInfo := &lnrpc.NetworkInfo{ netInfo := &lnrpc.NetworkInfo{
MaxOutDegree: maxChanOut, MaxOutDegree: maxChanOut,
AvgOutDegree: float64(numChannels) / float64(numNodes), AvgOutDegree: float64(numChannels) / float64(numNodes),
@@ -4046,8 +4054,9 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
TotalNetworkCapacity: int64(totalNetworkCapacity), TotalNetworkCapacity: int64(totalNetworkCapacity),
AvgChannelSize: float64(totalNetworkCapacity) / float64(numChannels), AvgChannelSize: float64(totalNetworkCapacity) / float64(numChannels),
MinChannelSize: int64(minChannelSize), MinChannelSize: int64(minChannelSize),
MaxChannelSize: int64(maxChannelSize), MaxChannelSize: int64(maxChannelSize),
MedianChannelSizeSat: int64(medianChanSize),
} }
// Similarly, if we don't have any channels, then we'll also set the // Similarly, if we don't have any channels, then we'll also set the