Skip to content

Commit 6da6453

Browse files
michaelsproulpaulhauner
authored andcommitted
Fix consensus, SSZ, tree hash & run merge EF tests (#2622)
* Update to v1.1.0-beta.4 (squash of #2548) * SSZ, cached tree hash, EF tests
1 parent 3edb900 commit 6da6453

File tree

23 files changed

+309
-202
lines changed

23 files changed

+309
-202
lines changed

beacon_node/store/src/partial_beacon_state.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,17 @@ impl<T: EthSpec> PartialBeaconState<T> {
197197
let epoch = slot.epoch(T::slots_per_epoch());
198198

199199
if spec
200+
.merge_fork_epoch
201+
.map_or(false, |merge_epoch| epoch >= merge_epoch)
202+
{
203+
PartialBeaconStateMerge::from_ssz_bytes(bytes).map(Self::Merge)
204+
} else if spec
200205
.altair_fork_epoch
201-
.map_or(true, |altair_epoch| epoch < altair_epoch)
206+
.map_or(false, |altair_epoch| epoch >= altair_epoch)
202207
{
203-
PartialBeaconStateBase::from_ssz_bytes(bytes).map(Self::Base)
204-
} else {
205208
PartialBeaconStateAltair::from_ssz_bytes(bytes).map(Self::Altair)
209+
} else {
210+
PartialBeaconStateBase::from_ssz_bytes(bytes).map(Self::Base)
206211
}
207212
}
208213

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::FixedVector;
2+
use eth2_serde_utils::hex::{self, PrefixedHexVisitor};
3+
use serde::{Deserializer, Serializer};
4+
use typenum::Unsigned;
5+
6+
pub fn serialize<S, U>(bytes: &FixedVector<u8, U>, serializer: S) -> Result<S::Ok, S::Error>
7+
where
8+
S: Serializer,
9+
U: Unsigned,
10+
{
11+
let mut hex_string: String = "0x".to_string();
12+
hex_string.push_str(&hex::encode(&bytes[..]));
13+
14+
serializer.serialize_str(&hex_string)
15+
}
16+
17+
pub fn deserialize<'de, D, U>(deserializer: D) -> Result<FixedVector<u8, U>, D::Error>
18+
where
19+
D: Deserializer<'de>,
20+
U: Unsigned,
21+
{
22+
let vec = deserializer.deserialize_string(PrefixedHexVisitor)?;
23+
FixedVector::new(vec)
24+
.map_err(|e| serde::de::Error::custom(format!("invalid fixed vector: {:?}", e)))
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Serialize `VariableList<u8, N>` as 0x-prefixed hex string.
2+
use crate::VariableList;
3+
use eth2_serde_utils::hex::{self, PrefixedHexVisitor};
4+
use serde::{Deserializer, Serializer};
5+
use typenum::Unsigned;
6+
7+
pub fn serialize<S, N>(bytes: &VariableList<u8, N>, serializer: S) -> Result<S::Ok, S::Error>
8+
where
9+
S: Serializer,
10+
N: Unsigned,
11+
{
12+
let mut hex_string: String = "0x".to_string();
13+
hex_string.push_str(&hex::encode(&**bytes));
14+
15+
serializer.serialize_str(&hex_string)
16+
}
17+
18+
pub fn deserialize<'de, D, N>(deserializer: D) -> Result<VariableList<u8, N>, D::Error>
19+
where
20+
D: Deserializer<'de>,
21+
N: Unsigned,
22+
{
23+
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
24+
VariableList::new(bytes)
25+
.map_err(|e| serde::de::Error::custom(format!("invalid variable list: {:?}", e)))
26+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
pub mod hex_fixed_vec;
2+
pub mod hex_var_list;
13
pub mod quoted_u64_fixed_vec;
24
pub mod quoted_u64_var_list;

consensus/state_processing/src/per_block_processing.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,22 @@ pub fn per_block_processing<T: EthSpec>(
131131
process_eth1_data(state, block.body().eth1_data())?;
132132
process_operations(state, block.body(), proposer_index, verify_signatures, spec)?;
133133

134-
if let BeaconBlockRef::Altair(inner) = block {
134+
if let Some(sync_aggregate) = block.body().sync_aggregate() {
135135
process_sync_aggregate(
136136
state,
137-
&inner.body.sync_aggregate,
137+
sync_aggregate,
138138
proposer_index,
139139
verify_signatures,
140140
spec,
141141
)?;
142142
}
143143

144144
if is_execution_enabled(state, block.body()) {
145-
process_execution_payload(state, block.body().execution_payload().unwrap(), spec)?
145+
let payload = block
146+
.body()
147+
.execution_payload()
148+
.ok_or(BlockProcessingError::IncorrectStateType)?;
149+
process_execution_payload(state, payload, spec)?;
146150
}
147151

148152
Ok(())

consensus/state_processing/src/per_block_processing/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub enum BlockProcessingError {
7777
expected: u64,
7878
found: u64,
7979
},
80+
ExecutionInvalid,
8081
}
8182

8283
impl From<BeaconStateError> for BlockProcessingError {

consensus/state_processing/src/per_block_processing/process_operations.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,15 @@ pub fn process_deposit<T: EthSpec>(
353353
state.validators_mut().push(validator)?;
354354
state.balances_mut().push(deposit.data.amount)?;
355355

356-
// Altair-specific initializations.
357-
if let BeaconState::Altair(altair_state) = state {
358-
altair_state
359-
.previous_epoch_participation
360-
.push(ParticipationFlags::default())?;
361-
altair_state
362-
.current_epoch_participation
363-
.push(ParticipationFlags::default())?;
364-
altair_state.inactivity_scores.push(0)?;
356+
// Altair or later initializations.
357+
if let Ok(previous_epoch_participation) = state.previous_epoch_participation_mut() {
358+
previous_epoch_participation.push(ParticipationFlags::default())?;
359+
}
360+
if let Ok(current_epoch_participation) = state.current_epoch_participation_mut() {
361+
current_epoch_participation.push(ParticipationFlags::default())?;
362+
}
363+
if let Ok(inactivity_scores) = state.inactivity_scores_mut() {
364+
inactivity_scores.push(0)?;
365365
}
366366
}
367367

consensus/types/src/beacon_block.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,17 @@ impl<T: EthSpec> BeaconBlock<T> {
8989
let epoch = slot.epoch(T::slots_per_epoch());
9090

9191
if spec
92+
.merge_fork_epoch
93+
.map_or(false, |merge_epoch| epoch >= merge_epoch)
94+
{
95+
BeaconBlockMerge::from_ssz_bytes(bytes).map(Self::Merge)
96+
} else if spec
9297
.altair_fork_epoch
93-
.map_or(true, |altair_epoch| epoch < altair_epoch)
98+
.map_or(false, |altair_epoch| epoch >= altair_epoch)
9499
{
95-
BeaconBlockBase::from_ssz_bytes(bytes).map(Self::Base)
96-
} else {
97100
BeaconBlockAltair::from_ssz_bytes(bytes).map(Self::Altair)
101+
} else {
102+
BeaconBlockBase::from_ssz_bytes(bytes).map(Self::Base)
98103
}
99104
}
100105

@@ -104,9 +109,13 @@ impl<T: EthSpec> BeaconBlock<T> {
104109
/// Usually it's better to prefer `from_ssz_bytes` which will decode the correct variant based
105110
/// on the fork slot.
106111
pub fn any_from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
107-
BeaconBlockAltair::from_ssz_bytes(bytes)
108-
.map(BeaconBlock::Altair)
109-
.or_else(|_| BeaconBlockBase::from_ssz_bytes(bytes).map(BeaconBlock::Base))
112+
BeaconBlockMerge::from_ssz_bytes(bytes)
113+
.map(BeaconBlock::Merge)
114+
.or_else(|_| {
115+
BeaconBlockAltair::from_ssz_bytes(bytes)
116+
.map(BeaconBlock::Altair)
117+
.or_else(|_| BeaconBlockBase::from_ssz_bytes(bytes).map(BeaconBlock::Base))
118+
})
110119
}
111120

112121
/// Convenience accessor for the `body` as a `BeaconBlockBodyRef`.

consensus/types/src/beacon_state.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,17 @@ impl<T: EthSpec> BeaconState<T> {
418418
let epoch = slot.epoch(T::slots_per_epoch());
419419

420420
if spec
421+
.merge_fork_epoch
422+
.map_or(false, |merge_epoch| epoch >= merge_epoch)
423+
{
424+
BeaconStateMerge::from_ssz_bytes(bytes).map(Self::Merge)
425+
} else if spec
421426
.altair_fork_epoch
422-
.map_or(true, |altair_epoch| epoch < altair_epoch)
427+
.map_or(false, |altair_epoch| epoch >= altair_epoch)
423428
{
424-
BeaconStateBase::from_ssz_bytes(bytes).map(Self::Base)
425-
} else {
426429
BeaconStateAltair::from_ssz_bytes(bytes).map(Self::Altair)
430+
} else {
431+
BeaconStateBase::from_ssz_bytes(bytes).map(Self::Base)
427432
}
428433
}
429434

@@ -1686,7 +1691,8 @@ impl<T: EthSpec> CompareFields for BeaconState<T> {
16861691
match (self, other) {
16871692
(BeaconState::Base(x), BeaconState::Base(y)) => x.compare_fields(y),
16881693
(BeaconState::Altair(x), BeaconState::Altair(y)) => x.compare_fields(y),
1689-
_ => panic!("compare_fields: mismatched state variants"),
1694+
(BeaconState::Merge(x), BeaconState::Merge(y)) => x.compare_fields(y),
1695+
_ => panic!("compare_fields: mismatched state variants",),
16901696
}
16911697
}
16921698
}

consensus/types/src/beacon_state/tree_hash_cache.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,26 @@ impl<T: EthSpec> BeaconTreeHashCacheInner<T> {
341341
)?;
342342
hasher.write(state.finalized_checkpoint().tree_hash_root().as_bytes())?;
343343

344-
// Inactivity & light-client sync committees
345-
if let BeaconState::Altair(ref state) = state {
344+
// Inactivity & light-client sync committees (Altair and later).
345+
if let Ok(inactivity_scores) = state.inactivity_scores() {
346346
hasher.write(
347347
self.inactivity_scores
348-
.recalculate_tree_hash_root(&state.inactivity_scores)?
348+
.recalculate_tree_hash_root(inactivity_scores)?
349349
.as_bytes(),
350350
)?;
351+
}
352+
353+
if let Ok(current_sync_committee) = state.current_sync_committee() {
354+
hasher.write(current_sync_committee.tree_hash_root().as_bytes())?;
355+
}
356+
357+
if let Ok(next_sync_committee) = state.next_sync_committee() {
358+
hasher.write(next_sync_committee.tree_hash_root().as_bytes())?;
359+
}
351360

352-
hasher.write(state.current_sync_committee.tree_hash_root().as_bytes())?;
353-
hasher.write(state.next_sync_committee.tree_hash_root().as_bytes())?;
361+
// Execution payload (merge and later).
362+
if let Ok(payload_header) = state.latest_execution_payload_header() {
363+
hasher.write(payload_header.tree_hash_root().as_bytes())?;
354364
}
355365

356366
let root = hasher.finish()?;

0 commit comments

Comments
 (0)