Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple KZG backends #88

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 298 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ regex = '1'
replace_with = '0.1'
reqwest = { version = '0.12', features = ['json', 'native-tls-vendored'] }
rusqlite = { version = '0.32', features = ['bundled'] }
rust-kzg-arkworks5 = { git = 'https://github.com/grandinetech/rust-kzg.git' }
rust-kzg-blst = { git = 'https://github.com/grandinetech/rust-kzg.git' }
rust-kzg-constantine = { git = 'https://github.com/grandinetech/rust-kzg.git' }
rust-kzg-zkcrypto = { git = 'https://github.com/grandinetech/rust-kzg.git' }
scrypt = '0.11'
semver = '1'
serde = { version = '1', features = ['derive', 'rc'] }
Expand Down
1 change: 1 addition & 0 deletions fork_choice_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,7 @@ impl<P: Preset> Store<P> {
&blob_sidecar.blob,
blob_sidecar.kzg_commitment,
blob_sidecar.kzg_proof,
self.store_config.kzg_backend,
)
.unwrap_or(false),
Error::BlobSidecarInvalid { blob_sidecar }
Expand Down
3 changes: 3 additions & 0 deletions fork_choice_store/src/store_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::{ops::Mul as _, time::Duration};

use derivative::Derivative;
use kzg_utils::{KzgBackend, DEFAULT_KZG_BACKEND};
use types::config::Config as ChainConfig;

pub const DEFAULT_CACHE_LOCK_TIMEOUT_MILLIS: u64 = 1500;
Expand All @@ -16,6 +17,8 @@ pub struct StoreConfig {
pub state_cache_lock_timeout: Duration,
#[derivative(Default(value = "128"))]
pub unfinalized_states_in_memory: u64,
#[derivative(Default(value = "DEFAULT_KZG_BACKEND"))]
pub kzg_backend: KzgBackend,
}

impl StoreConfig {
Expand Down
3 changes: 3 additions & 0 deletions grandine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ http_api_utils = { workspace = true }
itertools = { workspace = true }
libmdbx = { workspace = true }
keymanager = { workspace = true }
kzg_utils = { workspace = true }
log = { workspace = true }
logging = { workspace = true }
metrics = { workspace = true }
Expand Down Expand Up @@ -73,6 +74,8 @@ tempfile = { workspace = true }
test-case = { workspace = true }

[features]
default = ["kzg_utils/blst"]

logger-always-write-style = []

# `preset-any` and `network-any` should not be passed to Cargo.
Expand Down
6 changes: 6 additions & 0 deletions grandine/src/grandine_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use grandine_version::{APPLICATION_NAME, APPLICATION_NAME_AND_VERSION, APPLICATI
use http_api::HttpApiConfig;
use http_api_utils::DEFAULT_MAX_EVENTS;
use itertools::{EitherOrBoth, Itertools as _};
use kzg_utils::{KzgBackend, DEFAULT_KZG_BACKEND};
use log::warn;
use metrics::{MetricsServerConfig, MetricsServiceConfig};
use p2p::{Enr, Multiaddr, NetworkConfig};
Expand Down Expand Up @@ -386,6 +387,9 @@ struct BeaconNodeOptions {
/// [default: disabled]
#[clap(long)]
in_memory: bool,

#[clap(long, default_value_t = DEFAULT_KZG_BACKEND)]
kzg_backend: KzgBackend,
}

#[expect(
Expand Down Expand Up @@ -923,6 +927,7 @@ impl GrandineArgs {
track_liveness,
detect_doppelgangers,
in_memory,
kzg_backend,
} = beacon_node_options;

// let SlasherOptions {
Expand Down Expand Up @@ -1294,6 +1299,7 @@ impl GrandineArgs {
slashing_protection_history_limit,
in_memory,
validator_api_config,
kzg_backend,
})
}

Expand Down
2 changes: 2 additions & 0 deletions grandine/src/grandine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use eth1_api::AuthOptions;
use features::Feature;
use http_api::HttpApiConfig;
use itertools::Itertools as _;
use kzg_utils::KzgBackend;
use log::info;
use p2p::NetworkConfig;
use runtime::{MetricsConfig, StorageConfig};
Expand Down Expand Up @@ -67,6 +68,7 @@ pub struct GrandineConfig {
pub slashing_protection_history_limit: u64,
pub in_memory: bool,
pub validator_api_config: Option<ValidatorApiConfig>,
pub kzg_backend: KzgBackend,
}

impl GrandineConfig {
Expand Down
2 changes: 2 additions & 0 deletions grandine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ fn try_main() -> Result<()> {
slashing_protection_history_limit,
in_memory,
validator_api_config,
kzg_backend,
} = config;

features.into_iter().for_each(Feature::enable);
Expand Down Expand Up @@ -433,6 +434,7 @@ fn try_main() -> Result<()> {
max_epochs_to_retain_states_in_cache,
state_cache_lock_timeout,
unfinalized_states_in_memory,
kzg_backend,
};

let eth1_auth = Arc::new(Auth::new(auth_options)?);
Expand Down
15 changes: 14 additions & 1 deletion kzg_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ authors = ["Grandine <[email protected]>"]
[dependencies]
anyhow = { workspace = true }
kzg = { workspace = true }
rust-kzg-blst = { workspace = true }
rust-kzg-arkworks5 = { workspace = true, optional = true }
rust-kzg-blst = { workspace = true, optional = true }
rust-kzg-constantine = { workspace = true, optional = true }
rust-kzg-zkcrypto = { workspace = true, optional = true }
thiserror = { workspace = true }
types = { workspace = true }

Expand All @@ -20,3 +23,13 @@ test-generator = { workspace = true }

[lints]
workspace = true

[features]
# `bls-backend-any` should not be passed to Cargo.
# It only exist to avoid duplicating lists of features.
bls-backend-any = []

arkworks = ["bls-backend-any", "dep:rust-kzg-arkworks5"]
blst = ["bls-backend-any", "dep:rust-kzg-blst"]
constantine = ["bls-backend-any", "dep:rust-kzg-constantine"]
zkcrypto = ["bls-backend-any", "dep:rust-kzg-zkcrypto"]
83 changes: 83 additions & 0 deletions kzg_utils/src/bls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use core::{
fmt::{self, Display, Formatter},
str::FromStr,
};
use thiserror::Error;

#[derive(Debug, Clone, Copy)]
pub enum KzgBackend {
#[cfg(feature = "arkworks")]
Arkworks,
#[cfg(feature = "blst")]
Blst,
#[cfg(feature = "constantine")]
Constantine,
#[cfg(feature = "zkcrypto")]
Zkcrypto,
}

impl Display for KzgBackend {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "arkworks")]
Self::Arkworks => f.write_str("arkworks"),
#[cfg(feature = "blst")]
Self::Blst => f.write_str("blst"),
#[cfg(feature = "constantine")]
Self::Constantine => f.write_str("constantine"),
#[cfg(feature = "zkcrypto")]
Self::Zkcrypto => f.write_str("zkcrypto"),
}
}
}

#[derive(Debug, Error)]
pub enum KzgBackendParseError {
#[error("unknown backend {0} - valid values are arkworks, blst, constantine, zkcrypto")]
InvalidBackend(String),
#[error("backend is not compiled - please specify feature flag when compiling grandine")]
BackendNotCompiled,
}

impl FromStr for KzgBackend {
type Err = KzgBackendParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#[cfg(feature = "arkworks")]
"arkworks" => Ok(Self::Arkworks),
#[cfg(not(feature = "arkworks"))]
"arkworks" => Err(KzgBackendParseError::BackendNotCompiled),
#[cfg(feature = "blst")]
"blst" => Ok(Self::Blst),
#[cfg(not(feature = "blst"))]
"blst" => Err(KzgBackendParseError::BackendNotCompiled),
#[cfg(feature = "constantine")]
"constantine" => Ok(Self::Constantine),
#[cfg(not(feature = "constantine"))]
"constantine" => Err(KzgBackendParseError::BackendNotCompiled),
#[cfg(feature = "zkcrypto")]
"zkcrypto" => Ok(Self::Zkcrypto),
#[cfg(not(feature = "zkcrypto"))]
"zkcrypto" => Err(KzgBackendParseError::BackendNotCompiled),
unknown => Err(KzgBackendParseError::InvalidBackend(unknown.to_owned())),
}
}
}

#[cfg(feature = "blst")]
pub const DEFAULT_KZG_BACKEND: KzgBackend = KzgBackend::Blst;
#[cfg(all(not(feature = "blst"), feature = "zkcrypto"))]
pub const DEFAULT_KZG_BACKEND: KzgBackend = KzgBackend::Zkcrypto;
#[cfg(all(
not(feature = "blst"),
not(feature = "zkcrypto"),
feature = "constantine"
))]
pub const DEFAULT_KZG_BACKEND: KzgBackend = KzgBackend::Constantine;
#[cfg(all(
not(feature = "blst"),
not(feature = "zkcrypto"),
not(feature = "constantine")
))]
pub const DEFAULT_KZG_BACKEND: KzgBackend = KzgBackend::Arkworks;
Loading