forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatchain-receiver-source.hpp
157 lines (127 loc) · 4.47 KB
/
catchain-receiver-source.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
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 <map>
#include "catchain-receiver-source.h"
#include "catchain-receiver.h"
#include "catchain-received-block.h"
#include <queue>
namespace ton {
namespace catchain {
class CatChainReceiverSourceImpl : public CatChainReceiverSource {
public:
td::uint32 get_id() const override {
return id_;
}
PublicKeyHash get_hash() const override {
return src_;
}
PublicKey get_full_id() const override {
return full_id_;
}
adnl::AdnlNodeIdShort get_adnl_id() const override {
return adnl_id_;
}
td::uint32 add_fork() override;
bool blamed() const override {
return blamed_;
}
void blame(td::uint32 fork, CatChainBlockHeight height) override;
void blame() override;
void block_received(CatChainBlockHeight height) override;
void block_delivered(CatChainBlockHeight height) override;
const std::vector<td::uint32> &get_forks() const override {
return fork_ids_;
}
const std::vector<CatChainBlockHeight> &get_blamed_heights() const override {
return blamed_heights_;
}
td::actor::ActorId<EncryptorAsync> get_encryptor() const {
return encryptor_.get();
}
Encryptor *get_encryptor_sync() const override {
return encryptor_sync_.get();
}
td::uint32 get_forks_cnt() const override {
return static_cast<td::uint32>(fork_ids_.size());
}
CatChainBlockHeight delivered_height() const override {
return delivered_height_;
}
CatChainBlockHeight received_height() const override {
return received_height_;
}
bool has_unreceived() const override {
if (blamed()) {
return true;
}
if (blocks_.empty()) {
return false;
}
CHECK(blocks_.rbegin()->second->get_height() >= received_height_);
return blocks_.rbegin()->second->get_height() > received_height_;
}
bool has_undelivered() const override {
return delivered_height_ < received_height_;
}
CatChainReceivedBlock *get_block(CatChainBlockHeight height) const override;
void on_new_block(CatChainReceivedBlock *block) override;
void on_found_fork_proof(const td::Slice &proof) override;
bool fork_is_found() const override {
return !fork_proof_.empty();
}
td::BufferSlice fork_proof() const override {
if (!fork_proof_.empty()) {
return fork_proof_.clone_as_buffer_slice();
} else {
return {};
}
}
CatChainReceiver *get_chain() const override {
return chain_;
}
bool allow_send_block(CatChainBlockHash hash) override;
CatChainReceiverSourceImpl(CatChainReceiver *chain, PublicKey source, adnl::AdnlNodeIdShort adnl_id, td::uint32 id);
private:
CatChainReceiver *chain_;
td::uint32 id_;
PublicKeyHash src_;
bool blamed_ = false;
PublicKey full_id_;
adnl::AdnlNodeIdShort adnl_id_;
std::vector<td::uint32> fork_ids_;
td::actor::ActorOwn<EncryptorAsync> encryptor_;
std::unique_ptr<Encryptor> encryptor_sync_;
std::vector<CatChainBlockHeight> blamed_heights_;
std::map<CatChainBlockHeight, CatChainReceivedBlock *> blocks_;
td::SharedSlice fork_proof_;
CatChainBlockHeight delivered_height_ = 0;
CatChainBlockHeight received_height_ = 0;
std::map<CatChainBlockHash, td::uint32> block_requests_count_;
// One block can be sent to one node up to 5 times
static const td::uint32 MAX_BLOCK_REQUESTS = 5;
};
} // namespace catchain
} // namespace ton
namespace td {
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSourceImpl &source) {
sb << "[source " << source.get_chain()->get_incarnation() << " " << source.get_id() << "]";
return sb;
}
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSourceImpl *source) {
sb << *source;
return sb;
}
} // namespace td