rpcserver: ensure graph RPC's work with partially advertised channels

This commit is contained in:
Olaoluwa Osuntokun 2017-01-17 13:20:06 -08:00
parent 440cf6f956
commit e7631c9720
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -1445,30 +1445,55 @@ func (r *rpcServer) DescribeGraph(context.Context,
} }
func marshalDbEdge(c1, c2 *channeldb.ChannelEdge) *lnrpc.ChannelEdge { func marshalDbEdge(c1, c2 *channeldb.ChannelEdge) *lnrpc.ChannelEdge {
node1Pub := c2.Node.PubKey.SerializeCompressed() var (
node2Pub := c1.Node.PubKey.SerializeCompressed() node1Pub, node2Pub []byte
capacity btcutil.Amount
lastUpdate int64
chanID uint64
chanPoint string
)
if c2 != nil {
node1Pub = c2.Node.PubKey.SerializeCompressed()
lastUpdate = c2.LastUpdate.Unix()
capacity = c2.Capacity
chanID = c2.ChannelID
chanPoint = c2.ChannelPoint.String()
}
if c1 != nil {
node2Pub = c1.Node.PubKey.SerializeCompressed()
lastUpdate = c1.LastUpdate.Unix()
capacity = c1.Capacity
chanID = c1.ChannelID
chanPoint = c1.ChannelPoint.String()
}
edge := &lnrpc.ChannelEdge{ edge := &lnrpc.ChannelEdge{
ChannelId: c1.ChannelID, ChannelId: chanID,
ChanPoint: c1.ChannelPoint.String(), ChanPoint: chanPoint,
LastUpdate: uint32(c1.LastUpdate.Unix()), // TODO(roasbeef): update should be on edge info itself
LastUpdate: uint32(lastUpdate),
Node1Pub: hex.EncodeToString(node1Pub), Node1Pub: hex.EncodeToString(node1Pub),
Node2Pub: hex.EncodeToString(node2Pub), Node2Pub: hex.EncodeToString(node2Pub),
Capacity: int64(c1.Capacity), Capacity: int64(capacity),
} }
edge.Node1Policy = &lnrpc.RoutingPolicy{ if c1 != nil {
TimeLockDelta: uint32(c1.Expiry), edge.Node1Policy = &lnrpc.RoutingPolicy{
MinHtlc: int64(c1.MinHTLC), TimeLockDelta: uint32(c1.Expiry),
FeeBaseMsat: int64(c1.FeeBaseMSat), MinHtlc: int64(c1.MinHTLC),
FeeRateMilliMsat: int64(c1.FeeProportionalMillionths), FeeBaseMsat: int64(c1.FeeBaseMSat),
FeeRateMilliMsat: int64(c1.FeeProportionalMillionths),
}
} }
edge.Node2Policy = &lnrpc.RoutingPolicy{ if c2 != nil {
TimeLockDelta: uint32(c2.Expiry), edge.Node2Policy = &lnrpc.RoutingPolicy{
MinHtlc: int64(c2.MinHTLC), TimeLockDelta: uint32(c2.Expiry),
FeeBaseMsat: int64(c2.FeeBaseMSat), MinHtlc: int64(c2.MinHTLC),
FeeRateMilliMsat: int64(c2.FeeProportionalMillionths), FeeBaseMsat: int64(c2.FeeBaseMSat),
FeeRateMilliMsat: int64(c2.FeeProportionalMillionths),
}
} }
return edge return edge
@ -1533,6 +1558,7 @@ func (r *rpcServer) GetNodeInfo(_ context.Context, in *lnrpc.NodeInfoRequest) (*
return nil, err return nil, err
} }
// TODO(roasbeef): list channels as well?
return &lnrpc.NodeInfo{ return &lnrpc.NodeInfo{
Node: &lnrpc.LightningNode{ Node: &lnrpc.LightningNode{
LastUpdate: uint32(node.LastUpdate.Unix()), LastUpdate: uint32(node.LastUpdate.Unix()),
@ -1645,14 +1671,22 @@ func (r *rpcServer) GetNetworkInfo(context.Context, *lnrpc.NetworkInfoRequest) (
} }
if outDegree > maxChanOut { if outDegree > maxChanOut {
outDegree = maxChanOut maxChanOut = outDegree
} }
} }
// Finally, we traverse each channel visiting both channel edges at // Finally, we traverse each channel visiting both channel edges at
// once to avoid double counting any stats we're attempting to gather. // once to avoid double counting any stats we're attempting to gather.
if err := graph.ForEachChannel(func(c1, c2 *channeldb.ChannelEdge) error { if err := graph.ForEachChannel(func(c1, c2 *channeldb.ChannelEdge) error {
chanCapacity := c1.Capacity var chanCapacity btcutil.Amount
switch {
case c1 == nil:
chanCapacity = c2.Capacity
case c2 == nil:
chanCapacity = c1.Capacity
default:
chanCapacity = c1.Capacity
}
if chanCapacity < minChannelSize { if chanCapacity < minChannelSize {
minChannelSize = chanCapacity minChannelSize = chanCapacity
@ -1671,6 +1705,7 @@ func (r *rpcServer) GetNetworkInfo(context.Context, *lnrpc.NetworkInfoRequest) (
} }
// TODO(roasbeef): also add oldest channel? // TODO(roasbeef): also add oldest channel?
// * also add median channel size
return &lnrpc.NetworkInfo{ return &lnrpc.NetworkInfo{
MaxOutDegree: maxChanOut, MaxOutDegree: maxChanOut,
AvgOutDegree: float64(numChannels) / float64(numNodes), AvgOutDegree: float64(numChannels) / float64(numNodes),