From 91d85a402333de40e9bdb4c40cc372af34a63517 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 29 Mar 2015 22:23:59 +0200 Subject: [PATCH 1/2] Fix a load of horrible style that got through. Fix blockchain tests so they don't clobber the AZ blockchain DB! Make TransientDirectory use a temp path unless otherwise instructed. --- libdevcore/FixedHash.h | 1 + libp2p/Network.h | 2 +- libtestutils/Common.cpp | 2 +- libtestutils/TransientDirectory.cpp | 8 +++++-- libtestutils/TransientDirectory.h | 3 ++- test/blockchain.cpp | 34 ++++++++++++++--------------- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index f5469ada8..456365299 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -141,6 +141,7 @@ public: return ret; } + /// @returns a random valued object. static FixedHash random() { return random(s_fixedHashEngine); } /// A generic std::hash compatible function object. diff --git a/libp2p/Network.h b/libp2p/Network.h index aeeabf329..c116b29e0 100644 --- a/libp2p/Network.h +++ b/libp2p/Network.h @@ -61,7 +61,7 @@ public: static int tcp4Listen(bi::tcp::acceptor& _acceptor, unsigned short _listenPort); /// Return public endpoint of upnp interface. If successful o_upnpifaddr will be a private interface address and endpoint will contain public address and port. - static bi::tcp::endpoint traverseNAT(std::vector const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpifaddr); + static bi::tcp::endpoint traverseNAT(std::vector const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpInterfaceAddr); }; } diff --git a/libtestutils/Common.cpp b/libtestutils/Common.cpp index f15a2da12..86f96f667 100644 --- a/libtestutils/Common.cpp +++ b/libtestutils/Common.cpp @@ -74,7 +74,7 @@ std::string dev::test::toTestFilePath(std::string const& _filename) std::string dev::test::getRandomPath() { std::stringstream stream; - stream << getDataDir() << "/EthereumTests/" << randomNumber(); + stream << getDataDir("EthereumTests") << "/" << randomNumber(); return stream.str(); } diff --git a/libtestutils/TransientDirectory.cpp b/libtestutils/TransientDirectory.cpp index 48beca7e3..694784e25 100644 --- a/libtestutils/TransientDirectory.cpp +++ b/libtestutils/TransientDirectory.cpp @@ -22,12 +22,16 @@ #include #include #include "TransientDirectory.h" - using namespace std; using namespace dev; using namespace dev::test; -TransientDirectory::TransientDirectory(std::string const& _path) : m_path(_path) +TransientDirectory::TransientDirectory(): + TransientDirectory((boost::filesystem::temp_directory_path() / "eth_transient" / toString(FixedHash<4>::random())).string()) +{} + +TransientDirectory::TransientDirectory(std::string const& _path): + m_path(_path) { // we never ever want to delete a directory (including all its contents) that we did not create ourselves. if (boost::filesystem::exists(m_path)) diff --git a/libtestutils/TransientDirectory.h b/libtestutils/TransientDirectory.h index 328b4c92b..21a338e59 100644 --- a/libtestutils/TransientDirectory.h +++ b/libtestutils/TransientDirectory.h @@ -37,7 +37,8 @@ namespace test class TransientDirectory { public: - TransientDirectory(std::string const& _path = getRandomPath()); + TransientDirectory(); + TransientDirectory(std::string const& _path); ~TransientDirectory(); std::string const& path() const { return m_path; } diff --git a/test/blockchain.cpp b/test/blockchain.cpp index 988859fe5..e5bba607a 100644 --- a/test/blockchain.cpp +++ b/test/blockchain.cpp @@ -20,6 +20,7 @@ * block test functions. */ +#include #include #include #include "TestHelper.h" @@ -35,8 +36,8 @@ bytes createBlockRLPFromFields(mObject& _tObj); void overwriteBlockHeader(BlockInfo& _current_BlockHeader, mObject& _blObj); BlockInfo constructBlock(mObject& _o); void updatePoW(BlockInfo& _bi); -void writeBlockHeaderToJson(mObject& _o, const BlockInfo& _bi); -RLPStream createFullBlockFromHeader(const BlockInfo& _bi, const bytes& _txs = RLPEmptyList, const bytes& _uncles = RLPEmptyList); +void writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi); +RLPStream createFullBlockFromHeader(BlockInfo const& _bi, bytes const& _txs = RLPEmptyList, bytes const& _uncles = RLPEmptyList); void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) { @@ -75,7 +76,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) o["genesisRLP"] = "0x" + toHex(rlpGenesisBlock.out()); // construct blockchain - BlockChain bc(rlpGenesisBlock.out(), string(), true); + BlockChain bc(rlpGenesisBlock.out(), (boost::filesystem::temp_directory_path() / ("eth_test_" + toString(rand()))).string(), true); if (_fillin) { @@ -241,6 +242,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) if (sha3(RLP(state.blockData())[2].data()) != sha3(RLP(block2.out())[2].data())) cnote << "uncle list mismatch\n" << RLP(state.blockData())[2].data() << "\n" << RLP(block2.out())[2].data(); + try { state.sync(bc); @@ -292,7 +294,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) BOOST_CHECK(blObj.count("uncleHeaders") == 0); continue; } - catch(...) + catch (...) { cnote << "state sync or block import did throw an exception\n"; BOOST_CHECK(blObj.count("blockHeader") == 0); @@ -388,7 +390,6 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) BOOST_CHECK_MESSAGE(txsFromField[i] == txsFromRlp[i], "transactions from rlp and transaction from field do not match"); BOOST_CHECK_MESSAGE(txsFromField[i].rlp() == txsFromRlp[i].rlp(), "transactions rlp do not match"); - } // check uncle list @@ -488,12 +489,12 @@ bytes createBlockRLPFromFields(mObject& _tObj) return rlpStream.out(); } -void overwriteBlockHeader(BlockInfo& _current_BlockHeader, mObject& _blObj) +void overwriteBlockHeader(BlockInfo& _currentBlockHeader, mObject& _blObj) { if (_blObj["blockHeader"].get_obj().size() != 14) { - BlockInfo tmp = _current_BlockHeader; + BlockInfo tmp = _currentBlockHeader; if (_blObj["blockHeader"].get_obj().count("parentHash")) tmp.parentHash = h256(_blObj["blockHeader"].get_obj()["parentHash"].get_str()); @@ -539,16 +540,16 @@ void overwriteBlockHeader(BlockInfo& _current_BlockHeader, mObject& _blObj) // find new valid nonce - if (tmp != _current_BlockHeader) + if (tmp != _currentBlockHeader) { - _current_BlockHeader = tmp; + _currentBlockHeader = tmp; ProofOfWork pow; std::pair ret; - while (!ProofOfWork::verify(_current_BlockHeader)) + while (!ProofOfWork::verify(_currentBlockHeader)) { - ret = pow.mine(_current_BlockHeader, 1000, true, true); - Ethash::assignResult(ret.second, _current_BlockHeader); + ret = pow.mine(_currentBlockHeader, 1000, true, true); + Ethash::assignResult(ret.second, _currentBlockHeader); } } } @@ -557,13 +558,12 @@ void overwriteBlockHeader(BlockInfo& _current_BlockHeader, mObject& _blObj) // take the blockheader as is const bytes c_blockRLP = createBlockRLPFromFields(_blObj["blockHeader"].get_obj()); const RLP c_bRLP(c_blockRLP); - _current_BlockHeader.populateFromHeader(c_bRLP, IgnoreNonce); + _currentBlockHeader.populateFromHeader(c_bRLP, IgnoreNonce); } } BlockInfo constructBlock(mObject& _o) { - BlockInfo ret; try { @@ -600,7 +600,7 @@ void updatePoW(BlockInfo& _bi) _bi.hash = _bi.headerHash(WithNonce); } -void writeBlockHeaderToJson(mObject& _o, const BlockInfo& _bi) +void writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi) { _o["parentHash"] = toString(_bi.parentHash); _o["uncleHash"] = toString(_bi.sha3Uncles); @@ -620,7 +620,7 @@ void writeBlockHeaderToJson(mObject& _o, const BlockInfo& _bi) _o["hash"] = toString(_bi.hash); } -RLPStream createFullBlockFromHeader(const BlockInfo& _bi,const bytes& _txs, const bytes& _uncles ) +RLPStream createFullBlockFromHeader(BlockInfo const& _bi, bytes const& _txs, bytes const& _uncles) { RLPStream rlpStream; _bi.streamRLP(rlpStream, WithNonce); @@ -632,8 +632,8 @@ RLPStream createFullBlockFromHeader(const BlockInfo& _bi,const bytes& _txs, cons return ret; } -} }// Namespace Close +} }// Namespace Close BOOST_AUTO_TEST_SUITE(BlockChainTests) From 9a69198a747d8e1061d3e3a58214e87b1ae3f00c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 29 Mar 2015 22:37:32 +0200 Subject: [PATCH 2/2] Use TransientDirectory. --- test/blockchain.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/blockchain.cpp b/test/blockchain.cpp index e5bba607a..17e6c3588 100644 --- a/test/blockchain.cpp +++ b/test/blockchain.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include "TestHelper.h" @@ -76,7 +77,8 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) o["genesisRLP"] = "0x" + toHex(rlpGenesisBlock.out()); // construct blockchain - BlockChain bc(rlpGenesisBlock.out(), (boost::filesystem::temp_directory_path() / ("eth_test_" + toString(rand()))).string(), true); + TransientDirectory td; + BlockChain bc(rlpGenesisBlock.out(), td.path(), true); if (_fillin) {