Skip to content

Commit 2c80255

Browse files
committed
Integrate new electrum settings, two methods, tests.
1 parent 80e8fa7 commit 2c80255

9 files changed

Lines changed: 119 additions & 14 deletions

File tree

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ test_libbitcoin_server_test_SOURCES = \
8282
test/protocols/electrum/electrum.cpp \
8383
test/protocols/electrum/electrum.hpp \
8484
test/protocols/electrum/electrum_block_header.cpp \
85+
test/protocols/electrum/electrum_server.cpp \
8586
test/protocols/electrum/electrum_server_version.cpp
8687

8788
endif WITH_TESTS

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ if (with-tests)
311311
"../../test/protocols/electrum/electrum.cpp"
312312
"../../test/protocols/electrum/electrum.hpp"
313313
"../../test/protocols/electrum/electrum_block_header.cpp"
314+
"../../test/protocols/electrum/electrum_server.cpp"
314315
"../../test/protocols/electrum/electrum_server_version.cpp" )
315316
316317
add_test( NAME libbitcoin-server-test COMMAND libbitcoin-server-test

builds/msvc/vs2022/libbitcoin-server-test/libbitcoin-server-test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<ObjectFileName>$(IntDir)test_protocols_electrum_electrum.obj</ObjectFileName>
135135
</ClCompile>
136136
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_block_header.cpp" />
137+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp" />
137138
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server_version.cpp" />
138139
<ClCompile Include="..\..\..\..\test\settings.cpp" />
139140
<ClCompile Include="..\..\..\..\test\test.cpp">

builds/msvc/vs2022/libbitcoin-server-test/libbitcoin-server-test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_block_header.cpp">
6161
<Filter>src\protocols\electrum</Filter>
6262
</ClCompile>
63+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp">
64+
<Filter>src\protocols\electrum</Filter>
65+
</ClCompile>
6366
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server_version.cpp">
6467
<Filter>src\protocols\electrum</Filter>
6568
</ClCompile>

src/protocols/protocol_electrum.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,16 @@ void protocol_electrum::handle_server_banner(const code& ec,
429429
if (stopped(ec))
430430
return;
431431

432-
send_result(network_settings().user_agent, 70, BIND(complete, _1));
432+
send_result({ options().banner_message }, 42, BIND(complete, _1));
433433
}
434434

435435
void protocol_electrum::handle_server_donation_address(const code& ec,
436436
rpc_interface::server_donation_address) NOEXCEPT
437437
{
438-
if (stopped(ec)) return;
439-
send_code(error::not_implemented);
438+
if (stopped(ec))
439+
return;
440+
441+
send_result({ options().donation_address }, 42, BIND(complete, _1));
440442
}
441443

442444
void protocol_electrum::handle_server_features(const code& ec,
@@ -457,8 +459,11 @@ void protocol_electrum::handle_server_peers_subscribe(const code& ec,
457459
void protocol_electrum::handle_server_ping(const code& ec,
458460
rpc_interface::server_ping) NOEXCEPT
459461
{
460-
if (stopped(ec)) return;
461-
send_code(error::not_implemented);
462+
if (stopped(ec))
463+
return;
464+
465+
// Any receive, including ping, resets the base channel inactivity timer.
466+
send_result({ null_t{} }, 42, BIND(complete, _1));
462467
}
463468

464469
// Handlers (mempool).

src/protocols/protocol_electrum_version.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void protocol_electrum_version::handle_server_version(const code& ec,
115115

116116
std::string_view protocol_electrum_version::server_name() const NOEXCEPT
117117
{
118-
return network_settings().user_agent;
118+
return options().server_name;
119119
}
120120

121121
std::string_view protocol_electrum_version::client_name() const NOEXCEPT

test/protocols/electrum/electrum.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ electrum_setup_fixture::electrum_setup_fixture()
4444
auto& electrum = server_settings.electrum;
4545

4646
electrum.binds = { { ELECTRUM_ENDPOINT } };
47+
electrum.server_name = "server_name";
48+
electrum.banner_message = "banner_message";
49+
electrum.donation_address = "donation_address";
50+
electrum.maximum_subscriptions = 3;
4751
electrum.maximum_headers = 5;
4852
electrum.connections = 1;
4953
database_settings.interval_depth = 2;
@@ -95,7 +99,7 @@ boost::json::value electrum_setup_fixture::get(const std::string& request)
9599
catch (const boost::system::system_error& e)
96100
{
97101
BOOST_WARN_MESSAGE(false, e.what());
98-
return {};
102+
return boost::json::parse(R"({"dropped":true})");
99103
}
100104

101105
std::string response{};
@@ -113,15 +117,14 @@ bool electrum_setup_fixture::handshake(const std::string& version,
113117
) % id % name % version;
114118

115119
const auto response = get(request.str());
116-
if (!response.at("result").is_array() ||
117-
!response.at("id").is_int64() ||
118-
response.at("id").as_int64() != id)
120+
if (!response.at("id").is_int64() || response.at("id").as_int64() != id ||
121+
!response.at("result").is_array())
119122
return false;
120123

121124
// Assumes server always accepts proposed version.
122125
const auto& result = response.at("result").as_array();
123126
return (result.size() == two) &&
124127
(result.at(0).is_string() && result.at(1).is_string()) &&
125-
(result.at(0).as_string() == config().network.user_agent) &&
128+
(result.at(0).as_string() == config().server.electrum.server_name) &&
126129
(result.at(1).as_string() == version);
127130
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "../../test.hpp"
20+
#include "electrum.hpp"
21+
22+
BOOST_FIXTURE_TEST_SUITE(electrum_tests, electrum_setup_fixture)
23+
24+
// server.banner
25+
26+
using namespace system;
27+
static const code not_found{ server::error::not_found };
28+
static const code target_overflow{ server::error::target_overflow };
29+
static const code invalid_argument{ server::error::invalid_argument };
30+
31+
BOOST_AUTO_TEST_CASE(electrum__server_banner__jsonrpc_unspecified_no_aparams__dropped)
32+
{
33+
BOOST_CHECK(handshake());
34+
35+
// params[] required in json 1.0, server drops connection for invalid json-rpc.
36+
const auto response = get(R"({"id":42,"method":"server.banner"})" "\n");
37+
BOOST_CHECK(response.at("dropped").as_bool());
38+
}
39+
40+
BOOST_AUTO_TEST_CASE(electrum__server_banner__jsonrpc_unspecified_named_aparams__dropped)
41+
{
42+
BOOST_CHECK(handshake());
43+
44+
// params{} disallowed in json 1.0, server drops connection for invalid json-rpc.
45+
const auto response = get(R"({"id":42,"method":"server.banner","params":{}})" "\n");
46+
BOOST_CHECK(response.at("dropped").as_bool());
47+
}
48+
49+
BOOST_AUTO_TEST_CASE(electrum__server_banner__jsonrpc_unspecified_empty_params__expected)
50+
{
51+
BOOST_CHECK(handshake());
52+
53+
const auto response = get(R"({"id":42,"method":"server.banner","params":[]})" "\n");
54+
BOOST_CHECK_EQUAL(response.at("result").as_string(), "banner_message");
55+
}
56+
57+
BOOST_AUTO_TEST_CASE(electrum__server_banner__jsonrpc_1__expected)
58+
{
59+
BOOST_CHECK(handshake());
60+
61+
const auto response = get(R"({"jsonrpc":"1.0","id":42,"method":"server.banner","params":[]})" "\n");
62+
BOOST_CHECK_EQUAL(response.at("result").as_string(), "banner_message");
63+
}
64+
65+
BOOST_AUTO_TEST_CASE(electrum__server_banner__jsonrpc_2__expected)
66+
{
67+
BOOST_CHECK(handshake());
68+
69+
const auto response = get(R"({"jsonrpc":"2.0","id":42,"method":"server.banner"})" "\n");
70+
BOOST_CHECK_EQUAL(response.at("result").as_string(), "banner_message");
71+
}
72+
73+
// server.donation_address
74+
75+
BOOST_AUTO_TEST_CASE(electrum__server_donation_address__jsonrpc_1__expected)
76+
{
77+
BOOST_CHECK(handshake());
78+
79+
const auto response = get(R"({"jsonrpc":"1.0","id":43,"method":"server.donation_address","params":[]})" "\n");
80+
BOOST_CHECK_EQUAL(response.at("result").as_string(), "donation_address");
81+
}
82+
83+
BOOST_AUTO_TEST_CASE(electrum__server_donation_address__jsonrpc_2__expected)
84+
{
85+
BOOST_CHECK(handshake());
86+
87+
const auto response = get(R"({"jsonrpc":"2.0","id":43,"method":"server.donation_address"})" "\n");
88+
BOOST_CHECK_EQUAL(response.at("result").as_string(), "donation_address");
89+
}
90+
91+
BOOST_AUTO_TEST_SUITE_END()

test/protocols/electrum/electrum_server_version.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(electrum__server_version__default__expected)
3535
BOOST_CHECK_EQUAL(result.size(), 2u);
3636
BOOST_CHECK(result.at(0).is_string());
3737
BOOST_CHECK(result.at(1).is_string());
38-
BOOST_CHECK_EQUAL(result.at(0).as_string(), config().network.user_agent);
38+
BOOST_CHECK_EQUAL(result.at(0).as_string(), "server_name");
3939
BOOST_CHECK_EQUAL(result.at(1).as_string(), "1.4");
4040
}
4141

@@ -49,7 +49,7 @@ BOOST_AUTO_TEST_CASE(electrum__server_version__minimum__expected)
4949
BOOST_CHECK_EQUAL(result.size(), 2u);
5050
BOOST_CHECK(result.at(0).is_string());
5151
BOOST_CHECK(result.at(1).is_string());
52-
BOOST_CHECK_EQUAL(result.at(0).as_string(), config().network.user_agent);
52+
BOOST_CHECK_EQUAL(result.at(0).as_string(), "server_name");
5353
BOOST_CHECK_EQUAL(result.at(1).as_string(), "1.4");
5454
}
5555

@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(electrum__server_version__maximum__expected)
6363
BOOST_CHECK_EQUAL(result.size(), 2u);
6464
BOOST_CHECK(result.at(0).is_string());
6565
BOOST_CHECK(result.at(1).is_string());
66-
BOOST_CHECK_EQUAL(result.at(0).as_string(), config().network.user_agent);
66+
BOOST_CHECK_EQUAL(result.at(0).as_string(), "server_name");
6767
BOOST_CHECK_EQUAL(result.at(1).as_string(), "1.4.2");
6868
}
6969

0 commit comments

Comments
 (0)