Skip to content

refactor: rename FFI context structs to match key-wallet naming#612

Merged
QuantumExplorer merged 2 commits into
v0.42-devfrom
refactor/ffi-transaction-context-rename
Apr 1, 2026
Merged

refactor: rename FFI context structs to match key-wallet naming#612
QuantumExplorer merged 2 commits into
v0.42-devfrom
refactor/ffi-transaction-context-rename

Conversation

@xdustinface
Copy link
Copy Markdown
Collaborator

@xdustinface xdustinface commented Apr 1, 2026

Summary by CodeRabbit

  • Refactor
    • Streamlined and renamed transaction context representations in the FFI API for clearer separation between context kinds and context metadata; related interfaces updated for consistency.
  • Examples
    • Updated example usage to reflect the new context naming and interface.
  • Tests
    • Adjusted unit and integration tests to align with the revised context types and constructors.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

A cohesive refactor in key-wallet-ffi renames the enum FFITransactionContextFFITransactionContextType and the struct FFITransactionContextDetailsFFITransactionContext, updating related function signatures, imports, conversions, and tests across the crate.

Changes

Cohort / File(s) Summary
Type Definition Refactoring
key-wallet-ffi/src/types.rs
Renamed exported enum FFITransactionContextFFITransactionContextType and replaced struct FFITransactionContextDetailsFFITransactionContext; updated transaction_context_from_ffi signature, constructors, and From/To conversion implementations; adjusted unit tests.
FFI API docs
key-wallet-ffi/FFI_API.md
Updated FFI function signatures and safety docs to use the new type names for wallet_manager_process_transaction, managed_wallet_check_transaction, and wallet_check_transaction.
FFI callers & runtime
key-wallet-ffi/src/wallet_manager.rs, key-wallet-ffi/src/transaction_checking.rs, key-wallet-ffi/src/transaction.rs, key-wallet-ffi/src/managed_account.rs
Changed function parameter types and imports to use FFITransactionContextType for context kind and FFITransactionContext for the container; updated conversion calls (e.g., .to_transaction_context()), constructor usage, and struct field types where appropriate.
Tests
key-wallet-ffi/src/wallet_manager_tests.rs
Replaced test usages of FFITransactionContextDetails::... with FFITransactionContext::... constructors and adjusted discriminant assertions to the renamed enum.
Example
key-wallet-ffi/examples/check_transaction.c
Renamed the example typedef FFITransactionContextFFITransactionContextType and updated the wallet_check_transaction declaration to accept FFITransactionContextType.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I hopped through types with care,

Swapped names and stitched them fair;
ContextDetails made way,
Context and Type now play,
A small carrot-coded repair 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: renaming FFI context structs to align with key-wallet naming conventions, which is the primary focus of the entire changeset across multiple files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 refactor/ffi-transaction-context-rename

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 87.09677% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.44%. Comparing base (b5cc235) to head (f61ac42).
⚠️ Report is 1 commits behind head on v0.42-dev.

Files with missing lines Patch % Lines
key-wallet-ffi/src/types.rs 91.30% 2 Missing ⚠️
key-wallet-ffi/src/transaction.rs 0.00% 1 Missing ⚠️
key-wallet-ffi/src/transaction_checking.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##           v0.42-dev     #612      +/-   ##
=============================================
+ Coverage      67.40%   67.44%   +0.04%     
=============================================
  Files            320      320              
  Lines          67946    67949       +3     
=============================================
+ Hits           45800    45830      +30     
+ Misses         22146    22119      -27     
Flag Coverage Δ
core 75.21% <ø> (ø)
ffi 36.48% <87.09%> (+0.25%) ⬆️
rpc 19.92% <ø> (ø)
spv 83.80% <ø> (+0.02%) ⬆️
wallet 67.59% <ø> (ø)
Files with missing lines Coverage Δ
key-wallet-ffi/src/managed_account.rs 44.91% <100.00%> (ø)
key-wallet-ffi/src/wallet_manager.rs 59.52% <100.00%> (+4.76%) ⬆️
key-wallet-ffi/src/transaction.rs 0.00% <0.00%> (ø)
key-wallet-ffi/src/transaction_checking.rs 1.66% <80.00%> (ø)
key-wallet-ffi/src/types.rs 76.53% <91.30%> (+0.16%) ⬆️

... and 3 files with indirect coverage changes

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
key-wallet-ffi/FFI_API.md (1)

856-863: ⚠️ Potential issue | 🟡 Minor

These generated docs still describe the old contract.

Both entries now take FFITransactionContextType, but the surrounding text is still stale, and wallet_check_transaction still documents removed outputs like inputs_spent_out and new_address_out. Since this file is auto-generated, please update the source rustdoc on key-wallet-ffi/src/transaction_checking.rs and key-wallet-ffi/src/transaction.rs and regenerate the API docs.

Also applies to: 1320-1327

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@key-wallet-ffi/FFI_API.md` around lines 856 - 863, Update the autogenerated
docs by fixing the rustdoc comments in
key-wallet-ffi/src/transaction_checking.rs and key-wallet-ffi/src/transaction.rs
to reflect the new function signatures: document that both
managed_wallet_check_transaction and wallet_check_transaction now accept
FFITransactionContextType, remove references to removed outputs like
inputs_spent_out and new_address_out, and update the safety/description text to
match the current FFITransactionCheckResult shape (including noting
affected_accounts must be freed by transaction_check_result_free); then
regenerate the FFI_API.md so the generated docs and descriptions match the
actual function signatures and result fields.
key-wallet-ffi/src/types.rs (1)

841-923: ⚠️ Potential issue | 🟠 Major

This public rename needs the example and PR metadata updated too.

FFITransactionContext changes meaning from enum to struct here, but key-wallet-ffi/examples/check_transaction.c:15-24 still declares FFITransactionContext as the enum. That leaves the bundled example out of sync with the exported FFI surface, and refactor: does not fully signal the downstream rename/breaking change. Please update the example in the same PR and call the API break out explicitly.

As per coding guidelines, "Check whether the PR title prefix allowed in the pr-title.yml workflow accurately describes the changes."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@key-wallet-ffi/src/types.rs` around lines 841 - 923, The exported FFI type
FFITransactionContext was changed from an enum to a struct but the bundled C
example (check_transaction.c) still treats it as an enum and must be updated;
modify the example to construct and read the new struct form (use
FFITransactionContext.mempool(), in_block(), in_chain_locked_block() semantics
from the Rust API via the FFI helpers or adapt the C code to access
.context_type and .block_info), and also update PR metadata/pr title to
explicitly declare an API-breaking change (replace the "refactor:" prefix with a
breaking-aware prefix and adjust any pr-title.yml entries or PR description to
reflect the breaking rename) so downstream consumers know the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@key-wallet-ffi/FFI_API.md`:
- Around line 856-863: Update the autogenerated docs by fixing the rustdoc
comments in key-wallet-ffi/src/transaction_checking.rs and
key-wallet-ffi/src/transaction.rs to reflect the new function signatures:
document that both managed_wallet_check_transaction and wallet_check_transaction
now accept FFITransactionContextType, remove references to removed outputs like
inputs_spent_out and new_address_out, and update the safety/description text to
match the current FFITransactionCheckResult shape (including noting
affected_accounts must be freed by transaction_check_result_free); then
regenerate the FFI_API.md so the generated docs and descriptions match the
actual function signatures and result fields.

In `@key-wallet-ffi/src/types.rs`:
- Around line 841-923: The exported FFI type FFITransactionContext was changed
from an enum to a struct but the bundled C example (check_transaction.c) still
treats it as an enum and must be updated; modify the example to construct and
read the new struct form (use FFITransactionContext.mempool(), in_block(),
in_chain_locked_block() semantics from the Rust API via the FFI helpers or adapt
the C code to access .context_type and .block_info), and also update PR
metadata/pr title to explicitly declare an API-breaking change (replace the
"refactor:" prefix with a breaking-aware prefix and adjust any pr-title.yml
entries or PR description to reflect the breaking rename) so downstream
consumers know the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8960d1a9-d3f3-49e1-994b-a1bc895491e2

📥 Commits

Reviewing files that changed from the base of the PR and between b5cc235 and 6c29ed9.

📒 Files selected for processing (7)
  • key-wallet-ffi/FFI_API.md
  • key-wallet-ffi/src/managed_account.rs
  • key-wallet-ffi/src/transaction.rs
  • key-wallet-ffi/src/transaction_checking.rs
  • key-wallet-ffi/src/types.rs
  • key-wallet-ffi/src/wallet_manager.rs
  • key-wallet-ffi/src/wallet_manager_tests.rs

coderabbitai[bot]
coderabbitai Bot previously approved these changes Apr 1, 2026
@github-actions github-actions Bot added the ready-for-review CodeRabbit has approved this PR label Apr 1, 2026
@github-actions github-actions Bot removed the ready-for-review CodeRabbit has approved this PR label Apr 1, 2026
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
key-wallet-ffi/examples/check_transaction.c (2)

81-93: ⚠️ Potential issue | 🔴 Critical

Call sites also need to be updated to match the corrected signature.

Once the function declaration is fixed to use FFIBlockInfo, the call sites must be updated. For example:

Example fix for mempool transaction check (lines 81-93)
+    FFIBlockInfo mempool_block_info = {0};  // Zeroed for mempool
     bool success = wallet_check_transaction(
         wallet,
-        network,
         tx_bytes,
         tx_len,
         Mempool,  // Transaction is in mempool
-        0,        // No block height for mempool tx
-        NULL,     // No block hash for mempool tx
-        0,        // No timestamp
+        mempool_block_info,
         false,    // Don't update wallet state
         &result,
         &error
     );
Example fix for confirmed transaction check (lines 110-122)
     uint8_t block_hash[32] = { /* ... block hash ... */ };
+    FFIBlockInfo confirmed_block_info = {
+        .height = 650000,
+        .hash = { /* copy block_hash */ },
+        .timestamp = 1234567890
+    };
+    memcpy(confirmed_block_info.hash, block_hash, 32);
+    
     success = wallet_check_transaction(
         wallet,
-        network,
         tx_bytes,
         tx_len,
         InBlock,      // Transaction is in a block
-        650000,       // Block height
-        block_hash,   // Block hash
-        1234567890,   // Unix timestamp
+        confirmed_block_info,
         true,         // Update wallet state
         &result,
         &error
     );

Also applies to: 110-122

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@key-wallet-ffi/examples/check_transaction.c` around lines 81 - 93, The call
sites must be updated to match the corrected wallet_check_transaction signature
that now takes an FFIBlockInfo pointer; create an FFIBlockInfo variable (e.g.,
FFIBlockInfo block_info) and populate its fields for mempool checks (set .source
= Mempool, .height = 0, .hash = NULL, .timestamp = 0) and for confirmed checks
(set .source = Block, .height = <block_height>, .hash = <block_hash>, .timestamp
= <timestamp>), then pass &block_info as the block info argument to
wallet_check_transaction instead of separate height/hash/timestamp parameters;
update all call sites (the mempool example around wallet_check_transaction and
the confirmed-transaction example) to use this pattern and keep passing &result
and &error as before.

43-55: ⚠️ Potential issue | 🔴 Critical

Critical FFI signature mismatch will cause undefined behavior.

The extern declaration does not match the actual Rust wallet_check_transaction signature in transaction.rs:

  1. Extra network parameter (line 45): The Rust function has no FFINetwork parameter.
  2. Block info structure mismatch (lines 49-51): The Rust function expects a single block_info: FFIBlockInfo struct, but this C code declares three separate parameters (block_height, block_hash, timestamp).
  3. Timestamp type mismatch: The Rust struct uses u32 for timestamp, not u64.

This parameter misalignment at the FFI boundary will cause arguments to be interpreted incorrectly, leading to undefined behavior at runtime.

Proposed fix to match actual Rust FFI signature

First, add the FFIBlockInfo struct definition:

+typedef struct {
+    uint32_t height;
+    uint8_t block_hash[32];
+    uint32_t timestamp;
+} FFIBlockInfo;
+
 // External function declarations

Then fix the function declaration:

 extern bool wallet_check_transaction(
     void* wallet,
-    FFINetwork network,
     const uint8_t* tx_bytes,
     size_t tx_len,
     FFITransactionContextType context_type,
-    uint32_t block_height,
-    const uint8_t* block_hash,  // 32 bytes if not null
-    uint64_t timestamp,
+    FFIBlockInfo block_info,
     bool update_state,
     FFITransactionCheckResult* result_out,
     FFIError* error
 );

Update the call sites accordingly (lines 81-93 and 110-122).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@key-wallet-ffi/examples/check_transaction.c` around lines 43 - 55, The extern
declaration for wallet_check_transaction is misaligned with the Rust FFI: remove
the extra FFINetwork parameter, replace the separate
block_height/block_hash/timestamp parameters with a single FFIBlockInfo
parameter (define the FFIBlockInfo struct in this C file to match the Rust
layout) and change the timestamp field/type to uint32_t to match Rust's u32;
keep the remaining parameters (void* wallet, FFITransactionContextType
context_type, bool update_state, FFITransactionCheckResult* result_out,
FFIError* error) in the same order and update all local call sites that pass
block_height/block_hash/timestamp to instead construct and pass an FFIBlockInfo
instance when calling wallet_check_transaction.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@key-wallet-ffi/examples/check_transaction.c`:
- Around line 81-93: The call sites must be updated to match the corrected
wallet_check_transaction signature that now takes an FFIBlockInfo pointer;
create an FFIBlockInfo variable (e.g., FFIBlockInfo block_info) and populate its
fields for mempool checks (set .source = Mempool, .height = 0, .hash = NULL,
.timestamp = 0) and for confirmed checks (set .source = Block, .height =
<block_height>, .hash = <block_hash>, .timestamp = <timestamp>), then pass
&block_info as the block info argument to wallet_check_transaction instead of
separate height/hash/timestamp parameters; update all call sites (the mempool
example around wallet_check_transaction and the confirmed-transaction example)
to use this pattern and keep passing &result and &error as before.
- Around line 43-55: The extern declaration for wallet_check_transaction is
misaligned with the Rust FFI: remove the extra FFINetwork parameter, replace the
separate block_height/block_hash/timestamp parameters with a single FFIBlockInfo
parameter (define the FFIBlockInfo struct in this C file to match the Rust
layout) and change the timestamp field/type to uint32_t to match Rust's u32;
keep the remaining parameters (void* wallet, FFITransactionContextType
context_type, bool update_state, FFITransactionCheckResult* result_out,
FFIError* error) in the same order and update all local call sites that pass
block_height/block_hash/timestamp to instead construct and pass an FFIBlockInfo
instance when calling wallet_check_transaction.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2872d86f-7d70-4928-b918-dd6a34dc7f27

📥 Commits

Reviewing files that changed from the base of the PR and between 6c29ed9 and f61ac42.

📒 Files selected for processing (1)
  • key-wallet-ffi/examples/check_transaction.c

@QuantumExplorer QuantumExplorer merged commit 09f03e0 into v0.42-dev Apr 1, 2026
43 of 44 checks passed
@QuantumExplorer QuantumExplorer deleted the refactor/ffi-transaction-context-rename branch April 1, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review CodeRabbit has approved this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants