Store execution block hash in fork choice (#2643)

* - Update the fork choice `ProtoNode` to include `is_merge_complete`
- Add database migration for the persisted fork choice

* update tests

* Small cleanup

* lints

* store execution block hash in fork choice rather than bool
This commit is contained in:
realbigsean
2021-09-28 05:56:49 -04:00
committed by Paul Hauner
parent fd828199f5
commit aa534f8989
9 changed files with 148 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
use crate::error::Error;
use crate::proto_array::ProtoArray;
use crate::ssz_container::SszContainer;
use crate::ssz_container::{LegacySszContainer, SszContainer};
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
@@ -29,6 +29,7 @@ pub struct Block {
pub next_epoch_shuffling_id: AttestationShufflingId,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub execution_block_hash: Hash256,
}
/// A Vec-wrapper which will grow to match any request.
@@ -66,6 +67,7 @@ pub struct ProtoArrayForkChoice {
}
impl ProtoArrayForkChoice {
#[allow(clippy::too_many_arguments)]
pub fn new(
finalized_block_slot: Slot,
finalized_block_state_root: Hash256,
@@ -74,6 +76,7 @@ impl ProtoArrayForkChoice {
finalized_root: Hash256,
current_epoch_shuffling_id: AttestationShufflingId,
next_epoch_shuffling_id: AttestationShufflingId,
execution_block_hash: Hash256,
) -> Result<Self, String> {
let mut proto_array = ProtoArray {
prune_threshold: DEFAULT_PRUNE_THRESHOLD,
@@ -95,6 +98,7 @@ impl ProtoArrayForkChoice {
next_epoch_shuffling_id,
justified_epoch,
finalized_epoch,
execution_block_hash,
};
proto_array
@@ -204,6 +208,7 @@ impl ProtoArrayForkChoice {
next_epoch_shuffling_id: block.next_epoch_shuffling_id.clone(),
justified_epoch: block.justified_epoch,
finalized_epoch: block.finalized_epoch,
execution_block_hash: block.execution_block_hash,
})
}
@@ -252,6 +257,22 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("Failed to decode ProtoArrayForkChoice: {:?}", e))
}
/// Only used for SSZ deserialization of the persisted fork choice during the database migration
/// from schema 4 to schema 5.
pub fn from_bytes_legacy(bytes: &[u8]) -> Result<Self, String> {
LegacySszContainer::from_ssz_bytes(bytes)
.map(|legacy_container| {
let container: SszContainer = legacy_container.into();
container.into()
})
.map_err(|e| {
format!(
"Failed to decode ProtoArrayForkChoice during schema migration: {:?}",
e
)
})
}
/// Returns a read-lock to core `ProtoArray` struct.
///
/// Should only be used when encoding/decoding during troubleshooting.
@@ -351,6 +372,7 @@ mod test_compute_deltas {
let unknown = Hash256::from_low_u64_be(4);
let junk_shuffling_id =
AttestationShufflingId::from_components(Epoch::new(0), Hash256::zero());
let execution_block_hash = Hash256::zero();
let mut fc = ProtoArrayForkChoice::new(
genesis_slot,
@@ -360,6 +382,7 @@ mod test_compute_deltas {
finalized_root,
junk_shuffling_id.clone(),
junk_shuffling_id.clone(),
execution_block_hash,
)
.unwrap();
@@ -375,6 +398,7 @@ mod test_compute_deltas {
next_epoch_shuffling_id: junk_shuffling_id.clone(),
justified_epoch: genesis_epoch,
finalized_epoch: genesis_epoch,
execution_block_hash,
})
.unwrap();
@@ -390,6 +414,7 @@ mod test_compute_deltas {
next_epoch_shuffling_id: junk_shuffling_id,
justified_epoch: genesis_epoch,
finalized_epoch: genesis_epoch,
execution_block_hash,
})
.unwrap();