Skip to content

Commit

Permalink
updated tonlib
Browse files Browse the repository at this point in the history
1. updated tonlib
2. fixed bug in state download
  • Loading branch information
ton committed Sep 22, 2019
1 parent f40822b commit 28df741
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 115 deletions.
3 changes: 3 additions & 0 deletions crypto/block/check-proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ td::Result<Transaction::Info> Transaction::validate() {
}
Info res;
res.blkid = blkid;
res.now = trans.now;
res.prev_trans_lt = trans.prev_trans_lt;
res.prev_trans_hash = trans.prev_trans_hash;
res.transaction = root;
Expand All @@ -281,6 +282,8 @@ td::Result<TransactionList::Info> TransactionList::validate() const {
Info res;
auto current_lt = lt;
auto current_hash = hash;
res.lt = lt;
res.hash = hash;
for (auto& root : list) {
const auto& blkid = blkids[c++];
Transaction transaction;
Expand Down
14 changes: 3 additions & 11 deletions crypto/block/check-proof.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct Transaction {

struct Info {
ton::BlockIdExt blkid;
td::uint32 now;
ton::LogicalTime prev_trans_lt;
ton::Bits256 prev_trans_hash;
td::Ref<vm::Cell> transaction;
Expand All @@ -74,21 +75,12 @@ struct TransactionList {
td::BufferSlice transactions_boc;

struct Info {
ton::LogicalTime lt;
ton::Bits256 hash;
std::vector<Transaction::Info> transactions;
};

td::Result<Info> validate() const;
};

struct BlockChain {
ton::BlockIdExt from;
ton::BlockIdExt to;
td::int32 mode;
td::BufferSlice proof;

using Info = std::unique_ptr<block::BlockProofChain>;

td::Result<Info> validate() const;
};

} // namespace block
15 changes: 9 additions & 6 deletions tdutils/td/utils/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,20 @@ SecureString create_empty<SecureString>(size_t size) {
template <class T>
Result<T> read_file_impl(CSlice path, int64 size, int64 offset) {
TRY_RESULT(from_file, FileFd::open(path, FileFd::Read));
TRY_RESULT(file_size, from_file.get_size());
if (offset < 0 || offset > file_size) {
return Status::Error("Failed to read file: invalid offset");
}
if (size == -1) {
TRY_RESULT(file_size, from_file.get_size());
size = file_size;
size = file_size - offset;
} else if (size >= 0) {
if (size + offset > file_size) {
size = file_size - offset;
}
}
if (size < 0) {
return Status::Error("Failed to read file: invalid size");
}
if (offset < 0 || offset > size) {
return Status::Error("Failed to read file: invalid offset");
}
size -= offset;
auto content = create_empty<T>(narrow_cast<size_t>(size));
TRY_RESULT(got_size, from_file.pread(as_mutable_slice(content), offset));
if (got_size != static_cast<size_t>(size)) {
Expand Down
2 changes: 1 addition & 1 deletion third-party/wingetopt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ cmake_minimum_required(VERSION 2.8)

add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_library(wingetopt src/getopt.c src/getopt.h)
target_include_directories(wingetopt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(wingetopt PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>)

20 changes: 12 additions & 8 deletions tl/generate/scheme/tonlib_api.tl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ vector {t:Type} # [ t ] = Vector t;
error code:int32 message:string = Error;
ok = Ok;

options config:string keystore_directory:string = Options;
options config:string keystore_directory:string use_callbacks_for_network:Bool = Options;

key public_key:string secret:secureBytes = Key;
inputKey key:key local_password:secureBytes = InputKey;
Expand All @@ -31,17 +31,17 @@ accountAddress account_address:string = AccountAddress;
internal.transactionId lt:int64 hash:bytes = internal.TransactionId;

raw.initialAccountState code:bytes data:bytes = raw.InitialAccountState;
raw.accountState balance:int64 code:bytes data:bytes last_transaction_id:internal.transactionId = raw.AccountState;
raw.accountState balance:int64 code:bytes data:bytes last_transaction_id:internal.transactionId sync_utime:int53 = raw.AccountState;
raw.message source:string destination:string value:int64 = raw.Message;
raw.transaction data:bytes previous_transaction_id:internal.transactionId fee:int64 in_msg:raw.message out_msgs:vector<raw.message> = raw.Transaction;
raw.transactions transactions:vector<raw.Transaction> = raw.Transactions;
raw.transaction utime:int53 data:bytes transaction_id:internal.transactionId fee:int64 in_msg:raw.message out_msgs:vector<raw.message> = raw.Transaction;
raw.transactions transactions:vector<raw.Transaction> previous_transaction_id:internal.transactionId = raw.Transactions;

testWallet.initialAccountState public_key:string = testWallet.InitialAccountState;
testWallet.accountState balance:int64 seqno:int32 last_transaction_id:internal.transactionId = testWallet.AccountState;
testWallet.accountState balance:int64 seqno:int32 last_transaction_id:internal.transactionId sync_utime:int53 = testWallet.AccountState;

testGiver.accountState balance:int64 seqno:int32 last_transaction_id:internal.transactionId = testGiver.AccountState;
testGiver.accountState balance:int64 seqno:int32 last_transaction_id:internal.transactionId sync_utime:int53= testGiver.AccountState;

uninited.accountState balance:int64 = uninited.AccountState;
uninited.accountState balance:int64 last_transaction_id:internal.transactionId sync_utime:int53 = uninited.AccountState;

generic.initialAccountStateRaw initital_account_state:raw.initialAccountState = generic.InitialAccountState;
generic.initialAccountStateTestWallet initital_account_state:testWallet.initialAccountState = generic.InitialAccountState;
Expand All @@ -51,6 +51,8 @@ generic.accountStateTestWallet account_state:testWallet.accountState = generic.A
generic.accountStateTestGiver account_state:testGiver.accountState = generic.AccountState;
generic.accountStateUninited account_state:uninited.accountState = generic.AccountState;

updateSendLiteServerQuery id:int64 data:bytes = Update;

---functions---

init options:options = Ok;
Expand Down Expand Up @@ -89,5 +91,7 @@ testGiver.sendGrams destination:accountAddress seqno:int32 amount:int64 = Ok;
generic.getAccountState account_address:accountAddress = generic.AccountState;
generic.sendGrams private_key:inputKey source:accountAddress destination:accountAddress amount:int64 = Ok;

runTests dir:string = Ok;
onLiteServerQueryResult id:int64 bytes:bytes = Ok;
onLiteServerQueryError id:int64 error:error = Ok;

runTests dir:string = Ok;
Binary file modified tl/generate/scheme/tonlib_api.tlo
Binary file not shown.
2 changes: 2 additions & 0 deletions tonlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(TONLIB_SOURCE
tonlib/Config.cpp
tonlib/ExtClient.cpp
tonlib/ExtClientLazy.cpp
tonlib/ExtClientOutbound.cpp
tonlib/GenericAccount.cpp
tonlib/KeyStorage.cpp
tonlib/LastBlock.cpp
Expand All @@ -21,6 +22,7 @@ set(TONLIB_SOURCE
tonlib/Config.h
tonlib/ExtClient.h
tonlib/ExtClientLazy.h
tonlib/ExtClientOutbound.h
tonlib/GenericAccount.h
tonlib/KeyStorage.h
tonlib/LastBlock.h
Expand Down
15 changes: 8 additions & 7 deletions tonlib/test/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,17 @@ TEST(Tonlib, InitClose) {
{
Client client;
sync_send(client, make_object<tonlib_api::close>()).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "."))).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", ".", false))).ensure_error();
}
{
Client client;
sync_send(client, make_object<tonlib_api::init>(nullptr)).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("fdajkfldsjkafld", ".")))
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("fdajkfldsjkafld", ".", false)))
.ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "fdhskfds"))).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "."))).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "."))).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "fdhskfds", false)))
.ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", ".", false))).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", ".", false))).ensure_error();

td::Slice bad_config = R"abc(
{
Expand All @@ -194,7 +195,7 @@ TEST(Tonlib, InitClose) {
sync_send(client, make_object<tonlib_api::testGiver_getAccountState>()).ensure_error();
sync_send(client, make_object<tonlib_api::close>()).ensure();
sync_send(client, make_object<tonlib_api::close>()).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "."))).ensure_error();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", ".", false))).ensure_error();
}
}

Expand Down Expand Up @@ -289,7 +290,7 @@ TEST(Tonlib, KeysApi) {
Client client;

// init
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", "."))).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>("", ".", false))).ensure();
auto local_password = td::SecureString("local password");
auto mnemonic_password = td::SecureString("mnemonic password");
{
Expand Down
10 changes: 6 additions & 4 deletions tonlib/test/online.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ void dump_transaction_history(Client& client, std::string address) {
make_object<tonlib_api::accountAddress>(address), std::move(tid)))
.move_as_ok();
CHECK(got_transactions->transactions_.size() > 0);
CHECK(got_transactions->transactions_[0]->previous_transaction_id_->lt_ < lt);
CHECK(got_transactions->previous_transaction_id_->lt_ < lt);
for (auto& txn : got_transactions->transactions_) {
LOG(ERROR) << to_string(txn);
cnt++;
}
tid = std::move(got_transactions->transactions_.back()->previous_transaction_id_);
tid = std::move(got_transactions->previous_transaction_id_);
}
LOG(ERROR) << cnt;
}
Expand All @@ -196,7 +196,8 @@ int main(int argc, char* argv[]) {

Client client;
{
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>(global_config_str, "."))).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>(global_config_str, ".", false)))
.ensure();
}
//dump_transaction_history(client, get_test_giver_address(client));
auto wallet_a = create_wallet(client);
Expand All @@ -208,7 +209,8 @@ int main(int argc, char* argv[]) {
return 0;
{
// init
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>(global_config_str, "."))).ensure();
sync_send(client, make_object<tonlib_api::init>(make_object<tonlib_api::options>(global_config_str, ".", false)))
.ensure();

auto key = sync_send(client, make_object<tonlib_api::createNewKey>(
td::SecureString("local"), td::SecureString("mnemonic"), td::SecureString()))
Expand Down
6 changes: 3 additions & 3 deletions tonlib/tonlib/ExtClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#include "tonlib/LastBlock.h"

namespace tonlib {
void ExtClient::with_last_block(td::Promise<ton::BlockIdExt> promise) {
void ExtClient::with_last_block(td::Promise<LastBlockInfo> promise) {
auto query_id = last_block_queries_.create(std::move(promise));
td::Promise<ton::BlockIdExt> P = [query_id, self = this,
actor_id = td::actor::actor_id()](td::Result<ton::BlockIdExt> result) {
td::Promise<LastBlockInfo> P = [query_id, self = this,
actor_id = td::actor::actor_id()](td::Result<LastBlockInfo> result) {
send_lambda(actor_id, [self, query_id, result = std::move(result)]() mutable {
self->last_block_queries_.extract(query_id).set_result(std::move(result));
});
Expand Down
5 changes: 3 additions & 2 deletions tonlib/tonlib/ExtClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace tonlib {
class LastBlock;
struct LastBlockInfo;
struct ExtClientRef {
td::actor::ActorId<ton::adnl::AdnlExtClient> andl_ext_client_;
td::actor::ActorId<LastBlock> last_block_actor_;
Expand All @@ -49,7 +50,7 @@ class ExtClient {
return client_;
}

void with_last_block(td::Promise<ton::BlockIdExt> promise);
void with_last_block(td::Promise<LastBlockInfo> promise);

template <class QueryT>
void send_query(QueryT query, td::Promise<typename QueryT::ReturnType> promise) {
Expand All @@ -74,7 +75,7 @@ class ExtClient {
private:
ExtClientRef client_;
td::Container<td::Promise<td::BufferSlice>> queries_;
td::Container<td::Promise<ton::BlockIdExt>> last_block_queries_;
td::Container<td::Promise<LastBlockInfo>> last_block_queries_;

void send_raw_query(td::BufferSlice query, td::Promise<td::BufferSlice> promise);
};
Expand Down
Loading

0 comments on commit 28df741

Please sign in to comment.