@@ -29,6 +29,7 @@ namespace server {
2929
3030#define CLASS protocol_electrum
3131
32+ using namespace system ;
3233using namespace interface ;
3334using 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,
99100void 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
120159void protocol_electrum::handle_blockchain_scripthash_get_balance (const code& ec,
@@ -210,8 +249,10 @@ void protocol_electrum::handle_server_add_peer(const code& ec,
210249void 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
217258void protocol_electrum::handle_server_donation_address (const code& ec,
@@ -248,8 +289,19 @@ void protocol_electrum::handle_server_ping(const code& ec,
248289void 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
255307BC_POP_WARNING ()
0 commit comments