Skip to content

Commit

Permalink
chore(hardfork): resolve PR discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Jan 19, 2022
1 parent 0f8a367 commit 8ec85af
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 66 deletions.
4 changes: 2 additions & 2 deletions libraries/cli/include/cli/default_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const char *default_json = R"foo({
"deposit_delay": "0x5",
"withdrawal_delay": "0x5",
"eligibility_balance_threshold": "0x186A0",
"coins_per_vote": "0x186A0",
"vote_eligibility_balance_step": "0x186A0",
"genesis_state": {
"0x0274cfffea9fa850e54c93a23042f12a87358a82": {
"0x1f8333245650a19a0683891b7afe7787a3ce9f00": "0x989680",
Expand Down Expand Up @@ -207,7 +207,7 @@ const char *default_json = R"foo({
"111f91441efc8c6c0edf6534970cc887e2fabaa8": "0x24048ce3d000000000000000000"
},
"hardforks": {
"fix_genesis_hardfork_block_num": "0x12c"
"fix_genesis_fork_block": "0x12c"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions libraries/cli/include/cli/devnet_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const char *devnet_json = R"foo({
"deposit_delay": "0x5",
"withdrawal_delay": "0x5",
"eligibility_balance_threshold": "0xf4240",
"coins_per_vote": "0xf4240",
"vote_eligibility_balance_step": "0xf4240",
"genesis_state": {
"0x7e4aa664f71de4e9d0b4a6473d796372639bdcde": {
"0x780fe8b2226cf212c55635de399ee4c2a860810c": "0xf4240",
Expand All @@ -129,7 +129,7 @@ const char *devnet_json = R"foo({
"7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000"
},
"hardforks": {
"fix_genesis_hardfork_block_num": "0x0"
"fix_genesis_fork_block": "0x0"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions libraries/cli/include/cli/testnet_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const char *testnet_json = R"foo({
"deposit_delay": "0x5",
"withdrawal_delay": "0x5",
"eligibility_balance_threshold": "0xf4240",
"coins_per_vote": "0xf4240",
"vote_eligibility_balance_step": "0xf4240",
"genesis_state": {
"0x6c05d6e367a8c798308efbf4cefc1a18921a6f89": {
"0x18551e353aa65bc0ffbdf9d93b7ad4a8fe29cf95": "0xf4240",
Expand Down Expand Up @@ -132,7 +132,7 @@ const char *testnet_json = R"foo({
"f4a52b8f6dc8ab046fec6ad02e77023c044342e4": "0x1027e72f1f12813088000000"
},
"hardforks": {
"fix_genesis_hardfork_block_num": "0x0"
"fix_genesis_fork_block": "0x0"
}
}
},
Expand Down
27 changes: 15 additions & 12 deletions libraries/cli/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,24 @@ Config::Config(int argc, const char* argv[]) {
Json::Value config_json = Tools::readJsonFromFile(config);
Json::Value wallet_json = Tools::readJsonFromFile(wallet);

auto override = [&]() {
auto override_config_file = [&]() {
Tools::writeJsonToFile(config, config_json);
Tools::writeJsonToFile(wallet, wallet_json);
};

network_id = dev::getUInt(config_json["chain_config"]["chain_id"]);
auto default_config_json = Tools::generateConfig((Config::NetworkIdType)network_id);
// override hardforks data with one from default json
addNewHardforks(config_json, default_config_json);
// add coins_per_vote field if it is missing in the config
if (config_json["chain_config"]["final_chain"]["state"]["dpos"]["coins_per_vote"].isNull()) {
config_json["chain_config"]["final_chain"]["state"]["dpos"]["coins_per_vote"] =
default_config_json["chain_config"]["final_chain"]["state"]["dpos"]["coins_per_vote"];
// Check that it is not empty, to not create chain config with just overwritten files
if (!config_json["chain_config"].isNull()) {
network_id = dev::getUInt(config_json["chain_config"]["chain_id"]);
auto default_config_json = Tools::generateConfig((Config::NetworkIdType)network_id);
// override hardforks data with one from default json
addNewHardforks(config_json, default_config_json);
// add vote_eligibility_balance_step field if it is missing in the config
if (config_json["chain_config"]["final_chain"]["state"]["dpos"]["vote_eligibility_balance_step"].isNull()) {
config_json["chain_config"]["final_chain"]["state"]["dpos"]["vote_eligibility_balance_step"] =
default_config_json["chain_config"]["final_chain"]["state"]["dpos"]["vote_eligibility_balance_step"];
}
override_config_file();
}
override();

// Override config values with values from CLI
config_json = Tools::overrideConfig(config_json, data_dir, boot_node, boot_nodes, log_channels, boot_nodes_append,
Expand All @@ -190,7 +193,7 @@ Config::Config(int argc, const char* argv[]) {
// or if running config command
// This can overwrite secret keys in wallet
if (overwrite_config || command[0] == CONFIG_COMMAND) {
override();
override_config_file();
}

// Load config
Expand Down Expand Up @@ -231,12 +234,12 @@ bool Config::nodeConfigured() { return node_configured_; }
FullNodeConfig Config::getNodeConfiguration() { return node_config_; }

void Config::addNewHardforks(Json::Value& config, const Json::Value& default_config) {
// network_id is exactly the same thing as chain_id. So get it from config
auto& new_hardforks_json = default_config["chain_config"]["final_chain"]["state"]["hardforks"];
auto& local_hardforks_json = config["chain_config"]["final_chain"]["state"]["hardforks"];

if (local_hardforks_json.isNull()) {
local_hardforks_json = new_hardforks_json;
return;
}
for (auto itr = new_hardforks_json.begin(); itr != new_hardforks_json.end(); ++itr) {
auto& local = local_hardforks_json[itr.key().asString()];
Expand Down
2 changes: 1 addition & 1 deletion libraries/config/include/config/hardfork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "common/encoding_rlp.hpp"

struct Hardforks {
uint64_t fix_genesis_hardfork_block_num = 0;
uint64_t fix_genesis_fork_block = 0;
void processFixGenesisHardfork() const;
HAS_RLP_FIELDS
};
Expand Down
2 changes: 1 addition & 1 deletion libraries/config/include/config/state_api_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void dec_json(Json::Value const& json, BalanceMap& obj);

struct DPOSConfig {
u256 eligibility_balance_threshold;
u256 coins_per_vote;
u256 vote_eligibility_balance_step;
EthBlockNumber deposit_delay = 0;
EthBlockNumber withdrawal_delay = 0;
std::unordered_map<addr_t, BalanceMap> genesis_state;
Expand Down
2 changes: 1 addition & 1 deletion libraries/config/src/chain_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ decltype(ChainConfig::predefined_) const ChainConfig::predefined_([] {
cfg.final_chain.state.genesis_balances[root_node_addr] = 9007199254740991;
auto& dpos = cfg.final_chain.state.dpos.emplace();
dpos.eligibility_balance_threshold = 1000000000;
dpos.coins_per_vote = 1000000000;
dpos.vote_eligibility_balance_step = 1000000000;
dpos.genesis_state[root_node_addr][root_node_addr] = dpos.eligibility_balance_threshold;
// VDF config
cfg.sortition.vrf.threshold_upper = 0x8000;
Expand Down
8 changes: 4 additions & 4 deletions libraries/config/src/hardfork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Json::Value enc_json(const Hardforks& obj) {
Json::Value json(Json::objectValue);
json["fix_genesis_hardfork_block_num"] = dev::toJS(obj.fix_genesis_hardfork_block_num);
json["fix_genesis_fork_block"] = dev::toJS(obj.fix_genesis_fork_block);
return json;
}

void dec_json(const Json::Value& json, Hardforks& obj) {
if (auto const& e = json["fix_genesis_hardfork_block_num"]) {
obj.fix_genesis_hardfork_block_num = dev::getUInt(e);
if (auto const& e = json["fix_genesis_fork_block"]) {
obj.fix_genesis_fork_block = dev::getUInt(e);
}
}

RLP_FIELDS_DEFINE(Hardforks, fix_genesis_hardfork_block_num)
RLP_FIELDS_DEFINE(Hardforks, fix_genesis_fork_block)
8 changes: 4 additions & 4 deletions libraries/config/src/state_api_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Json::Value enc_json(DPOSConfig const& obj) {
json["eligibility_balance_threshold"] = dev::toJS(obj.eligibility_balance_threshold);
json["deposit_delay"] = dev::toJS(obj.deposit_delay);
json["withdrawal_delay"] = dev::toJS(obj.withdrawal_delay);
json["coins_per_vote"] = dev::toJS(obj.coins_per_vote);
json["vote_eligibility_balance_step"] = dev::toJS(obj.vote_eligibility_balance_step);
auto& genesis_state = json["genesis_state"] = Json::Value(Json::objectValue);
for (auto const& [k, v] : obj.genesis_state) {
genesis_state[dev::toJS(k)] = enc_json(v);
Expand All @@ -83,7 +83,7 @@ void dec_json(Json::Value const& json, DPOSConfig& obj) {
obj.eligibility_balance_threshold = dev::jsToU256(json["eligibility_balance_threshold"].asString());
obj.deposit_delay = dev::jsToInt(json["deposit_delay"].asString());
obj.withdrawal_delay = dev::jsToInt(json["withdrawal_delay"].asString());
obj.coins_per_vote = dev::jsToInt(json["coins_per_vote"].asString());
obj.vote_eligibility_balance_step = dev::jsToInt(json["vote_eligibility_balance_step"].asString());
auto const& genesis_state = json["genesis_state"];
for (auto const& k : genesis_state.getMemberNames()) {
dec_json(genesis_state[k], obj.genesis_state[addr_t(k)]);
Expand All @@ -105,8 +105,8 @@ void dec_json(Json::Value const& json, ExecutionOptions& obj) {
RLP_FIELDS_DEFINE(ExecutionOptions, disable_nonce_check, disable_gas_fee)
RLP_FIELDS_DEFINE(ETHChainConfig, homestead_block, dao_fork_block, eip_150_block, eip_158_block, byzantium_block,
constantinople_block, petersburg_block)
RLP_FIELDS_DEFINE(DPOSConfig, eligibility_balance_threshold, coins_per_vote, deposit_delay, withdrawal_delay,
genesis_state)
RLP_FIELDS_DEFINE(DPOSConfig, eligibility_balance_threshold, vote_eligibility_balance_step, deposit_delay,
withdrawal_delay, genesis_state)
RLP_FIELDS_DEFINE(Config, eth_chain_config, disable_block_rewards, execution_options, genesis_balances, dpos, hardforks)
RLP_FIELDS_DEFINE(Opts, expected_max_trx_per_block, max_trie_full_node_levels_to_cache)
RLP_FIELDS_DEFINE(OptsDB, db_path, disable_most_recent_trie_value_views)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class StateAPI {
uint64_t dpos_eligible_vote_count(EthBlockNumber blk_num, addr_t const& addr) const;

bool dpos_is_eligible(EthBlockNumber blk_num, addr_t const& addr) const;
u256 get_staking_balance(EthBlockNumber blk_num, addr_t const& addr) const;
u256 get_staking_balance(EthBlockNumber blk_num, const addr_t& addr) const;

DPOSQueryResult dpos_query(EthBlockNumber blk_num, DPOSQuery const& q) const;
static addr_t const& dpos_contract_addr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ class FinalChainImpl final : public FinalChain {
precommit_ext(*result, batch);
}
db_->commitWriteBatch(batch, db_opts_w_);
block_finalized_emitter_.emit(result);
state_api_.transition_state_commit();
{
unique_lock l(last_block_mu_);
last_block_ = blk_header;
}
num_executed_dag_blk_ = num_executed_dag_blk;
num_executed_trx_ = num_executed_trx;
block_finalized_emitter_.emit(result);
LOG(log_nf_) << " successful finalize block " << result->hash << " with number " << blk_header->number;
// Creates snapshot if needed
if (db_->createSnapshot(blk_header->number)) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/network/rpc/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Json::Value Test::get_all_nodes() {
return res;
}

Json::Value Test::get_coins_per_vote(const Json::Value &param1) {
Json::Value Test::get_vote_weight(const Json::Value &param1) {
Json::Value res;
try {
if (auto node = full_node_.lock()) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/network/rpc/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Test : public TestFace {
virtual Json::Value get_node_count() override;
virtual Json::Value get_all_peers() override;
virtual Json::Value get_all_nodes() override;
virtual Json::Value get_coins_per_vote(const Json::Value& param1) override;
virtual Json::Value get_vote_weight(const Json::Value& param1) override;
virtual Json::Value place_vote(const Json::Value& param1) override;
virtual Json::Value get_votes(const Json::Value& param1) override;
virtual Json::Value draw_graph(const Json::Value& param1) override;
Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/network/rpc/Test.jsonrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"returns": {}
},
{
"name": "get_coins_per_vote",
"name": "get_vote_weight",
"params": [
{}
],
Expand Down
4 changes: 2 additions & 2 deletions libraries/core_libs/network/rpc/TestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ class TestClient : public jsonrpc::Client {
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value get_coins_per_vote(const Json::Value& param1) throw(jsonrpc::JsonRpcException) {
Json::Value get_vote_weight(const Json::Value& param1) throw(jsonrpc::JsonRpcException) {
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("get_coins_per_vote", p);
Json::Value result = this->CallMethod("get_vote_weight", p);
if (result.isObject())
return result;
else
Expand Down
10 changes: 5 additions & 5 deletions libraries/core_libs/network/rpc/TestFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class TestFace : public ServerInterface<TestFace> {
&taraxa::net::TestFace::get_all_peersI);
this->bindAndAddMethod(jsonrpc::Procedure("get_all_nodes", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, NULL),
&taraxa::net::TestFace::get_all_nodesI);
this->bindAndAddMethod(jsonrpc::Procedure("get_coins_per_vote", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT,
this->bindAndAddMethod(jsonrpc::Procedure("get_vote_weight", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT,
"param1", jsonrpc::JSON_OBJECT, NULL),
&taraxa::net::TestFace::get_coins_per_voteI);
&taraxa::net::TestFace::get_vote_weightI);
this->bindAndAddMethod(jsonrpc::Procedure("place_vote", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",
jsonrpc::JSON_OBJECT, NULL),
&taraxa::net::TestFace::place_voteI);
Expand Down Expand Up @@ -140,8 +140,8 @@ class TestFace : public ServerInterface<TestFace> {
(void)request;
response = this->get_all_nodes();
}
inline virtual void get_coins_per_voteI(const Json::Value &request, Json::Value &response) {
response = this->get_coins_per_vote(request[0u]);
inline virtual void get_vote_weightI(const Json::Value &request, Json::Value &response) {
response = this->get_vote_weight(request[0u]);
}
inline virtual void place_voteI(const Json::Value &request, Json::Value &response) {
response = this->place_vote(request[0u]);
Expand Down Expand Up @@ -192,7 +192,7 @@ class TestFace : public ServerInterface<TestFace> {
virtual Json::Value get_node_version() = 0;
virtual Json::Value get_all_peers() = 0;
virtual Json::Value get_all_nodes() = 0;
virtual Json::Value get_coins_per_vote(const Json::Value &param1) = 0;
virtual Json::Value get_vote_weight(const Json::Value &param1) = 0;
virtual Json::Value place_vote(const Json::Value &param1) = 0;
virtual Json::Value get_votes(const Json::Value &param1) = 0;
virtual Json::Value draw_graph(const Json::Value &param1) = 0;
Expand Down
10 changes: 0 additions & 10 deletions libraries/core_libs/node/include/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,4 @@ class FullNode : public std::enable_shared_from_this<FullNode> {
void rebuildDb();
};

class HardforkExecutors {
using ForkExecutor = std::function<void(FullNode &)>;

public:
void registerExecutor(uint64_t block, ForkExecutor exec);

private:
std::map<uint64_t, ForkExecutor> executors;
};

} // namespace taraxa
4 changes: 2 additions & 2 deletions libraries/core_libs/node/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void FullNode::start() {
final_chain_->block_applying.subscribe([&](uint64_t block_num) {
// TODO: should have only common hardfork code calling hardfork executor
auto &state_conf = conf_.chain.final_chain.state;
if (state_conf.hardforks.fix_genesis_hardfork_block_num == block_num) {
if (state_conf.hardforks.fix_genesis_fork_block == block_num) {
for (auto &e : state_conf.dpos->genesis_state) {
for (auto &b : e.second) {
b.second = b.second * kOneTara;
Expand All @@ -202,7 +202,7 @@ void FullNode::start() {
// we are multiplying it by TARA precision
state_conf.dpos->eligibility_balance_threshold *= kOneTara;
// amount of stake per vote should be 10 times smaller than eligibility threshold
state_conf.dpos->coins_per_vote = state_conf.dpos->eligibility_balance_threshold;
state_conf.dpos->vote_eligibility_balance_step = state_conf.dpos->eligibility_balance_threshold;
state_conf.dpos->eligibility_balance_threshold *= 10;

final_chain_->update_state_config(state_conf);
Expand Down
2 changes: 1 addition & 1 deletion tests/final_chain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ TEST_F(FinalChainTest, genesis_balances) {
cfg.state.genesis_balances[addr_t::random()] = 1000;
cfg.state.genesis_balances[addr_t::random()] = 100000;
init();
cfg.state.hardforks.fix_genesis_hardfork_block_num = 2222222;
cfg.state.hardforks.fix_genesis_fork_block = 2222222;
SUT->update_state_config(cfg.state);
}

Expand Down
18 changes: 9 additions & 9 deletions tests/full_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ TEST_F(FullNodeTest, chain_config_json) {
"dpos": {
"deposit_delay": "0x0",
"eligibility_balance_threshold": "0x3b9aca00",
"coins_per_vote": "0x3b9aca00",
"vote_eligibility_balance_step": "0x3b9aca00",
"genesis_state": {
"0xde2b1203d72d3549ee2f733b00b2789414c7cea5": {
"0xde2b1203d72d3549ee2f733b00b2789414c7cea5": "0x3b9aca00"
Expand All @@ -1486,7 +1486,7 @@ TEST_F(FullNodeTest, chain_config_json) {
"0xde2b1203d72d3549ee2f733b00b2789414c7cea5": "0x1fffffffffffff"
},
"hardforks": {
"fix_genesis_hardfork_block_num": "0x0"
"fix_genesis_fork_block": "0x0"
}
}
},
Expand Down Expand Up @@ -1536,9 +1536,9 @@ TEST_F(FullNodeTest, hardfork_override) {
auto default_hardforks = default_json["chain_config"]["final_chain"]["state"]["hardforks"];
Json::Value config = default_json;
auto &state_cfg = config["chain_config"]["final_chain"]["state"];
state_cfg["hardforks"].removeMember("fix_genesis_hardfork_block_num");
state_cfg["hardforks"].removeMember("fix_genesis_fork_block");

EXPECT_TRUE(state_cfg["hardforks"]["fix_genesis_hardfork_block_num"].isNull());
EXPECT_TRUE(state_cfg["hardforks"]["fix_genesis_fork_block"].isNull());
cli::Config::addNewHardforks(config, default_json);
EXPECT_EQ(state_cfg["hardforks"], default_hardforks);

Expand All @@ -1549,19 +1549,19 @@ TEST_F(FullNodeTest, hardfork_override) {
EXPECT_EQ(state_cfg["hardforks"], default_hardforks);
}

TEST_F(FullNodeTest, fix_genesis_hardfork_block_num_is_zero) {
TEST_F(FullNodeTest, fix_genesis_fork_block_is_zero) {
auto node_cfgs = make_node_cfgs(1);
auto &cfg = node_cfgs.front().chain.final_chain;
cfg.state.hardforks.fix_genesis_hardfork_block_num = 0;
cfg.state.hardforks.fix_genesis_fork_block = 0;
auto node = launch_nodes(node_cfgs).front();
}

TEST_F(FullNodeTest, hardfork) {
auto node_cfgs = make_node_cfgs(1);
auto &cfg = node_cfgs.front().chain.final_chain;
cfg.state.hardforks.fix_genesis_hardfork_block_num = 2;
cfg.state.hardforks.fix_genesis_fork_block = 2;
cfg.state.dpos->eligibility_balance_threshold = 100000;
cfg.state.dpos->coins_per_vote = cfg.state.dpos->eligibility_balance_threshold;
cfg.state.dpos->vote_eligibility_balance_step = cfg.state.dpos->eligibility_balance_threshold;
for (auto &gs : cfg.state.dpos->genesis_state) {
for (auto &b : gs.second) {
b.second = 1100000;
Expand All @@ -1580,7 +1580,7 @@ TEST_F(FullNodeTest, hardfork) {
dummy_trx();
node->getFinalChain()->block_finalized_.subscribe([&](const std::shared_ptr<final_chain::FinalizationResult> &res) {
const auto block_num = res->final_chain_blk->number;
if (cfg.state.hardforks.fix_genesis_hardfork_block_num == block_num) {
if (cfg.state.hardforks.fix_genesis_fork_block == block_num) {
hardfork_applied = true;
return;
}
Expand Down
Loading

0 comments on commit 8ec85af

Please sign in to comment.