Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 251258c

Browse files
committed
Add custom global configs for hardfork utilites
1 parent 2dc4783 commit 251258c

File tree

2 files changed

+131
-6
lines changed

2 files changed

+131
-6
lines changed

create-hardfork/create-hardfork.cpp

+61-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include "td/utils/filesystem.h"
4444
#include "td/utils/port/path.h"
4545

46+
#include "ton/ton-types.h"
47+
#include "ton/ton-tl.hpp"
48+
#include "ton/ton-io.hpp"
49+
4650
#include "validator/fabric.h"
4751
#include "validator/impl/collator.h"
4852
#include "crypto/vm/cp0.h"
@@ -77,6 +81,8 @@ class HardforkCreator : public td::actor::Actor {
7781
td::actor::ActorOwn<ton::validator::ValidatorManagerInterface> validator_manager_;
7882

7983
std::string db_root_ = "/var/ton-work/db/";
84+
std::string global_config_;
85+
td::Ref<ton::validator::ValidatorManagerOptions> opts_;
8086
td::BufferSlice bs_;
8187
std::vector<td::BufferSlice> ext_msgs_;
8288
std::vector<td::BufferSlice> top_shard_descrs_;
@@ -91,6 +97,9 @@ class HardforkCreator : public td::actor::Actor {
9197
void set_db_root(std::string db_root) {
9298
db_root_ = db_root;
9399
}
100+
void set_global_config_path(std::string path) {
101+
global_config_ = path;
102+
}
94103
void set_shard(ton::ShardIdFull shard) {
95104
LOG(DEBUG) << "setting shard to " << shard.to_str();
96105
shard_ = shard;
@@ -141,6 +150,49 @@ class HardforkCreator : public td::actor::Actor {
141150
void do_save_file() {
142151
}
143152

153+
td::Status create_validator_options() {
154+
if(!global_config_.length()) {
155+
opts_ = ton::validator::ValidatorManagerOptions::create(
156+
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()},
157+
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()});
158+
return td::Status::OK();
159+
}
160+
TRY_RESULT_PREFIX(conf_data, td::read_file(global_config_), "failed to read: ");
161+
TRY_RESULT_PREFIX(conf_json, td::json_decode(conf_data.as_slice()), "failed to parse json: ");
162+
163+
ton::ton_api::config_global conf;
164+
TRY_STATUS_PREFIX(ton::ton_api::from_json(conf, conf_json.get_object()), "json does not fit TL scheme: ");
165+
166+
auto zero_state = ton::create_block_id(conf.validator_->zero_state_);
167+
ton::BlockIdExt init_block;
168+
if (!conf.validator_->init_block_) {
169+
LOG(INFO) << "no init block in config. using zero state";
170+
init_block = zero_state;
171+
} else {
172+
init_block = ton::create_block_id(conf.validator_->init_block_);
173+
}
174+
opts_ = ton::validator::ValidatorManagerOptions::create(zero_state, init_block);
175+
std::vector<ton::BlockIdExt> h;
176+
for (auto &x : conf.validator_->hardforks_) {
177+
auto b = ton::create_block_id(x);
178+
if (!b.is_masterchain()) {
179+
return td::Status::Error(ton::ErrorCode::error,
180+
"[validator/hardforks] section contains not masterchain block id");
181+
}
182+
if (!b.is_valid_full()) {
183+
return td::Status::Error(ton::ErrorCode::error, "[validator/hardforks] section contains invalid block_id");
184+
}
185+
for (auto &y : h) {
186+
if (y.is_valid() && y.seqno() >= b.seqno()) {
187+
y.invalidate();
188+
}
189+
}
190+
h.push_back(b);
191+
}
192+
opts_.write().set_hardforks(std::move(h));
193+
return td::Status::OK();
194+
}
195+
144196
void run() {
145197
td::mkdir(db_root_).ensure();
146198
ton::errorlog::ErrorLog::create(db_root_);
@@ -149,9 +201,13 @@ class HardforkCreator : public td::actor::Actor {
149201
do_save_file();
150202
}
151203

152-
auto opts = ton::validator::ValidatorManagerOptions::create(
153-
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()},
154-
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()});
204+
auto Sr = create_validator_options();
205+
if (Sr.is_error()) {
206+
LOG(ERROR) << "failed to load global config'" << global_config_ << "': " << Sr;
207+
std::_Exit(2);
208+
}
209+
210+
auto opts = opts_;
155211
opts.write().set_initial_sync_disabled(true);
156212
validator_manager_ =
157213
ton::validator::ValidatorManagerHardforkFactory::create(opts, shard_, shard_top_block_id_, db_root_);
@@ -270,6 +326,8 @@ int main(int argc, char *argv[]) {
270326
});
271327
p.add_option('D', "db", "root for dbs",
272328
[&](td::Slice fname) { td::actor::send_closure(x, &HardforkCreator::set_db_root, fname.str()); });
329+
p.add_option('C', "config", "global config path",
330+
[&](td::Slice fname) { td::actor::send_closure(x, &HardforkCreator::set_global_config_path, fname.str()); });
273331
p.add_option('m', "ext-message", "binary file with serialized inbound external message",
274332
[&](td::Slice fname) { td::actor::send_closure(x, &HardforkCreator::load_ext_message, fname.str()); });
275333
p.add_option(

test/test-ton-collator.cpp

+70-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
#include "td/utils/filesystem.h"
4444
#include "td/utils/port/path.h"
4545

46+
#include "ton/ton-types.h"
47+
#include "ton/ton-tl.hpp"
48+
#include "ton/ton-io.hpp"
49+
50+
4651
#include "validator/fabric.h"
4752
#include "validator/impl/collator.h"
4853
#include "crypto/vm/cp0.h"
@@ -76,6 +81,9 @@ class TestNode : public td::actor::Actor {
7681
td::actor::ActorOwn<ton::validator::ValidatorManagerInterface> validator_manager_;
7782

7883
std::string db_root_ = "/var/ton-work/db/";
84+
std::string global_config_;
85+
td::Ref<ton::validator::ValidatorManagerOptions> opts_;
86+
7987
ton::ZeroStateIdExt zero_id_;
8088
td::BufferSlice bs_;
8189
std::vector<td::BufferSlice> ext_msgs_;
@@ -92,6 +100,10 @@ class TestNode : public td::actor::Actor {
92100
void set_db_root(std::string db_root) {
93101
db_root_ = db_root;
94102
}
103+
void set_global_config_path(std::string path) {
104+
global_config_ = path;
105+
}
106+
95107
void set_zero_root_hash(td::Bits256 hash) {
96108
zero_id_.root_hash = hash;
97109
}
@@ -218,6 +230,54 @@ class TestNode : public td::actor::Actor {
218230
}
219231
}
220232

233+
td::Status create_validator_options() {
234+
if(!global_config_.length()) {
235+
LOG(INFO) << "no global config file passed. Using zero-init config";
236+
opts_ = ton::validator::ValidatorManagerOptions::create(
237+
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()},
238+
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, ton::RootHash::zero(), ton::FileHash::zero()});
239+
return td::Status::OK();
240+
}
241+
TRY_RESULT_PREFIX(conf_data, td::read_file(global_config_), "failed to read: ");
242+
TRY_RESULT_PREFIX(conf_json, td::json_decode(conf_data.as_slice()), "failed to parse json: ");
243+
244+
ton::ton_api::config_global conf;
245+
TRY_STATUS_PREFIX(ton::ton_api::from_json(conf, conf_json.get_object()), "json does not fit TL scheme: ");
246+
247+
auto zero_state = ton::create_block_id(conf.validator_->zero_state_);
248+
ton::BlockIdExt init_block;
249+
if (!conf.validator_->init_block_) {
250+
LOG(INFO) << "no init block in config. using zero state";
251+
init_block = zero_state;
252+
} else {
253+
init_block = ton::create_block_id(conf.validator_->init_block_);
254+
}
255+
opts_ = ton::validator::ValidatorManagerOptions::create(zero_state, init_block);
256+
std::vector<ton::BlockIdExt> h;
257+
for (auto &x : conf.validator_->hardforks_) {
258+
auto b = ton::create_block_id(x);
259+
if (!b.is_masterchain()) {
260+
return td::Status::Error(ton::ErrorCode::error,
261+
"[validator/hardforks] section contains not masterchain block id");
262+
}
263+
if (!b.is_valid_full()) {
264+
return td::Status::Error(ton::ErrorCode::error, "[validator/hardforks] section contains invalid block_id");
265+
}
266+
for (auto &y : h) {
267+
if (y.is_valid() && y.seqno() >= b.seqno()) {
268+
y.invalidate();
269+
}
270+
}
271+
h.push_back(b);
272+
}
273+
opts_.write().set_hardforks(std::move(h));
274+
275+
276+
LOG(INFO) << "Hardforks num in config: "<< opts_->get_hardforks().size();
277+
return td::Status::OK();
278+
}
279+
280+
221281
void run() {
222282
zero_id_.workchain = ton::masterchainId;
223283
td::mkdir(db_root_).ensure();
@@ -227,9 +287,14 @@ class TestNode : public td::actor::Actor {
227287
do_save_file();
228288
}
229289

230-
auto opts = ton::validator::ValidatorManagerOptions::create(
231-
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, zero_id_.root_hash, zero_id_.file_hash},
232-
ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, 0, zero_id_.root_hash, zero_id_.file_hash});
290+
auto Sr = create_validator_options();
291+
if (Sr.is_error()) {
292+
LOG(ERROR) << "failed to load global config'" << global_config_ << "': " << Sr;
293+
std::_Exit(2);
294+
}
295+
296+
auto opts = opts_;
297+
233298
opts.write().set_initial_sync_disabled(true);
234299
validator_manager_ = ton::validator::ValidatorManagerDiskFactory::create(ton::PublicKeyHash::zero(), opts, shard_,
235300
shard_top_block_id_, db_root_);
@@ -366,6 +431,8 @@ int main(int argc, char *argv[]) {
366431
[&](td::Slice fname) { td::actor::send_closure(x, &TestNode::set_zero_file, fname.str()); });
367432
p.add_option('D', "db", "root for dbs",
368433
[&](td::Slice fname) { td::actor::send_closure(x, &TestNode::set_db_root, fname.str()); });
434+
p.add_option('C', "config", "global config path",
435+
[&](td::Slice fname) { td::actor::send_closure(x, &TestNode::set_global_config_path, fname.str()); });
369436
p.add_option('m', "ext-message", "binary file with serialized inbound external message",
370437
[&](td::Slice fname) { td::actor::send_closure(x, &TestNode::load_ext_message, fname.str()); });
371438
p.add_option('M', "top-shard-message", "binary file with serialized shard top block description",

0 commit comments

Comments
 (0)