Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kdimentionaltree committed Jan 5, 2025
1 parent 0d63a83 commit 6382d3e
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 5 deletions.
23 changes: 23 additions & 0 deletions ton-http-api/handler_api_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ core::TonlibWorkerResponse ApiV2Handler::HandleTonlibRequest(const TonlibApiRequ
auto res = tonlib_component_.DoRequest(&core::TonlibWorker::getMasterchainInfo);
return core::TonlibWorkerResponse::from_tonlib_result(std::move(res));
}
if (ton_api_method == "getconsensusblock") {
auto res = tonlib_component_.DoRequest(&core::TonlibWorker::getConsensusBlock);
if (res.is_error()) {
return core::TonlibWorkerResponse::from_error_string(res.move_as_error().to_string());
}
auto res_str = res.move_as_ok().to_json_string();
return core::TonlibWorkerResponse::from_result_string(res_str);
}

if (ton_api_method == "getmasterchainblocksignatures") {
auto seqno = utils::stringToInt<ton::BlockSeqno>(request.GetArg("seqno"));
Expand Down Expand Up @@ -341,6 +349,21 @@ core::TonlibWorkerResponse ApiV2Handler::HandleTonlibRequest(const TonlibApiRequ
return tonlib_component_.DoPostprocess(&core::TonlibPostProcessor::process_getTransactions, std::move(res), ton_api_method == "gettransactionsv2");
}

if (ton_api_method == "getconfigparam") {
auto config_id = utils::stringToInt<std::int32_t>(request.GetArg("config_id"));
auto seqno = utils::stringToInt<ton::BlockSeqno>(request.GetArg("seqno"));
if (!config_id.has_value()) {
return core::TonlibWorkerResponse::from_error_string("config_id is required");
}
auto res = tonlib_component_.DoRequest(&core::TonlibWorker::getConfigParam, config_id.value(), seqno);
return core::TonlibWorkerResponse::from_tonlib_result(std::move(res));
}
if (ton_api_method == "getconfigall") {
auto seqno = utils::stringToInt<ton::BlockSeqno>(request.GetArg("seqno"));
auto res = tonlib_component_.DoRequest(&core::TonlibWorker::getConfigAll, seqno);
return core::TonlibWorkerResponse::from_tonlib_result(std::move(res));
}

return {false, nullptr, std::nullopt,
td::Status::Error(404, "method not found")};
}
Expand Down
4 changes: 3 additions & 1 deletion ton-http-api/openapi/openapi.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
docExpansion:"list",
tryItOutEnabled:true,
showMutatedRequest:false,
showExtensions: true,
showCommonExtensions: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
SwaggerUIBundle.SwaggerUIStandalonePreset
],
syntaxHighlight: {"activate":true,"theme":"agate"}
});
Expand Down
11 changes: 10 additions & 1 deletion ton-http-api/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
},
"servers": [
{
"url": "/api/v2"
"url": "/",
"description": "Current"
},
{
"url": "https://toncenter.com",
"description": "TON Center API v2 - mainnet"
},
{
"url": "https://testnet.toncenter.com",
"description": "TON Center API v2 - testnet"
}
],
"paths": {
Expand Down
15 changes: 13 additions & 2 deletions ton-http-api/openapi/openapi_page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ std::string GetOpenApiPage() {
docExpansion:"list",
tryItOutEnabled:true,
showMutatedRequest:false,
showExtensions: true,
showCommonExtensions: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
SwaggerUIBundle.SwaggerUIStandalonePreset
],
syntaxHighlight: {"activate":true,"theme":"agate"}
});
Expand All @@ -50,7 +52,16 @@ std::string GetOpenApiJson() {
},
"servers": [
{
"url": "/api/v2"
"url": "/",
"description": "Current"
},
{
"url": "https://toncenter.com",
"description": "TON Center API v2 - mainnet"
},
{
"url": "https://testnet.toncenter.com",
"description": "TON Center API v2 - testnet"
}
],
"paths": {
Expand Down
105 changes: 105 additions & 0 deletions ton-http-api/tonlib_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ std::string DetectHashResult::to_json_string() const {
auto res = ToString(builder.ExtractValue());
return std::move(res);
}
std::string ConsensusBlockResult::to_json_string() const {
using namespace userver::formats::json;

ValueBuilder builder;
builder["consensus_block"] = seqno;
builder["timestamp"] = timestamp;
auto res = ToString(builder.ExtractValue());
return std::move(res);
}

td::Result<ConsensusBlockResult> TonlibWorker::getConsensusBlock() const {
auto res = tonlib_.get_consensus_block();
if (res.is_error()) {
return res.move_as_error();
}
return ConsensusBlockResult{ res.move_as_ok(), std::time(nullptr) };
}
td::Result<DetectAddressResult> TonlibWorker::detectAddress(const std::string& address) const {
auto r_std_address = block::StdAddress::parse(address);
if (r_std_address.is_error()) {
Expand Down Expand Up @@ -363,6 +379,95 @@ td::Result<tonlib_api::blocks_getOutMsgQueueSizes::ReturnType> TonlibWorker::get
auto result = send_request_function(std::move(request), true);
return std::move(result);
}
td::Result<tonlib_api::getConfigParam::ReturnType> TonlibWorker::getConfigParam(
const std::int32_t& param, std::optional<ton::BlockSeqno> seqno
) const {
tonlib_api::object_ptr<tonlib_api::ton_blockIdExt> with_block;
if (seqno.has_value()) {
auto res = lookupBlock(ton::masterchainId, ton::shardIdAll, seqno.value());
if (!res.is_ok()) {
return res.move_as_error();
}
with_block = res.move_as_ok();
}
if (!with_block) {
auto request = multiclient::RequestFunction<tonlib_api::getConfigParam>{
.parameters = {.mode = multiclient::RequestMode::Single},
.request_creator =
[param_ = param] {
return tonlib_api::make_object<tonlib_api::getConfigParam>(param_, 0);
}
};
auto result = send_request_function(std::move(request), true);
return std::move(result);
} else {
auto request = multiclient::RequestFunction<tonlib_api::withBlock>{
.parameters = {.mode = multiclient::RequestMode::Single},
.request_creator =
[param_ = param,
workchain_ = with_block->workchain_,
shard_ = with_block->shard_,
seqno_ = with_block->seqno_,
root_hash_ = with_block->root_hash_,
file_hash_ = with_block->file_hash_] {
return tonlib_api::make_object<tonlib_api::withBlock>(
tonlib_api::make_object<tonlib_api::ton_blockIdExt>(
workchain_, shard_, seqno_, root_hash_, file_hash_
),
tonlib_api::make_object<tonlib_api::getConfigParam>(param_, 0)
);
}
};
auto result = send_request_function(std::move(request), true);
if (result.is_error()) {
return result.move_as_error();
}
return ton::move_tl_object_as<tonlib_api::configInfo>(result.move_as_ok());
}
}
td::Result<tonlib_api::getConfigParam::ReturnType> TonlibWorker::getConfigAll(std::optional<ton::BlockSeqno> seqno) const {
tonlib_api::object_ptr<tonlib_api::ton_blockIdExt> with_block;
if (seqno.has_value()) {
auto res = lookupBlock(ton::masterchainId, ton::shardIdAll, seqno.value());
if (!res.is_ok()) {
return res.move_as_error();
}
with_block = res.move_as_ok();
}
if (!with_block) {
auto request = multiclient::RequestFunction<tonlib_api::getConfigAll>{
.parameters = {.mode = multiclient::RequestMode::Single},
.request_creator =
[] {
return tonlib_api::make_object<tonlib_api::getConfigAll>(0);
}
};
auto result = send_request_function(std::move(request), true);
return std::move(result);
} else {
auto request = multiclient::RequestFunction<tonlib_api::withBlock>{
.parameters = {.mode = multiclient::RequestMode::Single},
.request_creator =
[workchain_ = with_block->workchain_,
shard_ = with_block->shard_,
seqno_ = with_block->seqno_,
root_hash_ = with_block->root_hash_,
file_hash_ = with_block->file_hash_] {
return tonlib_api::make_object<tonlib_api::withBlock>(
tonlib_api::make_object<tonlib_api::ton_blockIdExt>(
workchain_, shard_, seqno_, root_hash_, file_hash_
),
tonlib_api::make_object<tonlib_api::getConfigAll>(0)
);
}
};
auto result = send_request_function(std::move(request), true);
if (result.is_error()) {
return result.move_as_error();
}
return ton::move_tl_object_as<tonlib_api::configInfo>(result.move_as_ok());
}
}
td::Result<tonlib_api::blocks_getTransactions::ReturnType> TonlibWorker::raw_getBlockTransactions(
const tonlib_api::object_ptr<tonlib_api::ton_blockIdExt>& blk_id,
size_t count,
Expand Down
11 changes: 11 additions & 0 deletions ton-http-api/tonlib_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ struct DetectHashResult {
[[nodiscard]] std::string to_json_string() const;
};

struct ConsensusBlockResult {
std::int32_t seqno;
std::time_t timestamp;
[[nodiscard]] std::string to_json_string() const;
};

// TonlibWorker
struct TonlibWorkerResponse {
bool is_ok{false};
Expand Down Expand Up @@ -52,6 +58,7 @@ class TonlibWorker {
explicit TonlibWorker(const multiclient::MultiClientConfig& config) : tonlib_(config) {};
~TonlibWorker() = default;

td::Result<ConsensusBlockResult> getConsensusBlock() const;
td::Result<DetectAddressResult> detectAddress(const std::string& address) const;
td::Result<std::string> packAddress(const std::string& address) const;
td::Result<std::string> unpackAddress(const std::string& address) const;
Expand Down Expand Up @@ -81,6 +88,10 @@ class TonlibWorker {
const std::string& file_hash = ""
) const;
td::Result<tonlib_api::blocks_getOutMsgQueueSizes::ReturnType> getOutMsgQueueSizes() const;

td::Result<tonlib_api::getConfigParam::ReturnType> getConfigParam(const std::int32_t& param, std::optional<ton::BlockSeqno> seqno = std::nullopt) const;
td::Result<tonlib_api::getConfigParam::ReturnType> getConfigAll(std::optional<ton::BlockSeqno> seqno = std::nullopt) const;

td::Result<tonlib_api::blocks_getTransactions::ReturnType> raw_getBlockTransactions(const tonlib_api::object_ptr<tonlib_api::ton_blockIdExt>& blk_id,
size_t count, tonlib_api::object_ptr<tonlib_api::blocks_accountTransactionId>&& after = nullptr, std::optional<bool> archival = std::nullopt) const;
td::Result<tonlib_api::blocks_getTransactionsExt::ReturnType> raw_getBlockTransactionsExt(const tonlib_api::object_ptr<tonlib_api::ton_blockIdExt>& blk_id,
Expand Down
14 changes: 14 additions & 0 deletions tonlib-multiclient/multi_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,19 @@ void MultiClient::send_callback_request(RequestCallback req) const {
td::actor::send_closure(client_.get(), &MultiClientActor::send_callback_request, std::move(req));
});
}
td::Result<std::int32_t> MultiClient::get_consensus_block() const {
std::promise<td::Result<std::int32_t>> request_promise;
auto request_future = request_promise.get_future();

auto promise = td::Promise<std::int32_t>([p = std::move(request_promise)](auto result) mutable {
p.set_value(std::move(result));
});

scheduler_->run_in_context_external([this, p = std::move(promise)]() mutable {
td::actor::send_closure(client_.get(), &MultiClientActor::get_consensus_block, std::move(p));
});

return request_future.get();
}

} // namespace multiclient
1 change: 1 addition & 0 deletions tonlib-multiclient/multi_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MultiClient {
td::Result<std::string> send_request_json(RequestJson req) const;
void send_callback_request(RequestCallback req) const;

td::Result<std::int32_t> get_consensus_block() const;
private:
const MultiClientConfig config_;
std::shared_ptr<td::actor::Scheduler> scheduler_;
Expand Down
13 changes: 13 additions & 0 deletions tonlib-multiclient/multi_client_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ void MultiClientActor::on_archival_checked(size_t worker_index, bool is_archival
LOG(DEBUG) << "LS #" << worker_index << " archival: " << is_archival;
workers_[worker_index].is_archival = is_archival;
}
void MultiClientActor::get_consensus_block(td::Promise<std::int32_t>&& promise) {
std::int32_t consensus_block = 0;
for (const auto& worker : workers_) {
if (worker.is_alive && worker.last_mc_seqno > consensus_block) {
consensus_block = worker.last_mc_seqno;
}
}
if (consensus_block == 0) {
promise.set_error(td::Status::Error(500, "no workers alive"));
return;
}
promise.set_result(consensus_block);
}

std::vector<size_t> MultiClientActor::select_workers(const RequestParameters& options) const {
std::vector<size_t> result;
Expand Down
2 changes: 1 addition & 1 deletion tonlib-multiclient/multi_client_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MultiClientActor : public td::actor::Actor {
size_t worker_count() const {
return workers_.size();
}

void get_consensus_block(td::Promise<std::int32_t>&& promise);
private:
struct WorkerInfo {
td::actor::ActorOwn<ClientWrapper> id;
Expand Down

0 comments on commit 6382d3e

Please sign in to comment.