ethereum-consensus
ethereum-consensus copied to clipboard
Refactor presets to use (full) const generics
Refer this commit https://github.com/ralexstokes/ethereum_consensus/commit/9de3638e353d3e19254ea9e38d71e916f4f5d7fb where the decision was made to go for a duplicated version of the spec for each "preset". The codebase will suffer code duplication in the short run but avoid the compile-time cost and unwieldly UX of highly generic types.
Ideally, we could avoid this by leveraging generics in Rust, but the const generics feature is not fully developed yet. I think when the Rust team ships something that closes this issue https://github.com/rust-lang/rust/issues/60551, we should check if we can do something like the following:
pub trait Preset {
pub const ROOTS_LIMIT: usize;
// ... other constants
}
pub struct Mainnet;
impl Preset for Mainnet {
pub const ROOTS_LIMIT: usize = MAINNET_ROOTS_LIMIT;
// ... other constants
}
pub struct BeaconState<P: Preset> {
pub block_roots: Vec<Root, P::ROOTS_LIMIT>,
// ... other fields
}
// then e.g. something like:
pub fn apply_block<P: Preset>(state: BeaconState<P>, block: BeaconBlock<P>, config: Config) -> BeaconState<P> {
// ... implementation elided
}
This issue was partially resolved with #6 where the overhead of passing all of the required compile-time constants will be a bit of a chore inside the library, they can be specialized before users of this library have to deal with them which seems to be an acceptable compromise for now.
I'll leave this issue open as a reminder to revisit the ergonomics here once something like the above issue linked to rust-lang/rust
is resolved.