• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 9310800761

31 May 2024 12:08AM UTC coverage: 82.732% (-0.01%) from 82.746%
9310800761

push

github

web-flow
Merge pull request #1467 from evoskuil/master

Performance optimizations, style, comments.

233 of 289 new or added lines in 11 files covered. (80.62%)

8 existing lines in 4 files now uncovered.

9812 of 11860 relevant lines covered (82.73%)

4808594.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.74
/src/chain/input.cpp
1
/**
2
 * Copyright (c) 2011-2023 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 <bitcoin/system/chain/input.hpp>
20

21
#include <algorithm>
22
#include <memory>
23
#include <utility>
24
#include <bitcoin/system/chain/context.hpp>
25
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
26
#include <bitcoin/system/chain/point.hpp>
27
#include <bitcoin/system/chain/prevout.hpp>
28
#include <bitcoin/system/chain/script.hpp>
29
#include <bitcoin/system/chain/witness.hpp>
30
#include <bitcoin/system/define.hpp>
31
#include <bitcoin/system/math/math.hpp>
32
#include <bitcoin/system/stream/stream.hpp>
33

34
namespace libbitcoin {
35
namespace system {
36
namespace chain {
37

38
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
39

40
// Product overflows guarded by script size limit.
41
static_assert(max_script_size < 
42
    max_size_t / multisig_default_sigops / heavy_sigops_factor,
43
    "input sigop overflow guard");
44

45
// Constructors.
46
// ----------------------------------------------------------------------------
47

48
// Default point is null_hash and point::null_index. 
49
// Default metadata is spent, invalid, max_size_t value. 
50
input::input() NOEXCEPT
22✔
51
  : input(
52
      to_shared<chain::point>(),
22✔
53
      to_shared<chain::script>(),
22✔
54
      to_shared<chain::witness>(),
22✔
55
      0, false)
88✔
56
{
57
}
22✔
58

59
input::input(chain::point&& point, chain::script&& script,
73✔
60
    uint32_t sequence) NOEXCEPT
73✔
61
  : input(
62
      to_shared(std::move(point)),
73✔
63
      to_shared(std::move(script)),
73✔
64
      to_shared<chain::witness>(),
73✔
65
      sequence, true)
146✔
66
{
67
}
73✔
68

69
input::input(const chain::point& point, const chain::script& script,
734✔
70
    uint32_t sequence) NOEXCEPT
734✔
71
  : input(
72
      to_shared(point),
734✔
73
      to_shared(script),
734✔
74
      to_shared<chain::witness>(),
734✔
75
      sequence, true)
1,468✔
76
{
77
}
734✔
78

79
input::input(const chain::point::cptr& point,
×
80
    const chain::script::cptr& script, uint32_t sequence) NOEXCEPT
×
81
  : input(
82
      point ? point : to_shared<chain::point>(),
×
83
      script ? script : to_shared<chain::script>(),
×
84
      to_shared<chain::witness>(),
×
85
      sequence, true)
×
86
{
87
}
×
88

89
input::input(chain::point&& point, chain::script&& script,
11✔
90
    chain::witness&& witness, uint32_t sequence) NOEXCEPT
11✔
91
  : input(
92
      to_shared(std::move(point)),
11✔
93
      to_shared(std::move(script)),
11✔
94
      to_shared(std::move(witness)),
11✔
95
      sequence, true)
11✔
96
{
97
}
11✔
98

99
input::input(const chain::point& point, const chain::script& script,
1✔
100
    const chain::witness& witness, uint32_t sequence) NOEXCEPT
1✔
101
  : input(
102
      to_shared(point),
1✔
103
      to_shared(script),
1✔
104
      to_shared(witness),
1✔
105
      sequence, true)
1✔
106
{
107
}
1✔
108

109
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
×
110
    const chain::witness::cptr& witness, uint32_t sequence) NOEXCEPT
×
111
  : input(point, script, witness, sequence, true)
×
112
{
113
}
×
114

115
input::input(const data_slice& data) NOEXCEPT
5✔
116
  : input(stream::in::copy(data))
5✔
117
{
118
}
5✔
119

120
////input::input(stream::in::fast&& stream) NOEXCEPT
121
////  : input(read::bytes::fast(stream))
122
////{
123
////}
124

125
input::input(stream::in::fast& stream) NOEXCEPT
1✔
126
  : input(read::bytes::fast(stream))
1✔
127
{
128
}
1✔
129

130
input::input(std::istream&& stream) NOEXCEPT
5✔
131
  : input(read::bytes::istream(stream))
5✔
132
{
133
}
5✔
134

135
input::input(std::istream& stream) NOEXCEPT
3✔
136
  : input(read::bytes::istream(stream))
3✔
137
{
138
}
3✔
139

140
input::input(reader&& source) NOEXCEPT
9✔
141
  : input(from_data(source))
9✔
142
{
143
}
9✔
144

145
input::input(reader& source) NOEXCEPT
232✔
146
  : input(from_data(source))
232✔
147
{
148
}
232✔
149

150
// protected
151
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
1,082✔
152
    const chain::witness::cptr& witness, uint32_t sequence, bool valid) NOEXCEPT
1,082✔
153
  : point_(point),
154
    script_(script),
155
    witness_(witness),
156
    sequence_(sequence),
1,082✔
157
    valid_(valid)
1,082✔
158
{
159
}
1,082✔
160

161
// Operators.
162
// ----------------------------------------------------------------------------
163

164
bool input::operator==(const input& other) const NOEXCEPT
41✔
165
{
166
    return (sequence_ == other.sequence_)
41✔
167
        && (point_ == other.point_ || *point_ == *other.point_)
39✔
168
        && (script_ == other.script_ || *script_ == *other.script_)
39✔
169
        && (witness_ == other.witness_ || *witness_ == *other.witness_);
80✔
170
}
171

172
bool input::operator!=(const input& other) const NOEXCEPT
2✔
173
{
174
    return !(*this == other);
2✔
175
}
176

177
// Deserialization.
178
// ----------------------------------------------------------------------------
179

180
// static/private
181
input input::from_data(reader& source) NOEXCEPT
241✔
182
{
183
    // Witness is deserialized by transaction.
184
    return
241✔
185
    {
186
        to_shared<chain::point>(source),
241✔
187
        to_shared<chain::script>(source, true),
482✔
188
        to_shared<chain::witness>(),
241✔
189
        source.read_4_bytes_little_endian(),
241✔
190
        source
191
    };
482✔
192
}
193

194
// Serialization.
195
// ----------------------------------------------------------------------------
196

197
data_chunk input::to_data() const NOEXCEPT
1✔
198
{
199
    data_chunk data(serialized_size(false));
1✔
200
    stream::out::copy ostream(data);
1✔
201
    to_data(ostream);
1✔
202
    return data;
2✔
203
}
1✔
204

205
void input::to_data(std::ostream& stream) const NOEXCEPT
2✔
206
{
207
    write::bytes::ostream out(stream);
2✔
208
    to_data(out);
2✔
209
}
2✔
210

211
// Witness is serialized by transaction.
212
void input::to_data(writer& sink) const NOEXCEPT
1,384✔
213
{
214
    point_->to_data(sink);
1,384✔
215
    script_->to_data(sink, true);
1,384✔
216
    sink.write_4_bytes_little_endian(sequence_);
1,384✔
217
}
1,384✔
218

219
size_t input::serialized_size(bool witness) const NOEXCEPT
82✔
220
{
221
    // input.serialized_size(witness) provides sizing for witness, however
222
    // witnesses are serialized by the transaction. This is an ugly hack as a
223
    // consequence of bip144 not serializing witnesses as part of inputs, which
224
    // is logically the proper association.
225
    return point::serialized_size()
82✔
226
        + script_->serialized_size(true)
82✔
227
        + (witness ? witness_->serialized_size(true) : zero)
82✔
228
        + sizeof(sequence_);
82✔
229
}
230

231
// Properties.
232
// ----------------------------------------------------------------------------
233

234
bool input::is_valid() const NOEXCEPT
16✔
235
{
236
    return valid_;
16✔
237
}
238

239
const point& input::point() const NOEXCEPT
153✔
240
{
241
    return *point_;
10✔
242
}
243

244
const chain::script& input::script() const NOEXCEPT
72✔
245
{
246
    return *script_;
10✔
247
}
248

249
const chain::witness& input::witness() const NOEXCEPT
1,800✔
250
{
251
    return *witness_;
10✔
252
}
253

254
const point::cptr& input::point_ptr() const NOEXCEPT
×
255
{
256
    return point_;
×
257
}
258

259
const chain::script::cptr& input::script_ptr() const NOEXCEPT
1,555✔
260
{
261
    return script_;
1,555✔
262
}
263

264
const chain::witness::cptr& input::witness_ptr() const NOEXCEPT
×
265
{
266
    return witness_;
×
267
}
268

269
uint32_t input::sequence() const NOEXCEPT
73✔
270
{
271
    return sequence_;
10✔
272
}
273

274
// Methods.
275
// ----------------------------------------------------------------------------
276

277
bool input::is_final() const NOEXCEPT
22✔
278
{
279
    return sequence_ == max_input_sequence;
22✔
280
}
281

282
bool input::is_roller() const NOEXCEPT
4✔
283
{
284
    return script_->is_roller() || (prevout && prevout->script().is_roller());
4✔
285
}
286

287
// static
288
bool input::is_locked(uint32_t sequence, size_t height,
10✔
289
    uint32_t median_time_past, size_t prevout_height,
290
    uint32_t prevout_median_time_past) NOEXCEPT
291
{
292
    // BIP68: if bit 31 is set then no consensus meaning is applied.
293
    if (get_right(sequence, relative_locktime_disabled_bit))
10✔
294
        return false;
295

296
    // BIP68: the low 16 bits of the sequence apply to relative lock-time.
297
    const auto blocks = mask_left(sequence, relative_locktime_mask_left);
8✔
298

299
    // BIP68: bit 22 determines if relative lock is time or block based.
300
    if (get_right(sequence, relative_locktime_time_locked_bit))
8✔
301
    {
302
        // BIP68: change sequence to seconds by shift up by 9 bits (x 512).
303
        auto time = shift_left(blocks, relative_locktime_seconds_shift_left);
3✔
304
        auto age = floored_subtract(median_time_past, prevout_median_time_past);
3✔
305
        return age < time;
3✔
306
    }
307

308
    const auto age = floored_subtract(height, prevout_height);
5✔
309
    return age < blocks;
5✔
310
}
311

312
bool input::is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT
10✔
313
{
314
    // Prevout must be found and height/median_time_past metadata populated.
315
    ////BC_ASSERT(!is_zero(metadata.height));
316
    return is_locked(sequence_, height, median_time_past, metadata.height,
10✔
317
        metadata.median_time_past);
10✔
318
}
319

320
bool input::reserved_hash(hash_digest& out) const NOEXCEPT
×
321
{
322
    if (!witness::is_reserved_pattern(witness_->stack()))
×
323
        return false;
324

325
    std::copy_n(witness_->stack().front()->begin(), hash_size, out.begin());
×
326
    return true;
×
327
}
328

329
// private
330
// prevout_script is only used to determine is_pay_script_hash_pattern.
331
bool input::extract_sigop_script(chain::script& out,
×
332
    const chain::script& prevout_script) const NOEXCEPT
333
{
334
    // There are no embedded sigops when the prevout script is not p2sh.
335
    if (!script::is_pay_script_hash_pattern(prevout_script.ops()))
×
336
        return false;
337

338
    // There are no embedded sigops when the input script is not push only.
339
    const auto& ops = script_->ops();
×
NEW
340
    if (ops.empty() || !script::is_relaxed_push_pattern(ops))
×
341
        return false;
×
342

343
    // Parse the embedded script from the last input script item (data).
344
    // This cannot fail because there is no prefix to invalidate the length.
345
    out = { ops.back().data(), false };
×
346
    return true;
×
347
}
348

349
// TODO: Prior to block 79400 sigops were limited only by policy.
350
// TODO: Create legacy sigops fork/flag and pass here, return 0 if false.
351
// TODO: this was an unbipped flag day soft fork, prior to BIP16/141.
352
// TODO: if (nHeight > 79400 && GetSigOpCount() > MAX_BLOCK_SIGOPS).
353
size_t input::signature_operations(bool bip16, bool bip141) const NOEXCEPT
7✔
354
{
355
    // Penalize quadratic signature operations (bip141).
356
    const auto factor = bip141 ? heavy_sigops_factor : one;
7✔
357
    const auto sigops = script_->signature_operations(false) * factor;
7✔
358

359
    // ************************************************************************
360
    // CONSENSUS: coinbase input cannot execute, but sigops are counted anyway.
361
    // ************************************************************************
362
    if (!prevout)
7✔
363
        return sigops;
364

365
    // Null prevout/input (coinbase) cannot have witness or embedded script.
366
    // Embedded/witness scripts are deserialized here and again on scipt eval.
367

368
    chain::script witness;
×
369
    if (bip141 && witness_->extract_sigop_script(witness, prevout->script()))
×
370
    {
371
        // Add sigops in the witness script (bip141).
372
        return ceilinged_add(sigops, witness.signature_operations(true));
×
373
    }
374

375
    chain::script embedded;
×
376
    if (bip16 && extract_sigop_script(embedded, prevout->script()))
×
377
    {
378
        if (bip141 && witness_->extract_sigop_script(witness, embedded))
×
379
        {
380
            // Add sigops in the embedded witness script (bip141).
381
            return ceilinged_add(sigops, witness.signature_operations(true));
×
382
        }
383
        else
384
        {
385
            // Add heavy sigops in the embedded script (bip16).
386
            return ceilinged_add(sigops, embedded.signature_operations(true) *
×
387
                factor);
388
        }
389
    }
390

391
    return sigops;
392
}
×
393

394
BC_POP_WARNING()
395

396
// JSON value convertors.
397
// ----------------------------------------------------------------------------
398

399
namespace json = boost::json;
400

401
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
402
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
403

404
input tag_invoke(json::value_to_tag<input>, const json::value& value) NOEXCEPT
5✔
405
{
406
    return
5✔
407
    {
408
        json::value_to<chain::point>(value.at("point")),
5✔
409
        json::value_to<chain::script>(value.at("script")),
5✔
410
        json::value_to<chain::witness>(value.at("witness")),
10✔
411
        value.at("sequence").to_number<uint32_t>()
10✔
412
    };
5✔
413
}
414

415
void tag_invoke(json::value_from_tag, json::value& value,
10✔
416
    const input& input) NOEXCEPT
417
{
418
    value =
10✔
419
    {
420
        { "point", input.point() },
421
        { "script", input.script() },
422
        { "witness", input.witness() },
423
        { "sequence", input.sequence() }
424
    };
10✔
425
}
10✔
426

427
BC_POP_WARNING()
428

429
input::cptr tag_invoke(json::value_to_tag<input::cptr>,
×
430
    const json::value& value) NOEXCEPT
431
{
432
    return to_shared(tag_invoke(json::value_to_tag<input>{}, value));
×
433
}
434

435
// Shared pointer overload is required for navigation.
436
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
437
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
438

439
void tag_invoke(json::value_from_tag tag, json::value& value,
8✔
440
    const input::cptr& input) NOEXCEPT
441
{
442
    tag_invoke(tag, value, *input);
8✔
443
}
8✔
444

445
BC_POP_WARNING()
446
BC_POP_WARNING()
447

448
} // namespace chain
449
} // namespace system
450
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc