Skip to content

Commit

Permalink
remove support for BEP 30 merkle tree torrents
Browse files Browse the repository at this point in the history
With the adoption of BEP 52, BEP 30 is now obsolete. This code has also
been broken for some time. The merkle_* functions have been retained because
they will probably be usefull for BEP 52.
  • Loading branch information
Steven Siloti authored and ssiloti committed Aug 9, 2019
1 parent b55a188 commit 3d2d745
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 423 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -911,15 +911,12 @@ TEST_TORRENTS = \
invalid_filename.torrent \
invalid_filename2.torrent \
invalid_info.torrent \
invalid_merkle.torrent \
invalid_name.torrent \
invalid_name2.torrent \
invalid_name3.torrent \
invalid_path_list.torrent \
invalid_piece_len.torrent \
invalid_pieces.torrent \
invalid_root_hash.torrent \
invalid_root_hash2.torrent \
invalid_symlink.torrent \
large.torrent \
long_name.torrent \
Expand All @@ -935,7 +932,6 @@ TEST_TORRENTS = \
pad_file.torrent \
pad_file_no_path.torrent \
parent_path.torrent \
root_hash.torrent \
sample.torrent \
single_multi_file.torrent \
slash_path.torrent \
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/src/create_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ void bind_create_torrent()
;

s.attr("optimize_alignment") = create_torrent::optimize_alignment;
#if TORRENT_ABI_VERSION <= 2
s.attr("merkle") = create_torrent::merkle;
#endif
s.attr("modification_time") = create_torrent::modification_time;
s.attr("symlinks") = create_torrent::symlinks;
}
Expand All @@ -246,7 +248,9 @@ void bind_create_torrent()
s.attr("optimize") = create_torrent::optimize;
#endif
s.attr("optimize_alignment") = create_torrent::optimize_alignment;
#if TORRENT_ABI_VERSION <= 2
s.attr("merkle") = create_torrent::merkle;
#endif
s.attr("modification_time") = create_torrent::modification_time;
s.attr("symlinks") = create_torrent::symlinks;
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,9 @@ void bind_session()
.add_property("have_pieces", PROP(&add_torrent_params::have_pieces))
.add_property("verified_pieces", PROP(&add_torrent_params::verified_pieces))
.add_property("piece_priorities", PROP(&add_torrent_params::piece_priorities))
#if TORRENT_ABI_VERSION <= 2
.add_property("merkle_tree", PROP(&add_torrent_params::merkle_tree))
#endif
.add_property("renamed_files", PROP(&add_torrent_params::renamed_files))

#if TORRENT_ABI_VERSION == 1
Expand Down
6 changes: 6 additions & 0 deletions bindings/python/src/torrent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace
ti.set_web_seeds(web_seeds);
}

#if TORRENT_ABI_VERSION <= 2
list get_merkle_tree(torrent_info const& ti)
{
std::vector<sha1_hash> const& mt = ti.merkle_tree();
Expand All @@ -101,6 +102,7 @@ namespace

ti.set_merkle_tree(h);
}
#endif

bytes hash_for_piece(torrent_info const& ti, piece_index_t i)
{
Expand Down Expand Up @@ -259,8 +261,10 @@ void bind_torrent_info()
.def("num_pieces", &torrent_info::num_pieces)
.def("info_hash", &torrent_info::info_hash, copy)
.def("hash_for_piece", &hash_for_piece)
#if TORRENT_ABI_VERSION <= 2
.def("merkle_tree", get_merkle_tree)
.def("set_merkle_tree", set_merkle_tree)
#endif
.def("piece_size", &torrent_info::piece_size)

.def("similar_torrents", &torrent_info::similar_torrents)
Expand All @@ -280,7 +284,9 @@ void bind_torrent_info()
.def("is_valid", &torrent_info::is_valid)
.def("priv", &torrent_info::priv)
.def("is_i2p", &torrent_info::is_i2p)
#if TORRENT_ABI_VERSION <= 2
.def("is_merkle_torrent", &torrent_info::is_merkle_torrent)
#endif
.def("trackers", range(begin_trackers, end_trackers))

.def("creation_date", &torrent_info::creation_date)
Expand Down
3 changes: 2 additions & 1 deletion bindings/python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ def test_torrent_parameter(self):
# piece priorities weren't set explicitly, but they were updated by the
# file priorities being set
self.assertEqual(self.h.get_piece_priorities(), [1])
self.assertEqual(self.ti.merkle_tree(), [])
if HAVE_DEPRECATED_APIS:
self.assertEqual(self.ti.merkle_tree(), [])
self.assertEqual(self.st.verified_pieces, [])


Expand Down
16 changes: 0 additions & 16 deletions examples/make_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ or directory and writes it to standard out
OPTIONS:
-m file generate a merkle hash tree torrent.
merkle torrents require client support
the resulting full merkle tree is written to
the specified file
-w url adds a web seed to the torrent with
the specified url
-t url adds the specified tracker to the
Expand Down Expand Up @@ -173,7 +169,6 @@ int main(int argc_, char const* argv_[]) try
std::string root_cert;

std::string outfile;
std::string merklefile;
#ifdef TORRENT_WINDOWS
// don't ever write binary data to the console on windows
// it will just be interpreted as text and corrupted
Expand Down Expand Up @@ -221,10 +216,6 @@ int main(int argc_, char const* argv_[]) try
pad_file_limit = atoi(args[1]);
flags |= lt::create_torrent::optimize_alignment;
break;
case 'm':
merklefile = args[1];
flags |= lt::create_torrent::merkle;
break;
case 'S': {
if (strlen(args[1]) != 40) {
std::cerr << "invalid info-hash for -S. "
Expand Down Expand Up @@ -331,13 +322,6 @@ int main(int argc_, char const* argv_[]) try
std::cout.write(torrent.data(), torrent.size());
}

if (!merklefile.empty()) {
std::fstream merkle;
merkle.exceptions(std::ifstream::failbit);
merkle.open(merklefile.c_str(), std::ios_base::out | std::ios_base::binary);
auto const& tree = t.merkle_tree();
merkle.write(reinterpret_cast<char const*>(tree.data()), tree.size() * 20);
}
return 0;
}
catch (std::exception& e) {
Expand Down
6 changes: 5 additions & 1 deletion include/libtorrent/add_torrent_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,14 @@ TORRENT_VERSION_NAMESPACE_2
// precedence.
aux::noexcept_movable<std::vector<download_priority_t>> piece_priorities;

#if TORRENT_ABI_VERSION <= 2
// support for BEP 30 merkle torrents has been removed

// if this is a merkle tree torrent, and you're seeding, this field must
// be set. It is all the hashes in the binary tree, with the root as the
// first entry. See torrent_info::set_merkle_tree() for more info.
aux::noexcept_movable<std::vector<sha1_hash>> merkle_tree;
aux::noexcept_movable<std::vector<sha1_hash>> TORRENT_DEPRECATED_MEMBER merkle_tree;
#endif

// this is a map of file indices in the torrent and new filenames to be
// applied before the torrent is added.
Expand Down
20 changes: 10 additions & 10 deletions include/libtorrent/create_torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ namespace libtorrent {
// When creating merkle torrents, the full hash tree is also generated
// and should be saved off separately. It is accessed through the
// create_torrent::merkle_tree() function.
static constexpr create_flags_t merkle = 1_bit;
#ifndef TORRENT_NO_DEPRECATE
// support for BEP 30 merkle torrents has been removed
static constexpr create_flags_t TORRENT_DEPRECATED_MEMBER merkle = 1_bit;
#endif

// This will include the file modification time as part of the torrent.
// This is not enabled by default, as it might cause problems when you
Expand Down Expand Up @@ -272,13 +275,18 @@ namespace libtorrent {
int piece_length() const { return m_files.piece_length(); }
int piece_size(piece_index_t i) const { return m_files.piece_size(i); }

#ifndef TORRENT_NO_DEPRECATE
// support for BEP 30 merkle torrents has been removed

// This function returns the merkle hash tree, if the torrent was created as a merkle
// torrent. The tree is created by ``generate()`` and won't be valid until that function
// has been called. When creating a merkle tree torrent, the actual tree itself has to
// be saved off separately and fed into libtorrent the first time you start seeding it,
// through the ``torrent_info::set_merkle_tree()`` function. From that point onwards, the
// tree will be saved in the resume data.
std::vector<sha1_hash> const& merkle_tree() const { return m_merkle_tree; }
TORRENT_DEPRECATED
std::vector<sha1_hash> merkle_tree() const { return std::vector<sha1_hash>(); }
#endif

// Add similar torrents (by info-hash) or collections of similar torrents.
// Similar torrents are expected to share some files with this torrent.
Expand Down Expand Up @@ -312,11 +320,6 @@ namespace libtorrent {
std::vector<sha1_hash> m_similar;
std::vector<std::string> m_collections;

// if we're generating a merkle torrent, this is the
// merkle tree we got. This should be saved in fast-resume
// in order to start seeding the torrent
mutable aux::vector<sha1_hash> m_merkle_tree;

// dht nodes to add to the routing table/bootstrap from
std::vector<std::pair<std::string, int>> m_nodes;

Expand Down Expand Up @@ -347,9 +350,6 @@ namespace libtorrent {
// advertise itself on the DHT for this torrent
bool m_private:1;

// if set to one, a merkle torrent will be generated
bool m_merkle_torrent:1;

// if set, include the 'mtime' modification time in the
// torrent file
bool m_include_mtime:1;
Expand Down
8 changes: 7 additions & 1 deletion include/libtorrent/settings_pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,15 @@ namespace aux {
// if false, prevents libtorrent to advertise share-mode support
support_share_mode,

#if TORRENT_ABI_VERSION <= 2
// support for BEP 30 merkle torrents has been removed

// if this is false, don't advertise support for the Tribler merkle
// tree piece message
support_merkle_torrents,
support_merkle_torrents TORRENT_DEPRECATED_ENUM,
#else
deprecated_support_merkle_torrents,
#endif

// if this is true, the number of redundant bytes is sent to the
// tracker
Expand Down
2 changes: 0 additions & 2 deletions include/libtorrent/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,8 +1067,6 @@ namespace libtorrent {
{ return m_verified.get_bit(piece); }
void verified(piece_index_t piece);

bool add_merkle_nodes(std::map<int, sha1_hash> const& n, piece_index_t piece);

// this is called once periodically for torrents
// that are not private
void lsd_announce();
Expand Down
50 changes: 33 additions & 17 deletions include/libtorrent/torrent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,22 +463,17 @@ namespace libtorrent {
TORRENT_ASSERT_PRECOND(index < m_files.end_piece());
TORRENT_ASSERT(is_loaded());
int const idx = static_cast<int>(index);
if (is_merkle_torrent())
{
TORRENT_ASSERT(idx < m_merkle_tree.end_index() - m_merkle_first_leaf);
return m_merkle_tree[m_merkle_first_leaf + idx].data();
}
else
{
TORRENT_ASSERT(m_piece_hashes);
TORRENT_ASSERT(m_piece_hashes >= m_info_section.get());
TORRENT_ASSERT(m_piece_hashes < m_info_section.get() + m_info_section_size);
TORRENT_ASSERT(idx < int(m_info_section_size / 20));
return &m_piece_hashes[idx * 20];
}
TORRENT_ASSERT(m_piece_hashes);
TORRENT_ASSERT(m_piece_hashes >= m_info_section.get());
TORRENT_ASSERT(m_piece_hashes < m_info_section.get() + m_info_section_size);
TORRENT_ASSERT(idx < int(m_info_section_size / 20));
return &m_piece_hashes[idx * 20];
}

bool is_loaded() const { return m_piece_hashes || !m_merkle_tree.empty(); }
bool is_loaded() const { return m_piece_hashes; }

#if TORRENT_ABI_VERSION <= 2
// support for BEP 30 merkle torrents has been removed

// ``merkle_tree()`` returns a reference to the merkle tree for this
// torrent, if any.
Expand All @@ -489,9 +484,12 @@ namespace libtorrent {
// ``create_torrent::merkle_tree()`` function, and need to be saved
// separately from the torrent file itself. Once it's added to
// libtorrent, the merkle tree will be persisted in the resume data.
TORRENT_DEPRECATED
std::vector<sha1_hash> const& merkle_tree() const { return m_merkle_tree; }
TORRENT_DEPRECATED
void set_merkle_tree(std::vector<sha1_hash>& h)
{ TORRENT_ASSERT(h.size() == m_merkle_tree.size() ); m_merkle_tree.swap(h); }
#endif

// ``name()`` returns the name of the torrent.
// name contains UTF-8 encoded string.
Expand Down Expand Up @@ -546,16 +544,26 @@ namespace libtorrent {
boost::shared_array<char> metadata() const
{ return m_info_section; }

#if TORRENT_ABI_VERSION <= 2
// support for BEP 30 merkle torrents has been removed

// internal
bool add_merkle_nodes(std::map<int, sha1_hash> const& subtree
, piece_index_t piece);
std::map<int, sha1_hash> build_merkle_list(piece_index_t piece) const;
TORRENT_DEPRECATED
bool add_merkle_nodes(std::map<int, sha1_hash> const&
, piece_index_t) { return false; }
TORRENT_DEPRECATED
std::map<int, sha1_hash> build_merkle_list(piece_index_t) const
{
return std::map<int, sha1_hash>();
}

// returns whether or not this is a merkle torrent.
// see `BEP 30`__.
//
// __ http://bittorrent.org/beps/bep_0030.html
TORRENT_DEPRECATED
bool is_merkle_torrent() const { return !m_merkle_tree.empty(); }
#endif

bool parse_torrent_file(bdecode_node const& libtorrent, error_code& ec);

Expand Down Expand Up @@ -613,10 +621,14 @@ namespace libtorrent {
// cannot be pointers into that buffer.
std::vector<std::string> m_owned_collections;

#if TORRENT_ABI_VERSION <= 2
// if this is a merkle torrent, this is the merkle
// tree. It has space for merkle_num_nodes(merkle_num_leafs(num_pieces))
// hashes
aux::vector<sha1_hash> m_merkle_tree;
#else
aux::vector<sha1_hash> deprecated1;
#endif

// this is a copy of the info section from the torrent.
// it use maintained in this flat format in order to
Expand Down Expand Up @@ -651,9 +663,13 @@ namespace libtorrent {
// the number of bytes in m_info_section
std::int32_t m_info_section_size = 0;

#if TORRENT_ABI_VERSION <= 2
// the index to the first leaf. This is where the hash for the
// first piece is stored
std::int32_t m_merkle_first_leaf = 0;
#else
std::int32_t deprecated2 = 0;
#endif

enum flags_t : std::uint8_t
{
Expand Down
Loading

0 comments on commit 3d2d745

Please sign in to comment.