Add {fork_name}_enabled functions (#5951)

* add fork_name_enabled fn to Forkname impl

* refactor codebase to use new fork_enabled fn

* fmt

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into fork-ord-impl

* small code cleanup

* resolve merge conflicts

* fix beacon chain test

* merge conflicts

* fix ef test issue

* resolve merge conflicts
This commit is contained in:
Eitan Seri-Levi
2024-10-02 19:00:52 -07:00
committed by GitHub
parent dd08ebb2b0
commit 82faf975b3
22 changed files with 223 additions and 251 deletions

View File

@@ -1678,27 +1678,23 @@ impl<E: EthSpec> FullBlockContents<E> {
bytes: &[u8],
fork_name: ForkName,
) -> Result<Self, ssz::DecodeError> {
match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {
BeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
.map(|block| FullBlockContents::Block(block))
}
ForkName::Deneb | ForkName::Electra => {
let mut builder = ssz::SszDecoderBuilder::new(bytes);
if fork_name.deneb_enabled() {
let mut builder = ssz::SszDecoderBuilder::new(bytes);
builder.register_anonymous_variable_length_item()?;
builder.register_type::<KzgProofs<E>>()?;
builder.register_type::<BlobsList<E>>()?;
builder.register_anonymous_variable_length_item()?;
builder.register_type::<KzgProofs<E>>()?;
builder.register_type::<BlobsList<E>>()?;
let mut decoder = builder.build()?;
let block = decoder.decode_next_with(|bytes| {
BeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
})?;
let kzg_proofs = decoder.decode_next()?;
let blobs = decoder.decode_next()?;
let mut decoder = builder.build()?;
let block = decoder
.decode_next_with(|bytes| BeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name))?;
let kzg_proofs = decoder.decode_next()?;
let blobs = decoder.decode_next()?;
Ok(FullBlockContents::new(block, Some((kzg_proofs, blobs))))
}
Ok(FullBlockContents::new(block, Some((kzg_proofs, blobs))))
} else {
BeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
.map(|block| FullBlockContents::Block(block))
}
}
@@ -1738,15 +1734,14 @@ impl<E: EthSpec> ForkVersionDeserialize for FullBlockContents<E> {
value: serde_json::value::Value,
fork_name: ForkName,
) -> Result<Self, D::Error> {
match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {
Ok(FullBlockContents::Block(
BeaconBlock::deserialize_by_fork::<'de, D>(value, fork_name)?,
))
}
ForkName::Deneb | ForkName::Electra => Ok(FullBlockContents::BlockContents(
if fork_name.deneb_enabled() {
Ok(FullBlockContents::BlockContents(
BlockContents::deserialize_by_fork::<'de, D>(value, fork_name)?,
)),
))
} else {
Ok(FullBlockContents::Block(
BeaconBlock::deserialize_by_fork::<'de, D>(value, fork_name)?,
))
}
}
}
@@ -1838,28 +1833,25 @@ impl<E: EthSpec> PublishBlockRequest<E> {
/// SSZ decode with fork variant determined by `fork_name`.
pub fn from_ssz_bytes(bytes: &[u8], fork_name: ForkName) -> Result<Self, ssz::DecodeError> {
match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {
SignedBeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
.map(|block| PublishBlockRequest::Block(Arc::new(block)))
}
ForkName::Deneb | ForkName::Electra => {
let mut builder = ssz::SszDecoderBuilder::new(bytes);
builder.register_anonymous_variable_length_item()?;
builder.register_type::<KzgProofs<E>>()?;
builder.register_type::<BlobsList<E>>()?;
if fork_name.deneb_enabled() {
let mut builder = ssz::SszDecoderBuilder::new(bytes);
builder.register_anonymous_variable_length_item()?;
builder.register_type::<KzgProofs<E>>()?;
builder.register_type::<BlobsList<E>>()?;
let mut decoder = builder.build()?;
let block = decoder.decode_next_with(|bytes| {
SignedBeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
})?;
let kzg_proofs = decoder.decode_next()?;
let blobs = decoder.decode_next()?;
Ok(PublishBlockRequest::new(
Arc::new(block),
Some((kzg_proofs, blobs)),
))
}
let mut decoder = builder.build()?;
let block = decoder.decode_next_with(|bytes| {
SignedBeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
})?;
let kzg_proofs = decoder.decode_next()?;
let blobs = decoder.decode_next()?;
Ok(PublishBlockRequest::new(
Arc::new(block),
Some((kzg_proofs, blobs)),
))
} else {
SignedBeaconBlock::from_ssz_bytes_for_fork(bytes, fork_name)
.map(|block| PublishBlockRequest::Block(Arc::new(block)))
}
}