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

libbitcoin / libbitcoin-system / 9766181572

02 Jul 2024 06:30PM UTC coverage: 82.709%. Remained the same
9766181572

Pull #1487

github

web-flow
Merge 2af8e9df1 into 22ebdd3e8
Pull Request #1487: Factor block.check "bypass" into block.identify overrides.

7 of 29 new or added lines in 8 files covered. (24.14%)

4 existing lines in 1 file now uncovered.

9854 of 11914 relevant lines covered (82.71%)

4786847.07 hits per line

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

7.84
/src/wallet/points_value.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/wallet/points_value.hpp>
20

21
#include <numeric>
22
#include <bitcoin/system/math/math.hpp>
23
#include <bitcoin/system/wallet/point_value.hpp>
24

25
namespace libbitcoin {
26
namespace system {
27
namespace wallet {
28

29
// static
30
// ----------------------------------------------------------------------------
31

32
void points_value::greedy(points_value& out, const points_value& unspent,
×
33
    uint64_t minimum_value) NOEXCEPT
34
{
35
    out.points.clear();
×
36

37
    // The minimum required value does not exist.
38
    if (unspent.value() < minimum_value)
×
39
        return;
×
40

41
    // Optimization for simple case not requiring search.
NEW
42
    if (is_one(unspent.points.size()))
×
43
    {
44
        out.points.push_back(unspent.points.front());
×
45
        return;
×
46
    }
47

48
    // Copy the points list for safe manipulation.
49
    point_value::list points{ unspent.points };
×
50

51
    const auto below = [minimum_value](
×
52
        const point_value& point) NOEXCEPT
×
53
        {
54
            return point.value() < minimum_value;
×
55
        };
×
56

57
    const auto lesser = [](const point_value& left,
×
58
        const point_value& right) NOEXCEPT
59
        {
60
            return left.value() < right.value();
×
61
        };
62

63
    const auto greater = [](const point_value& left,
×
64
        const point_value& right) NOEXCEPT
65
        {
66
            return left.value() > right.value();
×
67
        };
68

69
    // C++17: Parallel policy for std::partition.
70
    // Reorder list between values that exceed minimum and those that do not.
71
    const auto sufficient = std::partition(points.begin(), points.end(), below);
×
72

73
    // C++17: Parallel policy for std::min_element.
74
    // If there are values large enough, return the smallest (of the largest).
75
    const auto minimum = std::min_element(sufficient, points.end(), lesser);
×
76

77
    if (minimum != points.end())
×
78
    {
79
        out.points.push_back(*minimum);
×
80
        return;
×
81
    }
82

83
    // C++17: Parallel policy for std::sort.
84
    // Sort all by descending value in order to use the fewest inputs possible.
85
    std::sort(points.begin(), points.end(), greater);
×
86

87
    // This is naive, will not necessarily find the smallest combination.
88
    for (const auto& point: points)
×
89
    {
90
        out.points.push_back(point);
×
91

92
        if (out.value() >= minimum_value)
×
93
            return;
94
    }
95
}
×
96

97
void points_value::individual(points_value& out, const points_value& unspent,
×
98
    uint64_t minimum_value) NOEXCEPT
99
{
100
    out.points.clear();
×
101
    out.points.reserve(unspent.points.size());
×
102

103
    // Select all individual points that satisfy the minimum.
104
    for (const auto& point: unspent.points)
×
105
        if (point.value() >= minimum_value)
×
106
            out.points.push_back(point);
×
107

108
    out.points.shrink_to_fit();
×
109

110
    const auto lesser = [](const point_value& left,
×
111
        const point_value& right) NOEXCEPT
112
        {
113
            return left.value() < right.value();
×
114
        };
115

116
    // C++17: Parallel policy for std::sort.
117
    // Return in ascending order by value.
118
    std::sort(out.points.begin(), out.points.end(), lesser);
×
119
}
×
120

121
void points_value::select(points_value& out, const points_value& unspent,
×
122
    uint64_t minimum_value, selection option) NOEXCEPT
123
{
124
    switch (option)
×
125
    {
126
        case selection::individual:
×
127
            individual(out, unspent, minimum_value);
×
128
            break;
×
129
        case selection::greedy:
×
130
        default:
×
131
            greedy(out, unspent, minimum_value);
×
132
            break;
×
133
    }
134
}
×
135

136
// public
137
// ----------------------------------------------------------------------------
138

139
// Overflow returns max_uint64.
140
uint64_t points_value::value() const NOEXCEPT
5✔
141
{
142
    const auto sum = [](uint64_t total, const point_value& point) NOEXCEPT
11✔
143
    {
144
        return ceilinged_add(total, point.value());
6✔
145
    };
146

147
    return std::accumulate(points.begin(), points.end(), min_uint64, sum);
5✔
148
}
149

150
} // namespace wallet
151
} // namespace system
152
} // 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