Skip to content

Commit eb2706b

Browse files
authored
Merge pull request #990 from evoskuil/master
Fix thread safety in protocol_block_in_31800, update logging.
2 parents 26f2483 + 85c73ff commit eb2706b

4 files changed

Lines changed: 37 additions & 31 deletions

File tree

include/bitcoin/node/protocols/protocol_block_in_31800.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class BCN_API protocol_block_in_31800
6767
bool is_idle() const NOEXCEPT override;
6868
virtual void do_purge(peer_t) NOEXCEPT;
6969
virtual void do_split(peer_t) NOEXCEPT;
70+
virtual void do_stall(peer_t) NOEXCEPT;
7071
virtual void do_report(count_t count) NOEXCEPT;
7172

7273
/// Check incoming block message.

src/chasers/chaser_confirm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ bool chaser_confirm::complete_block(const code& ec, const header_link& link,
350350
// CONFIRMABLE BLOCK
351351
notify(error::success, chase::confirmable, link);
352352
fire(events::block_confirmed, height);
353-
LOGV("Block confirmable: " << height << (bypass ? " (bypass)" : ""));
353+
LOGV("Block confirmed: " << height << (bypass ? " (bypass)" : ""));
354354
return true;
355355
}
356356

src/protocols/protocol_block_in_31800.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void protocol_block_in_31800::stopping(const code& ec) NOEXCEPT
9292

9393
bool protocol_block_in_31800::is_idle() const NOEXCEPT
9494
{
95+
BC_ASSERT(stranded());
9596
return map_->empty();
9697
}
9798

@@ -116,22 +117,14 @@ bool protocol_block_in_31800::handle_event(const code&, chase event_,
116117
// If this channel has divisible work, split it and stop.
117118
// There are no channels reporting work, either stalled or done.
118119
// This is initiated by any channel notifying chase::starved.
119-
if (map_->size() > one)
120-
{
121-
POST(do_split, peer_t{});
122-
}
123-
120+
POST(do_stall, peer_t{});
124121
break;
125122
}
126123
case chase::purge:
127124
{
128125
// If have work clear it and stop.
129126
// This is initiated by chase::regressed/disorganized.
130-
if (map_->size() > one)
131-
{
132-
POST(do_purge, peer_t{});
133-
}
134-
127+
POST(do_purge, peer_t{});
135128
break;
136129
}
137130
case chase::download:
@@ -162,38 +155,44 @@ bool protocol_block_in_31800::handle_event(const code&, chase event_,
162155
return true;
163156
}
164157

158+
void protocol_block_in_31800::do_report(count_t sequence) NOEXCEPT
159+
{
160+
BC_ASSERT(stranded());
161+
162+
// Uses application logging since it outputs to a runtime option.
163+
LOGA("Work report [" << sequence << "] is (" << map_->size() << ") for ["
164+
<< opposite() << "].");
165+
}
166+
165167
void protocol_block_in_31800::do_get_downloads(count_t) NOEXCEPT
166168
{
167169
BC_ASSERT(stranded());
168170

169-
if (stopped())
171+
if (stopped() || !is_idle())
170172
return;
171173

172-
if (is_idle())
173-
{
174-
// Assume performance was stopped due to exhaustion.
175-
start_performance();
176-
get_hashes(BIND(handle_get_hashes, _1, _2, _3));
177-
}
174+
// Assume performance was stopped due to exhaustion.
175+
start_performance();
176+
get_hashes(BIND(handle_get_hashes, _1, _2, _3));
178177
}
179178

180179
void protocol_block_in_31800::do_purge(peer_t) NOEXCEPT
181180
{
182181
BC_ASSERT(stranded());
183182

184-
if (!map_->empty())
185-
{
186-
LOGV("Purge work (" << map_->size() << ") from [" << opposite() << "].");
187-
map_->clear();
188-
stop(error::sacrificed_channel);
189-
}
183+
if (map_->empty())
184+
return;
185+
186+
LOGV("Purge work (" << map_->size() << ") from [" << opposite() << "].");
187+
map_->clear();
188+
stop(error::sacrificed_channel);
190189
}
191190

192-
void protocol_block_in_31800::do_split(peer_t) NOEXCEPT
191+
void protocol_block_in_31800::do_stall(peer_t) NOEXCEPT
193192
{
194193
BC_ASSERT(stranded());
195194

196-
if (stopped())
195+
if (stopped() || (map_->size() <= one))
197196
return;
198197

199198
LOGV("Divide work (" << map_->size() << ") from [" << opposite() << "].");
@@ -203,13 +202,18 @@ void protocol_block_in_31800::do_split(peer_t) NOEXCEPT
203202
stop(error::sacrificed_channel);
204203
}
205204

206-
void protocol_block_in_31800::do_report(count_t sequence) NOEXCEPT
205+
void protocol_block_in_31800::do_split(peer_t) NOEXCEPT
207206
{
208207
BC_ASSERT(stranded());
209208

210-
// Uses application logging since it outputs to a runtime option.
211-
LOGA("Work report [" << sequence << "] is (" << map_->size() << ") for ["
212-
<< opposite() << "].");
209+
if (stopped() || (map_->size() <= one))
210+
return;
211+
212+
LOGV("Split work (" << map_->size() << ") from [" << opposite() << "].");
213+
restore(chaser_check::split(map_));
214+
restore(map_);
215+
map_ = chaser_check::empty_map();
216+
stop(error::sacrificed_channel);
213217
}
214218

215219
// request hashes

src/protocols/protocol_header_out_70012.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ bool protocol_header_out_70012::do_announce(header_t link) NOEXCEPT
112112
return true;
113113
}
114114

115-
LOGN("Announce " << encode_hash(hash) << " to [" << opposite() << "].");
115+
LOGN("Announce ..." << encode_hash(hash).substr(hash_size - 8, 8)
116+
<< " to [" << opposite() << "].");
116117
SEND(headers{ { ptr } }, handle_send, _1);
117118
return true;
118119
}

0 commit comments

Comments
 (0)