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

require explicit conversions to and from address::DeviceId #289

Conversation

cosmicexplorer
Copy link
Contributor

@cosmicexplorer cosmicexplorer commented May 4, 2021

Problem

See https://github.com/signalapp/libsignal-client/pull/286/files#r625970592 from #286 for motivation. We currently use u32 to mean different things in different places, including in the .device_id() method of address::ProtocolAddress. We would prefer instead to require an explicit .into() from an intermediate struct that looks like:

#[derive(Copy, Clone)]
pub struct DeviceId(u32);
impl From<u32> for DeviceId { ... }
impl From<DeviceId> for u32 { ... }

So note that this is not part of the documentation push in #285, but rather a separate type of refactoring change.

Solution

(all of the below also have docstrings)

  1. Create address::DeviceId wrapping u32.
  2. Modify the existing definition of storage::prekey::PreKeyId wrapping u32.
  3. Modify the existing definition of storage::signed_prekey::SignedPreKeyId wrapping u32.
  4. Apply .into() calls to convert to and from u32 as necessary to make tests pass.

Result

We don't accidentally mistake a device id for a chain counter!

Copy link
Contributor

@jrose-signal jrose-signal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you've made a very strong case for why strong types are a good idea. ;-)

rust/bridge/shared/src/ffi/storage.rs Outdated Show resolved Hide resolved
rust/bridge/shared/src/ffi/storage.rs Outdated Show resolved Hide resolved
rust/bridge/shared/src/ffi/storage.rs Outdated Show resolved Hide resolved
rust/protocol/src/address.rs Outdated Show resolved Hide resolved
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 11 times, most recently from 7bd9093 to 4896397 Compare May 5, 2021 04:07
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch from 4896397 to 4227315 Compare May 5, 2021 04:45
@cosmicexplorer
Copy link
Contributor Author

@jrose-signal I've split up this change into three logically separate commits to make it easier to review.

@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 2 times, most recently from 8a01414 to 9a5a2ab Compare May 5, 2021 04:54
Copy link
Contributor

@jrose-signal jrose-signal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the split up commits aren't quite split up right anyway, so I just reviewed this all together.

rust/bridge/shared/src/ffi/storage.rs Outdated Show resolved Hide resolved
rust/protocol/src/kdf.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
let pending = session_structure::PendingPreKey {
pre_key_id: pre_key_id.unwrap_or(0),
pre_key_id: pre_key_id.unwrap_or(0.into()).into(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pre_key_id.map(|id| id.into()).unwrap_or(0)? I know it's both equivalent and a little longer, but it does make it clear that "0" is a placeholder rather than a real pre-key with ID #0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes much more sense!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this missed the rebase.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still shows up in at least one other spot!

rust/protocol/tests/session.rs Outdated Show resolved Hide resolved
@jrose-signal
Copy link
Contributor

I double-checked and the message versions for SenderKeyMessage and SenderKeyDistributionMessage should be the same, as should SignalMessage and PreKeySignalMessage, but they are two different message families and so they should not be the same as each other. I'll put in a PR to record the SK(D)M version in the stored state like we do for 1-1 sessions.

@jrose-signal
Copy link
Contributor

(That was #293.)

@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 2 times, most recently from 44184fd to 2648530 Compare May 16, 2021 23:29
@cosmicexplorer
Copy link
Contributor Author

Thanks so much for fixing the SenderKeyState dissonance in #293! In another branch I haven't pushed I had been wondering what to do with that.

Copy link
Contributor

@jrose-signal jrose-signal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking pretty good, but let's leave out some of the more complicated ones: MessageVersion (and the HKDF changes), and SignedPreKeyTimestamp

rust/protocol/src/error.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
rust/protocol/src/state/signed_prekey.rs Outdated Show resolved Hide resolved
rust/protocol/src/protocol.rs Outdated Show resolved Hide resolved
rust/protocol/src/storage/traits.rs Outdated Show resolved Hide resolved
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 3 times, most recently from b5371c5 to df2a1e0 Compare October 9, 2021 23:02
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 4 times, most recently from 1db6d8a to d6da511 Compare October 9, 2021 23:34
@cosmicexplorer
Copy link
Contributor Author

Was having lots of difficulty making the bridge tests pass and would probably like your assistance on those changes independently, so have reverted the RegistrationId changes and stuffed them into https://github.com/cosmicexplorer/libsignal-client/compare/type-safety-over-ambiguously-named-fields...TYPE-SAFETY-V2?expand=1 for a followup PR.

@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch from d6da511 to 1993ce6 Compare October 10, 2021 00:41
Copy link
Contributor

@jrose-signal jrose-signal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one's pretty straightforward, so I picked it up first. Only a few comments.

rust/protocol/src/address.rs Outdated Show resolved Hide resolved
rust/protocol/src/state/prekey.rs Outdated Show resolved Hide resolved
let pending = session_structure::PendingPreKey {
pre_key_id: pre_key_id.unwrap_or(0),
pre_key_id: pre_key_id.unwrap_or(0.into()).into(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this missed the rebase.

rust/protocol/src/state/signed_prekey.rs Outdated Show resolved Hide resolved
rust/protocol/tests/groups.rs Outdated Show resolved Hide resolved
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch 9 times, most recently from 2294852 to 9aa600f Compare June 27, 2022 03:51
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch from 9aa600f to 27a64b7 Compare July 1, 2022 11:21
@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch from dcbd8c3 to 7d55f25 Compare July 18, 2022 22:49
@cosmicexplorer
Copy link
Contributor Author

Ok, one of the CI runs is failing reliably, and I can't reproduce it locally. I'm running cargo +stable test --workspace --verbose -- --include-ignored, but that command succeeds for me. It doesn't look like this test is failing on main, so I'm wondering what I'm doing wrong. The beginning of the errors in https://github.com/signalapp/libsignal/runs/7399386113?check_suite_focus=true look like:

 ---- src/support/mod.rs - support::bridge_get (line 162) stdout ----
error: cannot find macro `bridge_get` in this scope
  --> src/support/mod.rs:172:1
   |
12 | bridge_get!(Foo::bar as GetBar -> &str, ffi = "foo_get_bar", jni = "Foo_GetBar", node = "Foo_GetBar");
   | ^^^^^^^^^^

@cosmicexplorer
Copy link
Contributor Author

Hm, it looks like that exact failure is happening for all the other PRs I have open right now as well. It seems like the CI might be wonky.

@cosmicexplorer cosmicexplorer force-pushed the type-safety-over-ambiguously-named-fields branch from 7d55f25 to 8b1112c Compare July 19, 2022 00:17
@jrose-signal
Copy link
Contributor

Ah, right, because doctests now get run on macro docs! We have a fix for this privately (because we updated our pinned nightly). Don't worry about it here; since I'll be cherry-picking things to the private branch, I'll make sure it works there before merging and closing the public branch.

@jrose-signal
Copy link
Contributor

Cherry-picked as ec3c2d3, will be in the next release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants