Skip to content

Commit

Permalink
feat: add state_root_hash saving test
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Dec 8, 2022
1 parent 2ac79e1 commit 0c1fb25
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion libraries/common/include/common/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GLOBAL_CONST(h256, EmptyRLPListSHA3);
GLOBAL_CONST(h64, EmptyNonce);
GLOBAL_CONST(u256, ZeroU256);

static const blk_hash_t kNullBlockHash = blk_hash_t(0);
static const blk_hash_t kNullBlockHash = blk_hash_t();

constexpr uint16_t kOnePercent = 100;
constexpr uint16_t kMaxLevelsPerPeriod = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "pbft/proposed_blocks.hpp"
#include "pbft/soft_voted_block_data.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)
#define NULL_BLOCK_HASH blk_hash_t()

namespace taraxa {

Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/consensus/src/dag/dag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp"
#include "transaction/transaction_manager.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)
#define NULL_BLOCK_HASH blk_hash_t()

namespace taraxa {

Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/consensus/src/dag/dag_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp"
#include "transaction/transaction_manager.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)
#define NULL_BLOCK_HASH blk_hash_t()

namespace taraxa {
DagManager::DagManager(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr,
Expand Down
6 changes: 1 addition & 5 deletions libraries/core_libs/consensus/src/pbft/pbft_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ using namespace std;
namespace taraxa {

PbftChain::PbftChain(addr_t node_addr, std::shared_ptr<DbStorage> db)
: head_hash_(blk_hash_t(0)),
size_(0),
non_empty_size_(0),
last_pbft_block_hash_(blk_hash_t(0)),
db_(std::move(db)) {
: head_hash_(blk_hash_t()), size_(0), non_empty_size_(0), last_pbft_block_hash_(blk_hash_t()), db_(std::move(db)) {
LOG_OBJECTS_CREATE("PBFT_CHAIN");
// Get PBFT head from DB
auto pbft_head_str = db_->getPbftHead(head_hash_);
Expand Down
6 changes: 3 additions & 3 deletions libraries/core_libs/storage/src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ std::map<level_t, std::vector<DagBlock>> DbStorage::getNonfinalizedDagBlocks() {
auto i = std::unique_ptr<rocksdb::Iterator>(db_->NewIterator(read_options_, handle(Columns::dag_blocks)));
for (i->SeekToFirst(); i->Valid(); i->Next()) {
DagBlock block(asBytes(i->value().ToString()));
if (block.getPivot() != blk_hash_t(0)) {
if (block.getPivot() != blk_hash_t()) {
res[block.getLevel()].emplace_back(std::move(block));
}
}
Expand Down Expand Up @@ -357,7 +357,7 @@ void DbStorage::updateDagBlockCounters(std::vector<DagBlock> blks) {
insert(write_batch, Columns::dag_blocks_index, toSlice(level), toSlice(blocks_stream.out()));
dag_blocks_count_.fetch_add(1);
// Do not count genesis pivot field
if (blk.getPivot() == blk_hash_t(0)) {
if (blk.getPivot() == blk_hash_t()) {
dag_edge_count_.fetch_add(blk.getTips().size());
} else {
dag_edge_count_.fetch_add(blk.getTips().size() + 1);
Expand Down Expand Up @@ -389,7 +389,7 @@ void DbStorage::saveDagBlock(DagBlock const& blk, Batch* write_batch_p) {
dag_blocks_count_.fetch_add(1);
insert(write_batch, Columns::status, toSlice((uint8_t)StatusDbField::DagBlkCount), toSlice(dag_blocks_count_.load()));
// Do not count genesis pivot field
if (blk.getPivot() == blk_hash_t(0)) {
if (blk.getPivot() == blk_hash_t()) {
dag_edge_count_.fetch_add(blk.getTips().size());
} else {
dag_edge_count_.fetch_add(blk.getTips().size() + 1);
Expand Down
4 changes: 2 additions & 2 deletions libraries/types/vote/include/vote/vote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ class Vote {
* step, the total votes weights must be greater or equal to PBFT 2t+1.
*/
struct VotesBundle {
blk_hash_t voted_block_hash{blk_hash_t(0)};
blk_hash_t voted_block_hash{blk_hash_t()};
PbftPeriod votes_period{0};
std::vector<std::shared_ptr<Vote>> votes; // Greater than 2t+1 votes

VotesBundle() : voted_block_hash(blk_hash_t(0)) {}
VotesBundle() : voted_block_hash(blk_hash_t()) {}
VotesBundle(blk_hash_t const& voted_block_hash, std::vector<std::shared_ptr<Vote>> const& votes)
: voted_block_hash(voted_block_hash), votes(votes) {}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/crypto_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ TEST_F(CryptoTest, DISABLED_compute_vdf_solution_cost_time) {
uint16_t difficulty_min = 0;
uint16_t difficulty_max = 0;
uint16_t lambda_bound = 100;
blk_hash_t proposal_dag_block_pivot_hash1 = blk_hash_t(0);
blk_hash_t proposal_dag_block_pivot_hash1 = blk_hash_t();
blk_hash_t proposal_dag_block_pivot_hash2 =
blk_hash_t("c9524784c4bf29e6facdd94ef7d214b9f512cdfd0f68184432dab85d053cbc69");
blk_hash_t proposal_dag_block_pivot_hash3 =
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 @@ -201,7 +201,7 @@ TEST_F(FullNodeTest, db_test) {
EXPECT_EQ(db.getCertVotedBlockInRound(), std::nullopt);

// pbft_blocks and cert votes
EXPECT_FALSE(db.pbftBlockInDb(blk_hash_t(0)));
EXPECT_FALSE(db.pbftBlockInDb(blk_hash_t()));
EXPECT_FALSE(db.pbftBlockInDb(blk_hash_t(1)));
auto pbft_block1 = make_simple_pbft_block(blk_hash_t(1), 2);
auto pbft_block2 = make_simple_pbft_block(blk_hash_t(2), 3);
Expand Down
5 changes: 2 additions & 3 deletions tests/network_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ TEST_F(NetworkTest, DISABLED_update_peer_chainsize) {
auto nw2 = nodes[1]->getNetwork();

std::vector<vote_hash_t> reward_votes{};
auto pbft_block = std::make_shared<PbftBlock>(blk_hash_t(1), blk_hash_t(0), blk_hash_t(0), blk_hash_t(),
auto pbft_block = std::make_shared<PbftBlock>(blk_hash_t(1), blk_hash_t(), blk_hash_t(), blk_hash_t(),
node1->getPbftManager()->getPbftPeriod(), node1->getAddress(),
node1->getSecretKey(), std::move(reward_votes));
auto vote =
Expand Down Expand Up @@ -935,12 +935,11 @@ TEST_F(NetworkTest, pbft_next_votes_sync_in_same_round_2) {
EXPECT_EQ(node2_pbft_2t_plus_1, 1);

// Node1 generate 1 next vote voted at NULL_BLOCK_HASH
blk_hash_t voted_pbft_block_hash1(blk_hash_t(0));
PbftVoteTypes type = PbftVoteTypes::next_vote;
PbftPeriod period = 1;
PbftRound round = 1;
PbftStep step = 5;
auto vote1 = pbft_mgr1->generateVote(voted_pbft_block_hash1, type, period, round, step);
auto vote1 = pbft_mgr1->generateVote(NULL_BLOCK_HASH, type, period, round, step);
vote1->calculateWeight(1, 1, 1);
std::vector<std::shared_ptr<Vote>> next_votes1{vote1};

Expand Down
2 changes: 1 addition & 1 deletion tests/p2p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TEST_F(P2PTest, block_propagate) {
EXPECT_GT(host1->peer_count(), 0);

DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)},
{g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), blk_hash_t(0),
{g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), blk_hash_t(),
addr_t(999));

SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]};
Expand Down
44 changes: 41 additions & 3 deletions tests/pbft_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ TEST_F(PbftManagerTest, propose_block_and_vote_broadcast) {
std::vector<vote_hash_t> reward_votes_hashes;
std::transform(reward_votes.begin(), reward_votes.end(), std::back_inserter(reward_votes_hashes),
[](const auto &v) { return v->getHash(); });
auto proposed_pbft_block = std::make_shared<PbftBlock>(prev_block_hash, blk_hash_t(0), blk_hash_t(0), blk_hash_t(0),
auto proposed_pbft_block = std::make_shared<PbftBlock>(prev_block_hash, blk_hash_t(), blk_hash_t(), blk_hash_t(),
node1->getPbftManager()->getPbftPeriod(), node1->getAddress(),
node1->getSecretKey(), std::move(reward_votes_hashes));
auto propose_vote = pbft_mgr1->generateVote(proposed_pbft_block->getBlockHash(), PbftVoteTypes::propose_vote,
Expand Down Expand Up @@ -885,7 +885,7 @@ TEST_F(PbftManagerWithDagCreation, DISABLED_pbft_block_is_overweighted) {
generateAndApplyInitialDag();

EXPECT_HAPPENS({10s, 500ms},
[&](auto &ctx) { WAIT_EXPECT_EQ(ctx, nonce, node->getDB()->getNumTransactionExecuted()); });
[&](auto &ctx) { WAIT_EXPECT_EQ(ctx, nonce, node->getDB()->getNumTransactionExecuted() + 1); });

node->getPbftManager()->stop();
// create pbft block
Expand Down Expand Up @@ -932,7 +932,7 @@ TEST_F(PbftManagerWithDagCreation, proposed_blocks) {
// Create blocks
for (uint32_t i = 1; i <= block_count; i++) {
std::vector<vote_hash_t> reward_votes_hashes;
auto block = std::make_shared<PbftBlock>(blk_hash_t(1), blk_hash_t(0), blk_hash_t(0), blk_hash_t(0), 2, addr_t(),
auto block = std::make_shared<PbftBlock>(blk_hash_t(1), blk_hash_t(), blk_hash_t(), blk_hash_t(), 2, addr_t(),
dev::KeyPair::create().secret(), std::move(reward_votes_hashes));
blocks.insert({block->getBlockHash(), block});
}
Expand All @@ -957,6 +957,44 @@ TEST_F(PbftManagerWithDagCreation, proposed_blocks) {
EXPECT_EQ(blocks_from_db.size(), 0);
}

TEST_F(PbftManagerWithDagCreation, state_root_hash) {
makeNode();
deployContract();
node->getDagBlockProposer()->stop();
const auto lambda = node->getConfig().genesis.pbft.lambda_ms;
auto prev_value = node->getDagManager()->getNumVerticesInDag().first;
generateAndApplyInitialDag();
EXPECT_HAPPENS({10s, 250ms}, [&](auto &ctx) {
WAIT_EXPECT_EQ(ctx, node->getDagManager()->getNumVerticesInDag().second, prev_value + getInitialDagSize());
});
// generate dag blocks with delays to distribute them between pbft blocks
for (uint8_t i = 0; i < 5; ++i) {
auto blocks = generateDagBlocks(2, 2, 2);
insertBlocks(std::move(blocks));
std::this_thread::sleep_for(std::chrono::milliseconds(3 * lambda));
}

EXPECT_HAPPENS({5s, 500ms}, [&](auto &ctx) {
WAIT_EXPECT_EQ(ctx, node->getFinalChain()->get_account(node->getAddress())->nonce, nonce);
WAIT_EXPECT_EQ(ctx, node->getDB()->getNumTransactionExecuted(), nonce - 1);
});

const auto &state_root_delay = node_cfgs.front().genesis.pbft.state_root_recording_delay;
const auto &head_hash = node->getPbftChain()->getLastPbftBlockHash();
auto pbft_block = node->getPbftChain()->getPbftBlockInChain(head_hash);
// Check that all produced blocks have correct state_root_hashes
while (pbft_block.getPeriod() != 1) {
auto period = pbft_block.getPeriod();
h256 state_root;
if (period > state_root_delay) {
state_root = node->getFinalChain()->block_header(period - state_root_delay)->state_root;
}
EXPECT_EQ(pbft_block.getPrevStateRoot(), state_root);

pbft_block = node->getPbftChain()->getPbftBlockInChain(pbft_block.getPrevBlockHash());
}
}

} // namespace taraxa::core_tests

using namespace taraxa;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util/include/test_util/test_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ state_api::BalanceMap effective_initial_balances(const state_api::Config& cfg);
u256 own_effective_genesis_bal(const FullNodeConfig& cfg);

std::shared_ptr<PbftBlock> make_simple_pbft_block(const h256& hash, uint64_t period,
const h256& anchor_hash = blk_hash_t(0));
const h256& anchor_hash = blk_hash_t());

std::vector<blk_hash_t> getOrderedDagBlocks(const std::shared_ptr<DbStorage>& db);

Expand Down

0 comments on commit 0c1fb25

Please sign in to comment.