Skip to content

Conversation

@prestwich
Copy link
Member

@prestwich prestwich commented Dec 31, 2025

Preload kzg during bin initialization

@prestwich prestwich marked this pull request as ready for review December 31, 2025 17:34
Copy link
Member Author

prestwich commented Dec 31, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

//
// This takes ~3 seconds, and we want to do it in parallel with the rest of
// the initialization.
std::thread::spawn(|| {
Copy link

Choose a reason for hiding this comment

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

Preface: not a rust expert, but just noting that I worry that an async version of this, if a call to _settings is made on the main thread could lazy init and create a race between the two. Perhaps it's worth a main thread call once and ensuring k8s readiness expires after we guarantee the initializations?

Copy link
Member Author

@prestwich prestwich Dec 31, 2025

Choose a reason for hiding this comment

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

so this is handled in the underlying c_kzg library, which uses a synchronization primitive to prevent this from happening

fn ethereum_kzg_settings_inner(precompute: u64) -> &'static Arc<KzgSettings> {
    static DEFAULT: OnceBox<Arc<KzgSettings>> = OnceBox::new();
    DEFAULT.get_or_init(|| {
        let settings = KzgSettings::load_trusted_setup(
            ETH_G1_MONOMIAL_POINTS,
            ETH_G1_LAGRANGE_POINTS,
            ETH_G2_MONOMIAL_POINTS,
            precompute,
        )
        .expect("failed to load default trusted setup");
        Box::new(Arc::new(settings))
    })
}

the OnceBox handles thread synchronization, allows only 1 entrant to the initialization. While initialization is occurring, threads trying to access the contents are blocked until it completes. No race condition allowed

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

oooh interesting, they explicitly use race

https://docs.rs/once_cell/latest/once_cell/race/index.html

Copy link
Member Author

Choose a reason for hiding this comment

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

🙄

Copy link
Member Author

Choose a reason for hiding this comment

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

on review, i am not concerned about this race and here's why:

  • we know that this process takes ~3 seconds
  • slot times are 12 seconds
  • the kzg build will not be invoked until the final 3 seconds of a slot
  • so this race will occur IFF all conditions hold
    • the builder is booted inside a slot that it controls
    • the builder is booted in the last half of the slot (as the 3s stretch started by init would have to last until the 3s in build, ie. initializtation is i the last 6 seconds of the slot)
    • the builder produces a block in that slot
      • this means the builder would have had to receive a header for the previous block
      • the builder will not receive a header from the WS subscription until the next block AFTER it boots
  • therefore there is no situation in which the builder boots in the last 6 seconds of a slot AND builds a block for that slot

@prestwich prestwich force-pushed the prestwich/preload-kzg branch from 9e72b68 to 306682c Compare December 31, 2025 18:05
Base automatically changed from prestwich/debug-span-build to main December 31, 2025 18:06
@prestwich prestwich force-pushed the prestwich/preload-kzg branch from 306682c to dd9b423 Compare December 31, 2025 18:06
@prestwich prestwich enabled auto-merge (squash) December 31, 2025 18:06
@prestwich prestwich merged commit 3b8e6eb into main Dec 31, 2025
6 checks passed
@prestwich prestwich deleted the prestwich/preload-kzg branch December 31, 2025 18:09
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.

4 participants