Skip to content

Commit 191c0e3

Browse files
authored
Merge pull request #709 from evoskuil/master
Implement electrum protocol outpoint notifications.
2 parents 663bdd7 + 955853a commit 191c0e3

12 files changed

Lines changed: 260 additions & 142 deletions

File tree

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ src_libbitcoin_server_la_SOURCES = \
5555
src/protocols/electrum/protocol_electrum_fees.cpp \
5656
src/protocols/electrum/protocol_electrum_headers.cpp \
5757
src/protocols/electrum/protocol_electrum_mempool.cpp \
58-
src/protocols/electrum/protocol_electrum_outputs.cpp \
58+
src/protocols/electrum/protocol_electrum_outpoints.cpp \
5959
src/protocols/electrum/protocol_electrum_scripthash.cpp \
6060
src/protocols/electrum/protocol_electrum_scripthash_subscribe.cpp \
6161
src/protocols/electrum/protocol_electrum_scriptpubkey.cpp \
@@ -104,7 +104,7 @@ test_libbitcoin_server_test_SOURCES = \
104104
test/protocols/electrum/electrum_fees.cpp \
105105
test/protocols/electrum/electrum_headers.cpp \
106106
test/protocols/electrum/electrum_mempool.cpp \
107-
test/protocols/electrum/electrum_outputs.cpp \
107+
test/protocols/electrum/electrum_outpoints.cpp \
108108
test/protocols/electrum/electrum_scripthash.cpp \
109109
test/protocols/electrum/electrum_scriptpubkey.cpp \
110110
test/protocols/electrum/electrum_server.cpp \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_fees.cpp" />
145145
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_headers.cpp" />
146146
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_mempool.cpp" />
147-
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_outputs.cpp" />
147+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_outpoints.cpp" />
148148
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_scripthash.cpp" />
149149
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_scriptpubkey.cpp" />
150150
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_server.cpp" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_mempool.cpp">
8282
<Filter>src\protocols\electrum</Filter>
8383
</ClCompile>
84-
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_outputs.cpp">
84+
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_outpoints.cpp">
8585
<Filter>src\protocols\electrum</Filter>
8686
</ClCompile>
8787
<ClCompile Include="..\..\..\..\test\protocols\electrum\electrum_scripthash.cpp">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_fees.cpp" />
138138
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_headers.cpp" />
139139
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_mempool.cpp" />
140-
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_outputs.cpp" />
140+
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_outpoints.cpp" />
141141
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scripthash.cpp" />
142142
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scripthash_subscribe.cpp" />
143143
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scriptpubkey.cpp" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_mempool.cpp">
112112
<Filter>src\protocols\electrum</Filter>
113113
</ClCompile>
114-
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_outputs.cpp">
114+
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_outpoints.cpp">
115115
<Filter>src\protocols\electrum</Filter>
116116
</ClCompile>
117117
<ClCompile Include="..\..\..\..\src\protocols\electrum\protocol_electrum_scripthash.cpp">

include/bitcoin/server/protocols/protocol.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ class BCS_API protocol
6161
} // namespace server
6262
} // namespace libbitcoin
6363

64+
// For use with secondary (e.g. notification) strands.
65+
#define NOTIFY(method, ...) notify<CLASS>(&CLASS::method, __VA_ARGS__)
66+
6467
#endif

include/bitcoin/server/protocols/protocol_electrum.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -242,39 +242,35 @@ class BCS_API protocol_electrum
242242
void do_header(node::header_t link) NOEXCEPT;
243243
void do_outpoint(node::header_t link) NOEXCEPT;
244244
void do_scripthash(node::header_t link) NOEXCEPT;
245-
void do_regressed(node::header_t link) NOEXCEPT;
245+
void do_reorganized(node::header_t link) NOEXCEPT;
246246

247247
/// Address.
248248
/// -----------------------------------------------------------------------
249249

250-
// subscription.
251250
void scripthash_subscribe(const hash_digest& hash,
252251
notify_t type) NOEXCEPT;
253252
void do_scripthash_subscribe(const hash_digest& hash,
254253
notify_t type) NOEXCEPT;
255254
void complete_scripthash_subscribe(const code& ec,
256255
hash_digest& status, const hash_digest& hash) NOEXCEPT;
257-
258-
// unsubscription.
259256
void scripthash_unsubscribe(const hash_digest& hash) NOEXCEPT;
260257
void do_scripthash_unsubscribe(const hash_digest& hash) NOEXCEPT;
261258
void complete_scripthash_unsubscribe(bool found) NOEXCEPT;
262-
263-
// notification (do_scripthash()).
264259
void scripthash_notify(const hash_digest& status, const hash_digest& hash,
265260
notify_t type) NOEXCEPT;
266261

267262
/// Outpoint.
268263
/// -----------------------------------------------------------------------
269264

270-
// subscription (do_outpoint()).
271-
bool get_outpoint_status(interface::object_t& status,
272-
const system::chain::point& prevout) const NOEXCEPT;
273-
bool send_outpoint_status(const system::chain::point& prevout,
274-
const std::string& spk_hint) NOEXCEPT;
275-
276-
// unsubscription.
277-
// notification.
265+
void do_outpoint_subscribe(const system::chain::point& prevout,
266+
const std::string& hint) NOEXCEPT;
267+
void complete_outpoint_subscribe(const code& ec,
268+
const system::chain::point& prevout,
269+
const std::string& hint) NOEXCEPT;
270+
void do_outpoint_unsubscribe(const system::chain::point& prevout) NOEXCEPT;
271+
void complete_outpoint_unsubscribe(bool found) NOEXCEPT;
272+
void outpoint_notify(const std::unique_ptr<interface::object_t>& status,
273+
const system::chain::point& prevout) NOEXCEPT;
278274

279275
/// Utilities.
280276
/// -----------------------------------------------------------------------
@@ -323,15 +319,21 @@ class BCS_API protocol_electrum
323319
static constexpr electrum::version minimum = version_t::minimum;
324320
static constexpr electrum::version maximum = version_t::maximum;
325321

326-
// Scripthash status.
322+
// Status utilities.
327323
code get_scripthash_status(hash_digest& out, subscription& sub,
328324
const hash_digest& hash) NOEXCEPT;
325+
bool get_outpoint_statuses(std::vector<interface::object_t>& out,
326+
const system::chain::point& prevout) const NOEXCEPT;
327+
bool get_outpoint_status(interface::object_t& out,
328+
const system::chain::point& prevout) const NOEXCEPT;
329+
bool send_outpoint_status(const system::chain::point& prevout,
330+
const std::string& hint) NOEXCEPT;
329331

330332
// Transformations.
331-
static std::string to_method_name(notify_t type) NOEXCEPT;
332333
static array_t transform(const unspents& unspents) NOEXCEPT;
333334
static array_t transform(const histories& histories) NOEXCEPT;
334335
static hash_digest to_status(const histories& histories) NOEXCEPT;
336+
static std::string to_method_name(notify_t type) NOEXCEPT;
335337

336338
// Compute server.features.hosts value from config.
337339
object_t self_hosts() const NOEXCEPT;
@@ -365,8 +367,8 @@ class BCS_API protocol_electrum
365367
network::asio::strand notification_strand_;
366368

367369
// These are protected by notification strand.
368-
std::map<hash_digest, subscription> address_subscriptions_{};
369370
std::set<system::chain::point> outpoint_subscriptions_{};
371+
std::map<hash_digest, subscription> address_subscriptions_{};
370372
};
371373

372374
} // namespace server

src/protocols/electrum/protocol_electrum.cpp

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ namespace libbitcoin {
2828
namespace server {
2929

3030
#define CLASS protocol_electrum
31-
#define NOTIFY(method, ...) notify<CLASS>(&CLASS::method, __VA_ARGS__)
3231

3332
using namespace system;
3433
using namespace network::rpc;
3534
using namespace std::placeholders;
35+
constexpr auto relaxed = std::memory_order_relaxed;
3636

3737
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3838

@@ -137,7 +137,6 @@ bool protocol_electrum::handle_event(const code&, node::chase event_,
137137
return false;
138138

139139
// TODO: collapse three atomics this into a single enumeration.
140-
constexpr auto relaxed = std::memory_order_relaxed;
141140
switch (event_)
142141
{
143142
////case node::chase::transaction:
@@ -170,12 +169,11 @@ bool protocol_electrum::handle_event(const code&, node::chase event_,
170169

171170
break;
172171
}
173-
case node::chase::regressed:
174-
case node::chase::disorganized:
172+
case node::chase::reorganized:
175173
{
176174
// value is regression branch_point.
177175
BC_ASSERT(std::holds_alternative<node::height_t>(value));
178-
NOTIFY(do_regressed, std::get<node::height_t>(value));
176+
NOTIFY(do_reorganized, std::get<node::height_t>(value));
179177
break;
180178
}
181179
default:
@@ -187,50 +185,6 @@ bool protocol_electrum::handle_event(const code&, node::chase event_,
187185
return true;
188186
}
189187

190-
// height/header notifications.
191-
// ----------------------------------------------------------------------------
192-
// Each notification is an independent message.
193-
194-
// Notifier for handle_blockchain_number_of_blocks_subscribe events.
195-
void protocol_electrum::do_height(node::header_t link) NOEXCEPT
196-
{
197-
BC_ASSERT(stranded());
198-
const auto& query = archive();
199-
const auto height = query.get_height(link);
200-
201-
if (height.is_terminal())
202-
{
203-
LOGF("Electrum::do_height, height not found (" << link << ").");
204-
return;
205-
}
206-
207-
send_notification("blockchain.numblocks.subscribe", array_t
208-
{
209-
height.value
210-
}, 48, BIND(complete, _1));
211-
}
212-
213-
// Notifier for blockchain_headers_subscribe events.
214-
void protocol_electrum::do_header(node::header_t link) NOEXCEPT
215-
{
216-
BC_ASSERT(stranded());
217-
const auto& query = archive();
218-
const auto height = query.get_height(link);
219-
const auto header = query.get_wire_header(link);
220-
221-
if (height.is_terminal())
222-
{
223-
LOGF("Electrum::do_header, header not found (" << link << ").");
224-
return;
225-
}
226-
227-
send_notification("blockchain.headers.subscribe", object_t
228-
{
229-
{ "height", height.value },
230-
{ "hex", encode_base16(header) }
231-
}, 64, BIND(complete, _1));
232-
}
233-
234188
BC_POP_WARNING()
235189

236190
} // namespace server

src/protocols/electrum/protocol_electrum_headers.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,52 @@ void protocol_electrum::handle_blockchain_headers_subscribe(const code& ec,
371371
}, 256, BIND(complete, _1));
372372
}
373373

374+
// height/header notifications.
375+
// ----------------------------------------------------------------------------
376+
// Each notification is an independent message.
377+
378+
// Notifier for handle_blockchain_number_of_blocks_subscribe events.
379+
void protocol_electrum::do_height(node::header_t link) NOEXCEPT
380+
{
381+
BC_ASSERT(stranded());
382+
383+
const auto& query = archive();
384+
const auto height = query.get_height(link);
385+
386+
if (height.is_terminal())
387+
{
388+
LOGF("Electrum::do_height, height not found (" << link << ").");
389+
return;
390+
}
391+
392+
send_notification("blockchain.numblocks.subscribe", array_t
393+
{
394+
height.value
395+
}, 48, BIND(complete, _1));
396+
}
397+
398+
// Notifier for blockchain_headers_subscribe events.
399+
void protocol_electrum::do_header(node::header_t link) NOEXCEPT
400+
{
401+
BC_ASSERT(stranded());
402+
403+
const auto& query = archive();
404+
const auto height = query.get_height(link);
405+
const auto header = query.get_wire_header(link);
406+
407+
if (height.is_terminal())
408+
{
409+
LOGF("Electrum::do_header, header not found (" << link << ").");
410+
return;
411+
}
412+
413+
send_notification("blockchain.headers.subscribe", object_t
414+
{
415+
{ "height", height.value },
416+
{ "hex", encode_base16(header) }
417+
}, 64, BIND(complete, _1));
418+
}
419+
374420
BC_POP_WARNING()
375421

376422
} // namespace server

0 commit comments

Comments
 (0)