Skip to content

Commit 7164753

Browse files
committed
Store state split depth in persistent state descriptions
1 parent ca80ed7 commit 7164753

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

tl/generate/scheme/ton_api.tl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ db.state.asyncSerializer block:tonNode.blockIdExt last:tonNode.blockIdExt last_t
568568
db.state.hardforks blocks:(vector tonNode.blockIdExt) = db.state.Hardforks;
569569
db.state.dbVersion version:int = db.state.DbVersion;
570570
db.state.persistentStateDescriptionShards shard_blocks:(vector tonNode.blockIdExt) = db.state.PersistentStateDescriptionShards;
571+
db.state.persistentStateDescriptionShard block:tonNode.blockIdExt split_depth:int = db.state.PersistentStateDescriptionShard;
572+
db.state.persistentStateDescriptionShardsV2 shard_blocks:(vector db.state.PersistentStateDescriptionShard) = db.state.PersistentStateDescriptionShards;
571573
db.state.persistentStateDescriptionHeader masterchain_id:tonNode.blockIdExt start_time:int end_time:int = db.state.PersistentStateDescriptionHeader;
572574
db.state.persistentStateDescriptionsList list:(vector db.state.persistentStateDescriptionHeader) = db.state.PersistentStateDescriptionsList;
573575

tl/generate/scheme/ton_api.tlo

368 Bytes
Binary file not shown.

ton/ton-types.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,13 @@ struct ValidatorSessionConfig {
515515
};
516516

517517
struct PersistentStateDescription : public td::CntObject {
518+
struct ShardBlock {
519+
BlockIdExt block;
520+
td::uint32 split_depth;
521+
};
522+
518523
BlockIdExt masterchain_id;
519-
std::vector<BlockIdExt> shard_blocks;
524+
std::vector<ShardBlock> shard_blocks;
520525
UnixTime start_time, end_time;
521526

522527
virtual CntObject* make_copy() const {

validator/db/statedb.cpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,36 @@ void StateDb::add_persistent_state_description(td::Ref<PersistentStateDescriptio
277277
}
278278
list->list_.resize(new_size);
279279

280-
std::vector<tl_object_ptr<ton_api::tonNode_blockIdExt>> shard_blocks;
281-
for (const BlockIdExt& block_id : desc->shard_blocks) {
282-
shard_blocks.push_back(create_tl_block_id(block_id));
280+
bool can_be_stored_as_v1 = true;
281+
for (auto const& [block_id, split_depth] : desc->shard_blocks) {
282+
if (split_depth != 0) {
283+
can_be_stored_as_v1 = false;
284+
break;
285+
}
283286
}
287+
288+
td::BufferSlice serialized_shards;
289+
290+
if (can_be_stored_as_v1) {
291+
std::vector<tl_object_ptr<ton_api::tonNode_blockIdExt>> shard_blocks;
292+
for (auto const& [block_id, _] : desc->shard_blocks) {
293+
shard_blocks.push_back(create_tl_block_id(block_id));
294+
}
295+
serialized_shards =
296+
create_serialize_tl_object<ton_api::db_state_persistentStateDescriptionShards>(std::move(shard_blocks));
297+
} else {
298+
std::vector<tl_object_ptr<ton_api::db_state_persistentStateDescriptionShard>> shard_blocks;
299+
for (auto const& [block_id, split_depth] : desc->shard_blocks) {
300+
shard_blocks.push_back(create_tl_object<ton_api::db_state_persistentStateDescriptionShard>(
301+
create_tl_block_id(block_id), static_cast<td::int32>(split_depth)));
302+
}
303+
serialized_shards =
304+
create_serialize_tl_object<ton_api::db_state_persistentStateDescriptionShardsV2>(std::move(shard_blocks));
305+
}
306+
284307
auto key =
285308
create_hash_tl_object<ton_api::db_state_key_persistentStateDescriptionShards>(desc->masterchain_id.seqno());
286-
kv_->set(key.as_slice(),
287-
create_serialize_tl_object<ton_api::db_state_persistentStateDescriptionShards>(std::move(shard_blocks))
288-
.as_slice())
289-
.ensure();
309+
kv_->set(key.as_slice(), serialized_shards.as_slice()).ensure();
290310

291311
list->list_.push_back(create_tl_object<ton_api::db_state_persistentStateDescriptionHeader>(
292312
create_tl_block_id(desc->masterchain_id), desc->start_time, desc->end_time));
@@ -325,11 +345,26 @@ void StateDb::get_persistent_state_descriptions(td::Promise<std::vector<td::Ref<
325345
if (R2.ok() == td::KeyValue::GetStatus::NotFound) {
326346
continue;
327347
}
328-
auto F2 = fetch_tl_object<ton_api::db_state_persistentStateDescriptionShards>(value, true);
348+
auto F2 = fetch_tl_object<ton_api::db_state_PersistentStateDescriptionShards>(value, true);
329349
F2.ensure();
330-
for (const auto& block_id : F2.ok()->shard_blocks_) {
331-
desc.shard_blocks.push_back(create_block_id(block_id));
332-
}
350+
ton_api::downcast_call(*F2.ok().get(),
351+
td::overloaded(
352+
[&](ton_api::db_state_persistentStateDescriptionShards const& shards) {
353+
for (auto const& block : shards.shard_blocks_) {
354+
desc.shard_blocks.push_back({
355+
.block = create_block_id(block),
356+
.split_depth = 0,
357+
});
358+
}
359+
},
360+
[&](ton_api::db_state_persistentStateDescriptionShardsV2 const& shards) {
361+
for (auto const& shard_description : shards.shard_blocks_) {
362+
desc.shard_blocks.push_back({
363+
.block = create_block_id(shard_description->block_),
364+
.split_depth = static_cast<td::uint32>(shard_description->split_depth_),
365+
});
366+
}
367+
}));
333368
result.push_back(td::Ref<PersistentStateDescription>(true, std::move(desc)));
334369
}
335370
promise.set_result(std::move(result));

validator/manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,7 +3254,7 @@ void ValidatorManagerImpl::add_persistent_state_description(td::Ref<PersistentSt
32543254
while (it != persistent_state_descriptions_.end()) {
32553255
const auto &prev_desc = it->second;
32563256
if (prev_desc->end_time <= now) {
3257-
for (const BlockIdExt &block_id : prev_desc->shard_blocks) {
3257+
for (auto const &[block_id, _] : prev_desc->shard_blocks) {
32583258
persistent_state_blocks_.erase(block_id);
32593259
}
32603260
it = persistent_state_descriptions_.erase(it);
@@ -3271,7 +3271,7 @@ void ValidatorManagerImpl::add_persistent_state_description_impl(td::Ref<Persist
32713271
}
32723272
LOG(DEBUG) << "Add persistent state description for mc block " << desc->masterchain_id.to_str()
32733273
<< " start_time=" << desc->start_time << " end_time=" << desc->end_time;
3274-
for (const BlockIdExt &block_id : desc->shard_blocks) {
3274+
for (auto const &[block_id, _] : desc->shard_blocks) {
32753275
persistent_state_blocks_[block_id] = desc;
32763276
LOG(DEBUG) << "Persistent state description: shard block " << block_id.to_str();
32773277
}

validator/state-serializer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ void AsyncStateSerializer::store_persistent_state_description(td::Ref<Masterchai
251251
desc.start_time = state->get_unix_time();
252252
desc.end_time = ValidatorManager::persistent_state_ttl(desc.start_time);
253253
for (const auto &v : state->get_shards()) {
254-
desc.shard_blocks.push_back(v->top_block_id());
254+
desc.shard_blocks.push_back({
255+
.block = v->top_block_id(),
256+
.split_depth = state->persistent_state_split_depth(v->shard().workchain),
257+
});
255258
}
256259
td::actor::send_closure(manager_, &ValidatorManager::add_persistent_state_description,
257260
td::Ref<PersistentStateDescription>(true, std::move(desc)));

0 commit comments

Comments
 (0)