Skip to content

Commit

Permalink
replace dummy propagate_block test with real one
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubFornadel committed Jun 5, 2023
1 parent 2da0310 commit a4e385e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 138 deletions.
68 changes: 59 additions & 9 deletions tests/network_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ auto g_secret = Lazy([] {
return dev::Secret("3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd",
dev::Secret::ConstructFromStringType::FromHex);
});
auto node_key = dev::KeyPair(g_secret);
auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, NUM_TRX, g_secret); });

struct NetworkTest : NodesTest {};
Expand Down Expand Up @@ -106,6 +105,51 @@ TEST_F(NetworkTest, transfer_lot_of_blocks) {
wait({120s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr2->getDagBlock(block_hash), nullptr) });
}

// TODO: debug why the test take so long...
TEST_F(NetworkTest, propagate_block) {
auto node_cfgs = make_node_cfgs(5, 1);
auto nodes = launch_nodes(node_cfgs);
const auto& node1 = nodes[0];

// Stop PBFT manager
for (auto& node : nodes) {
node->getPbftManager()->stop();
}

const auto db1 = node1->getDB();
const auto dag_mgr1 = node1->getDagManager();

auto trxs = samples::createSignedTrxSamples(0, 1, g_secret);
const auto estimation = node1->getTransactionManager()->estimateTransactionGas(trxs[0], {});

// node1 add one valid block
const auto proposal_level = 1;
const auto proposal_period = *db1->getProposalPeriodForDagLevel(proposal_level);
const auto period_block_hash = db1->getPeriodBlockHash(proposal_period);
const auto sortition_params = dag_mgr1->sortitionParamsManager().getSortitionParams(proposal_period);
vdf_sortition::VdfSortition vdf(sortition_params, node1->getVrfSecretKey(),
VrfSortitionBase::makeVrfInput(proposal_level, period_block_hash), 1, 1);
const auto dag_genesis = node1->getConfig().genesis.dag_genesis_block.getHash();
dev::bytes vdf_msg = DagManager::getVdfMessage(dag_genesis, {trxs[0]});
vdf.computeVdfSolution(sortition_params, vdf_msg, false);
DagBlock blk(dag_genesis, proposal_level, {}, {trxs[0]->getHash()}, estimation, vdf, node1->getSecretKey());

const auto block_hash = blk.getHash();

// Add block gossip it to connected peers
dag_mgr1->addDagBlock(std::move(blk), {trxs[0]});

wait({1s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr1->getDagBlock(block_hash), nullptr) });

std::cout << "Waiting Sync ..." << std::endl;
wait({20s, 200ms}, [&](auto& ctx) {
for (const auto& node : nodes) {
const auto dag_mgr = node->getDagManager();
WAIT_EXPECT_NE(ctx, dag_mgr->getDagBlock(block_hash), nullptr)
}
});
}

TEST_F(NetworkTest, DISABLED_update_peer_chainsize) {
auto node_cfgs = make_node_cfgs(2, 1, 5);
auto nodes = launch_nodes(node_cfgs);
Expand Down Expand Up @@ -278,10 +322,17 @@ TEST_F(NetworkTest, sync_large_pbft_block) {
// Test creates two Network setup and verifies sending transaction
// between is successfull
TEST_F(NetworkTest, transfer_transaction) {
auto nw1 = std::make_unique<Network>(node_cfgs[0]);
auto nw2 = std::make_unique<Network>(node_cfgs[1]);
nw1->start();
nw2->start();
auto node_cfgs = make_node_cfgs(2, 0, 20);
auto nodes = launch_nodes(node_cfgs);
const auto& node1 = nodes[0];
const auto& node2 = nodes[1];

// Stop PBFT manager
node1->getPbftManager()->stop();
node2->getPbftManager()->stop();

const auto nw1 = node1->getNetwork();
const auto nw2 = node2->getNetwork();

EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) {
nw1->setPendingPeersToReady();
Expand All @@ -300,13 +351,12 @@ TEST_F(NetworkTest, transfer_transaction) {

SharedTransactions transactions;
transactions.push_back(g_signed_trx_samples[0]);
transactions.push_back(g_signed_trx_samples[1]);
transactions.push_back(g_signed_trx_samples[2]);

nw2->getSpecificHandler<network::tarcap::TransactionPacketHandler>()->sendTransactions(peer1,
std::move(transactions));

EXPECT_HAPPENS({2s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_EQ(ctx, nw1->getReceivedTransactionsCount(), 3) });
const auto tx_mgr1 = node1->getTransactionManager();
EXPECT_HAPPENS({2s, 200ms},
[&](auto& ctx) { WAIT_EXPECT_TRUE(ctx, tx_mgr1->getTransaction(g_signed_trx_samples[0]->getHash())) });
}

// Test verifies saving network to a file and restoring it from a file
Expand Down
129 changes: 0 additions & 129 deletions tests/p2p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,135 +97,6 @@ TEST_F(P2PTest, p2p_discovery) {
});
}

/*
Test creates 50 host/network/capability which connect to each other
using node discovery. Block is created on one host and automatically
propagated to all other hosts. Test verifies that each node has received
the block
*/
//TEST_F(P2PTest, block_propagate) {
// int const nodeCount = 10;
// const char *const localhost = "127.0.0.1";
// dev::p2p::NetworkConfig prefs1(localhost, 10007, false, true);
// std::vector<dev::p2p::NetworkConfig> vPrefs;
// for (int i = 0; i < nodeCount; i++) {
// vPrefs.push_back(dev::p2p::NetworkConfig(localhost, 10007 + i + 1, false, true));
// }
// TaraxaNetworkConfig taraxa_net_conf_1;
// taraxa_net_conf_1.is_boot_node = true;
//
// FullNodeConfig conf;
// conf.network.transaction_interval_ms = 1000;
// h256 genesis;
// std::shared_ptr<taraxa::network::tarcap::TaraxaCapability> thc1;
// auto host1 = Host::make(
// "Test",
// [&](auto host) {
// thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION);
// return Host::CapabilityList{thc1};
// },
// KeyPair::create(), prefs1, taraxa_net_conf_1);
//
// util::ThreadPool tp;
// tp.post_loop({}, [=] { host1->do_work(); });
// std::vector<std::shared_ptr<Host>> vHosts;
// std::vector<std::shared_ptr<taraxa::network::tarcap::TaraxaCapability>> vCapabilities;
// for (int i = 0; i < nodeCount; i++) {
// auto host = vHosts.emplace_back(Host::make(
// "Test",
// [&](auto host) {
// auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION));
// return Host::CapabilityList{cap};
// },
// KeyPair::create(), vPrefs[i]));
// tp.post_loop({}, [=] { host->do_work(); });
// }
//
// printf("Starting %d hosts\n", nodeCount);
// auto port1 = host1->listenPort();
// EXPECT_NE(port1, 0);
// for (int i = 0; i < nodeCount; i++) {
// EXPECT_NE(vHosts[i]->listenPort(), 0);
// EXPECT_NE(port1, vHosts[i]->listenPort());
// for (int j = 0; j < i; j++) EXPECT_NE(vHosts[j]->listenPort(), vHosts[i]->listenPort());
// }
//
// for (int i = 0; i < nodeCount; i++) {
// if (i < 10)
// vHosts[i]->addNode(Node(host1->id(), NodeIPEndpoint(bi::address::from_string(localhost), port1, port1)));
// else
// vHosts[i]->addNode(
// Node(vHosts[i % 10]->id(), NodeIPEndpoint(bi::address::from_string(localhost), vHosts[i % 10]->listenPort(),
// vHosts[i % 10]->listenPort())));
// std::this_thread::sleep_for(std::chrono::milliseconds(20));
// }
//
// printf("Addnode %d hosts\n", nodeCount);
//
// // Wait for to give the hosts time to connect to each
// // other.
// bool connected = false;
// for (unsigned i = 0; i < 500; i++) {
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// connected = true;
// setPendingPeersToReady(thc1);
// for (int j = 0; j < nodeCount; j++) {
// setPendingPeersToReady(vCapabilities[j]);
// if (vHosts[j]->peer_count() < 1) connected = false;
// }
//
// if ((host1->peer_count() > 0) && connected) break;
// }
// EXPECT_TRUE(connected);
// 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), kNullBlockHash,
// addr_t(999));
//
// SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]};
// thc1->getSpecificHandler<network::tarcap::TransactionPacketHandler>()->onNewTransactions(
// SharedTransactions(transactions));
// SharedTransactions transactions2;
// thc1->getSpecificHandler<network::tarcap::TransactionPacketHandler>()->onNewTransactions(std::move(transactions2));
// thc1->getSpecificHandler<network::tarcap::DagBlockPacketHandler>()->onNewBlockReceived(DagBlock(blk));
//
// SharedTransactions transactions_to_send;
// transactions_to_send.push_back(g_signed_trx_samples[0]);
// transactions_to_send.push_back(g_signed_trx_samples[1]);
// for (int i = 0; i < nodeCount; i++) {
// thc1->getSpecificHandler<network::tarcap::TransactionPacketHandler>()->sendTransactions(
// thc1->getPeersState()->getPeer(vHosts[i]->id()), std::move(transactions_to_send));
// }
// for (int i = 0; i < 50; i++) {
// std::this_thread::sleep_for(std::chrono::seconds(1));
// bool synced = true;
// for (int j = 0; j < nodeCount; j++)
// if (vCapabilities[j]->test_state_->getBlocks().size() == 0) {
// synced = false;
// }
// if (synced) break;
// }
// auto blocks1 = thc1->test_state_->getBlocks();
// for (int i = 0; i < nodeCount; i++) {
// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().size(), 1);
// if (vCapabilities[i]->test_state_->getBlocks().size() == 1) {
// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second, blk);
// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second.getHash(), blk.getHash());
// }
// auto rtransactions = vCapabilities[i]->test_state_->getTransactions();
// EXPECT_EQ(rtransactions.size(), 2);
// if (rtransactions.size() == 2) {
// EXPECT_EQ(*transactions[0], *rtransactions[g_signed_trx_samples[0]->getHash()]);
// EXPECT_EQ(*transactions[1], *rtransactions[g_signed_trx_samples[1]->getHash()]);
// }
// }
// EXPECT_EQ(blocks1.size(), 1);
// if (blocks1.size()) {
// EXPECT_EQ(blk, blocks1.begin()->second);
// }
//}

TEST_F(P2PTest, multiple_capabilities) {
auto node_cfgs = make_node_cfgs(3);
h256 genesis_hash;
Expand Down

0 comments on commit a4e385e

Please sign in to comment.