Skip to content

refactor: extract BlockInfo from TransactionContext#578

Merged
xdustinface merged 1 commit intov0.42-devfrom
refactor/extract-block-info-from-transaction-context
Mar 25, 2026
Merged

refactor: extract BlockInfo from TransactionContext#578
xdustinface merged 1 commit intov0.42-devfrom
refactor/extract-block-info-from-transaction-context

Conversation

@xdustinface
Copy link
Collaborator

@xdustinface xdustinface commented Mar 24, 2026

The InBlock and InChainLockedBlock variants of TransactionContext used Option<BlockHash> and Option<u32> for block hash and timestamp, but these were never None in practice. Extract them into a dedicated BlockInfo struct with required fields, replacing the per-field accessor methods (block_height(), block_hash(), timestamp()) with a single block_info() that returns the struct.

Move BlockInfo and TransactionContext from wallet_checker.rs into their own transaction_context.rs module since they are general-purpose types, not specific to wallet checking logic. Also move the FFI helper block_info_from_ffi from lib.rs to types.rs where it belongs with other FFI type conversions.

Summary by CodeRabbit

  • Refactor
    • Improved internal organization of transaction context and block metadata handling.
    • Enhanced transaction confirmation logic for more reliable block information tracking.
    • Streamlined block parameter processing across FFI and wallet modules.

The `InBlock` and `InChainLockedBlock` variants of `TransactionContext`
used `Option<BlockHash>` and `Option<u32>` for block hash and timestamp,
but these were never `None` in practice. Extract them into a dedicated
`BlockInfo` struct with required fields, replacing the per-field accessor
methods (`block_height()`, `block_hash()`, `timestamp()`) with a single
`block_info()` that returns the struct.

Move `BlockInfo` and `TransactionContext` from `wallet_checker.rs` into
their own `transaction_context.rs` module since they are general-purpose
types, not specific to wallet checking logic. Also move the FFI helper
`block_info_from_ffi` from `lib.rs` to `types.rs` where it belongs with
other FFI type conversions.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 24, 2026

📝 Walkthrough

Walkthrough

This PR refactors block metadata handling across the transaction checking layer by introducing a new BlockInfo struct to encapsulate block height, hash, and timestamp. The TransactionContext enum is moved to a dedicated module and updated to use BlockInfo instead of separate fields. FFI conversion logic is simplified via a shared helper function, and all downstream usage is updated consistently.

Changes

Cohort / File(s) Summary
Transaction Context Definition
key-wallet/src/transaction_checking/transaction_context.rs (new)
Introduces BlockInfo struct and moves TransactionContext enum with variants refactored to use BlockInfo for block-related data. Adds Display impl and internal helper methods confirmed() and block_info().
Module Organization
key-wallet/src/transaction_checking/mod.rs
Updates re-exports to move TransactionContext from wallet_checker module to transaction_context module; adds BlockInfo re-export.
Core Wallet Checking
key-wallet/src/transaction_checking/wallet_checker.rs
Removes local TransactionContext definition and imports it from transaction_context module; updates test constructors to use BlockInfo::new(...) pattern.
FFI Layer Helpers
key-wallet-ffi/src/types.rs
Adds new block_info_from_ffi() unsafe helper to consolidate FFI block conversion logic; updates FFITransactionContextDetails::to_transaction_context() to delegate to this helper.
FFI Transaction Handling
key-wallet-ffi/src/transaction.rs, key-wallet-ffi/src/transaction_checking.rs
Simplifies block metadata parsing by calling block_info_from_ffi() instead of inlining pointer-to-hash and timestamp conversions.
Transaction Processing
key-wallet/src/managed_account/mod.rs, key-wallet/src/manager/process_block.rs
Updates UTXO and transaction recording logic to extract block metadata from BlockInfo via context.block_info(); constructs BlockInfo::new() during block processing.
Test Suite Updates
key-wallet/src/transaction_checking/transaction_router/tests/*
Across multiple test files (asset_unlock.rs, coinbase.rs, identity_transactions.rs, provider.rs, routing.rs), updates TransactionContext::InBlock construction to use BlockInfo::new(height, block_hash, timestamp) pattern instead of struct literals with optional fields.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

ready-for-review

Suggested reviewers

  • ZocoLini

Poem

🐰 Block info bundled, oh what a sight!
Transactions now flow through contexts so bright,
Struct fields compressed in types so clean,
The finest refactoring I've ever seen! ✨

🚥 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 'refactor: extract BlockInfo from TransactionContext' accurately and clearly describes the main change: extracting a new BlockInfo struct from TransactionContext and moving it to a dedicated module.
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/extract-block-info-from-transaction-context

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

codecov bot commented Mar 24, 2026

Codecov Report

❌ Patch coverage is 63.77953% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.56%. Comparing base (2ac3c9c) to head (d065738).
⚠️ Report is 4 commits behind head on v0.42-dev.

Files with missing lines Patch % Lines
key-wallet-ffi/src/types.rs 0.00% 21 Missing ⚠️
...et/src/transaction_checking/transaction_context.rs 48.48% 17 Missing ⚠️
key-wallet-ffi/src/transaction.rs 0.00% 4 Missing ⚠️
key-wallet-ffi/src/transaction_checking.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##           v0.42-dev     #578      +/-   ##
=============================================
+ Coverage      66.33%   66.56%   +0.23%     
=============================================
  Files            311      312       +1     
  Lines          64976    64909      -67     
=============================================
+ Hits           43102    43209     +107     
+ Misses         21874    21700     -174     
Flag Coverage Δ
core 75.13% <ø> (ø)
ffi 38.32% <0.00%> (+1.52%) ⬆️
rpc 28.28% <ø> (ø)
spv 81.11% <ø> (ø)
wallet 66.19% <82.65%> (-0.09%) ⬇️
Files with missing lines Coverage Δ
key-wallet/src/managed_account/mod.rs 49.44% <100.00%> (-0.19%) ⬇️
key-wallet/src/manager/process_block.rs 47.91% <ø> (-1.07%) ⬇️
...-wallet/src/transaction_checking/wallet_checker.rs 96.53% <100.00%> (+1.42%) ⬆️
key-wallet-ffi/src/transaction.rs 0.00% <0.00%> (ø)
key-wallet-ffi/src/transaction_checking.rs 1.65% <0.00%> (+0.07%) ⬆️
...et/src/transaction_checking/transaction_context.rs 48.48% <48.48%> (ø)
key-wallet-ffi/src/types.rs 59.20% <0.00%> (+0.77%) ⬆️

... and 16 files with indirect coverage changes

Copy link
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/types.rs`:
- Around line 14-28: The FFI wrapper block_info_from_ffi silently truncates a
u64 timestamp to u32 when calling BlockInfo::new; add a safety check and
documentation: update the function's safety docs to state the timestamp must fit
in u32 and add a debug_assert!(timestamp <= u32::MAX, "timestamp out of range")
(or similar runtime check) before casting, so unexpected large values are caught
during development while still calling BlockInfo::new(height, block_hash,
timestamp as u32).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4236dadb-ebb5-4903-8aae-7d48a45257b1

📥 Commits

Reviewing files that changed from the base of the PR and between 19fb152 and d065738.

📒 Files selected for processing (13)
  • key-wallet-ffi/src/transaction.rs
  • key-wallet-ffi/src/transaction_checking.rs
  • key-wallet-ffi/src/types.rs
  • key-wallet/src/managed_account/mod.rs
  • key-wallet/src/manager/process_block.rs
  • key-wallet/src/transaction_checking/mod.rs
  • key-wallet/src/transaction_checking/transaction_context.rs
  • key-wallet/src/transaction_checking/transaction_router/tests/asset_unlock.rs
  • key-wallet/src/transaction_checking/transaction_router/tests/coinbase.rs
  • key-wallet/src/transaction_checking/transaction_router/tests/identity_transactions.rs
  • key-wallet/src/transaction_checking/transaction_router/tests/provider.rs
  • key-wallet/src/transaction_checking/transaction_router/tests/routing.rs
  • key-wallet/src/transaction_checking/wallet_checker.rs

@xdustinface xdustinface requested a review from ZocoLini March 24, 2026 18:21
@github-actions github-actions bot added the ready-for-review CodeRabbit has approved this PR label Mar 24, 2026
@xdustinface xdustinface merged commit f2d2dfe into v0.42-dev Mar 25, 2026
42 checks passed
@xdustinface xdustinface deleted the refactor/extract-block-info-from-transaction-context branch March 25, 2026 02:13
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