Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,9 @@ class ProviderSpecialTxRestoreData(
* `onWalletChangesetUtxoAdded`); [scriptPubKey] is the output script —
* the Rust side reconstructs the address from `(script, network)`, so
* no address string crosses the FFI. `isTrusted` is runtime-only and
* recomputed on the next SPV pass (not carried).
* recomputed on the next SPV pass (not carried). [parentContext] /
* [parentBlockHeight] carry the creating transaction's row so the loader
* can raise a coin that lags its own transaction's confirmation.
*/
class UtxoRestoreData(
@JvmField val typeTag: Byte,
Expand All @@ -1136,6 +1138,14 @@ class UtxoRestoreData(
@JvmField val isConfirmed: Boolean,
@JvmField val isInstantLocked: Boolean,
@JvmField val isLocked: Boolean,
/**
* The stored context of the coin's creating transaction, copied as-is
* (0=mempool, 1=instantSend, 2=inBlock, 3=inChainLockedBlock; 0 when
* there is no row). The Rust loader decides what it means for the coin.
*/
@JvmField val parentContext: Int,
/** The stored block height of the coin's creating transaction; 0 when none. */
@JvmField val parentBlockHeight: Int,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3230,10 +3230,21 @@ class PlatformWalletPersistenceHandler(
* except here the single row is skipped rather than failing the
* whole load, matching this builder's per-row-skip convention.
*/
/** The txid of the transaction that created this coin: the row's own column, else the outpoint's txid half. */
private fun TxoEntity.creatingTxid(): ByteArray = txid ?: outpoint.copyOfRange(0, minOf(32, outpoint.size))

private suspend fun buildUtxoRestoreData(walletId: ByteArray): Array<UtxoRestoreData> {
val rows = database.txoDao().observeUnspentByWallet(walletId).first()
if (rows.isEmpty()) return emptyArray()
val out = ArrayList<UtxoRestoreData>(rows.size)
// The creating transactions' rows, read once for the whole batch:
// each coin carries its transaction's stored context so the Rust
// loader can raise a coin that lags its own transaction's
// confirmation.
val parentByTxid = chunkedFlatMap(
rows.map { it.creatingTxid() }.distinctBy { it.toHex() },
) { database.transactionDao().getConfirmationsByTxids(it) }
.associateBy { it.txid.toHex() }
// Per-call memo of address → account row (a wallet's UTXOs
// cluster on few addresses/accounts; avoids N duplicate joins).
val accountByAddress = HashMap<String, AccountEntity?>()
Expand Down Expand Up @@ -3281,11 +3292,12 @@ class PlatformWalletPersistenceHandler(
continue
}
val typeTag = account.accountType.toByte()
val prevTxid = txo.txid ?: txo.outpoint.copyOfRange(0, minOf(32, txo.outpoint.size))
val prevTxid = txo.creatingTxid()
if (prevTxid.size != 32) {
Log.w(TAG, "load: skipping UTXO with ${prevTxid.size}-byte txid")
continue
}
val parent = parentByTxid[prevTxid.toHex()]
out.add(
UtxoRestoreData(
typeTag = typeTag,
Expand All @@ -3304,6 +3316,8 @@ class PlatformWalletPersistenceHandler(
isConfirmed = txo.isConfirmed,
isInstantLocked = txo.isInstantLocked,
isLocked = txo.isLocked,
parentContext = parent?.context ?: 0,
parentBlockHeight = parent?.blockHeight ?: 0,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ interface TransactionDao {
@Query("SELECT * FROM transactions WHERE txid IN (:txids)")
suspend fun getByTxids(txids: List<ByteArray>): List<TransactionEntity>

/**
* The confirmation columns of the given transactions, without their
* raw bytes — what the wallet load needs for each coin's creating
* transaction. Chunked by the caller like [getByTxids].
*/
@Query("SELECT txid, context, blockHeight FROM transactions WHERE txid IN (:txids)")
suspend fun getConfirmationsByTxids(txids: List<ByteArray>): List<TransactionConfirmation>

/**
* Delete a sweep batch's losers in one statement, after every hold on
* their inputs is in place: the FK cascade takes any still-attached
Expand Down Expand Up @@ -159,3 +167,11 @@ interface TransactionDao {
}
}
}

/** The confirmation columns of one [TransactionEntity] row. */
class TransactionConfirmation(
val txid: ByteArray,
/** 0=mempool, 1=instantSend, 2=inBlock, 3=inChainLockedBlock. */
val context: Int,
val blockHeight: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,48 @@ class PlatformWalletPersistenceHandlerTest {
)
}

@Test
fun shouldCarryTheCreatingTransactionsStoredContextOnARestoredCoin() = runTest {
// A coin row can lag its own transaction's row: the transaction was
// confirmed through a record that named none of its coins, so only
// the transaction row was rewritten. The restore row carries the
// transaction's stored context and height as-is; the Rust loader
// decides what they mean for the coin.
seedWalletWithAddress(walletId, "yLagAddr")
val txid = ByteArray(32) { 31 }
handler.onChangesetBegin(walletId)
recordTransaction(
handler,
walletId, txid, ByteArray(10) { 4 }, 0, 0, ByteArray(32),
0, 0, "Standard", 0, 40_000, 0, false, "", 1_700_000_000,
ByteArray(0), 0,
)
handler.onWalletChangesetUtxoAdded(
walletId, txid, 1, 40_000, "yLagAddr", ByteArray(25) { 6 },
0, false, false, false, false,
)
handler.onChangesetEnd(walletId, success = true)

val inMempool = handler.onLoadWalletList().single().utxos.single()
assertEquals(0, inMempool.parentContext)
assertEquals(0, inMempool.parentBlockHeight)

// The block reaches the transaction row only.
handler.onChangesetBegin(walletId)
recordTransaction(
handler,
walletId, txid, ByteArray(10) { 4 }, 2, 500, ByteArray(32) { 7 },
1_700_000_500, 0, "Standard", 0, 40_000, 0, false, "", 1_700_000_000,
ByteArray(0), 0,
)
handler.onChangesetEnd(walletId, success = true)

val lagging = handler.onLoadWalletList().single().utxos.single()
assertFalse(lagging.isConfirmed)
assertEquals(2, lagging.parentContext)
assertEquals(500, lagging.parentBlockHeight)
}

@Test
fun loadWalletListRestoresUnspentUtxosAndExcludesConfirmedSpends() = runTest {
// CORE-06 regression: persisted unspent TXOs must come back on
Expand Down
140 changes: 138 additions & 2 deletions packages/rs-platform-wallet-ffi/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use platform_wallet::changeset::{
PlatformWalletChangeSet, PlatformWalletPersistence, ProviderKeyAccountEntry,
ProviderKeyExtendedPubKey, PERSISTENCE_CAPABILITIES_VERSION,
};
use platform_wallet::wallet::core::stored_coin::{reconcile_with_parent, StoredParentState};
use platform_wallet::wallet::platform_wallet::WalletId;
#[cfg(feature = "shielded")]
use platform_wallet::wallet::shielded::ShieldedActivityStatus;
Expand Down Expand Up @@ -95,6 +96,8 @@ use std::ffi::CStr;
/// `Mempool` vs no-evidence); see each match's comment.
pub(crate) const TX_CONTEXT_RAW_IN_BLOCK: u32 = 2;
pub(crate) const TX_CONTEXT_RAW_IN_CHAIN_LOCKED_BLOCK: u32 = 3;
/// The InstantSend discriminant of the same encoding.
pub(crate) const TX_CONTEXT_RAW_INSTANT_SEND: u32 = 1;

/// Byte budget for decoding a host-supplied account extended public key.
///
Expand Down Expand Up @@ -3982,7 +3985,7 @@ impl PlatformWalletPersistence for FFIPersister {

let context = match context_kind {
0 => TransactionContext::Mempool,
1 => {
k if u32::from(k) == TX_CONTEXT_RAW_INSTANT_SEND => {
// InstantSend requires the IS-lock blob, which the
// persister doesn't currently store. Treat as miss
// so the proof flow's SPV wait path completes the
Expand Down Expand Up @@ -5741,7 +5744,7 @@ fn build_wallet_start_state(
value: u.value_duffs,
script_pubkey,
};
let utxo = key_wallet::Utxo {
let mut utxo = key_wallet::Utxo {
outpoint,
txout,
address,
Expand All @@ -5755,6 +5758,15 @@ fn build_wallet_start_state(
// us). Recompute on the next SPV pass; default to false.
is_trusted: false,
};
// The coin row can lag its own transaction's row: the transaction
// was confirmed or locked through a record that named none of its
// coins, so only the transaction row was rewritten. Raise the coin
// to what its transaction's row says.
reconcile_with_parent(
&mut utxo,
stored_parent_state(u),
wallet_info.metadata.last_processed_height,
);
// Route into the target funds-bearing account. Match on the
// resolved `AccountType` and look up the right map field. Keys
// and Platform variants are intentionally no-ops.
Expand Down Expand Up @@ -6963,6 +6975,21 @@ impl UnresolvedRestoreStats {
}
}

/// What the host's row for a restored coin's creating transaction says,
/// decoded from the raw context the host copied.
fn stored_parent_state(u: &UtxoRestoreEntryFFI) -> StoredParentState {
match u.parent_context {
TX_CONTEXT_RAW_IN_BLOCK | TX_CONTEXT_RAW_IN_CHAIN_LOCKED_BLOCK => {
StoredParentState::InBlock {
height: u.parent_block_height,
}
}
TX_CONTEXT_RAW_INSTANT_SEND => StoredParentState::InstantLocked,
// Mempool, no row, or a value this build does not know: no evidence.
_ => StoredParentState::Unconfirmed,
}
}

/// Project a slice of [`UnresolvedAssetLockTxRecordFFI`] rows onto the
/// in-memory `transactions()` maps of the rebuilt `ManagedWalletInfo`,
/// routing each record to the first present family the proof lookup
Expand Down Expand Up @@ -10113,12 +10140,15 @@ mod tests {
use dashcore::blockdata::transaction::txout::TxOut;
use dashcore::blockdata::transaction::Transaction;
use dashcore::consensus::encode::serialize;
use dashcore::hashes::Hash;
use dashcore::secp256k1::Secp256k1;
use dashcore::{Network, ScriptBuf};
use dashcore::{OutPoint, Txid};
use key_wallet::account::{Account, AccountType, StandardAccountType};
use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey};
use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait;
use key_wallet::mnemonic::Mnemonic;
use key_wallet::test_utils::TestWalletContext;
use key_wallet::wallet::Wallet;

/// Regression: restored pool addresses must be tagged with the
Expand Down Expand Up @@ -10976,6 +11006,112 @@ mod tests {
assert!(restored_eddsa.is_watch_only);
}

/// A coin stored unconfirmed under a transaction the store has in a block
/// this wallet has processed is restored confirmed at that block's
/// height; one under an InstantSend-locked transaction is restored
/// locked; one under a mempool transaction, an unknown context, or a
/// block beyond this wallet's processed height is restored as stored.
#[test]
fn should_restore_a_coin_as_final_as_its_stored_transaction() {
let wallet = Wallet::from_seed_bytes(
[0x42; 64],
Network::Testnet,
key_wallet::wallet::initialization::WalletAccountCreationOptions::Default,
)
.expect("seeded wallet");
let bip44 = AccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP44Account,
};
let xpub = wallet
.accounts
.standard_bip44_accounts
.get(&0)
.expect("bip44 account")
.account_xpub;
let xpub_bytes = bincode::encode_to_vec(xpub, config::standard()).expect("encode xpub");
let specs = [build_account_spec_ffi(&bip44, &xpub_bytes)];
let script = TestWalletContext::new_random()
.receive_address
.script_pubkey();
let coin =
|txid_byte: u8, parent_context: u32, parent_block_height: u32| UtxoRestoreEntryFFI {
type_tag: specs[0].type_tag,
standard_tag: specs[0].standard_tag,
account_index: specs[0].index,
registration_index: specs[0].registration_index,
key_class: specs[0].key_class,
user_identity_id: specs[0].user_identity_id,
friend_identity_id: specs[0].friend_identity_id,
prev_txid: [txid_byte; 32],
vout: 1,
value_duffs: 1_000_000,
script_pubkey: script.as_bytes().as_ptr(),
script_pubkey_len: script.len(),
height: 0,
is_coinbase: false,
is_confirmed: false,
is_instantlocked: false,
is_locked: false,
parent_context,
parent_block_height,
};
let utxos = [
coin(1, TX_CONTEXT_RAW_IN_BLOCK, 500),
coin(2, TX_CONTEXT_RAW_INSTANT_SEND, 0),
coin(3, 0, 0),
coin(4, TX_CONTEXT_RAW_IN_CHAIN_LOCKED_BLOCK, 600),
coin(5, 7, 700),
coin(6, TX_CONTEXT_RAW_IN_BLOCK, 2_000),
];
let entry = WalletRestoreEntryFFI {
wallet_id: wallet.wallet_id,
network: Network::Testnet.into(),
accounts: specs.as_ptr(),
accounts_count: specs.len(),
utxos: utxos.as_ptr(),
utxos_count: utxos.len(),
synced_height: 1_000,
last_processed_height: 1_000,
..Default::default()
};

let (state, _) = build_wallet_start_state(&entry).expect("the wallet restores");

let account = state
.wallet_info
.accounts
.standard_bip44_accounts
.get(&0)
.expect("bip44 managed account");
let restored = |txid_byte: u8| {
account
.utxos
.get(&OutPoint {
txid: Txid::from_byte_array([txid_byte; 32]),
vout: 1,
})
.expect("the coin is restored")
};
let in_block = restored(1);
assert!(in_block.is_confirmed);
assert_eq!(in_block.height, 500);
let locked = restored(2);
assert!(locked.is_instantlocked);
assert!(!locked.is_confirmed);
let mempool = restored(3);
assert!(!mempool.is_confirmed);
assert!(!mempool.is_instantlocked);
let chain_locked = restored(4);
assert!(chain_locked.is_confirmed);
assert_eq!(chain_locked.height, 600);
let unknown_context = restored(5);
assert!(!unknown_context.is_confirmed);
assert!(!unknown_context.is_instantlocked);
let above_this_wallet = restored(6);
assert!(!above_this_wallet.is_confirmed);
}

/// The BLS xpub decoder is the only account-xpub graph with a
/// length-prefixed field (the public key bytes). A host-supplied blob
/// whose prefix claims a gigabyte must fail on the decode budget, before
Expand Down
9 changes: 9 additions & 0 deletions packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ pub struct UtxoRestoreEntryFFI {
pub is_confirmed: bool,
pub is_instantlocked: bool,
pub is_locked: bool,
/// The store's context for the coin's creating transaction, in the
/// `TransactionRecordFFI.context` encoding (`0` mempool, `1`
/// InstantSend, `2` in a block, `3` in a chain-locked block). `0` when
/// the store has no row for it. Hosts copy the row's value as-is; the
/// loader decides what it means for the coin.
pub parent_context: u32,
/// The store's block height for the coin's creating transaction; `0`
/// when it has none.
pub parent_block_height: u32,
Comment on lines +470 to +473

@thepastaclaw thepastaclaw Nov 2, 2026 •

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.

✅ Withdrawn at 8c7d8062; see the replies below.

🔴 Blocking: Negotiate the extended restore-row layout before reading it

Appending these fields changes UtxoRestoreEntryFFI's 64-bit size and array stride from 152 to 160 bytes, but on_load_wallet_list_fn and the existing manager constructors remain unchanged. A persistence host compiled against the previous header can still register and return its old allocation; build_wallet_start_state then constructs a slice with the new stride at persistence.rs:5665–5668. A single row now permits reads beyond its allocation, while subsequent rows are decoded at incorrect offsets, potentially interpreting unrelated bytes as script pointers and lengths. Updating the bundled Swift/JNI producers protects those rebuilt producers, but the documented rebuild requirement does not reject an older host before Rust dereferences its rows. This conflicts with the compatibility handling already present in PersistenceCallbacksExtension: its sidecars explicitly avoid changing bare-pointer payload strides, and manager.rs reads extension slots only after size/version negotiation. Keep the legacy restore row unchanged and transport parent evidence through a negotiated sidecar, or introduce a versioned restore interface that distinguishes the layouts before reading the allocation.

source: gpt-6.1-sol (phase2-reviewer: ffi-engineer)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed that the stride changes. I've handled it as a breaking change, not a sidecar, and the PR now says so: the title has !, and the Breaking Changes section names the 152 → 160 byte growth, the rebuild requirement, and the Kotlin UtxoRestoreData constructor change.

Why not negotiate the layout:

  • The struct's only producers are in this repo: the Swift package's PlatformWalletPersistenceHandler and the JNI crate. The Swift package ships with its xcframework, and the JNI crate is compiled into the same native library as this FFI crate. Neither can pair an old header with this Rust.
  • fix(platform-wallet)!: persist DashPay coreHeight backfill coverage so a relaunch resumes instead of rewinding again #5026 grew WalletRestoreEntryFFI the same way. That struct is read through this same on_load_wallet_list_fn array, and the PR was marked ! with the same rebuild note.
  • A sidecar would add a load callback and an outpoint lookup to both hosts to guard against a pairing the shipped artifacts cannot produce.

If a maintainer prefers the sidecar shape here (as #4799 did for the identity rows), I'll switch to it.

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.

You're right: I treated cross-version binary compatibility as a requirement without establishing that it is supported here. I checked that Swift allocates these rows using the header from its XCFramework, JNI constructs them using its direct Rust dependency, and #5026 likewise extended WalletRestoreEntryFFI. The mixed-version external-host case I described remains unsafe, but the documented rebuild requirement explicitly excludes that pairing; it does not demonstrate a defect in the matched producers shipped here. I withdraw the blocker—layout negotiation is not required merely because this deliberately breaks the old ABI.

Withdrawing this finding.

}

/// One persisted transaction record carried back at load time so the
Expand Down
1 change: 1 addition & 0 deletions packages/rs-platform-wallet/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod generation;
// Inherent `CoreWallet::sign_message` only — no types to re-export.
mod sign_message;
pub mod spend_observer;
pub mod stored_coin;
pub(crate) use sign_message::is_signable_funding_account;
mod transaction;
pub mod wallet;
Expand Down
Loading
Loading