Fix the new BLS to execution change test

This commit is contained in:
Michael Sproul 2023-01-25 15:47:07 +11:00
parent 79a20e8a5f
commit e48487db01
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
8 changed files with 123 additions and 65 deletions

1
Cargo.lock generated
View File

@ -3215,6 +3215,7 @@ dependencies = [
"eth2_ssz",
"execution_layer",
"futures",
"genesis",
"hex",
"lazy_static",
"lighthouse_metrics",

View File

@ -172,17 +172,6 @@ impl<E: EthSpec> Builder<EphemeralHarnessType<E>> {
.clone()
.expect("cannot build without validator keypairs");
// For the interop genesis state we know that the withdrawal credentials are set equal
// to the validator keypairs. Check for any manually initialised credentials.
assert!(
self.withdrawal_keypairs.is_empty(),
"withdrawal credentials are ignored by fresh_ephemeral_store"
);
self.withdrawal_keypairs = validator_keypairs
.iter()
.map(|kp| Some(kp.clone()))
.collect();
let store = Arc::new(
HotColdDB::open_ephemeral(
self.store_config.clone().unwrap_or_default(),
@ -321,6 +310,11 @@ where
self
}
pub fn withdrawal_keypairs(mut self, withdrawal_keypairs: Vec<Option<Keypair>>) -> Self {
self.withdrawal_keypairs = withdrawal_keypairs;
self
}
pub fn default_spec(self) -> Self {
self.spec_or_default(None)
}

View File

@ -10,7 +10,7 @@ use types::{
pub const DEFAULT_ETH1_BLOCK_HASH: &[u8] = &[0x42; 32];
fn bls_withdrawal_credentials(pubkey: &PublicKey, spec: &ChainSpec) -> Hash256 {
pub fn bls_withdrawal_credentials(pubkey: &PublicKey, spec: &ChainSpec) -> Hash256 {
let mut credentials = hash(&pubkey.as_ssz_bytes());
credentials[0] = spec.bls_withdrawal_prefix_byte;
Hash256::from_slice(&credentials)
@ -35,42 +35,18 @@ pub fn interop_genesis_state<T: EthSpec>(
execution_payload_header: Option<ExecutionPayloadHeader<T>>,
spec: &ChainSpec,
) -> Result<BeaconState<T>, String> {
let eth1_timestamp = 2_u64.pow(40);
let amount = spec.max_effective_balance;
let datas = keypairs
.into_par_iter()
.map(|keypair| {
let mut data = DepositData {
withdrawal_credentials: bls_withdrawal_credentials(&keypair.pk, spec),
pubkey: keypair.pk.clone().into(),
amount,
signature: Signature::empty().into(),
};
data.signature = data.create_signature(&keypair.sk, spec);
data
})
let withdrawal_credentials = keypairs
.iter()
.map(|keypair| bls_withdrawal_credentials(&keypair.pk, spec))
.collect::<Vec<_>>();
let mut state = initialize_beacon_state_from_eth1(
interop_genesis_state_with_withdrawal_credentials::<T>(
keypairs,
&withdrawal_credentials,
genesis_time,
eth1_block_hash,
eth1_timestamp,
genesis_deposits(datas, spec)?,
execution_payload_header,
spec,
)
.map_err(|e| format!("Unable to initialize genesis state: {:?}", e))?;
*state.genesis_time_mut() = genesis_time;
// Invalidate all the caches after all the manual state surgery.
state
.drop_all_caches()
.map_err(|e| format!("Unable to drop caches: {:?}", e))?;
Ok(state)
}
// returns an interop genesis state except every other
@ -82,23 +58,52 @@ pub fn interop_genesis_state_with_eth1<T: EthSpec>(
execution_payload_header: Option<ExecutionPayloadHeader<T>>,
spec: &ChainSpec,
) -> Result<BeaconState<T>, String> {
let withdrawal_credentials = keypairs
.iter()
.enumerate()
.map(|(index, keypair)| {
if index % 2 == 0 {
bls_withdrawal_credentials(&keypair.pk, spec)
} else {
eth1_withdrawal_credentials(&keypair.pk, spec)
}
})
.collect::<Vec<_>>();
interop_genesis_state_with_withdrawal_credentials::<T>(
keypairs,
&withdrawal_credentials,
genesis_time,
eth1_block_hash,
execution_payload_header,
spec,
)
}
pub fn interop_genesis_state_with_withdrawal_credentials<T: EthSpec>(
keypairs: &[Keypair],
withdrawal_credentials: &[Hash256],
genesis_time: u64,
eth1_block_hash: Hash256,
execution_payload_header: Option<ExecutionPayloadHeader<T>>,
spec: &ChainSpec,
) -> Result<BeaconState<T>, String> {
if keypairs.len() != withdrawal_credentials.len() {
return Err(format!(
"wrong number of withdrawal credentials, expected: {}, got: {}",
keypairs.len(),
withdrawal_credentials.len()
));
}
let eth1_timestamp = 2_u64.pow(40);
let amount = spec.max_effective_balance;
let withdrawal_credentials = |index: usize, pubkey: &PublicKey| {
if index % 2 == 0 {
bls_withdrawal_credentials(pubkey, spec)
} else {
eth1_withdrawal_credentials(pubkey, spec)
}
};
let datas = keypairs
.into_par_iter()
.enumerate()
.map(|(index, keypair)| {
.zip(withdrawal_credentials.into_par_iter())
.map(|(keypair, &withdrawal_credentials)| {
let mut data = DepositData {
withdrawal_credentials: withdrawal_credentials(index, &keypair.pk),
withdrawal_credentials,
pubkey: keypair.pk.clone().into(),
amount,
signature: Signature::empty().into(),

View File

@ -6,6 +6,7 @@ pub use eth1::Config as Eth1Config;
pub use eth1::Eth1Endpoint;
pub use eth1_genesis_service::{Eth1GenesisService, Statistics};
pub use interop::{
interop_genesis_state, interop_genesis_state_with_eth1, DEFAULT_ETH1_BLOCK_HASH,
bls_withdrawal_credentials, interop_genesis_state, interop_genesis_state_with_eth1,
interop_genesis_state_with_withdrawal_credentials, DEFAULT_ETH1_BLOCK_HASH,
};
pub use types::test_utils::generate_deterministic_keypairs;

View File

@ -45,6 +45,7 @@ logging = { path = "../../common/logging" }
serde_json = "1.0.58"
proto_array = { path = "../../consensus/proto_array" }
unused_port = {path = "../../common/unused_port"}
genesis = { path = "../genesis" }
[[test]]
name = "bn_http_api_tests"

View File

@ -1,5 +1,7 @@
use beacon_chain::{
test_utils::{BeaconChainHarness, BoxedMutator, EphemeralHarnessType},
test_utils::{
BeaconChainHarness, BoxedMutator, Builder as HarnessBuilder, EphemeralHarnessType,
},
BeaconChain, BeaconChainTypes,
};
use directory::DEFAULT_ROOT_DIR;
@ -55,25 +57,39 @@ pub struct ApiServer<E: EthSpec, SFut: Future<Output = ()>> {
pub external_peer_id: PeerId,
}
type Initializer<E> = Box<
dyn FnOnce(HarnessBuilder<EphemeralHarnessType<E>>) -> HarnessBuilder<EphemeralHarnessType<E>>,
>;
type Mutator<E> = BoxedMutator<E, MemoryStore<E>, MemoryStore<E>>;
impl<E: EthSpec> InteractiveTester<E> {
pub async fn new(spec: Option<ChainSpec>, validator_count: usize) -> Self {
Self::new_with_mutator(spec, validator_count, None).await
Self::new_with_initializer_and_mutator(spec, validator_count, None, None).await
}
pub async fn new_with_mutator(
pub async fn new_with_initializer_and_mutator(
spec: Option<ChainSpec>,
validator_count: usize,
initializer: Option<Initializer<E>>,
mutator: Option<Mutator<E>>,
) -> Self {
let mut harness_builder = BeaconChainHarness::builder(E::default())
.spec_or_default(spec)
.deterministic_keypairs(validator_count)
.logger(test_logger())
.mock_execution_layer()
.fresh_ephemeral_store();
.mock_execution_layer();
harness_builder = if let Some(initializer) = initializer {
// Apply custom initialization provided by the caller.
initializer(harness_builder)
} else {
// Apply default initial configuration.
harness_builder
.deterministic_keypairs(validator_count)
.fresh_ephemeral_store()
};
// Add a mutator for the beacon chain builder which will be called in
// `HarnessBuilder::build`.
if let Some(mutator) = mutator {
harness_builder = harness_builder.initial_mutator(mutator);
}

View File

@ -1,8 +1,15 @@
//! Tests for API behaviour across fork boundaries.
use crate::common::*;
use beacon_chain::{test_utils::RelativeSyncCommittee, StateSkipConfig};
use beacon_chain::{
test_utils::{RelativeSyncCommittee, DEFAULT_ETH1_BLOCK_HASH, HARNESS_GENESIS_TIME},
StateSkipConfig,
};
use eth2::types::{IndexedErrorMessage, StateId, SyncSubcommittee};
use types::{Address, ChainSpec, Epoch, EthSpec, MinimalEthSpec, Slot};
use genesis::{bls_withdrawal_credentials, interop_genesis_state_with_withdrawal_credentials};
use types::{
test_utils::{generate_deterministic_keypair, generate_deterministic_keypairs},
Address, ChainSpec, Epoch, EthSpec, Hash256, MinimalEthSpec, Slot,
};
type E = MinimalEthSpec;
@ -338,7 +345,39 @@ async fn bls_to_execution_changes_update_all_around_capella_fork() {
let fork_epoch = Epoch::new(2);
let spec = capella_spec(fork_epoch);
let max_bls_to_execution_changes = E::max_bls_to_execution_changes();
let tester = InteractiveTester::<E>::new(Some(spec.clone()), validator_count).await;
// Use a genesis state with entirely BLS withdrawal credentials.
// Offset keypairs by `validator_count` to create keys distinct from the signing keys.
let validator_keypairs = generate_deterministic_keypairs(validator_count);
let withdrawal_keypairs = (0..validator_count)
.map(|i| Some(generate_deterministic_keypair(i + validator_count)))
.collect::<Vec<_>>();
let withdrawal_credentials = withdrawal_keypairs
.iter()
.map(|keypair| bls_withdrawal_credentials(&keypair.as_ref().unwrap().pk, &spec))
.collect::<Vec<_>>();
let genesis_state = interop_genesis_state_with_withdrawal_credentials(
&validator_keypairs,
&withdrawal_credentials,
HARNESS_GENESIS_TIME,
Hash256::from_slice(DEFAULT_ETH1_BLOCK_HASH),
None,
&spec,
)
.unwrap();
let tester = InteractiveTester::<E>::new_with_initializer_and_mutator(
Some(spec.clone()),
validator_count,
Some(Box::new(|harness_builder| {
harness_builder
.keypairs(validator_keypairs)
.withdrawal_keypairs(withdrawal_keypairs)
.genesis_state_ephemeral_store(genesis_state)
})),
None,
)
.await;
let harness = &tester.harness;
let client = &tester.client;

View File

@ -278,9 +278,10 @@ pub async fn proposer_boost_re_org_test(
let num_empty_votes = Some(attesters_per_slot * percent_empty_votes / 100);
let num_head_votes = Some(attesters_per_slot * percent_head_votes / 100);
let tester = InteractiveTester::<E>::new_with_mutator(
let tester = InteractiveTester::<E>::new_with_initializer_and_mutator(
Some(spec),
validator_count,
None,
Some(Box::new(move |builder| {
builder
.proposer_re_org_threshold(Some(ReOrgThreshold(re_org_threshold)))