diff --git a/bindings/c/library.cpp b/bindings/c/library.cpp index 5b537a441ed..7624218fe2b 100644 --- a/bindings/c/library.cpp +++ b/bindings/c/library.cpp @@ -99,7 +99,7 @@ TORRENT_EXPORT void* session_create(int tag, ...) va_list lp; va_start(lp, tag); - fingerprint fing("LT", lt::version_major, lt::version_minor, lt::version_tiny, 0); + fingerprint find("LT", lt::version_major, lt::version_minor, lt::version_tiny, 0); std::pair listen_range(-1, -1); char const* listen_interface = "0.0.0.0"; int flags = session::start_default_features | session::add_default_plugins; @@ -112,8 +112,8 @@ TORRENT_EXPORT void* session_create(int tag, ...) case SES_FINGERPRINT: { char const* f = va_arg(lp, char const*); - fing.name[0] = f[0]; - fing.name[1] = f[1]; + find.name[0] = f[0]; + find.name[1] = f[1]; break; } case SES_LISTENPORT: @@ -123,16 +123,16 @@ TORRENT_EXPORT void* session_create(int tag, ...) listen_range.second = va_arg(lp, int); break; case SES_VERSION_MAJOR: - fing.major_version = va_arg(lp, int); + find.major_version = va_arg(lp, int); break; case SES_VERSION_MINOR: - fing.minor_version = va_arg(lp, int); + find.minor_version = va_arg(lp, int); break; case SES_VERSION_TINY: - fing.revision_version = va_arg(lp, int); + find.revision_version = va_arg(lp, int); break; case SES_VERSION_TAG: - fing.tag_version = va_arg(lp, int); + find.tag_version = va_arg(lp, int); break; case SES_FLAGS: flags = va_arg(lp, int); @@ -157,7 +157,7 @@ TORRENT_EXPORT void* session_create(int tag, ...) || listen_range.second < listen_range.first)) listen_range.second = listen_range.first; - return new (std::nothrow) session(fing, listen_range, listen_interface, flags, alert_mask); + return new (std::nothrow) session(find, listen_range, listen_interface, flags, alert_mask); } TORRENT_EXPORT void session_close(void* ses) diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 41247f29f21..56801c54552 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -177,12 +177,12 @@ list file_priorities(torrent_handle& handle) return ret; } -download_priority_t file_prioritity0(torrent_handle& h, file_index_t index) +download_priority_t file_priority0(torrent_handle& h, file_index_t index) { return h.file_priority(index); } -void file_prioritity1(torrent_handle& h, file_index_t index, download_priority_t prio) +void file_priority1(torrent_handle& h, file_index_t index, download_priority_t prio) { return h.file_priority(index, prio); } @@ -532,8 +532,8 @@ void bind_torrent_handle() .def("get_piece_priorities", &piece_priorities) .def("prioritize_files", &prioritize_files) .def("get_file_priorities", &file_priorities) - .def("file_priority", &file_prioritity0) - .def("file_priority", &file_prioritity1) + .def("file_priority", &file_priority0) + .def("file_priority", &file_priority1) .def("file_status", _(file_status0)) .def("save_resume_data", _(&torrent_handle::save_resume_data), arg("flags") = 0) .def("need_save_resume_data", _(need_save_resume_data0)) diff --git a/cmake/Modules/GeneratePkgConfig.cmake b/cmake/Modules/GeneratePkgConfig.cmake index fa523496422..49a14a6cb15 100644 --- a/cmake/Modules/GeneratePkgConfig.cmake +++ b/cmake/Modules/GeneratePkgConfig.cmake @@ -9,10 +9,10 @@ set(_GeneratePkGConfigDir "${CMAKE_CURRENT_LIST_DIR}/GeneratePkgConfig") include(GNUInstallDirs) -function(_get_target_property_merging_configs _var_name _target_name _propert_name) - get_property(prop_set TARGET ${_target_name} PROPERTY ${_propert_name} SET) +function(_get_target_property_merging_configs _var_name _target_name _property_name) + get_property(prop_set TARGET ${_target_name} PROPERTY ${_property_name} SET) if (prop_set) - get_property(vals TARGET ${_target_name} PROPERTY ${_propert_name}) + get_property(vals TARGET ${_target_name} PROPERTY ${_property_name}) else() if (CMAKE_BUILD_TYPE) list(APPEND configs ${CMAKE_BUILD_TYPE}) @@ -27,9 +27,9 @@ function(_get_target_property_merging_configs _var_name _target_name _propert_na else() set(target_cfg "${UPPERCFG}") endif() - get_property(prop_set TARGET ${_target_name} PROPERTY ${_propert_name}_${target_cfg} SET) + get_property(prop_set TARGET ${_target_name} PROPERTY ${_property_name}_${target_cfg} SET) if (prop_set) - get_property(val_for_cfg TARGET ${_target_name} PROPERTY ${_propert_name}_${target_cfg}) + get_property(val_for_cfg TARGET ${_target_name} PROPERTY ${_property_name}_${target_cfg}) list(APPEND vals "$<$:${val_for_cfg}>") break() endif() @@ -38,7 +38,7 @@ function(_get_target_property_merging_configs _var_name _target_name _propert_na get_property(imported_cfgs TARGET ${_target_name} PROPERTY IMPORTED_CONFIGURATIONS) # CMake docs say we can use any of the imported configs list(GET imported_cfgs 0 imported_config) - get_property(vals TARGET ${_target_name} PROPERTY ${_propert_name}_${imported_config}) + get_property(vals TARGET ${_target_name} PROPERTY ${_property_name}_${imported_config}) # remove config generator expression. Only in this case! Notice we use such expression # ourselves in the loop above string(REPLACE "$<$:" "$<1:" vals "${vals}") diff --git a/fuzzers/src/add_torrent.cpp b/fuzzers/src/add_torrent.cpp index a1deb1d2f74..4f1fd471cd3 100644 --- a/fuzzers/src/add_torrent.cpp +++ b/fuzzers/src/add_torrent.cpp @@ -58,9 +58,9 @@ std::vector g_tree; int const piece_size = 1024 * 1024; int const blocks_per_piece = piece_size / lt::default_block_size; int const num_pieces = 10; -int const num_leafs = merkle_num_leafs(num_pieces * blocks_per_piece); -int const num_nodes = merkle_num_nodes(num_leafs); -int const first_leaf = merkle_first_leaf(num_leafs); +int const num_leaves = merkle_num_leaves(num_pieces * blocks_per_piece); +int const num_nodes = merkle_num_nodes(num_leaves); +int const first_leaf = merkle_first_leaf(num_leaves); extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { @@ -113,7 +113,7 @@ extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) t.set_hash2(file_index_t{0}, piece_index_t::diff_type(i), r); } - merkle_fill_tree(g_tree, num_leafs); + merkle_fill_tree(g_tree, num_leaves); std::vector buf; bencode(std::back_inserter(buf), t.generate()); @@ -159,7 +159,7 @@ lt::add_torrent_params generate_atp(std::uint8_t const* data, size_t size) ret.merkle_trees.resize(1); ret.merkle_tree_mask.resize(1); ret.verified_leaf_hashes.resize(1); - ret.verified_leaf_hashes[0].resize(num_leafs, true); + ret.verified_leaf_hashes[0].resize(num_leaves, true); auto& t = ret.merkle_trees[0]; auto& mask = ret.merkle_tree_mask[0]; diff --git a/include/libtorrent/aux_/merkle.hpp b/include/libtorrent/aux_/merkle.hpp index 349961dfbe7..fe76713891a 100644 --- a/include/libtorrent/aux_/merkle.hpp +++ b/include/libtorrent/aux_/merkle.hpp @@ -54,14 +54,14 @@ namespace libtorrent { // given the number of blocks, how many leaves do we need? this rounds up to // an even power of 2 - TORRENT_EXTRA_EXPORT int merkle_num_leafs(int); + TORRENT_EXTRA_EXPORT int merkle_num_leaves(int); // returns the number of nodes in the tree, given the number of leaves TORRENT_EXTRA_EXPORT int merkle_num_nodes(int); - // given the number of leafs in the tree, returns the index to the first + // given the number of leaves in the tree, returns the index to the first // leaf - TORRENT_EXTRA_EXPORT int merkle_first_leaf(int num_leafs); + TORRENT_EXTRA_EXPORT int merkle_first_leaf(int num_leaves); // takes the number of leaves and returns the height of the merkle tree. // does not include the root node in the layer count. i.e. if there's only a @@ -75,8 +75,8 @@ namespace libtorrent { // given a tree and the number of leaves, expect all leaf hashes to be set and // compute all other hashes starting with the leaves. - TORRENT_EXTRA_EXPORT void merkle_fill_tree(span tree, int num_leafs, int level_start); - TORRENT_EXTRA_EXPORT void merkle_fill_tree(span tree, int num_leafs); + TORRENT_EXTRA_EXPORT void merkle_fill_tree(span tree, int num_leaves, int level_start); + TORRENT_EXTRA_EXPORT void merkle_fill_tree(span tree, int num_leaves); // fills in nodes that can be computed from a tree with arbitrary nodes set // all "orphan" hashes, i.e ones that do not contribute towards computing @@ -84,10 +84,10 @@ namespace libtorrent { TORRENT_EXTRA_EXPORT void merkle_fill_partial_tree(span tree); // given a merkle tree (`tree`), clears all hashes in the range of nodes: - // [ level_start, level_start+ num_leafs), as well as all of their parents, + // [ level_start, level_start+ num_leaves), as well as all of their parents, // within the sub-tree. It does not clear the root of the sub-tree. // see unit test for examples. - TORRENT_EXTRA_EXPORT void merkle_clear_tree(span tree, int num_leafs, int level_start); + TORRENT_EXTRA_EXPORT void merkle_clear_tree(span tree, int num_leaves, int level_start); // given the leaf hashes, computes the merkle root hash. The pad is the hash // to use for the right-side padding, in case the number of leaves is not a @@ -95,7 +95,7 @@ namespace libtorrent { TORRENT_EXTRA_EXPORT sha256_hash merkle_root(span leaves, sha256_hash const& pad = {}); TORRENT_EXTRA_EXPORT - sha256_hash merkle_root_scratch(span leaves, int num_leafs + sha256_hash merkle_root_scratch(span leaves, int num_leaves , sha256_hash pad, std::vector& scratch_space); // given a flat index, return which layer the node is in @@ -103,7 +103,7 @@ namespace libtorrent { // given a flat index, return the offset in the layer TORRENT_EXTRA_EXPORT int merkle_get_layer_offset(int idx); - // given "blocks" number of leafs in the full tree (i.e. at the block level) + // given "blocks" number of leaves in the full tree (i.e. at the block level) // and given "pieces" nodes in the piece layer, compute the pad hash for the // piece layer TORRENT_EXTRA_EXPORT sha256_hash merkle_pad(int blocks, int pieces); @@ -141,22 +141,22 @@ namespace libtorrent { // hashes in dst that are invalid in src. TORRENT_EXTRA_EXPORT void merkle_validate_copy(span src, span dst - , sha256_hash const& root, bitfield& verified_leafs); + , sha256_hash const& root, bitfield& verified_leaves); TORRENT_EXTRA_EXPORT bool merkle_validate_single_layer(span tree); // given a leaf index (0-based index in the leaf layer) and a tree, return - // the leafs_start, leafs_size and root_index representing a subtree that + // the leaves_start, leaves_size and root_index representing a subtree that // can be validated. The block_index and leaf_size is the range of the leaf // layer that can be verified, and the root_index is the node that needs to - // be known in (tree) to do so. The num_valid_leafs specifies how many of - // the leafs that are actually *supposed* to be non-zero. Any leafs beyond + // be known in (tree) to do so. The num_valid_leaves specifies how many of + // the leaves that are actually *supposed* to be non-zero. Any leaves beyond // these are padding and expected to be zero. // The caller must validate the hash at root_index. TORRENT_EXTRA_EXPORT std::tuple merkle_find_known_subtree(span const tree - , int block_index, int num_valid_leafs); + , int block_index, int num_valid_leaves); } #endif diff --git a/include/libtorrent/aux_/merkle_tree.hpp b/include/libtorrent/aux_/merkle_tree.hpp index 3057d2aec5d..0df444e5113 100644 --- a/include/libtorrent/aux_/merkle_tree.hpp +++ b/include/libtorrent/aux_/merkle_tree.hpp @@ -108,7 +108,7 @@ struct TORRENT_EXTRA_EXPORT merkle_tree std::pair, aux::vector> build_sparse_vector() const; // get bits indicating if each leaf hash is verified - std::vector verified_leafs() const; + std::vector verified_leaves() const; // returns true if the entire tree is known and verified bool is_complete() const; @@ -118,7 +118,7 @@ struct TORRENT_EXTRA_EXPORT merkle_tree bool load_piece_layer(span piece_layer); - // the leafs in "tree" must be block hashes (i.e. leaf hashes in the this + // the leaves in "tree" must be block hashes (i.e. leaf hashes in the this // tree). This function inserts those hashes as well as the nodes up the // tree. The destination start index is the index, in this tree, to the first leaf // where "tree" will be inserted. @@ -161,7 +161,7 @@ struct TORRENT_EXTRA_EXPORT merkle_tree int block_layer_start() const; int piece_layer_start() const; int num_pieces() const; - int num_leafs() const; + int num_leaves() const; void optimize_storage(); void optimize_storage_piece_layer(); @@ -181,7 +181,7 @@ struct TORRENT_EXTRA_EXPORT merkle_tree // yet bitfield m_block_verified; - // number of blocks in the file this tree represents. The number of leafs in + // number of blocks in the file this tree represents. The number of leaves in // the tree is rounded up to an even power of 2. int m_num_blocks = 0; diff --git a/include/libtorrent/file_storage.hpp b/include/libtorrent/file_storage.hpp index 80503973903..9e9b2df8dae 100644 --- a/include/libtorrent/file_storage.hpp +++ b/include/libtorrent/file_storage.hpp @@ -231,7 +231,7 @@ namespace aux { // internal limitations restrict file sizes to not be larger than this // We use int to index into file merkle trees, so a file may not contain more - // than INT_MAX entries. That means INT_MAX / 2 blocks (leafs) in each + // than INT_MAX entries. That means INT_MAX / 2 blocks (leaves) in each // tree. static constexpr std::int64_t max_file_size = (std::min)( (std::int64_t(1) << 48) - 1 diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index ff6faccb432..6680817dd8f 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -714,7 +714,7 @@ TORRENT_VERSION_NAMESPACE_3 #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)) + // tree. It has space for merkle_num_nodes(merkle_num_leaves(num_pieces)) // hashes aux::vector m_merkle_tree; #endif diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index fa480c06b8c..0dfb58efa73 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1293,7 +1293,7 @@ namespace { // subtract one because the the base layer doesn't count int const proof_hashes = std::max(0 - , proof_layers - (merkle_num_layers(merkle_num_leafs(count)) - 1)); + , proof_layers - (merkle_num_layers(merkle_num_leaves(count)) - 1)); if (m_recv_buffer.packet_size() != header_size + (count + proof_hashes) * int(sha256_hash::size())) diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 4b1244f82fe..3b9420a4195 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -191,19 +191,19 @@ namespace { TORRENT_ASSERT(file_size > 0); auto const file_blocks = st->ct.files().file_num_blocks(current_file); auto const piece_blocks = st->ct.files().blocks_in_piece2(piece); - int const num_leafs = merkle_num_leafs(file_blocks); + int const num_leaves = merkle_num_leaves(file_blocks); // If the file is smaller than one piece then the block hashes // should be padded to the next power of two instead of the next // piece boundary. - int const padded_leafs = file_size < st->ct.piece_length() - ? num_leafs + int const padded_leaves = file_size < st->ct.piece_length() + ? num_leaves : st->ct.piece_length() / default_block_size; - TORRENT_ASSERT(padded_leafs <= int(v2_blocks.size())); - for (auto i = piece_blocks; i < padded_leafs; ++i) + TORRENT_ASSERT(padded_leaves <= int(v2_blocks.size())); + for (auto i = piece_blocks; i < padded_leaves; ++i) v2_blocks[i].clear(); sha256_hash const piece_root = merkle_root( - span(v2_blocks).first(padded_leafs)); + span(v2_blocks).first(padded_leaves)); st->ct.set_hash2(current_file, file_piece_offset, piece_root); } } diff --git a/src/file_storage.cpp b/src/file_storage.cpp index 37dd153f996..84250a626d5 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -1110,7 +1110,7 @@ namespace { { TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file()); TORRENT_ASSERT_PRECOND(m_piece_length > 0); - int const piece_layer_size = merkle_num_leafs(file_num_pieces(index)); + int const piece_layer_size = merkle_num_leaves(file_num_pieces(index)); return merkle_num_nodes(piece_layer_size) - piece_layer_size; } @@ -1118,7 +1118,7 @@ namespace { { TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file()); TORRENT_ASSERT_PRECOND(m_piece_length > 0); - int const leaf_layer_size = merkle_num_leafs(file_num_blocks(index)); + int const leaf_layer_size = merkle_num_leaves(file_num_blocks(index)); return merkle_num_nodes(leaf_layer_size) - leaf_layer_size; } diff --git a/src/hash_picker.cpp b/src/hash_picker.cpp index 08d15fa4cca..e8a30caecf7 100644 --- a/src/hash_picker.cpp +++ b/src/hash_picker.cpp @@ -74,13 +74,13 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) || hr.proof_layers < 0) return false; - int const num_leafs = merkle_num_leafs(fs.file_num_blocks(hr.file)); - int const num_layers = merkle_num_layers(num_leafs); + int const num_leaves = merkle_num_leaves(fs.file_num_blocks(hr.file)); + int const num_layers = merkle_num_layers(num_leaves); if (hr.base >= num_layers) return false; // the number of hashes at the specified level - int const level_size = num_leafs >> hr.base; + int const level_size = num_leaves >> hr.base; // [index, index + count] must be within the number of nodes at the specified // level @@ -105,7 +105,7 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) if (m_files.pad_file_at(f)) continue; auto const& tree = m_merkle_trees[f]; - auto const v = tree.verified_leafs(); + auto const v = tree.verified_leaves(); if (m_files.file_size(f) <= m_files.piece_length()) continue; @@ -113,7 +113,7 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) m_piece_hash_requested[f].resize((m_files.file_num_pieces(f) + 511) / 512); int const piece_layer_idx = merkle_num_layers( - merkle_num_leafs(m_files.file_num_blocks(f))) - m_piece_layer; + merkle_num_leaves(m_files.file_num_blocks(f))) - m_piece_layer; int const piece_layer_start = merkle_layer_start(piece_layer_idx); // check for hashes we already have and flag entries in m_piece_hash_requested @@ -224,7 +224,7 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) return hash_request(fidx , m_piece_layer , i * 512 - , std::min(512, merkle_num_leafs(int(m_files.file_num_pieces(fidx) - i * 512))) + , std::min(512, merkle_num_leaves(int(m_files.file_num_pieces(fidx) - i * 512))) , layers_to_verify({ fidx, piece_tree_root }) + piece_tree_num_layers); } } @@ -237,7 +237,7 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) TORRENT_ASSERT(validate_hash_request(req, m_files)); int const unpadded_count = std::min(req.count, m_files.file_num_pieces(req.file) - req.index); - int const leaf_count = merkle_num_leafs(req.count); + int const leaf_count = merkle_num_leaves(req.count); int const base_num_layers = merkle_num_layers(leaf_count); int const num_uncle_hashes = std::max(0, req.proof_layers - base_num_layers + 1); @@ -308,9 +308,9 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) // TODO: use structured bindings in C++17 aux::merkle_tree::set_block_result result; - int leafs_index; - int leafs_size; - std::tie(result, leafs_index, leafs_size) = merkle_tree.set_block(block_index, h); + int leaves_index; + int leaves_size; + std::tie(result, leaves_index, leaves_size) = merkle_tree.set_block(block_index, h); if (result == aux::merkle_tree::set_block_result::unknown) return set_block_hash_result::unknown(); @@ -324,8 +324,8 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) int const blocks_per_piece = m_files.piece_length() / default_block_size; return { status - , int(leafs_index - static_cast(piece - file_first_piece) * blocks_per_piece) - , std::min(leafs_size, m_files.file_num_pieces(f) * blocks_per_piece - leafs_index) }; + , int(leaves_index - static_cast(piece - file_first_piece) * blocks_per_piece) + , std::min(leaves_size, m_files.file_num_pieces(f) * blocks_per_piece - leaves_index) }; } void hash_picker::hashes_rejected(hash_request const& req) @@ -408,7 +408,7 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) if (idx.node == 0) return -1; int layers = 0; - int const file_internal_layers = merkle_num_layers(merkle_num_leafs(m_files.file_num_pieces(idx.file))) - 1; + int const file_internal_layers = merkle_num_layers(merkle_num_leaves(m_files.file_num_pieces(idx.file))) - 1; auto const& tree = m_merkle_trees[idx.file]; for (;;) @@ -424,6 +424,6 @@ bool validate_hash_request(hash_request const& hr, file_storage const& fs) int hash_picker::file_num_layers(file_index_t const idx) const { - return merkle_num_layers(merkle_num_leafs(m_files.file_num_blocks(idx))); + return merkle_num_layers(merkle_num_leaves(m_files.file_num_blocks(idx))); } } diff --git a/src/load_torrent.cpp b/src/load_torrent.cpp index 9232daa46ee..1ec39184f22 100644 --- a/src/load_torrent.cpp +++ b/src/load_torrent.cpp @@ -83,9 +83,9 @@ namespace { layer.emplace_back(bytes.data() + i); int const full_size = merkle_num_nodes( - merkle_num_leafs(fs.file_num_blocks(f))); + merkle_num_leaves(fs.file_num_blocks(f))); int const num_pieces = fs.file_num_pieces(f); - int const piece_layer_size = merkle_num_leafs(num_pieces); + int const piece_layer_size = merkle_num_leaves(num_pieces); if (!layer.empty()) { diff --git a/src/merkle.cpp b/src/merkle.cpp index b7b45e479e5..2d38ba97e61 100644 --- a/src/merkle.cpp +++ b/src/merkle.cpp @@ -79,27 +79,27 @@ namespace libtorrent { return ((tree_node + 1) << depth) - 1; } - int merkle_num_nodes(int const leafs) + int merkle_num_nodes(int const leaves) { - TORRENT_ASSERT(leafs > 0); - TORRENT_ASSERT(leafs <= (std::numeric_limits::max() / 2) + 1); - TORRENT_ASSERT((leafs & (leafs - 1)) == 0); - // This is a way to calculate: (leafs << 1) - 1 without requiring an extra + TORRENT_ASSERT(leaves > 0); + TORRENT_ASSERT(leaves <= (std::numeric_limits::max() / 2) + 1); + TORRENT_ASSERT((leaves & (leaves - 1)) == 0); + // This is a way to calculate: (leaves << 1) - 1 without requiring an extra // bit in the far left. The first 1 we subtract is worth 2 after we // multiply by 2, so by just adding back one, we have effectively // subtracted one from the result of multiplying by 2 - return ((leafs - 1) << 1) + 1; + return ((leaves - 1) << 1) + 1; } - int merkle_first_leaf(int num_leafs) + int merkle_first_leaf(int num_leaves) { - // num_leafs must be a power of 2 - TORRENT_ASSERT(((num_leafs - 1) & num_leafs) == 0); - TORRENT_ASSERT(num_leafs > 0); - return num_leafs - 1; + // num_leaves must be a power of 2 + TORRENT_ASSERT(((num_leaves - 1) & num_leaves) == 0); + TORRENT_ASSERT(num_leaves > 0); + return num_leaves - 1; } - int merkle_num_leafs(int const blocks) + int merkle_num_leaves(int const blocks) { TORRENT_ASSERT(blocks > 0); TORRENT_ASSERT(blocks <= std::numeric_limits::max() / 2); @@ -122,17 +122,17 @@ namespace libtorrent { return layers; } - void merkle_fill_tree(span tree, int const num_leafs) + void merkle_fill_tree(span tree, int const num_leaves) { - merkle_fill_tree(tree, num_leafs, merkle_num_nodes(num_leafs) - num_leafs); + merkle_fill_tree(tree, num_leaves, merkle_num_nodes(num_leaves) - num_leaves); } - void merkle_fill_tree(span tree, int const num_leafs, int level_start) + void merkle_fill_tree(span tree, int const num_leaves, int level_start) { TORRENT_ASSERT(level_start >= 0); - TORRENT_ASSERT(num_leafs >= 1); + TORRENT_ASSERT(num_leaves >= 1); - int level_size = num_leafs; + int level_size = num_leaves; while (level_size > 1) { int parent = merkle_get_parent(level_start); @@ -159,9 +159,9 @@ namespace libtorrent { // "interior" hashes. Then to clear all the ones that don't have a // parent (i.e. "orphan" hashes). We clear them since we can't validate // them against the root, which mean they may be incorrect. - int const num_leafs = (num_nodes + 1) / 2; - int level_size = num_leafs; - int level_start = merkle_first_leaf(num_leafs); + int const num_leaves = (num_nodes + 1) / 2; + int level_size = num_leaves; + int level_start = merkle_first_leaf(num_leaves); while (level_size > 1) { level_start = merkle_get_parent(level_start); @@ -204,16 +204,16 @@ namespace libtorrent { } } - void merkle_clear_tree(span tree, int const num_leafs, int level_start) + void merkle_clear_tree(span tree, int const num_leaves, int level_start) { - TORRENT_ASSERT(num_leafs >= 1); + TORRENT_ASSERT(num_leaves >= 1); TORRENT_ASSERT(level_start >= 0); TORRENT_ASSERT(level_start < tree.size()); - TORRENT_ASSERT(level_start + num_leafs <= tree.size()); + TORRENT_ASSERT(level_start + num_leaves <= tree.size()); // the range of nodes must be within a single level - TORRENT_ASSERT(merkle_get_layer(level_start) == merkle_get_layer(level_start + num_leafs - 1)); + TORRENT_ASSERT(merkle_get_layer(level_start) == merkle_get_layer(level_start + num_leaves - 1)); - int level_size = num_leafs; + int level_size = num_leaves; for (;;) { for (int i = level_start; i < level_start + level_size; ++i) @@ -229,25 +229,25 @@ namespace libtorrent { // padding sha256_hash merkle_root(span const leaves, sha256_hash const& pad) { - int const num_leafs = merkle_num_leafs(int(leaves.size())); + int const num_leaves = merkle_num_leaves(int(leaves.size())); aux::vector merkle_tree; - return merkle_root_scratch(leaves, num_leafs, pad, merkle_tree); + return merkle_root_scratch(leaves, num_leaves, pad, merkle_tree); } // compute the merkle tree root, given the leaves and the has to use for // padding sha256_hash merkle_root_scratch(span leaves - , int num_leafs, sha256_hash pad + , int num_leaves, sha256_hash pad , std::vector& scratch_space) { - TORRENT_ASSERT(((num_leafs - 1) & num_leafs) == 0); + TORRENT_ASSERT(((num_leaves - 1) & num_leaves) == 0); scratch_space.resize(std::size_t(leaves.size() + 1) / 2); - TORRENT_ASSERT(num_leafs > 0); + TORRENT_ASSERT(num_leaves > 0); - if (num_leafs == 1) return leaves[0]; + if (num_leaves == 1) return leaves[0]; - while (num_leafs > 1) + while (num_leaves > 1) { int i = 0; for (; i < int(leaves.size()) / 2; ++i) @@ -273,7 +273,7 @@ namespace libtorrent { // step one level up leaves = span(scratch_space.data(), i); - num_leafs /= 2; + num_leaves /= 2; } return scratch_space[0]; @@ -366,14 +366,14 @@ namespace libtorrent { void merkle_validate_copy(span const src , span const dst, sha256_hash const& root - , bitfield& verified_leafs) + , bitfield& verified_leaves) { TORRENT_ASSERT(src.size() == dst.size()); - int const num_leafs = int((dst.size() + 1) / 2); + int const num_leaves = int((dst.size() + 1) / 2); if (src.empty()) return; if (src[0] != root) return; dst[0] = src[0]; - int const leaf_layer_start = int(src.size() - num_leafs); + int const leaf_layer_start = int(src.size() - num_leaves); for (int i = 0; i < leaf_layer_start; ++i) { if (dst[i].is_all_zeros()) continue; @@ -385,13 +385,13 @@ namespace libtorrent { dst[right_child] = src[right_child]; int const block_idx = left_child - leaf_layer_start; if (left_child >= leaf_layer_start - && block_idx < verified_leafs.size()) + && block_idx < verified_leaves.size()) { - verified_leafs.set_bit(block_idx); + verified_leaves.set_bit(block_idx); // the right child may be the first block of padding hash, // in which case it's not part of the verified bitfield - if (block_idx + 1 < verified_leafs.size()) - verified_leafs.set_bit(block_idx + 1); + if (block_idx + 1 < verified_leaves.size()) + verified_leaves.set_bit(block_idx + 1); } } } @@ -400,11 +400,11 @@ namespace libtorrent { bool merkle_validate_single_layer(span tree) { if (tree.size() == 1) return true; - int const num_leafs = int((tree.size() + 1) / 2); + int const num_leaves = int((tree.size() + 1) / 2); int const end = int(tree.size()); - TORRENT_ASSERT((num_leafs & (num_leafs - 1)) == 0); + TORRENT_ASSERT((num_leaves & (num_leaves - 1)) == 0); - int idx = merkle_first_leaf(num_leafs); + int idx = merkle_first_leaf(num_leaves); TORRENT_ASSERT(idx >= 1); while (idx < end) @@ -417,38 +417,38 @@ namespace libtorrent { } std::tuple merkle_find_known_subtree(span const tree - , int const block_index, int const num_valid_leafs) + , int const block_index, int const num_valid_leaves) { - // find the largest block of leafs from a single subtree we know the hashes of - int leafs_start = block_index; - int leafs_size = 1; + // find the largest block of leaves from a single subtree we know the hashes of + int leaves_start = block_index; + int leaves_size = 1; int const first_leaf = int(tree.size() / 2); int root_index = merkle_get_sibling(first_leaf + block_index); for (int i = block_index;; i >>= 1) { - int const first_check_index = leafs_start + ((i & 1) ? -leafs_size : leafs_size); - for (int j = 0; j < std::min(leafs_size, num_valid_leafs - first_check_index); ++j) + int const first_check_index = leaves_start + ((i & 1) ? -leaves_size : leaves_size); + for (int j = 0; j < std::min(leaves_size, num_valid_leaves - first_check_index); ++j) { if (tree[first_leaf + first_check_index + j].is_all_zeros()) - return std::make_tuple(leafs_start, leafs_size, root_index); + return std::make_tuple(leaves_start, leaves_size, root_index); } - if (i & 1) leafs_start -= leafs_size; - leafs_size *= 2; + if (i & 1) leaves_start -= leaves_size; + leaves_size *= 2; root_index = merkle_get_parent(root_index); // if an inner node is known then its parent must be known too // so if the root is known the next sibling subtree should already - // be computed if all of its leafs have valid hashes + // be computed if all of its leaves have valid hashes if (!tree[root_index].is_all_zeros()) break; TORRENT_ASSERT(root_index != 0); - TORRENT_ASSERT(leafs_start >= 0); - TORRENT_ASSERT(leafs_size <= merkle_num_leafs(num_valid_leafs)); + TORRENT_ASSERT(leaves_start >= 0); + TORRENT_ASSERT(leaves_size <= merkle_num_leaves(num_valid_leaves)); } - TORRENT_ASSERT(leafs_start >= 0); - TORRENT_ASSERT(leafs_start < merkle_num_leafs(num_valid_leafs)); - TORRENT_ASSERT(leafs_start + leafs_size > block_index); + TORRENT_ASSERT(leaves_start >= 0); + TORRENT_ASSERT(leaves_start < merkle_num_leaves(num_valid_leaves)); + TORRENT_ASSERT(leaves_start + leaves_size > block_index); - return std::make_tuple(leafs_start, leafs_size, root_index); + return std::make_tuple(leaves_start, leaves_size, root_index); } } diff --git a/src/merkle_tree.cpp b/src/merkle_tree.cpp index ccf9bc68a4f..8e448869f4d 100644 --- a/src/merkle_tree.cpp +++ b/src/merkle_tree.cpp @@ -231,8 +231,8 @@ namespace { int const blocks_in_piece = blocks_per_piece(); for (int b = 0; b < int(m_tree.size()); b += blocks_in_piece) { - auto const leafs = span(m_tree).subspan(b); - ret.push_back(merkle_root_scratch(leafs, blocks_in_piece, sha256_hash{}, scratch_space)); + auto const leaves = span(m_tree).subspan(b); + ret.push_back(merkle_root_scratch(leaves, blocks_in_piece, sha256_hash{}, scratch_space)); } break; } @@ -318,14 +318,14 @@ namespace { // TODO: this can be optimized by using m_tree as storage to fill this // tree into, and then clear it if the hashes fail - int const leaf_count = merkle_num_leafs(int(hashes.size())); + int const leaf_count = merkle_num_leaves(int(hashes.size())); aux::vector tree(merkle_num_nodes(leaf_count)); std::copy(hashes.begin(), hashes.end(), tree.end() - leaf_count); // the end of a file is a special case, we may need to pad the leaf layer if (leaf_count > hashes.size()) { - int const leaf_layer_size = num_leafs(); + int const leaf_layer_size = num_leaves(); // assuming uncle_hashes lead all the way to the root, they tell us // how many layers down we are int const insert_layer_size = leaf_count << uncle_hashes.size(); @@ -351,8 +351,8 @@ namespace { return {}; // first fill in the subtree of known hashes from the base layer - auto const num_leafs = merkle_num_leafs(m_num_blocks); - auto const first_leaf = merkle_first_leaf(num_leafs); + auto const num_leaves = merkle_num_leaves(m_num_blocks); + auto const first_leaf = merkle_first_leaf(num_leaves); // this is the start of the leaf layer of "tree". We'll use this // variable to step upwards towards the root @@ -466,11 +466,11 @@ namespace { { ret.passed.push_back(piece_index_t{piece - first_piece_idx} + file_piece_offset); // record that these block hashes are correct! - int const leafs_start = block_idx - block_layer_start(); - int const leafs_end = std::min(m_num_blocks, leafs_start + blocks_in_piece); + int const leaves_start = block_idx - block_layer_start(); + int const leaves_end = std::min(m_num_blocks, leaves_start + blocks_in_piece); // TODO: this could be done more efficiently if bitfield had a function // to set a range of bits - for (int k = leafs_start; k < leafs_end; ++k) + for (int k = leaves_start; k < leaves_end; ++k) m_block_verified.set_bit(k); } TORRENT_ASSERT((piece - first_piece_idx) >= 0); @@ -490,8 +490,8 @@ namespace { #endif TORRENT_ASSERT(block_index < m_num_blocks); - auto const num_leafs = merkle_num_leafs(m_num_blocks); - auto const first_leaf = merkle_first_leaf(num_leafs); + auto const num_leaves = merkle_num_leaves(m_num_blocks); + auto const first_leaf = merkle_first_leaf(num_leaves); auto const block_tree_index = first_leaf + block_index; if (blocks_verified(block_index, 1)) @@ -512,34 +512,34 @@ namespace { // computable subtree is known // TODO: use structured binding in C++17 - int leafs_start; - int leafs_size; + int leaves_start; + int leaves_size; int root_index; - std::tie(leafs_start, leafs_size, root_index) = + std::tie(leaves_start, leaves_size, root_index) = merkle_find_known_subtree(m_tree, block_index, m_num_blocks); // if the root node is unknown the hashes cannot be verified yet if (m_tree[root_index].is_all_zeros()) - return std::make_tuple(set_block_result::unknown, leafs_start, leafs_size); + return std::make_tuple(set_block_result::unknown, leaves_start, leaves_size); // save the root hash because merkle_fill_tree will overwrite it sha256_hash const root = m_tree[root_index]; - merkle_fill_tree(m_tree, leafs_size, first_leaf + leafs_start); + merkle_fill_tree(m_tree, leaves_size, first_leaf + leaves_start); if (root != m_tree[root_index]) { // hash failure, clear all the internal nodes // the whole piece failed the hash check. Clear all block hashes // in this piece and report a hash failure - merkle_clear_tree(m_tree, leafs_size, first_leaf + leafs_start); + merkle_clear_tree(m_tree, leaves_size, first_leaf + leaves_start); m_tree[root_index] = root; - return std::make_tuple(set_block_result::hash_failed, leafs_start, leafs_size); + return std::make_tuple(set_block_result::hash_failed, leaves_start, leaves_size); } // TODO: this could be done more efficiently if bitfield had a function // to set a range of bits - int const leafs_end = std::min(m_num_blocks, leafs_start + leafs_size); - for (int i = leafs_start; i < leafs_end; ++i) + int const leaves_end = std::min(m_num_blocks, leaves_start + leaves_size); + for (int i = leaves_start; i < leaves_end; ++i) m_block_verified.set_bit(i); // attempting to optimize storage is quite costly, only do it if we have @@ -547,12 +547,12 @@ namespace { if (block_index == m_num_blocks - 1 || !m_tree[block_tree_index + 1].is_all_zeros()) optimize_storage(); - return std::make_tuple(set_block_result::ok, leafs_start, leafs_size); + return std::make_tuple(set_block_result::ok, leaves_start, leaves_size); } std::size_t merkle_tree::size() const { - return static_cast(merkle_num_nodes(merkle_num_leafs(m_num_blocks))); + return static_cast(merkle_num_nodes(merkle_num_leaves(m_num_blocks))); } int merkle_tree::num_pieces() const @@ -564,21 +564,21 @@ namespace { int merkle_tree::block_layer_start() const { - int const num_leafs = merkle_num_leafs(m_num_blocks); - TORRENT_ASSERT(num_leafs > 0); - return merkle_first_leaf(num_leafs); + int const num_leaves = merkle_num_leaves(m_num_blocks); + TORRENT_ASSERT(num_leaves > 0); + return merkle_first_leaf(num_leaves); } int merkle_tree::piece_layer_start() const { - int const piece_layer_size = merkle_num_leafs(num_pieces()); + int const piece_layer_size = merkle_num_leaves(num_pieces()); TORRENT_ASSERT(piece_layer_size > 0); return merkle_first_leaf(piece_layer_size); } - int merkle_tree::num_leafs() const + int merkle_tree::num_leaves() const { - return merkle_num_leafs(m_num_blocks); + return merkle_num_leaves(m_num_blocks); } bool merkle_tree::has_node(int const idx) const @@ -615,7 +615,7 @@ namespace { int const first = piece_layer_start(); int const piece_count = num_pieces(); int const pieces_end = first + piece_count; - int const piece_layer_size = merkle_num_leafs(piece_count); + int const piece_layer_size = merkle_num_leaves(piece_count); int const end = first + piece_layer_size; if (idx >= end) return h.is_all_zeros(); @@ -712,7 +712,7 @@ namespace { break; case mode_t::piece_layer: { - int const piece_layer_size = merkle_num_leafs(num_pieces()); + int const piece_layer_size = merkle_num_leaves(num_pieces()); sha256_hash const pad_hash = merkle_pad(1 << m_blocks_per_piece_log, 1); int const start = merkle_first_leaf(piece_layer_size); TORRENT_ASSERT(m_tree.end_index() <= piece_layer_size); @@ -724,12 +724,12 @@ namespace { } case mode_t::block_layer: { - int const num_leafs = merkle_num_leafs(m_num_blocks); + int const num_leaves = merkle_num_leaves(m_num_blocks); sha256_hash const pad_hash{}; - int const start = merkle_first_leaf(num_leafs); + int const start = merkle_first_leaf(num_leaves); std::copy(m_tree.begin(), m_tree.end(), ret.begin() + start); - std::fill(ret.begin() + start + m_tree.end_index(), ret.begin() + start + num_leafs, sha256_hash{}); - merkle_fill_tree(ret, num_leafs); + std::fill(ret.begin() + start + m_tree.end_index(), ret.begin() + start + num_leaves, sha256_hash{}); + merkle_fill_tree(ret, num_leaves); break; } } @@ -757,7 +757,7 @@ namespace { break; case mode_t::piece_layer: { - int const piece_layer_size = merkle_num_leafs(num_pieces()); + int const piece_layer_size = merkle_num_leaves(num_pieces()); for (int i = merkle_first_leaf(piece_layer_size), end = i + m_tree.end_index(); i < end; ++i) mask[i] = true; ret = m_tree; @@ -765,8 +765,8 @@ namespace { } case mode_t::block_layer: { - int const num_leafs = merkle_num_leafs(m_num_blocks); - for (int i = merkle_first_leaf(num_leafs), end = i + m_tree.end_index(); i < end; ++i) + int const num_leaves = merkle_num_leaves(m_num_blocks); + for (int i = merkle_first_leaf(num_leaves), end = i + m_tree.end_index(); i < end; ++i) mask[i] = true; ret = m_tree; break; @@ -775,7 +775,7 @@ namespace { return {std::move(ret), std::move(mask)}; } - std::vector merkle_tree::verified_leafs() const + std::vector merkle_tree::verified_leaves() const { // note that for an empty tree (where the root is the full tree) and a // tree where we have the piece layer, we also know all leaves in case @@ -894,7 +894,7 @@ namespace { // if we have *any* blocks, we can't transition into piece layer mode, // since we would lose those hashes - int const piece_layer_size = merkle_num_leafs(num_pieces()); + int const piece_layer_size = merkle_num_leaves(num_pieces()); if (m_blocks_per_piece_log > 0 && merkle_validate_single_layer(span(m_tree).subspan(0, merkle_num_nodes(piece_layer_size))) && std::all_of(m_tree.begin() + block_layer_start(), m_tree.end(), [](sha256_hash const& h) { return h.is_all_zeros(); }) @@ -914,7 +914,7 @@ namespace { , int const index, int const count, int const proof_layers) const { // given the full size of the tree, half of it, rounded up, are leaf nodes - int const base_layer_idx = merkle_num_layers(num_leafs()) - base; + int const base_layer_idx = merkle_num_layers(num_leaves()) - base; int const base_start_idx = merkle_to_flat_index(base_layer_idx, index); int const layer_start_idx = base_start_idx; @@ -948,7 +948,7 @@ namespace { // the number of layers up the tree which can be computed from the base layer hashes // subtract one because the base layer doesn't count - int const base_tree_layers = merkle_num_layers(merkle_num_leafs(count)) - 1; + int const base_tree_layers = merkle_num_layers(merkle_num_leaves(count)) - 1; int proof_idx = layer_start_idx; for (int i = 0; i < proof_layers; ++i) @@ -996,14 +996,14 @@ namespace { TORRENT_ASSERT(m_tree[0] == root()); TORRENT_ASSERT(m_block_verified.size() == m_num_blocks); - auto const num_leafs = merkle_num_leafs(m_num_blocks); + auto const num_leaves = merkle_num_leaves(m_num_blocks); if (m_tree.size() == 1) break; // In all layers, except the block layer, all non-zero hashes // must have a non-zero sibling and they must validate with // their parent. - for (int i = 1; i < int(m_tree.size()) - num_leafs; i += 2) + for (int i = 1; i < int(m_tree.size()) - num_leaves; i += 2) { if (m_tree[i].is_all_zeros()) { @@ -1019,7 +1019,7 @@ namespace { // validate all blocks (that can be validated) // since these are checked in pairs, we skip 2, to always // consider the left side of the pair - auto const first_block = merkle_first_leaf(num_leafs); + auto const first_block = merkle_first_leaf(num_leaves); for (int i = first_block, b = 0; i < first_block + m_num_blocks; i += 2, b += 2) { if (i + 1 == first_block + m_num_blocks) diff --git a/src/torrent.cpp b/src/torrent.cpp index acfb3b194dd..d0faba948a8 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -7211,7 +7211,7 @@ namespace { std::tie(sparse_tree, mask) = t.build_sparse_vector(); ret.merkle_trees.emplace_back(std::move(sparse_tree)); ret.merkle_tree_mask.emplace_back(std::move(mask)); - ret.verified_leaf_hashes.emplace_back(t.verified_leafs()); + ret.verified_leaf_hashes.emplace_back(t.verified_leaves()); } if (!has_hash_picker() && !m_have_all) diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index ff32dc602bc..ef16885ce93 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -956,13 +956,13 @@ std::shared_ptr create_torrent(std::ostream* file if (!(flags & create_torrent::v1_only)) { int const blocks_in_piece = piece_size / default_block_size; - aux::vector v2tree(merkle_num_nodes(merkle_num_leafs(blocks_in_piece))); + aux::vector v2tree(merkle_num_nodes(merkle_num_leaves(blocks_in_piece))); for (int i = 0; i < blocks_in_piece; ++i) { sha256_hash const block_hash = hasher256(span(piece).subspan(i * default_block_size, default_block_size)).final(); - v2tree[v2tree.end_index() - merkle_num_leafs(blocks_in_piece) + i] = block_hash; + v2tree[v2tree.end_index() - merkle_num_leaves(blocks_in_piece) + i] = block_hash; } - merkle_fill_tree(v2tree, merkle_num_leafs(blocks_in_piece)); + merkle_fill_tree(v2tree, merkle_num_leaves(blocks_in_piece)); for (piece_index_t i(0); i < t.end_piece(); ++i) t.set_hash2(file_index_t{ 0 }, i - 0_piece, v2tree[0]); diff --git a/test/test_hash_picker.cpp b/test/test_hash_picker.cpp index 8ea3326ce2f..30c1b6eb177 100644 --- a/test/test_hash_picker.cpp +++ b/test/test_hash_picker.cpp @@ -96,9 +96,9 @@ TORRENT_TEST(pick_piece_layer) aux::vector trees; - trees.push_back(aux::merkle_tree(merkle_num_nodes(merkle_num_leafs(4 * 512)))); + trees.push_back(aux::merkle_tree(merkle_num_nodes(merkle_num_leaves(4 * 512)))); aux::from_hex("0000000000000000000000000000000000000000000000000000000000000001", trees.back()[0].data()); - trees.push_back(aux::merkle_tree(merkle_num_nodes(merkle_num_leafs(4 * 512)))); + trees.push_back(aux::merkle_tree(merkle_num_nodes(merkle_num_leaves(4 * 512)))); aux::from_hex("0000000000000000000000000000000000000000000000000000000000000001", trees.back()[0].data()); hash_picker picker(fs, trees); @@ -208,7 +208,7 @@ TORRENT_TEST(add_leaf_hashes) hash_picker picker(fs, trees); std::vector hashes; - auto const pieces_start = full_tree.end_index() - merkle_num_leafs(4 * 512); + auto const pieces_start = full_tree.end_index() - merkle_num_leaves(4 * 512); for (int i = 0; i < 512; ++i) hashes.push_back(full_tree[pieces_start + i]); for (int i = 3; i > 0; i = merkle_get_parent(i)) { @@ -219,7 +219,7 @@ TORRENT_TEST(add_leaf_hashes) TEST_CHECK(result.valid); result = picker.add_hashes(hash_request(0_file, 0, 512, 512, 0) - , span(full_tree).last(merkle_num_leafs(4 * 512) - 512).first(512)); + , span(full_tree).last(merkle_num_leaves(4 * 512) - 512).first(512)); TEST_CHECK(result.valid); hashes.clear(); @@ -234,7 +234,7 @@ TORRENT_TEST(add_leaf_hashes) TEST_CHECK(result.valid); result = picker.add_hashes(hash_request(0_file, 0, 1536, 512, 0) - , span(full_tree).last(merkle_num_leafs(4 * 512) - 1536).first(512)); + , span(full_tree).last(merkle_num_leaves(4 * 512) - 1536).first(512)); TEST_CHECK(result.valid); TEST_CHECK(trees.front().build_vector() == full_tree); @@ -285,7 +285,7 @@ TORRENT_TEST(add_piece_hashes_padded) hash_picker picker(fs, trees); - auto pieces_start = merkle_num_nodes(merkle_num_leafs(1029)) - merkle_num_leafs(1029); + auto pieces_start = merkle_num_nodes(merkle_num_leaves(1029)) - merkle_num_leaves(1029); std::vector hashes; // 5 hashes left after 1024 rounds up to 8, 1024 + 8 = 1032 @@ -315,7 +315,7 @@ TORRENT_TEST(add_piece_hashes_unpadded) hash_picker picker(fs, trees); - auto pieces_start = merkle_num_nodes(merkle_num_leafs(1029)) - merkle_num_leafs(1029); + auto pieces_start = merkle_num_nodes(merkle_num_leaves(1029)) - merkle_num_leaves(1029); std::vector hashes; std::copy(full_tree.begin() + pieces_start + 1024, full_tree.begin() + pieces_start + 1029 @@ -379,8 +379,8 @@ TORRENT_TEST(bad_block_hash) hash_picker picker(fs, trees); std::vector hashes; - auto leafs_start = full_tree.end() - merkle_num_leafs(4 * 512); - std::copy(leafs_start, leafs_start + 512, std::back_inserter(hashes)); + auto leaves_start = full_tree.end() - merkle_num_leaves(4 * 512); + std::copy(leaves_start, leaves_start + 512, std::back_inserter(hashes)); for (int i = 3; i > 0; i = merkle_get_parent(i)) { hashes.push_back(full_tree[merkle_get_sibling(i)]); @@ -401,9 +401,9 @@ TORRENT_TEST(set_block_hash) aux::vector trees; auto const full_tree = build_tree(4 * 512); trees.emplace_back(4 * 512, 4, full_tree[0].data()); - trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leafs(4 * 512)), false)); + trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leaves(4 * 512)), false)); - int const first_leaf = full_tree.end_index() - merkle_num_leafs(4 * 512); + int const first_leaf = full_tree.end_index() - merkle_num_leaves(4 * 512); hash_picker picker(fs, trees); auto result = picker.set_block_hash(1_piece, default_block_size @@ -433,13 +433,13 @@ TORRENT_TEST(set_block_hash_fail) // zero out the inner nodes for a piece along with a single leaf node // then add a bogus hash for the leaf - int const first_leaf = full_tree.end_index() - merkle_num_leafs(4 * 512); + int const first_leaf = full_tree.end_index() - merkle_num_leaves(4 * 512); full_tree[merkle_get_parent(first_leaf + 12)].clear(); full_tree[merkle_get_parent(first_leaf + 14)].clear(); full_tree[first_leaf + 13].clear(); - trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leafs(4 * 512)), false)); + trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leaves(4 * 512)), false)); hash_picker picker(fs, trees); @@ -473,14 +473,14 @@ TORRENT_TEST(set_block_hash_pass) // zero out the inner nodes for a piece along with a single leaf node // then add a bogus hash for the leaf - int const first_leaf = full_tree.end_index() - merkle_num_leafs(4 * 512); + int const first_leaf = full_tree.end_index() - merkle_num_leaves(4 * 512); full_tree[merkle_get_parent(first_leaf + 12)].clear(); full_tree[merkle_get_parent(first_leaf + 14)].clear(); auto const orig_hash = full_tree[first_leaf + 13]; full_tree[first_leaf + 13].clear(); - trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leafs(4 * 512)), false)); + trees.front().load_tree(full_tree, std::vector(std::size_t(merkle_num_leaves(4 * 512)), false)); hash_picker picker(fs, trees); @@ -510,7 +510,7 @@ TORRENT_TEST(pass_piece) hash_picker picker(fs, trees); - int const first_leaf = full_tree.end_index() - merkle_num_leafs(4 * 512); + int const first_leaf = full_tree.end_index() - merkle_num_leaves(4 * 512); for (int i = 0; i < 4; ++i) { @@ -573,7 +573,7 @@ TORRENT_TEST(validate_hash_request) fs.add_file("test/tmp1", 2048 * 16 * 1024); // the merkle tree for this file has 2048 blocks - int const num_leaves = merkle_num_leafs(2048); + int const num_leaves = merkle_num_leaves(2048); int const num_layers = merkle_num_layers(num_leaves); int const max = std::numeric_limits::max(); diff --git a/test/test_merkle.cpp b/test/test_merkle.cpp index a0961da0121..52ce0126c34 100644 --- a/test/test_merkle.cpp +++ b/test/test_merkle.cpp @@ -54,7 +54,7 @@ namespace { } } -TORRENT_TEST(num_leafs) +TORRENT_TEST(num_leaves) { // test merkle_*() functions @@ -63,26 +63,26 @@ TORRENT_TEST(num_leafs) // 1 2 // 3 4 5 6 // 7 8 9 10 11 12 13 14 - // num_leafs = 8 - - TEST_EQUAL(merkle_num_leafs(1), 1); - TEST_EQUAL(merkle_num_leafs(2), 2); - TEST_EQUAL(merkle_num_leafs(3), 4); - TEST_EQUAL(merkle_num_leafs(4), 4); - TEST_EQUAL(merkle_num_leafs(5), 8); - TEST_EQUAL(merkle_num_leafs(6), 8); - TEST_EQUAL(merkle_num_leafs(7), 8); - TEST_EQUAL(merkle_num_leafs(8), 8); - TEST_EQUAL(merkle_num_leafs(9), 16); - TEST_EQUAL(merkle_num_leafs(10), 16); - TEST_EQUAL(merkle_num_leafs(11), 16); - TEST_EQUAL(merkle_num_leafs(12), 16); - TEST_EQUAL(merkle_num_leafs(13), 16); - TEST_EQUAL(merkle_num_leafs(14), 16); - TEST_EQUAL(merkle_num_leafs(15), 16); - TEST_EQUAL(merkle_num_leafs(16), 16); - TEST_EQUAL(merkle_num_leafs(17), 32); - TEST_EQUAL(merkle_num_leafs(18), 32); + // num_leaves = 8 + + TEST_EQUAL(merkle_num_leaves(1), 1); + TEST_EQUAL(merkle_num_leaves(2), 2); + TEST_EQUAL(merkle_num_leaves(3), 4); + TEST_EQUAL(merkle_num_leaves(4), 4); + TEST_EQUAL(merkle_num_leaves(5), 8); + TEST_EQUAL(merkle_num_leaves(6), 8); + TEST_EQUAL(merkle_num_leaves(7), 8); + TEST_EQUAL(merkle_num_leaves(8), 8); + TEST_EQUAL(merkle_num_leaves(9), 16); + TEST_EQUAL(merkle_num_leaves(10), 16); + TEST_EQUAL(merkle_num_leaves(11), 16); + TEST_EQUAL(merkle_num_leaves(12), 16); + TEST_EQUAL(merkle_num_leaves(13), 16); + TEST_EQUAL(merkle_num_leaves(14), 16); + TEST_EQUAL(merkle_num_leaves(15), 16); + TEST_EQUAL(merkle_num_leaves(16), 16); + TEST_EQUAL(merkle_num_leaves(17), 32); + TEST_EQUAL(merkle_num_leaves(18), 32); } TORRENT_TEST(get_parent) @@ -650,9 +650,9 @@ TORRENT_TEST(merkle_root_scratch) namespace { void print_tree(span tree) { - int const num_leafs = static_cast((tree.size() + 1) / 2); - int spacing = num_leafs; - int const num_levels = merkle_num_layers(num_leafs) + 1; + int const num_leaves = static_cast((tree.size() + 1) / 2); + int spacing = num_leaves; + int const num_levels = merkle_num_layers(num_leaves) + 1; int layer_width = 1; int node = 0; for (int i = 0; i < num_levels; ++i) @@ -881,7 +881,7 @@ TORRENT_TEST(merkle_validate_copy_invalid_leaf) TEST_CHECK(empty_tree == expected); } -TORRENT_TEST(merkle_validate_copy_many_invalid_leafs) +TORRENT_TEST(merkle_validate_copy_many_invalid_leaves) { v const src{ ah, @@ -1078,7 +1078,7 @@ TORRENT_TEST(is_subtree_known_padding_two_levels) TORRENT_TEST(is_subtree_known_more_padding_two_levels) { - // the last two leafs are padding, they should be assumed to be correct despite + // the last two leaves are padding, they should be assumed to be correct despite // being zero v const src{ ah, diff --git a/test/test_merkle_tree.cpp b/test/test_merkle_tree.cpp index e6d58135c8b..feccf876ab1 100644 --- a/test/test_merkle_tree.cpp +++ b/test/test_merkle_tree.cpp @@ -47,9 +47,9 @@ namespace { int const num_blocks = 259; auto const f = build_tree(num_blocks); -int const num_leafs = merkle_num_leafs(num_blocks); -int const num_nodes = merkle_num_nodes(num_leafs); -int const num_pad_leafs = num_leafs - num_blocks; +int const num_leaves = merkle_num_leaves(num_blocks); +int const num_nodes = merkle_num_nodes(num_leaves); +int const num_pad_leaves = num_leaves - num_blocks; using verified_t = std::vector; verified_t const empty_verified(std::size_t(num_blocks), false); @@ -118,12 +118,12 @@ TORRENT_TEST(load_tree) { aux::merkle_tree t(num_blocks, 1, f[0].data()); t.load_tree(f, empty_verified); - for (int i = 0; i < num_nodes - num_pad_leafs; ++i) + for (int i = 0; i < num_nodes - num_pad_leaves; ++i) { TEST_CHECK(t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); } - for (int i = num_nodes - num_pad_leafs; i < num_nodes; ++i) + for (int i = num_nodes - num_pad_leaves; i < num_nodes; ++i) { TEST_CHECK(!t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); @@ -157,12 +157,12 @@ TORRENT_TEST(load_sparse_tree) std::vector mask(f.size(), true); aux::merkle_tree t(num_blocks, 1, f[0].data()); t.load_sparse_tree(f, mask, empty_verified); - for (int i = 0; i < num_nodes - num_pad_leafs; ++i) + for (int i = 0; i < num_nodes - num_pad_leaves; ++i) { TEST_CHECK(t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); } - for (int i = num_nodes - num_pad_leafs; i < num_nodes; ++i) + for (int i = num_nodes - num_pad_leaves; i < num_nodes; ++i) { TEST_CHECK(!t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); @@ -185,18 +185,18 @@ TORRENT_TEST(load_sparse_tree) // block layer { aux::merkle_tree t(num_blocks, 1, f[0].data()); - int const first_block = merkle_first_leaf(num_leafs); + int const first_block = merkle_first_leaf(num_leaves); int const end_block = first_block + num_blocks; std::vector mask(f.size(), false); for (int i = first_block; i < end_block; ++i) mask[std::size_t(i)] = true; t.load_sparse_tree(span(f).subspan(first_block, num_blocks), mask, empty_verified); - for (int i = 0; i < num_nodes - num_pad_leafs; ++i) + for (int i = 0; i < num_nodes - num_pad_leaves; ++i) { TEST_CHECK(t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); } - for (int i = num_nodes - num_pad_leafs; i < num_nodes; ++i) + for (int i = num_nodes - num_pad_leaves; i < num_nodes; ++i) { TEST_CHECK(!t.has_node(i)); TEST_CHECK(t.compare_node(i, f[i])); @@ -206,13 +206,13 @@ TORRENT_TEST(load_sparse_tree) // piece layer { int const num_pieces = (num_blocks + 1) / 2; - int const first_piece = merkle_first_leaf(merkle_num_leafs(num_pieces)); + int const first_piece = merkle_first_leaf(merkle_num_leaves(num_pieces)); aux::merkle_tree t(num_blocks, 2, f[0].data()); std::vector mask(f.size(), false); for (int i = first_piece, end = i + num_pieces; i < end; ++i) mask[std::size_t(i)] = true; t.load_sparse_tree(span(f).subspan(first_piece, num_pieces), mask, empty_verified); - int const end_piece_layer = first_piece + merkle_num_leafs(num_pieces); + int const end_piece_layer = first_piece + merkle_num_leaves(num_pieces); for (int i = 0; i < end_piece_layer; ++i) { TEST_CHECK(t.has_node(i)); @@ -359,15 +359,15 @@ TORRENT_TEST(get_piece_layer) { // 8 blocks per piece. aux::merkle_tree t(num_blocks, 8, f[0].data()); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); t.load_tree(span(f).first(int(t.size())), empty_verified); int const num_pieces = (num_blocks + 7) / 8; - int const piece_layer_size = merkle_num_leafs(num_pieces); + int const piece_layer_size = merkle_num_leaves(num_pieces); int const piece_layer_start = merkle_first_leaf(piece_layer_size); auto const piece_layer = t.get_piece_layer(); - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); TEST_EQUAL(num_pieces, int(piece_layer.size())); for (int i = 0; i < int(piece_layer.size()); ++i) @@ -384,11 +384,11 @@ TORRENT_TEST(get_piece_layer_piece_layer_mode) // add the entire piece layer t.load_piece_layer(span(f[127].data(), sha256_hash::size() * num_pieces)); - int const piece_layer_size = merkle_num_leafs(num_pieces); + int const piece_layer_size = merkle_num_leaves(num_pieces); int const piece_layer_start = merkle_first_leaf(piece_layer_size); auto const piece_layer = t.get_piece_layer(); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); TEST_EQUAL(num_pieces, int(piece_layer.size())); for (int i = 0; i < int(piece_layer.size()); ++i) @@ -535,7 +535,7 @@ TORRENT_TEST(add_hashes_full_tree) for (int i = 511; i < 1023; ++i) TEST_EQUAL(t[i], f[i]); - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); } } @@ -587,7 +587,7 @@ TORRENT_TEST(add_hashes_one_piece) int const start_block = piece_index * blocks_per_piece; int const end_block = std::min(blocks_per_piece, num_blocks - start_block); - TEST_CHECK(t.verified_leafs() == set_range(none_set(num_blocks) + TEST_CHECK(t.verified_leaves() == set_range(none_set(num_blocks) , start_block, end_block)); } } @@ -605,7 +605,7 @@ TORRENT_TEST(add_hashes_one_piece_invalid_proof) , corrupt(build_proof(f, insert_idx))); TEST_CHECK(!result); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } } @@ -622,7 +622,7 @@ TORRENT_TEST(add_hashes_one_piece_invalid_hash) , build_proof(f, insert_idx)); TEST_CHECK(!result); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } } @@ -659,7 +659,7 @@ TORRENT_TEST(add_hashes_full_tree_existing_valid_blocks) ++idx; } - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); } } } @@ -708,7 +708,7 @@ TORRENT_TEST(add_hashes_full_tree_existing_invalid_blocks) } } - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); } } } @@ -725,7 +725,7 @@ TORRENT_TEST(set_block_full_block_layer) if (!result) return; } - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); for (int block = 0; block < num_blocks; ++block) { @@ -750,7 +750,7 @@ TORRENT_TEST(set_block_invalid_full_block_layer) if (!result) return; } - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); for (int block = 0; block < num_blocks; ++block) { @@ -783,12 +783,12 @@ TORRENT_TEST(set_block_full_piece_layer) TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::ok); TEST_EQUAL(std::get<1>(result), block - (block % blocks_per_piece)); TEST_EQUAL(std::get<2>(result), blocks_per_piece); - TEST_CHECK(t.verified_leafs() == set_range(none_set(num_blocks), 0, block + 1)); + TEST_CHECK(t.verified_leaves() == set_range(none_set(num_blocks), 0, block + 1)); } else { TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::unknown); - TEST_CHECK(t.verified_leafs() == set_range(none_set(num_blocks), 0, block - (block % blocks_per_piece))); + TEST_CHECK(t.verified_leaves() == set_range(none_set(num_blocks), 0, block - (block % blocks_per_piece))); } } } @@ -818,7 +818,7 @@ TORRENT_TEST(set_block_invalid_full_piece_layer) { TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::unknown); } - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } } @@ -833,7 +833,7 @@ TORRENT_TEST(set_block_empty_tree) // comparing the hash against what we have in the tree auto const result = t.set_block(block, f[511 + block]); TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::unknown); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } int const block = num_blocks - 1; @@ -841,9 +841,9 @@ TORRENT_TEST(set_block_empty_tree) TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::ok); TEST_EQUAL(std::get<1>(result), 0); - TEST_EQUAL(std::get<2>(result), num_leafs); + TEST_EQUAL(std::get<2>(result), num_leaves); - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); } TORRENT_TEST(set_block_invalid_empty_tree) @@ -860,7 +860,7 @@ TORRENT_TEST(set_block_invalid_empty_tree) TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::hash_failed); else TEST_CHECK(std::get<0>(result) == aux::merkle_tree::set_block_result::unknown); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } } @@ -880,7 +880,7 @@ TORRENT_TEST(add_hashes_block_layer_no_padding) for (int i = 0; i < 1023; ++i) TEST_EQUAL(t[i], f[i]); - TEST_CHECK(t.verified_leafs() == all_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == all_set(num_blocks)); } TORRENT_TEST(add_hashes_piece_layer_no_padding) @@ -903,7 +903,7 @@ TORRENT_TEST(add_hashes_piece_layer_no_padding) for (int i = 255; i < 1023; ++i) TEST_CHECK(t[i].is_all_zeros()); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } TORRENT_TEST(add_hashes_partial_proofs) @@ -932,7 +932,7 @@ TORRENT_TEST(add_hashes_partial_proofs) for (int i = 127; i < 127 + 4; ++i) TEST_CHECK(t[i] == f[i]); - TEST_CHECK(t.verified_leafs() == none_set(num_blocks)); + TEST_CHECK(t.verified_leaves() == none_set(num_blocks)); } // TODO: add test for load_piece_layer() diff --git a/test/test_utils.cpp b/test/test_utils.cpp index 5e7e6da5ca0..6b54e4f229d 100644 --- a/test/test_utils.cpp +++ b/test/test_utils.cpp @@ -80,17 +80,17 @@ using namespace lt; aux::vector build_tree(int const size) { - int const num_leafs = merkle_num_leafs(size); - aux::vector full_tree(merkle_num_nodes(num_leafs)); + int const num_leaves = merkle_num_leaves(size); + aux::vector full_tree(merkle_num_nodes(num_leaves)); for (int i = 0; i < size; i++) { std::uint32_t hash[32 / 4]; std::fill(std::begin(hash), std::end(hash), i + 1); - full_tree[full_tree.end_index() - num_leafs + i] = sha256_hash(reinterpret_cast(hash)); + full_tree[full_tree.end_index() - num_leaves + i] = sha256_hash(reinterpret_cast(hash)); } - merkle_fill_tree(full_tree, num_leafs); + merkle_fill_tree(full_tree, num_leaves); return full_tree; }