Skip to content

Conversation

da2ce7
Copy link
Contributor

@da2ce7 da2ce7 commented Nov 24, 2022

No description provided.

@da2ce7 da2ce7 requested a review from josecelano November 24, 2022 20:33
@josecelano
Copy link
Member

hi @da2ce7, the reason I did it that way is that:

  • Generally, I am uncomfortable changing the contracts without tests. So I tried not to change other parts. Your solution changes the contract for get_info_hash_from_whitelist in the SQLite version. It indeed seems nobody else is using that function (only the tracker), and it seems safe to change. In fact, with your change, you make the contract the same for both versions (MySQL and SQLite). Maybe my fear comes from working with dynamic languages :-).
  • On the other hand, the method is a query that does not change the state. If it fails for another reason, we can try to insert the torrent in the whitelist. The SQL sentence for the "query" could be wrong, but the "insert" query could be OK. That was my thought (very odd thought since I usually prefer failing asap). Because I did not want to change the uncovered code, I was "forced" to introduce that "unexpected" behavior with code that does not reveal the intention. I apologize.

I think I should first evaluate the impact of changing uncovered code in the future.

I'm going to merge the PR. But I think what we really need to do is to add a new method like this:

async fn is_info_hash_whitelisted(info_hash: &str) -> Result<bool, database::Error>

so we can have this simpler version:

async fn add_torrent_to_database_whitelist(&self, info_hash: &InfoHash) -> Result<(), database::Error> {
    if self.database.is_info_hash_whitelisted(&info_hash.to_owned().to_string()) {
        // We do not need to whitelist it again
        Ok(())
    }
    self.database.add_info_hash_to_whitelist(*info_hash).await?;
    Ok(())
}

We can probably use the current get_info_hash_from_whitelistfuntion to implement the new one.

@josecelano
Copy link
Member

ACK 167b749

@josecelano josecelano merged commit a3a1cca into torrust:issue-74-fix-api-response-for-duplicate-whitelist-requests Nov 24, 2022
@josecelano
Copy link
Member

One more thing @da2ce7 do not you think we should simply "panic" when the SQL is wrong? I don't think we should try to handle those errors. I have to check that. It's strange that the DB package returns an error when the query does not have any result. I suppose the database trait should always return Option (for a single record result) or collection results.

@da2ce7
Copy link
Contributor Author

da2ce7 commented Nov 25, 2022

@josecelano The db error InvalidQuery is used incorrectly/inappropriately throughout the entire mysql.rs and sqlite.rs, the error-handling in both of these files really needs a complete overhaul.

You are very correct to suggest that the DB should be rerunning an Optional Value to indicate that there was no record. – The code in this pull request is simply the minimal changes, without changing any of the interface... I just edited the db code because to correct their "unexpected" behavior, i.e. they didn't follow a reasonable interpenetration of their own contracts.

da2ce7 added a commit that referenced this pull request Nov 25, 2022
5274b2c fix: [#74] send api ready event after starting the api server (Cameron Garnham)
15aa831 refactor: api job starter waits until api is ready (Jose Celano)
32a6d79 fix: [#74] send message from API when is ready (Jose Celano)
ed5c1ed refactor: extract fn is_info_hash_whitelisted (Jose Celano)
8af9834 refactor: rename tests (Jose Celano)
167b749 db: check info_hash record not found instead dropping all errors (Cameron Garnham)
2d621c5 fix: [#74] bug calling the whitelist API endpoint twice (Jose Celano)
9cfab4d test: [#74] reproduce API bug with an e2e test (Jose Celano)

Pull request description:

  - [x] Reproduce the error with an e2e test.
  - [x] Fix it.
  - [x] Rename test to `should_allow_to_whitelist_a_torrent`
  - [x] Refactor: extract function `is_info_hash_whitelisted` as described [here](#118 (comment)).

ACKs for top commit:
  josecelano:
    ACK 5274b2c
  da2ce7:
    ACK 5274b2c

Tree-SHA512: 82b5ed50816e09055bad14873a297ca847b7c7eb4b2707db4cce57a60bb54d2cb2122a1d6991f0d95079dc8353808556f4bdf30ccb225948c4dba871b452212a
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.

2 participants