Skip to content

feat: add wallet_manager_set_transaction_label FFI function#618

Open
xdustinface wants to merge 4 commits intov0.42-devfrom
feat/transaction-label-ffi
Open

feat: add wallet_manager_set_transaction_label FFI function#618
xdustinface wants to merge 4 commits intov0.42-devfrom
feat/transaction-label-ffi

Conversation

@xdustinface
Copy link
Copy Markdown
Collaborator

@xdustinface xdustinface commented Apr 1, 2026

Add wallet_manager_set_transaction_label.

Based on:

Summary by CodeRabbit

  • New Features

    • Transaction labeling for managed accounts: assign, update, or clear custom labels on individual transactions.
  • Documentation

    • API docs updated to include the new transaction-labeling entry and revised totals.
  • Tests

    • Added tests covering label lifecycle (set/overwrite/clear) and input/error cases.
  • Chores

    • Internal visibility adjustments to support the new functionality.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

Added a new FFI function wallet_manager_set_transaction_label to set or clear a transaction label via the shared wallet manager (pointer checks, 32‑byte id/txid handling, C-string UTF‑8 decode, async write-lock mutation, FFIError handling, and tests). Also changed two helper functions to crate-visible.

Changes

Cohort / File(s) Summary
Documentation
key-wallet-ffi/FFI_API.md
Added wallet_manager_set_transaction_label to API docs and updated total exported functions (259 → 260); includes C signature, parameters, and safety notes.
Managed Account FFI
key-wallet-ffi/src/managed_account.rs
Added wallet_manager_set_transaction_label FFI entrypoint implementing null checks, 32‑byte wallet_id/txid handling, CStr → UTF‑8 conversion, async write-lock update of account transaction record (set/clear label), FFIError mapping, and unit tests (helpers, lifecycle, error cases).
Address Pool Visibility
key-wallet-ffi/src/address_pool.rs
Changed helper visibility from private to crate-visible: get_managed_account_by_type and get_managed_account_by_type_mut (fnpub(crate) fn).

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client (FFI caller)
    participant FFI as FFI layer
    participant Manager as WalletManager
    participant Account as ManagedAccount
    participant Storage as Account Transactions

    rect rgba(200,200,255,0.5)
    Client->>FFI: wallet_manager_set_transaction_label(manager, wallet_id, account_type, account_index, txid, label)
    end

    rect rgba(200,255,200,0.5)
    FFI->>Manager: acquire write lock (async)
    Manager->>Manager: locate wallet by wallet_id
    Manager->>Account: select account by type/index
    Account->>Storage: find transaction by txid
    Storage-->>Account: transaction record (or not found)
    Account-->>Manager: update record.label (set or clear)
    Manager->>FFI: release lock, return success/failure
    end

    rect rgba(255,200,200,0.5)
    FFI-->>Client: boolean + FFIError
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I nudge the ledger, soft and bright,

I tuck a label in the night,
Set, change, or clear — a hop, a tweak,
Transactions now can whisper: speak!
Small paws, small code, a wallet's light.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately describes the main change: adding a new wallet_manager_set_transaction_label FFI function. It is concise, specific, and clearly summarizes the primary contribution of this PR.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/transaction-label-ffi

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 84.22939% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.13%. Comparing base (747d2fa) to head (7827000).
⚠️ Report is 22 commits behind head on v0.42-dev.

Files with missing lines Patch % Lines
key-wallet-ffi/src/managed_account.rs 84.47% 43 Missing ⚠️
key-wallet-ffi/src/address_pool.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##           v0.42-dev     #618      +/-   ##
=============================================
+ Coverage      67.65%   68.13%   +0.48%     
=============================================
  Files            319      319              
  Lines          67894    67938      +44     
=============================================
+ Hits           45932    46289     +357     
+ Misses         21962    21649     -313     
Flag Coverage Δ
core 75.52% <ø> (+0.26%) ⬆️
ffi 39.07% <84.22%> (+1.98%) ⬆️
rpc 20.00% <ø> (+<0.01%) ⬆️
spv 85.86% <ø> (+0.48%) ⬆️
wallet 68.05% <ø> (+0.43%) ⬆️
Files with missing lines Coverage Δ
key-wallet-ffi/src/address_pool.rs 35.58% <50.00%> (+6.18%) ⬆️
key-wallet-ffi/src/managed_account.rs 59.05% <84.47%> (+9.66%) ⬆️

... and 115 files with indirect coverage changes

Base automatically changed from feat/transaction-label-size to v0.42-dev April 2, 2026 22:41
@github-actions github-actions Bot added the merge-conflict The PR conflicts with the target branch. label Apr 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 6, 2026

This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them.

Add mutable access to `FFIManagedCoreAccount` via `inner_mut()` and
expose a function to set or clear transaction labels by txid.

Also add comprehensive tests for:
- `managed_core_account_get_transactions` with real transaction data
- `managed_core_account_free_transactions` verifying no crash on free
- Label round-trip (set, read back, clear, verify cleared)
- Error cases (null account, missing txid)
@xdustinface xdustinface force-pushed the feat/transaction-label-ffi branch from 4b5f8c7 to f272538 Compare April 17, 2026 00:01
@github-actions github-actions Bot removed the merge-conflict The PR conflicts with the target branch. label Apr 17, 2026
@xdustinface xdustinface marked this pull request as ready for review April 17, 2026 00:02
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@key-wallet-ffi/src/managed_account.rs`:
- Around line 896-969: The setter managed_core_account_set_transaction_label is
mutating only the cloned per-handle snapshot (FFIManagedCoreAccount built in
managed_wallet_get_account via Arc::new(account.clone())), so changes are not
persisted; update the function to locate and mutate the shared managed-wallet
storage instead of the local cloned record: look up the account in the wallet
manager using the same identifier used by managed_wallet_get_account (or accept
a wallet/account handle that references the shared storage), then fetch the
transaction record from managed.transactions in that shared account and call
record.set_label(...) on that shared record (handling UTF-8 and null label cases
as before), and only then return success so the label persists across handles
and future managed_wallet_get_account calls.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fb5cc841-5a97-4e0b-8ec7-f3eae7b62486

📥 Commits

Reviewing files that changed from the base of the PR and between 747d2fa and f272538.

📒 Files selected for processing (2)
  • key-wallet-ffi/FFI_API.md
  • key-wallet-ffi/src/managed_account.rs

Comment thread key-wallet-ffi/src/managed_account.rs Outdated
CodeRabbit flagged that the original `managed_core_account_set_transaction_label` mutated only a detached snapshot. `wallet_manager_get_managed_wallet_info` returns a cloned `ManagedWalletInfo`, and `FFIManagedCoreAccount::new` wraps each account in `Arc::new(account.clone())`, so writes through the handle never reached the shared `WalletManager::wallet_infos` map and were silently discarded when the handle was freed.

Replace the broken FFI with `wallet_manager_set_transaction_label`, which acquires the manager write lock, navigates to the canonical `ManagedCoreAccount` via `get_wallet_info_mut`, and mutates the shared `TransactionRecord` in place. Drop the vacuous `FFIManagedCoreAccount::inner_mut` (the `Arc::get_mut` guard always succeeded on a freshly cloned `Arc`). Promote `get_managed_account_by_type[_mut]` to `pub(crate)` so the new FFI can reuse the existing dispatch.

Rewrite the tests to insert the `TransactionRecord` through the manager's write lock and verify the label round-trips across handle frees by re-fetching the account after each mutation.

The broader `FFIManagedWalletInfo` dual-path hazard (other mutating FFIs lose writes when the handle comes from the manager) is tracked separately in xdustinface#110.

Addresses CodeRabbit review comment on PR #618
#618 (comment)
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@key-wallet-ffi/FFI_API.md`:
- Around line 719-733: The PR/README exposes the FFI symbol
wallet_manager_set_transaction_label but the PR title and release wording
reference managed_core_account_set_transaction_label; update the PR
title/release notes to reference wallet_manager_set_transaction_label (or
alternatively add a thin exported wrapper function named
managed_core_account_set_transaction_label that forwards to
wallet_manager_set_transaction_label and document it), and ensure the
pr-title.yml allowed prefix list is consistent with the chosen public symbol
name.

In `@key-wallet-ffi/src/managed_account.rs`:
- Around line 2313-2380: The test test_set_transaction_label_errors needs to be
updated to reflect that wallet_manager_set_transaction_label should return
FFIErrorCode::NotFound for missing wallets/txids: change the expected error code
for the case passing an all-zero wallet_id and for the fake_txid (the last two
assertions after calls to wallet_manager_set_transaction_label) from
InvalidInput to NotFound, and add an additional assertion/case that calls
wallet_manager_set_transaction_label with a valid manager and wallet_ids_out but
a non-existent txid for that real wallet to assert NotFound (so null-pointer
cases still assert InvalidInput while existence-mismatch cases assert NotFound).
- Around line 945-971: The lookup failures inside the
manager_ref.runtime.block_on closure currently return plain Err(String) which is
always reported as FFIErrorCode::InvalidInput; change the closure to return a
richer error so the caller can distinguish NotFound vs InvalidInput (e.g. make
the closure return Result<(), (FFIErrorCode, String)> or a small enum), replace
the three .ok_or_else(...) calls to produce Err((FFIErrorCode::NotFound, "Wallet
not found".to_string())) / Err((FFIErrorCode::NotFound, "Account not
found".to_string())) / Err((FFIErrorCode::NotFound, "Transaction not
found".to_string())), and then in the outer error branch call
FFIError::set_error(error, code, msg) using the propagated code; update the
test_set_transaction_label_errors test expectation accordingly to expect
NotFound for the fake-txid case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 12ac23bd-a28b-4030-a390-2d6d21c1362d

📥 Commits

Reviewing files that changed from the base of the PR and between f272538 and 8b08b87.

📒 Files selected for processing (3)
  • key-wallet-ffi/FFI_API.md
  • key-wallet-ffi/src/address_pool.rs
  • key-wallet-ffi/src/managed_account.rs

Comment thread key-wallet-ffi/FFI_API.md
Comment thread key-wallet-ffi/src/managed_account.rs Outdated
Comment thread key-wallet-ffi/src/managed_account.rs
…let_manager_set_transaction_label`

Lookup failures inside the async closure were all surfaced as `FFIErrorCode::InvalidInput`, conflating genuine caller mistakes (null pointers, bad UTF-8, bad txid length) with missing-resource conditions. Other FFI lookup functions in the same file and `WalletError::{WalletNotFound, AccountNotFound}` already map to `FFIErrorCode::NotFound`, so callers branching on the error code could not distinguish programmer errors from an expected "no such txid/wallet/account" outcome.

Propagate a typed `(FFIErrorCode, String)` pair out of the closure so the three lookup failures emit `NotFound` while `set_label` validation continues to emit `InvalidInput`. Update `test_set_transaction_label_errors` to assert `NotFound` for the real-wallet/fake-txid case and add a real-manager/unknown-wallet_id case that exercises the "Wallet not found" path.

Addresses CodeRabbit review comment on PR #618
#618 (comment)
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@key-wallet-ffi/src/managed_account.rs`:
- Around line 943-946: The code creates an unnecessary exclusive borrow with
`&mut *manager` before calling `runtime.block_on` and acquiring
`manager.write()`; replace those exclusive borrows with shared borrows (e.g.
`let manager_ref = &*manager`) so `runtime.block_on` and the async
`manager.write().await` use a shared reference instead of a mutable one; update
both occurrences (the one used before `runtime.block_on` and the second
occurrence around line 2191) to use the shared reference `manager_ref` and
remove any `&mut *manager` usage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 890447ea-a94a-4a76-a161-b2739590386e

📥 Commits

Reviewing files that changed from the base of the PR and between 8b08b87 and 9a2419e.

📒 Files selected for processing (1)
  • key-wallet-ffi/src/managed_account.rs

Comment thread key-wallet-ffi/src/managed_account.rs Outdated
@xdustinface xdustinface changed the title feat: add managed_core_account_set_transaction_label FFI function feat: add wallet_manager_set_transaction_label FFI function Apr 19, 2026
@github-actions github-actions Bot added ready-for-review CodeRabbit has approved this PR merge-conflict The PR conflicts with the target branch. labels Apr 22, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them.

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

Labels

merge-conflict The PR conflicts with the target branch. ready-for-review CodeRabbit has approved this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant