Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User forked version of serde-bencode #316

Merged

Conversation

josecelano
Copy link
Member

@josecelano josecelano commented Sep 26, 2023

Using our crate torrust-serde-bencode instead of the upstream repo fixes the problem with some torrent deserializations described in this issue.

There was a problem with lists (field nodes in the torrent) containing other lists (tuples) with different value types.

{
   "info": {
      "length": 8,
      "name": "minimal.txt",
      "piece length": 16384,
      "pieces": "<hex>30 9A 84 51 4A F1 09 D0 8C 34 42 11 26 67 7F 84 B3 23 4D 78</hex>"
   },
   "nodes": [
      [
         "188.163.121.224",
         56711
      ],
      [
         "162.250.131.26",
         13386
      ]
   ]
}

I have been trying to reproduce the bug in our fork here, but it works. I've added a test using the same torrent file that does not work here.

If you run:

cargo run --bin parse_torrent ./tests/fixtures/torrents/not-working-with-two-nodes.torrent 

With the current develop branch, you'll see that it does NOT work. But It works using our fork.

For this PR, I have changed the cargo.toml file to include the latest commit from the fork. And it works.

Surprisingly, it also works with:

torrust-serde-bencode = "^0.2.3"

Which was supposed to be the same code as:

serde_bencode = "^0.2.3"

So, no idea why it's working with an almost-exact copy of the original package.

Anyway, serde_bencode is not maintained. So I'll will:

  • Publish a new version for torrust-serde-bencode.
  • Change this PR to use the new version torrust-serde-bencode = "^0.2.4".

@josecelano josecelano temporarily deployed to coverage September 26, 2023 16:52 — with GitHub Actions Inactive
@josecelano josecelano temporarily deployed to coverage September 26, 2023 16:52 — with GitHub Actions Inactive
@josecelano josecelano requested a review from da2ce7 September 26, 2023 16:53
@codecov-commenter
Copy link

codecov-commenter commented Sep 26, 2023

Codecov Report

Merging #316 (3550c44) into develop (db99842) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           develop     #316      +/-   ##
===========================================
+ Coverage    42.45%   42.47%   +0.01%     
===========================================
  Files           79       79              
  Lines         4803     4801       -2     
===========================================
  Hits          2039     2039              
+ Misses        2764     2762       -2     
Files Coverage Δ
src/models/torrent_file.rs 70.58% <100.00%> (+0.48%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@josecelano
Copy link
Member Author

Finally, I've found the problem. The deserialisation works with the original serde_bencode package too. The only change needed was replacing the Option<Vec<TorrentNode>> with Option<Vec<(String, i64)>>. It could be a bug with the deserialization described here.

But the struct can be changed to avoid using tuple structs.

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Torrent {
    pub info: TorrentInfoDictionary, //
    #[serde(default)]
    pub announce: Option<String>,
    #[serde(default)]
    pub nodes: Option<Vec<TorrentNode>>, // <- change this to Option<Vec<(String, i64)>>,
    #[serde(default)]
    pub encoding: Option<String>,
    #[serde(default)]
    pub httpseeds: Option<Vec<String>>,
    #[serde(default)]
    #[serde(rename = "announce-list")]
    pub announce_list: Option<Vec<Vec<String>>>,
    #[serde(default)]
    #[serde(rename = "creation date")]
    pub creation_date: Option<i64>,
    #[serde(default)]
    pub comment: Option<String>,
    #[serde(default)]
    #[serde(rename = "created by")]
    pub created_by: Option<String>,
}

After changing that, you can rerun this command:

x$ cargo run --bin parse_torrent ./tests/fixtures/torrents/not-working-with-two-nodes.torrent 
   Compiling torrust-index-backend v2.0.0-alpha.3 (/home/josecelano/Documents/git/committer/me/github/torrust/torrust-index)
    Finished dev [unoptimized + debuginfo] target(s) in 6.01s
     Running `target/debug/parse_torrent ./tests/fixtures/torrents/not-working-with-two-nodes.torrent`
Reading the torrent file ...
Decoding torrent with standard serde implementation ...
Parsed torrent: 
Torrent {
    info: TorrentInfoDictionary {
        name: "minimal.txt",
        pieces: Some(
            [
                48,
                154,
                132,
                81,
                74,
                241,
                9,
                208,
                140,
                52,
                66,
                17,
                38,
                103,
                127,
                132,
                179,
                35,
                77,
                120,
            ],
        ),
        piece_length: 16384,
        md5sum: None,
        length: Some(
            8,
        ),
        files: None,
        private: None,
        path: None,
        root_hash: None,
        source: None,
    },
    announce: None,
    nodes: Some(
        [
            (
                "188.163.121.224",
                56711,
            ),
            (
                "162.250.131.26",
                13386,
            ),
        ],
    ),
    encoding: None,
    httpseeds: None,
    announce_list: None,
    creation_date: None,
    comment: None,
    created_by: None,
}

and you'll see the two nodes.

I'm going to keep the fork because:

  • I've made some improvements and plan to do more.
  • The package is not maintained anymore.

@josecelano
Copy link
Member Author

josecelano commented Sep 27, 2023

It works with the original serde_bencode crate but not with the latest publish crate (v0.2.3).

@madadam was fixing that problem, but a new version was not published after the fixing.

See the latest commits https://github.com/toby/serde-bencode/commits/master

That's why it's working with the fork (latest commit) because it includes those fixes. I will continue working on the fork and publish a new version. After that we can use that version on this PR.

@josecelano
Copy link
Member Author

I've published a new version (0.2.4) of the serde_bencode. This PR should work with the newer version.

@da2ce7 da2ce7 added the Needs Rebase Base Branch has Incompatibilities label Oct 13, 2023
There was a problem with list (nodes) containing other list (tuples)
with different value types.

```json
{
   "info": {
      "length": 8,
      "name": "minimal.txt",
      "piece length": 16384,
      "pieces": "<hex>30 9A 84 51 4A F1 09 D0 8C 34 42 11 26 67 7F 84 B3 23 4D 78</hex>"
   },
   "nodes": [
      [
         "188.163.121.224",
         56711
      ],
      [
         "162.250.131.26",
         13386
      ]
   ]
}
```
@josecelano josecelano force-pushed the issue-226-use-serde-bencode-fork branch from c750cbb to 3550c44 Compare October 27, 2023 14:14
@josecelano josecelano temporarily deployed to coverage October 27, 2023 14:14 — with GitHub Actions Inactive
@josecelano josecelano marked this pull request as ready for review October 27, 2023 14:14
@josecelano
Copy link
Member Author

It's fixed with serde_bencode 0.2.4

$ cargo run --bin parse_torrent ./tests/fixtures/torrents/not-working-with-two-nodes.torrent 
   Compiling torrust-index v3.0.0-alpha.3-develop (/home/josecelano/Documents/git/committer/me/github/torrust/torrust-index)
    Finished dev [unoptimized + debuginfo] target(s) in 2.61s
     Running `target/debug/parse_torrent ./tests/fixtures/torrents/not-working-with-two-nodes.torrent`
Reading the torrent file ...
Decoding torrent with standard serde implementation ...
Parsed torrent: 
Torrent {
    info: TorrentInfoDictionary {
        name: "minimal.txt",
        pieces: Some(
            [
                48,
                154,
                132,
                81,
                74,
                241,
                9,
                208,
                140,
                52,
                66,
                17,
                38,
                103,
                127,
                132,
                179,
                35,
                77,
                120,
            ],
        ),
        piece_length: 16384,
        md5sum: None,
        length: Some(
            8,
        ),
        files: None,
        private: None,
        path: None,
        root_hash: None,
        source: None,
    },
    announce: None,
    nodes: Some(
        [
            (
                "188.163.121.224",
                56711,
            ),
            (
                "162.250.131.26",
                13386,
            ),
        ],
    ),
    encoding: None,
    httpseeds: None,
    announce_list: None,
    creation_date: None,
    comment: None,
    created_by: None,
}

@josecelano
Copy link
Member Author

ACK 3550c44

@josecelano josecelano removed the Needs Rebase Base Branch has Incompatibilities label Oct 27, 2023
@josecelano josecelano merged commit b331eaf into torrust:develop Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants