Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 120
DeriveLineEnding: false
IndentWidth: 2
ContinuationIndentWidth: 2
IndentCaseBlocks: false
IndentExternBlock: AfterExternBlock
InsertNewlineAtEOF: true
Expand Down
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
set(PY_TONLIB_MULTICLIENT TRUE CACHE BOOL "Build python bindings")
set(TONLIB_MULTICLIENT_EXAMPLES TRUE CACHE BOOL "Build examples")
set(TON_HTTP_API_CPP TRUE CACHE BOOL "Build TON HTTP API")
set(BUILD_TON_PLAYGROUND FALSE CACHE BOOL "Build TON playground")

add_subdirectory(external/ton EXCLUDE_FROM_ALL)

Expand All @@ -23,6 +24,12 @@ if (PY_TONLIB_MULTICLIENT)
add_subdirectory(py)
endif()


if (BUILD_TON_PLAYGROUND)
message("-- Building TON Playground binary")
add_subdirectory(playground)
endif()

if (TON_HTTP_API_CPP)
# userver config
set(USERVER_FEATURE_REDIS TRUE)
Expand All @@ -34,4 +41,4 @@ if (TON_HTTP_API_CPP)
set(USERVER_USE_STATIC_LIBS TRUE)
add_subdirectory(external/userver EXCLUDE_FROM_ALL)
add_subdirectory(ton-http-api)
endif()
endif()
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt update -y \
libhiredis-dev libidn11-dev libjemalloc2 libjemalloc-dev libkrb5-dev libldap2-dev liblz4-dev \
libnghttp2-dev libpugixml-dev libsnappy-dev libsasl2-dev libssl-dev libxxhash-dev libyaml-cpp0.8 libyaml-cpp-dev \
libzstd-dev libssh2-1-dev netbase python3-dev python3-jinja2 python3-venv python3-yaml \
ragel yasm zlib1g-dev liblzma-dev libre2-dev clang-format gcc g++ \
ragel yasm zlib1g-dev liblzma-dev libre2-dev clang-format gcc g++ yq \
&& rm -rf /var/lib/apt/lists/*

ENV CC=/usr/bin/gcc
Expand Down
11 changes: 9 additions & 2 deletions config/config_vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ log_path: "@stdout" # log destination, available options:
# /path/to/file - log to file
# Note: if you log into file,
# logs will be deleted after deploying new container
log_format: json # logs format, one of `tskv`, `ltsv`, `json`
system_log_level: warning # userver system logs
system_log_path: "@null"
system_log_path: "@stdout"

jsonrpc_log_level: warning # jsonrpc endpoint logs
# note that requests to jsonRPC endpoint will appear in both of api-v2 and jsonrpc loggers
jsonrpc_log_path: "@stdout"

log_format: json # logs format, one of `tskv`, `ltsv`, `json`

http_worker_user_agent: empty # http user agent to set in request to boc endpoint

static_content_dir: "/static/" # directory for static content
123 changes: 103 additions & 20 deletions config/static_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,26 @@ components_manager:
format: $log_format
format#fallback: json
overflow_behavior: discard
api-v2-jsonrpc:
file_path: $jsonrpc_log_path
file_path#fallback: "@stdout"
level: $jsonrpc_log_level
level#fallback: warning
format: $log_format
format#fallback: json
overflow_behavior: discard
#
# components
#
tonlib:
global_config: $tonlib_config_path
keystore: $tonlib_keystore_path
keystore#fallback: /tmp/keystore/
threads: $tonlib_threads
threads#fallback: 4
external_message_endpoints: $tonlib_boc_endpoints
external_message_endpoints#fallback: []
# external_message_endpoints: $tonlib_boc_endpoints
# external_message_endpoints#fallback: []
task_processor: main-task-processor
cache-api-v2:
ways: 256
size: 20480
lifetime: 1
background-update: true
dns-client:
fs-task-processor: fs-task-processor
http-client:
Expand All @@ -74,12 +80,82 @@ components_manager:
destination-metrics-auto-max-size: 100
user-agent: $http_worker_user_agent
user-agent#fallback: empty
handler-server-monitor:
path: $monitor_path
path#fallback: /stats
method: GET
task_processor: monitor-task-processor
format: json
fs-cache-main:
dir: $static_content_dir
dir#fallback: /app/static/
update-period: 10s
#
# api v2 handlers
#
## utils
handler-DetectAddress:
path: /api/v2/detectAddress
method: GET,POST
logger: api-v2
handler-DetectHash:
path: /api/v2/detectHash
method: GET,POST
logger: api-v2
handler-PackAddress:
path: /api/v2/packAddress
method: GET,POST
logger: api-v2
handler-UnpackAddress:
path: /api/v2/unpackAddress
method: GET,POST
logger: api-v2
handler-GetAddressInformation:
path: /api/v2/getAddressInformation
method: GET,POST
logger: api-v2
handler-GetExtendedAddressInformation:
path: /api/v2/getExtendedAddressInformation
method: GET,POST
logger: api-v2
handler-GetWalletInformation:
path: /api/v2/getWalletInformation
method: GET,POST
logger: api-v2
handler-GetAddressBalance:
path: /api/v2/getAddressBalance
method: GET,POST
logger: api-v2
handler-GetAddressState:
path: /api/v2/getAddressState
method: GET,POST
logger: api-v2
handler-GetTokenData:
path: /api/v2/getTokenData
method: GET,POST
logger: api-v2

## blocks
handler-GetMasterchainInfo:
path: /api/v2/getMasterchainInfo
method: GET,POST
logger: api-v2
handler-GetMasterchainBlockSignatures:
path: /api/v2/getMasterchainBlockSignatures
method: GET,POST
logger: api-v2
handler-GetShardBlockProof:
path: /api/v2/getShardBlockProof
method: GET,POST
logger: api-v2

#
# jsonrpc handler
#
handler-JsonRpc:
path: /api/v2/jsonRPC
method: POST
logger: api-v2-jsonrpc
port: $server_port
port#fallback: 8081

#
# service handlers
#
handler-ping:
path: $ping_path
path#fallback: /health
Expand All @@ -88,10 +164,17 @@ components_manager:
log-level: warning
throttling_enabled: false
url_trailing_slash: strict-match
handler-api-v2:
path: /api/v2/{ton_api_method}
method: GET,POST
status-codes-log-level:
409: debug
500: debug
task_processor: main-task-processor
handler-static:
fs-cache-component: fs-cache-main
path: /api/v2/*
method: GET

#
# monitor handlers
#
handler-server-monitor:
path: $monitor_path
path#fallback: /stats
method: GET
task_processor: monitor-task-processor
format: json
30 changes: 22 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# TONlib Multiclient

The TONlib Multi Client is crafted with modern C++ to serve as a robust and flexible tool for seamless communication with multiple TON blockchain lite servers. It's engineered for efficiency, allowing for streamlined request handling across a diverse array of server endpoints.
The TONlib Multi Client is crafted with modern C++ to serve as a robust and flexible tool for seamless communication
with multiple TON blockchain lite servers. It's engineered for efficiency, allowing for streamlined request handling
across a diverse array of server endpoints.

## Features

- Multi-threaded Design: Enhances the ability to handle concurrent connections and requests efficiently.
- Flexible Request Handling: Supports a variety of request types, ensuring robust interaction with blockchain lite servers.
- Dynamic Request Creation: Allows for the creation of customizable requests, enabling tailored blockchain queries and operations.
- Advanced Configuration Options: Offers detailed settings for requests, including server selection and archival data queries, providing users with control over their blockchain interactions.
- Flexible Request Handling: Supports a variety of request types, ensuring robust interaction with blockchain lite
servers.
- Dynamic Request Creation: Allows for the creation of customizable requests, enabling tailored blockchain queries and
operations.
- Advanced Configuration Options: Offers detailed settings for requests, including server selection and archival data
queries, providing users with control over their blockchain interactions.

## Requirements

Expand Down Expand Up @@ -35,21 +40,30 @@ cmake -DPY_TONLIB_MULTICLIENT:BOOL=TRUE ..
cmake --build .
```

To use built python binding you need to copy `tonlib_multiclient` dynamic lib (.so) from `build/py` directory to your python project.
To use built python binding you need to copy `tonlib_multiclient` dynamic lib (.so) from `build/py` directory to your
python project.

```bash
cp build/py/tonlib_multiclient.so /path/to/your/python/project
```

## Requests

### Request<T>

This structure is suitable for general request operations where the response type is known and directly utilized.

### RequestFunction<T>
A specialized version of Request for cases where `TonlibClient::make_request` cannot process the request (`raw_getAccountState` etc).

A specialized version of Request for cases where `TonlibClient::make_request` cannot process the request (
`raw_getAccountState` etc).

### RequestCallback
Designed for advanced use cases where the user must initialize and manage the response through your own global callback. Requires explicit setup for callback handling.

Designed for advanced use cases where the user must initialize and manage the response through your own global callback.
Requires explicit setup for callback handling.

### RequestJson
Enables sending requests in raw JSON format, requiring minimal configuration besides the JSON string itself and the standard request parameters.

Enables sending requests in raw JSON format, requiring minimal configuration besides the JSON string itself and the
standard request parameters.
38 changes: 20 additions & 18 deletions examples/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include "auto/tl/tonlib_api.h"
#include "auto/tl/tonlib_api.hpp"
#include "auto/tl/tonlib_api_json.h"
#include "tonlib-multiclient/multi_client.h"
#include "tonlib-multiclient/request.h"
#include "tonlib-multiclient/response_callback.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "tl/tl_json.h"
#include "tonlib-multiclient/multi_client.h"
#include "tonlib-multiclient/request.h"
#include "tonlib-multiclient/response_callback.h"
#include "tonlib/Logging.h"

struct Cb : public multiclient::ResponseCallback {
Expand Down Expand Up @@ -45,12 +45,12 @@ int main(int argc, char* argv[]) {
std::unordered_set<uint64_t> requests{};

multiclient::MultiClient client(
multiclient::MultiClientConfig{
.global_config_path = std::filesystem::path("/tmp/global-config.json"),
.key_store_root = std::filesystem::path("/tmp/keystore"),
.scheduler_threads = 6,
},
std::make_unique<Cb>(requests)
multiclient::MultiClientConfig{
.global_config_path = std::filesystem::path("/tmp/global-config.json"),
.key_store_root = std::filesystem::path("/tmp/keystore"),
.scheduler_threads = 6,
},
std::make_unique<Cb>(requests)
);

sleep(5);
Expand All @@ -62,18 +62,20 @@ int main(int argc, char* argv[]) {
requests.insert(req_id);

LOG(INFO) << "send request, id: " << req_id;
client.send_callback_request(multiclient::RequestCallback{
client.send_callback_request(
multiclient::RequestCallback{
.parameters = {.mode = multiclient::RequestMode::Broadcast},
.request_creator =
[]() {
return ton::tonlib_api::make_object<ton::tonlib_api::getAccountState>(
ton::tonlib_api::make_object<ton::tonlib_api::accountAddress>(
"UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"
)
);
},
[]() {
return ton::tonlib_api::make_object<ton::tonlib_api::getAccountState>(
ton::tonlib_api::make_object<ton::tonlib_api::accountAddress>(
"UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"
)
);
},
.request_id = req_id,
});
}
);
}

return 0;
Expand Down
16 changes: 10 additions & 6 deletions examples/json.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
#include <unistd.h>
#include "td/utils/logging.h"
#include "tonlib-multiclient/multi_client.h"
#include "tonlib-multiclient/request.h"
#include "td/utils/logging.h"
#include "tonlib/Logging.h"

int main(int argc, char* argv[]) {
tonlib::Logging::set_verbosity_level(3);

multiclient::MultiClient client(multiclient::MultiClientConfig{
multiclient::MultiClient client(
multiclient::MultiClientConfig{
.global_config_path = std::filesystem::path("/tmp/global-config.json"),
.key_store_root = std::filesystem::path("/tmp/keystore"),
.scheduler_threads = 6,
});
}
);

sleep(5);

while (true) {
sleep(5);
LOG(INFO) << "send request";
auto resp = client.send_request_json(multiclient::RequestJson{
auto resp = client.send_request_json(
multiclient::RequestJson{
.parameters = {.mode = multiclient::RequestMode::Broadcast},
.request =
R"({"@type":"getAccountState","account_address":{"@type":"accountAddress","account_address":"UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"}})",
});
R"({"@type":"getAccountState","account_address":{"@type":"accountAddress","account_address":"UQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqEBI"}})",
}
);

if (resp.is_error()) {
LOG(ERROR) << "resp: " << resp.error();
Expand Down
Loading