Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3e40057
fix(swift-sdk): reconcile SwiftData transaction accounting per wallet
lklimek Oct 13, 2026
4413d32
fix(swift-sdk): report a received amount despite a contact's watch-on…
lklimek Oct 13, 2026
7a282c9
fix(swift-sdk): scope linked outputs to the spending wallets in recon…
lklimek Oct 13, 2026
70a1b30
fix(swift-sdk): reconcile accounting when a TXO heal completes a spend
lklimek Oct 13, 2026
bbb269c
chore: merge updated wallet base into Swift transaction accounting
lklimek Oct 21, 2026
018f00f
fix(swift-sdk): preserve base V4 schema before accounting migration
lklimek Oct 21, 2026
a22e324
style(swift-sdk): align migration stage indentation
lklimek Oct 21, 2026
94f9833
chore(platform-wallet): propagate reviewed SQLite capability fix
lklimek Oct 21, 2026
9400fa3
Merge branch 'fix/pr-5126' into fix/swiftdata-transaction-accounting
lklimek Oct 27, 2026
accca0d
feat(platform-wallet): emit per-wallet transaction accounting to pers…
lklimek Oct 28, 2026
c081e87
feat(swift-sdk): persist Rust-emitted per-wallet transaction accounting
lklimek Oct 28, 2026
b59aa58
refactor(swift-sdk): drop the Swift-side transaction accounting deriv…
lklimek Oct 28, 2026
aa086b4
fix(swift-sdk): log the live schema version as the migration target
lklimek Oct 28, 2026
98db33e
fix(platform-wallet): make the accounting backfill converge and match…
lklimek Oct 28, 2026
651d1d5
fix(swift-sdk): mark per-wallet accounting backfills and stop scalar …
lklimek Oct 28, 2026
01ceb6c
fix(platform-wallet): leave a wallet spend with an unresolved coin un…
lklimek Oct 29, 2026
4b0879a
fix(platform-wallet-ffi): read accounting funding bodies strictly and…
lklimek Oct 29, 2026
1144d8b
fix(swift-sdk): fail the accounting round on a transaction-row read e…
lklimek Oct 29, 2026
c1d068b
docs(platform-wallet): state the P2PKH premise of the owned-input check
lklimek Oct 29, 2026
3a111c1
docs(swift-sdk): describe the open backfill in the V5 schema notes
lklimek Oct 29, 2026
f323849
fix(swift-sdk): report a failed tx-record fetch to Rust instead of a …
lklimek Oct 29, 2026
fa006bc
chore: merge fix/pr-5126 into PR 5417
lklimek Nov 5, 2026
be655b8
refactor(wallet): integrate accounting-only storage into Swift history
lklimek Nov 5, 2026
7101253
fix(swift-sdk): expose accounting formatter and correct migration ass…
lklimek Nov 5, 2026
1ea9eab
chore(swift-sdk): merge validated history prerequisites into accounting
lklimek Nov 5, 2026
39a3319
chore: merge fix/sqlite-asset-unlock-history-aliases into PR 5417
lklimek Nov 6, 2026
28a6650
fix(platform-wallet): follow the rust-dashcore hash API in transactio…
lklimek Nov 6, 2026
d8bbf50
test(swift-sdk): assert migrations land on the current schema version
lklimek Nov 6, 2026
f0e4ac3
Merge branch 'merge/5783-result' into fix/swiftdata-transaction-accou…
lklimek Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 66 additions & 6 deletions packages/rs-platform-wallet-ffi/src/core_wallet_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! C-compatible types for core wallet changeset FFI.

use key_wallet::managed_account::transaction_record::TransactionDirection;
use platform_wallet::changeset::WalletAccounting;
use platform_wallet::masternode::{provider_payload_fields, MasternodeRecord};
use std::os::raw::c_char;

Expand Down Expand Up @@ -363,6 +365,38 @@ pub struct AccountChangeSetFFI {
pub has_internal_highest_used: bool,
}

/// One transaction's Core accounting from ONE wallet's point of view, as
/// `platform_wallet::changeset::WalletAccounting` computes it. The host
/// persists it keyed by `(txid, wallet_id)` (the wallet is the callback's
/// `wallet_id`) and displays it; it never derives or corrects it.
///
/// [`TransactionRecordFFI`]'s own `net_amount` / `direction` / `fee` stay
/// for hosts that predate this row. A record with no accounting evidence
/// yields NO row here, so the host keeps what it stored.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransactionAccountingFFI {
pub txid: [u8; 32],
/// `Σ owned outputs − Σ owned inputs` in duffs (positive = received).
pub net_amount: i64,
/// 0=incoming, 1=outgoing, 2=internal, 3=coinJoin.
pub direction: u32,
/// Meaningful only when `has_fee`.
pub fee: u64,
pub has_fee: bool,
}

/// One stored Core transaction without this wallet's accounting row, handed
/// back for the load-time backfill (see
/// `on_load_core_history_for_accounting_fn`). Host-owned until the matching
/// free callback.
#[repr(C)]
pub struct CoreHistoryTransactionFFI {
/// Consensus-encoded transaction body.
pub tx_bytes: *const u8,
pub tx_bytes_len: usize,
Comment on lines +394 to +397

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🔴 Blocking: Preserve persisted transaction identities through the SwiftData backfill

CoreHistoryTransactionFFI carries only transaction bytes, and the native history input similarly loses the stored transaction key. That key cannot always be reconstructed with today's Transaction::txid(): historical AssetUnlock v2 records can legitimately be keyed by instance_hash(), as the SQLite historical-identity implementation and fixtures in this range recognize. Swift originally stores TransactionRecordFFI.txid unchanged, but history_accounting emits the newly computed canonical id. upsertTransactionAccounting cannot find the historical row under that id, silently returns success, and allows the completion marker to commit. Carry the persisted identifier through the history contract, validate its canonical or historical identity in Rust, and emit accounting attached to the actual persisted row. Cover the historical AssetUnlock fixture through the SwiftData callback path.

source: gpt-6.1-sol (phase2-reviewer: general, architecture-layering, ffi-engineer, platform-versioning, rust-quality, security-auditor)

}

// ---------------------------------------------------------------------------
// Top-level wallet changeset
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1523,6 +1557,37 @@ pub(crate) fn masternode_entry_v2_ffi(
}
}

/// `direction` wire values of [`TransactionRecordFFI`] and
/// [`TransactionAccountingFFI`], exported so hosts name them from the
/// generated header instead of re-declaring them.
pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING: u32 = 0;
pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING: u32 = 1;
pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL: u32 = 2;
pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN: u32 = 3;

fn direction_to_u32(direction: TransactionDirection) -> u32 {
match direction {
TransactionDirection::Incoming => PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING,
TransactionDirection::Outgoing => PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING,
TransactionDirection::Internal => PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL,
TransactionDirection::CoinJoin => PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN,
}
}

impl From<&WalletAccounting> for TransactionAccountingFFI {
fn from(row: &WalletAccounting) -> Self {
let mut txid = [0u8; 32];
txid.copy_from_slice(row.txid.as_ref());
Self {
txid,
net_amount: row.net_amount,
direction: direction_to_u32(row.direction),
fee: row.fee.unwrap_or(0),
has_fee: row.fee.is_some(),
}
}
}

fn tx_record_to_ffi(
tr: &key_wallet::managed_account::transaction_record::TransactionRecord,
) -> TransactionRecordFFI {
Expand Down Expand Up @@ -1569,12 +1634,7 @@ fn tx_record_to_ffi(
}
};

let dir_val = match tr.direction {
key_wallet::managed_account::transaction_record::TransactionDirection::Incoming => 0u32,
key_wallet::managed_account::transaction_record::TransactionDirection::Outgoing => 1,
key_wallet::managed_account::transaction_record::TransactionDirection::Internal => 2,
key_wallet::managed_account::transaction_record::TransactionDirection::CoinJoin => 3,
};
let dir_val = direction_to_u32(tr.direction);

let type_str = CString::new(format!("{:?}", tr.transaction_type))
.unwrap_or_else(|_| CString::new("Unknown").unwrap());
Expand Down
91 changes: 87 additions & 4 deletions packages/rs-platform-wallet-ffi/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::event_handler::{
};
use crate::handle::*;
use crate::persistence::{
FFIPersister, FreeTrackedMasternodesFn, LoadAssetLockOperationIdFn,
LoadAssetLockShieldedRecipientFn, LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn,
PersistAssetLockOperationIdFn, PersistAssetLockShieldedRecipientFn, PersistDpnsNameStatesFn,
PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn,
FFIPersister, FreeCoreHistoryForAccountingFn, FreeTrackedMasternodesFn,
LoadAssetLockOperationIdFn, LoadAssetLockShieldedRecipientFn, LoadCoreHistoryForAccountingFn,
LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn,
PersistAssetLockShieldedRecipientFn, PersistCoreHistoryAccountedFn, PersistDpnsNameStatesFn,
PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistTransactionAccountingFn,
PersistWalletChangesetChainLockHeightFn, PersistWalletChangesetSweepsFn,
PersistWalletChangesetUtxoVerdictsFn, PersistWalletDashPayBackfillFn, PersistenceCallbacks,
PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks,
Expand Down Expand Up @@ -299,6 +300,22 @@ unsafe fn persistence_extension_callbacks(
on_load_asset_lock_shielded_recipient_fn,
LoadAssetLockShieldedRecipientFn
),
persist_transaction_accounting: slot!(
on_persist_transaction_accounting_fn,
PersistTransactionAccountingFn
),
load_core_history_for_accounting: slot!(
on_load_core_history_for_accounting_fn,
LoadCoreHistoryForAccountingFn
),
load_core_history_for_accounting_free: slot!(
on_load_core_history_for_accounting_free_fn,
FreeCoreHistoryForAccountingFn
),
persist_core_history_accounted: slot!(
on_persist_core_history_accounted_fn,
PersistCoreHistoryAccountedFn
),
}
}

Expand Down Expand Up @@ -1424,6 +1441,72 @@ mod tests {
assert!(parsed.load_asset_lock_operation_id.is_none());
}

/// Each transaction-accounting slot is read only when the host's
/// `struct_size` covers it, so a host built before any of them simply
/// never has them read.
#[test]
fn should_size_gate_transaction_accounting_slots_independently() {
use crate::core_wallet_types::{CoreHistoryTransactionFFI, TransactionAccountingFFI};
unsafe extern "C" fn persist(
_: *mut c_void,
_: *const u8,
_: *const TransactionAccountingFFI,
_: usize,
) -> i32 {
0
}
unsafe extern "C" fn load(
_: *mut c_void,
_: *const u8,
_: *mut bool,
_: *mut *const CoreHistoryTransactionFFI,
_: *mut usize,
) -> i32 {
0
}
unsafe extern "C" fn free(_: *mut c_void, _: *const CoreHistoryTransactionFFI, _: usize) {}
unsafe extern "C" fn accounted(_: *mut c_void, _: *const u8) -> i32 {
0
}
let mut ext = PersistenceCallbacksExtension {
on_persist_transaction_accounting_fn: Some(persist),
on_load_core_history_for_accounting_fn: Some(load),
on_load_core_history_for_accounting_free_fn: Some(free),
on_persist_core_history_accounted_fn: Some(accounted),
..Default::default()
};
let present = |ext: &PersistenceCallbacksExtension| {
let parsed = unsafe { persistence_extension_callbacks(ext) };
[
parsed.persist_transaction_accounting.is_some(),
parsed.load_core_history_for_accounting.is_some(),
parsed.load_core_history_for_accounting_free.is_some(),
parsed.persist_core_history_accounted.is_some(),
]
};
assert_eq!(present(&ext), [true; 4]);
ext.struct_size = std::mem::offset_of!(
PersistenceCallbacksExtension,
on_persist_core_history_accounted_fn
);
assert_eq!(present(&ext), [true, true, true, false]);
ext.struct_size = std::mem::offset_of!(
PersistenceCallbacksExtension,
on_load_core_history_for_accounting_free_fn
);
assert_eq!(present(&ext), [true, true, false, false]);
ext.struct_size = std::mem::offset_of!(
PersistenceCallbacksExtension,
on_load_core_history_for_accounting_fn
);
assert_eq!(present(&ext), [true, false, false, false]);
ext.struct_size = std::mem::offset_of!(
PersistenceCallbacksExtension,
on_persist_transaction_accounting_fn
);
assert_eq!(present(&ext), [false; 4]);
}

/// A host whose `struct_size` stops right after the chainlock-height
/// slot (built before the credit-verdict slot existed) keeps every
/// earlier slot and simply never has the verdict slot read; a host
Expand Down
Loading
Loading