Skip to content

Commit

Permalink
Merge pull request #929 from ton-blockchain/testnet
Browse files Browse the repository at this point in the history
Merge developer branch
  • Loading branch information
EmelyanenkoK committed Mar 11, 2024
2 parents 692211f + 132a603 commit 200508c
Show file tree
Hide file tree
Showing 29 changed files with 1,051 additions and 144 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2024.03 Update

1. Preparatory (not enabled yet) code for pre-compiled smart-contract.
2. Minor fixes for fee-related opcodes.

## 2024.02 Update

1. Improvement of validator synchronisation:
Expand Down
48 changes: 28 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@
</div>

##
[![TON Overflow Group][ton-overflow-badge]][ton-overflow-url]
[![Stack Overflow Group][stack-overflow-badge]][stack-overflow-url]
[![Telegram Community Chat][telegram-tondev-badge]][telegram-tondev-url]
[![Telegram Community Group][telegram-community-badge]][telegram-community-url]
[![Telegram Foundation Group][telegram-foundation-badge]][telegram-foundation-url]
[![Twitter Group][twitter-badge]][twitter-url]

[telegram-foundation-badge]: https://img.shields.io/badge/TON%20Foundation-2CA5E0?logo=telegram&logoColor=white&style=flat
[telegram-community-badge]: https://img.shields.io/badge/TON%20Community-2CA5E0?logo=telegram&logoColor=white&style=flat
[telegram-tondev-badge]: https://img.shields.io/badge/chat-TONDev-2CA5E0?logo=telegram&logoColor=white&style=flat
[telegram-foundation-url]: https://t.me/tonblockchain
[telegram-community-url]: https://t.me/toncoin
[telegram-tondev-url]: https://t.me/tondev_eng
[twitter-badge]: https://img.shields.io/twitter/follow/ton_blockchain
[twitter-url]: https://twitter.com/ton_blockchain
[stack-overflow-badge]: https://img.shields.io/badge/-Stack%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white
[stack-overflow-url]: https://stackoverflow.com/questions/tagged/ton
[ton-overflow-badge]: https://img.shields.io/badge/-TON%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white
[ton-overflow-url]: https://answers.ton.org

<p align="center">
<a href="https://tonresear.ch">
<img src="https://img.shields.io/badge/TON%20Research-0098EA?style=flat&logo=discourse&label=Forum&labelColor=gray" alt="Ton Research">
</a>
<a href="https://t.me/toncoin">
<img src="https://img.shields.io/badge/TON%20Community-0098EA?logo=telegram&logoColor=white&style=flat" alt="Telegram Community Group">
</a>
<a href="https://t.me/tonblockchain">
<img src="https://img.shields.io/badge/TON%20Foundation-0098EA?logo=telegram&logoColor=white&style=flat" alt="Telegram Foundation Group">
</a>
<a href="https://t.me/tondev_eng">
<img src="https://img.shields.io/badge/chat-TONDev-0098EA?logo=telegram&logoColor=white&style=flat" alt="Telegram Community Chat">
</a>
</p>

<p align="center">
<a href="https://twitter.com/ton_blockchain">
<img src="https://img.shields.io/twitter/follow/ton_blockchain" alt="Twitter Group">
</a>
<a href="https://answers.ton.org">
<img src="https://img.shields.io/badge/-TON%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white" alt="TON Overflow Group">
</a>
<a href="https://stackoverflow.com/questions/tagged/ton">
<img src="https://img.shields.io/badge/-Stack%20Overflow-FE7A16?style=flat&logo=stack-overflow&logoColor=white" alt="Stack Overflow Group">
</a>
</p>



Expand Down Expand Up @@ -148,4 +156,4 @@ More examples for other platforms can be found under `assembly/nix`.

## Running tests

Tests are executed by running `ctest` in the build directory. See `doc/Tests.md` for more information.
Tests are executed by running `ctest` in the build directory. See `doc/Tests.md` for more information.
7 changes: 7 additions & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ set(BLOCK_SOURCE
block/mc-config.cpp
block/output-queue-merger.cpp
block/transaction.cpp
block/precompiled-smc/PrecompiledSmartContract.cpp
${TLB_BLOCK_AUTO}

block/block-binlog.h
Expand All @@ -223,6 +224,8 @@ set(BLOCK_SOURCE
block/check-proof.h
block/output-queue-merger.h
block/transaction.h
block/precompiled-smc/PrecompiledSmartContract.h
block/precompiled-smc/common.h
)

set(SMC_ENVELOPE_SOURCE
Expand Down Expand Up @@ -376,6 +379,10 @@ add_library(ton_block STATIC ${BLOCK_SOURCE})
target_include_directories(ton_block PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/block> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api)
if (USE_EMSCRIPTEN)
target_link_options(ton_block PRIVATE -fexceptions)
target_compile_options(ton_block PRIVATE -fexceptions)
endif()

add_executable(func func/func-main.cpp ${FUNC_LIB_SOURCE})
target_include_directories(func PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
Expand Down
4 changes: 4 additions & 0 deletions crypto/block/block.tlb
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,10 @@ _ SizeLimitsConfig = ConfigParam 43;
suspended_address_list#00 addresses:(HashmapE 288 Unit) suspended_until:uint32 = SuspendedAddressList;
_ SuspendedAddressList = ConfigParam 44;

precompiled_smc#b0 gas_usage:uint64 = PrecompiledSmc;
precompiled_contracts_config#c0 list:(HashmapE 256 PrecompiledSmc) = PrecompiledContractsConfig;
_ PrecompiledContractsConfig = ConfigParam 45;

oracle_bridge_params#_ bridge_address:bits256 oracle_mutlisig_address:bits256 oracles:(HashmapE 256 uint256) external_chain_address:bits256 = OracleBridgeParams;
_ OracleBridgeParams = ConfigParam 71; // Ethereum bridge
_ OracleBridgeParams = ConfigParam 72; // Binance Smart Chain bridge
Expand Down
27 changes: 27 additions & 0 deletions crypto/block/mc-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,17 @@ td::Ref<vm::Tuple> Config::get_unpacked_config_tuple(ton::UnixTime now) const {
return td::make_cnt_ref<std::vector<vm::StackEntry>>(std::move(tuple));
}

PrecompiledContractsConfig Config::get_precompiled_contracts_config() const {
PrecompiledContractsConfig c;
td::Ref<vm::Cell> param = get_config_param(45);
gen::PrecompiledContractsConfig::Record rec;
if (param.is_null() || !tlb::unpack_cell(param, rec)) {
return c;
}
c.list = vm::Dictionary{rec.list->prefetch_ref(), 256};
return c;
}

td::Result<std::pair<ton::UnixTime, ton::UnixTime>> Config::unpack_validator_set_start_stop(Ref<vm::Cell> vset_root) {
if (vset_root.is_null()) {
return td::Status::Error("validator set absent");
Expand Down Expand Up @@ -2316,4 +2327,20 @@ td::Result<Ref<vm::Tuple>> ConfigInfo::get_prev_blocks_info() const {
block_id_to_tuple(last_key_block));
}

td::optional<PrecompiledContractsConfig::Contract> PrecompiledContractsConfig::get_contract(
td::Bits256 code_hash) const {
auto list_copy = list;
auto cs = list_copy.lookup(code_hash);
if (cs.is_null()) {
return {};
}
gen::PrecompiledSmc::Record rec;
if (!tlb::csr_unpack(cs, rec)) {
return {};
}
Contract c;
c.gas_usage = rec.gas_usage;
return c;
}

} // namespace block
10 changes: 10 additions & 0 deletions crypto/block/mc-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ struct BurningConfig {
}
};

struct PrecompiledContractsConfig {
struct Contract {
td::uint64 gas_usage;
};
vm::Dictionary list{256};

td::optional<Contract> get_contract(td::Bits256 code_hash) const;
};

class Config {
enum {
default_mc_catchain_lifetime = 200,
Expand Down Expand Up @@ -644,6 +653,7 @@ class Config {
std::unique_ptr<vm::Dictionary> get_suspended_addresses(ton::UnixTime now) const;
BurningConfig get_burning_config() const;
td::Ref<vm::Tuple> get_unpacked_config_tuple(ton::UnixTime now) const;
PrecompiledContractsConfig get_precompiled_contracts_config() const;
static std::vector<ton::ValidatorDescr> do_compute_validator_set(const block::CatchainValidatorsConfig& ccv_conf,
ton::ShardIdFull shard,
const block::ValidatorSet& vset, ton::UnixTime time,
Expand Down
170 changes: 170 additions & 0 deletions crypto/block/precompiled-smc/PrecompiledSmartContract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <https://www.gnu.org/licenses/>.
*/
#include "common.h"
#include <memory>
#include "vm/memo.h"

namespace block::precompiled {

using namespace vm;

Result PrecompiledSmartContract::run(td::Ref<vm::CellSlice> my_address, ton::UnixTime now, ton::LogicalTime cur_lt,
CurrencyCollection balance, td::Ref<vm::Cell> c4, vm::CellSlice msg_body,
td::Ref<vm::Cell> msg, CurrencyCollection msg_balance, bool is_external,
std::vector<td::Ref<vm::Cell>> libraries, int global_version,
td::uint16 max_data_depth, td::Ref<vm::Cell> my_code,
td::Ref<vm::Tuple> unpacked_config, td::RefInt256 due_payment,
td::uint64 precompiled_gas_usage) {
my_address_ = std::move(my_address);
now_ = now;
cur_lt_ = cur_lt;
balance_ = std::move(balance);
c4_ = (c4.not_null() ? std::move(c4) : CellBuilder().finalize());
in_msg_body_ = std::move(msg_body);
in_msg_ = std::move(msg);
in_msg_balance_ = std::move(msg_balance);
is_external_ = is_external;
my_code_ = std::move(my_code);
unpacked_config_ = std::move(unpacked_config);
due_payment_ = std::move(due_payment);
precompiled_gas_usage_ = precompiled_gas_usage;

vm::DummyVmState vm_state{std::move(libraries), global_version};
vm::VmStateInterface::Guard guard{&vm_state};

Result result;
try {
result = do_run();
} catch (vm::VmError &e) {
result = Result::error(e.get_errno(), e.get_arg());
} catch (Result &r) {
result = std::move(r);
}

if (result.exit_code != 0 && result.exit_code != 1) {
// see VmState::try_commit()
if (c4_.is_null() || c4_->get_depth() > max_data_depth || c4_->get_level() != 0 || c5_.is_null() ||
c5_->get_depth() > max_data_depth || c5_->get_level() != 0) {
result = Result::error(Excno::cell_ov, 0);
}
}
return result;
}

void PrecompiledSmartContract::send_raw_message(const td::Ref<Cell> &msg, int mode) {
CellBuilder cb;
if (!(cb.store_ref_bool(c5_) // out_list$_ {n:#} prev:^(OutList n)
&& cb.store_long_bool(0x0ec3c86d, 32) // action_send_msg#0ec3c86d
&& cb.store_long_bool(mode, 8) // mode:(## 8)
&& cb.store_ref_bool(msg))) {
throw VmError{Excno::cell_ov, "cannot serialize raw output message into an output action cell"};
}
c5_ = cb.finalize_novm();
}

void PrecompiledSmartContract::raw_reserve(const td::RefInt256 &amount, int mode) {
if (amount->sgn() < 0) {
throw VmError{Excno::range_chk, "amount of nanograms must be non-negative"};
}
CellBuilder cb;
if (!(cb.store_ref_bool(c5_) // out_list$_ {n:#} prev:^(OutList n)
&& cb.store_long_bool(0x36e6b809, 32) // action_reserve_currency#36e6b809
&& cb.store_long_bool(mode, 8) // mode:(## 8)
&& util::store_coins(cb, std::move(amount), true) //
&& cb.store_maybe_ref({}))) {
throw VmError{Excno::cell_ov, "cannot serialize raw reserved currency amount into an output action cell"};
}
c5_ = cb.finalize_novm();
}

td::RefInt256 PrecompiledSmartContract::get_compute_fee(ton::WorkchainId wc, td::uint64 gas_used) {
if (gas_used >= (1ULL << 63)) {
throw VmError{Excno::range_chk};
}
block::GasLimitsPrices prices = util::get_gas_prices(unpacked_config_, wc);
return util::check_finite(prices.compute_gas_price(gas_used));
}

td::RefInt256 PrecompiledSmartContract::get_forward_fee(ton::WorkchainId wc, td::uint64 bits, td::uint64 cells) {
if (bits >= (1ULL << 63) || cells >= (1ULL << 63)) {
throw VmError{Excno::range_chk};
}
block::MsgPrices prices = util::get_msg_prices(unpacked_config_, wc);
return util::check_finite(prices.compute_fwd_fees256(cells, bits));
}

td::RefInt256 PrecompiledSmartContract::get_storage_fee(ton::WorkchainId wc, td::uint64 duration, td::uint64 bits,
td::uint64 cells) {
if (bits >= (1ULL << 63) || cells >= (1ULL << 63) || duration >= (1ULL << 63)) {
throw VmError{Excno::range_chk};
}
td::optional<block::StoragePrices> maybe_prices = util::get_storage_prices(unpacked_config_);
return util::check_finite(util::calculate_storage_fee(maybe_prices, wc, duration, bits, cells));
}

td::RefInt256 PrecompiledSmartContract::get_simple_compute_fee(ton::WorkchainId wc, td::uint64 gas_used) {
if (gas_used >= (1ULL << 63)) {
throw VmError{Excno::range_chk};
}
block::GasLimitsPrices prices = util::get_gas_prices(unpacked_config_, wc);
return util::check_finite(td::rshift(td::make_refint(prices.gas_price) * gas_used, 16, 1));
}

td::RefInt256 PrecompiledSmartContract::get_simple_forward_fee(ton::WorkchainId wc, td::uint64 bits, td::uint64 cells) {
if (bits >= (1ULL << 63) || cells >= (1ULL << 63)) {
throw VmError{Excno::range_chk};
}
block::MsgPrices prices = util::get_msg_prices(unpacked_config_, wc);
return util::check_finite(
td::rshift(td::make_refint(prices.bit_price) * bits + td::make_refint(prices.cell_price) * cells, 16, 1));
}

td::RefInt256 PrecompiledSmartContract::get_original_fwd_fee(ton::WorkchainId wc, const td::RefInt256 &x) {
if (x->sgn() < 0) {
throw VmError{Excno::range_chk, "fwd_fee is negative"};
}
block::MsgPrices prices = util::get_msg_prices(unpacked_config_, wc);
return util::check_finite(td::muldiv(x, td::make_refint(1 << 16), td::make_refint((1 << 16) - prices.first_frac)));
}

static std::atomic_bool precompiled_execution_enabled{false};

std::unique_ptr<PrecompiledSmartContract> get_implementation(td::Bits256 code_hash) {
if (!precompiled_execution_enabled) {
return nullptr;
}
static std::map<td::Bits256, std::unique_ptr<PrecompiledSmartContract> (*)()> map = []() {
auto from_hex = [](td::Slice s) -> td::Bits256 {
td::Bits256 x;
CHECK(x.from_hex(s) == 256);
return x;
};
std::map<td::Bits256, std::unique_ptr<PrecompiledSmartContract> (*)()> map;
#define CONTRACT(hash, cls) \
map[from_hex(hash)] = []() -> std::unique_ptr<PrecompiledSmartContract> { return std::make_unique<cls>(); };
// CONTRACT("CODE_HASH_HEX", ClassName);
return map;
}();
auto it = map.find(code_hash);
return it == map.end() ? nullptr : it->second();
}

void set_precompiled_execution_enabled(bool value) {
precompiled_execution_enabled = value;
}

} // namespace block::precompiled
Loading

0 comments on commit 200508c

Please sign in to comment.