1+ #include " adnl/adnl-ext-client.h"
2+ #include " auto/tl/ton_api.hpp"
3+
14#include " EngineConsoleClient.h"
25
36namespace tonlib {
@@ -11,13 +14,68 @@ bool is_engine_console_query(ton::tl_object_ptr<ton::ton_api::Function> const& f
1114 }
1215}
1316
14- EngineConsoleClient::EngineConsoleClient (td::IPAddress address, ton::PublicKey public_key,
15- ton::PrivateKey private_key) {
17+ class EngineConsoleClientCallback : public ton ::adnl::AdnlExtClient::Callback {
18+ public:
19+ EngineConsoleClientCallback (td::actor::ActorId<EngineConsoleClient> id) : id_(std::move(id)) {
20+ }
21+
22+ void on_ready () override {
23+ td::actor::send_closure (id_, &EngineConsoleClient::on_ready);
24+ }
25+
26+ void on_stop_ready () override {
27+ td::actor::send_closure (id_, &EngineConsoleClient::on_stop_ready);
28+ }
29+
30+ private:
31+ td::actor::ActorId<EngineConsoleClient> id_;
32+ };
33+
34+ EngineConsoleClient::EngineConsoleClient (td::IPAddress address, ton::PublicKey public_key, ton::PrivateKey private_key)
35+ : address_(address), public_key_(std::move(public_key)), private_key_(std::move(private_key)) {
36+ }
37+
38+ void EngineConsoleClient::on_ready () {
39+ ready_ = true ;
40+ for (auto & promise : pending_ready_promises_) {
41+ promise.set_value (td::Unit ());
42+ }
43+ pending_ready_promises_.clear ();
44+ }
45+
46+ void EngineConsoleClient::on_stop_ready () {
47+ for (auto & promise : pending_ready_promises_) {
48+ promise.set_error (td::Status::Error (" Connection closed" ));
49+ }
50+ pending_ready_promises_.clear ();
51+ ready_ = false ;
52+ client_ = {};
1653}
1754
1855td::actor::Task<ton::tl_object_ptr<ton::ton_api::Object>> EngineConsoleClient::query (
1956 ton::tl_object_ptr<ton::ton_api::Function> object) {
20- co_return td::Status::Error (" Not implemented" );
57+ if (!ready_) {
58+ if (client_.empty ()) {
59+ client_ = ton::adnl::AdnlExtClient::create (ton::adnl::AdnlNodeIdFull{public_key_}, private_key_, address_,
60+ std::make_unique<EngineConsoleClientCallback>(actor_id (this )));
61+ }
62+
63+ auto [ready_awaiter, ready_promise] = td::actor::StartedTask<td::Unit>::make_bridge ();
64+ pending_ready_promises_.push_back (std::move (ready_promise));
65+ co_await std::move (ready_awaiter);
66+ }
67+
68+ auto query_bytes = ton::serialize_tl_object (object, true );
69+ auto wrapped_query = ton::serialize_tl_object (
70+ ton::create_tl_object<ton::ton_api::engine_validator_controlQuery>(std::move (query_bytes)), true );
71+
72+ auto [response_awaiter, response_promise] = td::actor::StartedTask<td::BufferSlice>::make_bridge ();
73+ td::actor::send_closure (client_, &ton::adnl::AdnlExtClient::send_query, " query" , std::move (wrapped_query),
74+ td::Timestamp::in (10.0 ), std::move (response_promise));
75+ auto response = co_await std::move (response_awaiter);
76+
77+ auto result_obj = co_await ton::fetch_tl_object<ton::ton_api::Object>(response, true );
78+ co_return std::move (result_obj);
2179}
2280
2381} // namespace tonlib
0 commit comments