forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathputs.hpp
More file actions
161 lines (136 loc) · 4.78 KB
/
Copy pathputs.hpp
File metadata and controls
161 lines (136 loc) · 4.78 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
/**
* Copyright (c) 2011-2025 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_TABLES_ARCHIVES_PUTS_HPP
#define LIBBITCOIN_DATABASE_TABLES_ARCHIVES_PUTS_HPP
#include <algorithm>
#include <bitcoin/system.hpp>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/memory/memory.hpp>
#include <bitcoin/database/primitives/primitives.hpp>
#include <bitcoin/database/tables/schema.hpp>
namespace libbitcoin {
namespace database {
namespace table {
/// Puts is an blob of spend and output fk records.
struct puts
: public no_map<schema::puts>
{
using spend = linkage<schema::spend_>;
using out = linkage<schema::put>;
using spend_links = std_vector<spend::integer>;
using output_links = std_vector<out::integer>;
using no_map<schema::puts>::nomap;
// TODO: There is a potential optimization available given that the inputs
// (spend puts) for a given transaction are sequential. This means that an
// offset with count are sufficient to store and number of spends. This is
// more efficient if store averages more than two spends per tx (maybe).
struct slab
: public schema::puts
{
link count() const NOEXCEPT
{
const auto fks = spend_fks.size() * spend::size +
out_fks.size() * out::size;
return system::possible_narrow_cast<link::integer>(fks);
}
inline bool from_data(reader& source) NOEXCEPT
{
std::for_each(spend_fks.begin(), spend_fks.end(), [&](auto& fk) NOEXCEPT
{
fk = source.read_little_endian<spend::integer, spend::size>();
});
std::for_each(out_fks.begin(), out_fks.end(), [&](auto& fk) NOEXCEPT
{
fk = source.read_little_endian<out::integer, out::size>();
});
BC_ASSERT(!source || source.get_read_position() == count());
return source;
}
inline bool to_data(flipper& sink) const NOEXCEPT
{
std::for_each(spend_fks.begin(), spend_fks.end(), [&](const auto& fk) NOEXCEPT
{
sink.write_little_endian<spend::integer, spend::size>(fk);
});
std::for_each(out_fks.begin(), out_fks.end(), [&](const auto& fk) NOEXCEPT
{
sink.write_little_endian<out::integer, out::size>(fk);
});
BC_ASSERT(!sink || sink.get_write_position() == count());
return sink;
}
inline bool operator==(const slab& other) const NOEXCEPT
{
return spend_fks == other.spend_fks
&& out_fks == other.out_fks;
}
spend_links spend_fks{};
output_links out_fks{};
};
struct get_spends
: public schema::puts
{
inline bool from_data(reader& source) NOEXCEPT
{
std::for_each(spend_fks.begin(), spend_fks.end(), [&](auto& fk) NOEXCEPT
{
fk = source.read_little_endian<spend::integer, spend::size>();
});
return source;
}
spend_links spend_fks{};
};
struct get_outs
: public schema::puts
{
inline bool from_data(reader& source) NOEXCEPT
{
std::for_each(out_fks.begin(), out_fks.end(), [&](auto& fk) NOEXCEPT
{
fk = source.read_little_endian<out::integer, out::size>();
});
return source;
}
output_links out_fks{};
};
struct get_spend_at
: public schema::puts
{
inline bool from_data(reader& source) NOEXCEPT
{
spend_fk = source.read_little_endian<spend::integer, spend::size>();
return source;
}
spend::integer spend_fk{};
};
struct get_output_at
: public schema::puts
{
inline bool from_data(reader& source) NOEXCEPT
{
out_fk = source.read_little_endian<out::integer, out::size>();
return source;
}
out::integer out_fk{};
};
};
} // namespace table
} // namespace database
} // namespace libbitcoin
#endif