Skip to content

feat: add nucleus engine crate - #18

Open
bmuddha wants to merge 1 commit into
svmfrom
nucleus
Open

feat: add nucleus engine crate#18
bmuddha wants to merge 1 commit into
svmfrom
nucleus

Conversation

@bmuddha

@bmuddha bmuddha commented May 12, 2026

Copy link
Copy Markdown
Collaborator

What changed

Added the nucleus crate with feature-gated primitives shared across the engine stack.

Why

Ledger, runtime, storage, and orchestration crates need common types and lifecycle
coordination without assigning storage or execution policy to the shared dependency.

Closes #28.

Impact

  • Introduces shared slot and byte-size types, ledger positions, runtime messages,
    metrics helpers, thread-local runtime state, and test support.
  • Keeps Heed, ledger, metrics, runtime, shutdown, testkit, and TLS support behind
    narrow crate features.
  • Provides ShutdownManager, service handles, ordered cancellation tiers, and
    bounded termination reporting.

Reviewer notes

Shutdown cancels the replication client, pacemaker, sequencer, and remaining
services in order. Each tier receives its own bounded window to report completion
before shutdown advances.

Follow-up

Ledger, keeper, processor, engine, and replication services consume these
feature-gated primitives upstack.

@bmuddha
bmuddha force-pushed the nucleus branch 2 times, most recently from 410f558 to d323e3a Compare May 14, 2026 14:46
@bmuddha
bmuddha changed the base branch from solana-account to graphite-base/18 May 28, 2026 13:20
@bmuddha
bmuddha changed the base branch from graphite-base/18 to svm May 28, 2026 13:20
@bmuddha
bmuddha force-pushed the nucleus branch 4 times, most recently from fddd6f0 to c01202f Compare May 28, 2026 14:42
@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Engine primitives and calculator interface

Layer / File(s) Summary
Workspace and crate manifests
Cargo.toml, nucleus/Cargo.toml, programs/v42-calculator-interface/Cargo.toml
The workspace adds the new crates, shared dependencies, feature flags, and optional integrations.
Nucleus shared contracts
nucleus/README.md, nucleus/src/lib.rs, nucleus/src/config.rs, nucleus/src/ledger.rs, nucleus/src/runtime.rs
Nucleus adds shared configuration, ledger, runtime, slot, size, and time APIs with feature-gated modules.
Storage, notification, TLS, and shutdown
nucleus/src/heed.rs, nucleus/src/notifier.rs, nucleus/src/tls.rs, nucleus/src/shutdown.rs
Nucleus adds transaction helpers, asynchronous notification, thread-local message state, and ordered service shutdown.
Metrics and execution fixtures
nucleus/src/metrics.rs, nucleus/src/testkit.rs
Nucleus adds Prometheus and tracing helpers plus deterministic transaction, block, signing, and calculator fixtures.
Calculator opcodes and expression builder
programs/v42-calculator-interface/README.md, programs/v42-calculator-interface/src/lib.rs, programs/v42-calculator-interface/src/opcodes.rs, programs/v42-calculator-interface/src/builder.rs
The calculator interface adds RPN opcode definitions, transfer encoding, expression composition, arithmetic lowering, and recursive CPI serialization.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OS
  participant ShutdownManager
  participant ShutdownHandle
  participant Service
  OS->>ShutdownManager: Send shutdown signal
  ShutdownManager->>ShutdownHandle: Cancel service tier
  ShutdownHandle->>Service: Notify cancellation
  Service->>ShutdownManager: Report termination reason
  ShutdownManager->>ShutdownManager: Drain tier with timeout
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The new v42-calculator-interface crate and related workspace entries are not directly required by the nucleus objectives in [#28]. Move the v42-calculator-interface additions to a separate pull request, or document their direct dependency on the nucleus scope.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding the nucleus engine crate.
Description check ✅ Passed The description explains the nucleus crate, its feature-gated primitives, shutdown coordination, scope, and follow-up use.
Linked Issues check ✅ Passed The changes satisfy the shared primitives, feature boundaries, test support, Heed aliases, and ordered shutdown objectives in [#28].
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nucleus

Warning

Tools execution failed with the following error:

Failed to run tools: 14 UNAVAILABLE: Connection dropped


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.

@bmuddha
bmuddha force-pushed the nucleus branch 2 times, most recently from 02c3fc8 to 5400e0f Compare May 29, 2026 15:49
@bmuddha

bmuddha commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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.

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@nucleus/src/heed.rs`:
- Around line 27-45: Remove the lifetime-extending transmute calls from
DatabaseIndex::write_txn and DatabaseIndex::read_txn. Bind newly opened
transactions to the borrow of self.env(), or adopt an owner that keeps the Env
alive, while preserving supplied transactions and caller-owned storage without
requiring unsafe lifetime assumptions.

In `@nucleus/src/testkit.rs`:
- Around line 60-66: Update the block function’s hash construction so all bits
of Slot contribute to a deterministic hash, avoiding collisions between slots
that differ beyond the lowest byte. Preserve the existing block fields and
ensure distinct Slot values produce distinct hashes within the supported Slot
range.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ac2e5c95-96a6-453e-a119-976b7e354a00

📥 Commits

Reviewing files that changed from the base of the PR and between 1f187f7 and 432362c.

📒 Files selected for processing (18)
  • Cargo.toml
  • nucleus/Cargo.toml
  • nucleus/README.md
  • nucleus/src/config.rs
  • nucleus/src/heed.rs
  • nucleus/src/ledger.rs
  • nucleus/src/lib.rs
  • nucleus/src/metrics.rs
  • nucleus/src/notifier.rs
  • nucleus/src/runtime.rs
  • nucleus/src/shutdown.rs
  • nucleus/src/testkit.rs
  • nucleus/src/tls.rs
  • programs/v42-calculator-interface/Cargo.toml
  • programs/v42-calculator-interface/README.md
  • programs/v42-calculator-interface/src/builder.rs
  • programs/v42-calculator-interface/src/lib.rs
  • programs/v42-calculator-interface/src/opcodes.rs

Comment thread nucleus/src/heed.rs
Comment on lines +27 to +45
fn write_txn<'t, 'e>(&self, txn: OptRwTxn<'t, 'e>) -> Result<&'t mut RwTxn<'e>> {
if let Some(txn) = txn {
return Ok(txn);
}
// SAFETY: guaranteed by the trait contract. The transaction is stored
// in the caller-owned option and must be dropped before `env`.
let write = unsafe { mem::transmute::<RwTxn<'_>, RwTxn<'e>>(self.env().write_txn()?) };
Ok(txn.insert(write))
}

/// Uses the supplied read transaction or opens one on demand.
fn read_txn<'t, 'e>(&self, txn: OptRoTxn<'t, 'e>) -> Result<&'t RoTxnTls<'e>> {
if let Some(txn) = txn {
return Ok(txn);
}
// SAFETY: guaranteed by the trait contract. The transaction is stored
// in the caller-owned option and must be dropped before `env`.
let read = unsafe { mem::transmute::<RoTxnTls<'_>, RoTxnTls<'e>>(self.env().read_txn()?) };
Ok(txn.insert(read))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🔴 Critical | 🏗️ Heavy lift

Do not extend transaction lifetimes with transmute.

'e comes from the caller-owned Option, but it is not bound to &self. A safe caller can pass Option<RwTxn<'static>>::None, store the widened transaction, and drop the index before that transaction. This can access a dropped Env during transaction use or destruction.

Bind the transaction lifetime to the Env borrow, or use a transaction owner that keeps the Env alive. Do not require safe callers to uphold an unsafe lifetime condition. Heed defines Env::write_txn and Env::read_txn with lifetimes borrowed from Env; its explicit static read transaction instead owns the environment. (docs.rs)

#!/bin/bash
set -euo pipefail

# Find every implementation and use of the lifetime-extending API.
rg -n -C 5 'unsafe\s+impl.*DatabaseIndex|impl.*DatabaseIndex|\.write_txn\(|\.read_txn\(' \
  --glob '*.rs'

# Inspect the trait and all transaction-storage declarations.
rg -n -C 8 'DatabaseIndex|OptRwTxn|OptRoTxn|RwTxn|RoTxnTls|transmute' \
  nucleus/src/heed.rs
🤖 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 `@nucleus/src/heed.rs` around lines 27 - 45, Remove the lifetime-extending
transmute calls from DatabaseIndex::write_txn and DatabaseIndex::read_txn. Bind
newly opened transactions to the borrow of self.env(), or adopt an owner that
keeps the Env alive, while preserving supplied transactions and caller-owned
storage without requiring unsafe lifetime assumptions.

Comment thread nucleus/src/testkit.rs
Comment on lines +60 to +66
/// A block boundary with a distinct hash and time derived from `slot`.
pub fn block(slot: Slot) -> Block {
Block {
slot,
hash: Hash::new_from_array([slot as u8; 32]),
time: slot as i64,
parent: Hash::default(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Generate a unique deterministic hash for each Slot.

slot as u8 discards all higher bits. For example, slots 1 and 257 produce the same hash. This contradicts the block rustdoc and can alias different block identities in tests.

Proposed fix
 pub fn block(slot: Slot) -> Block {
+    let mut hash = [0; 32];
+    hash[..8].copy_from_slice(&slot.to_le_bytes());
     Block {
         slot,
-        hash: Hash::new_from_array([slot as u8; 32]),
+        hash: Hash::new_from_array(hash),
         time: slot as i64,
         parent: Hash::default(),
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// A block boundary with a distinct hash and time derived from `slot`.
pub fn block(slot: Slot) -> Block {
Block {
slot,
hash: Hash::new_from_array([slot as u8; 32]),
time: slot as i64,
parent: Hash::default(),
/// A block boundary with a distinct hash and time derived from `slot`.
pub fn block(slot: Slot) -> Block {
let mut hash = [0; 32];
hash[..8].copy_from_slice(&slot.to_le_bytes());
Block {
slot,
hash: Hash::new_from_array(hash),
time: slot as i64,
parent: Hash::default(),
🤖 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 `@nucleus/src/testkit.rs` around lines 60 - 66, Update the block function’s
hash construction so all bits of Slot contribute to a deterministic hash,
avoiding collisions between slots that differ beyond the lowest byte. Preserve
the existing block fields and ensure distinct Slot values produce distinct
hashes within the supported Slot range.

Source: Path instructions

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

Labels

coderabbit Trigger coderabbit review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add nucleus crate for shared engine primitives

1 participant