Skip to content

Commit

Permalink
Merge pull request #1475 from ton-blockchain/node-patch
Browse files Browse the repository at this point in the history
Bugfixes in node and tonlib
  • Loading branch information
EmelyanenkoK authored Jan 15, 2025
2 parents 46d4e12 + 87c4b4a commit 8b68210
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 52 deletions.
14 changes: 14 additions & 0 deletions adnl/adnl-peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void AdnlPeerPairImpl::discover() {
void AdnlPeerPairImpl::receive_packet_checked(AdnlPacket packet) {
last_received_packet_ = td::Timestamp::now();
try_reinit_at_ = td::Timestamp::never();
drop_addr_list_at_ = td::Timestamp::never();
request_reverse_ping_after_ = td::Timestamp::in(15.0);
auto d = Adnl::adnl_start_time();
if (packet.dst_reinit_date() > d) {
Expand Down Expand Up @@ -415,6 +416,9 @@ void AdnlPeerPairImpl::send_packet_continue(AdnlPacket packet, td::actor::ActorI
if (!try_reinit_at_ && last_received_packet_ < td::Timestamp::in(-5.0)) {
try_reinit_at_ = td::Timestamp::in(10.0);
}
if (!drop_addr_list_at_ && last_received_packet_ < td::Timestamp::in(-60.0 * 9.0)) {
drop_addr_list_at_ = td::Timestamp::in(60.0);
}
packet.run_basic_checks().ensure();
auto B = serialize_tl_object(packet.tl(), true);
if (via_channel) {
Expand Down Expand Up @@ -692,6 +696,16 @@ void AdnlPeerPairImpl::reinit(td::int32 date) {
}

td::Result<std::pair<td::actor::ActorId<AdnlNetworkConnection>, bool>> AdnlPeerPairImpl::get_conn() {
if (drop_addr_list_at_ && drop_addr_list_at_.is_in_past()) {
drop_addr_list_at_ = td::Timestamp::never();
priority_addr_list_ = AdnlAddressList{};
priority_conns_.clear();
addr_list_ = AdnlAddressList{};
conns_.clear();
has_reverse_addr_ = false;
return td::Status::Error(ErrorCode::notready, "no active connections");
}

if (!priority_addr_list_.empty() && priority_addr_list_.expire_at() < td::Clocks::system()) {
priority_addr_list_ = AdnlAddressList{};
priority_conns_.clear();
Expand Down
1 change: 1 addition & 0 deletions adnl/adnl-peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class AdnlPeerPairImpl : public AdnlPeerPair {

td::Timestamp last_received_packet_ = td::Timestamp::never();
td::Timestamp try_reinit_at_ = td::Timestamp::never();
td::Timestamp drop_addr_list_at_ = td::Timestamp::never();

bool has_reverse_addr_ = false;
td::Timestamp request_reverse_ping_after_ = td::Timestamp::now();
Expand Down
21 changes: 11 additions & 10 deletions crypto/smc-envelope/SmartContract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,17 @@ td::Ref<vm::Tuple> prepare_vm_c7(SmartContract::Args args, td::Ref<vm::Cell> cod
}

std::vector<vm::StackEntry> tuple = {
td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea
td::make_refint(0), // actions:Integer
td::make_refint(0), // msgs_sent:Integer
td::make_refint(now), // unixtime:Integer
td::make_refint(0), //TODO: // block_lt:Integer
td::make_refint(0), //TODO: // trans_lt:Integer
std::move(rand_seed_int), // rand_seed:Integer
block::CurrencyCollection(args.balance).as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)]
vm::load_cell_slice_ref(address), // myself:MsgAddressInt
vm::StackEntry::maybe(config) // vm::StackEntry::maybe(td::Ref<vm::Cell>())
td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea
td::make_refint(0), // actions:Integer
td::make_refint(0), // msgs_sent:Integer
td::make_refint(now), // unixtime:Integer
td::make_refint(0), // block_lt:Integer (TODO)
td::make_refint(0), // trans_lt:Integer (TODO)
std::move(rand_seed_int), // rand_seed:Integer
block::CurrencyCollection(args.balance, args.extra_currencies)
.as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)]
vm::load_cell_slice_ref(address), // myself:MsgAddressInt
vm::StackEntry::maybe(config) // vm::StackEntry::maybe(td::Ref<vm::Cell>())
};
if (args.config && args.config.value()->get_global_version() >= 4) {
tuple.push_back(vm::StackEntry::maybe(code)); // code:Cell
Expand Down
5 changes: 5 additions & 0 deletions crypto/smc-envelope/SmartContract.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SmartContract : public td::CntObject {
bool ignore_chksig{false};
td::uint64 amount{0};
td::uint64 balance{0};
td::Ref<vm::Cell> extra_currencies;
int vm_log_verbosity_level{0};
bool debug_enabled{false};

Expand Down Expand Up @@ -121,6 +122,10 @@ class SmartContract : public td::CntObject {
this->balance = balance;
return std::move(*this);
}
Args&& set_extra_currencies(td::Ref<vm::Cell> extra_currencies) {
this->extra_currencies = std::move(extra_currencies);
return std::move(*this);
}
Args&& set_address(block::StdAddress address) {
this->address = address;
return std::move(*this);
Expand Down
3 changes: 0 additions & 3 deletions crypto/tl/tlbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,9 +1800,6 @@ void Constructor::show(std::ostream& os, int mode) const {
}
for (int i = 0; i < type_arity; i++) {
os << ' ';
if (param_negated.at(i)) {
os << '~';
}
params.at(i)->show(os, this, 100, mode | 1);
}
if (!(mode & 2)) {
Expand Down
21 changes: 12 additions & 9 deletions tonlib/tonlib/TonlibClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,15 +1050,17 @@ class Query {
}

vm::GasLimits gas_limits = compute_gas_limits(td::make_refint(raw_.source->get_balance()), gas_limits_prices);
auto res = smc.write().send_external_message(raw_.message_body, ton::SmartContract::Args()
.set_limits(gas_limits)
.set_balance(raw_.source->get_balance())
.set_now(raw_.source->get_sync_time())
.set_ignore_chksig(ignore_chksig)
.set_address(raw_.source->get_address())
.set_config(cfg)
.set_prev_blocks_info(state.prev_blocks_info)
.set_libraries(libraries));
auto res = smc.write().send_external_message(raw_.message_body,
ton::SmartContract::Args()
.set_limits(gas_limits)
.set_balance(raw_.source->get_balance())
.set_extra_currencies(raw_.source->get_extra_currencies())
.set_now(raw_.source->get_sync_time())
.set_ignore_chksig(ignore_chksig)
.set_address(raw_.source->get_address())
.set_config(cfg)
.set_prev_blocks_info(state.prev_blocks_info)
.set_libraries(libraries));
td::int64 fwd_fee = 0;
if (res.success) {
LOG(DEBUG) << "output actions:\n"
Expand Down Expand Up @@ -4790,6 +4792,7 @@ td::Status TonlibClient::do_request(const tonlib_api::smc_runGetMethod& request,
}
args.set_stack(std::move(stack));
args.set_balance(it->second->get_balance());
args.set_extra_currencies(it->second->get_extra_currencies());
args.set_now(it->second->get_sync_time());
args.set_address(it->second->get_address());

Expand Down
5 changes: 3 additions & 2 deletions validator/downloaders/wait-block-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void WaitBlockState::start() {
if (reading_from_db_) {
return;
}
if (handle_->received_state()) {
bool inited_proof = handle_->id().is_masterchain() ? handle_->inited_proof() : handle_->inited_proof_link();
if (handle_->received_state() && inited_proof) {
reading_from_db_ = true;

auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
Expand Down Expand Up @@ -107,7 +108,7 @@ void WaitBlockState::start() {
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_zero_state_request, handle_->id(), priority_,
std::move(P));
} else if (check_persistent_state_desc()) {
} else if (check_persistent_state_desc() && !handle_->received_state()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
LOG(WARNING) << "failed to get persistent state: " << R.move_as_error();
Expand Down
59 changes: 32 additions & 27 deletions validator/impl/validate-query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,7 @@ void ValidateQuery::start_up() {
// return;
}
}
// 2. learn latest masterchain state and block id
LOG(DEBUG) << "sending get_top_masterchain_state_block() to Manager";
++pending;
td::actor::send_closure_later(manager, &ValidatorManager::get_top_masterchain_state_block,
[self = get_self()](td::Result<std::pair<Ref<MasterchainState>, BlockIdExt>> res) {
LOG(DEBUG) << "got answer to get_top_masterchain_state_block";
td::actor::send_closure_later(
std::move(self), &ValidateQuery::after_get_latest_mc_state, std::move(res));
});
// 3. load state(s) corresponding to previous block(s)
// 2. load state(s) corresponding to previous block(s)
prev_states.resize(prev_blocks.size());
for (int i = 0; (unsigned)i < prev_blocks.size(); i++) {
// 3.1. load state
Expand All @@ -368,21 +359,13 @@ void ValidateQuery::start_up() {
std::move(self), &ValidateQuery::after_get_shard_state, i, std::move(res));
});
}
// 4. unpack block candidate (while necessary data is being loaded)
// 3. unpack block candidate (while necessary data is being loaded)
if (!unpack_block_candidate()) {
reject_query("error unpacking block candidate");
return;
}
// 5. request masterchain state referred to in the block
// 4. request masterchain handle and 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<Ref<ShardState>> 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<BlockHandle> res) {
Expand Down Expand Up @@ -663,6 +646,19 @@ bool ValidateQuery::extract_collated_data() {
return true;
}

/**
* Send get_top_masterchain_state_block to manager, call after_get_latest_mc_state afterwards
*/
void ValidateQuery::request_latest_mc_state() {
++pending;
td::actor::send_closure_later(manager, &ValidatorManager::get_top_masterchain_state_block,
[self = get_self()](td::Result<std::pair<Ref<MasterchainState>, BlockIdExt>> res) {
LOG(DEBUG) << "got answer to get_top_masterchain_state_block";
td::actor::send_closure_later(
std::move(self), &ValidateQuery::after_get_latest_mc_state, std::move(res));
});
}

/**
* Callback function called after retrieving the latest masterchain state.
*
Expand Down Expand Up @@ -710,6 +706,7 @@ void ValidateQuery::after_get_latest_mc_state(td::Result<std::pair<Ref<Mastercha
* @param res The result of the masterchain state retrieval.
*/
void ValidateQuery::after_get_mc_state(td::Result<Ref<ShardState>> res) {
CHECK(!is_masterchain());
LOG(WARNING) << "in ValidateQuery::after_get_mc_state() for " << mc_blkid_.to_str();
--pending;
if (res.is_error()) {
Expand All @@ -720,6 +717,7 @@ void ValidateQuery::after_get_mc_state(td::Result<Ref<ShardState>> res) {
fatal_error("cannot process masterchain state for "s + mc_blkid_.to_str());
return;
}
request_latest_mc_state();
if (!pending) {
if (!try_validate()) {
fatal_error("cannot validate new block");
Expand All @@ -734,17 +732,21 @@ void ValidateQuery::after_get_mc_state(td::Result<Ref<ShardState>> res) {
*/
void ValidateQuery::got_mc_handle(td::Result<BlockHandle> res) {
LOG(DEBUG) << "in ValidateQuery::got_mc_handle() for " << mc_blkid_.to_str();
--pending;
if (res.is_error()) {
fatal_error(res.move_as_error());
return;
}
auto handle = res.move_as_ok();
if (!handle->inited_proof() && mc_blkid_.seqno()) {
fatal_error(-666, "reference masterchain block "s + mc_blkid_.to_str() + " for block " + id_.to_str() +
" does not have a valid proof");
return;
}
auto mc_handle = res.move_as_ok();
td::actor::send_closure_later(
manager, &ValidatorManager::wait_block_state, mc_handle, priority(), timeout,
[self = get_self(), id = id_, mc_handle](td::Result<Ref<ShardState>> res) {
LOG(DEBUG) << "got answer to wait_block_state() query for masterchain block";
if (res.is_ok() && mc_handle->id().seqno() > 0 && !mc_handle->inited_proof()) {
res = td::Status::Error(-666, "reference masterchain block "s + mc_handle->id().to_str() + " for block " +
id.to_str() + " does not have a valid proof");
}
td::actor::send_closure_later(std::move(self), &ValidateQuery::after_get_mc_state, std::move(res));
});
}

/**
Expand Down Expand Up @@ -778,6 +780,9 @@ void ValidateQuery::after_get_shard_state(int idx, td::Result<Ref<ShardState>> r
return;
}
}
if (is_masterchain()) {
request_latest_mc_state();
}
if (!pending) {
if (!try_validate()) {
fatal_error("cannot validate new block");
Expand Down
1 change: 1 addition & 0 deletions validator/impl/validate-query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class ValidateQuery : public td::actor::Actor {
return actor_id(this);
}

void request_latest_mc_state();
void after_get_latest_mc_state(td::Result<std::pair<Ref<MasterchainState>, BlockIdExt>> res);
void after_get_mc_state(td::Result<Ref<ShardState>> res);
void got_mc_handle(td::Result<BlockHandle> res);
Expand Down
2 changes: 1 addition & 1 deletion validator/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ void ValidatorManagerImpl::written_handle(BlockHandle handle, td::Promise<td::Un
td::actor::send_closure(it->second.actor_, &WaitBlockData::force_read_from_db);
}
}
if (inited_state) {
if (inited_state && inited_proof) {
auto it = wait_state_.find(handle->id());
if (it != wait_state_.end()) {
td::actor::send_closure(it->second.actor_, &WaitBlockState::force_read_from_db);
Expand Down

0 comments on commit 8b68210

Please sign in to comment.