Skip to content

fix(core): bound P2P wallet RPC reads to committed chain snapshots - #8366

Open
aswadi wants to merge 2 commits into
tari-project:developmentfrom
aswadi:fix/wallet-rpc-uncommitted-blocks-and-reorg-reads
Open

aswadi wants to merge 2 commits into
tari-project:developmentfrom
aswadi:fix/wallet-rpc-uncommitted-blocks-and-reorg-reads

Conversation

@aswadi

@aswadi aswadi commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Bounds P2P BaseNodeWalletRpcService reads (sync_utxos_by_block, get_header, get_header_by_height, fetch_kernel, fetch_matching_utxos, utxo_query, and query_deleted) to committed chain snapshots in chain_storage and fails closed with RpcStatus::conflict when horizon sync is rebuilding committed output state (Note: only the P2P BaseNodeWalletRpcService is covered here; HTTP query_service.rs paths can follow in a separate PR):

  1. Shared NotServed mapping:
    • Adds NotServed::OutputRebuild and makes not_served_status, REORG_DETECTED, END_BODY_NOT_STORED, and OUTPUT_REBUILD_CONFLICT_MSG pub(crate) in base_node::sync::rpc::service so SyncUtxosByBlockTask shares the single mapping.
  2. Consolidated chain_storage output readers:
    • Shares fetch_outputs_with_spend_status_up_to between fetch_outputs_with_spend_status_at_tip and fetch_committed_unspent_outputs (checking smt_hash(mined_height) against the JMT leaf value) and removes the unused fetch_outputs_mined_info and fetch_latest_mined_infos wrappers.
    • Replaces fetch_outputs_in_block_and_header_pair with fetch_outputs_in_block_on_chain and its unit test fetch_outputs_in_block_on_chain_checks_chain_tip_and_output_rebuild.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; both previously reported visibility errors are fixed, and no new blocking issue was found.

Summary

Bounds P2P wallet reads to committed chain state.

  • Wallet transaction and header queries only show committed chain data.
  • Output queries return committed results or a conflict during rebuild.
  • Block UTXO streams stop at committed chain data.

No new actionable issues were found. Tests were inspected but not run.

Reviews (4) · Last reviewed commit: "fix(core): bound P2P wallet RPC reads to..." · Reviewed by Greptile

@aswadi
aswadi requested a review from a team as a code owner November 14, 2026 13:10

@SWvheerden SWvheerden left a comment

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.

Thanks for this. The body-tip bound on sync_utxos_by_block is a real gap on development, and so is the horizon-sync output rebuild window. Your follow-up commit does handle the rebuild window Greptile raised: we traced every horizon-sync write phase (the reset to genesis, the mid-tranche batch commits, checkpointed tranches, the final tranche through to finalize_horizon_sync), and committed_outputs_are_consistent fails closed in each of them.

Part of this PR is already on development, though, and the branch no longer merges. We'd like the following changes before we can merge it.

Required

1. Rebase onto development and drop the reorg check that is already there.
Both files now conflict.

  • #8352 already reads the outputs and both chain headers under one read lock in fetch_block_on_requested_chain, so the mid-stream reorg check here duplicates it.
  • #8400 moved is_on_requested_chain into chain_storage and added read_on_requested_chain / NotServed as the shared per-block check for streamed block data. The import from base_node::sync::rpc no longer exists.

2. Build the sync_utxos_by_block body-tip bound on #8400's helper, not a second copy of it.
sync_utxos_by_block_task.rs:247-290 builds the "end height <= best block height" check and the chain check by hand, inside a spawn_blocking in the RPC layer. Instead:

  • Add BlockchainDatabase::fetch_outputs_in_block_on_chain(height, hash, end_height, end_hash) next to fetch_txos_in_block_on_chain / fetch_kernels_in_block_on_chain, built on read_on_requested_chain. It then gets the same single-guard read and the same body-tip bound.
  • Map NotServed the way the sync RPC does. BodyNotStored becomes Conflict with the "end block's body is not stored" text, logged at debug. NotOnChain becomes the reorg Conflict, logged at warn. At the moment a header-only end block is reported as "no longer an ancestor" at warn, so any peer can trigger that warning whenever it likes.
  • The output-rebuild check (committed_outputs_are_consistent) can run inside the same guard in that chain_storage function, either as an extra NotServed reason or as a separate check. Your call.
  • fetch_outputs_in_block_and_header_pair has no callers after this, so please remove it.
  • In validate, a start or end header that is stored but above the best block currently returns not_found("... was not found") (sync_utxos_by_block_task.rs:124-128). Please return the same body-not-stored Conflict there, so clients can tell "unknown hash" apart from "not served yet".

3. Drop the submit_transaction change and keep development's classify_spent_rejection (#8405) as it is.
service.rs:478-493 now answers DoubleSpend when the mempool says the kernel is already mined, but the kernel's block is above the committed tip. Two things make that a problem:

  • The mempool's duplicate-kernel check does not filter by tip (aggregate_body_chain_validator.rs:234-247).
  • After a failed horizon sync, a node that has bootstrapped before reports is_synced = true again (listening.rs:660-670) while the staged kernels are still stored.

So a wallet that rebroadcasts a transaction which really was mined gets DoubleSpend with is_synced = true, and transaction_broadcast_protocol.rs:262-312 cancels it. The old answer, AlreadyMined, was the right one here: the kernel exists. There is also no test for this path.

4. Put the committed-snapshot reads in chain_storage, not the RPC service.
with_read_db, fetch_committed_unspent_outputs, fetch_committed_outputs_mined_info, query_committed_deleted and the related helpers reach into db.inner().db_read_access() from service.rs. They also re-implement fetch_outputs_with_spend_status_at_tip (including the JMT lookup) and fetch_latest_mined_infos.

  • Please add the tip bound and the rebuild check to BlockchainDatabase methods instead, either by changing those existing methods or by adding siblings next to them.
  • The RPC handlers then keep calling AsyncBlockchainDb, the same way #8400 handled the sync RPC.
  • This also lets the HTTP wallet query service (query_service.rs) reuse the same methods later. The in-tree wallet now scans and validates over HTTP, which reads the same tables without these bounds. That is out of scope for this PR, but it's a reason to keep the logic in one place.

5. Remove changes the fix does not need.
To keep the diff reviewable, please drop the unrelated refactors:

  • the is_synced() helper and the tx_location_from_mempool split;
  • the bad_request(&"...".to_string()) → bad_request("...") cleanups;
  • the utxo.output.try_into()? rewrite in utxo_query.

Each of these is fine as a separate cleanup PR.

Smaller points

  • fetch_committed_header_by_hash re-hashes the header it just looked up by hash (header.hash() != *hash), once per output in utxo_query / query_deleted. The index is keyed by that hash, so the height check is enough.
  • In fetch_latest_committed_output and sanitize_committed_mined_info, info.mined_height <= tip_height repeats the height check that fetch_committed_header_by_hash already does.

Checked

  • cargo +nightly-2026-06-01 fmt --check is clean. (The current default nightly only flags files this PR doesn't touch.)
  • cargo lints clippy --all-targets --all-features -p tari_core shows nothing in the touched files.
  • Decoder parity check passes.
  • cargo test -p tari_core --lib base_node::rpc -- --test-threads=1 passes 30/30. All of these were run at the PR head, because it doesn't merge onto development.
  • There is no consensus, DB or proto change. Conflict and NotFound already exist for released clients.

Please keep the new tests after the rebase: the header-above-tip, rewind-then-resync, staged outputs/kernels and rebuild cases are all worth having.

@aswadi aswadi closed this Nov 21, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from e575517 to 8cfb3c1 Compare November 21, 2026 15:06
@aswadi

aswadi commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review @SWvheerden! I've rebased onto current development and updated the PR to address every point:

  1. Moved all committed-snapshot and output-rebuild checks into chain_storage (BlockchainDatabase & AsyncBlockchainDb):
    • Added BlockchainDatabase::fetch_outputs_in_block_on_chain built directly on read_on_requested_chain and committed_outputs_are_consistent, and removed the unused fetch_outputs_in_block_and_header_pair.
    • Added validate_sync_utxos_by_block_range, fetch_committed_header, fetch_committed_kernel_info, fetch_committed_unspent_outputs, fetch_committed_outputs_mined_info, and fetch_committed_latest_mined_infos on BlockchainDatabase and exposed them via AsyncBlockchainDb.
    • Removed the redundant hash re-verification in fetch_committed_header_by_hash and redundant height checks in fetch_latest_committed_output / sanitize_committed_mined_info.
  2. Mapped NotServed consistently in sync_utxos_by_block:
    • Mapped NotServed::BodyNotStored to RpcStatus::conflict(END_BODY_NOT_STORED) (logged at debug! during streaming and also returned from validate when a start/end header is stored above the best block).
    • Mapped NotServed::NotOnChain to the reorg Conflict (logged at warn!) and NotServed::OutputRebuild to the output-rebuild Conflict.
  3. Kept development's submit_transaction / classify_spent_rejection and removed unrelated refactors:
    • Dropped the submit_transaction changes, is_synced() / tx_location_from_mempool helpers, and unrelated formatting cleanups in base_node/rpc/service.rs, while retaining all the new unit tests.

@aswadi aswadi reopened this Nov 21, 2026
@aswadi aswadi closed this Nov 23, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 0032117 to c644942 Compare November 23, 2026 08:01
@aswadi aswadi reopened this Nov 23, 2026

@SWvheerden SWvheerden left a comment

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.

Thanks for the rework. Round 1 is addressed. The submit_transaction DoubleSpend change and the unrelated refactors are gone. fetch_outputs_in_block_on_chain is now built on read_on_requested_chain, and BodyNotStored maps to END_BODY_NOT_STORED with a debug log. The committed-read helpers live in BlockchainDatabase and each runs under one read lock. The horizon-rebuild gate (committed_outputs_are_consistent) fails closed at every stage. The logic looks right, but the branch does not build, so it cannot go in as it is.

1. tari_core does not compile: the NotServed match is not exhaustive

base_layer/core/src/base_node/sync/rpc/service.rs:144: not_served_status matches only NotOnChain and BodyNotStored. Adding NotServed::OutputRebuild makes this a hard error (E0004). Greptile flagged this on 2026-11-21 and it is still there at 5a7d77c. Confirmed with cargo check -p tari_core --all-features on this head merged onto current development.

Please fix this by putting the mapping in one place instead of adding a fourth hand-written copy:

  • Make not_served_status and its message constants pub(crate) in one module, for example next to NotServed, and add the OutputRebuild => OUTPUT_REBUILD_CONFLICT_MSG arm there.
  • sync_utxos_by_block_task.rs:24-27 re-declares REORG_DETECTED and END_BODY_NOT_STORED with the same text. is_reorg compares the text exactly, so the two copies would drift silently. Delete the copies and call the shared not_served_status from validate (:61-67) and the stream loop (:136-153). Keep only the extra debug/warn logging locally.

2. Lib tests still call the removed fetch_outputs_in_block_and_header_pair

base_layer/core/src/chain_storage/blockchain_database.rs:7231 and :7237 (fetch_outputs_in_block_and_header_pair_reads_the_block_and_both_headers) still call the removed method, so the test target does not compile (E0599, also flagged by Greptile). Please replace it with a test of fetch_outputs_in_block_on_chain that covers:

  • a block on chain returns its outputs;
  • an end height above the tip returns BodyNotStored;
  • an active checkpoint or root mismatch returns OutputRebuild.

3. rustfmt

cargo +nightly-2026-06-01 fmt --all -- --check fails in:

  • base_node/rpc/service.rs:670 (test imports);
  • chain_storage/async_db.rs:62 (import order);
  • chain_storage/blockchain_database.rs:1748 (match arm in fetch_committed_kernel_info).

4. Old output-read functions are now dead and duplicated by the new ones

  • BlockchainDatabase::fetch_outputs_mined_info (blockchain_database.rs:1550) and fetch_latest_mined_infos (:1567), plus their async_db.rs:207-218 wrappers, have no callers left. Please delete them.
  • fetch_committed_unspent_outputs copies fetch_outputs_with_spend_status_at_tip (:1502): the same "latest by mined_height" pick and the same JMT lookup, with a tip filter added. fetch_outputs_with_spend_status_at_tip is still used by query_service.rs:654 and inbound_handlers.rs:416. That leaves two copies of one logic, one guarded and one not. Please have both use one helper that takes tip_height (the old function can pass u64::MAX or the tip), so a later fix lands in one place.

This is the Round 1 ask to move the reads into chain storage without duplicating the existing readers.

Once 1-3 are fixed, please run cargo lints clippy --all-targets --all-features -p tari_core and the base_node::rpc / chain_storage lib tests. I could not run them here because the crate does not compile.

Non-blocking:

  • fetch_committed_unspent_outputs treats an output as unspent if its commitment key is in the JMT. It never checks that the leaf belongs to this output. If output H1 (commitment C) is spent and a later H2 reuses C (consensus only rejects duplicates in the unspent set), H1 reads as unspent. This bug predates the PR, but the function is being rewritten. Comparing the stored value with output.smt_hash(mined_height) in the shared helper from item 4 would fix it on every path.
  • transaction_batch_query still reads the batch-level metadata in a separate snapshot from the per-kernel fetch_committed_kernel_info calls. It is cosmetic, because the wallet uses its own tip, but a batched committed read would finish the job.
  • committed_outputs_are_consistent returns false silently when the root does not match and no checkpoint is set. A warn! in that case would make a stuck node visible instead of a quiet stream of Conflict replies.
  • The new pub type aliases and fetch_committed_* functions can be pub(crate).
  • The HTTP query_service.rs paths are still unguarded. That is fine as a follow-up, but please say in the PR description that only the P2P BaseNodeWalletRpcService is covered.
  • Clash: your #8364 extends fetch_outputs_in_block_and_header_pair to a 4-tuple, and this PR deletes that function. The two conflict in sync_utxos_by_block_task.rs and async_db.rs. Whichever merges second has to be rebased. If #8364 lands first, its best-height bound should move onto fetch_outputs_in_block_on_chain / read_on_requested_chain, which already bounds the body tip.

@aswadi aswadi closed this Nov 24, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 5a7d77c to 4ca1c87 Compare November 24, 2026 13:49
@aswadi

aswadi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @SWvheerden! Fixed all 4 blocking items and the non-blocking items:

  1. Exhaustive NotServed match & shared not_served_status: Added NotServed::OutputRebuild => RpcStatus::conflict(OUTPUT_REBUILD_CONFLICT_MSG) to not_served_status in base_layer/core/src/base_node/sync/rpc/service.rs (made pub(crate) along with the message constants) and removed the duplicate constant copies in sync_utxos_by_block_task.rs.
  2. Replaced removed fetch_outputs_in_block_and_header_pair test: Replaced fetch_outputs_in_block_and_header_pair_reads_the_block_and_both_headers in blockchain_database.rs with fetch_outputs_in_block_on_chain_checks_chain_tip_and_output_rebuild covering a block on chain, an end height above the tip (BodyNotStored), and an output root mismatch (OutputRebuild).
  3. rustfmt: Formatted test imports in base_node/rpc/service.rs, alphabetical imports in chain_storage/async_db.rs, and the match arm in fetch_committed_kernel_info.
  4. Consolidated output readers & removed dead code: Removed the unused fetch_outputs_mined_info and fetch_latest_mined_infos functions (and their async_db.rs wrappers), shared fetch_outputs_with_spend_status_up_to between fetch_outputs_with_spend_status_at_tip and fetch_committed_unspent_outputs (comparing the JMT leaf value against output.smt_hash(mined_height)), added a warn! on output root mismatch in committed_outputs_are_consistent, and changed the new type aliases and methods to pub(crate).

@aswadi aswadi reopened this Nov 24, 2026
@aswadi aswadi closed this Nov 25, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from b96747e to d48066d Compare November 25, 2026 11:18
@aswadi aswadi reopened this Nov 25, 2026
@aswadi aswadi closed this Nov 25, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 5ba047c to d48066d Compare November 25, 2026 11:25
@aswadi aswadi reopened this Nov 25, 2026
@aswadi aswadi closed this Nov 25, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 80c8a0f to d48066d Compare November 25, 2026 11:27
@aswadi aswadi reopened this Nov 25, 2026

@SWvheerden SWvheerden left a comment

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.

Thanks for working through the last round. Most of the earlier points are fixed: not_served_status now covers every case and the duplicate constants are gone, the stale test is replaced, the dead fetch_outputs_mined_info / fetch_latest_mined_infos are removed, and spend status now shares one helper that compares the JMT leaf value. The design looks right. Each fetch_committed_* reads under one lock, and the rebuild gate fails closed in every horizon-sync phase.

The new head still does not build, though, so this needs another round.

Required

  1. It does not compile (cargo check -p tari_core --all-targets --all-features). There are five errors:

    • chain_storage/mod.rs:46-47: E0365. pub use re-exports CommittedOutputsMinedInfo / CommittedQueryDeletedResult, but both are declared pub(crate). Move them to a pub(crate) use line.
    • base_node/rpc/service.rs:33, :681 and rpc/sync_utxos_by_block_task.rs:18: E0603. base_node::sync::rpc::service is a private module (mod service; in sync/rpc/mod.rs). Add pub(crate) use service::{not_served_status, END_BODY_NOT_STORED, OUTPUT_REBUILD_CONFLICT_MSG, REORG_DETECTED}; in sync/rpc/mod.rs and import them from crate::base_node::sync::rpc.
    • chain_storage/blockchain_database.rs:3326: E0308. smt.get(..) returns Option<Vec<u8>>, so .as_ref() != Some(expected_value.as_slice()) compares Option<&Vec<u8>> with Option<&[u8]>. Use .as_deref().

    Please run cargo check --all-targets and the chain_storage / base_node::rpc tests before pushing. Greptile flagged two of these errors on the previous head.

  2. rustfmt (CI toolchain nightly-2026-06-01) still fails at base_node/rpc/service.rs:676, in the test-module use crate::{base_node::{...}} block.

  3. transaction_batch_query still mixes snapshots (rpc/service.rs:326-351). The batch-level best_block_hash / best_block_height / tip_mined_timestamp come from a separate get_chain_metadata(). Each signature then takes its own lock through fetch_kernel → fetch_committed_kernel_info. A block or reorg between those reads can return an entry mined above the reported tip, or confirmations counted against a different tip. This PR's goal is one committed snapshot per wallet query. Please add one batched BlockchainDatabase method that reads the metadata once and resolves every kernel against that tip under a single db_read_access(), the same way fetch_committed_latest_mined_infos does. Use its metadata for the batch fields and for each entry's confirmations.

  4. Test the new spent check. fetch_outputs_with_spend_status_up_to now reports spent when the leaf differs from smt_hash(mined_height), not only when the key is missing. This is shared code: the HTTP get_utxo (query_service.rs:654) and the inbound FetchMatchingUtxos handler (inbound_handlers.rs:416) call it too. No test fails if the check is reverted, or if it is broken so that live outputs read as spent. One chain_storage test is enough: mine output X, spend it, then mine Y with the same commitment at a higher height. Assert that X reads as spent and Y as unspent.

Should fix

  • The root-mismatch warn! in committed_outputs_are_consistent (blockchain_database.rs:3270) runs on every wallet RPC call. After a failed horizon-sync attempt, discard_synced_outputs (synchronizer.rs:630-637) clears the checkpoint and resets outputs to genesis while the tip stays put. The mismatch then lasts until the next attempt passes its first tranche, so any peer can loop utxo_query / query_deleted / sync_utxos_by_block and add one WARN line per request. That is my fault, since I asked for the warning last round. Please log it at debug! on these read paths, or only when the (best hash, root) pair changes. Also move sync_utxos_by_block_task.rs:141 to debug!, since the peer already gets the Conflict.

Merge order

This conflicts with #8364 (same author, approved) in sync_utxos_by_block_task.rs, async_db.rs and blockchain_database.rs. #8364 adds a fourth return value to fetch_outputs_in_block_and_header_pair, and two new callers of it in base_node_grpc_server.rs and inbound_handlers.rs. This PR deletes that function. Once #8364 merges, please rebase and keep the function for those callers; only sync_utxos_by_block should move to fetch_outputs_in_block_on_chain. There is also a small textual conflict with #8365 next to the constants in sync/rpc/service.rs.

Follow-up (not for this PR)

The HTTP wallet query service (query_service.rs: fetch_kernel, get_utxos_mined_info, get_utxos_deleted_info, get_utxo, get_header_by_height) still reads without the committed-tip bound or the rebuild gate. The in-tree wallet uses that path, so please open a follow-up that routes it through the new fetch_committed_* helpers.

@aswadi aswadi closed this Nov 25, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 7b47d68 to e3526cf Compare November 25, 2026 13:35
@aswadi aswadi reopened this Nov 25, 2026
@aswadi

aswadi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @SWvheerden! Rebased onto current development (e3526cf23e, keeping fetch_outputs_in_block_and_header_pair and its test from #8364) and pushed da8c0d8db6 addressing all items:

  1. Compilation fixes:
    • Moved CommittedKernelsInfo, CommittedOutputsMinedInfo, and CommittedQueryDeletedResult to a pub(crate) use blockchain_database::{...}; block in chain_storage/mod.rs.
    • Re-exported OUTPUT_REBUILD_CONFLICT_MSG, not_served_status, and #[cfg(test)] END_BODY_NOT_STORED from base_node/sync/rpc/mod.rs and imported them via crate::base_node::sync::rpc.
    • Switched .as_ref() != Some(expected_value.as_slice()) to .as_deref() != Some(expected_value.as_slice()) in fetch_outputs_with_spend_status_up_to.
  2. rustfmt: Moved the test-only END_BODY_NOT_STORED import into mod sync_utxos_by_block where it is used and formatted all imports in base_node/rpc/service.rs and async_db.rs within 120 columns.
  3. Single-snapshot transaction_batch_query: Added BlockchainDatabase::fetch_committed_kernels_info (and its AsyncBlockchainDb wrapper) which reads ChainMetadata once and resolves all requested kernel signatures against that tip under a single db_read_access(). transaction_batch_query now uses that single snapshot for both the batch metadata fields and each entry's confirmations.
  4. JMT leaf value test: Added fetch_outputs_with_spend_status_at_tip_checks_smt_leaf_value_when_commitment_is_reused in blockchain_database.rs verifying that when output X is spent and output Y with the same commitment is mined at a higher height, X reads as spent (true) and Y reads as unspent (false).
  5. Log levels: Changed the root-mismatch log in committed_outputs_are_consistent and the NotServed::OutputRebuild log in sync_utxos_by_block_task.rs from warn! to debug!.

@SWvheerden SWvheerden left a comment

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.

Thanks for the update. Every point from the last round is addressed: it compiles now, transaction_batch_query reads one snapshot through fetch_committed_kernels_info, the rebuild log is at debug!, and fetch_outputs_in_block_and_header_pair is kept for #8364. The design is still right.

But the new spent check reports every unspent output as spent, and two of this PR's own tests fail because of it. CI would also fail on clippy and rustfmt.

Required

  1. The leaf-value spent check marks every live output as spent (chain_storage/blockchain_database.rs:3360-3365, fetch_outputs_with_spend_status_up_to).
    The JMT value table does not hold the raw smt_hash. LmdbTreeWriter::write_node_batch (lmdb_db/lmdb_tree_writer.rs:103-105) stores bincode::serialize(value) of the Option<Vec<u8>>, and lmdb_insert serialises that again. get_value_option (lmdb_tree_reader.rs:67) decodes only once. So smt.get(..) returns the bincode Option envelope (tag + length + 32 bytes), which never equals Some(expected_value.as_slice()), and spent is always true.
    Effect: fetch_matching_utxos returns no outputs, and the shared fetch_outputs_with_spend_status_at_tip reports every output as spent to its other callers (inbound FetchMatchingUtxos / gRPC). cargo test -p tari_core --lib fails on:

    • chain_storage::blockchain_database::test::fetch_outputs_with_spend_status_at_tip_checks_smt_leaf_value_when_commitment_is_reused (:7289: the unspent X comes back true)
    • base_node::rpc::service::test::wallet_utxo_queries_ignore_uncommitted_horizon_staged_outputs (rpc/service.rs:1027: 0 outputs instead of 1)

    Please compare against the decoded value: decode the returned bytes as Option<Vec<u8>> (or compare with the bincode encoding of Some(smt_hash)), and keep the change on the read side. Do not change the writer, because that changes the on-disk format of existing databases. Do not fall back to .is_none() either, since that brings back the commitment-reuse misreport the new test covers.

  2. clippy fails (cargo lints clippy --all-targets --all-features -p tari_core): clippy::too_many_lines (103/100) on SyncUtxosByBlockTask::start_streaming (rpc/sync_utxos_by_block_task.rs:90). The header lookup that used to live in fetch_block_on_requested_chain is now inlined. Move it back into a small helper (for example fetch_header_at(height, &start_header)) rather than adding an allow.

  3. rustfmt (CI toolchain nightly-2026-06-01) still fails at:

    • chain_storage/mod.rs:63-67: the pub(crate) use blockchain_database::{...} fits on one line.
    • chain_storage/blockchain_database.rs:7288: let initial = db.fetch_outputs_with_spend_status_at_tip(...) needs to be a method chain.

Please run cargo +nightly-2026-06-01 fmt --all, cargo lints clippy --all-targets --all-features -p tari_core, and cargo test -p tari_core --lib -- base_node::rpc chain_storage::blockchain_database before pushing.

Should fix

  • Duplicated kernel lookup. fetch_committed_kernel_info (blockchain_database.rs:1691-1706) is a line-for-line copy of fetch_committed_kernels_info for one signature, with its own async wrapper (async_db.rs:313-317). Remove it and have fetch_kernel call fetch_committed_kernels_info(vec![signature]), so the committed-kernel rule lives in one place.
  • Test the dropped uncommitted spend in query_deleted. sanitize_committed_mined_info (blockchain_database.rs:3377-3396) now drops an input mined above the tip, but the staged fixture only stages outputs. Please add an input at the synced-ahead block that spends one of B's outputs, and assert query_deleted (with chain_must_include_header = B) returns height_deleted_at == 0 and an empty block_deleted_in.

Merge order

#8365 is approved and conflicts with this PR in base_node/sync/rpc/service.rs, next to the constants. If it merges first, please rebase on top of it.

@aswadi

aswadi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @SWvheerden! Pushed 059b783e2e addressing all 5 items:

  1. Decode bincode Option<Vec<u8>> JMT leaf value: Updated fetch_outputs_with_spend_status_up_to to decode the bytes returned by smt.get(smt_key, current_version) via bincode::deserialize::<Option<Vec<u8>>>(&bytes) before comparing .as_deref() != Some(expected_value.as_slice()).
  2. clippy::too_many_lines on SyncUtxosByBlockTask::start_streaming: Extracted SyncUtxosByBlockTask::fetch_header_at in sync_utxos_by_block_task.rs.
  3. rustfmt: Collapsed pub(crate) use blockchain_database::{CommittedKernelsInfo, CommittedOutputsMinedInfo, CommittedQueryDeletedResult}; onto one line in chain_storage/mod.rs and formatted let initial = db.fetch_outputs_with_spend_status_at_tip(...) as a multi-line method chain in blockchain_database.rs.
  4. Deduplicated kernel lookup: Removed fetch_committed_kernel_info (and its AsyncBlockchainDb wrapper) and routed fetch_kernel through fetch_committed_kernels_info(vec![signature.clone()]).
  5. Staged uncommitted input test in query_deleted: Updated setup_staged_horizon_fixture so synced-ahead block C (Add start of crypto module as POC #3) stages an input spending B's output while committed tip and JMT state remain at B (Scaffold initial directory structure #2), and asserted in wallet_utxo_queries_ignore_uncommitted_horizon_staged_outputs that query_deleted (with chain_must_include_header = B) reports height_deleted_at == 0 and an empty block_deleted_in.

@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 04c5bf7 to 71cec6d Compare November 26, 2026 13:35
@aswadi aswadi closed this Nov 26, 2026
@aswadi
aswadi force-pushed the fix/wallet-rpc-uncommitted-blocks-and-reorg-reads branch from 71cec6d to ff58cd1 Compare November 26, 2026 14:35

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants