Skip to content

feat: high precision sysvar#1384

Open
Dodecahedr0x wants to merge 18 commits into
masterfrom
dode/higher-precision-sysvar
Open

feat: high precision sysvar#1384
Dodecahedr0x wants to merge 18 commits into
masterfrom
dode/higher-precision-sysvar

Conversation

@Dodecahedr0x

@Dodecahedr0x Dodecahedr0x commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Introduces a high precision sysvar updated at each slot and stored in the ledger. Closes #1434

Breaking Changes

  • None

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added a HighPrecisionClock sysvar that exposes Unix time in milliseconds.
    • Extended timestamp propagation so replication/scheduling uses timestamp_millis for higher-precision tracking.
  • Bug Fixes

    • Updated ledger block-time persistence, restore, replay, and tip reconstruction to preserve sub-second precision.
    • Reading block time now supports both whole-seconds (existing behavior) and milliseconds precision (new accessor).
  • Tests

    • Added/updated integration and replay tests to validate sysvar correctness and millisecond-accurate ledger restoration.
  • Chores

    • Updated Solana git dependency revisions and related configuration formatting.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 52597144-f8b1-40d8-9026-cdef96720616

📥 Commits

Reviewing files that changed from the base of the PR and between aba0311 and 4640b9e.

📒 Files selected for processing (2)
  • Cargo.toml
  • test-integration/Cargo.toml

📝 Walkthrough

Walkthrough

The change propagates millisecond-precision timestamps through replicated blocks, LatestBlockInner, ledger persistence, replay, and block-time APIs. It adds and exports the HighPrecisionClock sysvar, initializes and updates it during scheduling, exposes it to transaction execution, and adds processor and ledger-restore validation tests.

Assessment against linked issues

Objective Addressed Explanation
Introduce a millisecond-precision timestamp sysvar [#1434]

Suggested reviewers: bmuddha, gabrielepicco, thlorenz

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dode/higher-precision-sysvar

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.

@Dodecahedr0x
Dodecahedr0x marked this pull request as ready for review July 2, 2026 14:08
@Dodecahedr0x

Copy link
Copy Markdown
Contributor Author

This requires merging this SVM PR first

Comment thread magicblock-core/src/link/replication.rs Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 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 `@magicblock-processor/src/scheduler/mod.rs`:
- Around line 585-598: The scheduler hot path in the block/tick timestamp path
uses SystemTime::now().duration_since(UNIX_EPOCH).unwrap(), which can panic and
stop block production. Update the timestamp creation in the scheduler module
(around LatestBlockInner::new_with_millis and the Message::Block construction)
to handle the error explicitly, either by propagating/logging a recoverable
failure or by falling back to a safe timestamp, and keep the loop running
instead of unwrapping.
- Around line 670-684: The sysvar update logic is duplicated between
update_high_precision_clock_sysvar and update_clock_sysvar, both doing the same
get_account/serialize_data/insert_account flow. Extract that repeated sequence
into a shared helper in scheduler::mod, and have both update_clock_sysvar and
update_high_precision_clock_sysvar call it with their specific sysvar value and
account ID to keep future sysvar additions consistent.

In `@test-integration/test-ledger-restore/tests/17_high_precision_clock.rs`:
- Around line 56-63: The sanity check around observed.count in the
HighPrecisionClock restore tests uses an incorrect upper bound and misleading
label: count is derived from milliseconds modulo 1000, so the assertion should
validate a value in the documented 0–999 range instead of a nanosecond-sized
bound, and the failure message should say millis/sub-second value rather than
nanos. Update both occurrences in the test to use the same correct bound and
message so the check actually catches regressions in the HighPrecisionClock read
path.
- Around line 123-129: The restart check in high precision clock test is flaky
because it compares the `observed.updates` and `observed.count` components
independently in the `assert!`, which can fail even when real time advanced;
update the logic around the `high precision clock should advance after restart`
assertion to compare a single combined millisecond timestamp derived from the
same fields instead of separate component-wise greater-than checks, and keep the
`cleanup(&mut validator)` flow intact.
🪄 Autofix (Beta)

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: ASSERTIVE

Plan: Pro

Run ID: 90eae371-dadb-4a26-916f-f393ace9c697

📥 Commits

Reviewing files that changed from the base of the PR and between 63927e4 and eecf9ab.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • test-integration/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (18)
  • Cargo.toml
  • magicblock-core/src/link/replication.rs
  • magicblock-ledger/src/blockstore_processor/mod.rs
  • magicblock-ledger/src/database/columns.rs
  • magicblock-ledger/src/lib.rs
  • magicblock-ledger/src/store/api.rs
  • magicblock-magic-program-api/Cargo.toml
  • magicblock-magic-program-api/src/lib.rs
  • magicblock-magic-program-api/src/sysvar.rs
  • magicblock-processor/src/executor/mod.rs
  • magicblock-processor/src/scheduler/mod.rs
  • magicblock-processor/src/scheduler/state.rs
  • magicblock-processor/tests/high_precision_clock.rs
  • magicblock-processor/tests/replica_ordering.rs
  • test-integration/Cargo.toml
  • test-integration/programs/flexi-counter/src/instruction.rs
  • test-integration/programs/flexi-counter/src/processor.rs
  • test-integration/test-ledger-restore/tests/17_high_precision_clock.rs

Comment thread magicblock-processor/src/scheduler/mod.rs Outdated
Comment thread magicblock-processor/src/scheduler/mod.rs
@GabrielePicco

Copy link
Copy Markdown
Collaborator

Also as a reminder, we need to carefully release this as it would make previously produced block invalidly parse the clock (previously stored in seconds), until the ledger fully truncate the olds blocks

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
magicblock-ledger/src/store/api.rs (1)

287-302: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Version or migrate the blocktime_cf unit change.

Existing rows contain seconds, but Lines 287-293 now divide every value as milliseconds. Until old rows are truncated, RPC block times and replayed clocks become roughly 1000× too small. Use a schema/version marker or migration, rather than reinterpreting the existing column in place.

Also applies to: 430-431

🤖 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 `@magicblock-ledger/src/store/api.rs` around lines 287 - 302, The blocktime_cf
unit change must preserve compatibility with existing rows stored in seconds.
Add a schema/version marker or migration before changing the interpretation, and
update the block-time accessors around get_block_time and get_block_time_millis
so legacy values remain seconds while migrated values are converted from
milliseconds; apply the same handling to the related code near the second call
site.
🤖 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 `@magicblock-processor/src/scheduler/mod.rs`:
- Around line 749-755: Update update_sysvar_account to return and propagate
errors from both serialize_data and insert_account instead of discarding their
results. Thread the resulting error through the transition/replay update callers
so the operation aborts when Clock or HighPrecisionClock persistence fails.

---

Outside diff comments:
In `@magicblock-ledger/src/store/api.rs`:
- Around line 287-302: The blocktime_cf unit change must preserve compatibility
with existing rows stored in seconds. Add a schema/version marker or migration
before changing the interpretation, and update the block-time accessors around
get_block_time and get_block_time_millis so legacy values remain seconds while
migrated values are converted from milliseconds; apply the same handling to the
related code near the second call site.
🪄 Autofix (Beta)

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: ASSERTIVE

Plan: Pro

Run ID: 00e3736c-b0ef-4a04-a86a-79973f5da2cb

📥 Commits

Reviewing files that changed from the base of the PR and between eecf9ab and dcf325e.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • test-integration/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (10)
  • Cargo.toml
  • magicblock-core/src/link/blocks.rs
  • magicblock-core/src/link/replication.rs
  • magicblock-ledger/src/blockstore_processor/mod.rs
  • magicblock-ledger/src/store/api.rs
  • magicblock-magic-program-api/src/lib.rs
  • magicblock-processor/src/executor/mod.rs
  • magicblock-processor/src/scheduler/mod.rs
  • test-integration/Cargo.toml
  • test-integration/test-ledger-restore/tests/17_high_precision_clock.rs
💤 Files with no reviewable changes (1)
  • magicblock-core/src/link/replication.rs

Comment thread magicblock-processor/src/scheduler/mod.rs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

♻️ Duplicate comments (2)
magicblock-processor/src/scheduler/mod.rs (2)

571-584: ⚠️ Potential issue | 🟠 Major

Previously reported production unwrap() remains.

duration_since(UNIX_EPOCH).unwrap() can panic on a pre-epoch system clock and terminate the scheduler’s block-production loop. Handle the error explicitly or document and enforce the invariant.

As per path instructions, “Treat any usage of .unwrap() or .expect() in production Rust code as a MAJOR issue. Request proper error handling or explicit justification with invariants.”

🤖 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 `@magicblock-processor/src/scheduler/mod.rs` around lines 571 - 584, Replace
the production `duration_since(UNIX_EPOCH).unwrap()` in the block timestamp
construction with explicit handling for pre-epoch system times, preventing
scheduler termination; propagate or handle the error according to the
surrounding block-production flow while preserving millisecond timestamp
behavior.

Source: Path instructions


747-751: ⚠️ Potential issue | 🟠 Major

Previously reported persistence errors remain discarded.

Failures from serialize_data and insert_account are ignored, allowing Clock or HighPrecisionClock to remain stale while the scheduler advances. Propagate the error or enforce an explicit infallibility invariant.

🤖 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 `@magicblock-processor/src/scheduler/mod.rs` around lines 747 - 751, Update
update_sysvar_account to handle failures from account.serialize_data and
accountsdb.insert_account instead of discarding their results. Propagate the
error through the caller chain, or explicitly enforce and document an
infallibility invariant if the API cannot return errors, ensuring Clock and
HighPrecisionClock updates cannot silently remain stale.
🤖 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.

Duplicate comments:
In `@magicblock-processor/src/scheduler/mod.rs`:
- Around line 571-584: Replace the production
`duration_since(UNIX_EPOCH).unwrap()` in the block timestamp construction with
explicit handling for pre-epoch system times, preventing scheduler termination;
propagate or handle the error according to the surrounding block-production flow
while preserving millisecond timestamp behavior.
- Around line 747-751: Update update_sysvar_account to handle failures from
account.serialize_data and accountsdb.insert_account instead of discarding their
results. Propagate the error through the caller chain, or explicitly enforce and
document an infallibility invariant if the API cannot return errors, ensuring
Clock and HighPrecisionClock updates cannot silently remain stale.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 77107b5a-93f8-4329-8df1-d778b761807a

📥 Commits

Reviewing files that changed from the base of the PR and between dcf325e and 48f2c12.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • test-integration/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • Cargo.toml
  • magicblock-processor/src/scheduler/mod.rs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
magicblock-ledger/src/store/api.rs (1)

170-175: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Add a migration or legacy-ledger guard before rebuilding latest_block from blocktime_cf (magicblock-ledger/src/store/api.rs:170-175). New writes store milliseconds, but reopening an older ledger will treat second-based values as millis and reconstruct a tip clock that’s ~1000x too small. Add a versioned normalization/open-path fallback and a regression test for a pre-millis ledger.

🤖 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 `@magicblock-ledger/src/store/api.rs` around lines 170 - 175, Guard the
latest_block reconstruction around LatestBlockInner::new_with_millis and
ledger.get_block_time_millis so legacy blocktime_cf values stored in seconds are
detected and normalized before use. Add a versioned migration or open-path
fallback that preserves millisecond values for current ledgers, and add a
regression test covering reopening a pre-millis ledger.
🤖 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.

Outside diff comments:
In `@magicblock-ledger/src/store/api.rs`:
- Around line 170-175: Guard the latest_block reconstruction around
LatestBlockInner::new_with_millis and ledger.get_block_time_millis so legacy
blocktime_cf values stored in seconds are detected and normalized before use.
Add a versioned migration or open-path fallback that preserves millisecond
values for current ledgers, and add a regression test covering reopening a
pre-millis ledger.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: a63a080b-1fd4-4a0e-a2b2-43971b820209

📥 Commits

Reviewing files that changed from the base of the PR and between 48f2c12 and aba0311.

⛔ Files ignored due to path filters (2)
  • Cargo.lock is excluded by !**/*.lock
  • test-integration/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • Cargo.toml
  • magicblock-ledger/src/store/api.rs
  • magicblock-processor/src/scheduler/mod.rs
  • magicblock-processor/src/scheduler/state.rs
  • test-integration/Cargo.toml

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.

feat: introducing v1 HighPrecisionSysvar

2 participants