From 04c8915049547ee3082ae36c2a9b76a7c2afc329 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 21 Jun 2022 20:18:22 +0930 Subject: [PATCH] lightningd: handle old Tor version 2 addresses in db without crashing. In non-developer mode, this results in: "Could not load channels from the database" Fixes: #5238 Signed-off-by: Rusty Russell Changelog-Fixed: lightningd: fix failed startup "Could not load channels from the database" if old TORv2 addresses were present. --- .msggen.json | 3 ++- cln-grpc/proto/node.proto | 1 + cln-grpc/src/convert.rs | 1 + cln-rpc/src/model.rs | 2 ++ tests/test_misc.py | 1 - wallet/wallet.c | 9 +++++++-- 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.msggen.json b/.msggen.json index 550155b881a6..9fb35c735d4e 100644 --- a/.msggen.json +++ b/.msggen.json @@ -666,7 +666,8 @@ "ListPeers.peers[].features": 6, "ListPeers.peers[].id": 1, "ListPeers.peers[].log[]": 3, - "ListPeers.peers[].netaddr[]": 5 + "ListPeers.peers[].netaddr[]": 5, + "ListPeers.peers[].remote_addr": 7 }, "ListpeersPeersChannels": { "ListPeers.peers[].channels[].channel_id": 6, diff --git a/cln-grpc/proto/node.proto b/cln-grpc/proto/node.proto index 3b96e9d52afa..314d5d4191d1 100644 --- a/cln-grpc/proto/node.proto +++ b/cln-grpc/proto/node.proto @@ -128,6 +128,7 @@ message ListpeersPeers { repeated ListpeersPeersLog log = 3; repeated ListpeersPeersChannels channels = 4; repeated string netaddr = 5; + optional string remote_addr = 7; optional bytes features = 6; } diff --git a/cln-grpc/src/convert.rs b/cln-grpc/src/convert.rs index 2f837ed747bf..e6d33b9ab545 100644 --- a/cln-grpc/src/convert.rs +++ b/cln-grpc/src/convert.rs @@ -163,6 +163,7 @@ impl From<&responses::ListpeersPeers> for pb::ListpeersPeers { log: c.log.as_ref().map(|arr| arr.iter().map(|i| i.into()).collect()).unwrap_or(vec![]), // Rule #3 channels: c.channels.iter().map(|i| i.into()).collect(), // Rule #3 for type ListpeersPeersChannels netaddr: c.netaddr.as_ref().map(|arr| arr.iter().map(|i| i.into()).collect()).unwrap_or(vec![]), // Rule #3 + remote_addr: c.remote_addr.clone(), // Rule #2 for type string? features: c.features.as_ref().map(|v| hex::decode(&v).unwrap()), // Rule #2 for type hex? } } diff --git a/cln-rpc/src/model.rs b/cln-rpc/src/model.rs index 75e5308ec256..ceaf466f6f4d 100644 --- a/cln-rpc/src/model.rs +++ b/cln-rpc/src/model.rs @@ -1238,6 +1238,8 @@ pub mod responses { pub channels: Vec, #[serde(alias = "netaddr", skip_serializing_if = "Option::is_none")] pub netaddr: Option>, + #[serde(alias = "remote_addr", skip_serializing_if = "Option::is_none")] + pub remote_addr: Option, #[serde(alias = "features", skip_serializing_if = "Option::is_none")] pub features: Option, } diff --git a/tests/test_misc.py b/tests/test_misc.py index 65aef0b11756..9e2ba04b0805 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2607,7 +2607,6 @@ def test_datastore_keylist(node_factory): 'hex': b'ab2val2'.hex()}]} -@pytest.mark.xfail(strict=True) def test_torv2_in_db(node_factory): l1, l2 = node_factory.line_graph(2, wait_for_announce=True) diff --git a/wallet/wallet.c b/wallet/wallet.c index ff9506299b67..346ae90b27cd 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -836,10 +836,15 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) db_col_node_id(stmt, "node_id", &id); + /* This can happen for peers last seen on Torv2! */ addrstr = db_col_strdup(tmpctx, stmt, "address"); if (!parse_wireaddr_internal(addrstr, &addr, DEFAULT_PORT, - false, false, true, true, NULL)) - goto done; + false, false, true, true, NULL)) { + log_unusual(w->log, "Unparsable peer address %s: replacing", + addrstr); + parse_wireaddr_internal("127.0.0.1:1", &addr, DEFAULT_PORT, + false, false, true, true, NULL); + } /* FIXME: save incoming in db! */ peer = new_peer(w->ld, db_col_u64(stmt, "id"), &id, &addr, false);