Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listpeers: add num_channels #5968

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@
"ListPeers.peers[].id": 1,
"ListPeers.peers[].log[]": 3,
"ListPeers.peers[].netaddr[]": 5,
"ListPeers.peers[].num_channels": 8,
"ListPeers.peers[].remote_addr": 7
},
"ListpeersPeersChannels": {
Expand Down
1 change: 1 addition & 0 deletions cln-grpc/proto/node.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cln-grpc/src/convert.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cln-rpc/src/model.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/pyln-testing/pyln/testing/grpc2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def listpeers_peers2py(m):
return remove_default({
"id": hexlify(m.id), # PrimitiveField in generate_composite
"connected": m.connected, # PrimitiveField in generate_composite
"num_channels": m.num_channels, # PrimitiveField in generate_composite
"log": [listpeers_peers_log2py(i) for i in m.log], # ArrayField[composite] in generate_composite
"channels": [listpeers_peers_channels2py(i) for i in m.channels], # ArrayField[composite] in generate_composite
"netaddr": [m.netaddr for i in m.netaddr], # ArrayField[primitive] in generate_composite
Expand Down
3 changes: 2 additions & 1 deletion doc/lightning-listpeers.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ On success, an object containing **peers** is returned. It is an array of objec

- **id** (pubkey): the public key of the peer
- **connected** (boolean): True if the peer is currently connected
- **num\_channels** (u32): The number of channels the peer has with this node *(added v23.02)*
- **log** (array of objects, optional): if *level* is specified, logs for this peer:
- **type** (string) (one of "SKIPPED", "BROKEN", "UNUSUAL", "INFO", "DEBUG", "IO\_IN", "IO\_OUT")

Expand Down Expand Up @@ -399,4 +400,4 @@ Main web site: <https://github.com/ElementsProject/lightning> Lightning
RFC site (BOLT \#9):
<https://github.com/lightning/bolts/blob/master/09-features.md>

[comment]: # ( SHA256STAMP:b89450ac6f27e051003bcf0a382b51e117e2832729e9d80b0015d9cebfacfa2c)
[comment]: # ( SHA256STAMP:227b5af94d1f299a4e88e450c074960ca8d109b634e24693ad389ef02f64f525)
3 changes: 2 additions & 1 deletion doc/lightning-sql.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ The following tables are currently supported:
- `peers` indexed by `id` (see lightning-listpeers(7))
- `id` (type `pubkey`, sqltype `BLOB`)
- `connected` (type `boolean`, sqltype `INTEGER`)
- `num_channels` (type `u32`, sqltype `INTEGER`)
- related table `peers_netaddr`
- `row` (reference to `peers.rowid`, sqltype `INTEGER`)
- `arrindex` (index within array, sqltype `INTEGER`)
Expand Down Expand Up @@ -471,4 +472,4 @@ RESOURCES
---------

Main web site: <https://github.com/ElementsProject/lightning>
[comment]: # ( SHA256STAMP:dbb9286cf31dc82b33143d5274b1c4eecc75c5ba1dfc18bdf21b4baab585bd45)
[comment]: # ( SHA256STAMP:d25af4b0655ebd31db68932c5ea6b532bd134477e42df5d0c7428e4a03fd0335)
9 changes: 8 additions & 1 deletion doc/schemas/listpeers.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"additionalProperties": true,
"required": [
"id",
"connected"
"connected",
"num_channels"
],
"properties": {
"id": {
Expand All @@ -24,6 +25,11 @@
"type": "boolean",
"description": "True if the peer is currently connected"
},
"num_channels": {
"type": "u32",
"description": "The number of channels the peer has with this node",
"added": "v23.02"
},
"log": {
"type": "array",
"description": "if *level* is specified, logs for this peer",
Expand Down Expand Up @@ -1091,6 +1097,7 @@
"id": {},
"channels": {},
"connected": {},
"num_channels": {},
"htlcs": {},
"log": {},
"netaddr": {
Expand Down
6 changes: 5 additions & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1932,11 +1932,16 @@ static void json_add_peer(struct lightningd *ld,
const enum log_level *ll)
{
struct channel *channel;
u32 num_channels;

json_object_start(response, NULL);
json_add_node_id(response, "id", &p->id);

json_add_bool(response, "connected", p->connected == PEER_CONNECTED);
num_channels = 0;
list_for_each(&p->channels, channel, list)
num_channels++;
json_add_num(response, "num_channels", num_channels);

/* If it's not connected, features are unreliable: we don't
* store them in the database, and they would only reflect
Expand All @@ -1954,7 +1959,6 @@ static void json_add_peer(struct lightningd *ld,
fmt_wireaddr(response, p->remote_addr));
json_add_hex_talarr(response, "features", p->their_features);
}

if (deprecated_apis) {
json_array_start(response, "channels");
json_add_uncommitted_channel(response, p->uncommitted_channel, NULL);
Expand Down
29 changes: 19 additions & 10 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@

def test_connect_basic(node_factory):
l1, l2 = node_factory.line_graph(2, fundchannel=False)
l1id = l1.info['id']
l2id = l2.info['id']

# These should be in openingd.
assert l1.rpc.getpeer(l2.info['id'])['connected']
assert l2.rpc.getpeer(l1.info['id'])['connected']
assert len(l1.rpc.listpeerchannels(l2.info['id'])['channels']) == 0
assert len(l2.rpc.listpeerchannels(l1.info['id'])['channels']) == 0
assert l1.rpc.getpeer(l2id)['connected']
assert l2.rpc.getpeer(l1id)['connected']
assert len(l1.rpc.listpeerchannels(l2id)['channels']) == 0
assert len(l2.rpc.listpeerchannels(l1id)['channels']) == 0

# Reconnect should be a noop
ret = l1.rpc.connect(l2.info['id'], 'localhost', port=l2.port)
assert ret['id'] == l2.info['id']
ret = l1.rpc.connect(l2id, 'localhost', port=l2.port)
assert ret['id'] == l2id
assert ret['address'] == {'type': 'ipv4', 'address': '127.0.0.1', 'port': l2.port}

ret = l2.rpc.connect(l1.info['id'], host='localhost', port=l1.port)
assert ret['id'] == l1.info['id']
ret = l2.rpc.connect(l1id, host='localhost', port=l1.port)
assert ret['id'] == l1id
# FIXME: This gives a bogus address (since they connected to us): better to give none!
assert 'address' in ret

# Should still only have one peer!
assert len(l1.rpc.listpeers()) == 1
assert len(l2.rpc.listpeers()) == 1
assert len(l1.rpc.listpeers()['peers']) == 1
assert len(l2.rpc.listpeers()['peers']) == 1
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved

# Should get reasonable error if unknown addr for peer.
with pytest.raises(RpcError, match=r'Unable to connect, no address known'):
Expand All @@ -58,6 +60,13 @@ def test_connect_basic(node_factory):
with pytest.raises(RpcError, match=r'Cryptographic handshake: peer closed connection \(wrong key\?\)'):
l1.rpc.connect('032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e', 'localhost', l2.port)

# test new `num_channels` param
assert l1.rpc.listpeers(l2id)['peers'][0]['num_channels'] == 0
l1.fundchannel(l2)
assert l1.rpc.listpeers(l2id)['peers'][0]['num_channels'] == 1
l1.fundchannel(l2)
assert l1.rpc.listpeers(l2id)['peers'][0]['num_channels'] == 2


@pytest.mark.developer("needs DEVELOPER=1 for fast gossip and --dev-allow-localhost for local remote_addr")
def test_remote_addr(node_factory, bitcoind):
Expand Down