Skip to content

Commit fa58d37

Browse files
committed
Make electrum version and client name available via channel.
1 parent 49f698c commit fa58d37

4 files changed

Lines changed: 65 additions & 35 deletions

File tree

include/bitcoin/node/channels/channel_electrum.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <bitcoin/node/configuration.hpp>
2525
#include <bitcoin/node/define.hpp>
2626
#include <bitcoin/node/interfaces/interfaces.hpp>
27+
#include <bitcoin/node/parsers/parsers.hpp>
2728

2829
namespace libbitcoin {
2930
namespace node {
@@ -51,6 +52,34 @@ class BCN_API channel_electrum
5152
network::tracker<channel_electrum>(log)
5253
{
5354
}
55+
56+
/// Properties.
57+
/// -----------------------------------------------------------------------
58+
59+
inline void set_client(const std::string& name) NOEXCEPT
60+
{
61+
name_ = name;
62+
}
63+
64+
inline const std::string& client() const NOEXCEPT
65+
{
66+
return name_;
67+
}
68+
69+
inline void set_version(electrum_version version) NOEXCEPT
70+
{
71+
version_ = version;
72+
}
73+
74+
inline electrum_version version() const NOEXCEPT
75+
{
76+
return version_;
77+
}
78+
79+
private:
80+
// These are protected by strand.
81+
electrum_version version_{ electrum_version::v0_0 };
82+
std::string name_{};
5483
};
5584

5685
} // namespace node

include/bitcoin/node/protocols/protocol_electrum.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class BCN_API protocol_electrum
4242
const network::channel::ptr& channel,
4343
const options_t& options) NOEXCEPT
4444
: node::protocol_rpc<channel_electrum>(session, channel, options),
45+
channel_(std::dynamic_pointer_cast<channel_t>(channel)),
4546
network::tracker<protocol_electrum>(session->log)
4647
{
4748
}
@@ -116,10 +117,14 @@ class BCN_API protocol_electrum
116117
rpc_interface::mempool_get_fee_histogram) NOEXCEPT;
117118

118119
protected:
119-
////bool is_version(protocol_version version) const NOEXCEPT
120-
////{
121-
//// return version_ >= version;
122-
////}
120+
inline bool is_version(electrum_version version) const NOEXCEPT
121+
{
122+
return channel_->version() >= version;
123+
}
124+
125+
private:
126+
// This is mostly thread safe, and used in a thread safe manner.
127+
const channel_t::ptr channel_;
123128
};
124129

125130
} // namespace node

include/bitcoin/node/protocols/protocol_electrum_version.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class BCN_API protocol_electrum_version
4242
const network::channel::ptr& channel,
4343
const options_t& options) NOEXCEPT
4444
: node::protocol_rpc<channel_electrum>(session, channel, options),
45+
channel_(std::dynamic_pointer_cast<channel_t>(channel)),
4546
network::tracker<protocol_electrum_version>(session->log)
4647
{
4748
}
@@ -50,22 +51,22 @@ class BCN_API protocol_electrum_version
5051
virtual void complete(const code& ec, const code& shake) NOEXCEPT;
5152

5253
protected:
54+
static constexpr electrum_version minimum = electrum_version::v1_4;
55+
static constexpr electrum_version maximum = electrum_version::v1_4_2;
56+
static constexpr size_t max_client_name_length = 1024;
57+
5358
void handle_server_version(const code& ec,
5459
rpc_interface::server_version, const std::string& client_name,
5560
const interface::value_t& protocol_version) NOEXCEPT;
5661

57-
protected:
58-
static constexpr electrum_version minimum = electrum_version::v1_4;
59-
static constexpr electrum_version maximum = electrum_version::v1_4_2;
60-
6162
electrum_version version() const NOEXCEPT;
62-
std::string_view get_version() const NOEXCEPT;
63+
std::string_view negotiated_version() const NOEXCEPT;
6364
bool set_version(const interface::value_t& version) NOEXCEPT;
6465
bool get_versions(electrum_version& min, electrum_version& max,
6566
const interface::value_t& version) NOEXCEPT;
6667

67-
std::string_view get_server() const NOEXCEPT;
68-
std::string_view get_client() const NOEXCEPT;
68+
std::string_view server_name() const NOEXCEPT;
69+
std::string_view client_name() const NOEXCEPT;
6970
std::string escape_client(const std::string& in) NOEXCEPT;
7071
bool set_client(const std::string& name) NOEXCEPT;
7172

@@ -75,10 +76,11 @@ class BCN_API protocol_electrum_version
7576
static electrum_version version_from_string(
7677
const std::string_view& version) NOEXCEPT;
7778

78-
// These are protected by strand.
79+
// This is mostly thread safe, and used in a thread safe manner.
80+
const channel_t::ptr channel_;
81+
82+
// This is protected by strand.
7983
std::shared_ptr<network::result_handler> handler_{};
80-
electrum_version version_{ electrum_version::v0_0 };
81-
std::string name_{};
8284
};
8385

8486
} // namespace node

src/protocols/protocol_electrum_version.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ using namespace network;
3434
using namespace interface;
3535
using namespace std::placeholders;
3636

37-
constexpr auto max_client_name_length = 1024u;
38-
3937
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
4038
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
4139
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
@@ -84,14 +82,14 @@ void protocol_electrum_version::complete(const code& ec,
8482
// Changed in version 1.6: server must tolerate and ignore extraneous args.
8583
void protocol_electrum_version::handle_server_version(const code& ec,
8684
rpc_interface::server_version, const std::string& client_name,
87-
const value_t& electrum_version) NOEXCEPT
85+
const value_t& protocol_version) NOEXCEPT
8886
{
8987
if (stopped(ec))
9088
return;
9189

9290
// v0_0 implies version has not been set (first call).
93-
if ((version() == electrum_version::v0_0) &&
94-
(!set_client(client_name) || !set_version(electrum_version)))
91+
if ((channel_->version() == electrum_version::v0_0) &&
92+
(!set_client(client_name) || !set_version(protocol_version)))
9593
{
9694
const auto reason = error::invalid_argument;
9795
send_code(reason, BIND(complete, _1, reason));
@@ -102,8 +100,8 @@ void protocol_electrum_version::handle_server_version(const code& ec,
102100
{
103101
array_t
104102
{
105-
{ string_t{ get_server() } },
106-
{ string_t{ get_version() } }
103+
{ string_t{ server_name() } },
104+
{ string_t{ negotiated_version() } }
107105
}
108106
}, 70, BIND(complete, _1, error::success));
109107
}
@@ -112,14 +110,14 @@ void protocol_electrum_version::handle_server_version(const code& ec,
112110
// Client/server names.
113111
// ----------------------------------------------------------------------------
114112

115-
std::string_view protocol_electrum_version::get_server() const NOEXCEPT
113+
std::string_view protocol_electrum_version::server_name() const NOEXCEPT
116114
{
117115
return settings().user_agent;
118116
}
119117

120-
std::string_view protocol_electrum_version::get_client() const NOEXCEPT
118+
std::string_view protocol_electrum_version::client_name() const NOEXCEPT
121119
{
122-
return name_;
120+
return channel_->client();
123121
}
124122

125123
bool protocol_electrum_version::set_client(const std::string& name) NOEXCEPT
@@ -129,11 +127,12 @@ bool protocol_electrum_version::set_client(const std::string& name) NOEXCEPT
129127
return false;
130128

131129
// Do not put to log without escaping.
132-
name_ = escape_client(name);
130+
channel_->set_client(escape_client(name));
133131
return true;
134132
}
135133

136-
std::string protocol_electrum_version::escape_client(const std::string& in) NOEXCEPT
134+
std::string protocol_electrum_version::escape_client(
135+
const std::string& in) NOEXCEPT
137136
{
138137
std::string out(in.size(), '*');
139138
std::transform(in.begin(), in.end(), out.begin(), [](char c) NOEXCEPT
@@ -148,14 +147,9 @@ std::string protocol_electrum_version::escape_client(const std::string& in) NOEX
148147
// Negotiated version.
149148
// ----------------------------------------------------------------------------
150149

151-
electrum_version protocol_electrum_version::version() const NOEXCEPT
152-
{
153-
return version_;
154-
}
155-
156-
std::string_view protocol_electrum_version::get_version() const NOEXCEPT
150+
std::string_view protocol_electrum_version::negotiated_version() const NOEXCEPT
157151
{
158-
return version_to_string(version_);
152+
return version_to_string(channel_->version());
159153
}
160154

161155
bool protocol_electrum_version::set_version(const value_t& version) NOEXCEPT
@@ -171,9 +165,9 @@ bool protocol_electrum_version::set_version(const value_t& version) NOEXCEPT
171165
return false;
172166

173167
LOGA("Electrum [" << authority() << "] version ("
174-
<< version_to_string(client_max) << ") " << get_client());
168+
<< version_to_string(client_max) << ") " << client_name());
175169

176-
version_ = upper;
170+
channel_->set_version(upper);
177171
return true;
178172
}
179173

0 commit comments

Comments
 (0)