Skip to content

Commit 6b871ae

Browse files
committed
Retry connecting to bitcoin core if not available
In some setups, namely when deploying sv2-tp alongside an unsynced bitcoin node, we want to wait for the node to fully sync before we are able to connect to it. Otherwise the deployment process would be two steps instead of one. This commit adds a retry mechanism when connection to a bitcoin core node every 10 seconds.
1 parent 0f934df commit 6b871ae

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/sv2-tp.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,24 @@ MAIN_FUNCTION
160160
options.fee_check_interval = std::chrono::seconds(args.GetIntArg("-sv2interval", 0));
161161
}
162162

163-
// Connect to existing bitcoin-node process or spawn new one.
163+
// Connect to bitcoin-node via IPC
164+
//
165+
// If the node is not available, keep retrying in a loop every 10 seconds.
164166
std::unique_ptr<interfaces::Init> mine_init{interfaces::MakeBasicInit("sv2-tp", argc > 0 ? argv[0] : "")};
165167
assert(mine_init);
166168
std::unique_ptr<interfaces::Init> node_init;
167-
try {
168-
std::string address{args.GetArg("-ipcconnect", "unix")};
169-
node_init = mine_init->ipc()->connectAddress(address);
170-
} catch (const std::exception& exception) {
171-
tfm::format(std::cerr, "Error: %s\n", exception.what());
172-
tfm::format(std::cerr, "Probably bitcoin-node is not running or not listening on a unix socket. Can be started with:\n\n");
173-
tfm::format(std::cerr, " bitcoin-node -chain=%s -ipcbind=unix\n", args.GetChainTypeString());
174-
return EXIT_FAILURE;
169+
std::string address{args.GetArg("-ipcconnect", "unix")};
170+
while (true) {
171+
try {
172+
node_init = mine_init->ipc()->connectAddress(address);
173+
break; // Success: break out of the loop
174+
} catch (const std::exception& exception) {
175+
tfm::format(std::cerr, "IPC connection failed: %s\n", exception.what());
176+
tfm::format(std::cerr, "bitcoin-node might not be running or listening on a UNIX socket.\n");
177+
tfm::format(std::cerr, "Retrying in 10 seconds...\n\n");
178+
179+
std::this_thread::sleep_for(std::chrono::seconds(10));
180+
}
175181
}
176182
assert(node_init);
177183
tfm::format(std::cout, "Connected to bitcoin-node\n");

0 commit comments

Comments
 (0)