Skip to content

Commit c3e1967

Browse files
authored
Merge pull request #622 from evoskuil/master
Capture chaser event in protocol_electrum (stub).
2 parents bdb480a + 8bd0d0a commit c3e1967

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

include/bitcoin/server/define.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
#define BCS_INTERNAL BC_HELPER_DLL_LOCAL
3838
#endif
3939

40-
/// Prevent bogus vc++ warnings on protocol.
41-
BC_DISABLE_WARNING(DIAMOND_INHERITANCE)
42-
4340
/// Augment limited xcode placeholder defines (10 vs. common 20).
4441
/// ---------------------------------------------------------------------------
4542
#if defined (HAVE_XCODE)

include/bitcoin/server/interfaces/electrum.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ struct electrum_methods
3232
static constexpr std::tuple methods
3333
{
3434
/// Blockchain methods.
35-
method<"blockchain.block.header", number_t, number_t>{ "height", "cp_height" },
36-
method<"blockchain.block.headers", number_t, number_t, number_t>{ "start_height", "count", "cp_height" },
35+
method<"blockchain.block.header", number_t, optional<0.0>>{ "height", "cp_height" },
36+
method<"blockchain.block.headers", number_t, number_t, optional<0.0>>{ "start_height", "count", "cp_height" },
3737
method<"blockchain.headers.subscribe">{},
3838
method<"blockchain.estimatefee", number_t, optional<""_t>>{ "number", "mode" },
3939
method<"blockchain.relayfee">{},

include/bitcoin/server/protocols/protocol_electrum.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ class BCS_API protocol_electrum
4848
}
4949

5050
void start() NOEXCEPT override;
51+
void stopping(const code& ec) NOEXCEPT override;
5152

5253
protected:
54+
/// Handlers (event subscription).
55+
bool handle_event(const code&, node::chase event_,
56+
node::event_value) NOEXCEPT;
57+
5358
/// Handlers (blockchain).
5459
void handle_blockchain_block_header(const code& ec,
5560
rpc_interface::blockchain_block_header, double height,

src/protocols/protocol_electrum.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void protocol_electrum::start() NOEXCEPT
4747
if (started())
4848
return;
4949

50+
// Events subscription is asynchronous, events may be missed.
51+
subscribe_events(BIND(handle_event, _1, _2, _3));
52+
5053
// Blockchain methods.
5154
SUBSCRIBE_RPC(handle_blockchain_block_header, _1, _2, _3, _4);
5255
SUBSCRIBE_RPC(handle_blockchain_block_headers, _1, _2, _3, _4, _5);
@@ -78,17 +81,51 @@ void protocol_electrum::start() NOEXCEPT
7881
protocol_rpc<channel_electrum>::start();
7982
}
8083

84+
void protocol_electrum::stopping(const code& ec) NOEXCEPT
85+
{
86+
// Unsubscriber race is ok.
87+
BC_ASSERT(stranded());
88+
unsubscribe_events();
89+
protocol_rpc<channel_electrum>::stopping(ec);
90+
}
91+
92+
// Handlers (event subscription).
93+
// ----------------------------------------------------------------------------
94+
95+
bool protocol_electrum::handle_event(const code&, node::chase event_,
96+
node::event_value) NOEXCEPT
97+
{
98+
// Do not pass ec to stopped as it is not a call status.
99+
if (stopped())
100+
return false;
101+
102+
switch (event_)
103+
{
104+
case node::chase::suspend:
105+
{
106+
break;
107+
}
108+
default:
109+
{
110+
break;
111+
}
112+
}
113+
114+
return true;
115+
}
116+
81117
// Handlers (blockchain).
82118
// ----------------------------------------------------------------------------
83119

120+
// electrum-protocol.readthedocs.io/en/latest/protocol-basics.html#block-headers
84121
void protocol_electrum::handle_blockchain_block_header(const code& ec,
85-
rpc_interface::blockchain_block_header, double ,
86-
double ) NOEXCEPT
122+
rpc_interface::blockchain_block_header, double height,
123+
double cp_height) NOEXCEPT
87124
{
88-
if (stopped(ec)) return;
89-
send_code(error::not_implemented);
125+
handle_blockchain_block_headers(ec, {}, height, 1, cp_height);
90126
}
91127

128+
// electrum-protocol.readthedocs.io/en/latest/protocol-basics.html#block-headers
92129
void protocol_electrum::handle_blockchain_block_headers(const code& ec,
93130
rpc_interface::blockchain_block_headers, double ,
94131
double , double ) NOEXCEPT
@@ -120,6 +157,8 @@ void protocol_electrum::handle_blockchain_headers_subscribe(const code& ec,
120157
header->to_data(writer);
121158
BC_ASSERT(writer);
122159

160+
// TODO: signal header subscription.
161+
123162
// TODO: idempotent subscribe to chase::organized via session/chaser/node.
124163
// TODO: upon notification send just the header notified by the link.
125164
// TODO: it is client responsibility to deal with reorgs and race gaps.
@@ -269,6 +308,7 @@ void protocol_electrum::handle_server_features(const code& ec,
269308
send_code(error::not_implemented);
270309
}
271310

311+
// This is not actually a subscription method.
272312
void protocol_electrum::handle_server_peers_subscribe(const code& ec,
273313
rpc_interface::server_peers_subscribe) NOEXCEPT
274314
{

0 commit comments

Comments
 (0)