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

Commit d4f1845

Browse files
committed
Some tests for BlockChainSync
1 parent 2c542ca commit d4f1845

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
This file is part of cpp-ethereum.
3+
4+
cpp-ethereum is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
cpp-ethereum is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <libdevcore/TransientDirectory.h>
19+
#include <libwebthree/WebThree.h>
20+
#include <test/tools/libtesteth/TestOutputHelper.h>
21+
#include <test/tools/libtesteth/TestHelper.h>
22+
#include <boost/test/unit_test.hpp>
23+
24+
namespace dev
25+
{
26+
namespace test
27+
{
28+
namespace
29+
{
30+
struct BlockChainSyncFixture: public TestOutputHelperFixture
31+
{
32+
BlockChainSyncFixture()
33+
{
34+
// TODO this will not work wth parallel tests
35+
p2p::NetworkPreferences nprefs1("127.0.0.1", p2p::c_defaultListenPort);
36+
nprefs1.discovery = false;
37+
eth::ChainParams chainParams;
38+
chainParams.sealEngineName = eth::NoProof::name();
39+
chainParams.allowFutureBlocks = true;
40+
chainParams.daoHardforkBlock = 0;
41+
chainParams.difficulty = chainParams.minimumDifficulty;
42+
chainParams.gasLimit = chainParams.maxGasLimit;
43+
// add random extra data to randomize genesis hash and get random DB path,
44+
// so that tests can be run in parallel
45+
// TODO: better make it use ethemeral in-memory databases
46+
chainParams.extraData = h256::random().asBytes();
47+
web3Node1.reset(new WebThreeDirect(
48+
"testclient1", dbDir1.path(), "", chainParams, WithExisting::Kill, {"eth"}, nprefs1));
49+
50+
//web3->setIdealPeerCount(5);
51+
52+
web3Node1->ethereum()->setAuthor(coinbase.address());
53+
54+
web3Node1->startNetwork();
55+
56+
p2p::NetworkPreferences nprefs2("127.0.0.1", p2p::c_defaultListenPort + 1);
57+
nprefs2.discovery = false;
58+
web3Node2.reset(new WebThreeDirect(
59+
"testclient2", dbDir2.path(), "", chainParams, WithExisting::Kill, {"eth"}, nprefs2));
60+
61+
web3Node2->startNetwork();
62+
}
63+
64+
TransientDirectory dbDir1;
65+
std::unique_ptr<WebThreeDirect> web3Node1;
66+
TransientDirectory dbDir2;
67+
std::unique_ptr<WebThreeDirect> web3Node2;
68+
KeyPair coinbase{KeyPair::create()};
69+
};
70+
71+
}
72+
73+
BOOST_FIXTURE_TEST_SUITE(BlockChainSyncSuite, BlockChainSyncFixture)
74+
75+
76+
BOOST_AUTO_TEST_CASE(syncFromMiner)
77+
{
78+
dev::eth::mine(*(web3Node1->ethereum()), 10);
79+
80+
int blocksImported = 0;
81+
std::promise<void> allBlocksImported;
82+
auto importHandler =
83+
web3Node2->ethereum()->setOnBlockImport([&blocksImported, &allBlocksImported](eth::BlockHeader const&) {
84+
if (++blocksImported == 10)
85+
allBlocksImported.set_value();
86+
});
87+
88+
// p2p::NodeSpec node2spec{"127.0.0.1", p2p::c_defaultListenPort + 1};
89+
// web3Node1->addPeer(/*web3Node2->enode()*/node2spec, p2p::PeerType::Required);
90+
web3Node1->requirePeer(web3Node2->id(), "127.0.0.1:30304");
91+
92+
allBlocksImported.get_future().wait_for(std::chrono::minutes(1));
93+
94+
BOOST_REQUIRE_EQUAL(blocksImported, 10);
95+
}
96+
97+
BOOST_AUTO_TEST_SUITE_END()
98+
99+
}
100+
}

0 commit comments

Comments
 (0)