Skip to content

Commit

Permalink
refactor: [#301] rename table field root_hash to is_bep_30
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 5, 2024
1 parent 2e8bd5a commit 4d7091d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents CHANGE COLUMN root_hash is_bep_30 BOOLEAN NOT NULL DEFAULT FALSE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents RENAME COLUMN root_hash TO is_bep_30;
9 changes: 5 additions & 4 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -457,7 +458,7 @@ impl Database for Mysql {
pieces,
piece_length,
private,
root_hash,
is_bep_30,
`source`,
comment,
date_uploaded,
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -447,7 +447,7 @@ impl Database for Sqlite {
pieces,
piece_length,
private,
root_hash,
is_bep_30,
`source`,
comment,
date_uploaded,
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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<u8>, root_hash: i64, pieces: &str, files: &[TorrentFile]) -> Self {
pub fn with(name: &str, piece_length: i64, private: Option<u8>, is_bep_30: i64, pieces: &str, files: &[TorrentFile]) -> Self {
let mut info_dict = Self {
name: name.to_string(),
pieces: None,
Expand All @@ -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");
Expand Down Expand Up @@ -334,7 +334,7 @@ pub struct DbTorrent {
pub piece_length: i64,
#[serde(default)]
pub private: Option<u8>,
pub root_hash: i64,
pub is_bep_30: i64,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
Expand Down
7 changes: 4 additions & 3 deletions src/services/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct CreateTorrentRequest {
pub pieces: String,
pub piece_length: i64,
pub private: Option<u8>,
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<TorrentFile>,
// Other fields of the root level metainfo dictionary
pub announce_urls: Vec<Vec<String>>,
Expand Down Expand Up @@ -58,7 +59,7 @@ impl CreateTorrentRequest {
&self.name,
self.piece_length,
self.private,
self.root_hash,
self.is_bep_30,
&self.pieces,
&self.files,
)
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct TorrentRecordV2 {
pub pieces: String,
pub piece_length: i64,
pub private: Option<u8>,
pub root_hash: i64,
pub is_bep_30: i64,
pub date_uploaded: String,
}

Expand All @@ -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),
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ impl SqliteDatabaseV2_0_0 {
pieces,
piece_length,
private,
root_hash,
is_bep_30,
date_uploaded
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4d7091d

Please sign in to comment.