1
0
mirror of https://github.com/danog/ton.git synced 2024-12-12 09:09:37 +01:00
ton/validator/db/statedb.cpp
2019-09-07 14:33:36 +04:00

192 lines
6.5 KiB
C++

/*
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 <http://www.gnu.org/licenses/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#include "statedb.hpp"
#include "ton/ton-tl.hpp"
#include "adnl/utils.hpp"
#include "td/db/RocksDb.h"
#include "ton/ton-shard.h"
namespace ton {
namespace validator {
void StateDb::update_init_masterchain_block(BlockIdExt block, td::Promise<td::Unit> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_initBlockId>();
kv_->begin_transaction().ensure();
kv_->set(key.as_slice(),
create_serialize_tl_object<ton_api::db_state_initBlockId>(create_tl_block_id(block)).as_slice())
.ensure();
kv_->commit_transaction().ensure();
promise.set_value(td::Unit());
}
void StateDb::get_init_masterchain_block(td::Promise<BlockIdExt> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_initBlockId>();
std::string value;
auto R = kv_->get(key.as_slice(), value);
R.ensure();
if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) {
promise.set_error(td::Status::Error(ErrorCode::notready, "not found"));
return;
}
auto F = fetch_tl_object<ton_api::db_state_initBlockId>(td::BufferSlice{value}, true);
F.ensure();
auto obj = F.move_as_ok();
promise.set_value(create_block_id(obj->block_));
}
void StateDb::update_gc_masterchain_block(BlockIdExt block, td::Promise<td::Unit> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_gcBlockId>();
kv_->begin_transaction().ensure();
kv_->set(key.as_slice(),
create_serialize_tl_object<ton_api::db_state_gcBlockId>(create_tl_block_id(block)).as_slice())
.ensure();
kv_->commit_transaction().ensure();
promise.set_value(td::Unit());
}
void StateDb::get_gc_masterchain_block(td::Promise<BlockIdExt> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_gcBlockId>();
std::string value;
auto R = kv_->get(key.as_slice(), value);
R.ensure();
if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) {
promise.set_error(td::Status::Error(ErrorCode::notready, "not found"));
return;
}
auto F = fetch_tl_object<ton_api::db_state_gcBlockId>(td::BufferSlice{value}, true);
F.ensure();
auto obj = F.move_as_ok();
promise.set_value(create_block_id(obj->block_));
}
void StateDb::update_shard_client_state(BlockIdExt masterchain_block_id, td::Promise<td::Unit> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_shardClient>();
kv_->begin_transaction().ensure();
kv_->set(key.as_slice(),
create_serialize_tl_object<ton_api::db_state_shardClient>(create_tl_block_id(masterchain_block_id)))
.ensure();
kv_->commit_transaction().ensure();
promise.set_value(td::Unit());
}
void StateDb::get_shard_client_state(td::Promise<BlockIdExt> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_shardClient>();
std::string value;
auto R = kv_->get(key.as_slice(), value);
R.ensure();
if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) {
promise.set_error(td::Status::Error(ErrorCode::notready, "not found"));
return;
}
auto F = fetch_tl_object<ton_api::db_state_shardClient>(td::BufferSlice{value}, true);
F.ensure();
auto obj = F.move_as_ok();
promise.set_value(create_block_id(obj->block_));
}
void StateDb::update_destroyed_validator_sessions(std::vector<ValidatorSessionId> sessions,
td::Promise<td::Unit> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_destroyedSessions>();
kv_->begin_transaction().ensure();
kv_->set(key.as_slice(), create_serialize_tl_object<ton_api::db_state_destroyedSessions>(std::move(sessions)))
.ensure();
kv_->commit_transaction().ensure();
promise.set_value(td::Unit());
}
void StateDb::get_destroyed_validator_sessions(td::Promise<std::vector<ValidatorSessionId>> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_destroyedSessions>();
std::string value;
auto R = kv_->get(key.as_slice(), value);
R.ensure();
if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) {
promise.set_value(std::vector<ValidatorSessionId>{});
return;
}
auto F = fetch_tl_object<ton_api::db_state_destroyedSessions>(td::BufferSlice{value}, true);
F.ensure();
auto obj = F.move_as_ok();
promise.set_value(std::move(obj->sessions_));
}
void StateDb::update_async_serializer_state(AsyncSerializerState state, td::Promise<td::Unit> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_asyncSerializer>();
auto value = create_serialize_tl_object<ton_api::db_state_asyncSerializer>(
create_tl_block_id(state.last_block_id), create_tl_block_id(state.last_written_block_id),
state.last_written_block_ts);
kv_->begin_transaction().ensure();
kv_->set(key.as_slice(), value.as_slice()).ensure();
kv_->commit_transaction().ensure();
promise.set_value(td::Unit());
}
void StateDb::get_async_serializer_state(td::Promise<AsyncSerializerState> promise) {
auto key = create_hash_tl_object<ton_api::db_state_key_asyncSerializer>();
std::string value;
auto R = kv_->get(key.as_slice(), value);
R.ensure();
if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) {
promise.set_value(AsyncSerializerState{BlockIdExt{}, BlockIdExt{}, 0});
return;
}
auto F = fetch_tl_object<ton_api::db_state_asyncSerializer>(td::BufferSlice{value}, true);
F.ensure();
auto obj = F.move_as_ok();
promise.set_value(AsyncSerializerState{create_block_id(obj->block_), create_block_id(obj->last_),
static_cast<UnixTime>(obj->last_ts_)});
}
StateDb::StateDb(td::actor::ActorId<RootDb> root_db, std::string db_path) : root_db_(root_db), db_path_(db_path) {
}
void StateDb::start_up() {
kv_ = std::make_shared<td::RocksDb>(td::RocksDb::open(db_path_).move_as_ok());
}
} // namespace validator
} // namespace ton