forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull-node-shard-queries.hpp
133 lines (115 loc) · 4.66 KB
/
full-node-shard-queries.hpp
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
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "validator/validator.h"
#include "ton/ton-tl.hpp"
#include "full-node-serializer.hpp"
namespace ton {
namespace validator {
namespace fullnode {
class BlockFullSender : public td::actor::Actor {
public:
BlockFullSender(BlockIdExt block_id, bool next, td::actor::ActorId<ValidatorManagerInterface> manager,
td::Promise<td::BufferSlice> promise)
: block_id_(block_id), next_(next), manager_(manager), promise_(std::move(promise)) {
}
void abort_query(td::Status error) {
promise_.set_value(create_serialize_tl_object<ton_api::tonNode_dataFullEmpty>());
stop();
}
void finish_query() {
promise_.set_result(
serialize_block_full(block_id_, proof_, data_, is_proof_link_, false)); // compression_enabled = false
stop();
}
void start_up() override {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &BlockFullSender::got_block_handle, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_handle, block_id_, false, std::move(P));
}
void got_block_handle(BlockHandle handle) {
if (next_) {
if (!handle->inited_next_left()) {
return abort_query(td::Status::Error(ErrorCode::notready, "next not known"));
}
next_ = false;
block_id_ = handle->one_next(true);
start_up();
return;
}
if (!handle->received() || (!handle->inited_proof() && !handle->inited_proof_link()) || handle->deleted()) {
return abort_query(td::Status::Error(ErrorCode::notready, "not in db"));
}
handle_ = std::move(handle);
is_proof_link_ = !handle_->inited_proof();
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &BlockFullSender::got_block_data, R.move_as_ok()->data());
}
});
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_data_from_db, handle_, std::move(P));
if (!is_proof_link_) {
auto Q = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<Proof>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &BlockFullSender::got_block_proof, R.move_as_ok()->data());
}
});
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_proof_from_db, handle_, std::move(Q));
} else {
auto Q = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ProofLink>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &BlockFullSender::got_block_proof, R.move_as_ok()->data());
}
});
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_proof_link_from_db, handle_,
std::move(Q));
}
}
void got_block_data(td::BufferSlice data) {
data_ = std::move(data);
if (!proof_.empty()) {
finish_query();
}
}
void got_block_proof(td::BufferSlice data) {
proof_ = std::move(data);
if (!data_.empty()) {
finish_query();
}
}
private:
BlockIdExt block_id_;
bool next_;
BlockHandle handle_;
bool is_proof_link_;
td::BufferSlice proof_;
td::BufferSlice data_;
td::actor::ActorId<ValidatorManagerInterface> manager_;
td::Promise<td::BufferSlice> promise_;
};
} // namespace fullnode
} // namespace validator
} // namespace ton