Skip to content

platform-wallet: serialize contact-details saves per identity so two saves cannot pick the same document slot #5599

Open

Description

@Claudius-Maginificent

TL;DR: Saving the nickname or note of two contacts at almost the same time can make both saves pick the same slot on Platform. The second save is then refused, its change stays on this device only, and the next sync undoes it.

User story

As a DashPay user, I want every contact nickname, note or hide/unhide I save to reach my other devices, even when I edit several contacts quickly.

As a wallet app developer, I want the wallet to order contact-details saves for an identity, so I don't need my own lock around its call.

Scenario

Base flow

  1. The identity has at least two established contacts, so contact details are published.
  2. The user saves a note for contact A and, before that save finishes, a note for contact B. Neither contact has published details yet.

Actual behavior

Both saves read the identity's published contact-details documents at the same time, see the same highest slot number, and choose the same next slot. Platform enforces one document per (owner, key, slot), so the second create is refused.

That save had already written its change locally. The local note now differs from Platform. The next background sync applies Platform's state ("remote wins") and removes the note the user saved.

A similar overlap between a save and a running sync pass can also re-apply the older published value for the contact being edited, in the window between the local write and the publish.

Expected behavior

Saves for one identity run one after another, from reading the published documents to the broadcast result. A sync pass does not apply published details over a save that is still in flight for that identity. Each save then gets its own slot, and nothing the user saved is undone.

Detailed discussion

On v5.1-dev @ 575d961fe0, set_contact_info_with_external_signer (network/contact_info.rs#L504):

  1. Writes the metadata locally under a short wallet_manager.write() guard that is dropped before any network call (~#L544-L580).
  2. Fetches the owner's contactInfo documents and computes high_water per root key (#L626-L628).
  3. For a contact with no document yet, allocates next_index = high_water[root] + 1 (~#L678-L690), then seals and broadcasts.

No lock spans steps 2–3 for an owner. The contract's unique index is (ownerId, rootEncryptionKeyIndex, derivationEncryptionKeyIndex) (comment at ~#L665-L672). Two concurrent creates therefore collide, and Platform refuses the second.

sync_contact_infos documents "Remote state wins … convergence is last-writer through Platform" (#L284-L290). For the refused save, that means its local change is reverted on the next pass.

Platform backstops the corruption (the unique index), so no document is lost. But the user's edit is, and the refused transition may still be charged.

Downstream: Dash Evo Tool holds its own per-owner async mutex around the call (src/context/contact_info_writes.rs, ContactInfoWrites::acquire). That lock only covers the app's own calls, not the wallet's background sync, which is why the fix belongs in the wallet.

Proposed change

  • A per-owner async lock inside IdentityWallet / DashPayView (a map of Weak<tokio::sync::Mutex<()>> keyed by owner id), held from the document fetch to the broadcast result in set_contact_info_with_external_signer.
  • sync_contact_infos / drain_contact_info_decrypt take the same lock per owner while applying, so a pass cannot apply over an in-flight save.
  • Worth the same look: whether sync_profiles can overwrite a just-confirmed update_profile_with_external_signer result with a pre-write fetch, when the pass's fetch started before the write and is applied after it. Not verified. A shared per-owner lock would close that too.

Prior work

Attribution

🤖 Co-authored by Claudius the Magnificent AI Agent

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions