From bce33f588afe63aae8fedf1163fbe3993e128400 Mon Sep 17 00:00:00 2001 From: ton Date: Mon, 16 Sep 2019 12:06:04 +0400 Subject: [PATCH] updated smartcontract code updated lite-client and configuration smartcontract updated tonlib code --- crypto/block/block.cpp | 55 +++- crypto/block/block.h | 21 +- crypto/block/block.tlb | 4 + crypto/block/check-proof.cpp | 13 +- crypto/block/transaction.cpp | 2 +- crypto/block/transaction.h | 7 +- crypto/fift/words.cpp | 24 ++ crypto/smartcont/CreateState.fif | 14 +- crypto/smartcont/gen-zerostate.fif | 6 +- crypto/smartcont/new-pinger.fif | 2 +- crypto/smartcont/new-wallet.fif | 7 +- crypto/smartcont/recover-stake.fif | 2 +- crypto/smartcont/show-addr.fif | 2 +- crypto/smartcont/testgiver.fif | 20 +- crypto/smartcont/update-config-smc.fif | 2 +- crypto/smartcont/update-config.fif | 2 +- crypto/smartcont/update-elector-smc.fif | 2 +- crypto/smartcont/validator-elect-req.fif | 2 +- crypto/smartcont/validator-elect-signed.fif | 2 +- crypto/smartcont/wallet.fif | 18 +- doc/LiteClient-HOWTO | 304 ++++++++++++-------- lite-client-docs/README | 5 +- lite-client/lite-client.cpp | 177 ++++++++---- lite-client/lite-client.h | 9 +- tdutils/td/utils/OptionsParser.cpp | 5 + tdutils/td/utils/Random.cpp | 9 + tdutils/td/utils/Random.h | 1 + tdutils/td/utils/misc.h | 7 +- tl/generate/scheme/lite_api.tl | 2 + tl/generate/scheme/lite_api.tlo | Bin 10364 -> 10884 bytes tl/generate/scheme/tonlib_api.tl | 6 +- tl/generate/scheme/tonlib_api.tlo | Bin 8420 -> 8696 bytes tonlib/test/offline.cpp | 12 +- tonlib/test/online.cpp | 10 +- tonlib/tonlib/KeyStorage.cpp | 4 +- tonlib/tonlib/KeyStorage.h | 2 +- tonlib/tonlib/TonlibClient.cpp | 11 +- tonlib/tonlib/TonlibClient.h | 1 + tonlib/tonlib/keys/Mnemonic.cpp | 42 ++- tonlib/tonlib/keys/Mnemonic.h | 2 + tonlib/tonlib/keys/bip39.h | 3 +- tonlib/tonlib/tonlib-cli.cpp | 31 +- validator/impl/collator.cpp | 27 +- validator/impl/liteserver.cpp | 28 +- validator/impl/liteserver.hpp | 11 +- validator/impl/validate-query.cpp | 60 ++-- 46 files changed, 677 insertions(+), 299 deletions(-) diff --git a/crypto/block/block.cpp b/crypto/block/block.cpp index ed19b469..8b0ff8b3 100644 --- a/crypto/block/block.cpp +++ b/crypto/block/block.cpp @@ -24,10 +24,61 @@ #include "common/util.h" #include "td/utils/crypto.h" #include "td/utils/tl_storers.h" +#include "td/utils/misc.h" namespace block { using namespace std::literals::string_literals; +td::Result PublicKey::from_bytes(td::Slice key) { + if (key.size() != 32) { + return td::Status::Error("Ed25519 public key must be exactly 32 bytes long"); + } + PublicKey res; + res.key = key.str(); + return res; +} + +td::Result PublicKey::parse(td::Slice key) { + if (key.size() != 48) { + return td::Status::Error("Serialized Ed25519 public key must be exactly 48 characters long"); + } + td::uint8 buf[36]; + if (!buff_base64_decode(td::MutableSlice(buf, 36), key, true)) { + return td::Status::Error("Public key is not serialized in base64 encoding"); + } + + td::uint16 hash = static_cast((static_cast(buf[34]) << 8) + buf[35]); + if (hash != td::crc16(td::Slice(buf, 34))) { + return td::Status::Error("Public key has incorrect crc16 hash"); + } + + if (buf[0] != 0x3e) { + return td::Status::Error("Not a public key"); + } + if (buf[1] != 0xe6) { + return td::Status::Error("Not an ed25519 public key"); + } + + return from_bytes(td::Slice(buf + 2, 32)); +} + +std::string PublicKey::serialize(bool base64_url) { + CHECK(key.size() == 32); + std::string buf(36, 0); + td::MutableSlice bytes(buf); + + bytes[0] = static_cast(0x3e); + bytes[1] = static_cast(0xe6); + bytes.substr(2).copy_from(key); + auto hash = td::crc16(bytes.substr(0, 34)); + bytes[34] = static_cast(hash >> 8); + bytes[35] = static_cast(hash & 255); + + std::string res(48, 0); + buff_base64_encode(res, bytes, base64_url); + return res; +} + bool pack_std_smc_addr_to(char result[48], bool base64_url, ton::WorkchainId wc, const ton::StdSmcAddress& addr, bool bounceable, bool testnet) { if (wc < -128 || wc >= 128) { @@ -316,14 +367,14 @@ std::unique_ptr MsgProcessedUptoCollection::unpack(t return v && v->valid ? std::move(v) : std::unique_ptr{}; } -bool MsgProcessedUpto::contains(const MsgProcessedUpto& other) const & { +bool MsgProcessedUpto::contains(const MsgProcessedUpto& other) const& { return ton::shard_is_ancestor(shard, other.shard) && mc_seqno >= other.mc_seqno && (last_inmsg_lt > other.last_inmsg_lt || (last_inmsg_lt == other.last_inmsg_lt && !(last_inmsg_hash < other.last_inmsg_hash))); } bool MsgProcessedUpto::contains(ton::ShardId other_shard, ton::LogicalTime other_lt, td::ConstBitPtr other_hash, - ton::BlockSeqno other_mc_seqno) const & { + ton::BlockSeqno other_mc_seqno) const& { return ton::shard_is_ancestor(shard, other_shard) && mc_seqno >= other_mc_seqno && (last_inmsg_lt > other_lt || (last_inmsg_lt == other_lt && !(last_inmsg_hash < other_hash))); } diff --git a/crypto/block/block.h b/crypto/block/block.h index 15ae2f7b..8b5455af 100644 --- a/crypto/block/block.h +++ b/crypto/block/block.h @@ -33,6 +33,16 @@ namespace block { using td::Ref; +struct PublicKey { + std::string key; + + static td::Result from_bytes(td::Slice key); + + static td::Result parse(td::Slice key); + + std::string serialize(bool base64_url = false); +}; + struct StdAddress { ton::WorkchainId workchain{ton::workchainInvalid}; bool bounceable{true}; // addresses must be bounceable by default @@ -149,12 +159,12 @@ struct MsgProcessedUpto { MsgProcessedUpto(ton::ShardId _shard, ton::BlockSeqno _mcseqno, ton::LogicalTime _lt, td::ConstBitPtr _hash) : shard(_shard), mc_seqno(_mcseqno), last_inmsg_lt(_lt), last_inmsg_hash(_hash) { } - bool operator<(const MsgProcessedUpto& other) const & { + bool operator<(const MsgProcessedUpto& other) const& { return shard < other.shard || (shard == other.shard && mc_seqno < other.mc_seqno); } - bool contains(const MsgProcessedUpto& other) const &; + bool contains(const MsgProcessedUpto& other) const&; bool contains(ton::ShardId other_shard, ton::LogicalTime other_lt, td::ConstBitPtr other_hash, - ton::BlockSeqno other_mc_seqno) const &; + ton::BlockSeqno other_mc_seqno) const&; // NB: this is for checking whether we have already imported an internal message bool already_processed(const EnqueuedMsgDescr& msg) const; }; @@ -470,13 +480,14 @@ struct BlockProofLink { bool incomplete() const { return dest_proof.is_null(); } - td::Status validate() const; + td::Status validate(td::uint32* save_utime = nullptr) const; }; struct BlockProofChain { ton::BlockIdExt from, to; int mode; - bool complete{false}, has_key_block{false}, valid{false}; + td::uint32 last_utime{0}; + bool complete{false}, has_key_block{false}, has_utime{false}, valid{false}; ton::BlockIdExt key_blkid; std::vector links; std::size_t link_count() const { diff --git a/crypto/block/block.tlb b/crypto/block/block.tlb index bac766be..0ec2b4c2 100644 --- a/crypto/block/block.tlb +++ b/crypto/block/block.tlb @@ -599,6 +599,10 @@ gas_prices#dd gas_price:uint64 gas_limit:uint64 gas_credit:uint64 block_gas_limit:uint64 freeze_due_limit:uint64 delete_due_limit:uint64 = GasLimitsPrices; +gas_prices_ext#de gas_price:uint64 gas_limit:uint64 special_gas_limit:uint64 gas_credit:uint64 + block_gas_limit:uint64 freeze_due_limit:uint64 delete_due_limit:uint64 + = GasLimitsPrices; + config_mc_gas_prices#_ GasLimitsPrices = ConfigParam 20; config_gas_prices#_ GasLimitsPrices = ConfigParam 21; diff --git a/crypto/block/check-proof.cpp b/crypto/block/check-proof.cpp index f0d0d377..e8316f5d 100644 --- a/crypto/block/check-proof.cpp +++ b/crypto/block/check-proof.cpp @@ -296,7 +296,10 @@ td::Result TransactionList::validate() const { return std::move(res); } -td::Status BlockProofLink::validate() const { +td::Status BlockProofLink::validate(td::uint32* save_utime) const { + if (save_utime) { + *save_utime = 0; + } if (!(from.is_masterchain_ext() && to.is_masterchain_ext())) { return td::Status::Error("BlockProofLink must have both source and destination blocks in the masterchain"); } @@ -346,6 +349,9 @@ td::Status BlockProofLink::validate() const { return td::Status::Error(PSTRING() << "incorrect is_key_block value " << is_key << " for destination block " << to.to_str()); } + if (save_utime) { + *save_utime = info.gen_utime; + } } else if (!is_key) { // return td::Status::Error("Zerostate destination block "s + to.to_str() + " does not have is_key_block set"); } @@ -414,6 +420,8 @@ td::Status BlockProofLink::validate() const { td::Status BlockProofChain::validate() { valid = false; has_key_block = false; + has_utime = false; + last_utime = 0; key_blkid.invalidate(); if (!(from.is_masterchain_ext() && to.is_masterchain_ext())) { return td::Status::Error("BlockProofChain must have both source and destination blocks in the masterchain"); @@ -435,7 +443,7 @@ td::Status BlockProofChain::validate() { << link.from.to_str() << " but the previous link ends at different block " << cur.to_str()); } - auto err = link.validate(); + auto err = link.validate(&last_utime); if (err.is_error()) { return td::Status::Error(PSTRING() << "link #" << i << " in BlockProofChain is invalid: " << err.to_string()); } @@ -449,6 +457,7 @@ td::Status BlockProofChain::validate() { return td::Status::Error("last link of BlockProofChain ends at block "s + cur.to_str() + " different from declared chain destination block " + to.to_str()); } + has_utime = (last_utime > 0); valid = true; return td::Status::OK(); } diff --git a/crypto/block/transaction.cpp b/crypto/block/transaction.cpp index 9175ef27..77f3ad07 100644 --- a/crypto/block/transaction.cpp +++ b/crypto/block/transaction.cpp @@ -695,7 +695,7 @@ td::RefInt256 ComputePhaseConfig::compute_gas_price(td::uint64 gas_used) const { bool Transaction::compute_gas_limits(ComputePhase& cp, const ComputePhaseConfig& cfg) { // Compute gas limits if (account.is_special) { - cp.gas_max = cfg.gas_limit; // TODO: introduce special gas limits? + cp.gas_max = cfg.special_gas_limit; } else { cp.gas_max = cfg.gas_bought_for(balance.grams); } diff --git a/crypto/block/transaction.h b/crypto/block/transaction.h index c06f1525..94b2a853 100644 --- a/crypto/block/transaction.h +++ b/crypto/block/transaction.h @@ -96,6 +96,7 @@ struct StoragePhase { struct ComputePhaseConfig { td::uint64 gas_price; td::uint64 gas_limit; + td::uint64 special_gas_limit; td::uint64 gas_credit; static constexpr td::uint64 gas_infty = (1ULL << 63) - 1; td::RefInt256 gas_price256; @@ -104,7 +105,11 @@ struct ComputePhaseConfig { Ref global_config; td::BitArray<256> block_rand_seed; ComputePhaseConfig(td::uint64 _gas_price = 0, td::uint64 _gas_limit = 0, td::uint64 _gas_credit = 0) - : gas_price(_gas_price), gas_limit(_gas_limit), gas_credit(_gas_credit) { + : gas_price(_gas_price), gas_limit(_gas_limit), special_gas_limit(_gas_limit), gas_credit(_gas_credit) { + compute_threshold(); + } + ComputePhaseConfig(td::uint64 _gas_price, td::uint64 _gas_limit, td::uint64 _spec_gas_limit, td::uint64 _gas_credit) + : gas_price(_gas_price), gas_limit(_gas_limit), special_gas_limit(_spec_gas_limit), gas_credit(_gas_credit) { compute_threshold(); } void compute_threshold(); diff --git a/crypto/fift/words.cpp b/crypto/fift/words.cpp index 775f4ba0..defb1bdf 100644 --- a/crypto/fift/words.cpp +++ b/crypto/fift/words.cpp @@ -50,6 +50,7 @@ #include "td/utils/port/Stat.h" #include "td/utils/Timer.h" #include "td/utils/tl_helpers.h" +#include "td/utils/crypto.h" #include @@ -704,6 +705,10 @@ void interpret_int_to_bytes(vm::Stack& stack, bool sgnd, bool lsb) { stack.push_bytes(std::string{(char*)buffer, sz}); } +void interpret_string_to_bytes(vm::Stack& stack) { + stack.push_bytes(stack.pop_string()); +} + void interpret_bytes_hash(vm::Stack& stack) { std::string str = stack.pop_bytes(); unsigned char buffer[32]; @@ -1306,6 +1311,21 @@ void interpret_ed25519_chksign(vm::Stack& stack) { stack.push_bool(res.is_ok()); } +void interpret_crc16(vm::Stack& stack) { + std::string str = stack.pop_bytes(); + stack.push_smallint(td::crc16(td::Slice{str})); +} + +void interpret_crc32(vm::Stack& stack) { + std::string str = stack.pop_bytes(); + stack.push_smallint(td::crc32(td::Slice{str})); +} + +void interpret_crc32c(vm::Stack& stack) { + std::string str = stack.pop_bytes(); + stack.push_smallint(td::crc32c(td::Slice{str})); +} + // vm dictionaries void interpret_dict_new(vm::Stack& stack) { stack.push({}); @@ -2427,6 +2447,7 @@ void init_words_common(Dictionary& d) { d.def_stack_word("B>Li@ ", std::bind(interpret_bytes_fetch_int, _1, 0x11)); d.def_stack_word("B>Lu@+ ", std::bind(interpret_bytes_fetch_int, _1, 0x12)); d.def_stack_word("B>Li@+ ", std::bind(interpret_bytes_fetch_int, _1, 0x13)); + d.def_stack_word("$>B ", interpret_string_to_bytes); d.def_stack_word("Bhash ", interpret_bytes_hash); // cell manipulation (create, write and modify cells) d.def_stack_word("s ", interpret_dict_to_slice); diff --git a/crypto/smartcont/CreateState.fif b/crypto/smartcont/CreateState.fif index a2711475..4a2b571a 100644 --- a/crypto/smartcont/CreateState.fif +++ b/crypto/smartcont/CreateState.fif @@ -96,8 +96,8 @@ dictnew constant special-dict 1 'nop } ::_ sg~ -// gas_price gas_limit gas_credit block_gas_limit freeze_due_limit delete_due_limit -- c -{ 6 0 reverse +// gas_price gas_limit spec_limit gas_credit block_gas_limit freeze_due_limit delete_due_limit -- c +{ 7 0 reverse } : make-gas-prices { make-gas-prices 20 config! } : config.mc_gas_prices! { make-gas-prices 21 config! } : config.gas_prices! @@ -136,7 +136,6 @@ dictnew constant special-dict { 7 config! } : config.to_mint! - 1000000000 constant Gram 1000000 constant mGram 1000 constant uGram @@ -149,6 +148,7 @@ dictnew constant special-dict { mGram swap */r } : mGram*/ { uGram swap */r } : uGram*/ { /r } : nGram*/ + // GR$.17 is equivalent to 170000000 { bl word (number) ?dup 0= abort"not a valid Gram amount" 1- { Gram swap */r } { Gram * } cond @@ -157,8 +157,16 @@ dictnew constant special-dict { 10 << } : *Ki { 20 << } : *Mi +{ 30 << } : *Gi { 10 <.pk" cr ."('new-wallet.pk' by default)" cr 1 halt } : usage -def? $# { @' $# 1- -2 and ' usage if } if +$# 1- -2 and ' usage if -Basechain constant wc // create a wallet in workchain 0 (basechain) -def? $1 { @' $1 parse-workchain-id =: wc } if // set workchain id from command line argument +$1 parse-workchain-id =: wc // set workchain id from command line argument def? $2 { @' $2 } { "new-wallet" } cond constant file-base ."Creating new wallet in workchain " wc . cr diff --git a/crypto/smartcont/recover-stake.fif b/crypto/smartcont/recover-stake.fif index 871b3855..573cb336 100644 --- a/crypto/smartcont/recover-stake.fif +++ b/crypto/smartcont/recover-stake.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/show-addr.fif b/crypto/smartcont/show-addr.fif index 5d309464..74536528 100644 --- a/crypto/smartcont/show-addr.fif +++ b/crypto/smartcont/show-addr.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." " cr diff --git a/crypto/smartcont/testgiver.fif b/crypto/smartcont/testgiver.fif index 3d717d05..1b90fdb8 100644 --- a/crypto/smartcont/testgiver.fif +++ b/crypto/smartcont/testgiver.fif @@ -1,29 +1,21 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr ."Creates a request to TestGiver and saves it into .boc" cr ."('testgiver-query.boc' by default)" cr 1 halt } : usage -def? $# { @' $# 3 - -2 and ' usage if } if + +$# 3 - -2 and ' usage if // "testgiver.addr" load-address Masterchain 0x8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d 2constant giver_addr ."Test giver address = " giver_addr 2dup .addr cr 6 .Addr cr -Basechain 0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 -2constant dest_addr -false constant bounce - -0 constant seqno -GR$6.666 constant amount - -def? $3 { - @' $1 bounce parse-load-address =: bounce 2=: dest_addr - @' $2 parse-int =: seqno - @' $3 $>GR =: amount -} if +$1 true parse-load-address =: bounce 2=: dest_addr +$2 parse-int =: seqno +$3 $>GR =: amount def? $4 { @' $4 } { "testgiver-query" } cond constant savefile ."Requesting " amount .GR ."to account " diff --git a/crypto/smartcont/update-config-smc.fif b/crypto/smartcont/update-config-smc.fif index 3dd51c58..3840e128 100644 --- a/crypto/smartcont/update-config-smc.fif +++ b/crypto/smartcont/update-config-smc.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/update-config.fif b/crypto/smartcont/update-config.fif index 662dec81..9956a8ce 100644 --- a/crypto/smartcont/update-config.fif +++ b/crypto/smartcont/update-config.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/update-elector-smc.fif b/crypto/smartcont/update-elector-smc.fif index e485729a..df153f60 100644 --- a/crypto/smartcont/update-elector-smc.fif +++ b/crypto/smartcont/update-elector-smc.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/validator-elect-req.fif b/crypto/smartcont/validator-elect-req.fif index 215fe9c1..c2aca4b5 100644 --- a/crypto/smartcont/validator-elect-req.fif +++ b/crypto/smartcont/validator-elect-req.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/validator-elect-signed.fif b/crypto/smartcont/validator-elect-signed.fif index 55e3d9dd..9755bfac 100644 --- a/crypto/smartcont/validator-elect-signed.fif +++ b/crypto/smartcont/validator-elect-signed.fif @@ -1,4 +1,4 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." []" cr diff --git a/crypto/smartcont/wallet.fif b/crypto/smartcont/wallet.fif index 5938eb2e..8742e3cc 100644 --- a/crypto/smartcont/wallet.fif +++ b/crypto/smartcont/wallet.fif @@ -1,26 +1,20 @@ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s "TonUtil.fif" include { ."usage: " @' $0 type ." [-B ] []" cr ."Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file .pk " ."and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" cr 1 halt } : usage +$# dup 4 < swap 5 > or ' usage if def? $6 { @' $5 "-B" $= { @' $6 =: body-boc-file [forget] $6 def? $7 { @' $7 =: $5 [forget] $7 } { [forget] $5 } cond @' $# 2- =: $# } if } if -def? $# { @' $# dup 4 < swap 5 > or ' usage if } if -"new-wallet" constant file-base -Basechain 0x13CB612A00A7C092C7DFD2EA45D603A9B54591BA4C88F71E707E009B879F0FB2 2constant dest_addr -0 constant seqno -GR$.666 constant amount true constant bounce -def? $4 { - @' $1 =: file-base - @' $2 bounce parse-load-address =: bounce 2=: dest_addr - @' $3 parse-int =: seqno - @' $4 $>GR =: amount -} if +$1 =: file-base +$2 bounce parse-load-address =: bounce 2=: dest_addr +$3 parse-int =: seqno +$4 $>GR =: amount def? $5 { @' $5 } { "wallet-query" } cond constant savefile file-base +".addr" load-address diff --git a/doc/LiteClient-HOWTO b/doc/LiteClient-HOWTO index bff3b124..a4aacbb0 100644 --- a/doc/LiteClient-HOWTO +++ b/doc/LiteClient-HOWTO @@ -31,9 +31,18 @@ and Ef+BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb (base64) or Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb (base64url) - in the "user-friendly" form (to be displayed by user-friendly clients). Notice that both forms (base64 and base64url) are valid and must be accepted. +Incidentally, other binary data related to the TON Blockchain have similar "armored" base64 representations, differing by their first bytes. For example, the ubiquitious 256-bit Ed25519 public keys are represented by first creating a 36-byte sequence as follows: +- one tag byte 0x3E, meaning that this is a public key +- one tag byte 0xE6, meaning that this is a Ed25519 public key +- 32 bytes containing the standard binary representation of the Ed25519 public key +- 2 bytes containing the big-endian representation of CRC16-CCITT of the previous 34 bytes. + +The resulting 36-byte sequence is converted into a 48-character base64 or base64url string in the standard fashion. For example, the Ed25519 public key E39ECDA0A7B0C60A7107EC43967829DBE8BC356A49B9DFC6186B3EAC74B5477D (usually represented by a sequence of 32 bytes 0xE3, 0x9E, ..., 0x7D) has the following "armored" representation: + +Pubjns2gp7DGCnEH7EOWeCnb6Lw1akm538YYaz6sdLVHfRB2 + 2. Inspecting the state of a smart contract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -76,7 +85,7 @@ account state is (account data:(just value:(raw@^Cell x{} - x{00000000} + x{00009A15} )) library:hme_empty)))) x{CFF8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D2068086C0000000000000000000000001C0E35FA931A000134_} @@ -92,7 +101,7 @@ Finally, the last several lines beginning with x{CFF538... (the "raw dump") cont We can see that x{FF0020DDA4F260...} is the code of this smart contract. If we consult the Appendix A of the TON Virtual Machine documentation, we can even disassemble this code: FF00 is SETCP 0, 20 is DUP, DD is IFNOTRET, A4 is INC, F260 is THROWIF 32, and so on. (Incidentally, you can find the source code of this smartcontract in the source file crypto/block/mc0.fif .) -We can also see that x{00000000} (the actual value you see may be different) is the persistent data of this smart contract. It is actually an unsigned 32-bit integer, used by the smart contract as the counter of operations performed so far. Notice that this value is big-endian (i.e., 3 is encoded as x{00000003}, not as x{03000000}), as are all integers inside the TON Blockchain. +We can also see that x{00009A15} (the actual value you see may be different) is the persistent data of this smart contract. It is actually an unsigned 32-bit integer, used by the smart contract as the counter of operations performed so far. Notice that this value is big-endian (i.e., 3 is encoded as x{00000003}, not as x{03000000}), as are all integers inside the TON Blockchain. In this case the counter is equal to 0x9A15 = 39445. The current balance of the smart contract is easily seen in the pretty-printed portion of the output. In this case, we see ... balance:(currencies:(grams:(nanograms:(... value:1000000000000000...)))), which is the balance of the account in (test) nanograms (a million test Grams in this example; the actual number you see may be smaller). If you study the TL-B scheme provided in crypto/block/scheme.tlb, you will be able to find this number (10^15) in binary big-endian form in the raw dump portion as well (it is located near the end of the data bits of the root cell). @@ -103,96 +112,117 @@ Before uploading a new smart contract into the TON Blockchain, you need to deter Obviously, you'll need some tools for developing smart contracts - namely, a TON smart contract compiler. Basically, a TON smart contract compiler is a program that reads the source of a smart contract in a specialized high-level programming language and creates a .boc file from this source. -One such tool is the Fift interpreter, which is included in this distribution and can help create simple smart contracts. You may wish to develop more sophisticated tools. However, Fift is sufficient for demonstration purposes. +One such tool is the Fift interpreter, which is included in this distribution and can help create simple smart contracts. Larger smart contracts should be developed using more sophisticated tools (such as the FunC compiler included in this distribution, that creates Fift assembler files from FunC source files; you can find some FunC smart-contract sources in the directory `crypto/smartcont`). However, Fift is sufficient for demonstration purposes. -Create the file `new-wallet.fif` containing the source of our new smart contract: +Consider the file `new-wallet.fif` (usually located as `crypto/smartcont/new-wallet.fif` with respect to the source directory) containing the source of a simple wallet smart contract: ------------------------------------ -#!/usr/bin/fift -s +#!/usr/bin/env fift -s +"TonUtil.fif" include "Asm.fif" include -0 constant wc // create a wallet in workchain 0 (basechain) +{ ."usage: " @' $0 type ." []" cr + ."Creates a new wallet in specified workchain, with private key saved to or loaded from .pk" cr + ."('new-wallet.pk' by default)" cr 1 halt +} : usage +$# 1- -2 and ' usage if + +$1 parse-workchain-id =: wc // set workchain id from command line argument +def? $2 { @' $2 } { "new-wallet" } cond constant file-base + +."Creating new wallet in workchain " wc . cr // Create new simple wallet -<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external - 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt - c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk - s1 s2 XCPU // sign cs cnt pubk cnt' cnt - EQUAL 33 THROWIFNOT // ( seqno mismatch? ) - s2 PUSH HASHSU // sign cs cnt pubk hash - s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk - CHKSIGNU // pubk cs cnt ? - 34 THROWIFNOT // signature mismatch - ACCEPT - SWAP 32 LDU NIP - DUP SREFS IF:<{ - 8 LDU LDREF // pubk cnt mode msg cs - s0 s2 XCHG SENDRAWMSG // pubk cnt cs ; ( message sent ) - }> - ENDS - INC NEWC 32 STU 256 STU ENDC c4 POPCTR -}>c +<{ SETCP0 DUP IFNOTRET // return if recv_internal + DUP 85143 INT EQUAL IFJMP:<{ // "seqno" get-method + DROP c4 PUSHCTR CTOS 32 PLDU // cnt + }> + INC 32 THROWIF // fail unless recv_external + 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk + s1 s2 XCPU // sign cs cnt pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s2 PUSH HASHSU // sign cs cnt pubk hash + s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk + CHKSIGNU // pubk cs cnt ? + 34 THROWIFNOT // signature mismatch + ACCEPT + SWAP 32 LDU NIP + DUP SREFS IF:<{ + // 3 INT 35 LSHIFT# 3 INT RAWRESERVE // reserve all but 103 Grams from the balance + 8 LDU LDREF // pubk cnt mode msg cs + s0 s2 XCHG SENDRAWMSG // pubk cnt cs ; ( message sent ) + }> + ENDS + INC NEWC 32 STU 256 STU ENDC c4 POPCTR +}>c // >libref // code file + file-base +".pk" load-generate-keypair + constant wallet_pk B, b> // data -// no libraries - // create StateInit +null // no libraries +// Libs{ x{ABACABADABACABA} drop x{AAAA} s>c public_lib x{1234} x{5678} |_ s>c public_lib }Libs + // create StateInit dup ."StateInit: " $ type cr -256 u>B "new-wallet.addr" B>file +dup hash wc swap 2dup 2constant wallet_addr +."new wallet address = " 2dup .addr cr +2dup file-base +".addr" save-address-verbose +."Non-bounceable address (for init): " 2dup 7 .Addr cr +."Bounceable address (for later access): " 6 .Addr cr dup ."signing message: " + dup ."External message for initialization is " B dup Bx. cr -"new-wallet-query.boc" tuck B>file -."(Saved to file " type .")" cr +file-base +"-query.boc" tuck B>file +."(Saved wallet creating query to file " type .")" cr -------------------------------------------- -Incidentally, you can find a more sophisticated version of this sample file in crypto/smartcont/new-wallet.fif. It accepts command-line arguments, so you don't need to edit the source file each time you want to create a new wallet. +(The actual source file in your distribution may be slighly different.) Essentially, it is a complete Fift script for creating a new instance of this smart contract controlled by a newly-generated keypair. The script accepts command-line arguments, so you don't need to edit the source file each time you want to create a new wallet. Now, provided that you have compiled Fift binary (usually located as "crypto/fift" with respect to the build directory), you can run -crypto/fift -I/crypto/fift/lib new-wallet.fif +$ crypto/fift -I/crypto/fift/lib -s /crypto/smartcont/new-wallet.fif 0 my_wallet_name -assuming that you have copied new-wallet.fif into the current directory. Alternatively, you might skip the source editing phase and simply run +where 0 is the workchain to contain the new wallet (0 = basechain, -1 = masterchain), `my_wallet_name` is any identifier you wish to be associated with this wallet. The address of the new wallet will be saved into file `my_wallet_name.addr`, its newly-generated private key will be saved to `my_wallet_name.pk` (unless this file already exists; then the key will be loaded from this file instead), and the external message will be saved into my_wallet_name-query.boc. If you do not indicate the name of your wallet (`my_wallet_name` in the example above), the default name `new-wallet` is used. -crypto/fift -I/crypto/fift/lib -s /smartcont/new-wallet.fif 0 my_wallet_name +You may opt to set the FIFTPATH environment variable to /crypto/fift/lib:/crypto/smartcont, the directories containing Fift.fif and Asm.fif library files, and the sample smart-contract sources, respectively; then you can omit the -I argument to the Fift interpreter. If you install the Fift binary `crypto/fift` to a directory included in your PATH (e.g., /usr/bin/fift), you can simply invoke -where 0 is the workchain to contain the new wallet (0 = basechain, -1 = masterchain), `my_wallet_name` is any identifier you wish to be associated with this wallet. The address of the new wallet will be saved into file `my_wallet_name.addr`, its newly-generated private key will be saved to `my_wallet_name.pk` (unless this file already exists; then the key will be loaded from this file instead), and the external message will be saved into my_new_wallet-query.boc. If you do not indicate the name of your wallet (`my_wallet_name` in the example above), the default name `new-wallet` is used. +$ fift -s new-wallet.fif 0 my_wallet_name -You may wish to set the FIFTPATH environment variable to /crypto/fift/lib, the directory containing Fift.fif and Asm.fif library files; then you can omit the -I argument to the Fift interpreter. +instead of indicating the complete search paths in the command line. If everything worked, you'll see something like the following -------------------------------------------- +Creating new wallet in workchain 0 +Saved new private key to file my_wallet_name.pk StateInit: x{34_} - x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} - x{00000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} + x{FF0020DD2082014C97BA9730ED44D0D70B1FE0A4F260810200D71820D70B1FED44D0D31FD3FFD15112BAF2A122F901541044F910F2A2F80001D31F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED54} + x{00000000C59DC52962CC568AC5E72735EABB025C5BDF457D029AEEA6C2FFA5EB2A945446} -new wallet address = -1 : 60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 -0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD +new wallet address = 0:2ee9b4fd4f077c9b223280c35763df9edab0b41ac20d36f4009677df95c3afe2 +(Saving address to file my_wallet_name.addr) +Non-bounceable address (for init): 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb +Bounceable address (for later access): kQAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4rie signing message: x{00000000} -External message for initialization is x{89FEC18082838D4F72DAD0C2BCF523A4CB5A1E7535245D35D39203A9F507EBA781A0119401748E6F89C1BA026A363C9F58765508DFF6854475357210D0D69F07C3A5453CEEDF1A0383FC405B57FF10CE060C2377BDD954A336DE5161F0AC1C61084180E00000001_} - x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} - x{00000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} +External message for initialization is x{88005DD369FA9E0EF93644650186AEC7BF3DB5616835841A6DE8012CEFBF2B875FC41190260D403E40B2EE8BEB2855D0F4447679D9B9519BE64BE421166ABA2C66BEAAAF4EBAF8E162886430243216DDA10FCE68C07B6D7DDAA3E372478D711E3E1041C00000001_} + x{FF0020DD2082014C97BA9730ED44D0D70B1FE0A4F260810200D71820D70B1FED44D0D31FD3FFD15112BAF2A122F901541044F910F2A2F80001D31F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED54} + x{00000000C59DC52962CC568AC5E72735EABB025C5BDF457D029AEEA6C2FFA5EB2A945446} -B5EE9C724104030100000000DA0002CF89FEC18082838D4F72DAD0C2BCF523A4CB5A1E7535245D35D39203A9F507EBA781A0119401748E6F89C1BA026A363C9F58765508DFF6854475357210D0D69F07C3A5453CEEDF1A0383FC405B57FF10CE060C2377BDD954A336DE5161F0AC1C61084180E0000000100102008CFF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54004800000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA32DB9BE63 -(Saved to file new-wallet-query.boc) +B5EE9C724104030100000000E50002CF88005DD369FA9E0EF93644650186AEC7BF3DB5616835841A6DE8012CEFBF2B875FC41190260D403E40B2EE8BEB2855D0F4447679D9B9519BE64BE421166ABA2C66BEAAAF4EBAF8E162886430243216DDA10FCE68C07B6D7DDAA3E372478D711E3E1041C000000010010200A2FF0020DD2082014C97BA9730ED44D0D70B1FE0A4F260810200D71820D70B1FED44D0D31FD3FFD15112BAF2A122F901541044F910F2A2F80001D31F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED54004800000000C59DC52962CC568AC5E72735EABB025C5BDF457D029AEEA6C2FFA5EB2A945446BCF59C17 +(Saved wallet creating query to file my_wallet_name-query.boc) -------------------------------------------- -In a nutshell, the Fift assembler (loaded by the "Asm.fif" include line) is used to compile the source code of the smart contract (contained in <{ SETCP0 ... c4 POPCTR }> lines) into its internal representation. The initial data of the smart contract is also created (by lines), containing a 32-bit sequence number (equal to zero) and a 256-bit public key from a newly-generated Ed25519 keypair. The corresponding private key is saved into the file `new-wallet.pk` (be careful not to run this code twice in the same directory, otherwise this private key file will be overwritten). +In a nutshell, the Fift assembler (loaded by the "Asm.fif" include line) is used to compile the source code of the smart contract (contained in <{ SETCP0 ... c4 POPCTR }> lines) into its internal representation. The initial data of the smart contract is also created (by lines), containing a 32-bit sequence number (equal to zero) and a 256-bit public key from a newly-generated Ed25519 keypair. The corresponding private key is saved into the file `my_wallet_name.pk` unless it already exists (if you run this code twice in the same directory, the private key will be loaded from this file instead). The code and data for the new smart contract are combined into a StateInit structure (in the next lines), the address of the new smart contract (equal to the hash of this StateInit structure) is computed and output, and then an external message with a destination address equal to that of the new smart contract is created. This external message contains both the correct StateInit for the new smart contract and a non-trivial payload (signed by the correct private key). -Finally, the external message is serialized into a bag of cells (represented by B5EE...BE63) and saved into the file `new-wallet-query.boc`. Essentially, this file is your compiled smart contract with all additional information necessary to upload it into the TON Blockchain. +Finally, the external message is serialized into a bag of cells (represented by B5EE...BE63) and saved into the file `my_wallet_name-query.boc`. Essentially, this file is your compiled smart contract with all additional information necessary to upload it into the TON Blockchain. 4. Transferring some funds to the new smart contract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -203,9 +233,9 @@ You might try to upload the new smart contract immediately by running the Lite C or -> sendfile my_new_wallet-query.boc +> sendfile my_wallet_name-query.boc -if you chose to name your wallet `my_new_wallet`. +if you chose to name your wallet `my_wallet_name`. Unfortunately, this won't work, because smart contracts must have a positive balance to be able to pay for storing and processing their data in the blockchain. So you have to transfer some funds to your new smart contract address first, displayed during its generation as -1:60c0...c0d0 (in raw form) and 0f9..EKD (in user-friendly form). @@ -223,64 +253,78 @@ You need to know the address of the test giver smart contract. We'll assume that as explained above in Section 2. The only number you need from the output is the 32-bit sequence number stored in the smart contract data (it is zero in the example above, but generally it will be non-zero). -Next, you create an external message to the test giver asking it to send another message to your (uninitialized) smart contract carrying a specified amount of test Grams. There is a special Fift source file for generating this external message, a more sophisticated version of which is located at crypto/smartcont/testgiver.fif: +Next, you create an external message to the test giver asking it to send another message to your (uninitialized) smart contract carrying a specified amount of test Grams. There is a special Fift script for generating this external message located at crypto/smartcont/testgiver.fif: -------------------------------------------- -// "testgiver.addr" file>B 256 B>u@ -0x8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d -dup constant wallet_addr ."Test giver address = " x. cr +#!/usr/bin/env fift -s +"TonUtil.fif" include -0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 -constant dest_addr +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to TestGiver and saves it into .boc" cr + ."('testgiver-query.boc' by default)" cr 1 halt +} : usage --1 constant wc -0 constant seqno +$# 3 - -2 and ' usage if -1000000000 constant Gram -{ Gram swap */ } : Gram*/ +// "testgiver.addr" load-address +Masterchain 0x8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +2constant giver_addr + ."Test giver address = " giver_addr 2dup .addr cr 6 .Addr cr -6.666 Gram*/ constant amount +$1 true parse-load-address =: bounce 2=: dest_addr +$2 parse-int =: seqno +$3 $>GR =: amount +def? $4 { @' $4 } { "testgiver-query" } cond constant savefile + +."Requesting " amount .GR ."to account " +dest_addr 2dup bounce 7 + .Addr ." = " .addr +."seqno=0x" seqno x. ."bounce=" bounce . cr -// b x --> b' ( serializes a Gram amount ) -{ -1 { 1+ 2dup 8 * ufits } until - rot over 4 u, -rot 8 * u, } : Gram, - // create a message (NB: 01b00.., b = bounce) - + dup ."enveloping message: " dup ."resulting external message: " B dup Bx. cr -"wallet-query.boc" B>file +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr --------------------------------------------- -In general, you will need to edit the line containing the destination address (0x60c0...c0d0 in our example) and the sequence number of the test giver (the "0" in the "0 constant seqno" line). Alternatively, you can pass the required parameters as command-line arguments to +You can pass the required parameters as command-line arguments to this script - fift -I -s [] +$ crypto/fift -I -s [] For instance, - fift -I/crypto/fift/lib:/crypto/smartcont -s testgiver.fif 0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD 6.666 wallet-query +$ crypto/fift -I/crypto/fift/lib:/crypto/smartcont -s testgiver.fif 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb 0x9A15 6.666 wallet-query -(Again, setting FIFTPATH to /crypto/fift/lib:/crypto/smartcont and installing fift binary as /usr/bin/fift is a good idea.) +or simply -The newly-created message to the new smart contract must have its bounce bit clear, otherwise the transfer will be "bounced" to its sender. +$ fift -s testgiver.fif 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb 0x9A15 6.666 wallet-query + +provided you have set up the environment variable FIFTPATH to /crypto/fift/lib:/crypto/smartcont and installed the fift binary as /usr/bin/fift (or anywhere else in your PATH). + +The newly-created message to the new smart contract must have its bounce bit clear, otherwise the transfer will be "bounced" to its sender. This is the reason we have passed the "non-bounceable" address 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb of our new wallet smart contract. This Fift code creates an internal message from the test giver smart contract to the address of our new smart contract carrying 6.666 test Grams (you can enter any other amount here up to approximately 20 Grams). Then this message is enveloped into an external message addressed to the test giver; this external message must also contain the correct sequence number of the test giver. When the test giver receives such an external message, it checks whether the sequence number matches the one stored in its persistent data, and if it does, sends the embedded internal message with the required amount of test Grams to its destination (our smart contract in this case). The external message is serialized and saved into the file `wallet-query.boc`. Some output is generated in the process: --------------------------------------------- -Test giver address = 8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d -enveloping message: x{0000000001} - x{427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B4000000000000000000000000000047494654} +Test giver address = -1:8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +kf-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODZKR +Requesting GR$6.666 to account 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb = 0:2ee9b4fd4f077c9b223280c35763df9edab0b41ac20d36f4009677df95c3afe2 seqno=0x9a15 bounce=0 +enveloping message: x{00009A1501} + x{42001774DA7EA783BE4D91194061ABB1EFCF6D585A0D61069B7A004B3BEFCAE1D7F1280C6A98B4000000000000000000000000000047494654} -resulting external message: x{89FEA71F4F9849FF1D54203B094BE356FD065FC3B0966139BFDE9DD286E755901EFA00000000000C_} - x{427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B4000000000000000000000000000047494654} +resulting external message: x{89FF02ACEEB6F264BCBAC5CE85B372D8616CA2B4B9A5E3EC98BB496327807E0E1C1A000004D0A80C_} + x{42001774DA7EA783BE4D91194061ABB1EFCF6D585A0D61069B7A004B3BEFCAE1D7F1280C6A98B4000000000000000000000000000047494654} -B5EE9C7241040201000000006600014F89FEA71F4F9849FF1D54203B094BE356FD065FC3B0966139BFDE9DD286E755901EFA00000000000C010072427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B40000000000000000000000000000474946545D6254A9 +B5EE9C7241040201000000006600014F89FF02ACEEB6F264BCBAC5CE85B372D8616CA2B4B9A5E3EC98BB496327807E0E1C1A000004D0A80C01007242001774DA7EA783BE4D91194061ABB1EFCF6D585A0D61069B7A004B3BEFCAE1D7F1280C6A98B4000000000000000000000000000047494654AFC17FA4 +(Saved to file wallet-query.boc) --------------------------------------------- 6. Uploading the external message to the test giver smart contract @@ -329,7 +373,7 @@ account state is (account data:(just value:(raw@^Cell x{} - x{00000001} + x{00009A16} )) library:hme_empty)))) x{CFF8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D2068086C00000000000000009F65D110DC0E35F450FA914134_} @@ -337,21 +381,20 @@ x{CFF8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D2068086C000 x{00000001} --------------------------------------------- -You may notice that the sequence number stored in the persistent data has changed (in our example, to one), and the last_trans_lt field (the logical time of the last transaction of this account) has been increased. +You may notice that the sequence number stored in the persistent data has changed (in our example, to 0x9A16 = 39446), and the last_trans_lt field (the logical time of the last transaction of this account) has been increased. Now we can inspect the state of our new smart contract: -> getaccount 0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD +> getaccount 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb or -> getaccount -1:60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 - +> getaccount 0:2ee9b4fd4f077c9b223280c35763df9edab0b41ac20d36f4009677df95c3afe2 Now we see --------------------------------------------- -got account state for -1:60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0 with respect to blocks (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB and (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB +got account state for 0:2EE9B4FD4F077C9B223280C35763DF9EDAB0B41AC20D36F4009677DF95C3AFE2 with respect to blocks (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB and (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB account state is (account addr:(addr_std - anycast:nothing workchain_id:-1 address:x60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0) + anycast:nothing workchain_id:0 address:x2EE9B4FD4F077C9B223280C35763DF9EDAB0B41AC20D36F4009677DF95C3AFE2) storage_stat:(storage_info used:(storage_used cells:(var_uint len:1 value:1) @@ -376,16 +419,16 @@ Our new smart contract has some positive balance (of 6.666 test Grams), but has Now you can finally upload the external message with the StateInit of the new smart contract, containing its code and data: --------------------------------------------- -> sendfile new-wallet-query.boc +> sendfile my_wallet_name-query.boc ... external message status is 1 > last ... -> getaccount -1:60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 +> getaccount 0QAu6bT9Twd8myIygMNXY9-e2rC0GsINNvQAlnfflcOv4uVb ... -got account state for -1:60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0 with respect to blocks (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E and (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E +got account state for 0:2EE9B4FD4F077C9B223280C35763DF9EDAB0B41AC20D36F4009677DF95C3AFE2 with respect to blocks (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E and (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E account state is (account addr:(addr_std - anycast:nothing workchain_id:-1 address:x60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0) + anycast:nothing workchain_id:0 address:x2EE9B4FD4F077C9B223280C35763DF9EDAB0B41AC20D36F4009677DF95C3AFE2) storage_stat:(storage_info used:(storage_used cells:(var_uint len:1 value:3) @@ -423,63 +466,76 @@ You will see that the smart contract has been initialized using code and data fr 8. Using the simple wallet smart contract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Actually, the simple wallet smart contract used in this example can be used to transfer test Grams to any other accounts. It is in this respect similar to the test giver smart contract discussed above, with the difference that it processes only external messages signed by the correct private key (of its owner). In our case, it is the private key saved into the file "new-wallet.pk" during the compilation of the smart contract (see Section 3). +Actually, the simple wallet smart contract used in this example can be used to transfer test Grams to any other accounts. It is in this respect similar to the test giver smart contract discussed above, with the difference that it processes only external messages signed by the correct private key (of its owner). In our case, it is the private key saved into the file "my_wallet_name.pk" during the compilation of the smart contract (see Section 3). An example of how you might use this smart contract is provided in sample file crypto/smartcont/wallet.fif : -------------------------------------------------------- -#!/usr/bin/fift -s -def? $1 { "new-wallet" =: $1 } ifnot -$1 $len { "new-wallet" =: $1 } ifnot -$1 +".addr" file>B 256 B>u@ dup constant wallet_addr -."Wallet address = " x. cr -$1 +".pk" file>B dup Blen 32 <> abort"Private key must be exactly 32 bytes long" -constant wallet_pk +#!/usr/bin/env fift -s +"TonUtil.fif" include -// 0x1111111122222222333333334444444455555555666666667777777788888888 constant dest_addr -0x13CB612A00A7C092C7DFD2EA45D603A9B54591BA4C88F71E707E009B879F0FB2 constant dest_addr --1 constant wc -0 constant seqno +{ ."usage: " @' $0 type ." [-B ] []" cr + ."Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file .pk " + ."and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" cr 1 halt +} : usage +$# dup 4 < swap 5 > or ' usage if +def? $6 { @' $5 "-B" $= { @' $6 =: body-boc-file [forget] $6 def? $7 { @' $7 =: $5 [forget] $7 } { [forget] $5 } cond + @' $# 2- =: $# } if } if -1000000000 constant Gram -{ Gram swap */ } : Gram*/ +true constant bounce -.666 Gram*/ constant amount +$1 =: file-base +$2 bounce parse-load-address =: bounce 2=: dest_addr +$3 parse-int =: seqno +$4 $>GR =: amount +def? $5 { @' $5 } { "wallet-query" } cond constant savefile -// b x --> b' ( serializes a Gram amount ) -{ -1 { 1+ 2dup 8 * ufits } until - rot over 4 u, -rot 8 * u, } : Gram, +file-base +".addr" load-address +2dup 2constant wallet_addr +."Source wallet address = " 2dup .addr cr 6 .Addr cr +file-base +".pk" load-keypair nip constant wallet_pk + +def? body-boc-file { @' body-boc-file file>B B>boc } { } cond +constant body-cell + +."Transferring " amount .GR ."to account " +dest_addr 2dup bounce 7 + .Addr ." = " .addr +."seqno=0x" seqno x. ."bounce=" bounce . cr +."Body of transfer message is " body-cell + dup ."signing message: " dup ."resulting external message: " B dup Bx. cr -$1 +"-query.boc" B>file +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr ------------------------------------- -You can hard-code the address of your smart contract here by changing the fourth line to, say, +You can invoke this script as follows: -0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 dup constant wallet_addr +$ fift -I/crypto/fift/lib:/crypto/smartcont -s wallet.fif -You will also need to change the destination address, the name of the file with the private key, the sequence number (it will be 1 immediately after the smart contract is initialized; the actual value can always be retrieved by inspecting the current account state) and the Gram amount to be transferred. The payload of the internal message contains 32 bits with the string "TEST"; it can be changed to something more useful if necessary. +or simply -Alternatively, you can use the more sophisticated version of this script provided in crypto/smartcont/wallet.fif in the source tree, and pass all the required parameters as command-line arguments: +$ fift -s wallet.fif - fift -I/crypto/fift/lib:/crypto/smartcont -s wallet.fif +if you have correctly set up PATH and FIFTPATH. For example, - fift -I/crypto/fift/lib:/crypto/smartcont -s wallet.fif my_new_wallet kf8Ty2EqAKfAksff0upF1gOptUWRukyI9x5wfgCbh58Pss9j 1 .666 +$ fift -s wallet.fif my_wallet_name kf8Ty2EqAKfAksff0upF1gOptUWRukyI9x5wfgCbh58Pss9j 1 .666 -Here `my_new_wallet` is the identifier of your wallet used before with new-wallet.fif; the address and the private key of your test wallet will be loaded from files `my_new_wallet.addr` and `my_new_wallet.pk` in the current directory. +Here `my_wallet_name` is the identifier of your wallet used before with new-wallet.fif; the address and the private key of your test wallet will be loaded from files `my_wallet_name.addr` and `my_wallet_name.pk` in the current directory. When you run this code (by invoking the Fift interpreter), you create an external message with a destination equal to the address of your wallet smart contract, containing a correct Ed25519 signature, a sequence number, and an enveloped internal message from your wallet smart contract to the smart contract indicated in dest_addr, with an arbitrary value attached and an arbitrary payload. When your smart contract receives and processes this external message, it first checks the signature and the sequence number. If they are correct, it accepts the external message, sends the embedded internal message from itself to the intended destination, and increases the sequence number in its persistent data (this is a simple measure to prevent replay attacks, in case this sample wallet smart contract code ends up used in a real wallet application). Of course, a true TON Blockchain wallet application would hide all the intermediate steps explained above. It would first communicate the address of the new smart contract to the user, asking them to transfer some funds to the indicated address (displayed in its non-bounceable user-friendly form) from another wallet or a cryptocurrency exchange, and then would provide a simple interface to display the current balance and to transfer funds to whatever other addresses the user wants. (The aim of this document is to explain how to create new non-trivial smart contracts and experiment with the TON Blockchain Test Network, rather than to explain how one could use the Lite Client instead of a more user-friendly wallet application.) -One final remark: The above examples used smart contracts in the basic workchain (workchain 0). They would work in exactly the same way in the masterchain (workchain -1), if one changes the "0 constant wc" to "-1 constant wc" in relevant places. The only difference is that the processing and storage fees in the basic workchain are 10-100 times lower than in the masterchain. Some smart contracts (such as the validator election smart contract) accept transfers only from masterchain smart contracts, so you'll need a wallet in the masterchain if you wish to make stakes on behalf of your own validator and participate in the elections. +One final remark: The above examples used smart contracts in the basic workchain (workchain 0). They would work in exactly the same way in the masterchain (workchain -1), if one passes workchain identifier -1 instead of 0 as the first argument to `new-wallet.fif`. The only difference is that the processing and storage fees in the basic workchain are 100-1000 times lower than in the masterchain. Some smart contracts (such as the validator election smart contract) accept transfers only from masterchain smart contracts, so you'll need a wallet in the masterchain if you wish to make stakes on behalf of your own validator and participate in the elections. diff --git a/lite-client-docs/README b/lite-client-docs/README index a497ade7..7379d91e 100644 --- a/lite-client-docs/README +++ b/lite-client-docs/README @@ -14,7 +14,7 @@ https://test.ton.org/download The TON Blockchain Test Network is updated quite often, so we cannot guarantee that older versions of the Lite Client will always work. Backward compatibility is not enforced at this development stage. -2) Install the newest versions of make, cmake (version 3.0.2 or later), OpenSSL (including C header files), and g++ or clang (or another C++14-compatible compiler as appropriate for your operating system). +2) Install the newest versions of make, cmake (version 3.0.2 or later), OpenSSL (including C header files), and g++ or clang (or another C++14-compatible compiler as appropriate for your operating system). We strongly recommend installing OpenSSL version 1.1.1 or later for better performance, especially if you intend to run a Full Node or a Validator as well. 3) Suppose that you have unpacked this archive to directory ~/lite-client, where ~ is your home directory, and that you have created an empty directory ~/liteclient-build. Then run the following in a terminal on a Linux system: @@ -22,9 +22,10 @@ cd ~/liteclient-build cmake ~/lite-client cmake --build . --target lite-client -You might also want to build some extra utilities useful for smart-contract development: +You might also build some extra utilities useful for smart-contract development: cmake --build . --target fift +cmake --build . --target func 4) Download the newest configuration file from https://test.ton.org/ton-lite-client-test1.config.json : diff --git a/lite-client/lite-client.cpp b/lite-client/lite-client.cpp index 30cec39b..4dc47e52 100644 --- a/lite-client/lite-client.cpp +++ b/lite-client/lite-client.cpp @@ -235,57 +235,98 @@ bool TestNode::get_server_time() { }); } -bool TestNode::get_server_version() { +bool TestNode::get_server_version(int mode) { auto b = ton::serialize_tl_object(ton::create_tl_object(), true); - return envelope_send_query(std::move(b), [&, Self = actor_id(this) ](td::Result res)->void { - server_ok_ = false; - if (res.is_error()) { - LOG(ERROR) << "cannot get server version and time (server too old?)"; - } else { - auto F = ton::fetch_tl_object(res.move_as_ok(), true); - if (F.is_error()) { - LOG(ERROR) << "cannot parse answer to liteServer.getVersion"; - } else { - auto a = F.move_as_ok(); - server_version_ = a->version_; - server_capabilities_ = a->capabilities_; - server_time_ = a->now_; - server_time_got_at_ = static_cast(td::Clocks::system()); - LOG(INFO) << "server time is " << server_time_ << " (delta " << server_time_ - server_time_got_at_ << ")"; - LOG(WARNING) << "server version is " << (server_version_ >> 8) << "." << (server_version_ & 0xff) - << ", capabilities " << server_capabilities_; - server_ok_ = (server_version_ >= min_ls_version) && !(~server_capabilities_ & min_ls_capabilities); - } - } - if (!server_ok_) { - LOG(ERROR) << "server version is too old (at least " << (min_ls_version >> 8) << "." << (min_ls_version & 0xff) - << " with capabilities " << min_ls_capabilities << " required), some queries are unavailable"; - } + return envelope_send_query(std::move(b), [ Self = actor_id(this), mode ](td::Result res) { + td::actor::send_closure_later(Self, &TestNode::got_server_version, std::move(res), mode); }); +}; + +void TestNode::got_server_version(td::Result res, int mode) { + server_ok_ = false; + if (res.is_error()) { + LOG(ERROR) << "cannot get server version and time (server too old?)"; + } else { + auto F = ton::fetch_tl_object(res.move_as_ok(), true); + if (F.is_error()) { + LOG(ERROR) << "cannot parse answer to liteServer.getVersion"; + } else { + auto a = F.move_as_ok(); + set_server_version(a->version_, a->capabilities_); + set_server_time(a->now_); + } + } + if (!server_ok_) { + LOG(ERROR) << "server version is too old (at least " << (min_ls_version >> 8) << "." << (min_ls_version & 0xff) + << " with capabilities " << min_ls_capabilities << " required), some queries are unavailable"; + } + if (mode & 0x100) { + get_server_mc_block_id(); + } +} + +void TestNode::set_server_version(td::int32 version, td::int64 capabilities) { + if (server_version_ != version || server_capabilities_ != capabilities) { + server_version_ = version; + server_capabilities_ = capabilities; + LOG(WARNING) << "server version is " << (server_version_ >> 8) << "." << (server_version_ & 0xff) + << ", capabilities " << server_capabilities_; + } + server_ok_ = (server_version_ >= min_ls_version) && !(~server_capabilities_ & min_ls_capabilities); +} + +void TestNode::set_server_time(int server_utime) { + server_time_ = server_utime; + server_time_got_at_ = static_cast(td::Clocks::system()); + LOG(INFO) << "server time is " << server_time_ << " (delta " << server_time_ - server_time_got_at_ << ")"; } bool TestNode::get_server_mc_block_id() { - auto b = ton::serialize_tl_object(ton::create_tl_object(), true); - return envelope_send_query(std::move(b), [Self = actor_id(this)](td::Result res)->void { - if (res.is_error()) { - LOG(ERROR) << "cannot get masterchain info from server"; - return; - } else { - auto F = ton::fetch_tl_object(res.move_as_ok(), true); - if (F.is_error()) { - LOG(ERROR) << "cannot parse answer to liteServer.getMasterchainInfo"; + int mode = (server_capabilities_ & 2) ? 0 : -1; + if (mode < 0) { + auto b = ton::serialize_tl_object(ton::create_tl_object(), true); + return envelope_send_query(std::move(b), [Self = actor_id(this)](td::Result res)->void { + if (res.is_error()) { + LOG(ERROR) << "cannot get masterchain info from server"; + return; } else { - auto f = F.move_as_ok(); - auto blk_id = create_block_id(f->last_); - auto zstate_id = create_zero_state_id(f->init_); - LOG(INFO) << "last masterchain block is " << blk_id.to_str(); - td::actor::send_closure_later(Self, &TestNode::got_server_mc_block_id, blk_id, zstate_id); + auto F = ton::fetch_tl_object(res.move_as_ok(), true); + if (F.is_error()) { + LOG(ERROR) << "cannot parse answer to liteServer.getMasterchainInfo"; + } else { + auto f = F.move_as_ok(); + auto blk_id = create_block_id(f->last_); + auto zstate_id = create_zero_state_id(f->init_); + LOG(INFO) << "last masterchain block is " << blk_id.to_str(); + td::actor::send_closure_later(Self, &TestNode::got_server_mc_block_id, blk_id, zstate_id, 0); + } } - } - }); + }); + } else { + auto b = + ton::serialize_tl_object(ton::create_tl_object(mode), true); + return envelope_send_query(std::move(b), [ Self = actor_id(this), mode ](td::Result res)->void { + if (res.is_error()) { + LOG(ERROR) << "cannot get extended masterchain info from server"; + return; + } else { + auto F = ton::fetch_tl_object(res.move_as_ok(), true); + if (F.is_error()) { + LOG(ERROR) << "cannot parse answer to liteServer.getMasterchainInfoExt"; + } else { + auto f = F.move_as_ok(); + auto blk_id = create_block_id(f->last_); + auto zstate_id = create_zero_state_id(f->init_); + LOG(INFO) << "last masterchain block is " << blk_id.to_str(); + td::actor::send_closure_later(Self, &TestNode::got_server_mc_block_id_ext, blk_id, zstate_id, mode, + f->version_, f->capabilities_, f->last_utime_, f->now_); + } + } + }); + } } -void TestNode::got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid) { +void TestNode::got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid, int created) { if (!zstate_id_.is_valid()) { zstate_id_ = zstateid; LOG(INFO) << "zerostate id set to " << zstate_id_.to_str(); @@ -304,9 +345,36 @@ void TestNode::got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt } else if (mc_last_id_.id.seqno < blkid.id.seqno) { mc_last_id_ = blkid; } + td::TerminalIO::out() << "latest masterchain block known to server is " << blkid.to_str(); + if (created > 0) { + td::TerminalIO::out() << " created at " << created << " (" << static_cast(td::Clocks::system()) - created + << " seconds ago)\n"; + } else { + td::TerminalIO::out() << "\n"; + } show_new_blkids(); } +void TestNode::got_server_mc_block_id_ext(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid, int mode, int version, + long long capabilities, int last_utime, int server_now) { + set_server_version(version, capabilities); + set_server_time(server_now); + if (last_utime > server_now) { + LOG(WARNING) << "server claims to have a masterchain block " << blkid.to_str() << " created at " << last_utime + << " (" << last_utime - server_now << " seconds in the future)"; + } else if (last_utime < server_now - 60) { + LOG(WARNING) << "server appears to be out of sync: its newest masterchain block is " << blkid.to_str() + << " created at " << last_utime << " (" << server_now - last_utime + << " seconds ago according to the server's clock)"; + } else if (last_utime < server_time_got_at_ - 60) { + LOG(WARNING) << "either the server is out of sync, or the local clock is set incorrectly: the newest masterchain " + "block known to server is " + << blkid.to_str() << " created at " << last_utime << " (" << server_now - server_time_got_at_ + << " seconds ago according to the local clock)"; + } + got_server_mc_block_id(blkid, zstateid, last_utime); +} + bool TestNode::request_block(ton::BlockIdExt blkid) { auto b = ton::serialize_tl_object( ton::create_tl_object(ton::create_tl_lite_block_id(blkid)), true); @@ -423,8 +491,7 @@ td::Status TestNode::save_db_file(ton::FileHash file_hash, td::BufferSlice data) } void TestNode::run_init_queries() { - get_server_version(); - get_server_mc_block_id(); + get_server_version(0x100); } std::string TestNode::get_word(char delim) { @@ -2008,10 +2075,10 @@ bool TestNode::get_block_proof(ton::BlockIdExt from, ton::BlockIdExt to, int mod } if (!(mode & 0x2000)) { LOG(INFO) << "got block proof request from " << from.to_str() << " to " - << ((mode & 1) ? to.to_str() : "last masterchain block") << " with mode=" << mode; + << ((mode & 1) ? to.to_str() : "last masterchain block") << " with mode=" << mode; } else { LOG(DEBUG) << "got block proof request from " << from.to_str() << " to " - << ((mode & 1) ? to.to_str() : "last masterchain block") << " with mode=" << mode; + << ((mode & 1) ? to.to_str() : "last masterchain block") << " with mode=" << mode; } if (!from.is_masterchain_ext()) { LOG(ERROR) << "source block " << from.to_str() << " is not a valid masterchain block id"; @@ -2112,22 +2179,36 @@ void TestNode::got_block_proof(ton::BlockIdExt from, ton::BlockIdExt to, int mod return; } auto chain = res.move_as_ok(); + if (chain->from != from) { + LOG(ERROR) << "block proof chain starts from block " << chain->from.to_str() << ", not from requested block " + << from.to_str(); + return; + } auto err = chain->validate(); if (err.is_error()) { LOG(ERROR) << "block proof chain is invalid: " << err; return; } - LOG(INFO) << "valid " << (chain->complete ? "" : "in") << "complete proof chain: last block is " << chain->to.to_str() - << ", last key block is " << (chain->has_key_block ? chain->key_blkid.to_str() : "(undefined)"); // TODO: if `from` was a trusted key block, then mark `to` as a trusted key block, and update the known value of latest trusted key block if `to` is newer if (!chain->complete && (mode & 0x1000)) { + LOG(INFO) << "valid " << (chain->complete ? "" : "in") << "complete proof chain: last block is " + << chain->to.to_str() << ", last key block is " + << (chain->has_key_block ? chain->key_blkid.to_str() : "(undefined)"); get_block_proof(chain->to, to, mode | 0x2000); return; } + td::TerminalIO::out() << "valid " << (chain->complete ? "" : "in") << "complete proof chain: last block is " + << chain->to.to_str() << ", last key block is " + << (chain->has_key_block ? chain->key_blkid.to_str() : "(undefined)") << std::endl; if (chain->has_key_block) { register_blkid(chain->key_blkid); } register_blkid(chain->to); + auto now = static_cast(td::Clocks::system()); + if (!(mode & 1) || (chain->last_utime > now - 3600)) { + td::TerminalIO::out() << "last block in chain was generated at " << chain->last_utime << " (" + << now - chain->last_utime << " seconds ago)\n"; + } show_new_blkids(); } diff --git a/lite-client/lite-client.h b/lite-client/lite-client.h index 5631b49d..3c920c4e 100644 --- a/lite-client/lite-client.h +++ b/lite-client/lite-client.h @@ -94,9 +94,14 @@ class TestNode : public td::actor::Actor { void run_init_queries(); bool get_server_time(); - bool get_server_version(); + bool get_server_version(int mode = 0); + void got_server_version(td::Result res, int mode); bool get_server_mc_block_id(); - void got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid); + void got_server_mc_block_id(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid, int created_at); + void got_server_mc_block_id_ext(ton::BlockIdExt blkid, ton::ZeroStateIdExt zstateid, int mode, int version, + long long capabilities, int last_utime, int server_now); + void set_server_version(td::int32 version, td::int64 capabilities); + void set_server_time(int server_utime); bool request_block(ton::BlockIdExt blkid); bool request_state(ton::BlockIdExt blkid); void got_mc_block(ton::BlockIdExt blkid, td::BufferSlice data); diff --git a/tdutils/td/utils/OptionsParser.cpp b/tdutils/td/utils/OptionsParser.cpp index 1bc2441f..fc5e474a 100644 --- a/tdutils/td/utils/OptionsParser.cpp +++ b/tdutils/td/utils/OptionsParser.cpp @@ -35,6 +35,11 @@ void OptionsParser::set_description(std::string description) { void OptionsParser::add_option(Option::Type type, char short_key, Slice long_key, Slice description, std::function callback) { + for (auto &option : options_) { + if (option.short_key == short_key || (!long_key.empty() && long_key == option.long_key)) { + LOG(ERROR) << "Ignore duplicated option '" << short_key << "' '" << long_key << "'"; + } + } options_.push_back(Option{type, short_key, long_key.str(), description.str(), std::move(callback)}); } diff --git a/tdutils/td/utils/Random.cpp b/tdutils/td/utils/Random.cpp index e9cd0d88..d5be75a0 100644 --- a/tdutils/td/utils/Random.cpp +++ b/tdutils/td/utils/Random.cpp @@ -51,6 +51,11 @@ void Random::secure_bytes(unsigned char *ptr, size_t size) { buf_pos = buf_size; generation = 0; } + if (ptr == nullptr) { + td::MutableSlice(buf, buf_size).fill_zero_secure(); + buf_pos = buf_size; + return; + } if (generation != random_seed_generation.load(std::memory_order_relaxed)) { generation = random_seed_generation.load(std::memory_order_acquire); buf_pos = buf_size; @@ -109,6 +114,10 @@ void Random::add_seed(Slice bytes, double entropy) { RAND_add(bytes.data(), static_cast(bytes.size()), entropy); random_seed_generation++; } + +void Random::secure_cleanup() { + Random::secure_bytes(nullptr, 0); +} #endif static unsigned int rand_device_helper() { diff --git a/tdutils/td/utils/Random.h b/tdutils/td/utils/Random.h index e578335e..5ca74d62 100644 --- a/tdutils/td/utils/Random.h +++ b/tdutils/td/utils/Random.h @@ -35,6 +35,7 @@ class Random { // works only for current thread static void add_seed(Slice bytes, double entropy = 0); + static void secure_cleanup(); #endif static uint32 fast_uint32(); diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index f8dc6a25..d747313e 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -153,10 +153,11 @@ inline char to_lower(char c) { return c; } -inline void to_lower_inplace(MutableSlice slice) { +inline MutableSlice to_lower_inplace(MutableSlice slice) { for (auto &c : slice) { c = to_lower(c); } + return slice; } inline string to_lower(Slice slice) { @@ -321,8 +322,8 @@ string url_encode(Slice str); namespace detail { template -struct is_same_signedness - : public std::integral_constant::value == std::is_signed::value> {}; +struct is_same_signedness : public std::integral_constant::value == std::is_signed::value> { +}; template struct safe_undeflying_type { diff --git a/tl/generate/scheme/lite_api.tl b/tl/generate/scheme/lite_api.tl index 66243365..9b85e91b 100644 --- a/tl/generate/scheme/lite_api.tl +++ b/tl/generate/scheme/lite_api.tl @@ -27,6 +27,7 @@ liteServer.error code:int message:string = liteServer.Error; liteServer.accountId workchain:int id:int256 = liteServer.AccountId; liteServer.masterchainInfo last:tonNode.blockIdExt state_root_hash:int256 init:tonNode.zeroStateIdExt = liteServer.MasterchainInfo; +liteServer.masterchainInfoExt mode:# version:int capabilities:long last:tonNode.blockIdExt last_utime:int now:int state_root_hash:int256 init:tonNode.zeroStateIdExt = liteServer.MasterchainInfoExt; liteServer.currentTime now:int = liteServer.CurrentTime; liteServer.version mode:# version:int capabilities:long now:int = liteServer.Version; liteServer.blockData id:tonNode.blockIdExt data:bytes = liteServer.BlockData; @@ -53,6 +54,7 @@ liteServer.debug.verbosity value:int = liteServer.debug.Verbosity; ---functions--- liteServer.getMasterchainInfo = liteServer.MasterchainInfo; +liteServer.getMasterchainInfoExt mode:# = liteServer.MasterchainInfoExt; liteServer.getTime = liteServer.CurrentTime; liteServer.getVersion = liteServer.Version; liteServer.getBlock id:tonNode.blockIdExt = liteServer.BlockData; diff --git a/tl/generate/scheme/lite_api.tlo b/tl/generate/scheme/lite_api.tlo index a029226f905ac3feb47921642ca9e54ce43fee45..3cdaa35e41ea6278089dee9b1d4d58e8547bdad3 100644 GIT binary patch delta 181 zcmewp&=Sh~Xtur;0~Ba&;1xI1qzcpm1OzBCc=$m1RAN%Q{>F1!(rQY5UlNh H(jHa-jMhWR delta 51 zcmZn({S(0ZXtur;0~Ba( CG!M4` diff --git a/tl/generate/scheme/tonlib_api.tl b/tl/generate/scheme/tonlib_api.tl index 53fdf3d0..c0e81d61 100644 --- a/tl/generate/scheme/tonlib_api.tl +++ b/tl/generate/scheme/tonlib_api.tl @@ -24,6 +24,8 @@ exportedKey word_list:vector = ExportedKey; exportedPemKey pem:secureString = ExportedPemKey; exportedEncryptedKey data:secureBytes = ExportedEncryptedKey; +bip39Hints words:vector = Bip39Hints; + accountAddress account_address:string = AccountAddress; internal.transactionId lt:int64 hash:bytes = internal.TransactionId; @@ -56,7 +58,7 @@ close = Ok; options.setConfig config:string = Ok; -createNewKey local_password:secureBytes mnemonic_password:secureBytes = Key; +createNewKey local_password:secureBytes mnemonic_password:secureBytes random_extra_seed:secureBytes = Key; deleteKey public_key:bytes = Ok; exportKey input_key:inputKey = ExportedKey; exportPemKey input_key:inputKey key_password:secureBytes = ExportedPemKey; @@ -66,6 +68,8 @@ importPemKey local_password:secureBytes key_password:secureBytes exported_key:ex importEncryptedKey local_password:secureBytes key_password:secureBytes exported_encrypted_key:exportedEncryptedKey = Key; changeLocalPassword input_key:inputKey new_local_password:secureBytes = Key; +getBip39Hints prefix:string = Bip39Hints; + //raw.init initial_account_state:raw.initialAccountState = Ok; raw.getAccountAddress initital_account_state:raw.initialAccountState = AccountAddress; raw.getAccountState account_address:accountAddress = raw.AccountState; diff --git a/tl/generate/scheme/tonlib_api.tlo b/tl/generate/scheme/tonlib_api.tlo index a1571a3c6f6c8b5886757f4651bf31a4408d8f1e..a0e7ddc81539b79246e344e9abeb8a51fb55052e 100644 GIT binary patch delta 225 zcmaFj_`{j^(QJJy1}IRP$Q#VhFelW8%PF(K*wQ02ucUbLc}B^N2|BEds*`(J)78MT zNpM*P5dY>eAs*q~R iEkQU4Xc3y7Yz0NBX_*z1`ME?V3$SxQENziU-~a$xv_>5O delta 77 zcmez2{KS#>(QJJy1}IRR$Q!(|Lx+`7W%5?m^vx}7DSV7llV6G)X0ZyH{Acn8Q3*z- d$-2_|lYfY6Oyb~}BrUi(RqQ#_<|UFL8~~ya7;gXo diff --git a/tonlib/test/offline.cpp b/tonlib/test/offline.cpp index b9915703..5d418034 100644 --- a/tonlib/test/offline.cpp +++ b/tonlib/test/offline.cpp @@ -132,6 +132,12 @@ TEST(Tonlib, TestGiver) { vm::CellSlice(vm::NoVm(), res).print_rec(std::cerr); CHECK(vm::std_boc_deserialize(wallet_query).move_as_ok()->get_hash() == res->get_hash()); } +TEST(Tonlib, PublicKey) { + block::PublicKey::parse("pubjns2gp7DGCnEH7EOWeCnb6Lw1akm538YYaz6sdLVHfRB2").ensure_error(); + auto key = block::PublicKey::parse("Pubjns2gp7DGCnEH7EOWeCnb6Lw1akm538YYaz6sdLVHfRB2").move_as_ok(); + CHECK(td::buffer_to_hex(key.key) == "3EE9DC0A7A0B6CA01770CE34698792BD8ECB53A6949BFD6C81B6E3CA475B74D7"); + CHECK(key.serialize() == "Pubjns2gp7DGCnEH7EOWeCnb6Lw1akm538YYaz6sdLVHfRB2"); +} TEST(Tonlib, Address) { auto a = block::StdAddress::parse("-1:538fa7cc24ff8eaa101d84a5f1ab7e832fe1d84b309cdfef4ee94373aac80f7d").move_as_ok(); @@ -287,12 +293,14 @@ TEST(Tonlib, KeysApi) { auto local_password = td::SecureString("local password"); auto mnemonic_password = td::SecureString("mnemonic password"); { - auto key = sync_send(client, make_object(local_password.copy(), td::SecureString{})) + auto key = sync_send(client, make_object(local_password.copy(), td::SecureString{}, + td::SecureString{})) .move_as_ok(); } //createNewKey local_password:bytes mnemonic_password:bytes = Key; - auto key = sync_send(client, make_object(local_password.copy(), mnemonic_password.copy())) + auto key = sync_send(client, make_object(local_password.copy(), mnemonic_password.copy(), + td::SecureString{})) .move_as_ok(); sync_send(client, make_object(make_object( diff --git a/tonlib/test/online.cpp b/tonlib/test/online.cpp index 739cc208..89a9eaa7 100644 --- a/tonlib/test/online.cpp +++ b/tonlib/test/online.cpp @@ -126,9 +126,9 @@ void transfer_grams(Client& client, std::string from, std::string to, td::int64 } Wallet create_empty_wallet(Client& client) { using tonlib_api::make_object; - auto key = - sync_send(client, make_object(td::SecureString("local"), td::SecureString("mnemonic"))) - .move_as_ok(); + auto key = sync_send(client, make_object(td::SecureString("local"), + td::SecureString("mnemonic"), td::SecureString())) + .move_as_ok(); Wallet wallet{"", {key->public_key_, std::move(key->secret_)}}; auto account_address = @@ -210,8 +210,8 @@ int main(int argc, char* argv[]) { // init sync_send(client, make_object(make_object(global_config_str, "."))).ensure(); - auto key = sync_send(client, - make_object(td::SecureString("local"), td::SecureString("mnemonic"))) + auto key = sync_send(client, make_object( + td::SecureString("local"), td::SecureString("mnemonic"), td::SecureString())) .move_as_ok(); auto create_input_key = [&] { diff --git a/tonlib/tonlib/KeyStorage.cpp b/tonlib/tonlib/KeyStorage.cpp index d9f444de..1c257643 100644 --- a/tonlib/tonlib/KeyStorage.cpp +++ b/tonlib/tonlib/KeyStorage.cpp @@ -63,9 +63,11 @@ td::Result KeyStorage::save_key(const DecryptedKey &decrypted_k return std::move(res); } -td::Result KeyStorage::create_new_key(td::Slice local_password, td::Slice mnemonic_password) { +td::Result KeyStorage::create_new_key(td::Slice local_password, td::Slice mnemonic_password, + td::Slice entropy) { Mnemonic::Options create_options; create_options.password = td::SecureString(mnemonic_password); + create_options.entropy = td::SecureString(entropy); TRY_RESULT(mnemonic, Mnemonic::create_new(std::move(create_options))); return save_key(DecryptedKey(std::move(mnemonic)), local_password); diff --git a/tonlib/tonlib/KeyStorage.h b/tonlib/tonlib/KeyStorage.h index 1b357aa0..305872be 100644 --- a/tonlib/tonlib/KeyStorage.h +++ b/tonlib/tonlib/KeyStorage.h @@ -50,7 +50,7 @@ class KeyStorage { td::Status set_directory(std::string directory); - td::Result create_new_key(td::Slice local_password, td::Slice key_password); + td::Result create_new_key(td::Slice local_password, td::Slice key_password, td::Slice entropy); td::Result export_key(InputKey input_key); td::Result export_pem_key(InputKey input_key, td::Slice key_password); diff --git a/tonlib/tonlib/TonlibClient.cpp b/tonlib/tonlib/TonlibClient.cpp index 9b00852d..17503ec6 100644 --- a/tonlib/tonlib/TonlibClient.cpp +++ b/tonlib/tonlib/TonlibClient.cpp @@ -24,6 +24,7 @@ #include "tonlib/TestWallet.h" #include "tonlib/TestGiver.h" #include "tonlib/utils.h" +#include "tonlib/keys/Mnemonic.h" #include "auto/tl/tonlib_api.hpp" #include "block/block-auto.h" @@ -350,6 +351,7 @@ bool TonlibClient::is_static_request(td::int32 id) { case tonlib_api::raw_getAccountAddress::ID: case tonlib_api::testWallet_getAccountAddress::ID: case tonlib_api::testGiver_getAccountAddress::ID: + case tonlib_api::getBip39Hints::ID: return true; default: return false; @@ -405,6 +407,11 @@ tonlib_api::object_ptr TonlibClient::do_static_request( return tonlib_api::make_object(TestGiver::address().rserialize()); } +tonlib_api::object_ptr TonlibClient::do_static_request(tonlib_api::getBip39Hints& request) { + return tonlib_api::make_object( + td::transform(Mnemonic::word_hints(td::trim(td::to_lower_inplace(request.prefix_))), [](auto& x) { return x; })); +} + td::Status TonlibClient::do_request(const tonlib_api::init& request, td::Promise>&& promise) { if (state_ != State::Uninited) { @@ -900,8 +907,8 @@ td::Status TonlibClient::do_request(tonlib_api::generic_sendGrams& request, td::Status TonlibClient::do_request(const tonlib_api::createNewKey& request, td::Promise>&& promise) { - TRY_RESULT(key, - key_storage_.create_new_key(std::move(request.local_password_), std::move(request.mnemonic_password_))); + TRY_RESULT(key, key_storage_.create_new_key(std::move(request.local_password_), std::move(request.mnemonic_password_), + std::move(request.random_extra_seed_))); promise.set_value(tonlib_api::make_object(key.public_key.as_slice().str(), std::move(key.secret))); return td::Status::OK(); } diff --git a/tonlib/tonlib/TonlibClient.h b/tonlib/tonlib/TonlibClient.h index db1b3ca0..244506c8 100644 --- a/tonlib/tonlib/TonlibClient.h +++ b/tonlib/tonlib/TonlibClient.h @@ -79,6 +79,7 @@ class TonlibClient : public td::actor::Actor { static object_ptr do_static_request(const tonlib_api::raw_getAccountAddress& request); static object_ptr do_static_request(const tonlib_api::testWallet_getAccountAddress& request); static object_ptr do_static_request(const tonlib_api::testGiver_getAccountAddress& request); + static object_ptr do_static_request(tonlib_api::getBip39Hints& request); template td::Status do_request(const T& request, P&& promise) { return td::Status::Error(400, "Function is unsupported"); diff --git a/tonlib/tonlib/keys/Mnemonic.cpp b/tonlib/tonlib/keys/Mnemonic.cpp index 00dfc17e..9cddd862 100644 --- a/tonlib/tonlib/keys/Mnemonic.cpp +++ b/tonlib/tonlib/keys/Mnemonic.cpp @@ -28,9 +28,12 @@ #include "td/utils/Span.h" #include "td/utils/misc.h" #include "td/utils/optional.h" +#include "td/utils/Timer.h" #include "crypto/Ed25519.h" +#include + namespace tonlib { td::Result Mnemonic::create(td::SecureString words, td::SecureString password) { return create_from_normalized(normalize_and_split(std::move(words)), std::move(password)); @@ -133,7 +136,28 @@ td::SecureString Mnemonic::join(td::Span words) { return res; } +td::Span Mnemonic::word_hints(td::Slice prefix) { + static std::vector words = [] { + auto bip_words = Mnemonic::normalize_and_split(td::SecureString(bip39_english())); + std::vector res; + for (auto &word : bip_words) { + res.push_back(word.as_slice().str()); + } + return res; + }(); + if (prefix.empty()) { + return words; + } + + auto p = std::equal_range(words.begin(), words.end(), prefix, [&](td::Slice a, td::Slice b) { + return a.truncate(prefix.size()) < b.truncate(prefix.size()); + }); + + return td::Span(&*p.first, p.second - p.first); +} + td::Result Mnemonic::create_new(Options options) { + td::Timer timer; if (options.words_count == 0) { options.words_count = 24; } @@ -146,14 +170,28 @@ td::Result Mnemonic::create_new(Options options) { max_iterations *= 256; } + td::Random::add_seed(options.entropy.as_slice()); + SCOPE_EXIT { + td::Random::secure_cleanup(); + }; + auto bip_words = Mnemonic::normalize_and_split(td::SecureString(bip39_english())); CHECK(bip_words.size() == 2048); int A = 0, B = 0, C = 0; for (int iteration = 0; iteration < max_iterations; iteration++) { std::vector words; + td::SecureString rnd((options.words_count * 11 + 7) / 8); + td::Random::secure_bytes(rnd.as_mutable_slice()); for (int i = 0; i < options.words_count; i++) { - words.push_back(bip_words[td::Random::secure_int32() & 2047].copy()); + size_t word_i = 0; + for (size_t j = 0; j < 11; j++) { + size_t offset = i * 11 + j; + if ((rnd[offset / 8] & (1 << (offset & 7))) != 0) { + word_i |= 1 << j; + } + } + words.push_back(bip_words[word_i].copy()); } bool has_password = !options.password.empty(); @@ -180,7 +218,7 @@ td::Result Mnemonic::create_new(Options options) { continue; } - LOG(INFO) << "Mnemonic generation debug stats: " << A << " " << B << " " << C; + LOG(INFO) << "Mnemonic generation debug stats: " << A << " " << B << " " << C << " " << timer; return std::move(mnemonic); } return td::Status::Error("Failed to create a mnemonic (should not happen)"); diff --git a/tonlib/tonlib/keys/Mnemonic.h b/tonlib/tonlib/keys/Mnemonic.h index adff528e..27d6a833 100644 --- a/tonlib/tonlib/keys/Mnemonic.h +++ b/tonlib/tonlib/keys/Mnemonic.h @@ -36,6 +36,7 @@ class Mnemonic { } int words_count = 24; td::SecureString password; + td::SecureString entropy; }; static td::Result create_new(Options options = {}); @@ -51,6 +52,7 @@ class Mnemonic { std::vector get_words() const; static std::vector normalize_and_split(td::SecureString words); + static td::Span word_hints(td::Slice prefix); private: std::vector words_; diff --git a/tonlib/tonlib/keys/bip39.h b/tonlib/tonlib/keys/bip39.h index c37ded40..da894dcb 100644 --- a/tonlib/tonlib/keys/bip39.h +++ b/tonlib/tonlib/keys/bip39.h @@ -18,7 +18,8 @@ */ #pragma once #include "td/utils/Slice.h" +#include "td/utils/Span.h" namespace tonlib { td::CSlice bip39_english(); -} +} // namespace tonlib diff --git a/tonlib/tonlib/tonlib-cli.cpp b/tonlib/tonlib/tonlib-cli.cpp index a12b0d53..85eb6e31 100644 --- a/tonlib/tonlib/tonlib-cli.cpp +++ b/tonlib/tonlib/tonlib-cli.cpp @@ -152,6 +152,8 @@ class TonlibCli : public td::actor::Actor { auto to = parser.read_word(); auto grams = parser.read_word(); transfer(from, to, grams); + } else if (cmd == "hint") { + get_hints(parser.read_word()); } } @@ -191,21 +193,27 @@ class TonlibCli : public td::actor::Actor { }; } - void generate_key(std::string entropy = "") { + void generate_key(td::SecureString entropy = {}) { if (entropy.size() < 20) { td::TerminalIO::out() << "Enter some entropy"; - cont_ = [this, entropy](td::Slice new_entropy) { generate_key(entropy + new_entropy.str()); }; + cont_ = [this, entropy = std::move(entropy)](td::Slice new_entropy) { + td::SecureString res(entropy.size() + new_entropy.size()); + res.as_mutable_slice().copy_from(entropy.as_slice()); + res.as_mutable_slice().substr(entropy.size()).copy_from(new_entropy); + generate_key(std::move(res)); + }; return; } td::TerminalIO::out() << "Enter password (could be empty)"; - cont_ = [this, entropy](td::Slice password) { generate_key(std::move(entropy), td::SecureString(password)); }; + cont_ = [this, entropy = std::move(entropy)](td::Slice password) mutable { + generate_key(std::move(entropy), td::SecureString(password)); + }; } - void generate_key(std::string entropy, td::SecureString password) { - //TODO: use entropy + void generate_key(td::SecureString entropy, td::SecureString password) { auto password_copy = password.copy(); - send_query(tonlib_api::make_object(std::move(password_copy), - td::SecureString() /*mnemonic password*/), + send_query(tonlib_api::make_object( + std::move(password_copy), td::SecureString() /*mnemonic password*/, std::move(entropy)), [this, password = std::move(password)](auto r_key) mutable { if (r_key.is_error()) { LOG(ERROR) << "Failed to create new key: " << r_key.error(); @@ -542,6 +550,15 @@ class TonlibCli : public td::actor::Actor { td::TerminalIO::out() << to_string(r_res.ok()); }); } + + void get_hints(td::Slice prefix) { + using tonlib_api::make_object; + auto obj = tonlib::TonlibClient::static_request(make_object(prefix.str())); + if (obj->get_id() == tonlib_api::error::ID) { + return; + } + td::TerminalIO::out() << to_string(obj); + } }; int main(int argc, char* argv[]) { diff --git a/validator/impl/collator.cpp b/validator/impl/collator.cpp index 8c40acfb..8627c3b8 100644 --- a/validator/impl/collator.cpp +++ b/validator/impl/collator.cpp @@ -1443,19 +1443,32 @@ bool Collator::fetch_config_params() { { // compute compute_phase_cfg / storage_phase_cfg auto cell = config_->get_config_param(is_masterchain() ? 20 : 21); - block::gen::GasLimitsPrices::Record rec; - if (cell.is_null() || !tlb::unpack_cell(std::move(cell), rec)) { + if (cell.is_null()) { return fatal_error("cannot fetch current gas prices and limits from masterchain configuration"); } - compute_phase_cfg_.gas_limit = rec.gas_limit; - compute_phase_cfg_.gas_credit = rec.gas_credit; - compute_phase_cfg_.gas_price = rec.gas_price; + auto f = [self = this](const auto& r, td::uint64 spec_limit) { + self->compute_phase_cfg_.gas_limit = r.gas_limit; + self->compute_phase_cfg_.special_gas_limit = spec_limit; + self->compute_phase_cfg_.gas_credit = r.gas_credit; + self->compute_phase_cfg_.gas_price = r.gas_price; + self->storage_phase_cfg_.freeze_due_limit = td::RefInt256{true, r.freeze_due_limit}; + self->storage_phase_cfg_.delete_due_limit = td::RefInt256{true, r.delete_due_limit}; + }; + block::gen::GasLimitsPrices::Record_gas_prices_ext rec; + if (tlb::unpack_cell(cell, rec)) { + f(rec, rec.special_gas_limit); + } else { + block::gen::GasLimitsPrices::Record_gas_prices rec0; + if (tlb::unpack_cell(std::move(cell), rec0)) { + f(rec0, rec0.gas_limit); + } else { + return fatal_error("cannot unpack current gas prices and limits from masterchain configuration"); + } + } compute_phase_cfg_.compute_threshold(); compute_phase_cfg_.block_rand_seed = rand_seed_; compute_phase_cfg_.libraries = std::make_unique(config_->get_libraries_root(), 256); compute_phase_cfg_.global_config = config_->get_root_cell(); - storage_phase_cfg_.freeze_due_limit = td::RefInt256{true, rec.freeze_due_limit}; - storage_phase_cfg_.delete_due_limit = td::RefInt256{true, rec.delete_due_limit}; } { // compute action_phase_cfg diff --git a/validator/impl/liteserver.cpp b/validator/impl/liteserver.cpp index 4ba21b71..b59a64fe 100644 --- a/validator/impl/liteserver.cpp +++ b/validator/impl/liteserver.cpp @@ -118,7 +118,10 @@ void LiteQuery::start_up() { td::overloaded( [&](lite_api::liteServer_getTime& q) { this->perform_getTime(); }, [&](lite_api::liteServer_getVersion& q) { this->perform_getVersion(); }, - [&](lite_api::liteServer_getMasterchainInfo& q) { this->perform_getMasterchainInfo(); }, + [&](lite_api::liteServer_getMasterchainInfo& q) { this->perform_getMasterchainInfo(-1); }, + [&](lite_api::liteServer_getMasterchainInfoExt& q) { + this->perform_getMasterchainInfo(q.mode_ & 0x7fffffff); + }, [&](lite_api::liteServer_getBlock& q) { this->perform_getBlock(ton::create_block_id(q.id_)); }, [&](lite_api::liteServer_getBlockHeader& q) { this->perform_getBlockHeader(ton::create_block_id(q.id_), q.mode_); @@ -181,22 +184,27 @@ void LiteQuery::perform_getVersion() { finish_query(std::move(b)); } -void LiteQuery::perform_getMasterchainInfo() { - LOG(INFO) << "started a getMasterchainInfo() liteserver query"; +void LiteQuery::perform_getMasterchainInfo(int mode) { + LOG(INFO) << "started a getMasterchainInfo(" << mode << ") liteserver query"; + if (mode > 0) { + fatal_error("unsupported getMasterchainInfo mode"); + return; + } td::actor::send_closure_later( manager_, &ton::validator::ValidatorManager::get_top_masterchain_state_block, - [Self = actor_id(this)](td::Result, BlockIdExt>> res)->void { + [ Self = actor_id(this), mode ](td::Result, BlockIdExt>> res) { if (res.is_error()) { td::actor::send_closure(Self, &LiteQuery::abort_query, res.move_as_error()); } else { auto pair = res.move_as_ok(); td::actor::send_closure_later(Self, &LiteQuery::continue_getMasterchainInfo, std::move(pair.first), - pair.second); + pair.second, mode); } }); } -void LiteQuery::continue_getMasterchainInfo(Ref mc_state, BlockIdExt blkid) { +void LiteQuery::continue_getMasterchainInfo(Ref mc_state, BlockIdExt blkid, + int mode) { LOG(INFO) << "obtained data for getMasterchainInfo() : last block = " << blkid.to_str(); auto mc_state_q = Ref(std::move(mc_state)); if (mc_state_q.is_null()) { @@ -206,8 +214,12 @@ void LiteQuery::continue_getMasterchainInfo(Refget_zerostate_id(); auto zs_tl = create_tl_object(zerostate_id.workchain, zerostate_id.root_hash, zerostate_id.file_hash); - auto b = ton::create_serialize_tl_object( - ton::create_tl_lite_block_id(blkid), mc_state_q->root_hash(), std::move(zs_tl)); + td::int32 now = static_cast(std::time(nullptr)); + auto b = (mode == -1) ? ton::create_serialize_tl_object( + ton::create_tl_lite_block_id(blkid), mc_state_q->root_hash(), std::move(zs_tl)) + : ton::create_serialize_tl_object( + mode, ls_version, ls_capabilities, ton::create_tl_lite_block_id(blkid), + mc_state_q->get_unix_time(), now, mc_state_q->root_hash(), std::move(zs_tl)); finish_query(std::move(b)); } diff --git a/validator/impl/liteserver.hpp b/validator/impl/liteserver.hpp index f0bc64c1..41077b2d 100644 --- a/validator/impl/liteserver.hpp +++ b/validator/impl/liteserver.hpp @@ -57,8 +57,11 @@ class LiteQuery : public td::actor::Actor { std::unique_ptr chain_; public: - enum { default_timeout_msec = 4500 }; // 4.5 seconds - enum { ls_version = 0x101, ls_capabilities = 1 }; // version 1.1; +1 = build block proof chains + enum { default_timeout_msec = 4500 }; // 4.5 seconds + enum { + ls_version = 0x101, + ls_capabilities = 3 + }; // version 1.1; +1 = build block proof chains, +2 = masterchainInfoExt LiteQuery(td::BufferSlice data, td::actor::ActorId manager, td::Promise promise); static void run_query(td::BufferSlice data, td::actor::ActorId manager, @@ -75,8 +78,8 @@ class LiteQuery : public td::actor::Actor { void start_up() override; void perform_getTime(); void perform_getVersion(); - void perform_getMasterchainInfo(); - void continue_getMasterchainInfo(Ref mc_state, BlockIdExt blkid); + void perform_getMasterchainInfo(int mode); + void continue_getMasterchainInfo(Ref mc_state, BlockIdExt blkid, int mode); void perform_getBlock(BlockIdExt blkid); void continue_getBlock(BlockIdExt blkid, Ref block); void perform_getBlockHeader(BlockIdExt blkid, int mode); diff --git a/validator/impl/validate-query.cpp b/validator/impl/validate-query.cpp index f2eb00cb..95d47942 100644 --- a/validator/impl/validate-query.cpp +++ b/validator/impl/validate-query.cpp @@ -255,7 +255,7 @@ void ValidateQuery::start_up() { LOG(DEBUG) << "sending wait_block_state() query #" << i << " for " << prev_blocks[i].to_str() << " to Manager"; ++pending; td::actor::send_closure_later(manager, &ValidatorManager::wait_block_state_short, prev_blocks[i], priority(), - timeout, [self = get_self(), i](td::Result> res) -> void { + timeout, [ self = get_self(), i ](td::Result> res)->void { LOG(DEBUG) << "got answer to wait_block_state_short query #" << i; td::actor::send_closure_later( std::move(self), &ValidateQuery::after_get_shard_state, i, std::move(res)); @@ -269,16 +269,16 @@ void ValidateQuery::start_up() { // 5. request masterchain state referred to in the block if (!is_masterchain()) { ++pending; - td::actor::send_closure_later(manager, &ValidatorManager::wait_block_state_short, mc_blkid_, priority(), timeout, - [self = get_self()](td::Result> res) { + td::actor::send_closure_later(manager, &ValidatorManager::wait_block_state_short, mc_blkid_, priority(), + timeout, [self = get_self()](td::Result> res) { LOG(DEBUG) << "got answer to wait_block_state() query for masterchain block"; td::actor::send_closure_later(std::move(self), &ValidateQuery::after_get_mc_state, std::move(res)); }); // 5.1. request corresponding block handle ++pending; - td::actor::send_closure_later(manager, &ValidatorManager::get_block_handle, mc_blkid_, true, - [self = get_self()](td::Result res) { + td::actor::send_closure_later(manager, &ValidatorManager::get_block_handle, mc_blkid_, + true, [self = get_self()](td::Result res) { LOG(DEBUG) << "got answer to get_block_handle() query for masterchain block"; td::actor::send_closure_later(std::move(self), &ValidateQuery::got_mc_handle, std::move(res)); @@ -722,19 +722,32 @@ bool ValidateQuery::fetch_config_params() { { // compute compute_phase_cfg / storage_phase_cfg auto cell = config_->get_config_param(is_masterchain() ? 20 : 21); - block::gen::GasLimitsPrices::Record rec; - if (cell.is_null() || !tlb::unpack_cell(std::move(cell), rec)) { + if (cell.is_null()) { return fatal_error("cannot fetch current gas prices and limits from masterchain configuration"); } - compute_phase_cfg_.gas_limit = rec.gas_limit; - compute_phase_cfg_.gas_credit = rec.gas_credit; - compute_phase_cfg_.gas_price = rec.gas_price; + auto f = [self = this](const auto& r, td::uint64 spec_limit) { + self->compute_phase_cfg_.gas_limit = r.gas_limit; + self->compute_phase_cfg_.special_gas_limit = spec_limit; + self->compute_phase_cfg_.gas_credit = r.gas_credit; + self->compute_phase_cfg_.gas_price = r.gas_price; + self->storage_phase_cfg_.freeze_due_limit = td::RefInt256{true, r.freeze_due_limit}; + self->storage_phase_cfg_.delete_due_limit = td::RefInt256{true, r.delete_due_limit}; + }; + block::gen::GasLimitsPrices::Record_gas_prices_ext rec; + if (tlb::unpack_cell(cell, rec)) { + f(rec, rec.special_gas_limit); + } else { + block::gen::GasLimitsPrices::Record_gas_prices rec0; + if (tlb::unpack_cell(std::move(cell), rec0)) { + f(rec0, rec0.gas_limit); + } else { + return fatal_error("cannot unpack current gas prices and limits from masterchain configuration"); + } + } compute_phase_cfg_.compute_threshold(); compute_phase_cfg_.block_rand_seed = rand_seed_; compute_phase_cfg_.libraries = std::make_unique(config_->get_libraries_root(), 256); compute_phase_cfg_.global_config = config_->get_root_cell(); - storage_phase_cfg_.freeze_due_limit = td::RefInt256{true, rec.freeze_due_limit}; - storage_phase_cfg_.delete_due_limit = td::RefInt256{true, rec.delete_due_limit}; } { // compute action_phase_cfg @@ -1167,7 +1180,7 @@ bool ValidateQuery::request_neighbor_queues() { LOG(DEBUG) << "neighbor #" << i << " : " << descr.blk_.to_str(); ++pending; send_closure_later(manager, &ValidatorManager::wait_block_message_queue_short, descr.blk_, priority(), timeout, - [self = get_self(), i](td::Result> res) { + [ self = get_self(), i ](td::Result> res) { td::actor::send_closure(std::move(self), &ValidateQuery::got_neighbor_out_queue, i, std::move(res)); }); @@ -1285,13 +1298,12 @@ bool ValidateQuery::request_aux_mc_state(BlockSeqno seqno, Ref> res) { - LOG(DEBUG) << "got answer to wait_block_state query for " << blkid.to_str(); - td::actor::send_closure_later(std::move(self), - &ValidateQuery::after_get_aux_shard_state, blkid, - std::move(res)); - }); + td::actor::send_closure_later(manager, &ValidatorManager::wait_block_state_short, blkid, priority(), timeout, [ + self = get_self(), blkid + ](td::Result> res) { + LOG(DEBUG) << "got answer to wait_block_state query for " << blkid.to_str(); + td::actor::send_closure_later(std::move(self), &ValidateQuery::after_get_aux_shard_state, blkid, std::move(res)); + }); state.clear(); return true; } @@ -1627,8 +1639,8 @@ bool ValidateQuery::check_shard_layout() { WorkchainId wc_id{ton::workchainInvalid}; Ref wc_info; - if (!new_shard_conf_->process_sibling_shard_hashes([self = this, &wc_set, &wc_id, &wc_info, &ccvc]( - block::McShardHash& cur, const block::McShardHash* sibling) { + if (!new_shard_conf_->process_sibling_shard_hashes([ self = this, &wc_set, &wc_id, &wc_info, &ccvc ]( + block::McShardHash & cur, const block::McShardHash* sibling) { if (!cur.is_valid()) { return -2; } @@ -4619,8 +4631,8 @@ bool ValidateQuery::check_one_library_update(td::ConstBitPtr key, Ref(256); } if (!old_publishers->scan_diff(*new_publishers, - [this, lib_key = key](td::ConstBitPtr key, int key_len, Ref old_val, - Ref new_val) { + [ this, lib_key = key ](td::ConstBitPtr key, int key_len, Ref old_val, + Ref new_val) { CHECK(key_len == 256); if (old_val.not_null() && !old_val->empty_ext()) { return false;