Skip to content

Commit daa092a

Browse files
committed
Merge header.milestone into header.parent_fk, style.
1 parent 1620ea7 commit daa092a

5 files changed

Lines changed: 105 additions & 73 deletions

File tree

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

Lines changed: 91 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,61 @@ namespace table {
3333
struct header
3434
: public hash_map<schema::header>
3535
{
36+
using head = schema::header::link;
3637
using hash_map<schema::header>::hashmap;
38+
static constexpr auto offset = head::bits;
39+
static_assert(offset < to_bits(head::size));
40+
41+
static constexpr size_t skip_to_height =
42+
context::flag_t::size;
43+
44+
static constexpr size_t skip_to_mtp =
45+
skip_to_height +
46+
context::height_t::size;
47+
48+
static constexpr size_t skip_to_parent =
49+
skip_to_mtp +
50+
sizeof(uint32_t);
51+
52+
static constexpr size_t skip_to_version =
53+
skip_to_parent +
54+
link::size;
55+
56+
static constexpr size_t skip_to_timestamp =
57+
skip_to_version +
58+
sizeof(uint32_t);
59+
60+
static constexpr size_t skip_to_bits =
61+
skip_to_timestamp +
62+
sizeof(uint32_t);
63+
64+
static constexpr head::integer merge(bool milestone,
65+
head::integer parent_fk) NOEXCEPT
66+
{
67+
using namespace system;
68+
BC_ASSERT_MSG(!get_right(parent_fk, offset), "overflow");
69+
return set_right(parent_fk, offset, milestone);
70+
}
71+
72+
static constexpr bool is_milestone(link::integer merged) NOEXCEPT
73+
{
74+
return system::get_right(merged, offset);
75+
}
76+
77+
static constexpr link::integer to_parent(link::integer merged) NOEXCEPT
78+
{
79+
return system::set_right(merged, offset, false);
80+
}
3781

3882
struct record
3983
: public schema::header
4084
{
4185
inline bool from_data(reader& source) NOEXCEPT
4286
{
4387
context::from_data(source, ctx);
44-
milestone = to_bool(source.read_byte());
45-
parent_fk = source.read_little_endian<link::integer, link::size>();
88+
const auto merged = source.read_little_endian<link::integer, link::size>();
89+
milestone = is_milestone(merged);
90+
parent_fk = to_parent(merged);
4691
version = source.read_little_endian<uint32_t>();
4792
timestamp = source.read_little_endian<uint32_t>();
4893
bits = source.read_little_endian<uint32_t>();
@@ -55,8 +100,7 @@ struct header
55100
inline bool to_data(finalizer& sink) const NOEXCEPT
56101
{
57102
context::to_data(sink, ctx);
58-
sink.write_byte(to_int<uint8_t>(milestone));
59-
sink.write_little_endian<link::integer, link::size>(parent_fk);
103+
sink.write_little_endian<link::integer, link::size>(merge(milestone, parent_fk));
60104
sink.write_little_endian<uint32_t>(version);
61105
sink.write_little_endian<uint32_t>(timestamp);
62106
sink.write_little_endian<uint32_t>(bits);
@@ -96,8 +140,7 @@ struct header
96140
inline bool to_data(finalizer& sink) const NOEXCEPT
97141
{
98142
context::to_data(sink, ctx);
99-
sink.write_byte(to_int<uint8_t>(milestone));
100-
sink.write_little_endian<link::integer, link::size>(parent_fk);
143+
sink.write_little_endian<link::integer, link::size>(merge(milestone, parent_fk));
101144
sink.write_little_endian<uint32_t>(header.version());
102145
sink.write_little_endian<uint32_t>(header.timestamp());
103146
sink.write_little_endian<uint32_t>(header.bits());
@@ -144,109 +187,117 @@ struct header
144187
key key{};
145188
};
146189

147-
struct get_version
190+
struct record_context
148191
: public schema::header
149192
{
150193
inline bool from_data(reader& source) NOEXCEPT
151194
{
152-
source.skip_bytes(context::size + schema::bit + link::size);
153-
version = source.read_little_endian<uint32_t>();
195+
context::from_data(source, ctx);
154196
return source;
155197
}
156198

157-
uint32_t version{};
199+
context ctx{};
158200
};
159201

160-
struct get_timestamp
202+
struct get_flags
161203
: public schema::header
162204
{
205+
using flag_t = context::flag_t;
163206
inline bool from_data(reader& source) NOEXCEPT
164207
{
165-
source.skip_bytes(context::size + schema::bit + link::size +
166-
sizeof(uint32_t));
167-
timestamp = source.read_little_endian<uint32_t>();
208+
flags = source.read_little_endian<flag_t::integer, flag_t::size>();
168209
return source;
169210
}
170211

171-
uint32_t timestamp{};
212+
flag_t::integer flags{};
172213
};
173214

174-
struct get_bits
215+
struct get_height
175216
: public schema::header
176217
{
218+
using height_t = context::height_t;
177219
inline bool from_data(reader& source) NOEXCEPT
178220
{
179-
source.skip_bytes(context::size + schema::bit + link::size +
180-
sizeof(uint32_t) + sizeof(uint32_t));
181-
bits = source.read_little_endian<uint32_t>();
221+
source.skip_bytes(skip_to_height);
222+
height = source.read_little_endian<height_t::integer, height_t::size>();
182223
return source;
183224
}
184225

185-
uint32_t bits{};
226+
height_t::integer height{};
227+
};
228+
229+
struct get_mtp
230+
: public schema::header
231+
{
232+
inline bool from_data(reader& source) NOEXCEPT
233+
{
234+
source.skip_bytes(skip_to_mtp);
235+
mtp = source.read_little_endian<uint32_t>();
236+
return source;
237+
}
238+
239+
context::mtp_t mtp{};
186240
};
187241

188242
struct get_parent_fk
189243
: public schema::header
190244
{
191245
inline bool from_data(reader& source) NOEXCEPT
192246
{
193-
source.skip_bytes(context::size + schema::bit);
194-
parent_fk = source.read_little_endian<link::integer, link::size>();
247+
source.skip_bytes(skip_to_parent);
248+
parent_fk = to_parent(source.read_little_endian<link::integer, link::size>());
195249
return source;
196250
}
197251

198252
link::integer parent_fk{};
199253
};
200254

201-
struct get_flags
255+
struct get_version
202256
: public schema::header
203257
{
204-
using flag_t = context::flag_t;
205-
206258
inline bool from_data(reader& source) NOEXCEPT
207259
{
208-
flags = source.read_little_endian<flag_t::integer, flag_t::size>();
260+
source.skip_bytes(skip_to_version);
261+
version = source.read_little_endian<uint32_t>();
209262
return source;
210263
}
211264

212-
flag_t::integer flags{};
265+
uint32_t version{};
213266
};
214267

215-
struct get_height
268+
struct get_timestamp
216269
: public schema::header
217270
{
218-
using height_t = context::height_t;
219-
220271
inline bool from_data(reader& source) NOEXCEPT
221272
{
222-
source.skip_bytes(context::flag_t::size);
223-
height = source.read_little_endian<height_t::integer, height_t::size>();
273+
source.skip_bytes(skip_to_timestamp);
274+
timestamp = source.read_little_endian<uint32_t>();
224275
return source;
225276
}
226277

227-
height_t::integer height{};
278+
uint32_t timestamp{};
228279
};
229280

230-
struct get_mtp
281+
struct get_bits
231282
: public schema::header
232283
{
233284
inline bool from_data(reader& source) NOEXCEPT
234285
{
235-
source.skip_bytes(context::flag_t::size + context::height_t::size);
236-
mtp = source.read_little_endian<uint32_t>();
286+
source.skip_bytes(skip_to_bits);
287+
bits = source.read_little_endian<uint32_t>();
237288
return source;
238289
}
239290

240-
context::mtp_t mtp{};
291+
uint32_t bits{};
241292
};
242293

243294
struct get_milestone
244295
: public schema::header
245296
{
246297
inline bool from_data(reader& source) NOEXCEPT
247298
{
248-
source.skip_bytes(context::size);
249-
milestone = to_bool(source.read_byte());
299+
source.skip_bytes(skip_to_parent);
300+
milestone = is_milestone(source.read_little_endian<link::integer, link::size>());
250301
return source;
251302
}
252303

@@ -261,7 +312,7 @@ struct header
261312
source.rewind_bytes(sk);
262313
key = source.read_hash();
263314
context::from_data(source, ctx);
264-
source.skip_bytes(schema::bit + link::size + sizeof(uint32_t));
315+
source.skip_bytes(skip_to_timestamp - skip_to_parent);
265316
timestamp = source.read_little_endian<uint32_t>();
266317
return source;
267318
}
@@ -270,18 +321,6 @@ struct header
270321
context ctx{};
271322
uint32_t timestamp{};
272323
};
273-
274-
struct record_context
275-
: public schema::header
276-
{
277-
inline bool from_data(reader& source) NOEXCEPT
278-
{
279-
context::from_data(source, ctx);
280-
return source;
281-
}
282-
283-
context ctx{};
284-
};
285324
};
286325

287326
} // namespace table

include/bitcoin/database/tables/schema.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ struct header
6464
{
6565
static constexpr size_t sk = schema::hash;
6666
static constexpr size_t pk = schema::block;
67-
using link = linkage<pk, sub1(to_bits(pk))>; // reduced for strong_tx merge.
68-
using key = system::data_array<sk>;
67+
using link = linkage<pk, sub1(to_bits(pk))>; // reduced for milestone
68+
using key = system::data_array<sk>; // ...and strong_tx merges.
6969
static constexpr size_t minsize =
7070
schema::flags + // context.flags
7171
schema::height_ + // context.height
7272
sizeof(uint32_t) + // context.mtp
73-
schema::bit + // milestone [TODO: merge into parent pk]
73+
///schema::bit + // milestone (merged into parent pk)
7474
pk + // parent.pk
7575
sizeof(uint32_t) + // version
7676
sizeof(uint32_t) + // timestamp
@@ -81,8 +81,8 @@ struct header
8181
static constexpr size_t size = minsize;
8282
static constexpr size_t cell = sizeof(unsigned_type<link::size>);
8383
static constexpr link count() NOEXCEPT { return 1; }
84-
static_assert(minsize == 63u);
85-
static_assert(minrow == 98u);
84+
static_assert(minsize == 62u);
85+
static_assert(minrow == 97u);
8686
static_assert(link::size == 3u);
8787
static_assert(cell == 4u);
8888
};

test/query/archive_read.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_header__invalid_parent__expected)
125125
"14131211" // flags
126126
"040302" // height
127127
"24232221" // mtp
128-
"424242" // previous_block_hash (header_fk - invalid)
128+
"424242" // previous_block_hash (header_fk - invalid) (milestone false)
129129
"34333231" // version
130130
"44434241" // timestamp
131131
"54535251" // bits
@@ -187,8 +187,7 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_header__default__expected)
187187
"14131211" // flags
188188
"040302" // height
189189
"24232221" // mtp
190-
"01" // milestone
191-
"ffff7f" // previous_block_hash (header_fk - terminal)
190+
"ffffff" // previous_block_hash (header_fk - terminal) (milestone true)
192191
"34333231" // version
193192
"44434241" // timestamp
194193
"54535251" // bits

test/query/archive_write.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ BOOST_AUTO_TEST_CASE(query_archive_write__set_link_header__is_header__expected)
118118
"04030201" // flags
119119
"141312" // height
120120
"24232221" // mtp
121-
"01" // milestone
122-
"ffff7f" // previous_block_hash (header_fk - not found)
121+
"ffffff" // previous_block_hash (header_fk - not found) (milestone true)
123122
"34333231" // version
124123
"44434241" // timestamp
125124
"54535251" // bits
@@ -463,8 +462,7 @@ BOOST_AUTO_TEST_CASE(query_archive_write__set_block__get_block__expected)
463462
"04030201" // flags
464463
"141312" // height
465464
"24232221" // mtp
466-
"01" // milestone
467-
"ffff7f" // previous_block_hash (header_fk - not found)
465+
"ffffff" // previous_block_hash (header_fk - not found) (milestone true)
468466
"01000000" // version
469467
"29ab5f49" // timestamp
470468
"ffff001d" // bits
@@ -611,8 +609,7 @@ BOOST_AUTO_TEST_CASE(query_archive_write__set_block_txs__get_block__expected)
611609
"04030201" // flags
612610
"141312" // height
613611
"24232221" // mtp
614-
"01" // milestone
615-
"ffff7f" // previous_block_hash (header_fk - not found)
612+
"ffffff" // previous_block_hash (header_fk - not found) (milestone true)
616613
"01000000" // version
617614
"29ab5f49" // timestamp
618615
"ffff001d" // bits
@@ -1021,7 +1018,7 @@ BOOST_AUTO_TEST_CASE(query_archive_write__get_header__invalid_parent__expected)
10211018
"14131211" // flags
10221019
"040302" // height
10231020
"24232221" // mtp
1024-
"424242" // previous_block_hash (header_fk - invalid)
1021+
"424242" // previous_block_hash (header_fk - invalid) (milestone false)
10251022
"34333231" // version
10261023
"44434241" // timestamp
10271024
"54535251" // bits
@@ -1083,8 +1080,7 @@ BOOST_AUTO_TEST_CASE(query_archive_write__get_header__default__expected)
10831080
"14131211" // flags
10841081
"040302" // height
10851082
"24232221" // mtp
1086-
"01" // milestone
1087-
"ffff7f" // previous_block_hash (header_fk - terminal)
1083+
"ffffff" // previous_block_hash (header_fk - terminal) (milestone true)
10881084
"34333231" // version
10891085
"44434241" // timestamp
10901086
"54535251" // bits

test/tables/archives/header.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ const data_chunk expected_file
6464
0x00, 0x00, 0x00, 0x00,
6565
0x00, 0x00, 0x00,
6666
0x00, 0x00, 0x00, 0x00,
67-
0x00,
68-
0x00, 0x00, 0x00,
67+
0x00, 0x00, 0x00, // milestone merged (false)
6968
0x00, 0x00, 0x00, 0x00,
7069
0x00, 0x00, 0x00, 0x00,
7170
0x00, 0x00, 0x00, 0x00,
@@ -86,8 +85,7 @@ const data_chunk expected_file
8685
0x02, 0x12, 0x34, 0x56,
8786
0x01, 0x12, 0x34,
8887
0x03, 0x12, 0x34, 0x56,
89-
0x01,
90-
0x04, 0x12, 0x34,
88+
0x04, 0x12, 0xb4, // milestone merged (true)
9189
0x05, 0x12, 0x34, 0x56,
9290
0x06, 0x12, 0x34, 0x56,
9391
0x07, 0x12, 0x34, 0x56,

0 commit comments

Comments
 (0)