Skip to content

Commit 835005f

Browse files
committed
Update network/server type selection code in connect().
- Don't crash when a blank/invalid network is provided (due to throwing an uncaught pointer resulting in terminate()) - Remove redundant checks/asserts - Don't make a full copy of the network JSON or a merge update - Move "state_dir" hack to session_impl to be shared for spv - Throw a human readable error if the network is incorrect
1 parent 96f87f4 commit 835005f

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

src/network_parameters.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "assertion.hpp"
44
#include "boost_wrapper.hpp"
55
#include "containers.hpp"
6+
#include "exception.hpp"
67
#include "network_parameters.hpp"
78

89
// TODO: Use std::string_view when its fully supported
@@ -534,7 +535,9 @@ namespace sdk {
534535
std::unique_lock<std::mutex> l{ registered_networks_mutex };
535536

536537
const auto p = registered_networks.find(name);
537-
GDK_RUNTIME_ASSERT_MSG(p != registered_networks.end(), "Unknown network");
538+
if (p == registered_networks.end()) {
539+
throw user_error("Unknown network");
540+
}
538541
return *p->second;
539542
}
540543

src/session.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,43 +123,23 @@ namespace sdk {
123123
{
124124
try {
125125
GDK_RUNTIME_ASSERT_MSG(init_done, "You must call GA_init first");
126+
GDK_RUNTIME_ASSERT_MSG(!get_impl(), "session already connected");
126127

127-
auto impl = get_impl();
128-
GDK_RUNTIME_ASSERT_MSG(!impl, "session already connected");
128+
const auto network = network_parameters::get(net_params.value("name", std::string()));
129+
const auto type = net_params.value("server_type", network.value("server_type", std::string()));
129130

130131
session_ptr session_p;
131-
const auto list = ga::sdk::network_parameters::get_all();
132-
const auto network_name = net_params.value("name", "");
133132

134-
if (network_name == "") {
135-
GDK_LOG_SEV(log_level::error) << "network name not provided";
136-
throw new std::runtime_error("network name not provided");
137-
}
138-
139-
auto network = list.at(network_name);
140-
141-
if (network == nullptr) {
142-
GDK_LOG_SEV(log_level::error) << "network not found: " << network_name;
143-
throw new std::runtime_error("network not found");
144-
}
145-
146-
// merge with net_params
147-
network.update(net_params.begin(), net_params.end());
148-
149-
GDK_RUNTIME_ASSERT_MSG(network.contains("server_type"), "server_type field missing");
150-
if (network.value("server_type", "") == "green") {
151-
session_p = boost::make_shared<ga_session>(network);
133+
if (type == "green") {
134+
session_p = boost::make_shared<ga_session>(net_params);
152135
#ifdef BUILD_GDK_RUST
153-
} else if (network.value("server_type", "") == "electrum") {
154-
GDK_RUNTIME_ASSERT(!gdk_config().at("datadir").empty());
155-
network["state_dir"] = std::string(gdk_config().at("datadir")) + "/state";
156-
session_p = boost::make_shared<ga_rust>(network);
136+
} else if (type == "electrum") {
137+
session_p = boost::make_shared<ga_rust>(net_params);
157138
#endif
158139
} else {
159140
GDK_RUNTIME_ASSERT_MSG(false, "server_type field unknown value");
160141
}
161142

162-
GDK_RUNTIME_ASSERT(session_p != nullptr);
163143
boost::weak_ptr<session_impl> weak_session = session_p;
164144
session_p->set_ping_fail_handler([weak_session] {
165145
if (auto p = weak_session.lock()) {

src/session_impl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "session_impl.hpp"
22
#include "exception.hpp"
33
#include "logging.hpp"
4+
#include "session.hpp"
45

56
namespace ga {
67
namespace sdk {
@@ -26,7 +27,10 @@ namespace sdk {
2627
set_override(ret, "spv_enabled", user_params, false);
2728
set_override(ret, "use_tor", user_params, false);
2829
set_override(ret, "user_agent", user_params, std::string());
29-
30+
// FIXME: Remove this by fetching it directly where needed
31+
const std::string datadir = gdk_config().value("datadir", std::string());
32+
GDK_RUNTIME_ASSERT(!datadir.empty());
33+
ret["state_dir"] = datadir + "/state";
3034
return network_parameters{ ret };
3135
}
3236

0 commit comments

Comments
 (0)