mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2024-12-24 20:47:17 +00:00
Store beacon state committee cache in DB
This commit is contained in:
parent
f530f5a848
commit
67fdb4a7fb
@ -1,6 +1,8 @@
|
||||
use crate::*;
|
||||
use ssz::{Decode, Encode};
|
||||
|
||||
mod beacon_state;
|
||||
|
||||
impl StoreItem for BeaconBlock {
|
||||
fn db_column() -> DBColumn {
|
||||
DBColumn::BeaconBlock
|
||||
@ -14,17 +16,3 @@ impl StoreItem for BeaconBlock {
|
||||
Self::from_ssz_bytes(bytes).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> StoreItem for BeaconState<T> {
|
||||
fn db_column() -> DBColumn {
|
||||
DBColumn::BeaconState
|
||||
}
|
||||
|
||||
fn as_store_bytes(&self) -> Vec<u8> {
|
||||
self.as_ssz_bytes()
|
||||
}
|
||||
|
||||
fn from_store_bytes(bytes: &mut [u8]) -> Result<Self, Error> {
|
||||
Self::from_ssz_bytes(bytes).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
64
beacon_node/store/src/impls/beacon_state.rs
Normal file
64
beacon_node/store/src/impls/beacon_state.rs
Normal file
@ -0,0 +1,64 @@
|
||||
use crate::*;
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use std::convert::TryInto;
|
||||
use types::beacon_state::{CACHED_EPOCHS, CommitteeCache};
|
||||
|
||||
/// A container for storing `BeaconState` components.
|
||||
#[derive(Encode, Decode)]
|
||||
struct StorageContainer {
|
||||
state_bytes: Vec<u8>,
|
||||
committee_caches_bytes: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl StorageContainer {
|
||||
/// Create a new instance for storing a `BeaconState`.
|
||||
pub fn new<T: EthSpec>(state: &BeaconState<T>) -> Self {
|
||||
let mut committee_caches_bytes = vec![];
|
||||
|
||||
for cache in state.committee_caches[..].iter() {
|
||||
committee_caches_bytes.push(cache.as_ssz_bytes());
|
||||
}
|
||||
|
||||
Self {
|
||||
state_bytes: state.as_ssz_bytes(),
|
||||
committee_caches_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> TryInto<BeaconState<T>> for StorageContainer {
|
||||
type Error = Error;
|
||||
|
||||
fn try_into(self) -> Result<BeaconState<T>, Error> {
|
||||
let mut state: BeaconState<T> = BeaconState::from_ssz_bytes(&self.state_bytes)?;
|
||||
|
||||
for i in 0..CACHED_EPOCHS {
|
||||
let bytes = &self.committee_caches_bytes.get(i).ok_or_else(|| {
|
||||
Error::SszDecodeError(DecodeError::BytesInvalid(
|
||||
"Insufficient committees for BeaconState".to_string(),
|
||||
))
|
||||
})?;
|
||||
|
||||
state.committee_caches[i] = CommitteeCache::from_ssz_bytes(bytes)?;
|
||||
}
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> StoreItem for BeaconState<T> {
|
||||
fn db_column() -> DBColumn {
|
||||
DBColumn::BeaconState
|
||||
}
|
||||
|
||||
fn as_store_bytes(&self) -> Vec<u8> {
|
||||
let container = StorageContainer::new(self);
|
||||
container.as_ssz_bytes()
|
||||
}
|
||||
|
||||
fn from_store_bytes(bytes: &mut [u8]) -> Result<Self, Error> {
|
||||
let container = StorageContainer::from_ssz_bytes(bytes)?;
|
||||
container.try_into()
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use self::committee_cache::{get_active_validator_indices, CommitteeCache};
|
||||
use self::committee_cache::get_active_validator_indices;
|
||||
use self::exit_cache::ExitCache;
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
@ -15,6 +15,7 @@ use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
pub use self::committee_cache::CommitteeCache;
|
||||
pub use beacon_state_types::*;
|
||||
|
||||
mod beacon_state_types;
|
||||
|
@ -2,6 +2,7 @@ use super::BeaconState;
|
||||
use crate::*;
|
||||
use core::num::NonZeroUsize;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use std::ops::Range;
|
||||
use swap_or_not_shuffle::shuffle_list;
|
||||
|
||||
@ -9,7 +10,7 @@ mod tests;
|
||||
|
||||
/// Computes and stores the shuffling for an epoch. Provides various getters to allow callers to
|
||||
/// read the committees for the given epoch.
|
||||
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize, Encode, Decode)]
|
||||
pub struct CommitteeCache {
|
||||
initialized_epoch: Option<Epoch>,
|
||||
shuffling: Vec<usize>,
|
||||
|
Loading…
Reference in New Issue
Block a user