diff --git a/migrations/mysql/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql b/migrations/mysql/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql new file mode 100644 index 00000000..f50075bc --- /dev/null +++ b/migrations/mysql/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents CHANGE COLUMN root_hash is_bep_30 BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/migrations/sqlite3/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql b/migrations/sqlite3/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql new file mode 100644 index 00000000..0a33c94b --- /dev/null +++ b/migrations/sqlite3/20240305110750_torrust_bep_rename_root_hash_to_is_bep_30.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents RENAME COLUMN root_hash TO is_bep_30; diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index 07ae8839..660ead9e 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -438,8 +438,9 @@ impl Database for Mysql { // start db transaction let mut tx = conn.begin().await.map_err(|_| database::Error::Error)?; - // torrent file can only hold a pieces key or a root hash key: http://www.bittorrent.org/beps/bep_0030.html - let (pieces, root_hash): (String, bool) = if let Some(pieces) = &torrent.info.pieces { + // torrent file can only hold a `pieces` key or a `root hash` key + // BEP 30: http://www.bittorrent.org/beps/bep_0030.html + let (pieces, is_bep_30): (String, bool) = if let Some(pieces) = &torrent.info.pieces { (from_bytes(pieces.as_ref()), false) } else { let root_hash = torrent.info.root_hash.as_ref().ok_or(database::Error::Error)?; @@ -457,7 +458,7 @@ impl Database for Mysql { pieces, piece_length, private, - root_hash, + is_bep_30, `source`, comment, date_uploaded, @@ -474,7 +475,7 @@ impl Database for Mysql { .bind(pieces) .bind(torrent.info.piece_length) .bind(torrent.info.private) - .bind(root_hash) + .bind(is_bep_30) .bind(torrent.info.source.clone()) .bind(torrent.comment.clone()) .bind(torrent.creation_date) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index e6fb01f5..a31f16d4 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -429,7 +429,7 @@ impl Database for Sqlite { let mut tx = conn.begin().await.map_err(|_| database::Error::Error)?; // torrent file can only hold a pieces key or a root hash key: http://www.bittorrent.org/beps/bep_0030.html - let (pieces, root_hash): (String, bool) = if let Some(pieces) = &torrent.info.pieces { + let (pieces, is_bep_30): (String, bool) = if let Some(pieces) = &torrent.info.pieces { (from_bytes(pieces.as_ref()), false) } else { let root_hash = torrent.info.root_hash.as_ref().ok_or(database::Error::Error)?; @@ -447,7 +447,7 @@ impl Database for Sqlite { pieces, piece_length, private, - root_hash, + is_bep_30, `source`, comment, date_uploaded, @@ -464,7 +464,7 @@ impl Database for Sqlite { .bind(pieces) .bind(torrent.info.piece_length) .bind(torrent.info.private) - .bind(root_hash) + .bind(is_bep_30) .bind(torrent.info.source.clone()) .bind(torrent.comment.clone()) .bind(torrent.creation_date) diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 64fa5e89..01c97af0 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -81,7 +81,7 @@ impl Torrent { &db_torrent.name, db_torrent.piece_length, db_torrent.private, - db_torrent.root_hash, + db_torrent.is_bep_30, &db_torrent.pieces, torrent_files, ); @@ -235,7 +235,7 @@ impl TorrentInfoDictionary { /// - The `pieces` field is not a valid hex string. /// - For single files torrents the `TorrentFile` path is empty. #[must_use] - pub fn with(name: &str, piece_length: i64, private: Option, root_hash: i64, pieces: &str, files: &[TorrentFile]) -> Self { + pub fn with(name: &str, piece_length: i64, private: Option, is_bep_30: i64, pieces: &str, files: &[TorrentFile]) -> Self { let mut info_dict = Self { name: name.to_string(), pieces: None, @@ -250,8 +250,8 @@ impl TorrentInfoDictionary { }; // a torrent file has a root hash or a pieces key, but not both. - if root_hash > 0 { - // If `root_hash` is true the `pieces` field contains the `root hash` + if is_bep_30 > 0 { + // If `is_bep_30` is true the `pieces` field contains the `root hash` info_dict.root_hash = Some(pieces.to_owned()); } else { let buffer = into_bytes(pieces).expect("variable `torrent_info.pieces` is not a valid hex string"); @@ -334,7 +334,7 @@ pub struct DbTorrent { pub piece_length: i64, #[serde(default)] pub private: Option, - pub root_hash: i64, + pub is_bep_30: i64, pub comment: Option, pub creation_date: Option, pub created_by: Option, diff --git a/src/services/torrent_file.rs b/src/services/torrent_file.rs index 3824ea9e..6326e4a4 100644 --- a/src/services/torrent_file.rs +++ b/src/services/torrent_file.rs @@ -14,7 +14,8 @@ pub struct CreateTorrentRequest { pub pieces: String, pub piece_length: i64, pub private: Option, - pub root_hash: i64, // True (1) if it's a BEP 30 torrent. + /// True (1) if it's a BEP 30 torrent. + pub is_bep_30: i64, pub files: Vec, // Other fields of the root level metainfo dictionary pub announce_urls: Vec>, @@ -58,7 +59,7 @@ impl CreateTorrentRequest { &self.name, self.piece_length, self.private, - self.root_hash, + self.is_bep_30, &self.pieces, &self.files, ) @@ -92,7 +93,7 @@ pub fn generate_random_torrent(id: Uuid) -> Torrent { pieces: sha1(&file_contents), piece_length: 16384, private: None, - root_hash: 0, + is_bep_30: 0, files: torrent_files, announce_urls: torrent_announce_urls, comment: None, diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs index cbaed29f..1508c6f1 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs @@ -26,7 +26,7 @@ pub struct TorrentRecordV2 { pub pieces: String, pub piece_length: i64, pub private: Option, - pub root_hash: i64, + pub is_bep_30: i64, pub date_uploaded: String, } @@ -43,7 +43,7 @@ impl TorrentRecordV2 { pieces: torrent_info.get_pieces_as_string(), piece_length: torrent_info.piece_length, private: torrent_info.private, - root_hash: torrent_info.get_root_hash_as_i64(), + is_bep_30: torrent_info.get_root_hash_as_i64(), date_uploaded: convert_timestamp_to_datetime(torrent.upload_date), } } @@ -196,7 +196,7 @@ impl SqliteDatabaseV2_0_0 { pieces, piece_length, private, - root_hash, + is_bep_30, date_uploaded ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ) @@ -209,7 +209,7 @@ impl SqliteDatabaseV2_0_0 { .bind(torrent.pieces.clone()) .bind(torrent.piece_length) .bind(torrent.private.unwrap_or(0)) - .bind(torrent.root_hash) + .bind(torrent.is_bep_30) .bind(torrent.date_uploaded.clone()) .execute(&self.pool) .await diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/transferrer_testers/torrent_transferrer_tester.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/transferrer_testers/torrent_transferrer_tester.rs index 6677b04b..768e8a50 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/transferrer_testers/torrent_transferrer_tester.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/transferrer_testers/torrent_transferrer_tester.rs @@ -119,7 +119,7 @@ impl TorrentTester { } else { assert_eq!(imported_torrent.private, torrent_file.info.private); } - assert_eq!(imported_torrent.root_hash, torrent_file.info.get_root_hash_as_i64()); + assert_eq!(imported_torrent.is_bep_30, torrent_file.info.get_root_hash_as_i64()); assert_eq!( imported_torrent.date_uploaded, convert_timestamp_to_datetime(torrent.upload_date)