Skip to content

Commit 8140aac

Browse files
authored
Merge pull request #982 from evoskuil/master
Fix protocol_block_out incorrect overlapped req errors.
2 parents 6ec41df + b887bc7 commit 8140aac

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

include/bitcoin/node/protocols/protocol_block_out_106.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class BCN_API protocol_block_out_106
4747
void stopping(const code& ec) NOEXCEPT override;
4848

4949
protected:
50+
using get_data = network::messages::peer::get_data;
51+
using get_blocks = network::messages::peer::get_blocks;
52+
using inventory = network::messages::peer::inventory;
53+
using inventory_items = network::messages::peer::inventory_items;
54+
using inventory_items_ptr = std::shared_ptr<inventory_items>;
55+
5056
/// Block announcements are superseded by send_headers.
5157
virtual bool superseded() const NOEXCEPT;
5258

@@ -58,15 +64,14 @@ class BCN_API protocol_block_out_106
5864
virtual bool do_announce(header_t link) NOEXCEPT;
5965

6066
virtual bool handle_receive_get_blocks(const code& ec,
61-
const network::messages::peer::get_blocks::cptr& message) NOEXCEPT;
67+
const get_blocks::cptr& message) NOEXCEPT;
6268
virtual bool handle_receive_get_data(const code& ec,
63-
const network::messages::peer::get_data::cptr& message) NOEXCEPT;
64-
virtual void send_block(const code& ec, size_t index,
65-
const network::messages::peer::get_data::cptr& message) NOEXCEPT;
69+
const get_data::cptr& message) NOEXCEPT;
70+
virtual void send_block(const code& ec,
71+
const inventory_items_ptr& items) NOEXCEPT;
6672

6773
private:
68-
network::messages::peer::inventory create_inventory(
69-
const network::messages::peer::get_blocks& locator) const NOEXCEPT;
74+
inventory create_inventory(const get_blocks& locator) const NOEXCEPT;
7075

7176
private:
7277
// This is thread safe.

src/protocols/protocol_block_out_106.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace node {
2828

2929
using namespace system;
3030
using namespace network;
31-
using namespace network::messages::peer;
3231
using namespace std::chrono;
3332
using namespace std::placeholders;
3433

@@ -147,7 +146,16 @@ bool protocol_block_out_106::handle_receive_get_data(const code& ec,
147146
if (stopped(ec))
148147
return false;
149148

150-
if (!message->any_block())
149+
if (!node_witness_ && message->any_witness())
150+
{
151+
LOGR("Unsupported witness get_data from [" << opposite() << "].");
152+
stop(network::error::protocol_violation);
153+
return false;
154+
}
155+
156+
constexpr auto only = get_data::selector::blocks;
157+
const auto blocks = emplace_shared<inventory_items>(message->select(only));
158+
if (blocks->empty())
151159
return true;
152160

153161
if (busy_)
@@ -158,32 +166,22 @@ bool protocol_block_out_106::handle_receive_get_data(const code& ec,
158166
}
159167

160168
busy_ = true;
161-
send_block(error::success, zero, message);
169+
send_block(error::success, blocks);
162170
return true;
163171
}
164172

165173
// Outbound (block).
166174
// ----------------------------------------------------------------------------
167175

168-
void protocol_block_out_106::send_block(const code& ec, size_t index,
169-
const get_data::cptr& message) NOEXCEPT
176+
void protocol_block_out_106::send_block(const code& ec,
177+
const inventory_items_ptr& items) NOEXCEPT
170178
{
171179
BC_ASSERT(stranded());
172180
if (stopped(ec))
173181
return;
174182

175-
// Skip over non-block requests.
176-
const auto& items = message->items;
177-
for (; index < items.size() && !items.at(index).is_block_type(); ++index);
178-
179-
// No more block requests.
180-
if (index == items.size())
181-
{
182-
busy_ = false;
183-
return;
184-
}
185-
186-
const auto& item = items.at(index);
183+
if (items->empty()) return;
184+
const auto item = pop(*items);
187185
const auto witness = item.is_witness_type();
188186
if (!node_witness_ && witness)
189187
{
@@ -206,13 +204,14 @@ void protocol_block_out_106::send_block(const code& ec, size_t index,
206204
}
207205

208206
span<microseconds>(events::block_usecs, start);
209-
SEND(block{ ptr }, send_block, _1, add1(index), message);
207+
if (items->empty()) busy_ = false;
208+
SEND(messages::peer::block{ ptr }, send_block, _1, items);
210209
}
211210

212211
// utilities
213212
// ----------------------------------------------------------------------------
214213

215-
inventory protocol_block_out_106::create_inventory(
214+
protocol_block_out_106::inventory protocol_block_out_106::create_inventory(
216215
const get_blocks& locator) const NOEXCEPT
217216
{
218217
// Empty response implies complete (success).
@@ -222,7 +221,7 @@ inventory protocol_block_out_106::create_inventory(
222221
return inventory::factory
223222
(
224223
archive().get_blocks(locator.start_hashes, locator.stop_hash,
225-
max_get_blocks), type_id::block
224+
messages::peer::max_get_blocks), type_id::block
226225
);
227226
}
228227

0 commit comments

Comments
 (0)