Skip to content

Commit 1a96816

Browse files
authored
Merge pull request #951 from evoskuil/master
Make electrum version and client name available via channel.
2 parents 49f698c + 6416ff1 commit 1a96816

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

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: 19 additions & 22 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)
@@ -72,7 +70,7 @@ void protocol_electrum_version::complete(const code& ec,
7270
// Calls after handshake completion are allowed and will skip this.
7371
if (handler_)
7472
{
75-
// shake error will result in stopped channel.
73+
// Invoke handshake completion, error will result in stopped channel.
7674
(*handler_)(shake);
7775
handler_.reset();
7876
}
@@ -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,24 +100,27 @@ 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
}
108+
109+
// Handshake must leave channel paused, before leaving stranded handler.
110+
if (handler_) pause();
110111
}
111112

112113
// Client/server names.
113114
// ----------------------------------------------------------------------------
114115

115-
std::string_view protocol_electrum_version::get_server() const NOEXCEPT
116+
std::string_view protocol_electrum_version::server_name() const NOEXCEPT
116117
{
117118
return settings().user_agent;
118119
}
119120

120-
std::string_view protocol_electrum_version::get_client() const NOEXCEPT
121+
std::string_view protocol_electrum_version::client_name() const NOEXCEPT
121122
{
122-
return name_;
123+
return channel_->client();
123124
}
124125

125126
bool protocol_electrum_version::set_client(const std::string& name) NOEXCEPT
@@ -129,11 +130,12 @@ bool protocol_electrum_version::set_client(const std::string& name) NOEXCEPT
129130
return false;
130131

131132
// Do not put to log without escaping.
132-
name_ = escape_client(name);
133+
channel_->set_client(escape_client(name));
133134
return true;
134135
}
135136

136-
std::string protocol_electrum_version::escape_client(const std::string& in) NOEXCEPT
137+
std::string protocol_electrum_version::escape_client(
138+
const std::string& in) NOEXCEPT
137139
{
138140
std::string out(in.size(), '*');
139141
std::transform(in.begin(), in.end(), out.begin(), [](char c) NOEXCEPT
@@ -148,14 +150,9 @@ std::string protocol_electrum_version::escape_client(const std::string& in) NOEX
148150
// Negotiated version.
149151
// ----------------------------------------------------------------------------
150152

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
153+
std::string_view protocol_electrum_version::negotiated_version() const NOEXCEPT
157154
{
158-
return version_to_string(version_);
155+
return version_to_string(channel_->version());
159156
}
160157

161158
bool protocol_electrum_version::set_version(const value_t& version) NOEXCEPT
@@ -171,9 +168,9 @@ bool protocol_electrum_version::set_version(const value_t& version) NOEXCEPT
171168
return false;
172169

173170
LOGA("Electrum [" << authority() << "] version ("
174-
<< version_to_string(client_max) << ") " << get_client());
171+
<< version_to_string(client_max) << ") " << client_name());
175172

176-
version_ = upper;
173+
channel_->set_version(upper);
177174
return true;
178175
}
179176

0 commit comments

Comments
 (0)