diff --git a/.gitignore b/.gitignore index 4a0174f..e1c7852 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ /cmake-build-debug-visual-studio/ /cmake-build-release/ /cmake-build-release-ccache/ -/cmake-build-releasewithtests +/cmake-build-release-with-tests/ diff --git a/main.cpp b/main.cpp index f80f15c..6a9f0d5 100644 --- a/main.cpp +++ b/main.cpp @@ -15,8 +15,12 @@ */ #include + +#include + #include +#include "scn/Common/Common.h" #include "scn/Miner/MinerLocal.h" #include "scn/Blockchain/Blockchain.h" #include "scn/P2PConnector/P2PConnector.h" @@ -41,6 +45,8 @@ int startCommandLineApp(int argc, char* argv[]) { return 1; } + boost::filesystem::create_directory(scn::path_helper::getRuntimeDir()); + std::srand(std::time(nullptr)); std::ifstream public_key_stream(argv[1]); @@ -49,7 +55,7 @@ int startCommandLineApp(int argc, char* argv[]) { std::stringstream private_key_string; private_key_string << private_key_stream.rdbuf(); - scn::Blockchain blockchain("./blockchains/" + std::string(argv[4]) + "/"); + scn::Blockchain blockchain(scn::path_helper::getRuntimeDir() + "blockchains/" + std::string(argv[4]) + "/"); scn::P2PConnector p2p_connector(std::stoi(std::string(argv[4])), blockchain); scn::MinerLocal miner(std::stoi(std::string(argv[3]))); scn::BlockchainManager manager(public_key, private_key_string.str(), blockchain, p2p_connector, miner); diff --git a/src/scn/Common/Common.cpp b/src/scn/Common/Common.cpp index 1d55842..7683299 100644 --- a/src/scn/Common/Common.cpp +++ b/src/scn/Common/Common.cpp @@ -16,6 +16,10 @@ #include "Common.h" +#include + +#include + using namespace scn; std::string scn::hash_helper::toString(const hash_t& hash) { @@ -37,4 +41,13 @@ hash_t scn::hash_helper::fromArray(const uint8_t* array) { ret |= array[i]; } return ret; +} + + +std::string scn::path_helper::getRuntimeDir() { + + std::stringstream runtime_dir; + runtime_dir << boost::filesystem::temp_directory_path().string() << "/" << "SwabianCoin-" << getpid() << "/"; + return runtime_dir.str(); + } \ No newline at end of file diff --git a/src/scn/Common/Common.h b/src/scn/Common/Common.h index e19acc0..ee511ad 100644 --- a/src/scn/Common/Common.h +++ b/src/scn/Common/Common.h @@ -64,6 +64,12 @@ namespace scn { hash_t fromArray(const uint8_t* array); } + + namespace path_helper { + + std::string getRuntimeDir(); + + } } diff --git a/src/scn/P2PConnector/P2PConnector.cpp b/src/scn/P2PConnector/P2PConnector.cpp index 041ee5e..735b852 100644 --- a/src/scn/P2PConnector/P2PConnector.cpp +++ b/src/scn/P2PConnector/P2PConnector.cpp @@ -52,7 +52,7 @@ P2PConnector::P2PConnector(uint16_t port, const Blockchain& blockchain) t.add_node(std::pair(entry_point.first, entry_point.second)); } libtorrent::set_piece_hashes(t, "."); - std::ofstream out("scn.torrent", std::ios_base::binary); + std::ofstream out(scn::path_helper::getRuntimeDir() + "scn.torrent", std::ios_base::binary); libtorrent::bencode(std::ostream_iterator(out), t.generate()); } boost::filesystem::remove("scn"); @@ -78,7 +78,7 @@ P2PConnector::P2PConnector(uint16_t port, const Blockchain& blockchain) p.save_path = "./"; p.max_connections = 10; libtorrent::error_code ec; - p.ti = std::make_shared("scn.torrent", std::ref(ec)); + p.ti = std::make_shared(scn::path_helper::getRuntimeDir() + "scn.torrent", std::ref(ec)); if (ec) { LOG(ERROR) << "error reading torrent file: " << ec.message(); return; diff --git a/test/TestP2PConnector.cpp b/test/TestP2PConnector.cpp index 090715f..2c92282 100644 --- a/test/TestP2PConnector.cpp +++ b/test/TestP2PConnector.cpp @@ -25,7 +25,7 @@ using namespace scn; class TestP2PConnector : public testing::Test { public: TestP2PConnector() - :blockchain_("./blockchain_test/") + :blockchain_(scn::path_helper::getRuntimeDir() + "blockchain_test/") ,p2p_connector_(13386, blockchain_) ,last_received_baseline_block(nullptr) ,last_received_collection_block(nullptr) {