Merge branch 'master' into disk-db

This commit is contained in:
Paul Hauner
2019-05-20 16:56:04 +10:00
203 changed files with 3893 additions and 19921 deletions

View File

@@ -99,7 +99,7 @@ impl ClientDB for DiskDB {
None => Err(DBError {
message: "Unknown column".to_string(),
}),
Some(handle) => self.db.put_cf(handle, key, val).map_err(|e| e.into()),
Some(handle) => self.db.put_cf(handle, key, val).map_err(Into::into),
}
}

View File

@@ -1,6 +1,6 @@
use super::BLOCKS_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError};
use ssz::decode;
use ssz::Decode;
use std::sync::Arc;
use types::{BeaconBlock, Hash256, Slot};
@@ -30,7 +30,7 @@ impl<T: ClientDB> BeaconBlockStore<T> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let block = decode::<BeaconBlock>(&ssz).map_err(|_| DBError {
let block = BeaconBlock::from_ssz_bytes(&ssz).map_err(|_| DBError {
message: "Bad BeaconBlock SSZ.".to_string(),
})?;
Ok(Some(block))

View File

@@ -1,8 +1,8 @@
use super::STATES_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError};
use ssz::decode;
use ssz::Decode;
use std::sync::Arc;
use types::{BeaconState, Hash256};
use types::{BeaconState, EthSpec, Hash256};
pub struct BeaconStateStore<T>
where
@@ -19,11 +19,14 @@ impl<T: ClientDB> BeaconStateStore<T> {
Self { db }
}
pub fn get_deserialized(&self, hash: &Hash256) -> Result<Option<BeaconState>, DBError> {
pub fn get_deserialized<E: EthSpec>(
&self,
hash: &Hash256,
) -> Result<Option<BeaconState<E>>, DBError> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let state = decode::<BeaconState>(&ssz).map_err(|_| DBError {
let state = BeaconState::from_ssz_bytes(&ssz).map_err(|_| DBError {
message: "Bad State SSZ.".to_string(),
})?;
Ok(Some(state))
@@ -40,7 +43,7 @@ mod tests {
use ssz::ssz_encode;
use std::sync::Arc;
use types::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use types::Hash256;
use types::{FoundationBeaconState, Hash256};
test_crud_for_store!(BeaconStateStore, DB_COLUMN);
@@ -50,7 +53,7 @@ mod tests {
let store = BeaconStateStore::new(db.clone());
let mut rng = XorShiftRng::from_seed([42; 16]);
let state = BeaconState::random_for_test(&mut rng);
let state: FoundationBeaconState = BeaconState::random_for_test(&mut rng);
let state_root = state.canonical_root();
store.put(&state_root, &ssz_encode(&state)).unwrap();

View File

@@ -4,7 +4,7 @@ use self::bytes::{BufMut, BytesMut};
use super::VALIDATOR_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError};
use bls::PublicKey;
use ssz::{decode, ssz_encode};
use ssz::{Decode, Encode};
use std::sync::Arc;
#[derive(Debug, PartialEq)]
@@ -55,7 +55,7 @@ impl<T: ClientDB> ValidatorStore<T> {
public_key: &PublicKey,
) -> Result<(), ValidatorStoreError> {
let key = self.get_db_key_for_index(&KeyPrefixes::PublicKey, index);
let val = ssz_encode(public_key);
let val = public_key.as_ssz_bytes();
self.db
.put(DB_COLUMN, &key[..], &val[..])
.map_err(ValidatorStoreError::from)
@@ -69,7 +69,7 @@ impl<T: ClientDB> ValidatorStore<T> {
let val = self.db.get(DB_COLUMN, &key[..])?;
match val {
None => Ok(None),
Some(val) => match decode::<PublicKey>(&val) {
Some(val) => match PublicKey::from_ssz_bytes(&val) {
Ok(key) => Ok(Some(key)),
Err(_) => Err(ValidatorStoreError::DecodeError),
},
@@ -125,7 +125,7 @@ mod tests {
.unwrap()
.unwrap();
assert_eq!(public_key_at_index, ssz_encode(&public_key));
assert_eq!(public_key_at_index, public_key.as_ssz_bytes());
}
#[test]
@@ -139,7 +139,7 @@ mod tests {
db.put(
DB_COLUMN,
&store.get_db_key_for_index(&KeyPrefixes::PublicKey, index)[..],
&ssz_encode(&public_key)[..],
&public_key.as_ssz_bytes(),
)
.unwrap();
@@ -157,7 +157,7 @@ mod tests {
db.put(
DB_COLUMN,
&store.get_db_key_for_index(&KeyPrefixes::PublicKey, 3)[..],
&ssz_encode(&public_key)[..],
&public_key.as_ssz_bytes(),
)
.unwrap();