Skip to content

Commit 29cfd09

Browse files
authored
Merge pull request #984 from evoskuil/master
Integrate new optimizing block message.
2 parents 8f90f87 + 1a8b668 commit 29cfd09

File tree

12 files changed

+342
-18
lines changed

12 files changed

+342
-18
lines changed

Makefile.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ src_libbitcoin_node_la_SOURCES = \
5252
src/chasers/chaser_template.cpp \
5353
src/chasers/chaser_transaction.cpp \
5454
src/chasers/chaser_validate.cpp \
55+
src/messages/block.cpp \
56+
src/messages/transaction.cpp \
5557
src/protocols/protocol.cpp \
5658
src/protocols/protocol_block_in_106.cpp \
5759
src/protocols/protocol_block_in_31800.cpp \
@@ -149,6 +151,12 @@ include_bitcoin_node_impl_chasersdir = ${includedir}/bitcoin/node/impl/chasers
149151
include_bitcoin_node_impl_chasers_HEADERS = \
150152
include/bitcoin/node/impl/chasers/chaser_organize.ipp
151153

154+
include_bitcoin_node_messagesdir = ${includedir}/bitcoin/node/messages
155+
include_bitcoin_node_messages_HEADERS = \
156+
include/bitcoin/node/messages/block.hpp \
157+
include/bitcoin/node/messages/messages.hpp \
158+
include/bitcoin/node/messages/transaction.hpp
159+
152160
include_bitcoin_node_protocolsdir = ${includedir}/bitcoin/node/protocols
153161
include_bitcoin_node_protocols_HEADERS = \
154162
include/bitcoin/node/protocols/protocol.hpp \

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
<ClCompile Include="..\..\..\..\src\configuration.cpp" />
138138
<ClCompile Include="..\..\..\..\src\error.cpp" />
139139
<ClCompile Include="..\..\..\..\src\full_node.cpp" />
140+
<ClCompile Include="..\..\..\..\src\messages\block.cpp" />
141+
<ClCompile Include="..\..\..\..\src\messages\transaction.cpp" />
140142
<ClCompile Include="..\..\..\..\src\protocols\protocol.cpp" />
141143
<ClCompile Include="..\..\..\..\src\protocols\protocol_block_in_106.cpp" />
142144
<ClCompile Include="..\..\..\..\src\protocols\protocol_block_in_31800.cpp" />
@@ -183,6 +185,9 @@
183185
<ClInclude Include="..\..\..\..\include\bitcoin\node\error.hpp" />
184186
<ClInclude Include="..\..\..\..\include\bitcoin\node\events.hpp" />
185187
<ClInclude Include="..\..\..\..\include\bitcoin\node\full_node.hpp" />
188+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\block.hpp" />
189+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\messages.hpp" />
190+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\transaction.hpp" />
186191
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol.hpp" />
187192
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_block_in_106.hpp" />
188193
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol_block_in_31800.hpp" />

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,37 @@
88
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
99
<ItemGroup>
1010
<Filter Include="include">
11-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000005}</UniqueIdentifier>
11+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000006}</UniqueIdentifier>
1212
</Filter>
1313
<Filter Include="include\bitcoin">
14-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000006}</UniqueIdentifier>
14+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000007}</UniqueIdentifier>
1515
</Filter>
1616
<Filter Include="include\bitcoin\node">
17-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000007}</UniqueIdentifier>
17+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000008}</UniqueIdentifier>
1818
</Filter>
1919
<Filter Include="include\bitcoin\node\channels">
20-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000008}</UniqueIdentifier>
20+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000009}</UniqueIdentifier>
2121
</Filter>
2222
<Filter Include="include\bitcoin\node\chasers">
23-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000009}</UniqueIdentifier>
23+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000A}</UniqueIdentifier>
2424
</Filter>
2525
<Filter Include="include\bitcoin\node\impl">
26-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000A}</UniqueIdentifier>
26+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000B}</UniqueIdentifier>
2727
</Filter>
2828
<Filter Include="include\bitcoin\node\impl\chasers">
29-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000D}</UniqueIdentifier>
29+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000F}</UniqueIdentifier>
30+
</Filter>
31+
<Filter Include="include\bitcoin\node\messages">
32+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000C}</UniqueIdentifier>
3033
</Filter>
3134
<Filter Include="include\bitcoin\node\protocols">
32-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000B}</UniqueIdentifier>
35+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000D}</UniqueIdentifier>
3336
</Filter>
3437
<Filter Include="include\bitcoin\node\sessions">
35-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000C}</UniqueIdentifier>
38+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000E}</UniqueIdentifier>
3639
</Filter>
3740
<Filter Include="resource">
38-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000E}</UniqueIdentifier>
41+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000001}</UniqueIdentifier>
3942
</Filter>
4043
<Filter Include="src">
4144
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000000}</UniqueIdentifier>
@@ -46,12 +49,15 @@
4649
<Filter Include="src\chasers">
4750
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000002}</UniqueIdentifier>
4851
</Filter>
49-
<Filter Include="src\protocols">
52+
<Filter Include="src\messages">
5053
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000003}</UniqueIdentifier>
5154
</Filter>
52-
<Filter Include="src\sessions">
55+
<Filter Include="src\protocols">
5356
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000004}</UniqueIdentifier>
5457
</Filter>
58+
<Filter Include="src\sessions">
59+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000005}</UniqueIdentifier>
60+
</Filter>
5561
</ItemGroup>
5662
<ItemGroup>
5763
<ClCompile Include="..\..\..\..\src\block_arena.cpp">
@@ -102,6 +108,12 @@
102108
<ClCompile Include="..\..\..\..\src\full_node.cpp">
103109
<Filter>src</Filter>
104110
</ClCompile>
111+
<ClCompile Include="..\..\..\..\src\messages\block.cpp">
112+
<Filter>src\messages</Filter>
113+
</ClCompile>
114+
<ClCompile Include="..\..\..\..\src\messages\transaction.cpp">
115+
<Filter>src\messages</Filter>
116+
</ClCompile>
105117
<ClCompile Include="..\..\..\..\src\protocols\protocol.cpp">
106118
<Filter>src\protocols</Filter>
107119
</ClCompile>
@@ -236,6 +248,15 @@
236248
<ClInclude Include="..\..\..\..\include\bitcoin\node\full_node.hpp">
237249
<Filter>include\bitcoin\node</Filter>
238250
</ClInclude>
251+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\block.hpp">
252+
<Filter>include\bitcoin\node\messages</Filter>
253+
</ClInclude>
254+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\messages.hpp">
255+
<Filter>include\bitcoin\node\messages</Filter>
256+
</ClInclude>
257+
<ClInclude Include="..\..\..\..\include\bitcoin\node\messages\transaction.hpp">
258+
<Filter>include\bitcoin\node\messages</Filter>
259+
</ClInclude>
239260
<ClInclude Include="..\..\..\..\include\bitcoin\node\protocols\protocol.hpp">
240261
<Filter>include\bitcoin\node\protocols</Filter>
241262
</ClInclude>

include/bitcoin/node.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#include <bitcoin/node/chasers/chaser_transaction.hpp>
4242
#include <bitcoin/node/chasers/chaser_validate.hpp>
4343
#include <bitcoin/node/chasers/chasers.hpp>
44+
#include <bitcoin/node/messages/block.hpp>
45+
#include <bitcoin/node/messages/messages.hpp>
46+
#include <bitcoin/node/messages/transaction.hpp>
4447
#include <bitcoin/node/protocols/protocol.hpp>
4548
#include <bitcoin/node/protocols/protocol_block_in_106.hpp>
4649
#include <bitcoin/node/protocols/protocol_block_in_31800.hpp>

include/bitcoin/node/define.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,6 @@ using type_id = network::messages::peer::inventory_item::type_id;
123123
// /channels : define configuration
124124
// full_node : define /chasers
125125
// session : define [forward: full_node]
126+
// /messages : define
126127
// /protocols : define /channels [session.hpp]
127128
// /sessions : define /protocols [forward: full_node]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef LIBBITCOIN_NODE_MESSAGES_BLOCK_HPP
20+
#define LIBBITCOIN_NODE_MESSAGES_BLOCK_HPP
21+
22+
#include <bitcoin/node/define.hpp>
23+
24+
namespace libbitcoin {
25+
namespace node {
26+
namespace messages {
27+
28+
/// Based on network::messages::peer::block.
29+
struct BCN_API block
30+
{
31+
typedef std::shared_ptr<const block> cptr;
32+
33+
static const network::messages::peer::identifier id;
34+
static const std::string command;
35+
static const uint32_t version_minimum;
36+
static const uint32_t version_maximum;
37+
38+
// TODO: optimized translation direct to store.
39+
////static cptr deserialize(uint32_t version, const system::data_chunk& data,
40+
//// bool witness=true) NOEXCEPT;
41+
////static block deserialize(uint32_t version, system::reader& source,
42+
//// bool witness=true) NOEXCEPT;
43+
44+
/// These return false if witness or version is inconsistent with block data.
45+
bool serialize(uint32_t version, const system::data_slab& data,
46+
bool witness=true) const NOEXCEPT;
47+
void serialize(uint32_t version, system::writer& sink,
48+
bool witness=true) const NOEXCEPT;
49+
size_t size(uint32_t version, bool witness=true) const NOEXCEPT;
50+
51+
/// Wire serialized block.
52+
system::data_chunk block_data{};
53+
54+
/// Block contains witness data (if applicable).
55+
const bool witnessed_{};
56+
};
57+
58+
} // namespace messages
59+
} // namespace node
60+
} // namespace libbitcoin
61+
62+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef LIBBITCOIN_NODE_MESSAGES_MESSAGES_HPP
20+
#define LIBBITCOIN_NODE_MESSAGES_MESSAGES_HPP
21+
22+
#include <bitcoin/node/messages/block.hpp>
23+
#include <bitcoin/node/messages/transaction.hpp>
24+
25+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#ifndef LIBBITCOIN_NODE_MESSAGES_TRANSACTION_HPP
20+
#define LIBBITCOIN_NODE_MESSAGES_TRANSACTION_HPP
21+
22+
#include <optional>
23+
#include <bitcoin/node/define.hpp>
24+
25+
namespace libbitcoin {
26+
namespace node {
27+
namespace messages {
28+
29+
/// Based on network::messages::peer::transaction.
30+
struct BCN_API transaction
31+
{
32+
typedef std::shared_ptr<const transaction> cptr;
33+
34+
static const network::messages::peer::identifier id;
35+
static const std::string command;
36+
static const uint32_t version_minimum;
37+
static const uint32_t version_maximum;
38+
39+
// TODO: optimized translation direct to store.
40+
////static cptr deserialize(uint32_t version, const system::data_chunk& data,
41+
//// bool witness=true) NOEXCEPT;
42+
////static transaction deserialize(uint32_t version, system::reader& source,
43+
//// bool witness=true) NOEXCEPT;
44+
45+
/// These return false if witness or version is inconsistent with tx data.
46+
bool serialize(uint32_t version, const system::data_slab& data,
47+
bool witness=true) const NOEXCEPT;
48+
void serialize(uint32_t version, system::writer& sink,
49+
bool witness=true) const NOEXCEPT;
50+
size_t size(uint32_t version, bool witness=true) const NOEXCEPT;
51+
52+
/// Wire serialized transaction.
53+
system::data_chunk tx_data{};
54+
55+
/// Non-witness hash of the transaction (for non-witness send optimize).
56+
system::hash_digest hash{};
57+
58+
/// Transaction contains witness data (if applicable).
59+
const bool witnessed_{};
60+
};
61+
62+
} // namespace messages
63+
} // namespace node
64+
} // namespace libbitcoin
65+
66+
#endif

include/bitcoin/node/sessions/session_peer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class session_peer
8989

9090
using namespace system;
9191
using namespace network;
92-
using namespace messages::peer;
92+
using namespace network::messages::peer;
9393
using base = session_peer<NetworkSession>;
9494

9595
const auto self = this->template shared_from_base<base>();

src/messages/block.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include <bitcoin/node/messages/block.hpp>
20+
21+
#include <bitcoin/node/define.hpp>
22+
23+
namespace libbitcoin {
24+
namespace node {
25+
namespace messages {
26+
27+
using namespace system;
28+
using namespace network::messages::peer;
29+
30+
const std::string block::command = "block";
31+
const identifier block::id = identifier::block;
32+
const uint32_t block::version_minimum = level::minimum_protocol;
33+
const uint32_t block::version_maximum = level::maximum_protocol;
34+
35+
// data_slab is preallocated after the message header using size().
36+
bool block::serialize(uint32_t version, const data_slab& data,
37+
bool witness) const NOEXCEPT
38+
{
39+
if (witness != witnessed_)
40+
return false;
41+
42+
system::stream::out::fast out{ data };
43+
system::write::bytes::fast writer{ out };
44+
serialize(version, writer, witness);
45+
return writer;
46+
}
47+
48+
// Sender must ensure that version/witness are consistent with channel.
49+
void block::serialize(uint32_t, writer& sink,
50+
bool BC_DEBUG_ONLY(witness)) const NOEXCEPT
51+
{
52+
BC_ASSERT(witness == witnessed_);
53+
sink.write_bytes(block_data);
54+
}
55+
56+
// Sender must ensure that version/witness are consistent with channel.
57+
size_t block::size(uint32_t, bool BC_DEBUG_ONLY(witness)) const NOEXCEPT
58+
{
59+
BC_ASSERT(witness == witnessed_);
60+
return block_data.size();
61+
}
62+
63+
} // namespace messages
64+
} // namespace node
65+
} // namespace libbitcoin

0 commit comments

Comments
 (0)