@@ -61,7 +61,7 @@ server_node::server_node(settings_type& config)
6161 network_(network_pool_),
6262 protocol_(network_pool_, hosts_, handshake_, network_,
6363 network::protocol::default_seeds, bc::protocol_port,
64- true , config.out_connections),
64+ config.out_connections),
6565
6666 // Blockchain database service.
6767 chain_(disk_pool_, config.blockchain_path.string(),
@@ -84,6 +84,11 @@ bool server_node::start(settings_type& config)
8484 const auto skip_log = if_else (config.log_requests , " " , LOG_REQUEST );
8585 initialize_logging (outfile_, errfile_, bc::cout, bc::cerr, skip_log);
8686
87+ // Subscribe to new connections.
88+ protocol_.subscribe_channel (
89+ std::bind (&server_node::monitor_tx,
90+ this , _1, _2));
91+
8792 // Start blockchain.
8893 if (!chain_.start ())
8994 {
@@ -92,7 +97,8 @@ bool server_node::start(settings_type& config)
9297 }
9398
9499 chain_.subscribe_reorganize (
95- std::bind (&server_node::reorganize, this , _1, _2, _3, _4));
100+ std::bind (&server_node::reorganize,
101+ this , _1, _2, _3, _4));
96102
97103 // Start transaction pool
98104 txpool_.start ();
@@ -144,6 +150,7 @@ bool server_node::stop()
144150 ec_session.set_value (ec);
145151 };
146152 session_.stop (session_stopped);
153+
147154 // Query the error_code and wait for startup completion.
148155 const auto ec = ec_session.get_future ().get ();
149156 if (ec)
@@ -156,6 +163,7 @@ bool server_node::stop()
156163 network_pool_.stop ();
157164 disk_pool_.stop ();
158165 mem_pool_.stop ();
166+
159167 // Join threadpools. Wait for them to finish.
160168 network_pool_.join ();
161169 disk_pool_.join ();
@@ -200,12 +208,16 @@ void server_node::monitor_tx(const std::error_code& ec, network::channel_ptr nod
200208 log_warning (LOG_NODE ) << " Couldn't start connection: " << ec.message ();
201209 return ;
202210 }
211+
203212 // Subscribe to transaction messages from this node.
204213 node->subscribe_transaction (
205- std::bind (&server_node::recv_transaction, this , _1, _2, node));
214+ std::bind (&server_node::recv_transaction,
215+ this , _1, _2, node));
216+
206217 // Stay subscribed to new connections.
207218 protocol_.subscribe_channel (
208- std::bind (&server_node::monitor_tx, this , _1, _2));
219+ std::bind (&server_node::monitor_tx,
220+ this , _1, _2));
209221}
210222
211223void server_node::recv_transaction (const std::error_code& ec,
@@ -221,23 +233,29 @@ void server_node::recv_transaction(const std::error_code& ec,
221233 if (ec)
222234 log_error (LOG_NODE ) << " Deindex error: " << ec.message ();
223235 };
236+
224237 // Called when the transaction becomes confirmed in a block.
225238 const auto handle_confirm = [this , tx, handle_deindex](
226239 const std::error_code& ec)
227240 {
228241 log_debug (LOG_NODE ) << " Confirm transaction: " << ec.message ()
229242 << " " << encode_hash (hash_transaction (tx));
243+
230244 // Always try to deindex tx.
231245 // The error could be error::forced_removal from txpool.
232246 indexer_.deindex (tx, handle_deindex);
233247 };
248+
234249 // Validate the transaction from the network.
235250 // Attempt to store in the transaction pool and check the result.
236251 txpool_.store (tx, handle_confirm,
237- std::bind (&server_node::handle_mempool_store, this , _1, _2, tx, node));
252+ std::bind (&server_node::handle_mempool_store,
253+ this , _1, _2, tx, node));
254+
238255 // Resubscribe to transaction messages from this node.
239256 node->subscribe_transaction (
240- std::bind (&server_node::recv_transaction, this , _1, _2, node));
257+ std::bind (&server_node::recv_transaction,
258+ this , _1, _2, node));
241259}
242260
243261void server_node::handle_mempool_store (
@@ -250,6 +268,7 @@ void server_node::handle_mempool_store(
250268 << encode_hash (hash_transaction (tx)) << " : " << ec.message ();
251269 return ;
252270 }
271+
253272 const auto handle_index = [](const std::error_code& ec)
254273 {
255274 if (ec)
@@ -262,6 +281,7 @@ void server_node::handle_mempool_store(
262281 notify (tx);
263282}
264283
284+ // Conflicts with libbitcoin-node session implementation.
265285void server_node::reorganize (const std::error_code& /* ec */ ,
266286 size_t fork_point,
267287 const blockchain::block_list& new_blocks,
@@ -277,11 +297,12 @@ void server_node::reorganize(const std::error_code& /* ec */,
277297 for (const auto notify: notify_blocks_)
278298 notify (height, blk);
279299 }
300+
280301 chain_.subscribe_reorganize (
281- std::bind (&server_node::reorganize, this , _1, _2, _3, _4));
302+ std::bind (&server_node::reorganize,
303+ this , _1, _2, _3, _4));
282304}
283305
284- // TODO: use existing libbitcoin-node implementation.
285306void server_node::fullnode_fetch_history (server_node& node,
286307 const incoming_message& request, queue_send_callback queue_send)
287308{
@@ -290,9 +311,11 @@ void server_node::fullnode_fetch_history(server_node& node,
290311 if (!unwrap_fetch_history_args (payaddr, from_height, request))
291312 return ;
292313
314+ const auto handler = std::bind (send_history_result,
315+ _1, _2, request, queue_send);
316+
293317 fetch_history (node.blockchain (), node.transaction_indexer (), payaddr,
294- std::bind (send_history_result, _1, _2, request, queue_send),
295- from_height);
318+ handler, from_height);
296319}
297320
298321
0 commit comments