Skip to content

Commit b1d3cf1

Browse files
committed
Add/test initial wire reader (get_wire_header()).
1 parent 9fd983e commit b1d3cf1

14 files changed

Lines changed: 365 additions & 88 deletions

File tree

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ test_libbitcoin_database_test_SOURCES = \
101101
test/query/optional.cpp \
102102
test/query/translate.cpp \
103103
test/query/validate.cpp \
104+
test/query/wire.cpp \
104105
test/tables/archives/header.cpp \
105106
test/tables/archives/input.cpp \
106107
test/tables/archives/output.cpp \
@@ -192,7 +193,8 @@ include_bitcoin_database_impl_query_HEADERS = \
192193
include/bitcoin/database/impl/query/optional.ipp \
193194
include/bitcoin/database/impl/query/query.ipp \
194195
include/bitcoin/database/impl/query/translate.ipp \
195-
include/bitcoin/database/impl/query/validate.ipp
196+
include/bitcoin/database/impl/query/validate.ipp \
197+
include/bitcoin/database/impl/query/wire.ipp
196198

197199
include_bitcoin_database_locksdir = ${includedir}/bitcoin/database/locks
198200
include_bitcoin_database_locks_HEADERS = \

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
<ClCompile Include="..\..\..\..\test\query\optional.cpp" />
165165
<ClCompile Include="..\..\..\..\test\query\translate.cpp" />
166166
<ClCompile Include="..\..\..\..\test\query\validate.cpp" />
167+
<ClCompile Include="..\..\..\..\test\query\wire.cpp" />
167168
<ClCompile Include="..\..\..\..\test\settings.cpp" />
168169
<ClCompile Include="..\..\..\..\test\store.cpp" />
169170
<ClCompile Include="..\..\..\..\test\tables\archives\header.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
@@ -153,6 +153,9 @@
153153
<ClCompile Include="..\..\..\..\test\query\validate.cpp">
154154
<Filter>src\query</Filter>
155155
</ClCompile>
156+
<ClCompile Include="..\..\..\..\test\query\wire.cpp">
157+
<Filter>src\query</Filter>
158+
</ClCompile>
156159
<ClCompile Include="..\..\..\..\test\settings.cpp">
157160
<Filter>src</Filter>
158161
</ClCompile>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
<None Include="..\..\..\..\include\bitcoin\database\impl\query\query.ipp" />
231231
<None Include="..\..\..\..\include\bitcoin\database\impl\query\translate.ipp" />
232232
<None Include="..\..\..\..\include\bitcoin\database\impl\query\validate.ipp" />
233+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\wire.ipp" />
233234
<None Include="..\..\..\..\include\bitcoin\database\impl\store.ipp" />
234235
<None Include="packages.config" />
235236
</ItemGroup>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@
379379
<None Include="..\..\..\..\include\bitcoin\database\impl\query\validate.ipp">
380380
<Filter>include\bitcoin\database\impl\query</Filter>
381381
</None>
382+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\wire.ipp">
383+
<Filter>include\bitcoin\database\impl\query</Filter>
384+
</None>
382385
<None Include="..\..\..\..\include\bitcoin\database\impl\store.ipp">
383386
<Filter>include\bitcoin\database\impl</Filter>
384387
</None>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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_DATABASE_QUERY_WIRE_IPP
20+
#define LIBBITCOIN_DATABASE_QUERY_WIRE_IPP
21+
22+
#include <algorithm>
23+
#include <utility>
24+
#include <bitcoin/database/define.hpp>
25+
26+
namespace libbitcoin {
27+
namespace database {
28+
29+
// Wire serialized objects.
30+
// ----------------------------------------------------------------------------
31+
32+
TEMPLATE
33+
bool CLASS::get_wire_header(byteflipper& flipper,
34+
const header_link& link) const NOEXCEPT
35+
{
36+
const auto start = flipper.get_write_position();
37+
table::header::wire_header header{ {}, flipper };
38+
if (!store_.header.get(link, header))
39+
{
40+
flipper.invalidate();
41+
return false;
42+
}
43+
44+
// Genesis header parent is defaulted, others must be looked up.
45+
if (header.parent_fk != schema::header::link::terminal)
46+
{
47+
flipper.set_position(start);
48+
table::header::wire_key key{ {}, flipper };
49+
if (!store_.header.get(header.parent_fk, key))
50+
{
51+
flipper.invalidate();
52+
return false;
53+
}
54+
55+
flipper.set_position(start + system::chain::header::serialized_size());
56+
}
57+
58+
return true;
59+
}
60+
61+
TEMPLATE
62+
bool CLASS::get_wire_tx(byteflipper& , const tx_link& ,
63+
bool ) const NOEXCEPT
64+
{
65+
return {};
66+
}
67+
68+
TEMPLATE
69+
bool CLASS::get_wire_block(byteflipper& , const header_link& ,
70+
bool ) const NOEXCEPT
71+
{
72+
return {};
73+
}
74+
75+
TEMPLATE
76+
data_chunk CLASS::get_wire_header(const header_link& link) const NOEXCEPT
77+
{
78+
using namespace system;
79+
data_chunk data(chain::header::serialized_size());
80+
stream::flip::fast ostream(data);
81+
flip::bytes::fast out(ostream);
82+
if (!get_wire_header(out, link) || !out)
83+
data.clear();
84+
85+
return data;
86+
}
87+
88+
TEMPLATE
89+
data_chunk CLASS::get_wire_tx(const tx_link& , bool ) const NOEXCEPT
90+
{
91+
return {};
92+
}
93+
94+
TEMPLATE
95+
data_chunk CLASS::get_wire_block(const header_link& , bool ) const NOEXCEPT
96+
{
97+
return {};
98+
}
99+
100+
} // namespace database
101+
} // namespace libbitcoin
102+
103+
#endif

include/bitcoin/database/query.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ class query
374374
bool get_block_spend(uint64_t& out, const header_link& link) const NOEXCEPT;
375375
bool get_block_fee(uint64_t& out, const header_link& link) const NOEXCEPT;
376376

377+
/// Wire.
378+
/// -----------------------------------------------------------------------
379+
380+
data_chunk get_wire_header(const header_link& link) const NOEXCEPT;
381+
bool get_wire_header(byteflipper& flipper,
382+
const header_link& link) const NOEXCEPT;
383+
384+
data_chunk get_wire_tx(const tx_link& link, bool witness) const NOEXCEPT;
385+
bool get_wire_tx(byteflipper& flipper, const tx_link& link,
386+
bool witness) const NOEXCEPT;
387+
388+
data_chunk get_wire_block(const header_link& link, bool witness) const NOEXCEPT;
389+
bool get_wire_block(byteflipper& flipper, const header_link& link,
390+
bool witness) const NOEXCEPT;
391+
377392
/// Objects.
378393
/// -----------------------------------------------------------------------
379394

@@ -836,6 +851,7 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
836851
#include <bitcoin/database/impl/query/merkle.ipp>
837852
#include <bitcoin/database/impl/query/translate.ipp>
838853
#include <bitcoin/database/impl/query/validate.ipp>
854+
#include <bitcoin/database/impl/query/wire.ipp>
839855

840856
BC_POP_WARNING()
841857

include/bitcoin/database/tables/archives/header.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,43 @@ struct header
321321
context ctx{};
322322
uint32_t timestamp{};
323323
};
324+
325+
struct wire_key
326+
: public schema::header
327+
{
328+
inline bool from_data(reader& source) NOEXCEPT
329+
{
330+
const auto version_size = sizeof(uint32_t);
331+
source.rewind_bytes(sk);
332+
flipper.skip_bytes(version_size);
333+
flipper.write_bytes(source.read_hash());
334+
return source;
335+
}
336+
337+
system::byteflipper& flipper;
338+
};
339+
340+
struct wire_header
341+
: public schema::header
342+
{
343+
inline bool from_data(reader& source) NOEXCEPT
344+
{
345+
const auto version_size = sizeof(uint32_t);
346+
const auto time_bits_nonce_size = 3u * sizeof(uint32_t);
347+
source.skip_bytes(skip_to_parent);
348+
parent_fk = to_parent(source.read_little_endian<link::integer, link::size>());
349+
flipper.write_bytes(source.read_bytes(version_size));
350+
flipper.write_bytes(system::null_hash);
351+
source.skip_bytes(time_bits_nonce_size);
352+
flipper.write_bytes(source.read_hash());
353+
source.rewind_bytes(time_bits_nonce_size + schema::hash);
354+
flipper.write_bytes(source.read_bytes(time_bits_nonce_size));
355+
return source;
356+
}
357+
358+
system::byteflipper& flipper;
359+
link::integer parent_fk{};
360+
};
324361
};
325362

326363
} // namespace table

include/bitcoin/database/tables/archives/input.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,47 @@ struct input
175175

176176
const system::chain::transaction& tx_{};
177177
};
178+
179+
struct wire_script
180+
: public schema::input
181+
{
182+
inline bool from_data(reader& source) NOEXCEPT
183+
{
184+
// script (prefixed)
185+
const auto length = source.read_size();
186+
flipper.write_variable(length);
187+
flipper.write_bytes(source.read_bytes(length));
188+
return source;
189+
}
190+
191+
system::byteflipper& flipper;
192+
};
193+
194+
struct wire_witness
195+
: public schema::input
196+
{
197+
inline bool from_data(reader& source) NOEXCEPT
198+
{
199+
// script (skip)
200+
source.skip_bytes(source.read_size());
201+
202+
// witness (count)
203+
const auto count = source.read_size();
204+
flipper.write_variable(count);
205+
206+
// witness (prefixed)
207+
for (size_t element{}; element < count; ++element)
208+
{
209+
const auto length = source.read_size();
210+
flipper.write_variable(length);
211+
flipper.write_bytes(source.read_bytes(length));
212+
}
213+
214+
return source;
215+
}
216+
217+
system::byteflipper& flipper;
218+
};
178219
};
179220

180221
BC_POP_WARNING()

include/bitcoin/database/tables/archives/output.hpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ struct output
4646
inline link count() const NOEXCEPT
4747
{
4848
return system::possible_narrow_cast<link::integer>(
49-
tx::size +
50-
variable_size(value) +
49+
tx::size + variable_size(value) +
5150
script.serialized_size(true));
5251
}
5352

@@ -231,6 +230,27 @@ struct output
231230
const tx::integer parent_fk{};
232231
const system::chain::transaction& tx_{};
233232
};
233+
234+
struct wire_script
235+
: public schema::output
236+
{
237+
inline bool from_data(reader& source) NOEXCEPT
238+
{
239+
// skip: parent_fk
240+
source.skip_bytes(tx::size);
241+
242+
// value
243+
flipper.write_variable(source.read_variable());
244+
245+
// script (prefixed)
246+
const auto length = source.read_size();
247+
flipper.write_variable(length);
248+
flipper.write_bytes(source.read_bytes(length));
249+
return source;
250+
}
251+
252+
system::byteflipper& flipper;
253+
};
234254
};
235255

236256
BC_POP_WARNING()

0 commit comments

Comments
 (0)