forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatchain-received-block.hpp
179 lines (135 loc) · 4.83 KB
/
catchain-received-block.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
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 "catchain/catchain-received-block.h"
namespace ton {
namespace catchain {
class CatChainReceiver;
class CatChainReceiverSource;
class CatChainReceiverFork;
class CatChainReceivedBlockImpl : public CatChainReceivedBlock {
public:
const td::SharedSlice &get_payload() const override {
return payload_;
}
CatChainBlockHash get_hash() const override {
return hash_;
}
const td::SharedSlice &get_signature() const override {
return signature_;
}
CatChainBlockHeight get_height() const override {
return height_;
}
CatChainReceivedBlock *get_prev() const override;
CatChainBlockHash get_prev_hash() const override;
const std::vector<CatChainBlockHeight> &get_deps() const override {
return deps_;
}
std::vector<CatChainBlockHash> get_dep_hashes() const override;
CatChainReceiver *get_chain() const override {
return chain_;
}
td::uint32 get_fork_id() const override {
return fork_id_;
}
td::uint32 get_source_id() const override {
return source_id_;
}
tl_object_ptr<ton_api::catchain_block> export_tl() const override;
tl_object_ptr<ton_api::catchain_block_dep> export_tl_dep() const override;
void find_pending_deps(std::vector<CatChainBlockHash> &vec, td::uint32 max_size) const override;
public:
bool initialized() const override {
return state_ >= bs_initialized;
}
bool delivered() const override {
return state_ >= bs_delivered;
}
bool is_ill() const override {
return state_ == bs_ill;
}
bool is_custom() const override {
return is_custom_;
}
bool in_db() const override {
return in_db_;
}
public:
void initialize(tl_object_ptr<ton_api::catchain_block> block, td::SharedSlice payload) override;
void run() override;
void pre_deliver(ton_api::catchain_block_data_fork &b);
void pre_deliver(ton_api::catchain_block_data_badBlock &b);
void pre_deliver(ton_api::catchain_block_data_nop &b);
template <class T>
void pre_deliver(T &b) {
// do nothing, it is custom block
is_custom_ = true;
}
void pre_deliver();
void deliver();
void dep_delivered(CatChainReceivedBlockImpl *block);
void dep_ill(CatChainReceivedBlockImpl *block);
void set_ill() override;
void schedule();
void written() override;
public:
CatChainReceivedBlockImpl(tl_object_ptr<ton_api::catchain_block> block, td::SharedSlice payload,
CatChainReceiver *chain);
CatChainReceivedBlockImpl(tl_object_ptr<ton_api::catchain_block_dep> block, CatChainReceiver *chain);
CatChainReceivedBlockImpl(td::uint32 source_id, CatChainSessionId hash, CatChainReceiver *chain);
private:
enum State {
bs_none,
bs_ill,
bs_initialized,
bs_delivered,
} state_ = bs_none;
void update_deps(CatChainReceivedBlockImpl *block);
void add_rev_dep(CatChainReceivedBlockImpl *block);
void add_child_dep(CatChainReceivedBlockImpl *block);
void initialize_fork();
void on_ready_to_deliver();
td::uint32 fork_id_{0};
td::uint32 source_id_;
CatChainReceiver *chain_;
tl_object_ptr<ton_api::catchain_block_inner_Data> data_;
td::SharedSlice payload_;
CatChainBlockHash hash_;
CatChainBlockPayloadHash data_hash_;
CatChainReceivedBlockImpl *prev_;
CatChainBlockHeight height_;
CatChainReceivedBlockImpl *next_ = nullptr;
std::vector<CatChainReceivedBlockImpl *> block_deps_;
std::vector<CatChainBlockHeight> deps_;
td::SharedSlice signature_;
std::vector<CatChainReceivedBlockImpl *> rev_deps_;
td::uint32 pending_deps_ = 0;
bool is_custom_ = false;
bool in_db_ = false;
};
} // namespace catchain
} // namespace ton
namespace td {
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlockImpl &block) {
sb << "[block " << block.get_chain()->get_incarnation() << " " << block.get_source_id() << " " << block.get_fork_id()
<< " " << block.get_hash() << "]";
return sb;
}
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlockImpl *block) {
sb << *block;
return sb;
}
} // namespace td