Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ test_libbitcoin_server_test_SOURCES = \
test/protocols/blocks.hpp \
test/protocols/electrum/electrum.cpp \
test/protocols/electrum/electrum.hpp \
test/protocols/electrum/electrum_block_header.cpp \
test/protocols/electrum/electrum_addresses.cpp \
test/protocols/electrum/electrum_fees.cpp \
test/protocols/electrum/electrum_headers.cpp \
test/protocols/electrum/electrum_server.cpp \
test/protocols/electrum/electrum_server_version.cpp
test/protocols/electrum/electrum_server_version.cpp \
test/protocols/electrum/electrum_transactions.cpp

endif WITH_TESTS

Expand Down
7 changes: 5 additions & 2 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ if (with-tests)
"../../test/protocols/blocks.hpp"
"../../test/protocols/electrum/electrum.cpp"
"../../test/protocols/electrum/electrum.hpp"
"../../test/protocols/electrum/electrum_block_header.cpp"
"../../test/protocols/electrum/electrum_addresses.cpp"
"../../test/protocols/electrum/electrum_fees.cpp"
"../../test/protocols/electrum/electrum_headers.cpp"
"../../test/protocols/electrum/electrum_server.cpp"
"../../test/protocols/electrum/electrum_server_version.cpp" )
"../../test/protocols/electrum/electrum_server_version.cpp"
"../../test/protocols/electrum/electrum_transactions.cpp" )

add_test( NAME libbitcoin-server-test COMMAND libbitcoin-server-test
--run_test=*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum.cpp">
<ObjectFileName>$(IntDir)test_protocols_electrum_electrum.obj</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_block_header.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_addresses.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_fees.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_headers.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server_version.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_transactions.cpp" />
<ClCompile Include="..\..\..\..\test\settings.cpp" />
<ClCompile Include="..\..\..\..\test\test.cpp">
<ObjectFileName>$(IntDir)test_test.obj</ObjectFileName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_block_header.cpp">
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_addresses.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_fees.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_headers.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp">
Expand All @@ -66,6 +72,9 @@
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server_version.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_transactions.cpp">
<Filter>src\protocols\electrum</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\settings.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/server/channels/channel_electrum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class BCS_API channel_electrum
return name_;
}

inline void set_version(electrum::version version) NOEXCEPT
inline void set_version(server::electrum::version version) NOEXCEPT
{
version_ = version;
}

inline electrum::version version() const NOEXCEPT
inline server::electrum::version version() const NOEXCEPT
{
return version_;
}
Expand All @@ -85,7 +85,7 @@ class BCS_API channel_electrum
const options_t& options_;

// These are protected by strand.
electrum::version version_{ electrum::version::v0_0 };
server::electrum::version version_{ server::electrum::version::v0_0 };
std::string name_{};
};

Expand Down
8 changes: 6 additions & 2 deletions include/bitcoin/server/protocols/protocol_electrum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ class BCS_API protocol_electrum
void blockchain_block_headers(size_t starting, size_t quantity,
size_t waypoint, bool multiplicity) NOEXCEPT;

inline bool is_version(electrum::version version) const NOEXCEPT
/// Notify client of new header.
void do_header(node::header_t link) NOEXCEPT;

inline bool is_version(server::electrum::version version) const NOEXCEPT
{
return channel_->version() >= version;
}
Expand All @@ -139,8 +142,9 @@ class BCS_API protocol_electrum
}

private:
// This is thread safe.
// These are thread safe.
const options_t& options_;
std::atomic_bool subscribed_{};

// This is mostly thread safe, and used in a thread safe manner.
const channel_t::ptr channel_;
Expand Down
68 changes: 48 additions & 20 deletions src/protocols/protocol_electrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,22 @@ void protocol_electrum::stopping(const code& ec) NOEXCEPT
// ----------------------------------------------------------------------------

bool protocol_electrum::handle_event(const code&, node::chase event_,
node::event_value) NOEXCEPT
node::event_value value) NOEXCEPT
{
// Do not pass ec to stopped as it is not a call status.
if (stopped())
return false;

switch (event_)
{
case node::chase::suspend:
case node::chase::organized:
{
if (subscribed_.load(std::memory_order_relaxed))
{
BC_ASSERT(std::holds_alternative<node::header_t>(value));
POST(do_header, std::get<node::header_t>(value));
}

break;
}
default:
Expand Down Expand Up @@ -297,19 +303,39 @@ void protocol_electrum::handle_blockchain_headers_subscribe(const code& ec,
return;
}

// TODO: signal header subscription.
subscribed_.store(true, std::memory_order_relaxed);
send_result(
{
object_t
{
{ "height", uint64_t{ top } },
{ "hex", to_hex(*header, chain::header::serialized_size()) }
}
}, 256, BIND(complete, _1));
}

// TODO: idempotent subscribe to chase::organized via session/chaser/node.
// TODO: upon notification send just the header notified by the link.
// TODO: it is client responsibility to deal with reorgs and race gaps.
send_result(value_t
void protocol_electrum::do_header(node::header_t link) NOEXCEPT
{
BC_ASSERT(stranded());

const auto& query = archive();
const auto height = query.get_height(link);
const auto header = query.get_header(link);

if (height.is_terminal() || !header)
{
LOGF("Electrum::do_header, object not found (" << link << ").");
return;
}

send_notification("blockchain.headers.subscribe",
{
object_t
{
object_t
{
{ "height", uint64_t{ top } },
{ "hex", to_hex(*header, chain::header::serialized_size()) }
}
}, 256, BIND(complete, _1));
{ "height", height.value },
{ "hex", to_hex(*header, chain::header::serialized_size()) }
}
}, 100, BIND(complete, _1));
}

void protocol_electrum::handle_blockchain_estimate_fee(const code& ec,
Expand Down Expand Up @@ -381,6 +407,8 @@ void protocol_electrum::handle_blockchain_scripthash_unsubscribe(const code& ec,
send_code(error::not_implemented);
}

// TODO: requires tx pool in order to validate against unconfirmed txs.
// TODO: requires that p2p channels subscribe to transaction broadcast.
void protocol_electrum::handle_blockchain_transaction_broadcast(const code& ec,
rpc_interface::blockchain_transaction_broadcast,
const std::string& ) NOEXCEPT
Expand Down Expand Up @@ -477,14 +505,14 @@ void protocol_electrum::handle_mempool_get_fee_histogram(const code& ec,

// TODO: requires tx pool metadata graph.
send_result(value_t
{
array_t
{
array_t
{
array_t{ 1, 1024 },
array_t{ 2, 2048 },
array_t{ 4, 4096 }
}
}, 256, BIND(complete, _1));
array_t{ 1, 1024 },
array_t{ 2, 2048 },
array_t{ 4, 4096 }
}
}, 256, BIND(complete, _1));
}

BC_POP_WARNING()
Expand Down
2 changes: 1 addition & 1 deletion test/protocols/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const chain::block block5{ block5_data, true };
const chain::block block6{ block6_data, true };
const chain::block block7{ block7_data, true };
const chain::block block8{ block8_data, true };
const chain::block block9{ block8_data, true };
const chain::block block9{ block9_data, true };

const server::settings::embedded_pages admin{};
const server::settings::embedded_pages native{};
Expand Down
3 changes: 2 additions & 1 deletion test/protocols/electrum/electrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ electrum_setup_fixture::electrum_setup_fixture()
electrum.connections = 1;
database_settings.interval_depth = 2;
node_settings.delay_inbound = false;
node_settings.minimum_fee_rate = 99.0;
network_settings.inbound.connections = 0;
network_settings.outbound.connections = 0;
auto ec = store_.create([](auto, auto) {});
Expand Down Expand Up @@ -127,4 +128,4 @@ bool electrum_setup_fixture::handshake(const std::string& version,
(result.at(0).is_string() && result.at(1).is_string()) &&
(result.at(0).as_string() == config().server.electrum.server_name) &&
(result.at(1).as_string() == version);
}
}
24 changes: 24 additions & 0 deletions test/protocols/electrum/electrum_addresses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../../test.hpp"
#include "electrum.hpp"

BOOST_FIXTURE_TEST_SUITE(electrum_tests, electrum_setup_fixture)

BOOST_AUTO_TEST_SUITE_END()
Loading
Loading