Skip to content

Commit b5927b1

Browse files
authored
Merge pull request #651 from evoskuil/master
Create network.ipp and associated test stub.
2 parents 39f5758 + 261bd77 commit b5927b1

File tree

10 files changed

+187
-92
lines changed

10 files changed

+187
-92
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test_libbitcoin_database_test_SOURCES = \
9494
test/query/extent.cpp \
9595
test/query/height.cpp \
9696
test/query/initialize.cpp \
97+
test/query/network.cpp \
9798
test/query/objects.cpp \
9899
test/query/optional.cpp \
99100
test/query/translate.cpp \
@@ -181,6 +182,7 @@ include_bitcoin_database_impl_query_HEADERS = \
181182
include/bitcoin/database/impl/query/extent.ipp \
182183
include/bitcoin/database/impl/query/height.ipp \
183184
include/bitcoin/database/impl/query/initialize.ipp \
185+
include/bitcoin/database/impl/query/network.ipp \
184186
include/bitcoin/database/impl/query/objects.ipp \
185187
include/bitcoin/database/impl/query/optional.ipp \
186188
include/bitcoin/database/impl/query/query.ipp \

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ if (with-tests)
304304
"../../test/query/extent.cpp"
305305
"../../test/query/height.cpp"
306306
"../../test/query/initialize.cpp"
307+
"../../test/query/network.cpp"
307308
"../../test/query/objects.cpp"
308309
"../../test/query/optional.cpp"
309310
"../../test/query/translate.cpp"

builds/msvc/vs2022/libbitcoin-database-test/libbitcoin-database-test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<ObjectFileName>$(IntDir)test_query_height.obj</ObjectFileName>
154154
</ClCompile>
155155
<ClCompile Include="..\..\..\..\test\query\initialize.cpp" />
156+
<ClCompile Include="..\..\..\..\test\query\network.cpp" />
156157
<ClCompile Include="..\..\..\..\test\query\objects.cpp" />
157158
<ClCompile Include="..\..\..\..\test\query\optional.cpp" />
158159
<ClCompile Include="..\..\..\..\test\query\translate.cpp" />

builds/msvc/vs2022/libbitcoin-database-test/libbitcoin-database-test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
<ClCompile Include="..\..\..\..\test\query\initialize.cpp">
133133
<Filter>src\query</Filter>
134134
</ClCompile>
135+
<ClCompile Include="..\..\..\..\test\query\network.cpp">
136+
<Filter>src\query</Filter>
137+
</ClCompile>
135138
<ClCompile Include="..\..\..\..\test\query\objects.cpp">
136139
<Filter>src\query</Filter>
137140
</ClCompile>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
<None Include="..\..\..\..\include\bitcoin\database\impl\query\extent.ipp" />
220220
<None Include="..\..\..\..\include\bitcoin\database\impl\query\height.ipp" />
221221
<None Include="..\..\..\..\include\bitcoin\database\impl\query\initialize.ipp" />
222+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\network.ipp" />
222223
<None Include="..\..\..\..\include\bitcoin\database\impl\query\objects.ipp" />
223224
<None Include="..\..\..\..\include\bitcoin\database\impl\query\optional.ipp" />
224225
<None Include="..\..\..\..\include\bitcoin\database\impl\query\query.ipp" />

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@
352352
<None Include="..\..\..\..\include\bitcoin\database\impl\query\initialize.ipp">
353353
<Filter>include\bitcoin\database\impl\query</Filter>
354354
</None>
355+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\network.ipp">
356+
<Filter>include\bitcoin\database\impl\query</Filter>
357+
</None>
355358
<None Include="..\..\..\..\include\bitcoin\database\impl\query\objects.ipp">
356359
<Filter>include\bitcoin\database\impl\query</Filter>
357360
</None>

include/bitcoin/database/impl/query/height.ipp

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -60,98 +60,6 @@ size_t CLASS::get_confirmed_size(size_t top) const NOEXCEPT
6060
return wire;
6161
}
6262

63-
// locator readers
64-
// ----------------------------------------------------------------------------
65-
// These do not require strict consistency.
66-
67-
TEMPLATE
68-
CLASS::headers CLASS::get_headers(const hashes& locator,
69-
const hash_digest& stop, size_t limit) const NOEXCEPT
70-
{
71-
headers out{};
72-
const auto span = get_locator_span(locator, stop, limit);
73-
out.reserve(span.size());
74-
75-
for (auto height = span.begin; height < span.end; ++height)
76-
{
77-
// Terminal implies intervening reorganization.
78-
const auto link = to_confirmed(height);
79-
if (link.is_terminal())
80-
return {};
81-
82-
out.push_back(get_header(link));
83-
BC_ASSERT(!is_null(out.back()));
84-
}
85-
86-
return out;
87-
}
88-
89-
TEMPLATE
90-
hashes CLASS::get_blocks(const hashes& locator,
91-
const hash_digest& stop, size_t limit) const NOEXCEPT
92-
{
93-
hashes out{};
94-
const auto span = get_locator_span(locator, stop, limit);
95-
out.reserve(span.size());
96-
97-
for (auto height = span.begin; height < span.end; ++height)
98-
{
99-
// Terminal implies intervening reorganization.
100-
const auto link = to_confirmed(height);
101-
if (link.is_terminal())
102-
return {};
103-
104-
out.push_back(get_header_key(link));
105-
BC_ASSERT(out.back() != system::null_hash);
106-
}
107-
108-
return out;
109-
}
110-
111-
TEMPLATE
112-
CLASS::span CLASS::get_locator_span(const hashes& locator,
113-
const hash_digest& stop, size_t limit) const NOEXCEPT
114-
{
115-
using namespace system;
116-
span out{};
117-
118-
// Start at fork point, stop at given header (both excluded).
119-
const auto start = add1(get_fork(locator));
120-
const auto last1 = (stop == null_hash) ? max_uint32 :
121-
get_height(to_header(stop)).value;
122-
123-
// Determine number of headers requested, limited by max allowed.
124-
const auto request = floored_subtract<size_t>(last1, start);
125-
const auto allowed = std::min(request, limit);
126-
127-
// Set end to (start + allowed), limited by (top + 1).
128-
const auto top1 = ceilinged_add(get_top_confirmed(), one);
129-
const auto end = std::min(ceilinged_add(start, allowed), top1);
130-
131-
// Convert negative range to empty.
132-
out.end = std::max(start, end);
133-
return out;
134-
}
135-
136-
// protected
137-
TEMPLATE
138-
size_t CLASS::get_fork(const hashes& locator) const NOEXCEPT
139-
{
140-
// Locator is presumed (by convention) to be in order by height.
141-
for (const auto& hash: locator)
142-
{
143-
const auto link = to_header(hash);
144-
const auto height = get_height(link);
145-
146-
table::height::record confirmed{};
147-
if (store_.confirmed.get(height, confirmed) &&
148-
confirmed.header_fk == link)
149-
return height;
150-
}
151-
152-
return zero;
153-
}
154-
15563
// shared_lock readers
15664
// ----------------------------------------------------------------------------
15765
// Protected against index pop (low contention) to ensure branch consistency.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright (c) 2011-2025 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_DATABASE_QUERY_NETWORK_IPP
20+
#define LIBBITCOIN_DATABASE_QUERY_NETWORK_IPP
21+
22+
#include <algorithm>
23+
#include <bitcoin/system.hpp>
24+
#include <bitcoin/database/define.hpp>
25+
26+
namespace libbitcoin {
27+
namespace database {
28+
29+
// locator readers
30+
// ----------------------------------------------------------------------------
31+
// These do not require strict consistency.
32+
33+
TEMPLATE
34+
CLASS::headers CLASS::get_headers(const hashes& locator,
35+
const hash_digest& stop, size_t limit) const NOEXCEPT
36+
{
37+
headers out{};
38+
const auto span = get_locator_span(locator, stop, limit);
39+
out.reserve(span.size());
40+
41+
for (auto height = span.begin; height < span.end; ++height)
42+
{
43+
// Terminal implies intervening reorganization.
44+
const auto link = to_confirmed(height);
45+
if (link.is_terminal())
46+
return {};
47+
48+
out.push_back(get_header(link));
49+
BC_ASSERT(!is_null(out.back()));
50+
}
51+
52+
return out;
53+
}
54+
55+
TEMPLATE
56+
hashes CLASS::get_blocks(const hashes& locator,
57+
const hash_digest& stop, size_t limit) const NOEXCEPT
58+
{
59+
hashes out{};
60+
const auto span = get_locator_span(locator, stop, limit);
61+
out.reserve(span.size());
62+
63+
for (auto height = span.begin; height < span.end; ++height)
64+
{
65+
// Terminal implies intervening reorganization.
66+
const auto link = to_confirmed(height);
67+
if (link.is_terminal())
68+
return {};
69+
70+
out.push_back(get_header_key(link));
71+
BC_ASSERT(out.back() != system::null_hash);
72+
}
73+
74+
return out;
75+
}
76+
77+
TEMPLATE
78+
CLASS::span CLASS::get_locator_span(const hashes& locator,
79+
const hash_digest& stop, size_t limit) const NOEXCEPT
80+
{
81+
using namespace system;
82+
span out{};
83+
84+
// Start at fork point, stop at given header (both excluded).
85+
const auto start = add1(get_fork(locator));
86+
const auto last1 = (stop == null_hash) ? max_uint32 :
87+
get_height(to_header(stop)).value;
88+
89+
// Determine number of headers requested, limited by max allowed.
90+
const auto request = floored_subtract<size_t>(last1, start);
91+
const auto allowed = std::min(request, limit);
92+
93+
// Set end to (start + allowed), limited by (top + 1).
94+
const auto top1 = ceilinged_add(get_top_confirmed(), one);
95+
const auto end = std::min(ceilinged_add(start, allowed), top1);
96+
97+
// Convert negative range to empty.
98+
out.end = std::max(start, end);
99+
return out;
100+
}
101+
102+
// protected
103+
TEMPLATE
104+
size_t CLASS::get_fork(const hashes& locator) const NOEXCEPT
105+
{
106+
// Locator is presumed (by convention) to be in order by height.
107+
for (const auto& hash: locator)
108+
{
109+
const auto link = to_header(hash);
110+
const auto height = get_height(link);
111+
112+
table::height::record confirmed{};
113+
if (store_.confirmed.get(height, confirmed) &&
114+
confirmed.header_fk == link)
115+
return height;
116+
}
117+
118+
return zero;
119+
}
120+
121+
} // namespace database
122+
} // namespace libbitcoin
123+
124+
#endif

include/bitcoin/database/query.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
711711
#include <bitcoin/database/impl/query/extent.ipp>
712712
#include <bitcoin/database/impl/query/height.ipp>
713713
#include <bitcoin/database/impl/query/initialize.ipp>
714+
#include <bitcoin/database/impl/query/network.ipp>
714715
#include <bitcoin/database/impl/query/objects.ipp>
715716
#include <bitcoin/database/impl/query/optional.ipp>
716717
#include <bitcoin/database/impl/query/translate.ipp>

test/query/network.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2011-2025 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 "../test.hpp"
20+
#include "../mocks/blocks.hpp"
21+
#include "../mocks/chunk_store.hpp"
22+
23+
struct query_network_setup_fixture
24+
{
25+
DELETE_COPY_MOVE(query_network_setup_fixture);
26+
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
27+
28+
query_network_setup_fixture() NOEXCEPT
29+
{
30+
BOOST_REQUIRE(test::clear(test::directory));
31+
}
32+
33+
~query_network_setup_fixture() NOEXCEPT
34+
{
35+
BOOST_REQUIRE(test::clear(test::directory));
36+
}
37+
38+
BC_POP_WARNING()
39+
};
40+
41+
BOOST_FIXTURE_TEST_SUITE(query_network_tests, query_network_setup_fixture)
42+
43+
const auto events_handler = [](auto, auto) {};
44+
45+
46+
BOOST_AUTO_TEST_CASE(query_network_test)
47+
{
48+
BOOST_REQUIRE(true);
49+
}
50+
51+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)