Skip to content

Commit

Permalink
Merge pull request #33 from IrKor/master
Browse files Browse the repository at this point in the history
Liteclient update - 19.08.19
  • Loading branch information
skywinder authored Aug 19, 2019
2 parents d86845d + fe41b47 commit 5323b3b
Show file tree
Hide file tree
Showing 98 changed files with 12,938 additions and 7,996 deletions.
2 changes: 1 addition & 1 deletion ton-test-liteclient-full/lite-client/adnl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ set (ADNL_LITE_SOURCE
add_library(adnllite STATIC ${ADNL_LITE_SOURCE})

target_include_directories(adnllite PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(adnllite PUBLIC tdactor ton_crypto tl_api tdnet keys )
target_link_libraries(adnllite PUBLIC tdactor ton_crypto tl_lite_api tdnet keys )
86 changes: 85 additions & 1 deletion ton-test-liteclient-full/lite-client/adnl/adnl-ext-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,84 @@ void AdnlExtClientImpl::alarm() {

conn_ = td::actor::create_actor<AdnlOutboundConnection>(td::actor::ActorOptions().with_name("outconn").with_poll(),
fd.move_as_ok(), std::make_unique<Cb>(actor_id(this)), dst_,
actor_id(this));
local_id_, actor_id(this));
}
}

td::Status AdnlOutboundConnection::process_custom_packet(td::BufferSlice &data, bool &processed) {
if (data.size() == 12) {
auto F = fetch_tl_object<ton_api::tcp_pong>(data.clone(), true);
if (F.is_ok()) {
processed = true;
return td::Status::OK();
}
}
if (!local_id_.empty() && nonce_.size() != 0) {
auto F = fetch_tl_object<ton_api::tcp_authentificationNonce>(data.clone(), true);
if (F.is_ok()) {
auto f = F.move_as_ok();
if (f->nonce_.size() == 0 || f->nonce_.size() > 512) {
return td::Status::Error(ErrorCode::protoviolation, "bad nonce size");
}
td::SecureString ss{nonce_.size() + f->nonce_.size()};
ss.as_mutable_slice().copy_from(nonce_.as_slice());
ss.as_mutable_slice().remove_prefix(nonce_.size()).copy_from(f->nonce_.as_slice());

TRY_RESULT(dec, local_id_.create_decryptor());
TRY_RESULT(B, dec->sign(ss.as_slice()));

auto obj =
create_tl_object<ton_api::tcp_authentificationComplete>(local_id_.compute_public_key().tl(), std::move(B));
send(serialize_tl_object(obj, true));

nonce_.clear();

processed = true;
return td::Status::OK();
}
}
return td::Status::OK();
}

void AdnlOutboundConnection::start_up() {
AdnlExtConnection::start_up();
auto X = dst_.pubkey().create_encryptor();
if (X.is_error()) {
LOG(ERROR) << "failed to init encryptor: " << X.move_as_error();
stop();
return;
}
auto enc = X.move_as_ok();

td::BufferSlice d{256};
auto id = dst_.compute_short_id();
auto S = d.as_slice();
S.copy_from(id.as_slice());
S.remove_prefix(32);
S.truncate(256 - 64 - 32);
td::Random::secure_bytes(S);
init_crypto(S);

auto R = enc->encrypt(S);
if (R.is_error()) {
LOG(ERROR) << "failed to encrypt: " << R.move_as_error();
stop();
return;
}
auto data = R.move_as_ok();
LOG_CHECK(data.size() == 256 - 32) << "size=" << data.size();
S = d.as_slice();
S.remove_prefix(32);
CHECK(S.size() == data.size());
S.copy_from(data.as_slice());

send_uninit(std::move(d));

if (!local_id_.empty()) {
nonce_ = td::SecureString{32};
td::Random::secure_bytes(nonce_.as_mutable_slice());
auto obj = create_tl_object<ton_api::tcp_authentificate>(td::BufferSlice{nonce_.as_slice()});
send(serialize_tl_object(obj, true));
}
}

Expand All @@ -51,6 +128,13 @@ td::actor::ActorOwn<AdnlExtClient> AdnlExtClient::create(AdnlNodeIdFull dst, td:
return td::actor::create_actor<AdnlExtClientImpl>("extclient", std::move(dst), dst_addr, std::move(callback));
}

td::actor::ActorOwn<AdnlExtClient> AdnlExtClient::create(AdnlNodeIdFull dst, PrivateKey local_id,
td::IPAddress dst_addr,
std::unique_ptr<AdnlExtClient::Callback> callback) {
return td::actor::create_actor<AdnlExtClientImpl>("extclient", std::move(dst), std::move(local_id), dst_addr,
std::move(callback));
}

td::Status AdnlOutboundConnection::process_packet(td::BufferSlice data) {
TRY_RESULT(F, fetch_tl_object<lite_api::adnl_message_answer>(std::move(data), true));
td::actor::send_closure(ext_client_, &AdnlExtClientImpl::answer_query, F->query_id_, std::move(F->answer_));
Expand Down
2 changes: 2 additions & 0 deletions ton-test-liteclient-full/lite-client/adnl/adnl-ext-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class AdnlExtClient : public td::actor::Actor {
td::Promise<td::BufferSlice> promise) = 0;
static td::actor::ActorOwn<AdnlExtClient> create(AdnlNodeIdFull dst, td::IPAddress dst_addr,
std::unique_ptr<AdnlExtClient::Callback> callback);
static td::actor::ActorOwn<AdnlExtClient> create(AdnlNodeIdFull dst, PrivateKey local_id, td::IPAddress dst_addr,
std::unique_ptr<AdnlExtClient::Callback> callback);
};

} // namespace adnl
Expand Down
50 changes: 16 additions & 34 deletions ton-test-liteclient-full/lite-client/adnl/adnl-ext-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,39 @@ class AdnlExtClientImpl;
class AdnlOutboundConnection : public AdnlExtConnection {
private:
AdnlNodeIdFull dst_;
PrivateKey local_id_;
td::actor::ActorId<AdnlExtClientImpl> ext_client_;
td::SecureString nonce_;

public:
AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr<AdnlExtConnection::Callback> callback, AdnlNodeIdFull dst,
td::actor::ActorId<AdnlExtClientImpl> ext_client)
: AdnlExtConnection(std::move(fd), std::move(callback), true), dst_(std::move(dst)), ext_client_(ext_client) {
}
AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr<AdnlExtConnection::Callback> callback, AdnlNodeIdFull dst,
PrivateKey local_id, td::actor::ActorId<AdnlExtClientImpl> ext_client)
: AdnlExtConnection(std::move(fd), std::move(callback), true)
, dst_(std::move(dst))
, local_id_(local_id)
, ext_client_(ext_client) {
}
td::Status process_packet(td::BufferSlice data) override;
td::Status process_init_packet(td::BufferSlice data) override {
UNREACHABLE();
}
void start_up() override {
AdnlExtConnection::start_up();
auto X = dst_.pubkey().create_encryptor();
if (X.is_error()) {
LOG(ERROR) << "failed to init encryptor: " << X.move_as_error();
stop();
return;
}
auto enc = X.move_as_ok();

td::BufferSlice d{256};
auto id = dst_.compute_short_id();
auto S = d.as_slice();
S.copy_from(id.as_slice());
S.remove_prefix(32);
S.truncate(256 - 64 - 32);
td::Random::secure_bytes(S);
init_crypto(S);

auto R = enc->encrypt(S);
if (R.is_error()) {
LOG(ERROR) << "failed to encrypt: " << R.move_as_error();
stop();
return;
}
auto data = R.move_as_ok();
LOG_CHECK(data.size() == 256 - 32) << "size=" << data.size();
S = d.as_slice();
S.remove_prefix(32);
CHECK(S.size() == data.size());
S.copy_from(data.as_slice());

send_uninit(std::move(d));
}
td::Status process_custom_packet(td::BufferSlice &data, bool &processed) override;
void start_up() override;
};

class AdnlExtClientImpl : public AdnlExtClient {
public:
AdnlExtClientImpl(AdnlNodeIdFull dst_id, td::IPAddress dst_addr, std::unique_ptr<Callback> callback)
: dst_(std::move(dst_id)), dst_addr_(dst_addr), callback_(std::move(callback)) {
}
AdnlExtClientImpl(AdnlNodeIdFull dst_id, PrivateKey local_id, td::IPAddress dst_addr,
std::unique_ptr<Callback> callback)
: dst_(std::move(dst_id)), local_id_(local_id), dst_addr_(dst_addr), callback_(std::move(callback)) {
}

void start_up() override {
alarm_timestamp() = next_create_at_;
Expand Down Expand Up @@ -120,6 +101,7 @@ class AdnlExtClientImpl : public AdnlExtClient {

private:
AdnlNodeIdFull dst_;
PrivateKey local_id_;
td::IPAddress dst_addr_;

std::unique_ptr<Callback> callback_;
Expand Down
19 changes: 5 additions & 14 deletions ton-test-liteclient-full/lite-client/adnl/adnl-ext-connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,11 @@ td::Status AdnlExtConnection::receive_packet(td::BufferSlice data) {
// keepalive
return td::Status::OK();
}
if (data.size() == 12) {
auto F = fetch_tl_object<ton_api::tcp_ping>(data.clone(), true);
if (F.is_ok()) {
auto f = F.move_as_ok();
auto obj = create_tl_object<ton_api::tcp_pong>(f->random_id_);
send(serialize_tl_object(obj, true));
return td::Status::OK();
}
}
if (data.size() == 12) {
auto F = fetch_tl_object<ton_api::tcp_pong>(data.clone(), true);
if (F.is_ok()) {
return td::Status::OK();
}

bool processed = false;
TRY_STATUS(process_custom_packet(data, processed));
if (processed) {
return td::Status::OK();
}

return process_packet(std::move(data));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AdnlExtConnection : public td::actor::Actor, public td::ObserverBase {
td::Status receive(td::ChainBufferReader &input, bool &exit_loop);
virtual td::Status process_packet(td::BufferSlice data) = 0;
td::Status receive_packet(td::BufferSlice data);
virtual td::Status process_custom_packet(td::BufferSlice &data, bool &processed) = 0;
virtual td::Status process_init_packet(td::BufferSlice data) = 0;
td::Status init_crypto(td::Slice data);
void stop_read() {
Expand Down
39 changes: 35 additions & 4 deletions ton-test-liteclient-full/lite-client/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endif()
set (TON_CRYPTO_SOURCE
Ed25519.cpp
common/bigint.cpp
common/refcnt.cpp
common/refint.cpp
common/bitstring.cpp
common/util.cpp
Expand Down Expand Up @@ -143,6 +144,30 @@ set (FIFT_SOURCE
fift/words.h
)

set (PARSER_SOURCE
parser/srcread.cpp
parser/lexer.cpp
parser/symtable.cpp

parser/srcread.h
parser/lexer.h
parser/symtable.h
)

set (FUNC_LIB_SOURCE
func/keywords.cpp
func/unify-types.cpp
func/parse-func.cpp
func/abscode.cpp
func/gen-abscode.cpp
func/analyzer.cpp
func/asmops.cpp
func/builtins.cpp
func/stack-transform.cpp
func/optimize.cpp
func/codegen.cpp
)

set(TLB_BLOCK_AUTO
${CMAKE_CURRENT_SOURCE_DIR}/block/block-auto.cpp
${CMAKE_CURRENT_SOURCE_DIR}/block/block-auto.h
Expand All @@ -153,6 +178,7 @@ set (BLOCK_SOURCE
block/Binlog.cpp
block/block.cpp
block/block-db.cpp
block/block-parse.cpp
block/check-proof.cpp
block/mc-config.cpp
block/output-queue-merger.cpp
Expand All @@ -163,6 +189,7 @@ set (BLOCK_SOURCE
block/block-db-impl.h
block/block-db.h
block/block.h
block/block-parse.h
block/check-proof.h
block/output-queue-merger.h
block/transaction.h
Expand Down Expand Up @@ -222,16 +249,20 @@ if (WINGETOPT_FOUND)
target_link_libraries_system(fift wingetopt)
endif()

add_executable(func func/func.cpp)
add_library(src_parser ${PARSER_SOURCE})
target_include_directories(src_parser PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(src_parser PUBLIC ton_crypto)

add_executable(func func/func.cpp ${FUNC_LIB_SOURCE})
target_include_directories(func PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(func PUBLIC ton_crypto)
target_link_libraries(func PUBLIC ton_crypto src_parser)
if (WINGETOPT_FOUND)
target_link_libraries_system(func wingetopt)
endif()

add_executable(tlbc tl/tlbc.cpp)
target_include_directories(tlbc PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/func>)
target_link_libraries(tlbc PUBLIC ton_crypto)
target_include_directories(tlbc PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(tlbc PUBLIC ton_crypto src_parser)
if (WINGETOPT_FOUND)
target_link_libraries_system(tlbc wingetopt)
endif()
Expand Down
Loading

0 comments on commit 5323b3b

Please sign in to comment.