rpc: fix bug in GetNetworkInfo that causes NaN outputs in JSON
Previously the integers would either underflow or the floating point division would result in odd numbers if a node knew of zero channels within the network graph. We now fix this situation by checking to see if any channels exist before computing the network stats. Fixes #211.
This commit is contained in:
parent
a5d90b9ad1
commit
1aaf37974a
24
rpcserver.go
24
rpcserver.go
@ -1963,20 +1963,36 @@ func (r *rpcServer) GetNetworkInfo(context.Context, *lnrpc.NetworkInfoRequest) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we don't have any channels, then reset the minChannelSize to zero
|
||||||
|
// to avoid outputting NaN in encoded JSOn.
|
||||||
|
if numChannels == 0 {
|
||||||
|
minChannelSize = 0
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
// * also add median channel size
|
||||||
return &lnrpc.NetworkInfo{
|
netInfo := &lnrpc.NetworkInfo{
|
||||||
MaxOutDegree: maxChanOut,
|
MaxOutDegree: maxChanOut,
|
||||||
AvgOutDegree: float64(numChannels) / float64(numNodes),
|
AvgOutDegree: float64(numChannels) / float64(numNodes),
|
||||||
NumNodes: numNodes,
|
NumNodes: numNodes,
|
||||||
NumChannels: numChannels,
|
NumChannels: numChannels,
|
||||||
TotalNetworkCapacity: int64(totalNetworkCapacity),
|
TotalNetworkCapacity: int64(totalNetworkCapacity),
|
||||||
AvgChannelSize: float64(totalNetworkCapacity) / float64(numChannels),
|
AvgChannelSize: float64(totalNetworkCapacity) / float64(numChannels),
|
||||||
MinChannelSize: int64(minChannelSize),
|
|
||||||
MaxChannelSize: int64(maxChannelSize),
|
MinChannelSize: int64(minChannelSize),
|
||||||
}, nil
|
MaxChannelSize: int64(maxChannelSize),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Similarly, if we don't have any channels, then we'll also set the
|
||||||
|
// average channel size to zero in order to avoid weird JSON encoding
|
||||||
|
// outputs.
|
||||||
|
if numChannels == 0 {
|
||||||
|
netInfo.AvgChannelSize = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return netInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopDaemon will send a shutdown request to the interrupt handler, triggering
|
// StopDaemon will send a shutdown request to the interrupt handler, triggering
|
||||||
|
Loading…
Reference in New Issue
Block a user