Skip to content

Commit

Permalink
LS queries to nonfinal blocks (#941)
Browse files Browse the repository at this point in the history
Co-authored-by: SpyCheese <[email protected]>
  • Loading branch information
EmelyanenkoK and SpyCheese authored Mar 26, 2024
1 parent 0feaaf5 commit 9452c36
Show file tree
Hide file tree
Showing 23 changed files with 809 additions and 150 deletions.
4 changes: 3 additions & 1 deletion tl-utils/lite-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ std::string lite_query_name_by_id(int id) {
{lite_api::liteServer_getConfigParams::ID, "getConfigParams"},
{lite_api::liteServer_getValidatorStats::ID, "getValidatorStats"},
{lite_api::liteServer_getLibraries::ID, "getLibraries"},
{lite_api::liteServer_getShardBlockProof::ID, "getShardBlockProof"}};
{lite_api::liteServer_getShardBlockProof::ID, "getShardBlockProof"},
{lite_api::liteServer_nonfinal_getCandidate::ID, "nonfinal.getCandidate"},
{lite_api::liteServer_nonfinal_getValidatorGroups::ID, "nonfinal.getValidatorGroups"}};
auto it = names.find(id);
if (it == names.end()) {
return "unknown";
Expand Down
9 changes: 9 additions & 0 deletions tl/generate/scheme/lite_api.tl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ liteServer.lookupBlockResult id:tonNode.blockIdExt mode:# mc_block_id:tonNode.bl

liteServer.debug.verbosity value:int = liteServer.debug.Verbosity;

liteServer.nonfinal.candidateId block_id:tonNode.blockIdExt creator:int256 collated_data_hash:int256 = liteServer.nonfinal.CandidateId;
liteServer.nonfinal.candidate id:liteServer.nonfinal.candidateId data:bytes collated_data:bytes = liteServer.nonfinal.Candidate;
liteServer.nonfinal.candidateInfo id:liteServer.nonfinal.candidateId available:Bool approved_weight:long signed_weight:long total_weight:long = liteServer.nonfinal.CandidateInfo;
liteServer.nonfinal.validatorGroupInfo next_block_id:tonNode.blockId cc_seqno:int prev:(vector tonNode.blockIdExt) candidates:(vector liteServer.nonfinal.candidateInfo) = liteServer.nonfinal.ValidatorGroupInfo;
liteServer.nonfinal.validatorGroups groups:(vector liteServer.nonfinal.validatorGroupInfo) = liteServer.nonfinal.ValidatorGroups;

---functions---

liteServer.getMasterchainInfo = liteServer.MasterchainInfo;
Expand Down Expand Up @@ -89,6 +95,9 @@ liteServer.getLibraries library_list:(vector int256) = liteServer.LibraryResult;
liteServer.getLibrariesWithProof id:tonNode.blockIdExt mode:# library_list:(vector int256) = liteServer.LibraryResultWithProof;
liteServer.getShardBlockProof id:tonNode.blockIdExt = liteServer.ShardBlockProof;

liteServer.nonfinal.getValidatorGroups mode:# wc:mode.0?int shard:mode.1?long = liteServer.nonfinal.ValidatorGroups;
liteServer.nonfinal.getCandidate id:liteServer.nonfinal.candidateId = liteServer.nonfinal.Candidate;

liteServer.queryPrefix = Object;
liteServer.query data:bytes = Object;
liteServer.waitMasterchainSeqno seqno:int timeout_ms:int = Object; // query prefix
Binary file modified tl/generate/scheme/lite_api.tlo
Binary file not shown.
4 changes: 4 additions & 0 deletions validator-engine/validator-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ td::Status ValidatorEngine::load_global_config() {
validator_options_.write().set_max_open_archive_files(max_open_archive_files_);
validator_options_.write().set_archive_preload_period(archive_preload_period_);
validator_options_.write().set_disable_rocksdb_stats(disable_rocksdb_stats_);
validator_options_.write().set_nonfinal_ls_queries_enabled(nonfinal_ls_queries_enabled_);

std::vector<ton::BlockIdExt> h;
for (auto &x : conf.validator_->hardforks_) {
Expand Down Expand Up @@ -3820,6 +3821,9 @@ int main(int argc, char *argv[]) {
p.add_option('\0', "disable-rocksdb-stats", "disable gathering rocksdb statistics (enabled by default)", [&]() {
acts.push_back([&x]() { td::actor::send_closure(x, &ValidatorEngine::set_disable_rocksdb_stats, true); });
});
p.add_option('\0', "nonfinal-ls", "enable special LS queries to non-finalized blocks", [&]() {
acts.push_back([&x]() { td::actor::send_closure(x, &ValidatorEngine::set_nonfinal_ls_queries_enabled); });
});
auto S = p.run(argc, argv);
if (S.is_error()) {
LOG(ERROR) << "failed to parse options: " << S.move_as_error();
Expand Down
4 changes: 4 additions & 0 deletions validator-engine/validator-engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class ValidatorEngine : public td::actor::Actor {
size_t max_open_archive_files_ = 0;
double archive_preload_period_ = 0.0;
bool disable_rocksdb_stats_ = false;
bool nonfinal_ls_queries_enabled_ = false;
bool read_config_ = false;
bool started_keyring_ = false;
bool started_ = false;
Expand Down Expand Up @@ -276,6 +277,9 @@ class ValidatorEngine : public td::actor::Actor {
void set_disable_rocksdb_stats(bool value) {
disable_rocksdb_stats_ = value;
}
void set_nonfinal_ls_queries_enabled() {
nonfinal_ls_queries_enabled_ = true;
}
void start_up() override;
ValidatorEngine() {
}
Expand Down
22 changes: 22 additions & 0 deletions validator-session/validator-session-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ class ValidatorSessionRoundState : public ValidatorSessionDescription::RootObjec
void dump(ValidatorSessionDescription& desc, td::StringBuilder& sb, td::uint32 att) const;
void dump_cur_attempt(ValidatorSessionDescription& desc, td::StringBuilder& sb) const;

void for_each_sent_block(std::function<void(const SessionBlockCandidate*)> foo) const {
if (!sent_blocks_) {
return;
}
for (td::uint32 i = 0; i < sent_blocks_->size(); ++i) {
foo(sent_blocks_->at(i));
}
}

private:
const SentBlock* precommitted_block_;
const td::uint32 seqno_;
Expand Down Expand Up @@ -516,6 +525,19 @@ class ValidatorSessionState : public ValidatorSessionDescription::RootObject {
cur_round_->dump_cur_attempt(desc, sb);
}

void for_each_cur_round_sent_block(std::function<void(const SessionBlockCandidate*)> foo) const {
cur_round_->for_each_sent_block(std::move(foo));
}

const SentBlock* get_cur_round_precommitted_block() const {
bool found;
return cur_round_->get_precommitted_block(found);
}

const CntVector<const SessionBlockCandidateSignature*>* get_cur_round_signatures() const {
return cur_round_->get_signatures();
}

static const ValidatorSessionState* make_one(ValidatorSessionDescription& desc, const ValidatorSessionState* state,
td::uint32 src_idx, td::uint32 att, bool& made);
static const ValidatorSessionState* make_all(ValidatorSessionDescription& desc, const ValidatorSessionState* state,
Expand Down
47 changes: 47 additions & 0 deletions validator-session/validator-session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,53 @@ void ValidatorSessionImpl::get_current_stats(td::Promise<ValidatorSessionStats>
promise.set_result(std::move(stats));
}

void ValidatorSessionImpl::get_validator_group_info_for_litequery(
td::uint32 cur_round,
td::Promise<std::vector<tl_object_ptr<lite_api::liteServer_nonfinal_candidateInfo>>> promise) {
if (cur_round != cur_round_ || real_state_->cur_round_seqno() != cur_round) {
promise.set_value({});
return;
}
std::vector<tl_object_ptr<lite_api::liteServer_nonfinal_candidateInfo>> result;
real_state_->for_each_cur_round_sent_block([&](const SessionBlockCandidate *block) {
if (block->get_block() == nullptr) {
return;
}
auto candidate = create_tl_object<lite_api::liteServer_nonfinal_candidateInfo>();

candidate->id_ = create_tl_object<lite_api::liteServer_nonfinal_candidateId>();
candidate->id_->block_id_ = create_tl_object<lite_api::tonNode_blockIdExt>();
candidate->id_->block_id_->root_hash_ =
block->get_block()->get_root_hash(); // other fields will be filled in validator-group.cpp
candidate->id_->block_id_->file_hash_ = block->get_block()->get_file_hash();
candidate->id_->creator_ =
description().get_source_public_key(block->get_block()->get_src_idx()).ed25519_value().raw();
candidate->id_->collated_data_hash_ = block->get_block()->get_collated_data_file_hash();

candidate->total_weight_ = description().get_total_weight();
candidate->approved_weight_ = 0;
candidate->signed_weight_ = 0;
for (td::uint32 i = 0; i < description().get_total_nodes(); ++i) {
if (real_state_->check_block_is_approved_by(description(), i, block->get_id())) {
candidate->approved_weight_ += description().get_node_weight(i);
}
}
auto precommited = real_state_->get_cur_round_precommitted_block();
if (SentBlock::get_block_id(precommited) == SentBlock::get_block_id(block->get_block())) {
auto signatures = real_state_->get_cur_round_signatures();
if (signatures) {
for (td::uint32 i = 0; i < description().get_total_nodes(); ++i) {
if (signatures->at(i)) {
candidate->signed_weight_ += description().get_node_weight(i);
}
}
}
}
result.push_back(std::move(candidate));
});
promise.set_result(std::move(result));
}

void ValidatorSessionImpl::start_up() {
CHECK(!rldp_.empty());
cur_round_ = 0;
Expand Down
4 changes: 4 additions & 0 deletions validator-session/validator-session.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "catchain/catchain-types.h"

#include "validator-session-types.h"
#include "auto/tl/lite_api.h"

namespace ton {

Expand Down Expand Up @@ -92,6 +93,9 @@ class ValidatorSession : public td::actor::Actor {
virtual void start() = 0;
virtual void destroy() = 0;
virtual void get_current_stats(td::Promise<ValidatorSessionStats> promise) = 0;
virtual void get_validator_group_info_for_litequery(
td::uint32 cur_round,
td::Promise<std::vector<tl_object_ptr<lite_api::liteServer_nonfinal_candidateInfo>>> promise) = 0;

static td::actor::ActorOwn<ValidatorSession> create(
catchain::CatChainSessionId session_id, ValidatorSessionOptions opts, PublicKeyHash local_id,
Expand Down
3 changes: 3 additions & 0 deletions validator-session/validator-session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ class ValidatorSessionImpl : public ValidatorSession {
void start() override;
void destroy() override;
void get_current_stats(td::Promise<ValidatorSessionStats> promise) override;
void get_validator_group_info_for_litequery(
td::uint32 cur_round,
td::Promise<std::vector<tl_object_ptr<lite_api::liteServer_nonfinal_candidateInfo>>> promise) override;

void process_blocks(std::vector<catchain::CatChainBlock *> blocks);
void finished_processing();
Expand Down
2 changes: 2 additions & 0 deletions validator/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endif()
set(TON_VALIDATOR_SOURCE
accept-block.cpp
block.cpp
candidates-buffer.cpp
check-proof.cpp
collator.cpp
config.cpp
Expand All @@ -24,6 +25,7 @@ set(TON_VALIDATOR_SOURCE

accept-block.hpp
block.hpp
candidates-buffer.hpp
check-proof.hpp
collate-query-impl.h
collator-impl.h
Expand Down
Loading

0 comments on commit 9452c36

Please sign in to comment.