Skip to content

Commit 3af875c

Browse files
committed
Implement some electrum interface methods.
1 parent 762c00c commit 3af875c

3 files changed

Lines changed: 76 additions & 23 deletions

File tree

include/bitcoin/server/interfaces/electrum.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct electrum_methods
3535
method<"blockchain.block.header", number_t, number_t>{ "height", "cp_height" },
3636
method<"blockchain.block.headers", number_t, number_t, number_t>{ "start_height", "count", "cp_height" },
3737
method<"blockchain.headers.subscribe">{},
38-
method<"blockchain.estimatefee", number_t>{ "number" },
38+
method<"blockchain.estimatefee", number_t, optional<""_t>>{ "number", "mode" },
3939
method<"blockchain.relayfee">{},
4040
method<"blockchain.scripthash.get_balance", string_t>{ "scripthash" },
4141
method<"blockchain.scripthash.get_history", string_t>{ "scripthash" },
@@ -71,8 +71,8 @@ struct electrum_methods
7171
using blockchain_block_header = at<0>;
7272
using blockchain_block_headers = at<1>;
7373
using blockchain_headers_subscribe = at<2>;
74-
using blockchain_estimatefee = at<3>;
75-
using blockchain_relayfee = at<4>;
74+
using blockchain_estimate_fee = at<3>;
75+
using blockchain_relay_fee = at<4>;
7676
using blockchain_scripthash_get_balance = at<5>;
7777
using blockchain_scripthash_get_history = at<6>;
7878
using blockchain_scripthash_get_mempool = at<7>;

include/bitcoin/server/protocols/protocol_electrum.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ class BCS_API protocol_electrum
5959
double count, double cp_height) NOEXCEPT;
6060
void handle_blockchain_headers_subscribe(const code& ec,
6161
rpc_interface::blockchain_headers_subscribe) NOEXCEPT;
62-
void handle_blockchain_estimatefee(const code& ec,
63-
rpc_interface::blockchain_estimatefee, double) NOEXCEPT;
64-
void handle_blockchain_relayfee(const code& ec,
65-
rpc_interface::blockchain_relayfee) NOEXCEPT;
62+
void handle_blockchain_estimate_fee(const code& ec,
63+
rpc_interface::blockchain_estimate_fee, double number,
64+
const std::string& mode) NOEXCEPT;
65+
void handle_blockchain_relay_fee(const code& ec,
66+
rpc_interface::blockchain_relay_fee) NOEXCEPT;
6667
void handle_blockchain_scripthash_get_balance(const code& ec,
6768
rpc_interface::blockchain_scripthash_get_balance,
6869
const std::string& scripthash) NOEXCEPT;

src/protocols/protocol_electrum.cpp

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace server {
2929

3030
#define CLASS protocol_electrum
3131

32+
using namespace system;
3233
using namespace interface;
3334
using namespace std::placeholders;
3435

@@ -50,8 +51,8 @@ void protocol_electrum::start() NOEXCEPT
5051
SUBSCRIBE_RPC(handle_blockchain_block_header, _1, _2, _3, _4);
5152
SUBSCRIBE_RPC(handle_blockchain_block_headers, _1, _2, _3, _4, _5);
5253
SUBSCRIBE_RPC(handle_blockchain_headers_subscribe, _1, _2);
53-
SUBSCRIBE_RPC(handle_blockchain_estimatefee, _1, _2, _3);
54-
SUBSCRIBE_RPC(handle_blockchain_relayfee, _1, _2);
54+
SUBSCRIBE_RPC(handle_blockchain_estimate_fee, _1, _2, _3, _4);
55+
SUBSCRIBE_RPC(handle_blockchain_relay_fee, _1, _2);
5556
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_balance, _1, _2, _3);
5657
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_history, _1, _2, _3);
5758
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_mempool, _1, _2, _3);
@@ -99,22 +100,60 @@ void protocol_electrum::handle_blockchain_block_headers(const code& ec,
99100
void protocol_electrum::handle_blockchain_headers_subscribe(const code& ec,
100101
rpc_interface::blockchain_headers_subscribe) NOEXCEPT
101102
{
102-
if (stopped(ec)) return;
103-
send_code(error::not_implemented);
103+
if (stopped(ec))
104+
return;
105+
106+
const auto& query = archive();
107+
const auto link = query.to_header(query.get_top_confirmed_hash());
108+
const auto height = query.get_height(link);
109+
const auto header = query.get_header(link);
110+
if (height.is_terminal() || !header)
111+
{
112+
send_code(error::not_found);
113+
return;
114+
}
115+
116+
// See protocol_native::to_hex().
117+
std::string hex(two * chain::header::serialized_size(), '\0');
118+
stream::out::fast sink{ hex };
119+
write::base16::fast writer{ sink };
120+
header->to_data(writer);
121+
BC_ASSERT(writer);
122+
123+
// TODO: subscribe to block confirmation (idempotent).
124+
// TODO: upon notification send just the notifying header.
125+
// TODO: it is client responsiblity to deal with reorgs and race gaps.
126+
send_result(value_t
127+
{
128+
object_t
129+
{
130+
{ "height", height.value },
131+
{ "hex", hex }
132+
}
133+
}, 256, BIND(complete, _1));
104134
}
105135

106-
void protocol_electrum::handle_blockchain_estimatefee(const code& ec,
107-
rpc_interface::blockchain_estimatefee, double ) NOEXCEPT
136+
void protocol_electrum::handle_blockchain_estimate_fee(const code& ec,
137+
rpc_interface::blockchain_estimate_fee, double number,
138+
const std::string& ) NOEXCEPT
108139
{
109-
if (stopped(ec)) return;
110-
send_code(error::not_implemented);
140+
if (stopped(ec))
141+
return;
142+
143+
// TODO: estimate fees from blocks based on expected block inclusion.
144+
// TODO: this can be computed from recent blocks and cached by the server.
145+
// TODO: update the cache before boradcasting header notifications.
146+
send_result(number, 70, BIND(complete, _1));
111147
}
112148

113-
void protocol_electrum::handle_blockchain_relayfee(const code& ec,
114-
rpc_interface::blockchain_relayfee) NOEXCEPT
149+
void protocol_electrum::handle_blockchain_relay_fee(const code& ec,
150+
rpc_interface::blockchain_relay_fee) NOEXCEPT
115151
{
116-
if (stopped(ec)) return;
117-
send_code(error::not_implemented);
152+
if (stopped(ec))
153+
return;
154+
155+
// TODO: implement from [node] config, removed in protocol 1.6.
156+
send_result(0.00000001, 70, BIND(complete, _1));
118157
}
119158

120159
void protocol_electrum::handle_blockchain_scripthash_get_balance(const code& ec,
@@ -210,8 +249,10 @@ void protocol_electrum::handle_server_add_peer(const code& ec,
210249
void protocol_electrum::handle_server_banner(const code& ec,
211250
rpc_interface::server_banner) NOEXCEPT
212251
{
213-
if (stopped(ec)) return;
214-
send_code(error::not_implemented);
252+
if (stopped(ec))
253+
return;
254+
255+
send_result(network_settings().user_agent, 70, BIND(complete, _1));
215256
}
216257

217258
void protocol_electrum::handle_server_donation_address(const code& ec,
@@ -248,8 +289,19 @@ void protocol_electrum::handle_server_ping(const code& ec,
248289
void protocol_electrum::handle_mempool_get_fee_histogram(const code& ec,
249290
rpc_interface::mempool_get_fee_histogram) NOEXCEPT
250291
{
251-
if (stopped(ec)) return;
252-
send_code(error::not_implemented);
292+
if (stopped(ec))
293+
return;
294+
295+
// TODO: requires tx pool metadata graph.
296+
send_result(value_t
297+
{
298+
array_t
299+
{
300+
array_t{ 1, 1024 },
301+
array_t{ 2, 2048 },
302+
array_t{ 4, 4096 }
303+
}
304+
}, 256, BIND(complete, _1));
253305
}
254306

255307
BC_POP_WARNING()

0 commit comments

Comments
 (0)