Skip to content

consensus: pass spent KIs to cuprated#605

Open
redsh4de wants to merge 1 commit into
Cuprate:mainfrom
redsh4de:feat/ki-map-reuse
Open

consensus: pass spent KIs to cuprated#605
redsh4de wants to merge 1 commit into
Cuprate:mainfrom
redsh4de:feat/ki-map-reuse

Conversation

@redsh4de
Copy link
Copy Markdown
Contributor

@redsh4de redsh4de commented May 1, 2026

What

Addresses the FIXME in add_valid_block_to_main_chain.

Why

Block verification in consensus walks every transaction's inputs to dedupe and check key images. After verification, cuprated walked them a second time to notify the txpool.

This makes it so that the KI vec is returned from consensus, eliminating the second walk.

Where

consensus, helper, cuprated

How

In helper:

  • New tx_key_images() helper for extracting a transactions key images

In consensus:

  • Batch preparation now returns TxsWithKis per block (transactions + validated key images).
  • The verify functions now return VerifiedBlock (wrapper for verified block info + the spent key images).
  • verify_prepped_main_chain_block takes a PreparedBlockTxs::{Checked, Unchecked} enum so the caller can say whether key images still need checking:
    • Unchecked(Vec<TransactionVerificationData>) - single-block path. Extracts KIs and checks.
    • Checked(TxsWithKis) - batch-sync path. TxsWithKis is only produced by batch_prepare_main_chain_blocks where validation was already done, so we skip checking here and use the passed KIs as is.
  • Split FullVerification into two methods, so the txpool path doesn't allocate a key-image Vec it would discard:
    • verify (txpool): returns just the transactions.
    • verify_with_kis (block, internal): returns transactions and key images.
  • Key-image checking seperated into three:
    • check_kis: used by the batch & txpool paths. Takes pre-computed key images.
    • extract_and_check_kis: single-block path. Walks inputs once and builds both the KI dedup set and Vec in one pass.
    • check_kis_unspent: shared chain-spent db query, skipped on a empty set.
  • key_images_spent_checked bool removed from BatchPrepareCache. The existence of TxsWithKis means that the key images were checked, so this becomes redundant.
  • Capacity hint change: check_kis_unique's old with_capacity(size_hint().1.unwrap_or(0) * 2) always resolved to zero for the batch caller, because when flat_map is used on a slice iter, the upper-bound size hint is always None. To avoid rehashing, we count the KIs once up front and pass capacity as a param.
  • New alt_block_to_verified_block replaces cuprated's alt_block_to_verified_block_information. Computes total_fees and spent_key_images in one pass, then constructs a VerifiedBlock.

In cuprated:

  • add_valid_block_to_main_chain splits the new VerifiedBlock into block information and key images, used like before.
  • The reorg-reverse path now calls the new alt_block_to_verified_block in consensus; the local helper is removed.

@github-actions github-actions Bot added A-dependency Area: Related to dependencies, or changes to a Cargo.{toml,lock} file. A-consensus Area: Related to consensus. A-helper Area: Related to cuprate-helper. A-binaries Area: Related to binaries. labels May 1, 2026
@redsh4de redsh4de force-pushed the feat/ki-map-reuse branch from c0c79de to 4b96430 Compare May 5, 2026 20:32
@redsh4de redsh4de force-pushed the feat/ki-map-reuse branch from 4b96430 to 6251608 Compare May 10, 2026 20:37
@redsh4de
Copy link
Copy Markdown
Contributor Author

redsh4de commented May 13, 2026

Diagram of how key images flow and where checks happen on each path in this PR.
It can be seen that with this, we have a guarantee that we will only extract key images once at the earliest possible step for each path, and get them passed down to the block handler

image

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

Labels

A-binaries Area: Related to binaries. A-consensus Area: Related to consensus. A-dependency Area: Related to dependencies, or changes to a Cargo.{toml,lock} file. A-helper Area: Related to cuprate-helper.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant