Merge pull request #3316 from cfromknecht/num-zombies-rpc

rpcserver+channeldb: return num zombies in NetworkInfo response
This commit is contained in:
Conner Fromknecht 2019-07-17 16:27:48 -07:00 committed by GitHub
commit 377b7bf3ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 573 additions and 504 deletions

View File

@ -3195,6 +3195,31 @@ func isZombieEdge(zombieIndex *bbolt.Bucket,
return true, pubKey1, pubKey2 return true, pubKey1, pubKey2
} }
// NumZombies returns the current number of zombie channels in the graph.
func (c *ChannelGraph) NumZombies() (uint64, error) {
var numZombies uint64
err := c.db.View(func(tx *bbolt.Tx) error {
edges := tx.Bucket(edgeBucket)
if edges == nil {
return nil
}
zombieIndex := edges.Bucket(zombieBucket)
if zombieIndex == nil {
return nil
}
return zombieIndex.ForEach(func(_, _ []byte) error {
numZombies++
return nil
})
})
if err != nil {
return 0, err
}
return numZombies, nil
}
func putLightningNode(nodeBucket *bbolt.Bucket, aliasBucket *bbolt.Bucket, func putLightningNode(nodeBucket *bbolt.Bucket, aliasBucket *bbolt.Bucket,
updateIndex *bbolt.Bucket, node *LightningNode) error { updateIndex *bbolt.Bucket, node *LightningNode) error {

View File

@ -2847,6 +2847,22 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo) assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
} }
// assertNumZombies queries the provided ChannelGraph for NumZombies, and
// asserts that the returned number is equal to expZombies.
func assertNumZombies(t *testing.T, graph *ChannelGraph, expZombies uint64) {
t.Helper()
numZombies, err := graph.NumZombies()
if err != nil {
t.Fatalf("unable to query number of zombies: %v", err)
}
if numZombies != expZombies {
t.Fatalf("expected %d zombies, found %d",
expZombies, numZombies)
}
}
// TestGraphZombieIndex ensures that we can mark edges correctly as zombie/live. // TestGraphZombieIndex ensures that we can mark edges correctly as zombie/live.
func TestGraphZombieIndex(t *testing.T) { func TestGraphZombieIndex(t *testing.T) {
t.Parallel() t.Parallel()
@ -2885,6 +2901,7 @@ func TestGraphZombieIndex(t *testing.T) {
if isZombie { if isZombie {
t.Fatal("expected edge to not be marked as zombie") t.Fatal("expected edge to not be marked as zombie")
} }
assertNumZombies(t, graph, 0)
// If we delete the edge and mark it as a zombie, then we should expect // If we delete the edge and mark it as a zombie, then we should expect
// to see it within the index. // to see it within the index.
@ -2904,6 +2921,7 @@ func TestGraphZombieIndex(t *testing.T) {
t.Fatalf("expected pubKey2 %x, got %x", node2.PubKeyBytes, t.Fatalf("expected pubKey2 %x, got %x", node2.PubKeyBytes,
pubKey2) pubKey2)
} }
assertNumZombies(t, graph, 1)
// Similarly, if we mark the same edge as live, we should no longer see // Similarly, if we mark the same edge as live, we should no longer see
// it within the index. // it within the index.
@ -2914,6 +2932,7 @@ func TestGraphZombieIndex(t *testing.T) {
if isZombie { if isZombie {
t.Fatal("expected edge to not be marked as zombie") t.Fatal("expected edge to not be marked as zombie")
} }
assertNumZombies(t, graph, 0)
} }
// compareNodes is used to compare two LightningNodes while excluding the // compareNodes is used to compare two LightningNodes while excluding the

File diff suppressed because it is too large Load Diff

View File

@ -1869,6 +1869,9 @@ message NetworkInfo {
int64 max_channel_size = 9 [json_name = "max_channel_size"]; int64 max_channel_size = 9 [json_name = "max_channel_size"];
int64 median_channel_size_sat = 10 [json_name = "median_channel_size_sat"]; int64 median_channel_size_sat = 10 [json_name = "median_channel_size_sat"];
// The number of edges marked as zombies.
uint64 num_zombie_chans = 11 [json_name = "num_zombie_chans"];
// TODO(roasbeef): fee rate info, expiry // TODO(roasbeef): fee rate info, expiry
// * also additional RPC for tracking fee info once in // * also additional RPC for tracking fee info once in
} }

View File

@ -2633,6 +2633,11 @@
"median_channel_size_sat": { "median_channel_size_sat": {
"type": "string", "type": "string",
"format": "int64" "format": "int64"
},
"num_zombie_chans": {
"type": "string",
"format": "uint64",
"description": "The number of edges marked as zombies."
} }
} }
}, },

View File

@ -3999,6 +3999,12 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
return nil, err return nil, err
} }
// Query the graph for the current number of zombie channels.
numZombies, err := graph.NumZombies()
if err != nil {
return nil, err
}
// Find the median. // Find the median.
medianChanSize = autopilot.Median(allChans) medianChanSize = autopilot.Median(allChans)
@ -4022,6 +4028,7 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
MinChannelSize: int64(minChannelSize), MinChannelSize: int64(minChannelSize),
MaxChannelSize: int64(maxChannelSize), MaxChannelSize: int64(maxChannelSize),
MedianChannelSizeSat: int64(medianChanSize), MedianChannelSizeSat: int64(medianChanSize),
NumZombieChans: numZombies,
} }
// 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