PSwap: ClientBuilder observer registration: opt-out + dedup, stop hardcoding the PSWAP observer
- Dominant language
- Rust
- Stars
- 78
- Forks
- 129
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 52
Description
**Context**
Follow-up from PR #2231 review ([thread](https://github.com/0xMiden/miden-client/pull/2231#discussion_r3422130013)). Quoting @igamigo:
> As we discussed, if the decision was to make the implementation generic, we should add `ClientBuilder` methods for extending the list of observers (e.g., `ClientBuilder::with_tx_observer()`). For this, we should not make the observers a list, but rather the observers should be deduplicated so that a user cannot accidentally use the same observer twice. I'm not entirely sure we should enforce PSWAP observers to always be present, or at least we should have a way to opt out of it.
**Problem**
The `PswapTransactionObserver` is hardcoded into every client at `crates/rust-client/src/builder.rs:527`:
```rust
let transaction_observers: Vec> =
vec![Arc::new(PswapTransactionObserver::new(store.clone()))];
```
There is no `ClientBuilder` method to register observers, no deduplication, and no way to opt out of the PSWAP observers — even for a client that only cares about other note types.
**Proposed work**
- Add `ClientBuilder` registration methods (e.g. `with_transaction_observer()` / `with_note_observer()`).
- Deduplicate observers so the same observer can't be attached twice.
- Make the built-in PSWAP observers opt-in/opt-out rather than unconditionally present.
- Applies to both observer kinds: `TransactionObserver` (`PswapTransactionObserver`) and `NoteObserver` (`PswapChainObserver`).
**Acceptance criteria**
- [ ] A client can be built with the PSWAP observers omitted.
- [ ] Registering the same observer twice does not double-fire it.
- [ ] Coordinated with #: if `NoteObserver` collapses into `OnNoteReceived`, the registration story mirrors it.
Contributor guide
Research direction
The issue points to the builder code in crates/rust-client/src/builder.rs. Start by examining the ClientBuilder struct and its current observer setup. Look for existing observer registration patterns and the PswapTransactionObserver. The goal is to add methods like with_transaction_observer, implement deduplication (likely using a set or checking for existing observers), and make the PSWAP observers optional. Check if there are tests for the builder to understand the expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend-api-design
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100