1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

New liteserver config format

* Specify shards and seqno/utime/lt limits for liteservers in global config
* Support in lite-client, tonlib, blockchain-explorer
* Rework proxy-liteserver
This commit is contained in:
SpyCheese 2024-06-12 18:12:45 +03:00
parent 38ab70c037
commit 007f1fb1d7
26 changed files with 1187 additions and 1130 deletions

View file

@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
add_library(lite-client-common STATIC lite-client-common.cpp lite-client-common.h ext-client.cpp ext-client.h QueryTraits.h)
target_link_libraries(lite-client-common PUBLIC tdutils tdactor adnllite tl_api tl_lite_api tl-lite-utils ton_crypto ton_block)
add_library(lite-client-common STATIC lite-client-common.cpp lite-client-common.h ext-client.cpp ext-client.h
query-utils.hpp query-utils.cpp)
target_link_libraries(lite-client-common PUBLIC tdutils tdactor adnllite tl_api tl_lite_api tl-lite-utils ton_crypto
ton_block)
add_executable(lite-client lite-client.cpp lite-client.h ext-client.h ext-client.cpp)
target_link_libraries(lite-client tdutils tdactor adnllite tl_api tl_lite_api tl-lite-utils ton_crypto ton_block

View file

@ -1,229 +0,0 @@
/*
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/>.
*/
#pragma once
#include "ton/ton-types.h"
#include "auto/tl/lite_api.h"
#include "auto/tl/lite_api.hpp"
#include "vm/boc.h"
#include "vm/cellslice.h"
#include "block/block-auto.h"
#include "block/block-parse.h"
#include "auto/tl/lite_api.hpp"
namespace liteclient {
template <typename Query>
struct QueryTraits {
static ton::ShardIdFull get_shard(const Query& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getMasterchainInfo> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getMasterchainInfo& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getMasterchainInfoExt> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getMasterchainInfoExt& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getTime> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getTime& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getVersion> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getVersion& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getBlock> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getBlock& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getState> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getState& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getBlockHeader> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getBlockHeader& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_sendMessage> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_sendMessage& q) {
auto shard = [&]() -> td::Result<ton::ShardIdFull> {
vm::BagOfCells boc;
TRY_STATUS(boc.deserialize(q.body_.as_slice()));
if (boc.get_root_count() != 1) {
return td::Status::Error("external message is not a valid bag of cells");
}
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
if (!tlb::unpack_cell_inexact(boc.get_root_cell(), info)) {
return td::Status::Error("cannot unpack external message header");
}
auto dest_prefix = block::tlb::t_MsgAddressInt.get_prefix(info.dest);
if (!dest_prefix.is_valid()) {
return td::Status::Error("destination of an inbound external message is an invalid blockchain address");
}
return dest_prefix.as_leaf_shard();
}();
if (shard.is_error()) {
LOG(DEBUG) << "Failed to get shard from query liteServer.sendMessage: " << shard.move_as_error();
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
return shard.move_as_ok();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getAccountState> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getAccountState& q) {
return ton::AccountIdPrefixFull(q.account_->workchain_, q.account_->id_.bits().get_uint(64)).as_leaf_shard();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getAccountStatePrunned> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getAccountStatePrunned& q) {
return ton::AccountIdPrefixFull(q.account_->workchain_, q.account_->id_.bits().get_uint(64)).as_leaf_shard();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_runSmcMethod> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_runSmcMethod& q) {
return ton::AccountIdPrefixFull(q.account_->workchain_, q.account_->id_.bits().get_uint(64)).as_leaf_shard();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getShardInfo> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getShardInfo& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getAllShardsInfo> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getAllShardsInfo& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getOneTransaction> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getOneTransaction& q) {
return ton::AccountIdPrefixFull(q.account_->workchain_, q.account_->id_.bits().get_uint(64)).as_leaf_shard();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getTransactions> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getTransactions& q) {
return ton::AccountIdPrefixFull(q.account_->workchain_, q.account_->id_.bits().get_uint(64)).as_leaf_shard();
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_lookupBlock> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_lookupBlock& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_listBlockTransactions> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_listBlockTransactions& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_listBlockTransactionsExt> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_listBlockTransactionsExt& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getBlockProof> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getBlockProof& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getConfigAll> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getConfigAll& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getConfigParams> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getConfigParams& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getValidatorStats> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getValidatorStats& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getLibraries> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getLibraries& q) {
return ton::ShardIdFull(ton::masterchainId, ton::shardIdAll);
}
};
template<>
struct QueryTraits<ton::lite_api::liteServer_getShardBlockProof> {
static ton::ShardIdFull get_shard(const ton::lite_api::liteServer_getShardBlockProof& q) {
return ton::ShardIdFull(q.id_->workchain_, q.id_->shard_);
}
};
template<typename Query>
inline ton::ShardIdFull get_query_shard(const Query& q) {
return QueryTraits<Query>::get_shard(q);
}
} // namespace tonlib

View file

@ -17,92 +17,75 @@
#include "ext-client.h"
#include "td/utils/Random.h"
#include "ton/ton-shard.h"
#include <map>
namespace liteclient {
class ExtClientImpl : public ExtClient {
public:
ExtClientImpl(std::vector<LiteServer> servers, td::unique_ptr<ExtClient::Callback> callback)
ExtClientImpl(std::vector<LiteServerConfig> liteservers, td::unique_ptr<Callback> callback)
: callback_(std::move(callback)) {
CHECK(!servers.empty());
servers_.resize(servers.size());
CHECK(!liteservers.empty());
servers_.resize(liteservers.size());
for (size_t i = 0; i < servers_.size(); ++i) {
servers_[i].s = std::move(servers[i]);
if (!servers_[i].s.is_full) {
for (auto shard : servers_[i].s.shards) {
CHECK(shard.is_valid_ext());
max_server_shard_depth_ = std::max(max_server_shard_depth_, shard.pfx_len());
}
}
servers_[i].config = std::move(liteservers[i]);
servers_[i].idx = i;
}
}
void start_up() override {
LOG(INFO) << "Started ext client, " << servers_.size() << " liteservers";
td::Random::Fast rnd;
td::random_shuffle(td::as_mutable_span(servers_), rnd);
}
void send_query(std::string name, td::BufferSlice data, ton::ShardIdFull shard, td::Timestamp timeout,
void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout,
td::Promise<td::BufferSlice> promise) override {
TRY_RESULT_PROMISE(promise, server_idx, before_query(shard));
QueryInfo query_info = get_query_info(data);
TRY_RESULT_PROMISE(promise, server_idx, select_server(query_info));
auto& server = servers_[server_idx];
CHECK(!server.client.empty());
alarm_timestamp().relax(server.timeout = td::Timestamp::in(MAX_NO_QUERIES_TIMEOUT));
td::Promise<td::BufferSlice> P = [SelfId = actor_id(this), server_idx,
promise = std::move(promise)](td::Result<td::BufferSlice> R) mutable {
promise = std::move(promise)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error() &&
(R.error().code() == ton::ErrorCode::timeout || R.error().code() == ton::ErrorCode::cancelled)) {
td::actor::send_closure(SelfId, &ExtClientImpl::set_server_bad, server_idx);
td::actor::send_closure(SelfId, &ExtClientImpl::on_server_error, server_idx);
}
promise.set_result(std::move(R));
};
LOG(DEBUG) << "Sending query " << query_info.to_str() << " to server #" << server.idx << " ("
<< server.config.addr.get_ip_str() << ":" << server.config.addr.get_port() << ")";
send_closure(server.client, &ton::adnl::AdnlExtClient::send_query, std::move(name), std::move(data), timeout,
std::move(P));
}
void force_change_liteserver() override {
if (servers_.size() == 1) {
return;
}
auto it = shard_to_server_.find(ton::ShardIdFull(ton::masterchainId));
if (it != shard_to_server_.end()) {
set_server_bad(it->second);
void reset_servers() override {
LOG(INFO) << "Force resetting all liteservers";
for (Server& server : servers_) {
server.alive = false;
server.timeout = {};
server.ignore_until = {};
server.client.reset();
}
}
private:
td::Result<size_t> before_query(ton::ShardIdFull shard) {
if (!shard.is_valid_ext()) {
return td::Status::Error("Invalid shard");
}
if (is_closing_) {
return td::Status::Error("Client is closing");
}
if (shard.pfx_len() > max_server_shard_depth_) {
shard = shard_prefix(shard, max_server_shard_depth_);
}
auto it = shard_to_server_.find(shard);
if (it != shard_to_server_.end()) {
size_t server_idx = it->second;
if (!servers_[server_idx].client.empty()) {
return server_idx;
td::Result<size_t> select_server(const QueryInfo& query_info) {
for (size_t i = 0; i < servers_.size(); ++i) {
if (servers_[i].alive && servers_[i].config.accepts_query(query_info)) {
return i;
}
shard_to_server_.erase(it);
}
size_t server_idx = servers_.size();
int cnt = 0;
int best_priority = -1;
for (size_t i = 0; i < servers_.size(); ++i) {
Server& server = servers_[i];
if (!server.supports(shard)) {
if (!server.config.accepts_query(query_info)) {
continue;
}
int priority = 0;
priority += (server.client.empty() ? 0 : 100);
priority += (server.ignore_until && !server.ignore_until.is_in_past() ? 0 : 10);
priority += (server.s.is_full ? 1 : 0);
if (priority < best_priority) {
continue;
}
@ -116,100 +99,76 @@ class ExtClientImpl : public ExtClient {
++cnt;
}
if (server_idx == servers_.size()) {
return td::Status::Error(PSTRING() << "No liteserver for shard " << shard.to_str());
return td::Status::Error(PSTRING() << "no liteserver for query " << query_info.to_str());
}
Server& server = servers_[server_idx];
server.alive = true;
server.ignore_until = {};
alarm_timestamp().relax(server.timeout = td::Timestamp::in(MAX_NO_QUERIES_TIMEOUT));
if (!server.client.empty()) {
return server_idx;
}
class Callback : public ton::adnl::AdnlExtClient::Callback {
public:
explicit Callback(td::actor::ActorShared<ExtClientImpl> parent, size_t idx)
: parent_(std::move(parent)), idx_(idx) {
explicit Callback(td::actor::ActorId<ExtClientImpl> parent, size_t idx) : parent_(std::move(parent)), idx_(idx) {
}
void on_ready() override {
}
void on_stop_ready() override {
td::actor::send_closure(parent_, &ExtClientImpl::set_server_bad, idx_);
td::actor::send_closure(parent_, &ExtClientImpl::on_server_error, idx_);
}
private:
td::actor::ActorShared<ExtClientImpl> parent_;
td::actor::ActorId<ExtClientImpl> parent_;
size_t idx_;
};
ref_cnt_++;
if (shard.is_masterchain()) {
LOG(INFO) << "Connecting to liteserver " << server.s.address << " for masterchain";
} else {
LOG(INFO) << "Connecting to liteserver " << server.s.address << " for shard " << shard.to_str();
}
server.client = ton::adnl::AdnlExtClient::create(
server.s.adnl_id, server.s.address, std::make_unique<Callback>(td::actor::actor_shared(this), server_idx));
alarm_timestamp().relax(server.timeout = td::Timestamp::in(MAX_NO_QUERIES_TIMEOUT));
LOG(INFO) << "Connecting to liteserver #" << server.idx << " (" << server.config.addr.get_ip_str() << ":"
<< server.config.addr.get_port() << ") for query " << query_info.to_str();
server.client = ton::adnl::AdnlExtClient::create(server.config.adnl_id, server.config.addr,
std::make_unique<Callback>(actor_id(this), server_idx));
return server_idx;
}
struct Server {
LiteServer s;
LiteServerConfig config;
size_t idx = 0;
td::actor::ActorOwn<ton::adnl::AdnlExtClient> client;
bool alive = false;
td::Timestamp timeout = td::Timestamp::never();
td::Timestamp ignore_until = td::Timestamp::never();
bool supports(const ton::ShardIdFull& shard) const {
return s.is_full || shard.is_masterchain() ||
std::any_of(s.shards.begin(), s.shards.end(),
[&](const ton::ShardIdFull s_shard) { return ton::shard_intersects(shard, s_shard); });
}
};
std::vector<Server> servers_;
std::map<ton::ShardIdFull, size_t> shard_to_server_;
int max_server_shard_depth_ = 0;
td::unique_ptr<ExtClient::Callback> callback_;
static constexpr double MAX_NO_QUERIES_TIMEOUT = 100;
bool is_closing_{false};
td::uint32 ref_cnt_{1};
td::unique_ptr<Callback> callback_;
static constexpr double MAX_NO_QUERIES_TIMEOUT = 100.0;
static constexpr double BAD_SERVER_TIMEOUT = 30.0;
void alarm() override {
for (Server& server : servers_) {
if (server.timeout && server.timeout.is_in_past()) {
LOG(INFO) << "Closing connection to liteserver #" << server.idx << " (" << server.config.addr.get_ip_str()
<< ":" << server.config.addr.get_port() << ")";
server.client.reset();
server.alive = false;
server.ignore_until = {};
}
}
}
void set_server_bad(size_t idx) {
servers_[idx].client.reset();
servers_[idx].timeout = td::Timestamp::never();
servers_[idx].ignore_until = td::Timestamp::in(60.0);
}
void hangup_shared() override {
ref_cnt_--;
try_stop();
}
void hangup() override {
is_closing_ = true;
ref_cnt_--;
for (Server& server : servers_) {
server.client.reset();
}
try_stop();
}
void try_stop() {
if (is_closing_ && ref_cnt_ == 0) {
stop();
}
void on_server_error(size_t idx) {
servers_[idx].alive = false;
servers_[idx].ignore_until = td::Timestamp::in(BAD_SERVER_TIMEOUT);
}
};
td::actor::ActorOwn<ExtClient> ExtClient::create(ton::adnl::AdnlNodeIdFull dst, td::IPAddress dst_addr,
td::unique_ptr<Callback> callback) {
return create({LiteServer{dst, dst_addr, true, {}}}, std::move(callback));
td::unique_ptr<Callback> callback) {
return create({LiteServerConfig{dst, dst_addr}}, std::move(callback));
}
td::actor::ActorOwn<ExtClient> ExtClient::create(std::vector<LiteServer> servers,
td::unique_ptr<Callback> callback) {
return td::actor::create_actor<ExtClientImpl>("ExtClient", std::move(servers), std::move(callback));
td::actor::ActorOwn<ExtClient> ExtClient::create(std::vector<LiteServerConfig> liteservers,
td::unique_ptr<Callback> callback) {
return td::actor::create_actor<ExtClientImpl>("ExtClient", std::move(liteservers), std::move(callback));
}
} // namespace liteclient

View file

@ -18,29 +18,24 @@
#include "td/actor/actor.h"
#include "ton/ton-types.h"
#include "adnl/adnl-ext-client.h"
#include "query-utils.hpp"
namespace liteclient {
class ExtClient : public td::actor::Actor {
public:
struct LiteServer {
ton::adnl::AdnlNodeIdFull adnl_id;
td::IPAddress address;
bool is_full = true;
std::vector<ton::ShardIdFull> shards;
};
class Callback {
public:
virtual ~Callback() {
}
virtual ~Callback() = default;
};
virtual void send_query(std::string name, td::BufferSlice data, ton::ShardIdFull shard, td::Timestamp timeout,
virtual void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout,
td::Promise<td::BufferSlice> promise) = 0;
virtual void force_change_liteserver() = 0;
virtual void reset_servers() {
}
static td::actor::ActorOwn<ExtClient> create(ton::adnl::AdnlNodeIdFull dst, td::IPAddress dst_addr,
td::unique_ptr<Callback> callback);
static td::actor::ActorOwn<ExtClient> create(std::vector<LiteServer> servers, td::unique_ptr<Callback> callback);
static td::actor::ActorOwn<ExtClient> create(std::vector<LiteServerConfig> liteservers,
td::unique_ptr<Callback> callback);
};
} // namespace liteclient

File diff suppressed because it is too large Load diff

View file

@ -26,6 +26,7 @@
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "ext-client.h"
#include "adnl/adnl-ext-client.h"
#include "tl-utils/tl-utils.hpp"
#include "ton/ton-types.h"
@ -46,35 +47,19 @@ class TestNode : public td::actor::Actor {
min_ls_version = 0x101,
min_ls_capabilities = 1
}; // server version >= 1.1, capabilities at least +1 = build proof chains
td::actor::ActorOwn<liteclient::ExtClient> client_;
td::actor::ActorOwn<td::TerminalIO> io_;
struct LiteServer {
td::IPAddress addr;
ton::PublicKey public_key;
bool is_full = true;
std::vector<ton::ShardIdFull> shards;
td::actor::ActorOwn<ton::adnl::AdnlExtClient> client;
bool client_ready = false;
std::vector<td::Promise<td::Unit>> wait_client_ready;
bool supports(ton::ShardIdFull shard) const;
};
std::vector<LiteServer> servers_;
bool ready_ = false;
td::int32 single_liteserver_idx_ = -1;
td::IPAddress single_remote_addr_;
ton::PublicKey single_remote_public_key_;
std::map<ton::ShardIdFull, td::int32> shard_server_idx_cached_;
bool readline_enabled_ = true;
int print_limit_ = 1024;
std::string db_root_;
// mc_server is the server for queries to masterchain
int mc_server_idx_ = -1;
int mc_server_time_ = 0;
int mc_server_time_got_at_ = 0;
int mc_server_version_ = 0;
@ -443,18 +428,7 @@ class TestNode : public td::actor::Actor {
void got_result(td::Result<td::BufferSlice> R, td::Promise<td::BufferSlice> promise);
void after_got_result(bool ok);
bool envelope_send_query_to_any(td::BufferSlice query, td::Promise<td::BufferSlice> promise);
bool envelope_send_query_to_shard(ton::ShardIdFull shard, td::BufferSlice query,
td::Promise<td::BufferSlice> promise);
bool envelope_send_query_to_account(ton::AccountIdPrefixFull prefix, td::BufferSlice query,
td::Promise<td::BufferSlice> promise);
bool envelope_send_query_to_server(td::int32 server_idx, td::BufferSlice query, td::Promise<td::BufferSlice> promise);
void start_client(int server_idx);
void conn_ready(int server_idx);
void conn_closed(int server_idx);
bool envelope_send_query(td::BufferSlice query, td::Promise<td::BufferSlice> promise);
void parse_line(td::BufferSlice data);
TestNode() = default;

394
lite-client/query-utils.cpp Normal file
View file

@ -0,0 +1,394 @@
/*
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/>.
*/
#include "query-utils.hpp"
#include "block-parse.h"
#include "td/utils/overloaded.h"
#include "tl-utils/common-utils.hpp"
#include "block/block-auto.h"
#include "auto/tl/lite_api.hpp"
#include "overlay/overlay-broadcast.hpp"
#include "tl-utils/lite-utils.hpp"
#include "ton/lite-tl.hpp"
#include "ton/ton-shard.h"
#include <ton/ton-tl.hpp>
namespace liteclient {
using namespace ton;
std::string QueryInfo::to_str() const {
td::StringBuilder sb;
sb << "[ " << lite_query_name_by_id(query_id) << " " << shard_id.to_str();
switch (type) {
case t_simple:
break;
case t_seqno:
sb << " seqno=" << value;
break;
case t_utime:
sb << " utime=" << value;
break;
case t_lt:
sb << " lt=" << value;
break;
case t_mc_seqno:
sb << " mc_seqno=" << value;
break;
}
sb << " ]";
return sb.as_cslice().str();
}
QueryInfo get_query_info(td::Slice data) {
auto F = fetch_tl_object<lite_api::liteServer_query>(data, true);
if (F.is_ok()) {
data = F.ok()->data_;
} else {
fetch_tl_prefix<lite_api::liteServer_queryPrefix>(data, true).ignore();
}
fetch_tl_prefix<lite_api::liteServer_waitMasterchainSeqno>(data, true).ignore();
auto Q = fetch_tl_object<lite_api::Function>(data, true);
if (Q.is_error()) {
return {};
}
return get_query_info(*Q.ok());
}
QueryInfo get_query_info(const lite_api::Function& f) {
QueryInfo info;
info.query_id = f.get_id();
auto from_block_id = [&](const tl_object_ptr<lite_api::tonNode_blockIdExt>& id) {
BlockIdExt block_id = create_block_id(id);
info.shard_id = block_id.shard_full();
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno();
};
downcast_call(
const_cast<lite_api::Function&>(f),
td::overloaded([&](const lite_api::liteServer_getTime& q) { /* t_simple */ },
[&](const lite_api::liteServer_getVersion& q) { /* t_simple */ },
[&](const lite_api::liteServer_getMasterchainInfo& q) { /* t_simple */ },
[&](const lite_api::liteServer_getMasterchainInfoExt& q) { /* t_simple */ },
[&](const lite_api::liteServer_getBlock& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getBlockHeader& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getState& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getAccountState& q) {
BlockIdExt block_id = create_block_id(q.id_);
AccountIdPrefixFull acc_id_prefix = extract_addr_prefix(q.account_->workchain_, q.account_->id_);
info.shard_id = acc_id_prefix.as_leaf_shard();
// See LiteQuery::perform_getAccountState
if (block_id.id.workchain != masterchainId) {
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno();
} else if (block_id.id.seqno != ~0U) {
info.type = QueryInfo::t_mc_seqno;
info.value = block_id.seqno();
} else {
info.type = QueryInfo::t_simple;
}
},
[&](const lite_api::liteServer_getAccountStatePrunned& q) {
BlockIdExt block_id = create_block_id(q.id_);
AccountIdPrefixFull acc_id_prefix = extract_addr_prefix(q.account_->workchain_, q.account_->id_);
info.shard_id = acc_id_prefix.as_leaf_shard();
// See LiteQuery::perform_getAccountState
if (block_id.id.workchain != masterchainId) {
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno();
} else if (block_id.id.seqno != ~0U) {
info.type = QueryInfo::t_mc_seqno;
info.value = block_id.seqno();
} else {
info.type = QueryInfo::t_simple;
}
},
[&](const lite_api::liteServer_getOneTransaction& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getTransactions& q) {
AccountIdPrefixFull acc_id_prefix = extract_addr_prefix(q.account_->workchain_, q.account_->id_);
info.shard_id = acc_id_prefix.as_leaf_shard();
info.type = QueryInfo::t_lt;
info.value = q.lt_;
},
[&](const lite_api::liteServer_sendMessage& q) {
info.type = QueryInfo::t_simple;
auto r_root = vm::std_boc_deserialize(q.body_);
if (r_root.is_error()) {
return;
}
block::gen::CommonMsgInfo::Record_ext_in_msg_info msg_info;
if (!tlb::unpack_cell_inexact(r_root.ok(), msg_info)) {
return;
}
auto dest_prefix = block::tlb::MsgAddressInt::get_prefix(msg_info.dest);
if (!dest_prefix.is_valid()) {
return;
}
info.shard_id = dest_prefix.as_leaf_shard();
},
[&](const lite_api::liteServer_getShardInfo& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getAllShardsInfo& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_lookupBlock& q) {
BlockId block_id = create_block_id_simple(q.id_);
info.shard_id = block_id.shard_full();
// See LiteQuery::perform_lookupBlock
if (q.mode_ & 1) {
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno;
} else if (q.mode_ == 2) {
info.type = QueryInfo::t_lt;
info.value = q.lt_;
} else if (q.mode_ == 4) {
info.type = QueryInfo::t_utime;
info.value = q.utime_;
}
},
[&](const lite_api::liteServer_lookupBlockWithProof& q) {
BlockId block_id = create_block_id_simple(q.id_);
info.shard_id = block_id.shard_full();
// See LiteQuery::perform_lookupBlockWithProof
if (q.mode_ & 1) {
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno;
} else if (q.mode_ == 2) {
info.type = QueryInfo::t_lt;
info.value = q.lt_;
} else if (q.mode_ == 4) {
info.type = QueryInfo::t_utime;
info.value = q.utime_;
}
},
[&](const lite_api::liteServer_listBlockTransactions& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_listBlockTransactionsExt& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getConfigParams& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getConfigAll& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getBlockProof& q) {
info.shard_id = ShardIdFull{masterchainId};
BlockIdExt from = create_block_id(q.known_block_);
BlockIdExt to = create_block_id(q.target_block_);
// See LiteQuery::perform_getBlockProof
if ((q.mode_ & 1) && (q.mode_ & 0x1000)) {
info.type = QueryInfo::t_seqno;
info.value = std::max(from.seqno(), to.seqno());
} else {
info.type = QueryInfo::t_simple;
}
},
[&](const lite_api::liteServer_getValidatorStats& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_runSmcMethod& q) {
BlockIdExt block_id = create_block_id(q.id_);
AccountIdPrefixFull acc_id_prefix = extract_addr_prefix(q.account_->workchain_, q.account_->id_);
info.shard_id = acc_id_prefix.as_leaf_shard();
// See LiteQuery::perform_getAccountState
if (block_id.id.workchain != masterchainId) {
info.type = QueryInfo::t_seqno;
info.value = block_id.seqno();
} else if (block_id.id.seqno != ~0U) {
info.type = QueryInfo::t_mc_seqno;
info.value = block_id.seqno();
} else {
info.type = QueryInfo::t_simple;
}
},
[&](const lite_api::liteServer_getLibraries& q) { /* t_simple */ },
[&](const lite_api::liteServer_getLibrariesWithProof& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_getShardBlockProof& q) { from_block_id(q.id_); },
[&](const lite_api::liteServer_nonfinal_getCandidate& q) { /* t_simple */ },
[&](const lite_api::liteServer_nonfinal_getValidatorGroups& q) { /* t_simple */ },
[&](const lite_api::liteServer_getOutMsgQueueSizes& q) { /* t_simple */ },
[&](const auto&) { /* t_simple */ }));
if (info.shard_id.workchain == masterchainId) {
info.shard_id.shard = shardIdAll;
}
if (!info.shard_id.is_valid_ext()) {
info.shard_id = ShardIdFull{masterchainId};
info.type = QueryInfo::t_simple;
info.value = 0;
}
return info;
}
bool LiteServerConfig::accepts_query(const QueryInfo& query_info) const {
if (is_full) {
return true;
}
for (const Slice& s : slices) {
if (s.accepts_query(query_info)) {
return true;
}
}
return false;
}
bool LiteServerConfig::Slice::accepts_query(const QueryInfo& query_info) const {
if (unlimited) {
for (const ShardInfo& shard : shards_from) {
if (shard_intersects(shard.shard_id, query_info.shard_id)) {
return true;
}
}
return false;
}
if (!shards_from.empty()) {
bool from_ok = false;
DCHECK(shards_from[0].shard_id.is_masterchain());
for (const ShardInfo& shard : shards_from) {
if (shard_intersects(shard.shard_id, query_info.shard_id)) {
switch (query_info.type) {
case QueryInfo::t_simple:
from_ok = true;
break;
case QueryInfo::t_seqno:
from_ok = shard.seqno <= query_info.value;
break;
case QueryInfo::t_utime:
from_ok = shard.utime <= query_info.value;
break;
case QueryInfo::t_lt:
from_ok = shard.lt <= query_info.value;
break;
case QueryInfo::t_mc_seqno:
from_ok = shards_from[0].seqno <= query_info.value;
break;
}
if (from_ok) {
break;
}
}
}
if (!from_ok) {
return false;
}
}
if (!shards_to.empty()) {
bool to_ok = false;
DCHECK(shards_to[0].shard_id.is_masterchain());
for (const ShardInfo& shard : shards_to) {
if (shard_intersects(shard.shard_id, query_info.shard_id)) {
switch (query_info.type) {
case QueryInfo::t_simple:
break;
case QueryInfo::t_seqno:
to_ok = shard.seqno >= query_info.value;
break;
case QueryInfo::t_utime:
to_ok = shard.utime >= query_info.value;
break;
case QueryInfo::t_lt:
to_ok = shard.lt >= query_info.value;
break;
case QueryInfo::t_mc_seqno:
to_ok = shards_from[0].seqno >= query_info.value;
break;
}
if (to_ok) {
break;
}
}
}
if (!to_ok) {
return false;
}
}
return true;
}
td::Result<std::vector<LiteServerConfig>> LiteServerConfig::parse_global_config(
const ton_api::liteclient_config_global& config) {
std::vector<LiteServerConfig> servers;
for (const auto& f : config.liteservers_) {
LiteServerConfig server;
TRY_STATUS(server.addr.init_host_port(td::IPAddress::ipv4_to_str(f->ip_), f->port_));
server.adnl_id = adnl::AdnlNodeIdFull{PublicKey{f->id_}};
server.is_full = true;
servers.push_back(std::move(server));
}
for (const auto& f : config.liteservers_v2_) {
LiteServerConfig server;
TRY_STATUS(server.addr.init_host_port(td::IPAddress::ipv4_to_str(f->ip_), f->port_));
server.adnl_id = adnl::AdnlNodeIdFull{PublicKey{f->id_}};
server.is_full = false;
for (const auto& slice_obj : f->slices_) {
Slice slice;
td::Status S = td::Status::OK();
downcast_call(*slice_obj,
td::overloaded(
[&](const ton_api::liteserver_descV2_sliceSimple& s) {
slice.unlimited = true;
slice.shards_from.push_back({ShardIdFull{masterchainId}, 0, 0, 0});
for (const auto& shard_obj : s.shards_) {
ShardIdFull shard_id = create_shard_id(shard_obj);
if (!shard_id.is_valid_ext()) {
S = td::Status::Error(PSTRING() << "invalid shard id " << shard_id.to_str());
break;
}
if (!shard_id.is_masterchain()) {
slice.shards_from.push_back({shard_id, 0, 0, 0});
}
}
},
[&](const ton_api::liteserver_descV2_sliceTimed& s) {
auto parse_shards =
[](const std::vector<tl_object_ptr<ton_api::liteserver_descV2_shardInfo>>& shard_objs,
std::vector<ShardInfo>& shards) -> td::Status {
if (shard_objs.empty()) {
return td::Status::OK();
}
size_t i = 0;
int mc_idx = -1;
for (const auto& shard_obj : shard_objs) {
ShardIdFull shard_id = create_shard_id(shard_obj->shard_id_);
if (!shard_id.is_valid_ext()) {
return td::Status::Error(PSTRING() << "invalid shard id " << shard_id.to_str());
}
if (shard_id.is_masterchain()) {
shard_id = ShardIdFull{masterchainId};
if (mc_idx != -1) {
return td::Status::Error("duplicate masterchain shard in sliceTimed");
}
mc_idx = (int)i;
}
shards.push_back({shard_id, (BlockSeqno)shard_obj->seqno_, (UnixTime)shard_obj->utime_,
(LogicalTime)shard_obj->lt_});
++i;
}
if (mc_idx == -1) {
return td::Status::Error("no masterchain shard in sliceTimed");
}
std::swap(shards[0], shards[mc_idx]);
return td::Status::OK();
};
S = parse_shards(s.shards_from_, slice.shards_from);
if (S.is_ok()) {
S = parse_shards(s.shards_to_, slice.shards_to);
}
if (S.is_ok() && slice.shards_from.empty() && slice.shards_to.empty()) {
S = td::Status::Error("shards_from and shards_to are both empty");
}
}));
TRY_STATUS(std::move(S));
server.slices.push_back(slice);
}
servers.push_back(std::move(server));
}
return servers;
}
} // namespace liteclient

View file

@ -0,0 +1,89 @@
/*
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/>.
*/
#pragma once
#include "ton/ton-types.h"
#include "auto/tl/lite_api.h"
#include "td/utils/port/IPAddress.h"
#include "adnl/adnl-node-id.hpp"
namespace liteclient {
struct QueryInfo {
enum Type { t_simple, t_seqno, t_utime, t_lt, t_mc_seqno };
int query_id = 0;
ton::ShardIdFull shard_id{ton::masterchainId};
Type type = t_simple;
td::uint64 value = 0;
/* Query types and examples:
* t_simple - query to the recent blocks in a shard, or general info. value = 0.
* getTime, getMasterchainInfo (shard_id = masterchain)
* sendMessage
* getAccountState, runSmcMethod - when no block is given
* t_seqno - query to block with seqno in a shard. value = seqno.
* lookupBlock by seqno
* getBlock, getBlockHeader
* getAccountState, runSmcMethod - when shard block is given
* t_utime - query to a block with given unixtime in a shard. value = utime.
* lookupBlock by utime
* t_lt - query to a block with given lt in a shard. value = lt.
* lookupBlock by lt
* getTransactions
* t_mc_seqno - query to a block in a shard, masterchain seqno is given. value = mc_seqno.
* getAccountState, runSmcMethod - when mc block is given
*/
std::string to_str() const;
};
QueryInfo get_query_info(td::Slice data);
QueryInfo get_query_info(const ton::lite_api::Function& f);
struct LiteServerConfig {
private:
struct ShardInfo {
ton::ShardIdFull shard_id;
ton::BlockSeqno seqno;
ton::UnixTime utime;
ton::LogicalTime lt;
};
struct Slice {
std::vector<ShardInfo> shards_from, shards_to;
bool unlimited = false;
bool accepts_query(const QueryInfo& query_info) const;
};
bool is_full = false;
std::vector<Slice> slices;
public:
ton::adnl::AdnlNodeIdFull adnl_id;
td::IPAddress addr;
LiteServerConfig() = default;
LiteServerConfig(ton::adnl::AdnlNodeIdFull adnl_id, td::IPAddress addr)
: is_full(true), adnl_id(adnl_id), addr(addr) {
}
bool accepts_query(const QueryInfo& query_info) const;
static td::Result<std::vector<LiteServerConfig>> parse_global_config(
const ton::ton_api::liteclient_config_global& config);
};
} // namespace liteclient