Skip to content

Commit a5aaf40

Browse files
committed
Update server_node parameterization for upstream changes.
1 parent 7039ab2 commit a5aaf40

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

include/bitcoin/server/server_node.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class BCS_API server_node
5151
// Access to underlying services.
5252
bc::chain::blockchain& blockchain();
5353
bc::chain::transaction_pool& transaction_pool();
54-
bc::node::transaction_indexer& transaction_indexer();
54+
bc::node::indexer& transaction_indexer();
5555
bc::network::protocol& protocol();
5656

5757
// Threadpool for memory related operations.
@@ -71,10 +71,12 @@ class BCS_API server_node
7171
// New connection has been started.
7272
// Subscribe to new transaction messages from the network.
7373
void monitor_tx(const std::error_code& ec, bc::network::channel_ptr node);
74+
7475
// New transaction message from the network.
7576
// Attempt to validate it by storing it in the transaction pool.
7677
void recv_transaction(const std::error_code& ec,
7778
const bc::transaction_type& tx, bc::network::channel_ptr node);
79+
7880
// Result of store operation in transaction pool.
7981
void handle_mempool_store(const std::error_code& ec,
8082
const bc::index_list& unconfirmed, const bc::transaction_type& tx,
@@ -85,8 +87,10 @@ class BCS_API server_node
8587
const bc::chain::blockchain::block_list& replaced_blocks);
8688

8789
bc::ofstream outfile_, errfile_;
90+
8891
// Threadpools
8992
bc::threadpool network_pool_, disk_pool_, mem_pool_;
93+
9094
// Services
9195
bc::network::hosts hosts_;
9296
bc::network::handshake handshake_;
@@ -95,7 +99,7 @@ class BCS_API server_node
9599
bc::chain::blockchain_impl chain_;
96100
bc::node::poller poller_;
97101
bc::chain::transaction_pool txpool_;
98-
bc::node::transaction_indexer indexer_;
102+
bc::node::indexer indexer_;
99103
bc::node::session session_;
100104

101105
block_notify_list notify_blocks_;

src/server_node.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

211223
void 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

243261
void 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.
265285
void 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.
285306
void 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

Comments
 (0)