1
0
mirror of https://github.com/danog/ton.git synced 2024-11-26 20:14:55 +01:00
ton/catchain/catchain-received-block.hpp
2019-09-07 14:33:36 +04:00

180 lines
4.8 KiB
C++

/*
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-2019 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