@@ -94,16 +94,22 @@ void protocol_electrum::stopping(const code& ec) NOEXCEPT
9494// ----------------------------------------------------------------------------
9595
9696bool protocol_electrum::handle_event (const code&, node::chase event_,
97- node::event_value) NOEXCEPT
97+ node::event_value value ) NOEXCEPT
9898{
9999 // Do not pass ec to stopped as it is not a call status.
100100 if (stopped ())
101101 return false ;
102102
103103 switch (event_)
104104 {
105- case node::chase::suspend :
105+ case node::chase::organized :
106106 {
107+ if (subscribed_.load (std::memory_order_relaxed))
108+ {
109+ BC_ASSERT (std::holds_alternative<node::header_t >(value));
110+ POST (do_header, std::get<node::header_t >(value));
111+ }
112+
107113 break ;
108114 }
109115 default :
@@ -297,19 +303,39 @@ void protocol_electrum::handle_blockchain_headers_subscribe(const code& ec,
297303 return ;
298304 }
299305
300- // TODO: signal header subscription.
306+ subscribed_.store (true , std::memory_order_relaxed);
307+ send_result (
308+ {
309+ object_t
310+ {
311+ { " height" , uint64_t { top } },
312+ { " hex" , to_hex (*header, chain::header::serialized_size ()) }
313+ }
314+ }, 256 , BIND (complete, _1));
315+ }
301316
302- // TODO: idempotent subscribe to chase::organized via session/chaser/node.
303- // TODO: upon notification send just the header notified by the link.
304- // TODO: it is client responsibility to deal with reorgs and race gaps.
305- send_result (value_t
317+ void protocol_electrum::do_header (node::header_t link) NOEXCEPT
318+ {
319+ BC_ASSERT (stranded ());
320+
321+ const auto & query = archive ();
322+ const auto height = query.get_height (link);
323+ const auto header = query.get_header (link);
324+
325+ if (height.is_terminal () || !header)
326+ {
327+ LOGF (" Electrum::do_header, object not found (" << link << " )." );
328+ return ;
329+ }
330+
331+ send_notification (" blockchain.headers.subscribe" ,
332+ {
333+ object_t
306334 {
307- object_t
308- {
309- { " height" , uint64_t { top } },
310- { " hex" , to_hex (*header, chain::header::serialized_size ()) }
311- }
312- }, 256 , BIND (complete, _1));
335+ { " height" , height.value },
336+ { " hex" , to_hex (*header, chain::header::serialized_size ()) }
337+ }
338+ }, 100 , BIND (complete, _1));
313339}
314340
315341void protocol_electrum::handle_blockchain_estimate_fee (const code& ec,
@@ -381,6 +407,8 @@ void protocol_electrum::handle_blockchain_scripthash_unsubscribe(const code& ec,
381407 send_code (error::not_implemented);
382408}
383409
410+ // TODO: requires tx pool in order to validate against unconfirmed txs.
411+ // TODO: requires that p2p channels subscribe to transaction broadcast.
384412void protocol_electrum::handle_blockchain_transaction_broadcast (const code& ec,
385413 rpc_interface::blockchain_transaction_broadcast,
386414 const std::string& ) NOEXCEPT
@@ -477,14 +505,14 @@ void protocol_electrum::handle_mempool_get_fee_histogram(const code& ec,
477505
478506 // TODO: requires tx pool metadata graph.
479507 send_result (value_t
508+ {
509+ array_t
480510 {
481- array_t
482- {
483- array_t { 1 , 1024 },
484- array_t { 2 , 2048 },
485- array_t { 4 , 4096 }
486- }
487- }, 256 , BIND (complete, _1));
511+ array_t { 1 , 1024 },
512+ array_t { 2 , 2048 },
513+ array_t { 4 , 4096 }
514+ }
515+ }, 256 , BIND (complete, _1));
488516}
489517
490518BC_POP_WARNING ()
0 commit comments