Skip to content

Commit 06c46f4

Browse files
authored
[Altair] Applying Base block to Altair chain (#2353)
* Add hack to allow harness with altair spec * Add test for applying base block to altair chain * Add fork_name methods * Add fork checks and tests * Add tests for applying altair to base * Allow providing a custom spec to test harness * Fix compile error in network * Add tests for slot, epoch processing * Fix clippy lints * Fix release test * Fix test compile error * Fix test with inconsistent spec * Use fork_name_at_slot in genesis
1 parent 5cb0211 commit 06c46f4

File tree

37 files changed

+570
-45
lines changed

37 files changed

+570
-45
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
13581358
.collect::<Vec<_>>();
13591359

13601360
for (i, block) in chain_segment.into_iter().enumerate() {
1361+
// Ensure the block is the correct structure for the fork at `block.slot()`.
1362+
if let Err(e) = block.fork_name(&self.spec) {
1363+
return ChainSegmentResult::Failed {
1364+
imported_blocks,
1365+
error: BlockError::InconsistentFork(e),
1366+
};
1367+
}
1368+
13611369
let block_root = get_block_root(&block);
13621370

13631371
if let Some((child_parent_root, child_slot)) = children.get(i) {

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use store::{Error as DBError, HotColdDB, HotStateSummary, KeyValueStore, StoreOp
7272
use tree_hash::TreeHash;
7373
use types::{
7474
BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, CloneConfig, Epoch, EthSpec, Hash256,
75-
PublicKey, RelativeEpoch, SignedBeaconBlock, SignedBeaconBlockHeader, Slot,
75+
InconsistentFork, PublicKey, RelativeEpoch, SignedBeaconBlock, SignedBeaconBlockHeader, Slot,
7676
};
7777

7878
/// Maximum block slot number. Block with slots bigger than this constant will NOT be processed.
@@ -219,6 +219,12 @@ pub enum BlockError<T: EthSpec> {
219219
///
220220
/// The block is invalid and the peer is faulty.
221221
WeakSubjectivityConflict,
222+
/// The block has the wrong structure for the fork at `block.slot`.
223+
///
224+
/// ## Peer scoring
225+
///
226+
/// The block is invalid and the peer is faulty.
227+
InconsistentFork(InconsistentFork),
222228
}
223229

224230
impl<T: EthSpec> std::fmt::Display for BlockError<T> {
@@ -477,6 +483,11 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
477483
block: SignedBeaconBlock<T::EthSpec>,
478484
chain: &BeaconChain<T>,
479485
) -> Result<Self, BlockError<T::EthSpec>> {
486+
// Ensure the block is the correct structure for the fork at `block.slot()`.
487+
block
488+
.fork_name(&chain.spec)
489+
.map_err(BlockError::InconsistentFork)?;
490+
480491
// Do not gossip or process blocks from future slots.
481492
let present_slot_with_tolerance = chain
482493
.slot_clock
@@ -692,6 +703,11 @@ impl<T: BeaconChainTypes> SignatureVerifiedBlock<T> {
692703
block: SignedBeaconBlock<T::EthSpec>,
693704
chain: &BeaconChain<T>,
694705
) -> Result<Self, BlockError<T::EthSpec>> {
706+
// Ensure the block is the correct structure for the fork at `block.slot()`.
707+
block
708+
.fork_name(&chain.spec)
709+
.map_err(BlockError::InconsistentFork)?;
710+
695711
let (mut parent, block) = load_parent(block, chain)?;
696712

697713
// Reject any block that exceeds our limit on skipped slots.

beacon_node/beacon_chain/src/snapshot_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ mod test {
289289
fn get_harness() -> BeaconChainHarness<EphemeralHarnessType<MainnetEthSpec>> {
290290
let harness = BeaconChainHarness::new_with_store_config(
291291
MainnetEthSpec,
292+
None,
292293
types::test_utils::generate_deterministic_keypairs(1),
293294
StoreConfig::default(),
294295
);

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,35 +156,49 @@ pub type HarnessAttestations<E> = Vec<(
156156
)>;
157157

158158
impl<E: EthSpec> BeaconChainHarness<EphemeralHarnessType<E>> {
159-
pub fn new(eth_spec_instance: E, validator_keypairs: Vec<Keypair>) -> Self {
159+
pub fn new(
160+
eth_spec_instance: E,
161+
spec: Option<ChainSpec>,
162+
validator_keypairs: Vec<Keypair>,
163+
) -> Self {
160164
Self::new_with_store_config(
161165
eth_spec_instance,
166+
spec,
162167
validator_keypairs,
163168
StoreConfig::default(),
164169
)
165170
}
166171

167172
pub fn new_with_store_config(
168173
eth_spec_instance: E,
174+
spec: Option<ChainSpec>,
169175
validator_keypairs: Vec<Keypair>,
170176
config: StoreConfig,
171177
) -> Self {
172178
// Setting the target aggregators to really high means that _all_ validators in the
173179
// committee are required to produce an aggregate. This is overkill, however with small
174180
// validator counts it's the only way to be certain there is _at least one_ aggregator per
175181
// committee.
176-
Self::new_with_target_aggregators(eth_spec_instance, validator_keypairs, 1 << 32, config)
182+
Self::new_with_target_aggregators(
183+
eth_spec_instance,
184+
spec,
185+
validator_keypairs,
186+
1 << 32,
187+
config,
188+
)
177189
}
178190

179191
/// Instantiate a new harness with a custom `target_aggregators_per_committee` spec value
180192
pub fn new_with_target_aggregators(
181193
eth_spec_instance: E,
194+
spec: Option<ChainSpec>,
182195
validator_keypairs: Vec<Keypair>,
183196
target_aggregators_per_committee: u64,
184197
store_config: StoreConfig,
185198
) -> Self {
186199
Self::new_with_chain_config(
187200
eth_spec_instance,
201+
spec,
188202
validator_keypairs,
189203
target_aggregators_per_committee,
190204
store_config,
@@ -196,13 +210,14 @@ impl<E: EthSpec> BeaconChainHarness<EphemeralHarnessType<E>> {
196210
/// `target_aggregators_per_committee` spec value, and a `ChainConfig`
197211
pub fn new_with_chain_config(
198212
eth_spec_instance: E,
213+
spec: Option<ChainSpec>,
199214
validator_keypairs: Vec<Keypair>,
200215
target_aggregators_per_committee: u64,
201216
store_config: StoreConfig,
202217
chain_config: ChainConfig,
203218
) -> Self {
204219
let data_dir = tempdir().expect("should create temporary data_dir");
205-
let mut spec = test_spec::<E>();
220+
let mut spec = spec.unwrap_or_else(test_spec::<E>);
206221

207222
spec.target_aggregators_per_committee = target_aggregators_per_committee;
208223

@@ -250,11 +265,12 @@ impl<E: EthSpec> BeaconChainHarness<DiskHarnessType<E>> {
250265
/// Instantiate a new harness with `validator_count` initial validators.
251266
pub fn new_with_disk_store(
252267
eth_spec_instance: E,
268+
spec: Option<ChainSpec>,
253269
store: Arc<HotColdDB<E, LevelDB<E>, LevelDB<E>>>,
254270
validator_keypairs: Vec<Keypair>,
255271
) -> Self {
256272
let data_dir = tempdir().expect("should create temporary data_dir");
257-
let spec = test_spec::<E>();
273+
let spec = spec.unwrap_or_else(test_spec::<E>);
258274

259275
let log = test_logger();
260276
let (shutdown_tx, shutdown_receiver) = futures::channel::mpsc::channel(1);
@@ -294,11 +310,12 @@ impl<E: EthSpec> BeaconChainHarness<DiskHarnessType<E>> {
294310
/// Instantiate a new harness with `validator_count` initial validators.
295311
pub fn resume_from_disk_store(
296312
eth_spec_instance: E,
313+
spec: Option<ChainSpec>,
297314
store: Arc<HotColdDB<E, LevelDB<E>, LevelDB<E>>>,
298315
validator_keypairs: Vec<Keypair>,
299316
data_dir: TempDir,
300317
) -> Self {
301-
let spec = test_spec::<E>();
318+
let spec = spec.unwrap_or_else(test_spec::<E>);
302319

303320
let log = test_logger();
304321
let (shutdown_tx, shutdown_receiver) = futures::channel::mpsc::channel(1);

beacon_node/beacon_chain/src/validator_pubkey_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ mod test {
330330
fn get_state(validator_count: usize) -> (BeaconState<E>, Vec<Keypair>) {
331331
let harness = BeaconChainHarness::new_with_store_config(
332332
MainnetEthSpec,
333+
None,
333334
types::test_utils::generate_deterministic_keypairs(validator_count),
334335
StoreConfig::default(),
335336
);

beacon_node/beacon_chain/tests/attestation_production.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn produces_attestations() {
2727

2828
let harness = BeaconChainHarness::new_with_store_config(
2929
MainnetEthSpec,
30+
None,
3031
KEYPAIRS[..].to_vec(),
3132
StoreConfig::default(),
3233
);

beacon_node/beacon_chain/tests/attestation_verification.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lazy_static! {
3434
fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessType<E>> {
3535
let harness = BeaconChainHarness::new_with_target_aggregators(
3636
MainnetEthSpec,
37+
None,
3738
KEYPAIRS[0..validator_count].to_vec(),
3839
// A kind-of arbitrary number that ensures that _some_ validators are aggregators, but
3940
// not all.

0 commit comments

Comments
 (0)