Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge develop to testnet #2885

Merged
merged 13 commits into from
Nov 8, 2024
Merged
2 changes: 1 addition & 1 deletion libraries/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(HEADERS
include/common/constants.hpp
include/common/static_init.hpp
include/common/init.hpp
include/common/types.hpp
include/common/config_exception.hpp
include/common/vrf_wrapper.hpp
Expand Down
1 change: 1 addition & 0 deletions libraries/common/include/common/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const uint64_t kMinTxGas{21000};
constexpr uint32_t kMinTransactionPoolSize{30000};
constexpr uint32_t kDefaultTransactionPoolSize{200000};
constexpr uint32_t kMaxNonFinalizedTransactions{1000000};
constexpr uint32_t kMaxNonFinalizedDagBlocks{100};

const size_t kV3NetworkVersion = 3;

Expand Down
36 changes: 36 additions & 0 deletions libraries/common/include/common/init.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <sodium/core.h>
#include <sys/statvfs.h>

#include "common/util.hpp"

namespace taraxa {

inline void static_init() {
if (sodium_init() == -1) {
throw std::runtime_error("libsodium init failure");
}
}

inline bool checkDiskSpace(const std::string& path, uint64_t required_space_MB) {
// Convert MB to bytes
const uint64_t required_space_bytes = required_space_MB * 1024 * 1024;

struct statvfs stat;

// Get file system statistics
if (statvfs(path.c_str(), &stat) != 0) {
// If statvfs fails, return true
std::cerr << "Error getting file system stats" << std::endl;
return true;
}

// Calculate available space
const uint64_t available_space = stat.f_bsize * stat.f_bavail;

// Check if available space is greater than or equal to the required space
return available_space >= required_space_bytes;
}

} // namespace taraxa
15 changes: 0 additions & 15 deletions libraries/common/include/common/static_init.hpp

This file was deleted.

3 changes: 3 additions & 0 deletions libraries/core_libs/consensus/include/dag/dag_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class DagManager : public std::enable_shared_from_this<DagManager> {
*/
std::pair<size_t, size_t> getNonFinalizedBlocksSize() const;

uint32_t getNonFinalizedBlocksMinDifficulty() const;

util::Event<DagManager, DagBlock> const block_verified_{};

/**
Expand Down Expand Up @@ -258,6 +260,7 @@ class DagManager : public std::enable_shared_from_this<DagManager> {
blk_hash_t old_anchor_; // anchor of the second to last period
PbftPeriod period_; // last period
std::map<uint64_t, std::unordered_set<blk_hash_t>> non_finalized_blks_;
uint32_t non_finalized_blks_min_difficulty_ = UINT32_MAX;
DagFrontier frontier_;
SortitionParamsManager sortition_params_manager_;
const DagConfig &dag_config_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class PillarChainManager {
* @param pillar_block
* @param new_vote_counts
*/
void saveNewPillarBlock(std::shared_ptr<PillarBlock> pillar_block,
void saveNewPillarBlock(const std::shared_ptr<PillarBlock>& pillar_block,
std::vector<state_api::ValidatorVoteCount>&& new_vote_counts);

private:
Expand Down
11 changes: 11 additions & 0 deletions libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ bool DagBlockProposer::proposeDagBlock() {
vdf_sortition::VdfSortition vdf(sortition_params, vrf_sk_,
VrfSortitionBase::makeVrfInput(propose_level, period_block_hash), vote_count,
max_vote_count);

auto anchor = dag_mgr_->getAnchors().second;
if (frontier.pivot != anchor) {
if (dag_mgr_->getNonFinalizedBlocksSize().second > kMaxNonFinalizedDagBlocks) {
return false;
}
if (dag_mgr_->getNonFinalizedBlocksMinDifficulty() < vdf.getDifficulty()) {
return false;
}
}

if (vdf.isStale(sortition_params)) {
if (last_propose_level_ == propose_level) {
if (num_tries_ < max_num_tries_) {
Expand Down
12 changes: 12 additions & 0 deletions libraries/core_libs/consensus/src/dag/dag_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ std::pair<bool, std::vector<blk_hash_t>> DagManager::addDagBlock(DagBlock &&blk,
max_level_ = std::max(current_max_level, blk.getLevel());

addToDag(blk_hash, pivot_hash, tips, blk.getLevel());
if (non_finalized_blks_min_difficulty_ > blk.getDifficulty()) {
non_finalized_blks_min_difficulty_ = blk.getDifficulty();
}

updateFrontier();
}
Expand Down Expand Up @@ -361,6 +364,7 @@ uint DagManager::setDagBlockOrder(blk_hash_t const &new_anchor, PbftPeriod perio
std::unordered_map<blk_hash_t, std::shared_ptr<DagBlock>> expired_dag_blocks_to_remove;
std::vector<trx_hash_t> expired_dag_blocks_transactions;

non_finalized_blks_min_difficulty_ = UINT32_MAX;
for (auto &v : non_finalized_blocks) {
for (auto &blk_hash : v.second) {
if (dag_order_set.count(blk_hash) != 0) {
Expand All @@ -372,6 +376,9 @@ uint DagManager::setDagBlockOrder(blk_hash_t const &new_anchor, PbftPeriod perio

if (validateBlockNotExpired(dag_block, expired_dag_blocks_to_remove)) {
addToDag(blk_hash, pivot_hash, dag_block->getTips(), dag_block->getLevel(), false);
if (non_finalized_blks_min_difficulty_ > dag_block->getDifficulty()) {
non_finalized_blks_min_difficulty_ = dag_block->getDifficulty();
}
} else {
db_->removeDagBlock(blk_hash);
seen_blocks_.erase(blk_hash);
Expand Down Expand Up @@ -572,6 +579,11 @@ DagManager::getNonFinalizedBlocksWithTransactions(const std::unordered_set<blk_h
return {period_, std::move(dag_blocks), std::move(trxs)};
}

uint32_t DagManager::getNonFinalizedBlocksMinDifficulty() const {
std::shared_lock lock(mutex_);
return non_finalized_blks_min_difficulty_;
}

std::pair<size_t, size_t> DagManager::getNonFinalizedBlocksSize() const {
std::shared_lock lock(mutex_);

Expand Down
11 changes: 11 additions & 0 deletions libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ PbftManager::PbftManager(const FullNodeConfig &conf, std::shared_ptr<DbStorage>
const auto &node_addr = node_addr_;
LOG_OBJECTS_CREATE("PBFT_MGR");

auto current_pbft_period = pbft_chain_->getPbftChainSize();
if (kGenesisConfig.state.hardforks.ficus_hf.isPillarBlockPeriod(current_pbft_period)) {
const auto current_pillar_block = pillar_chain_mgr_->getCurrentPillarBlock();
// There is a race condition where pbt block could have been saved and node stopped before saving pillar block
if (current_pbft_period ==
current_pillar_block->getPeriod() + kGenesisConfig.state.hardforks.ficus_hf.pillar_blocks_interval)
LOG(log_er_) << "Pillar block was not processed before restart, current period: " << current_pbft_period
<< ", current pillar block period: " << current_pillar_block->getPeriod();
processPillarBlock(current_pbft_period);
}

for (auto period = final_chain_->lastBlockNumber() + 1, curr_period = pbft_chain_->getPbftChainSize();
period <= curr_period; ++period) {
auto period_raw = db_->getPeriodDataRaw(period);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,12 @@ std::shared_ptr<PillarBlock> PillarChainManager::createPillarBlock(
return pillar_block;
}

void PillarChainManager::saveNewPillarBlock(std::shared_ptr<PillarBlock> pillar_block,
void PillarChainManager::saveNewPillarBlock(const std::shared_ptr<PillarBlock>& pillar_block,
std::vector<state_api::ValidatorVoteCount>&& new_vote_counts) {
CurrentPillarBlockDataDb data{std::move(pillar_block), std::move(new_vote_counts)};
db_->saveCurrentPillarBlockData(data);

std::scoped_lock<std::shared_mutex> lock(mutex_);
current_pillar_block_ = std::move(data.pillar_block);
current_pillar_block_vote_counts_ = std::move(data.vote_counts);
db_->saveCurrentPillarBlockData({pillar_block, new_vote_counts});
current_pillar_block_ = pillar_block;
current_pillar_block_vote_counts_ = std::move(new_vote_counts);
}

std::shared_ptr<PillarVote> PillarChainManager::genAndPlacePillarVote(PbftPeriod period,
Expand Down Expand Up @@ -333,7 +331,6 @@ bool PillarChainManager::isValidPillarBlock(const std::shared_ptr<PillarBlock>&
}

const auto last_finalized_pillar_block = getLastFinalizedPillarBlock();
std::shared_lock<std::shared_mutex> lock(mutex_);
assert(last_finalized_pillar_block);

// Check if some block was not skipped
Expand Down
10 changes: 8 additions & 2 deletions programs/taraxad/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
#include <condition_variable>

#include "cli/config.hpp"
#include "cli/tools.hpp"
#include "common/config_exception.hpp"
#include "common/static_init.hpp"
#include "common/init.hpp"
#include "node/node.hpp"

using namespace taraxa;

int main(int argc, const char* argv[]) {
static_init();

if (!checkDiskSpace(cli::tools::getTaraxaDefaultConfigFile(), 10)) {
std::cerr << "Insufficient disk space" << std::endl;
return 1;
}

try {
cli::Config cli_conf(argc, argv);

if (cli_conf.nodeConfigured()) {
auto node = std::make_shared<FullNode>(cli_conf.getNodeConfiguration());
node->start();
Expand Down
2 changes: 1 addition & 1 deletion tests/crypto_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <iostream>
#include <string>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "common/vrf_wrapper.hpp"
#include "logger/logger.hpp"
#include "test_util/gtest.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/dag_block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iostream>
#include <vector>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "common/types.hpp"
#include "common/util.hpp"
#include "dag/dag.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/dag_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "common/types.hpp"
#include "dag/dag_manager.hpp"
#include "logger/logger.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/full_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "cli/config.hpp"
#include "cli/tools.hpp"
#include "common/constants.hpp"
#include "common/static_init.hpp"
#include "common/init.hpp"
#include "dag/dag_block_proposer.hpp"
#include "dag/dag_manager.hpp"
#include "graphql/mutation.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/network_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <iostream>
#include <vector>

#include "common/init.hpp"
#include "common/lazy.hpp"
#include "common/static_init.hpp"
#include "config/config.hpp"
#include "dag/dag.hpp"
#include "dag/dag_block_proposer.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/p2p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <vector>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "logger/logger.hpp"
#include "network/tarcap/tarcap_version.hpp"
#include "test_util/samples.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/pbft_chain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iostream>
#include <vector>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "logger/logger.hpp"
#include "network/network.hpp"
#include "pbft/pbft_manager.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/pbft_manager_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "logger/logger.hpp"
#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp"
#include "test_util/node_dag_creation_fixture.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/pillar_chain_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>

#include "common/encoding_solidity.hpp"
#include "common/static_init.hpp"
#include "common/init.hpp"
#include "logger/logger.hpp"
#include "pbft/pbft_manager.hpp"
#include "pillar_chain/pillar_chain_manager.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util/gtest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <filesystem>

#include "common/init.hpp"
#include "common/lazy.hpp"
#include "common/static_init.hpp"
#include "config/config.hpp"

namespace fs = std::filesystem;
Expand Down
2 changes: 1 addition & 1 deletion tests/transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <thread>
#include <vector>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "config/genesis.hpp"
#include "final_chain/final_chain.hpp"
#include "final_chain/trie_common.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/vote_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <libdevcore/SHA3.h>

#include "common/static_init.hpp"
#include "common/init.hpp"
#include "logger/logger.hpp"
#include "network/network.hpp"
#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp"
Expand Down
Loading