Skip to content

Commit

Permalink
Fix some compilation errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Sep 11, 2023
1 parent 6771954 commit a19fb23
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 39 deletions.
8 changes: 4 additions & 4 deletions beacon_node/http_api/tests/broadcast_validation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,19 +912,19 @@ pub async fn blinded_gossip_full_pass_ssz() {
let slot_b = slot_a + 1;

let state_a = tester.harness.get_current_state();
let ((block, _), _): ((SignedBlindedBeaconBlock<E>, _), _) =
tester.harness.make_blinded_block(state_a, slot_b).await;
let (block_contents_tuple, _) = tester.harness.make_blinded_block(state_a, slot_b).await;
let block_contents = block_contents_tuple.into();

let response: Result<(), eth2::Error> = tester
.client
.post_beacon_blinded_blocks_v2_ssz(&block, validation_level)
.post_beacon_blinded_blocks_v2_ssz(&block_contents, validation_level)
.await;

assert!(response.is_ok());
assert!(tester
.harness
.chain
.block_is_known_to_fork_choice(&block.canonical_root()));
.block_is_known_to_fork_choice(&block_contents.signed_block().canonical_root()));
}

/// This test checks that a block that is **invalid** from a gossip perspective gets rejected when using `broadcast_validation=consensus`.
Expand Down
9 changes: 4 additions & 5 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2715,16 +2715,15 @@ impl ApiTester {
sk.sign(message).into()
};

let block = self
let block_contents = self
.client
.get_validator_blinded_blocks::<E, Payload>(slot, &randao_reveal, None)
.await
.unwrap()
.data
.deconstruct()
.0;
.data;

let signed_block = block.sign(&sk, &fork, genesis_validators_root, &self.chain.spec);
let signed_block =
block_contents.sign(&sk, &fork, genesis_validators_root, &self.chain.spec);

self.client
.post_beacon_blinded_blocks_ssz(&signed_block)
Expand Down
2 changes: 1 addition & 1 deletion common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ impl BeaconNodeHttpClient {
/// Returns `Ok(None)` on a 404 error.
pub async fn post_beacon_blinded_blocks_ssz<T: EthSpec, Payload: AbstractExecPayload<T>>(
&self,
block: &SignedBlindedBlockContents<T>,
block: &SignedBlockContents<T, Payload>,
) -> Result<(), Error> {
let mut path = self.eth_path(V1)?;

Expand Down
20 changes: 20 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,26 @@ impl<T: EthSpec, Payload: AbstractExecPayload<T>> BlockContents<T, Payload> {
BlockContents::Block(block) => (block, None),
}
}

/// Signs `self`, producing a `SignedBlockContents`.
pub fn sign(
self,
secret_key: &SecretKey,
fork: &Fork,
genesis_validators_root: Hash256,
spec: &ChainSpec,
) -> SignedBlockContents<T, Payload> {
let (block, maybe_blobs) = self.deconstruct();
let signed_block = block.sign(secret_key, fork, genesis_validators_root, spec);
let signed_blobs = maybe_blobs.map(|blobs| {
blobs
.into_iter()
.map(|blob| blob.sign(secret_key, fork, genesis_validators_root, spec))
.collect::<Vec<_>>()
.into()
});
SignedBlockContents::new(signed_block, signed_blobs)
}
}

impl<T: EthSpec, Payload: AbstractExecPayload<T>> ForkVersionDeserialize
Expand Down
29 changes: 1 addition & 28 deletions consensus/types/src/blob_sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::test_utils::TestRandom;
use crate::{Blob, ChainSpec, Domain, EthSpec, Fork, Hash256, SignedBlobSidecar, SignedRoot, Slot};
use bls::SecretKey;
use crate::{Blob, EthSpec, Hash256, SignedRoot, Slot};
use derivative::Derivative;
use kzg::{Kzg, KzgCommitment, KzgPreset, KzgProof};
use rand::Rng;
Expand All @@ -10,7 +9,6 @@ use ssz_derive::{Decode, Encode};
use ssz_types::{FixedVector, VariableList};
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
use std::sync::Arc;
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
Expand Down Expand Up @@ -171,31 +169,6 @@ impl<T: EthSpec> BlobSidecar<T> {
// Fixed part
Self::empty().as_ssz_bytes().len()
}

// this is mostly not used except for in testing
pub fn sign(
self: Arc<Self>,
secret_key: &SecretKey,
fork: &Fork,
genesis_validators_root: Hash256,
spec: &ChainSpec,
) -> SignedBlobSidecar<T> {
let signing_epoch = self.slot.epoch(T::slots_per_epoch());
let domain = spec.get_domain(
signing_epoch,
Domain::BlobSidecar,
fork,
genesis_validators_root,
);
let message = self.signing_root(domain);
let signature = secret_key.sign(message);

SignedBlobSidecar {
message: self,
signature,
_phantom: PhantomData,
}
}
}

#[derive(
Expand Down
32 changes: 31 additions & 1 deletion consensus/types/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use crate::beacon_block_body::KzgCommitments;
use crate::test_utils::TestRandom;
use crate::{
AbstractExecPayload, BeaconBlock, BlindedBlobSidecar, BlindedBlobSidecarList, BlobRootsList,
BlobSidecar, BlobSidecarList, BlobsList, EthSpec, SidecarList, SignedRoot, Slot,
BlobSidecar, BlobSidecarList, BlobsList, ChainSpec, Domain, EthSpec, Fork, Hash256,
SidecarList, SignedRoot, SignedSidecar, Slot,
};
use bls::SecretKey;
use kzg::KzgProof;
use serde::de::DeserializeOwned;
use ssz::{Decode, Encode};
use ssz_types::VariableList;
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
use std::sync::Arc;
use tree_hash::TreeHash;

Expand All @@ -29,13 +32,40 @@ pub trait Sidecar<E: EthSpec>:
+ for<'a> arbitrary::Arbitrary<'a>
{
type BlobItems: BlobItems<E>;

fn slot(&self) -> Slot;

fn build_sidecar<Payload: AbstractExecPayload<E>>(
blob_items: Self::BlobItems,
block: &BeaconBlock<E, Payload>,
expected_kzg_commitments: &KzgCommitments<E>,
kzg_proofs: Vec<KzgProof>,
) -> Result<SidecarList<E, Self>, String>;

// this is mostly not used except for in testing
fn sign(
self: Arc<Self>,
secret_key: &SecretKey,
fork: &Fork,
genesis_validators_root: Hash256,
spec: &ChainSpec,
) -> SignedSidecar<E, Self> {
let signing_epoch = self.slot().epoch(E::slots_per_epoch());
let domain = spec.get_domain(
signing_epoch,
Domain::BlobSidecar,
fork,
genesis_validators_root,
);
let message = self.signing_root(domain);
let signature = secret_key.sign(message);

SignedSidecar {
message: self,
signature,
_phantom: PhantomData,
}
}
}

pub trait BlobItems<T: EthSpec>: Sync + Send + Sized {
Expand Down

0 comments on commit a19fb23

Please sign in to comment.