Skip to content

Parallelize block input script checks for performance improvement - #7

Merged
metaphorics merged 1 commit into
mainfrom
perf/flat-block-script-checks
Jul 26, 2026
Merged

Parallelize block input script checks for performance improvement#7
metaphorics merged 1 commit into
mainfrom
perf/flat-block-script-checks

Conversation

@metaphorics

Copy link
Copy Markdown
Contributor

No description provided.

Flatten full block verification into ordered per-input Rayon work while preserving kernel authority and transaction/input error precedence.

The paired 0..150k kernel/Fjall replay improved total wall time by 1.148x and script verification by 1.42x with the canonical stop hash. A final current-tree replay completed in 183.84s.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added block-level input script verification with deterministic results, including consistent error ordering across parallel execution.
    • Added support for validating same-block transaction spends in the correct order.
    • Exposed block input script verification through the consensus API.
  • Bug Fixes

    • Improved reporting for invalid transaction scripts, prevout mismatches, duplicate inputs, and malformed transaction data.
    • Ensured earlier block transaction failures are reported before later failures consistently.

Walkthrough

Changes

Consensus block verification

Layer / File(s) Summary
Transaction and kernel preparation
crates/consensus/src/kernel.rs, crates/consensus/src/verify_tx.rs, crates/consensus/Cargo.toml
Transaction checks now prepare prevouts and totals before scripts; kernel verification reuses prepared transaction data for each input.
Deterministic block script pipeline
crates/consensus/src/verify_tx.rs, crates/consensus/src/lib.rs
Block inputs are prepared in order, verified in parallel, and scanned deterministically for phase-ordered failures.
Node block verification integration
crates/node/src/apply.rs
Node block application resolves ordered prevout rows and delegates script checks to the consensus block verifier, including same-block spend coverage.

Sequence Diagram(s)

sequenceDiagram
  participant Node as Node block application
  participant Consensus as verify_block_input_scripts
  participant Rayon as Rayon workers
  participant UTXO as UTXO views
  Node->>UTXO: Resolve transaction prevouts in block order
  UTXO-->>Node: Owned prevout matrix
  Node->>Consensus: Verify block input scripts
  Consensus->>Rayon: Run input script checks in parallel
  Rayon-->>Consensus: Return script results
  Consensus-->>Node: Return first deterministic failure or success
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive There is no real description, just an empty placeholder, so it provides no meaningful change summary. Add a brief description of the actual code changes and their purpose, especially the new parallel block script verification flow.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: parallelizing block input script checks for speed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch perf/flat-block-script-checks

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
crates/consensus/src/verify_tx.rs (1)

513-517: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

You cache the serialization in prep, then clone the whole thing per input. That's not caching, that's laundering an allocation.

prep.serialized is computed exactly once in prepare_block_input_checks (Line 467) so the transaction bytes are paid for one time. Then here, in the parallel per-input closure, prep.serialized.clone() deep-copies the entire serialized transaction for every input. A transaction with K inputs now allocates and memcpy's the full tx K times — on the single hottest path in the whole node. The single-tx path (Lines 174-177) got this right by threading one &mut Option<Vec<u8>> across inputs; the parallel path regressed it.

Share the bytes instead of copying them: make PreparedTx::serialized an Arc<Vec<u8>> and clone the Arc (a refcount bump), or hand verify_input_script_portable an Option<&[u8]> read-only view.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/consensus/src/verify_tx.rs` around lines 513 - 517, Update the
parallel per-input verification path around PreparedTx::serialized and
verify_input_script_portable to avoid deep-copying the serialized transaction
for each input. Share the cached bytes by changing PreparedTx::serialized to an
Arc<Vec<u8>> and cloning only the Arc, or pass an Option<&[u8]> read-only view
while preserving the existing non-bitcoinconsensus behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/consensus/src/verify_tx.rs`:
- Around line 513-517: Update the parallel per-input verification path around
PreparedTx::serialized and verify_input_script_portable to avoid deep-copying
the serialized transaction for each input. Share the cached bytes by changing
PreparedTx::serialized to an Arc<Vec<u8>> and cloning only the Arc, or pass an
Option<&[u8]> read-only view while preserving the existing non-bitcoinconsensus
behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: QUIET

Plan: Pro Plus

Run ID: 9fffb1a3-01d7-4b67-9108-d04218720118

📥 Commits

Reviewing files that changed from the base of the PR and between 369d8c0 and c0cee0c.

📒 Files selected for processing (5)
  • crates/consensus/Cargo.toml
  • crates/consensus/src/kernel.rs
  • crates/consensus/src/lib.rs
  • crates/consensus/src/verify_tx.rs
  • crates/node/src/apply.rs
📜 Review details
⏰ Context from checks skipped due to timeout. (4)
  • GitHub Check: kernel-node
  • GitHub Check: bench-smoke
  • GitHub Check: test
  • GitHub Check: clippy
🔇 Additional comments (9)
crates/consensus/Cargo.toml (1)

33-33: LGTM!

crates/consensus/src/verify_tx.rs (4)

150-182: LGTM!

Also applies to: 184-253, 255-280, 282-317


339-489: LGTM!


1178-1405: LGTM!


503-510: 🩺 Stability & Availability

No change needed. PreparedKernelTx is only par_iter-shared in Rust, and the shared-precompute kernel API is intentionally designed for concurrent per-input verification.

			> Likely an incorrect or invalid review comment.
crates/consensus/src/kernel.rs (1)

24-96: LGTM!

Also applies to: 175-176

crates/consensus/src/lib.rs (1)

64-66: LGTM!

Also applies to: 168-175

crates/node/src/apply.rs (2)

921-971: LGTM!


1781-1863: LGTM!

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.

1 participant