Skip to content

Commit e6fcb9d

Browse files
authored
Merge pull request #741 from evoskuil/master
Use writer vs. flipper interface in wire serializers.
2 parents f138ee2 + c0aa1b5 commit e6fcb9d

9 files changed

Lines changed: 63 additions & 130 deletions

File tree

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

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -33,162 +33,113 @@ namespace database {
3333
// This normalized approach is also the most efficient.
3434

3535
TEMPLATE
36-
bool CLASS::get_wire_header(byteflipper& flipper,
36+
bool CLASS::get_wire_header(bytewriter& sink,
3737
const header_link& link) const NOEXCEPT
3838
{
39-
const auto start = flipper.get_write_position();
40-
table::header::wire_header header{ {}, flipper };
41-
if (!store_.header.get(link, header))
42-
{
43-
flipper.invalidate();
44-
return false;
45-
}
46-
47-
// Genesis header parent is defaulted.
48-
if (header.parent_fk == schema::header::link::terminal)
49-
return true;
50-
51-
flipper.set_position(start);
52-
table::header::wire_key key{ {}, flipper };
53-
if (!store_.header.get(header.parent_fk, key))
54-
{
55-
flipper.invalidate();
56-
return false;
57-
}
58-
59-
return true;
39+
// Double read of header table is small, prevents need for writer rewind.
40+
const auto parent = to_parent(link);
41+
table::header::wire_header header{ {}, sink, get_header_key(parent) };
42+
return store_.header.get(link, header);
6043
}
6144

6245
TEMPLATE
63-
bool CLASS::get_wire_input(byteflipper& flipper,
46+
bool CLASS::get_wire_input(bytewriter& sink,
6447
const point_link& link) const NOEXCEPT
6548
{
66-
// [point]
67-
table::point::wire_point point{ {}, flipper };
49+
table::point::wire_point point{ {}, sink };
6850
if (!store_.point.get(link, point))
69-
{
70-
flipper.invalidate();
7151
return false;
72-
}
7352

74-
// [[size]script]
7553
table::ins::get_input ins{};
76-
table::input::wire_script script{ {}, flipper };
54+
table::input::wire_script script{ {}, sink };
7755
if (!store_.ins.get(link, ins) ||
7856
!store_.input.get(ins.input_fk, script))
79-
{
80-
flipper.invalidate();
8157
return false;
82-
}
8358

84-
// [sequence]
85-
flipper.write_4_bytes_little_endian(ins.sequence);
59+
sink.write_4_bytes_little_endian(ins.sequence);
8660
return true;
8761
}
8862

8963
TEMPLATE
90-
bool CLASS::get_wire_output(byteflipper& flipper,
64+
bool CLASS::get_wire_output(bytewriter& sink,
9165
const output_link& link) const NOEXCEPT
9266
{
93-
// [value][[[size]script]]
94-
table::output::wire_script out{ {}, flipper };
95-
if (!store_.output.get(link, out))
96-
{
97-
flipper.invalidate();
98-
return false;
99-
}
100-
101-
return true;
67+
table::output::wire_script out{ {}, sink };
68+
return store_.output.get(link, out);
10269
}
10370

10471
TEMPLATE
105-
bool CLASS::get_wire_witness(byteflipper& flipper,
72+
bool CLASS::get_wire_witness(bytewriter& sink,
10673
const point_link& link) const NOEXCEPT
10774
{
108-
// [count][[[size]element]]
10975
table::ins::get_input ins{};
110-
table::input::wire_witness wire{ {}, flipper };
111-
if (!store_.ins.get(link, ins) ||
112-
!store_.input.get(ins.input_fk, wire))
113-
{
114-
flipper.invalidate();
115-
return false;
116-
}
117-
118-
return true;
76+
table::input::wire_witness wire{ {}, sink };
77+
return store_.ins.get(link, ins)
78+
&& store_.input.get(ins.input_fk, wire);
11979
}
12080

12181
TEMPLATE
122-
bool CLASS::get_wire_tx(byteflipper& flipper, const tx_link& link,
82+
bool CLASS::get_wire_tx(bytewriter& sink, const tx_link& link,
12383
bool witness) const NOEXCEPT
12484
{
12585
table::transaction::record tx{};
12686
if (!store_.tx.get(link, tx))
127-
{
128-
flipper.invalidate();
12987
return false;
130-
}
13188

13289
table::outs::record outs{};
13390
outs.out_fks.resize(tx.outs_count);
13491
if (!store_.outs.get(tx.outs_fk, outs))
135-
{
136-
flipper.invalidate();
13792
return false;
138-
}
13993

14094
// Point links are contiguous (computed).
14195
const auto ins_begin = tx.point_fk;
14296
const auto ins_count = tx.ins_count;
14397
const auto ins_final = ins_begin + ins_count;
14498
const auto witnessed = witness && (tx.heavy != tx.light);
14599

146-
flipper.write_4_bytes_little_endian(tx.version);
100+
sink.write_4_bytes_little_endian(tx.version);
147101

148102
if (witnessed)
149103
{
150-
flipper.write_byte(system::chain::witness_marker);
151-
flipper.write_byte(system::chain::witness_enabled);
104+
sink.write_byte(system::chain::witness_marker);
105+
sink.write_byte(system::chain::witness_enabled);
152106
}
153107

154-
flipper.write_variable(ins_count);
108+
sink.write_variable(ins_count);
155109
for (auto fk = ins_begin; fk < ins_final; ++fk)
156-
if (!get_wire_input(flipper, fk))
110+
if (!get_wire_input(sink, fk))
157111
return false;
158112

159-
flipper.write_variable(outs.out_fks.size());
113+
sink.write_variable(outs.out_fks.size());
160114
for (const auto& fk: outs.out_fks)
161-
if (!get_wire_output(flipper, fk))
115+
if (!get_wire_output(sink, fk))
162116
return false;
163117

164118
if (witnessed)
165119
{
166120
for (auto fk = ins_begin; fk < ins_final; ++fk)
167-
if (!get_wire_witness(flipper, fk))
121+
if (!get_wire_witness(sink, fk))
168122
return false;
169123
}
170124

171-
flipper.write_4_bytes_little_endian(tx.locktime);
125+
sink.write_4_bytes_little_endian(tx.locktime);
172126
return true;
173127
}
174128

175129
TEMPLATE
176-
bool CLASS::get_wire_block(byteflipper& flipper, const header_link& link,
130+
bool CLASS::get_wire_block(bytewriter& sink, const header_link& link,
177131
bool witness) const NOEXCEPT
178132
{
179-
if (!get_wire_header(flipper, link))
133+
if (!get_wire_header(sink, link))
180134
return false;
181135

182136
const auto txs = to_transactions(link);
183137
if (txs.empty())
184-
{
185-
flipper.invalidate();
186138
return false;
187-
}
188139

189-
flipper.write_variable(txs.size());
140+
sink.write_variable(txs.size());
190141
for (const auto& tx_link: txs)
191-
if (!get_wire_tx(flipper, tx_link, witness))
142+
if (!get_wire_tx(sink, tx_link, witness))
192143
return false;
193144

194145
return true;

include/bitcoin/database/memory/streamers.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ namespace libbitcoin {
2626
namespace database {
2727

2828
/// These all operate over a system::iostream.
29-
3029
using reader = system::byte_reader<system::iostream<>>;
3130
using writer = system::byte_writer<system::iostream<>>;
3231
using flipper = system::byte_flipper<system::iostream<>>;
3332

33+
/// Interfaces.
34+
using bytewriter = system::bytewriter;
35+
3436
} // namespace database
3537
} // namespace libbitcoin
3638

include/bitcoin/database/query.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,13 @@ class query
373373
/// Wire.
374374
/// -----------------------------------------------------------------------
375375

376-
bool get_wire_input(byteflipper& flipper, const point_link& link) const NOEXCEPT;
377-
bool get_wire_output(byteflipper& flipper, const output_link& link) const NOEXCEPT;
378-
bool get_wire_witness(byteflipper& flipper, const point_link& link) const NOEXCEPT;
379-
bool get_wire_header(byteflipper& flipper, const header_link& link) const NOEXCEPT;
380-
bool get_wire_tx(byteflipper& flipper, const tx_link& link, bool witness) const NOEXCEPT;
381-
bool get_wire_block(byteflipper& flipper, const header_link& link,
376+
bool get_wire_input(bytewriter& sink, const point_link& link) const NOEXCEPT;
377+
bool get_wire_output(bytewriter& sink, const output_link& link) const NOEXCEPT;
378+
bool get_wire_witness(bytewriter& sink, const point_link& link) const NOEXCEPT;
379+
bool get_wire_header(bytewriter& sink, const header_link& link) const NOEXCEPT;
380+
bool get_wire_tx(bytewriter& sink, const tx_link& link,
381+
bool witness) const NOEXCEPT;
382+
bool get_wire_block(bytewriter& sink, const header_link& link,
382383
bool witness) const NOEXCEPT;
383384

384385
data_chunk get_wire_header(const header_link& link) const NOEXCEPT;

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -322,43 +322,25 @@ struct header
322322
uint32_t timestamp{};
323323
};
324324

325-
struct wire_key
326-
: public schema::header
327-
{
328-
inline bool from_data(reader& source) NOEXCEPT
329-
{
330-
const auto time_bits_nonce_size = 3u * sizeof(uint32_t);
331-
const auto version_size = sizeof(uint32_t);
332-
source.rewind_bytes(sk);
333-
flipper.skip_bytes(version_size);
334-
flipper.write_bytes(source.read_hash());
335-
flipper.skip_bytes(schema::hash + time_bits_nonce_size);
336-
return source;
337-
}
338-
339-
system::byteflipper& flipper;
340-
};
341-
342325
struct wire_header
343326
: public schema::header
344327
{
345328
inline bool from_data(reader& source) NOEXCEPT
346329
{
347330
const auto time_bits_nonce_size = 3u * sizeof(uint32_t);
348331
const auto version_size = sizeof(uint32_t);
349-
source.skip_bytes(skip_to_parent);
350-
parent_fk = to_parent(source.read_little_endian<link::integer, link::size>());
351-
flipper.write_bytes(source.read_bytes(version_size));
352-
flipper.write_bytes(system::null_hash);
332+
source.skip_bytes(skip_to_version);
333+
sink.write_bytes(source.read_bytes(version_size));
334+
sink.write_bytes(parent_hash);
353335
source.skip_bytes(time_bits_nonce_size);
354-
flipper.write_bytes(source.read_hash());
336+
sink.write_bytes(source.read_hash());
355337
source.rewind_bytes(time_bits_nonce_size + schema::hash);
356-
flipper.write_bytes(source.read_bytes(time_bits_nonce_size));
338+
sink.write_bytes(source.read_bytes(time_bits_nonce_size));
357339
return source;
358340
}
359341

360-
system::byteflipper& flipper;
361-
link::integer parent_fk{};
342+
bytewriter& sink;
343+
hash_digest parent_hash{};
362344
};
363345
};
364346

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ struct input
183183
{
184184
// script (prefixed)
185185
const auto length = source.read_size();
186-
flipper.write_variable(length);
187-
flipper.write_bytes(source.read_bytes(length));
186+
sink.write_variable(length);
187+
sink.write_bytes(source.read_bytes(length));
188188
return source;
189189
}
190190

191-
system::byteflipper& flipper;
191+
bytewriter& sink;
192192
};
193193

194194
struct wire_witness
@@ -201,20 +201,20 @@ struct input
201201

202202
// witness (count)
203203
const auto count = source.read_size();
204-
flipper.write_variable(count);
204+
sink.write_variable(count);
205205

206206
// witness (prefixed)
207207
for (size_t element{}; element < count; ++element)
208208
{
209209
const auto length = source.read_size();
210-
flipper.write_variable(length);
211-
flipper.write_bytes(source.read_bytes(length));
210+
sink.write_variable(length);
211+
sink.write_bytes(source.read_bytes(length));
212212
}
213213

214214
return source;
215215
}
216216

217-
system::byteflipper& flipper;
217+
bytewriter& sink;
218218
};
219219
};
220220

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ struct ins
139139
inline bool from_data(reader& source) NOEXCEPT
140140
{
141141
const auto sequence_size = sizeof(uint32_t);
142-
flipper.write_bytes(source.read_bytes(sequence_size));
142+
sink.write_bytes(source.read_bytes(sequence_size));
143143
return source;
144144
}
145145

146-
system::byteflipper& flipper;
146+
bytewriter& sink;
147147
};
148148
};
149149

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,16 @@ struct output
240240
source.skip_bytes(tx::size);
241241

242242
// value (translates from variable to fixed width)
243-
flipper.write_8_bytes_little_endian(source.read_variable());
243+
sink.write_8_bytes_little_endian(source.read_variable());
244244

245245
// script (prefixed)
246246
const auto length = source.read_size();
247-
flipper.write_variable(length);
248-
flipper.write_bytes(source.read_bytes(length));
247+
sink.write_variable(length);
248+
sink.write_bytes(source.read_bytes(length));
249249
return source;
250250
}
251251

252-
system::byteflipper& flipper;
252+
bytewriter& sink;
253253
};
254254
};
255255

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ struct point
9999
inline bool from_data(reader& source) NOEXCEPT
100100
{
101101
source.rewind_bytes(schema::point::sk);
102-
flipper.write_bytes(source.read_hash());
102+
sink.write_bytes(source.read_hash());
103103
const auto value = source.read_little_endian<ix::integer, ix::size>();
104-
flipper.write_4_bytes_little_endian(to_index(value));
104+
sink.write_4_bytes_little_endian(to_index(value));
105105
return source;
106106
}
107107

108-
system::byteflipper& flipper;
108+
bytewriter& sink;
109109
};
110110
};
111111

include/bitcoin/database/types.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ using outpoint = system::chain::outpoint;
5252
using outpoints = std::set<outpoint>;
5353

5454
using data_chunk = system::data_chunk;
55-
using bytereader = system::bytereader;
56-
using bytewriter = system::bytewriter;
57-
using byteflipper = system::byteflipper;
5855

5956
struct header_state { header_link link; code ec; };
6057
using header_states = std::vector<header_state>;

0 commit comments

Comments
 (0)