Skip to content

Commit

Permalink
get spec tests working and fix json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean authored and Woodpile37 committed Jan 6, 2024
1 parent 96c33f6 commit f21d48c
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 65 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CROSS_FEATURES ?= gnosis,slasher-lmdb,slasher-mdbx
CROSS_PROFILE ?= release

# List of features to use when running EF tests.
EF_TEST_FEATURES ?= beacon_chain/withdrawals,beacon_chain/withdrawals-processing
EF_TEST_FEATURES ?= withdrawals,withdrawals-processing

# Cargo profile for regular builds.
PROFILE ?= release
Expand All @@ -38,7 +38,7 @@ install:

# Builds the lcli binary in release (optimized).
install-lcli:
cargo install --path lcli --force --locked --features "$(FEATURES)" --profile "$(PROFILE)"
cargo install --path lcli --force --locked --features "$(FEATURES),$(EF_TEST_FEATURES)" --profile "$(PROFILE)"

# The following commands use `cross` to build a cross-compile.
#
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/blob_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn validate_blob_for_gossip<T: BeaconChainTypes>(
// }

// Verify that the KZG proof is a valid G1 point
if PublicKey::deserialize(&blob_sidecar.kzg_aggregate_proof.0).is_err() {
if PublicKey::deserialize(&blob_sidecar.kzg_aggregated_proof.0).is_err() {
return Err(BlobError::InvalidKZGCommitment);
}

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/kzg_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn validate_blobs_sidecar<T: EthSpec>(
kzg.verify_aggregate_kzg_proof(
&blobs,
expected_kzg_commitments,
blobs_sidecar.kzg_aggregate_proof,
blobs_sidecar.kzg_aggregated_proof,
)
.map_err(|e| format!("Failed to verify kzg proof: {:?}", e))
}
Expand Down
7 changes: 4 additions & 3 deletions beacon_node/execution_layer/src/engine_api/json_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@ impl From<JsonPayloadAttributes> for PayloadAttributes {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(bound = "T: EthSpec", rename_all = "camelCase")]
pub struct JsonBlobBundles<T: EthSpec> {
pub struct JsonBlobsBundle<T: EthSpec> {
pub block_hash: ExecutionBlockHash,
pub kzgs: Vec<KzgCommitment>,
pub blobs: Vec<Blob<T>>,
pub kzgs: VariableList<KzgCommitment, T::MaxBlobsPerBlock>,
#[serde(with = "ssz_types::serde_utils::list_of_hex_fixed_vec")]
pub blobs: VariableList<Blob<T>, T::MaxBlobsPerBlock>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down
36 changes: 11 additions & 25 deletions beacon_node/execution_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ pub enum BlockProposalContents<T: EthSpec, Payload: AbstractExecPayload<T>> {
Payload(Payload),
PayloadAndBlobs {
payload: Payload,
kzg_commitments: Vec<KzgCommitment>,
blobs: Vec<Blob<T>>,
kzg_commitments: VariableList<KzgCommitment, T::MaxBlobsPerBlock>,
blobs: VariableList<Blob<T>, T::MaxBlobsPerBlock>,
},
}

impl<T: EthSpec, Payload: AbstractExecPayload<T>> BlockProposalContents<T, Payload> {
pub fn deconstruct(self) -> (Payload, Option<Vec<KzgCommitment>>, Option<Vec<Blob<T>>>) {
pub fn deconstruct(
self,
) -> (
Payload,
Option<VariableList<KzgCommitment, T::MaxBlobsPerBlock>>,
Option<VariableList<Blob<T>, T::MaxBlobsPerBlock>>,
) {
match self {
Self::Payload(payload) => (payload, None, None),
Self::PayloadAndBlobs {
Expand Down Expand Up @@ -133,35 +139,15 @@ impl<T: EthSpec, Payload: AbstractExecPayload<T>> BlockProposalContents<T, Paylo
} => payload,
}
}
pub fn kzg_commitments(&self) -> Option<&[KzgCommitment]> {
match self {
Self::Payload(_) => None,
Self::PayloadAndBlobs {
payload: _,
kzg_commitments,
blobs: _,
} => Some(kzg_commitments),
}
}
pub fn blobs(&self) -> Option<&[Blob<T>]> {
match self {
Self::Payload(_) => None,
Self::PayloadAndBlobs {
payload: _,
kzg_commitments: _,
blobs,
} => Some(blobs),
}
}
pub fn default_at_fork(fork_name: ForkName) -> Self {
match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
BlockProposalContents::Payload(Payload::default_at_fork(fork_name))
}
ForkName::Eip4844 => BlockProposalContents::PayloadAndBlobs {
payload: Payload::default_at_fork(fork_name),
blobs: vec![],
kzg_commitments: vec![],
blobs: VariableList::default(),
kzg_commitments: VariableList::default(),
},
}
}
Expand Down
17 changes: 15 additions & 2 deletions beacon_node/execution_layer/src/test_utils/handle_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn handle_rpc<T: EthSpec>(
.unwrap())
}
}
ENGINE_NEW_PAYLOAD_V1 => {
ENGINE_NEW_PAYLOAD_V1 | ENGINE_NEW_PAYLOAD_V2 => {
let request: JsonExecutionPayload<T> = get_param(params, 0)?;

// Canned responses set by block hash take priority.
Expand Down Expand Up @@ -120,7 +120,7 @@ pub async fn handle_rpc<T: EthSpec>(

Ok(serde_json::to_value(JsonExecutionPayloadV1::try_from(response).unwrap()).unwrap())
}
ENGINE_FORKCHOICE_UPDATED_V1 => {
ENGINE_FORKCHOICE_UPDATED_V1 | ENGINE_FORKCHOICE_UPDATED_V2 => {
let forkchoice_state: JsonForkchoiceStateV1 = get_param(params, 0)?;
let payload_attributes: Option<JsonPayloadAttributes> = get_param(params, 1)?;

Expand Down Expand Up @@ -153,6 +153,19 @@ pub async fn handle_rpc<T: EthSpec>(

Ok(serde_json::to_value(response).unwrap())
}

ENGINE_GET_PAYLOAD_V2 => {
let request: JsonPayloadIdRequest = get_param(params, 0)?;
let id = request.into();

let response = ctx
.execution_block_generator
.write()
.get_payload(&id)
.ok_or_else(|| format!("no payload for id {:?}", id))?;

Ok(serde_json::to_value(JsonExecutionPayloadV2::try_from(response).unwrap()).unwrap())
}
ENGINE_EXCHANGE_TRANSITION_CONFIGURATION_V1 => {
let block_generator = ctx.execution_block_generator.read();
let transition_config: TransitionConfigurationV1 = TransitionConfigurationV1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<T: BeaconChainTypes> Worker<T> {
"gossip_block_low",
);
return None;
}
}
Err(blob_errors) => unimplemented!("handle")
};

Expand Down
77 changes: 77 additions & 0 deletions consensus/ssz_types/src/serde_utils/list_of_hex_fixed_vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! Serialize `VariableList<FixedVector<u8, M>, N>` as list of 0x-prefixed hex string.
use crate::{FixedVector, VariableList};
use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
use std::marker::PhantomData;
use typenum::Unsigned;

#[derive(Deserialize)]
#[serde(transparent)]
pub struct WrappedListOwned<N: Unsigned>(
#[serde(with = "crate::serde_utils::hex_fixed_vec")] FixedVector<u8, N>,
);

#[derive(Serialize)]
#[serde(transparent)]
pub struct WrappedListRef<'a, N: Unsigned>(
#[serde(with = "crate::serde_utils::hex_fixed_vec")] &'a FixedVector<u8, N>,
);

pub fn serialize<S, M, N>(
list: &VariableList<FixedVector<u8, M>, N>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
M: Unsigned,
N: Unsigned,
{
let mut seq = serializer.serialize_seq(Some(list.len()))?;
for bytes in list {
seq.serialize_element(&WrappedListRef(bytes))?;
}
seq.end()
}

#[derive(Default)]
pub struct Visitor<M, N> {
_phantom_m: PhantomData<M>,
_phantom_n: PhantomData<N>,
}

impl<'a, M, N> serde::de::Visitor<'a> for Visitor<M, N>
where
M: Unsigned,
N: Unsigned,
{
type Value = VariableList<FixedVector<u8, M>, N>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a list of 0x-prefixed hex bytes")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'a>,
{
let mut list: VariableList<FixedVector<u8, M>, N> = <_>::default();

while let Some(val) = seq.next_element::<WrappedListOwned<M>>()? {
list.push(val.0).map_err(|e| {
serde::de::Error::custom(format!("failed to push value to list: {:?}.", e))
})?;
}

Ok(list)
}
}

pub fn deserialize<'de, D, M, N>(
deserializer: D,
) -> Result<VariableList<FixedVector<u8, M>, N>, D::Error>
where
D: Deserializer<'de>,
M: Unsigned,
N: Unsigned,
{
deserializer.deserialize_seq(Visitor::default())
}
1 change: 1 addition & 0 deletions consensus/ssz_types/src/serde_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod hex_fixed_vec;
pub mod hex_var_list;
pub mod list_of_hex_fixed_vec;
pub mod list_of_hex_var_list;
pub mod quoted_u64_fixed_vec;
pub mod quoted_u64_var_list;
4 changes: 0 additions & 4 deletions consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub use process_operations::process_operations;
pub use verify_attestation::{
verify_attestation_for_block_inclusion, verify_attestation_for_state,
};
#[cfg(all(feature = "withdrawals", feature = "withdrawals-processing"))]
pub use verify_bls_to_execution_change::verify_bls_to_execution_change;
pub use verify_deposit::{
get_existing_validator_index, verify_deposit_merkle_proof, verify_deposit_signature,
Expand All @@ -36,13 +35,11 @@ pub mod signature_sets;
pub mod tests;
mod verify_attestation;
mod verify_attester_slashing;
#[cfg(all(feature = "withdrawals", feature = "withdrawals-processing"))]
mod verify_bls_to_execution_change;
mod verify_deposit;
mod verify_exit;
mod verify_proposer_slashing;

#[cfg(feature = "withdrawals-processing")]
use crate::common::decrease_balance;

#[cfg(feature = "arbitrary-fuzz")]
Expand Down Expand Up @@ -523,7 +520,6 @@ pub fn get_expected_withdrawals<T: EthSpec>(
}

/// FIXME: add link to this function once the spec is stable
#[cfg(all(feature = "withdrawals", feature = "withdrawals-processing"))]
pub fn process_withdrawals<'payload, T: EthSpec, Payload: AbstractExecPayload<T>>(
state: &mut BeaconState<T>,
payload: Payload::Ref<'payload>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ pub fn process_exits<T: EthSpec>(
///
/// Returns `Ok(())` if the validation and state updates completed successfully. Otherwise returs
/// an `Err` describing the invalid object or cause of failure.
#[cfg(all(feature = "withdrawals", feature = "withdrawals-processing"))]
pub fn process_bls_to_execution_changes<T: EthSpec>(
state: &mut BeaconState<T>,
bls_to_execution_changes: &[SignedBlsToExecutionChange],
Expand Down
1 change: 0 additions & 1 deletion crypto/kzg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ derivative = "2.1.1"
rand = "0.7.3"
serde = "1.0.116"
serde_derive = "1.0.116"
serde-big-array = {version = "0.3.2", features = ["const-generics"]}
eth2_serde_utils = "0.1.1"
hex = "0.4.2"
eth2_hashing = "0.3.0"
Expand Down
6 changes: 3 additions & 3 deletions lcli/src/parse_ssz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn run_parse_ssz<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
bytes
};

println!("Using {} spec", T::spec_name());
println!("Type: {:?}", type_str);
info!("Using {} spec", T::spec_name());
info!("Type: {:?}", type_str);

match type_str {
"signed_block_base" => decode_and_print::<SignedBeaconBlockBase<T>>(&bytes, format)?,
Expand All @@ -57,7 +57,7 @@ pub fn run_parse_ssz<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
"block_altair" => decode_and_print::<BeaconBlockAltair<T>>(&bytes, format)?,
"block_merge" => decode_and_print::<BeaconBlockMerge<T>>(&bytes, format)?,
"block_capella" => decode_and_print::<BeaconBlockCapella<T>>(&bytes, format)?,
"block_eip4844" => decode_and_print::<SignedBeaconBlockEip4844<T>>(&bytes, format)?,
"block_eip4844" => decode_and_print::<BeaconBlockEip4844<T>>(&bytes, format)?,
"state_base" => decode_and_print::<BeaconStateBase<T>>(&bytes, format)?,
"state_altair" => decode_and_print::<BeaconStateAltair<T>>(&bytes, format)?,
"state_merge" => decode_and_print::<BeaconStateMerge<T>>(&bytes, format)?,
Expand Down
8 changes: 8 additions & 0 deletions testing/ef_tests/src/cases/sanity_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ impl<E: EthSpec> Case for SanityBlocks<E> {
}

fn result(&self, _case_index: usize, fork_name: ForkName) -> Result<(), Error> {
if cfg!(feature = "withdrawals-processing") && fork_name == ForkName::Eip4844 {
return Ok(());
}

if !cfg!(feature = "withdrawals-processing") && fork_name == ForkName::Capella {
return Ok(());
}

self.metadata.bls_setting.unwrap_or_default().check()?;

let mut bulk_state = self.pre.clone();
Expand Down
16 changes: 12 additions & 4 deletions testing/ef_tests/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ impl<T, E> SszStaticHandler<T, E> {
Self::for_forks(vec![ForkName::Altair])
}

pub fn altair_and_later() -> Self {
Self::for_forks(ForkName::list_all()[1..].to_vec())
}

pub fn merge_only() -> Self {
Self::for_forks(vec![ForkName::Merge])
}
Expand All @@ -222,9 +218,21 @@ impl<T, E> SszStaticHandler<T, E> {
Self::for_forks(vec![ForkName::Capella])
}

pub fn eip4844_only() -> Self {
Self::for_forks(vec![ForkName::Eip4844])
}

pub fn altair_and_later() -> Self {
Self::for_forks(ForkName::list_all()[1..].to_vec())
}

pub fn merge_and_later() -> Self {
Self::for_forks(ForkName::list_all()[2..].to_vec())
}

pub fn capella_and_later() -> Self {
Self::for_forks(ForkName::list_all()[3..].to_vec())
}
}

/// Handler for SSZ types that implement `CachedTreeHash`.
Expand Down
5 changes: 5 additions & 0 deletions testing/ef_tests/src/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type_name_generic!(BeaconBlockBodyCapella, "BeaconBlockBody");
type_name_generic!(BeaconBlockBodyEip4844, "BeaconBlockBody");
type_name!(BeaconBlockHeader);
type_name_generic!(BeaconState);
type_name_generic!(BlobsSidecar);
type_name!(Checkpoint);
type_name_generic!(ContributionAndProof);
type_name!(Deposit);
Expand Down Expand Up @@ -86,4 +87,8 @@ type_name!(Validator);
type_name!(VoluntaryExit);
type_name!(Withdrawal);
type_name!(BlsToExecutionChange, "BLSToExecutionChange");
type_name_generic!(
SignedBeaconBlockAndBlobsSidecarDecode,
"SignedBeaconBlockAndBlobsSidecar"
);
type_name!(SignedBlsToExecutionChange, "SignedBLSToExecutionChange");
Loading

0 comments on commit f21d48c

Please sign in to comment.