forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwire.ipp
More file actions
253 lines (214 loc) · 6.48 KB
/
wire.ipp
File metadata and controls
253 lines (214 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_DATABASE_QUERY_WIRE_IPP
#define LIBBITCOIN_DATABASE_QUERY_WIRE_IPP
#include <algorithm>
#include <utility>
#include <bitcoin/database/define.hpp>
namespace libbitcoin {
namespace database {
// Wire serialized objects.
// ----------------------------------------------------------------------------
// Due to the idiotic segwit serialization of witness after output there is are
// duplicated navigations to store_.ins and store_.input by the witness reader.
// This normalized approach is also the most efficient.
TEMPLATE
bool CLASS::get_wire_header(byteflipper& flipper,
const header_link& link) const NOEXCEPT
{
const auto start = flipper.get_write_position();
table::header::wire_header header{ {}, flipper };
if (!store_.header.get(link, header))
{
flipper.invalidate();
return false;
}
// Genesis header parent is defaulted.
if (header.parent_fk == schema::header::link::terminal)
return true;
flipper.set_position(start);
table::header::wire_key key{ {}, flipper };
if (!store_.header.get(header.parent_fk, key))
{
flipper.invalidate();
return false;
}
return true;
}
TEMPLATE
bool CLASS::get_wire_input(byteflipper& flipper,
const point_link& link) const NOEXCEPT
{
// [point]
table::point::wire_point point{ {}, flipper };
if (!store_.point.get(link, point))
{
flipper.invalidate();
return false;
}
// [[size]script]
table::ins::get_input ins{};
table::input::wire_script script{ {}, flipper };
if (!store_.ins.get(link, ins) ||
!store_.input.get(ins.input_fk, script))
{
flipper.invalidate();
return false;
}
// [sequence]
flipper.write_4_bytes_little_endian(ins.sequence);
return true;
}
TEMPLATE
bool CLASS::get_wire_output(byteflipper& flipper,
const output_link& link) const NOEXCEPT
{
// [value][[[size]script]]
table::output::wire_script out{ {}, flipper };
if (!store_.output.get(link, out))
{
flipper.invalidate();
return false;
}
return true;
}
TEMPLATE
bool CLASS::get_wire_witness(byteflipper& flipper,
const point_link& link) const NOEXCEPT
{
// [count][[[size]element]]
table::ins::get_input ins{};
table::input::wire_witness wire{ {}, flipper };
if (!store_.ins.get(link, ins) ||
!store_.input.get(ins.input_fk, wire))
{
flipper.invalidate();
return false;
}
return true;
}
TEMPLATE
bool CLASS::get_wire_tx(byteflipper& flipper, const tx_link& link,
bool witness) const NOEXCEPT
{
table::transaction::record tx{};
if (!store_.tx.get(link, tx))
{
flipper.invalidate();
return false;
}
table::outs::record outs{};
outs.out_fks.resize(tx.outs_count);
if (!store_.outs.get(tx.outs_fk, outs))
{
flipper.invalidate();
return false;
}
// Point links are contiguous (computed).
const auto ins_begin = tx.point_fk;
const auto ins_count = tx.ins_count;
const auto ins_final = ins_begin + ins_count;
const auto witnessed = witness && (tx.heavy != tx.light);
flipper.write_4_bytes_little_endian(tx.version);
if (witnessed)
{
flipper.write_byte(system::chain::witness_marker);
flipper.write_byte(system::chain::witness_enabled);
}
flipper.write_variable(ins_count);
for (auto fk = ins_begin; fk < ins_final; ++fk)
if (!get_wire_input(flipper, fk))
return false;
flipper.write_variable(outs.out_fks.size());
for (const auto& fk: outs.out_fks)
if (!get_wire_output(flipper, fk))
return false;
if (witnessed)
{
for (auto fk = ins_begin; fk < ins_final; ++fk)
if (!get_wire_witness(flipper, fk))
return false;
}
flipper.write_4_bytes_little_endian(tx.locktime);
return true;
}
TEMPLATE
bool CLASS::get_wire_block(byteflipper& flipper, const header_link& link,
bool witness) const NOEXCEPT
{
if (!get_wire_header(flipper, link))
return false;
const auto txs = to_transactions(link);
if (txs.empty())
{
flipper.invalidate();
return false;
}
flipper.write_variable(txs.size());
for (const auto& tx_link: txs)
if (!get_wire_tx(flipper, tx_link, witness))
return false;
return true;
}
// These convenience wrappers are made practical by size caching for block and
// tx for both nominal and witness wire encodings (and fixed size headers).
// Intermediate objects (input, output, witness) have a size prefix
TEMPLATE
data_chunk CLASS::get_wire_header(const header_link& link) const NOEXCEPT
{
using namespace system;
data_chunk data(chain::header::serialized_size());
stream::flip::fast ostream(data);
flip::bytes::fast out(ostream);
if (!get_wire_header(out, link) || !out)
return {};
return data;
}
TEMPLATE
data_chunk CLASS::get_wire_tx(const tx_link& link, bool witness) const NOEXCEPT
{
using namespace system;
size_t size{};
if (!get_tx_size(size, link, witness))
return {};
data_chunk data(size);
stream::flip::fast ostream(data);
flip::bytes::fast out(ostream);
if (!get_wire_tx(out, link, witness) || !out)
return {};
return data;
}
TEMPLATE
data_chunk CLASS::get_wire_block(const header_link& link,
bool witness) const NOEXCEPT
{
using namespace system;
size_t size{};
if (!get_block_size(size, link, witness))
return {};
data_chunk data(size);
stream::flip::fast ostream(data);
flip::bytes::fast out(ostream);
if (!get_wire_block(out, link, witness) || !out)
return {};
return data;
}
} // namespace database
} // namespace libbitcoin
#endif