From d4f184572e7862f90b86e4f10e53212c65ef6972 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Wed, 27 Jun 2018 11:58:21 +0200 Subject: [PATCH 1/2] Some tests for BlockChainSync --- .../libethereum/BlockChainSyncTest.cpp | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/unittests/libethereum/BlockChainSyncTest.cpp diff --git a/test/unittests/libethereum/BlockChainSyncTest.cpp b/test/unittests/libethereum/BlockChainSyncTest.cpp new file mode 100644 index 00000000000..6c68e61814f --- /dev/null +++ b/test/unittests/libethereum/BlockChainSyncTest.cpp @@ -0,0 +1,100 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ + +#include +#include +#include +#include +#include + +namespace dev +{ +namespace test +{ +namespace +{ +struct BlockChainSyncFixture: public TestOutputHelperFixture +{ + BlockChainSyncFixture() + { + // TODO this will not work wth parallel tests + p2p::NetworkPreferences nprefs1("127.0.0.1", p2p::c_defaultListenPort); + nprefs1.discovery = false; + eth::ChainParams chainParams; + chainParams.sealEngineName = eth::NoProof::name(); + chainParams.allowFutureBlocks = true; + chainParams.daoHardforkBlock = 0; + chainParams.difficulty = chainParams.minimumDifficulty; + chainParams.gasLimit = chainParams.maxGasLimit; + // add random extra data to randomize genesis hash and get random DB path, + // so that tests can be run in parallel + // TODO: better make it use ethemeral in-memory databases + chainParams.extraData = h256::random().asBytes(); + web3Node1.reset(new WebThreeDirect( + "testclient1", dbDir1.path(), "", chainParams, WithExisting::Kill, {"eth"}, nprefs1)); + + //web3->setIdealPeerCount(5); + + web3Node1->ethereum()->setAuthor(coinbase.address()); + + web3Node1->startNetwork(); + + p2p::NetworkPreferences nprefs2("127.0.0.1", p2p::c_defaultListenPort + 1); + nprefs2.discovery = false; + web3Node2.reset(new WebThreeDirect( + "testclient2", dbDir2.path(), "", chainParams, WithExisting::Kill, {"eth"}, nprefs2)); + + web3Node2->startNetwork(); + } + + TransientDirectory dbDir1; + std::unique_ptr web3Node1; + TransientDirectory dbDir2; + std::unique_ptr web3Node2; + KeyPair coinbase{KeyPair::create()}; +}; + +} + +BOOST_FIXTURE_TEST_SUITE(BlockChainSyncSuite, BlockChainSyncFixture) + + +BOOST_AUTO_TEST_CASE(syncFromMiner) +{ + dev::eth::mine(*(web3Node1->ethereum()), 10); + + int blocksImported = 0; + std::promise allBlocksImported; + auto importHandler = + web3Node2->ethereum()->setOnBlockImport([&blocksImported, &allBlocksImported](eth::BlockHeader const&) { + if (++blocksImported == 10) + allBlocksImported.set_value(); + }); + + // p2p::NodeSpec node2spec{"127.0.0.1", p2p::c_defaultListenPort + 1}; +// web3Node1->addPeer(/*web3Node2->enode()*/node2spec, p2p::PeerType::Required); + web3Node1->requirePeer(web3Node2->id(), "127.0.0.1:30304"); + + allBlocksImported.get_future().wait_for(std::chrono::minutes(1)); + + BOOST_REQUIRE_EQUAL(blocksImported, 10); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} From b500067503340a207739394fd2da57f514730ae6 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Tue, 10 Jul 2018 16:54:25 +0200 Subject: [PATCH 2/2] WIP --- .../libethereum/BlockChainSyncTest.cpp | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/unittests/libethereum/BlockChainSyncTest.cpp b/test/unittests/libethereum/BlockChainSyncTest.cpp index 6c68e61814f..ce0fd2883a7 100644 --- a/test/unittests/libethereum/BlockChainSyncTest.cpp +++ b/test/unittests/libethereum/BlockChainSyncTest.cpp @@ -73,7 +73,7 @@ struct BlockChainSyncFixture: public TestOutputHelperFixture BOOST_FIXTURE_TEST_SUITE(BlockChainSyncSuite, BlockChainSyncFixture) -BOOST_AUTO_TEST_CASE(syncFromMiner) +BOOST_AUTO_TEST_CASE(basicSync) { dev::eth::mine(*(web3Node1->ethereum()), 10); @@ -94,6 +94,34 @@ BOOST_AUTO_TEST_CASE(syncFromMiner) BOOST_REQUIRE_EQUAL(blocksImported, 10); } +BOOST_AUTO_TEST_CASE(syncFromMiner) +{ + int sealedBlocks = 0; + auto sealHandler = + web3Node1->ethereum()->setOnBlockSealed([&sealedBlocks](bytes const&) { ++sealedBlocks; }); + + int importedBlocks = 0; + auto importHandler = web3Node1->ethereum()->setOnBlockImport( + [&importedBlocks](eth::BlockHeader const&) { ++importedBlocks; }); + + web3Node1->ethereum()->startSealing(); + + /* int blocksImported = 0; + std::promise allBlocksImported; + auto importHandler = + web3Node2->ethereum()->setOnBlockImport([&blocksImported, + &allBlocksImported](eth::BlockHeader const&) { if (++blocksImported == 10) + allBlocksImported.set_value(); + }); + */ + web3Node1->requirePeer(web3Node2->id(), "127.0.0.1:30304"); + + + // allBlocksImported.get_future().wait_for(std::chrono::minutes(1)); + + BOOST_REQUIRE(web3Node1->ethereum()->wouldSeal()); +} + BOOST_AUTO_TEST_SUITE_END() }