Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Some tests for BlockChainSync
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jul 10, 2018
1 parent 2c542ca commit d4f1845
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions test/unittests/libethereum/BlockChainSyncTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include <libdevcore/TransientDirectory.h>
#include <libwebthree/WebThree.h>
#include <test/tools/libtesteth/TestOutputHelper.h>
#include <test/tools/libtesteth/TestHelper.h>
#include <boost/test/unit_test.hpp>

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<WebThreeDirect> web3Node1;
TransientDirectory dbDir2;
std::unique_ptr<WebThreeDirect> 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<void> 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()

}
}

0 comments on commit d4f1845

Please sign in to comment.