Skip to content

Commit 16ff382

Browse files
authored
Merge pull request #677 from evoskuil/master
Electrum endpoint work in progress.
2 parents 9df6265 + 7032ffb commit 16ff382

25 files changed

Lines changed: 895 additions & 139 deletions

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ src_libbitcoin_server_la_SOURCES = \
5757
src/protocols/electrum/protocol_electrum_fees.cpp \
5858
src/protocols/electrum/protocol_electrum_headers.cpp \
5959
src/protocols/electrum/protocol_electrum_mempool.cpp \
60+
src/protocols/electrum/protocol_electrum_scripthash.cpp \
6061
src/protocols/electrum/protocol_electrum_server.cpp \
6162
src/protocols/electrum/protocol_electrum_transactions.cpp \
6263
src/protocols/electrum/protocol_electrum_version.cpp
@@ -91,6 +92,7 @@ test_libbitcoin_server_test_SOURCES = \
9192
test/protocols/electrum/electrum_fees.cpp \
9293
test/protocols/electrum/electrum_headers.cpp \
9394
test/protocols/electrum/electrum_mempool.cpp \
95+
test/protocols/electrum/electrum_scripthash.cpp \
9496
test/protocols/electrum/electrum_server.cpp \
9597
test/protocols/electrum/electrum_transactions.cpp \
9698
test/protocols/electrum/electrum_version.cpp

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_fees.cpp" />
140140
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_headers.cpp" />
141141
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_mempool.cpp" />
142+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_scripthash.cpp" />
142143
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp" />
143144
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_transactions.cpp" />
144145
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_version.cpp">
@@ -190,4 +191,4 @@
190191
<ItemGroup>
191192
<Natvis Include="..\..\debug.natvis" />
192193
</ItemGroup>
193-
</Project>
194+
</Project>

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
@@ -66,6 +66,9 @@
6666
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_mempool.cpp">
6767
<Filter>src\protocols\electrum</Filter>
6868
</ClCompile>
69+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_scripthash.cpp">
70+
<Filter>src\protocols\electrum</Filter>
71+
</ClCompile>
6972
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp">
7073
<Filter>src\protocols\electrum</Filter>
7174
</ClCompile>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_fees.cpp" />
136136
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_headers.cpp" />
137137
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_mempool.cpp" />
138+
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scripthash.cpp" />
138139
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_server.cpp" />
139140
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_transactions.cpp" />
140141
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_version.cpp" />

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_mempool.cpp">
9797
<Filter>src\protocols\electrum</Filter>
9898
</ClCompile>
99+
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scripthash.cpp">
100+
<Filter>src\protocols\electrum</Filter>
101+
</ClCompile>
99102
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_server.cpp">
100103
<Filter>src\protocols\electrum</Filter>
101104
</ClCompile>

include/bitcoin/server/interfaces/electrum.hpp

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,34 @@ namespace interface {
2828

2929
struct electrum_methods
3030
{
31-
/// Electrum protocol version 1.4.2
31+
/// Electrum protocol versions 1.0-1.6
3232
static constexpr std::tuple methods
3333
{
3434
/// Blockchain methods.
35+
method<"blockchain.numblocks.subscribe">{},
36+
method<"blockchain.block.get_chunk", number_t>{ "index" },
37+
method<"blockchain.block.get_header", number_t>{ "height" },
3538
method<"blockchain.block.header", number_t, optional<0.0>>{ "height", "cp_height" },
3639
method<"blockchain.block.headers", number_t, number_t, optional<0.0>>{ "start_height", "count", "cp_height" },
3740
method<"blockchain.headers.subscribe">{},
41+
3842
method<"blockchain.estimatefee", number_t, optional<""_t>>{ "number", "mode" },
3943
method<"blockchain.relayfee">{},
44+
45+
method<"blockchain.utxo.get_address", string_t, number_t>{ "tx_hash", "index" },
46+
method<"blockchain.address.get_balance", string_t>{ "address" },
47+
method<"blockchain.address.get_history", string_t>{ "address" },
48+
method<"blockchain.address.get_mempool", string_t>{ "address" },
49+
method<"blockchain.address.listunspent", string_t>{ "address" },
50+
method<"blockchain.address.subscribe", string_t>{ "address" },
51+
4052
method<"blockchain.scripthash.get_balance", string_t>{ "scripthash" },
4153
method<"blockchain.scripthash.get_history", string_t>{ "scripthash" },
4254
method<"blockchain.scripthash.get_mempool", string_t>{ "scripthash" },
4355
method<"blockchain.scripthash.list_unspent", string_t>{ "scripthash" },
4456
method<"blockchain.scripthash.subscribe", string_t>{ "scripthash" },
4557
method<"blockchain.scripthash.unsubscribe", string_t>{ "scripthash" },
58+
4659
method<"blockchain.transaction.broadcast", string_t>{ "raw_tx" },
4760
method<"blockchain.transaction.broadcast_package", value_t, optional<false>>{ "raw_txs", "verbose" },
4861
method<"blockchain.transaction.get", string_t, boolean_t>{ "tx_hash", "verbose" },
@@ -69,32 +82,46 @@ struct electrum_methods
6982
template <size_t Index>
7083
using at = method_at<methods, Index>;
7184

72-
// Derive this from above in c++26 using reflection.
73-
using blockchain_block_header = at<0>;
74-
using blockchain_block_headers = at<1>;
75-
using blockchain_headers_subscribe = at<2>;
76-
using blockchain_estimate_fee = at<3>;
77-
using blockchain_relay_fee = at<4>;
78-
using blockchain_scripthash_get_balance = at<5>;
79-
using blockchain_scripthash_get_history = at<6>;
80-
using blockchain_scripthash_get_mempool = at<7>;
81-
using blockchain_scripthash_list_unspent = at<8>;
82-
using blockchain_scripthash_subscribe = at<9>;
83-
using blockchain_scripthash_unsubscribe = at<10>;
84-
using blockchain_transaction_broadcast = at<11>;
85-
using blockchain_transaction_broadcast_package = at<12>;
86-
using blockchain_transaction_get = at<13>;
87-
using blockchain_transaction_get_merkle = at<14>;
88-
using blockchain_transaction_id_from_pos = at<15>;
89-
using server_add_peer = at<16>;
90-
using server_banner = at<17>;
91-
using server_donation_address = at<18>;
92-
using server_features = at<19>;
93-
using server_peers_subscribe = at<20>;
94-
using server_ping = at<21>;
95-
using server_version = at<22>;
96-
using mempool_get_fee_histogram = at<23>;
97-
using mempool_get_info = at<24>;
85+
using blockchain_number_of_blocks_subscribe = at<0>;
86+
using blockchain_block_get_chunk = at<1>;
87+
using blockchain_block_get_header = at<2>;
88+
using blockchain_block_header = at<3>;
89+
using blockchain_block_headers = at<4>;
90+
using blockchain_headers_subscribe = at<5>;
91+
92+
using blockchain_estimate_fee = at<6>;
93+
using blockchain_relay_fee = at<7>;
94+
95+
using blockchain_utxo_get_address = at<8>;
96+
using blockchain_address_get_balance = at<9>;
97+
using blockchain_address_get_history = at<10>;
98+
using blockchain_address_get_mempool = at<11>;
99+
using blockchain_address_list_unspent = at<12>;
100+
using blockchain_address_subscribe = at<13>;
101+
102+
using blockchain_scripthash_get_balance = at<14>;
103+
using blockchain_scripthash_get_history = at<15>;
104+
using blockchain_scripthash_get_mempool = at<16>;
105+
using blockchain_scripthash_list_unspent = at<17>;
106+
using blockchain_scripthash_subscribe = at<18>;
107+
using blockchain_scripthash_unsubscribe = at<19>;
108+
109+
using blockchain_transaction_broadcast = at<20>;
110+
using blockchain_transaction_broadcast_package = at<21>;
111+
using blockchain_transaction_get = at<22>;
112+
using blockchain_transaction_get_merkle = at<23>;
113+
using blockchain_transaction_id_from_position = at<24>;
114+
115+
using server_add_peer = at<25>;
116+
using server_banner = at<26>;
117+
using server_donation_address = at<27>;
118+
using server_features = at<28>;
119+
using server_peers_subscribe = at<29>;
120+
using server_ping = at<30>;
121+
using server_version = at<31>;
122+
123+
using mempool_get_fee_histogram = at<32>;
124+
using mempool_get_info = at<33>;
98125
};
99126

100127
} // namespace interface

include/bitcoin/server/protocols/protocol_electrum.hpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class BCS_API protocol_electrum
5858
node::event_value) NOEXCEPT;
5959

6060
/// Handlers (headers).
61+
void handle_blockchain_number_of_blocks_subscribe(const code& ec,
62+
rpc_interface::blockchain_number_of_blocks_subscribe) NOEXCEPT;
63+
void handle_blockchain_block_get_chunk(const code& ec,
64+
rpc_interface::blockchain_block_get_chunk, double index) NOEXCEPT;
65+
void handle_blockchain_block_get_header(const code& ec,
66+
rpc_interface::blockchain_block_get_header, double height) NOEXCEPT;
6167
void handle_blockchain_block_header(const code& ec,
6268
rpc_interface::blockchain_block_header, double height,
6369
double cp_height) NOEXCEPT;
@@ -75,6 +81,26 @@ class BCS_API protocol_electrum
7581
rpc_interface::blockchain_relay_fee) NOEXCEPT;
7682

7783
/// Handlers (addresses).
84+
void handle_blockchain_utxo_get_address(const code& ec,
85+
rpc_interface::blockchain_utxo_get_address,
86+
const std::string& tx_hash, double index) NOEXCEPT;
87+
void handle_blockchain_address_get_balance(const code& ec,
88+
rpc_interface::blockchain_address_get_balance,
89+
const std::string& address) NOEXCEPT;
90+
void handle_blockchain_address_get_history(const code& ec,
91+
rpc_interface::blockchain_address_get_history,
92+
const std::string& address) NOEXCEPT;
93+
void handle_blockchain_address_get_mempool(const code& ec,
94+
rpc_interface::blockchain_address_get_mempool,
95+
const std::string& address) NOEXCEPT;
96+
void handle_blockchain_address_list_unspent(const code& ec,
97+
rpc_interface::blockchain_address_list_unspent,
98+
const std::string& address) NOEXCEPT;
99+
void handle_blockchain_address_subscribe(const code& ec,
100+
rpc_interface::blockchain_address_subscribe,
101+
const std::string& address) NOEXCEPT;
102+
103+
/// Handlers (scripthash).
78104
void handle_blockchain_scripthash_get_balance(const code& ec,
79105
rpc_interface::blockchain_scripthash_get_balance,
80106
const std::string& scripthash) NOEXCEPT;
@@ -107,8 +133,8 @@ class BCS_API protocol_electrum
107133
void handle_blockchain_transaction_get_merkle(const code& ec,
108134
rpc_interface::blockchain_transaction_get_merkle,
109135
const std::string& tx_hash, double height) NOEXCEPT;
110-
void handle_blockchain_transaction_id_from_pos(const code& ec,
111-
rpc_interface::blockchain_transaction_id_from_pos, double height,
136+
void handle_blockchain_transaction_id_from_position(const code& ec,
137+
rpc_interface::blockchain_transaction_id_from_position, double height,
112138
double tx_pos, bool merkle) NOEXCEPT;
113139

114140
/// Handlers (server).
@@ -142,7 +168,8 @@ class BCS_API protocol_electrum
142168
void blockchain_block_headers(size_t starting, size_t quantity,
143169
size_t waypoint, bool multiplicity) NOEXCEPT;
144170

145-
/// Notify client of new header.
171+
/// Notify client of newly organized block.
172+
void do_height(node::header_t link) NOEXCEPT;
146173
void do_header(node::header_t link) NOEXCEPT;
147174

148175
inline bool at_least(server::electrum::version version) const NOEXCEPT
@@ -161,11 +188,17 @@ class BCS_API protocol_electrum
161188
static constexpr electrum::version maximum = version_t::maximum;
162189

163190
// Compute server.features.hosts value from config.
164-
network::rpc::object_t advertised_hosts() const NOEXCEPT;
191+
network::rpc::object_t self_hosts() const NOEXCEPT;
192+
network::rpc::array_t more_hosts() const NOEXCEPT;
193+
194+
// Extract the legacy bitcoin payment address of a script.
195+
system::wallet::payment_address extract_address(
196+
const system::chain::script& script) NOEXCEPT;
165197

166198
// These are thread safe.
167199
const options_t& options_;
168-
std::atomic_bool subscribed_{};
200+
std::atomic_bool subscribed_height_{};
201+
std::atomic_bool subscribed_header_{};
169202

170203
// This is mostly thread safe, and used in a thread safe manner.
171204
const channel_t::ptr channel_;

include/bitcoin/server/settings.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ class BCS_API settings
106106
/// Arbitrary string returned by server.banner.
107107
std::string banner_message{};
108108

109-
/// Advertised as self via server.features (limit one each per host).
110-
network::config::endpoints advertise_binds{};
111-
network::config::endpoints advertise_safes{};
109+
/// Advertised self via server.features (limit one each per host).
110+
network::config::endpoints self_binds{};
111+
network::config::endpoints self_safes{};
112+
113+
/// Advertised servers via server.peers.subscribe.
114+
network::config::endpoints more_binds{};
115+
network::config::endpoints more_safes{};
112116
};
113117

114118
/// html (http/s) document server settings (has directory/default).

src/parser.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,15 +1155,25 @@ options_metadata parser::load_settings() THROWS
11551155
"String returned by server.banner, defaults to empty."
11561156
)
11571157
(
1158-
"electrum.advertise_bind",
1159-
value<network::config::endpoints>(&configured.server.electrum.advertise_binds),
1158+
"electrum.self_bind",
1159+
value<network::config::endpoints>(&configured.server.electrum.self_binds),
11601160
"Advertised host:port at which this server can be reached (defaults to empty)."
11611161
)
11621162
(
1163-
"electrum.advertise_safe",
1164-
value<network::config::endpoints>(&configured.server.electrum.advertise_safes),
1163+
"electrum.self_safe",
1164+
value<network::config::endpoints>(&configured.server.electrum.self_safes),
11651165
"Advertised secure host:port at which this server can be reached (defaults to empty)."
11661166
)
1167+
(
1168+
"electrum.more_bind",
1169+
value<network::config::endpoints>(&configured.server.electrum.more_binds),
1170+
"Advertised host:port at which another server can be reached (defaults to empty)."
1171+
)
1172+
(
1173+
"electrum.more_safe",
1174+
value<network::config::endpoints>(&configured.server.electrum.more_safes),
1175+
"Advertised secure host:port at which another server can be reached (defaults to empty)."
1176+
)
11671177

11681178
/* [stratum_v1] */
11691179
(

0 commit comments

Comments
 (0)