From 2626159abe0767b5e19d4f83787c85a287ca417f Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 11 Nov 2015 01:17:36 -0500 Subject: [PATCH] use a consistent random number generator to make simulations deterministic --- include/libtorrent/random.hpp | 1 + src/http_connection.cpp | 3 ++- src/piece_picker.cpp | 2 +- src/random.cpp | 5 +++++ src/session_impl.cpp | 2 +- src/torrent.cpp | 6 +++--- src/torrent_info.cpp | 5 +++-- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/libtorrent/random.hpp b/include/libtorrent/random.hpp index b8d427a99..d33d1a1cc 100644 --- a/include/libtorrent/random.hpp +++ b/include/libtorrent/random.hpp @@ -36,4 +36,5 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { boost::uint32_t TORRENT_EXTRA_EXPORT random(); + boost::uint32_t TORRENT_EXTRA_EXPORT randint(int i); } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index a037b5018..c6f7c6545 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/resolver_interface.hpp" #include "libtorrent/settings_pack.hpp" #include "libtorrent/aux_/time.hpp" +#include "libtorrent/random.hpp" #if defined TORRENT_ASIO_DEBUGGING #include "libtorrent/debug.hpp" @@ -552,7 +553,7 @@ void http_connection::on_resolve(error_code const& e return; } - std::random_shuffle(m_endpoints.begin(), m_endpoints.end()); + std::random_shuffle(m_endpoints.begin(), m_endpoints.end(), randint); // The following statement causes msvc to crash (ICE). Since it's not // necessary in the vast majority of cases, just ignore the endpoint diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 421fd654d..793b22a74 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -1564,7 +1564,7 @@ namespace libtorrent , end(m_priority_boundries.end()); i != end; ++i) { if (start == *i) continue; - std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + *i); + std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + *i, randint); start = *i; } diff --git a/src/random.cpp b/src/random.cpp index 8b76164a0..d8ca0b0c0 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -94,5 +94,10 @@ namespace libtorrent #endif // TORRENT_BUILD_SIMULATOR + boost::uint32_t TORRENT_EXTRA_EXPORT randint(int i) + { + return random() % i; + } + } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 36077fedd..348a49db8 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3765,7 +3765,7 @@ retry: // unchoked // avoid having a bias towards peers that happen to be sorted first - std::random_shuffle(opt_unchoke.begin(), opt_unchoke.end()); + std::random_shuffle(opt_unchoke.begin(), opt_unchoke.end(), randint); // sort all candidates based on when they were last optimistically // unchoked. diff --git a/src/torrent.cpp b/src/torrent.cpp index d939d762f..84622821b 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -7305,7 +7305,7 @@ namespace libtorrent // if we didn't save 100 peers, fill in with second choice peers if (num_saved_peers < 100) { - std::random_shuffle(deferred_peers.begin(), deferred_peers.end()); + std::random_shuffle(deferred_peers.begin(), deferred_peers.end(), randint); for (std::vector::const_iterator i = deferred_peers.begin() , end(deferred_peers.end()); i != end && num_saved_peers < 100; ++i) { @@ -10339,7 +10339,7 @@ namespace libtorrent if (p->is_seed()) seeds.push_back(p); } - std::random_shuffle(seeds.begin(), seeds.end()); + std::random_shuffle(seeds.begin(), seeds.end(), randint); TORRENT_ASSERT(to_disconnect <= int(seeds.size())); for (int i = 0; i < to_disconnect; ++i) seeds[i]->disconnect(errors::upload_upload_connection @@ -10497,7 +10497,7 @@ namespace libtorrent --pieces[i->piece].first; } - std::random_shuffle(pieces.begin(), pieces.end()); + std::random_shuffle(pieces.begin(), pieces.end(), randint); std::stable_sort(pieces.begin(), pieces.end() , boost::bind(&std::pair::first, _1) < boost::bind(&std::pair::first, _2)); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index e1ee82368..c0e07054a 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/utf8.hpp" #include "libtorrent/time.hpp" +#include "libtorrent/random.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/aux_/escape_string.hpp" // maybe_url_encode @@ -1501,12 +1502,12 @@ namespace libtorrent { if (stop->tier != current_tier) { - std::random_shuffle(start, stop); + std::random_shuffle(start, stop, randint); start = stop; current_tier = stop->tier; } } - std::random_shuffle(start, stop); + std::random_shuffle(start, stop, randint); } }