2
This file is part of TON Blockchain Library.
4
TON Blockchain Library is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
TON Blockchain Library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public License
15
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
17
Copyright 2017-2020 Telegram Systems LLP
19
#include "output-queue-merger.h"
29
bool OutputQueueMerger::MsgKeyValue::operator<(const MsgKeyValue& other) const {
30
return lt < other.lt ||
31
(lt == other.lt && td::bitstring::bits_memcmp(key.cbits() + 96, other.key.cbits() + 96, 256) < 0);
34
bool OutputQueueMerger::MsgKeyValue::less(const std::unique_ptr<MsgKeyValue>& he1,
35
const std::unique_ptr<MsgKeyValue>& he2) {
39
bool OutputQueueMerger::MsgKeyValue::greater(const std::unique_ptr<MsgKeyValue>& he1,
40
const std::unique_ptr<MsgKeyValue>& he2) {
44
OutputQueueMerger::MsgKeyValue::MsgKeyValue(td::ConstBitPtr key_pfx, int key_pfx_len, int _src, Ref<vm::Cell> node)
46
unpack_node(key_pfx, key_pfx_len, std::move(node));
49
OutputQueueMerger::MsgKeyValue::MsgKeyValue(int _src, Ref<vm::Cell> node) : source(_src) {
50
unpack_node(td::ConstBitPtr{nullptr}, 0, std::move(node));
53
bool OutputQueueMerger::MsgKeyValue::invalidate() {
60
ton::LogicalTime OutputQueueMerger::MsgKeyValue::get_node_lt(Ref<vm::Cell> node, int key_pfx_len) {
61
if (node.is_null() || (unsigned)key_pfx_len > (unsigned)max_key_len) {
62
return std::numeric_limits<td::uint64>::max();
64
vm::dict::LabelParser label{std::move(node), max_key_len - key_pfx_len, vm::dict::LabelParser::chk_size};
65
if (!label.is_valid()) {
66
return std::numeric_limits<td::uint64>::max();
69
return label.remainder->prefetch_ulong(64);
72
bool OutputQueueMerger::MsgKeyValue::unpack_node(td::ConstBitPtr key_pfx, int key_pfx_len, Ref<vm::Cell> node) {
73
if (node.is_null() || (unsigned)key_pfx_len >= (unsigned)max_key_len) {
76
if (!key_pfx.is_null()) {
77
td::bitstring::bits_memcpy(key.bits(), key_pfx, key_pfx_len);
79
vm::dict::LabelParser label{std::move(node), max_key_len - key_pfx_len, vm::dict::LabelParser::chk_size};
80
if (!label.is_valid()) {
83
label.extract_label_to(key.bits() + key_pfx_len);
84
key_len = key_pfx_len + label.l_bits;
85
msg = std::move(label.remainder);
86
if (!msg.write().fetch_uint_to(64, lt)) {
89
if (is_fork() && msg->size_ext() != 0x20000) {
95
bool OutputQueueMerger::MsgKeyValue::replace_with_child(bool child_idx) {
96
if (!is_fork() || msg.is_null() || msg->size_ext() != 0x20000) {
99
key[key_len] = child_idx;
100
return unpack_node(td::ConstBitPtr{nullptr}, key_len + 1, msg->prefetch_ref(child_idx));
103
bool OutputQueueMerger::MsgKeyValue::replace_by_prefix(td::ConstBitPtr req_pfx, int req_pfx_len) {
105
if (td::bitstring::bits_memcmp(req_pfx, key.cbits(), std::min(req_pfx_len, key_len))) {
108
if (key_len >= req_pfx_len) {
111
} while (replace_with_child(req_pfx[key_len]));
115
bool OutputQueueMerger::MsgKeyValue::split(MsgKeyValue& second) {
116
if (!is_fork() || msg.is_null()) {
119
unsigned long long keep_lt = lt;
120
unsigned long long left_lt = get_node_lt(msg->prefetch_ref(0), key_len + 1);
121
bool sw = (left_lt == lt);
122
second.source = source;
124
if (!second.unpack_node(key.cbits(), key_len + 1, msg->prefetch_ref(sw))) {
127
key[key_len] = 1 - sw;
128
if (!unpack_node(td::ConstBitPtr{nullptr}, key_len + 1, msg->prefetch_ref(1 - sw))) {
131
if (lt != keep_lt || second.lt < keep_lt) {
137
bool OutputQueueMerger::add_root(int src, Ref<vm::Cell> outmsg_root) {
138
if (outmsg_root.is_null()) {
141
//block::gen::HashmapAug{352, block::gen::t_EnqueuedMsg, block::gen::t_uint64}.print_ref(std::cerr, outmsg_root);
142
auto kv = std::make_unique<MsgKeyValue>(src, std::move(outmsg_root));
143
if (kv->replace_by_prefix(common_pfx.cbits(), common_pfx_len)) {
144
heap.push_back(std::move(kv));
149
OutputQueueMerger::OutputQueueMerger(ton::ShardIdFull _queue_for, std::vector<Neighbor> _neighbors)
150
: queue_for(_queue_for), neighbors(std::move(_neighbors)), eof(false), failed(false) {
154
OutputQueueMerger::OutputQueueMerger(ton::ShardIdFull _queue_for, std::vector<block::McShardDescr> _neighbors)
155
: queue_for(_queue_for), eof(false), failed(false) {
156
for (auto& nb : _neighbors) {
157
neighbors.emplace_back(nb.top_block_id(), nb.outmsg_root, nb.is_disabled());
162
void OutputQueueMerger::init() {
163
common_pfx.bits().store_int(queue_for.workchain, 32);
164
int l = queue_for.pfx_len();
165
td::bitstring::bits_store_long_top(common_pfx.bits() + 32, queue_for.shard, l);
166
common_pfx_len = 32 + l;
168
for (Neighbor& neighbor : neighbors) {
169
if (!neighbor.disabled_) {
170
LOG(DEBUG) << "adding " << (neighbor.outmsg_root_.is_null() ? "" : "non-") << "empty output queue for neighbor #"
171
<< i << " (" << neighbor.block_id_.to_str() << ")";
172
add_root(i++, neighbor.outmsg_root_);
174
LOG(DEBUG) << "skipping output queue for disabled neighbor #" << i;
178
std::make_heap(heap.begin(), heap.end(), MsgKeyValue::greater);
185
OutputQueueMerger::MsgKeyValue* OutputQueueMerger::cur() {
186
return eof ? nullptr : msg_list.at(pos).get();
189
std::unique_ptr<OutputQueueMerger::MsgKeyValue> OutputQueueMerger::extract_cur() {
190
return eof ? std::unique_ptr<MsgKeyValue>{} : std::move(msg_list.at(pos));
193
bool OutputQueueMerger::next() {
196
} else if (++pos < msg_list.size() || load()) {
204
bool OutputQueueMerger::load() {
205
if (heap.empty() || failed) {
208
unsigned long long lt = heap[0]->lt;
209
std::size_t orig_size = msg_list.size();
211
while (heap[0]->is_fork()) {
212
auto other = std::make_unique<MsgKeyValue>();
213
if (!heap[0]->split(*other)) {
217
heap.push_back(std::move(other));
218
std::push_heap(heap.begin(), heap.end(), MsgKeyValue::greater);
220
assert(heap[0]->lt == lt);
221
std::pop_heap(heap.begin(), heap.end(), MsgKeyValue::greater);
222
msg_list.push_back(std::move(heap.back()));
224
} while (!heap.empty() && heap[0]->lt <= lt);
225
std::sort(msg_list.begin() + orig_size, msg_list.end(), MsgKeyValue::less);