Skip to content

Commit

Permalink
Merge remote-tracking branch 'up/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Jun 5, 2023
2 parents 1975c93 + 0829a67 commit 0ff28a7
Show file tree
Hide file tree
Showing 30 changed files with 662 additions and 523 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ elastic-array = "0.11"
enum-map = "2.1.0"
enumset = "1.0"
expect-test = "1.3.0"
finite-wasm = "0.4.0"
finite-wasm = "0.5.0"
flate2 = "1.0.22"
fs2 = "0.4"
futures = "0.3.5"
Expand Down
3 changes: 1 addition & 2 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5019,8 +5019,7 @@ impl<'a> ChainUpdate<'a> {
// Otherwise, save delta to disk so it will be used for flat storage creation later.
debug!(target: "chain", %shard_id, "Add delta for flat storage creation");
let mut store_update = self.chain_store_update.store().store_update();
store_helper::set_delta(&mut store_update, shard_uid, &delta)
.map_err(|e| StorageError::from(e))?;
store_helper::set_delta(&mut store_update, shard_uid, &delta);
self.chain_store_update.merge(store_update);
}

Expand Down
6 changes: 3 additions & 3 deletions chain/chain/src/flat_storage_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ impl FlatStorageShardCreator {
shard_uid,
key,
Some(FlatStateValue::value_ref(&value)),
)
.expect("Failed to put value in FlatState");
);
num_items += 1;
}
}
Expand Down Expand Up @@ -153,7 +152,8 @@ impl FlatStorageShardCreator {
) -> Result<bool, Error> {
let shard_id = self.shard_uid.shard_id();
let current_status =
store_helper::get_flat_storage_status(chain_store.store(), self.shard_uid);
store_helper::get_flat_storage_status(chain_store.store(), self.shard_uid)
.expect("failed to read flat storage status");
self.metrics.set_status(&current_status);
match &current_status {
FlatStorageStatus::Empty => {
Expand Down
10 changes: 6 additions & 4 deletions core/primitives-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use std::io::Write;
pub struct CryptoHash(pub [u8; 32]);

impl CryptoHash {
pub const LENGTH: usize = 32;

pub const fn new() -> Self {
Self([0; 32])
Self([0; Self::LENGTH])
}

/// Calculates hash of given bytes.
Expand Down Expand Up @@ -67,7 +69,7 @@ impl CryptoHash {
CryptoHash(hasher.finalize().into())
}

pub const fn as_bytes(&self) -> &[u8; 32] {
pub const fn as_bytes(&self) -> &[u8; Self::LENGTH] {
&self.0
}

Expand Down Expand Up @@ -191,8 +193,8 @@ impl From<&CryptoHash> for Vec<u8> {
}
}

impl From<CryptoHash> for [u8; 32] {
fn from(hash: CryptoHash) -> [u8; 32] {
impl From<CryptoHash> for [u8; CryptoHash::LENGTH] {
fn from(hash: CryptoHash) -> [u8; CryptoHash::LENGTH] {
hash.0
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/store/src/flat/chunk_view.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::flat::store_helper;
use near_primitives::errors::StorageError;
use near_primitives::hash::CryptoHash;
use near_primitives::state::FlatStateValue;

use crate::Store;

use super::types::FlatStateIterator;
use super::FlatStorage;

/// Struct for getting value references from the flat storage, corresponding
Expand Down Expand Up @@ -49,7 +49,7 @@ impl FlatStorageChunkView {
&'a self,
from: Option<&[u8]>,
to: Option<&[u8]>,
) -> impl Iterator<Item = Result<(Vec<u8>, FlatStateValue), StorageError>> + 'a {
) -> FlatStateIterator<'a> {
store_helper::iter_flat_state_entries(self.flat_storage.shard_uid(), &self.store, from, to)
}

Expand Down
4 changes: 2 additions & 2 deletions core/store/src/flat/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct FlatStateDeltaMetadata {
pub block: BlockInfo,
}

#[derive(Debug)]
pub struct KeyForFlatStateDelta {
pub shard_uid: ShardUId,
pub block_hash: CryptoHash,
Expand Down Expand Up @@ -108,8 +109,7 @@ impl FlatStateChanges {
/// Applies delta to the flat state.
pub fn apply_to_flat_state(self, store_update: &mut StoreUpdate, shard_uid: ShardUId) {
for (key, value) in self.0.into_iter() {
store_helper::set_flat_state_value(store_update, shard_uid, key, value)
.expect("Borsh cannot fail");
store_helper::set_flat_state_value(store_update, shard_uid, key, value);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/store/src/flat/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl FlatStorageManager {

pub fn get_flat_storage_status(&self, shard_uid: ShardUId) -> FlatStorageStatus {
store_helper::get_flat_storage_status(&self.0.store, shard_uid)
.expect("failed to read flat storage status")
}

/// Creates `FlatStorageChunkView` to access state for `shard_uid` and block `block_hash`.
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/flat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use manager::FlatStorageManager;
pub use metrics::FlatStorageCreationMetrics;
pub use storage::FlatStorage;
pub use types::{
BlockInfo, FetchingStateStatus, FlatStorageCreationStatus, FlatStorageError,
BlockInfo, FetchingStateStatus, FlatStateIterator, FlatStorageCreationStatus, FlatStorageError,
FlatStorageReadyStatus, FlatStorageStatus,
};

Expand Down
31 changes: 17 additions & 14 deletions core/store/src/flat/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl FlatStorageInner {
// and read single `ValueRef` from delta if it is not cached.
self.deltas
.get(block_hash)
.ok_or(FlatStorageError::StorageInternalError)
.ok_or_else(|| missing_delta_error(block_hash))
.map(|delta| delta.changes.clone())
}

Expand Down Expand Up @@ -134,7 +134,7 @@ impl FlatStorage {
pub fn new(store: Store, shard_uid: ShardUId) -> Self {
let shard_id = shard_uid.shard_id();
let flat_head = match store_helper::get_flat_storage_status(&store, shard_uid) {
FlatStorageStatus::Ready(ready_status) => ready_status.flat_head,
Ok(FlatStorageStatus::Ready(ready_status)) => ready_status.flat_head,
status => {
panic!("cannot create flat storage for shard {shard_id} with status {status:?}")
}
Expand All @@ -151,9 +151,9 @@ impl FlatStorage {
let block_hash = delta_metadata.block.hash;
let changes: CachedFlatStateChanges =
store_helper::get_delta_changes(&store, shard_uid, block_hash)
.expect("Borsh cannot fail")
.expect("failed to read flat state delta changes")
.unwrap_or_else(|| {
panic!("Cannot find block delta for block {block_hash:?} shard {shard_id}")
panic!("cannot find block delta for block {block_hash:?} shard {shard_id}")
})
.into();
deltas.insert(
Expand Down Expand Up @@ -225,7 +225,7 @@ impl FlatStorage {
// Delta must exist because flat storage is locked and we could retrieve
// path from old to new head. Otherwise we return internal error.
let changes = store_helper::get_delta_changes(&guard.store, shard_uid, block_hash)?
.ok_or(FlatStorageError::StorageInternalError)?;
.ok_or_else(|| missing_delta_error(&block_hash))?;
changes.apply_to_flat_state(&mut store_update, guard.shard_uid);
let block = &guard.deltas[&block_hash].metadata.block;
let block_height = block.height;
Expand All @@ -246,7 +246,7 @@ impl FlatStorage {
let gc_height = guard
.deltas
.get(&block_hash)
.ok_or(FlatStorageError::StorageInternalError)?
.ok_or_else(|| missing_delta_error(&block_hash))?
.metadata
.block
.height;
Expand Down Expand Up @@ -287,7 +287,7 @@ impl FlatStorage {
return Err(guard.create_block_not_supported_error(&block_hash));
}
let mut store_update = StoreUpdate::new(guard.store.storage.clone());
store_helper::set_delta(&mut store_update, shard_uid, &delta)?;
store_helper::set_delta(&mut store_update, shard_uid, &delta);
let cached_changes: CachedFlatStateChanges = delta.changes.into();
guard.deltas.insert(
block_hash,
Expand Down Expand Up @@ -333,6 +333,10 @@ impl FlatStorage {
}
}

fn missing_delta_error(block_hash: &CryptoHash) -> FlatStorageError {
FlatStorageError::StorageInternalError(format!("delta does not exist for block {block_hash}"))
}

#[cfg(test)]
mod tests {
use crate::flat::delta::{FlatStateChanges, FlatStateDelta, FlatStateDeltaMetadata};
Expand Down Expand Up @@ -466,7 +470,7 @@ mod tests {
changes: FlatStateChanges::default(),
metadata: FlatStateDeltaMetadata { block: chain.get_block(i) },
};
store_helper::set_delta(&mut store_update, shard_uid, &delta).unwrap();
store_helper::set_delta(&mut store_update, shard_uid, &delta);
}
store_update.commit().unwrap();

Expand Down Expand Up @@ -501,9 +505,9 @@ mod tests {
let mut store_update = store.store_update();
store_helper::remove_delta(&mut store_update, shard_uid, chain.get_block_hash(3));
store_update.commit().unwrap();
assert_eq!(
assert_matches!(
flat_storage.update_flat_head(&chain.get_block_hash(3)),
Err(FlatStorageError::StorageInternalError)
Err(FlatStorageError::StorageInternalError(_))
);
}

Expand All @@ -524,7 +528,7 @@ mod tests {
changes: FlatStateChanges::default(),
metadata: FlatStateDeltaMetadata { block: chain.get_block(i * 2) },
};
store_helper::set_delta(&mut store_update, shard_uid, &delta).unwrap();
store_helper::set_delta(&mut store_update, shard_uid, &delta);
}
store_update.commit().unwrap();

Expand Down Expand Up @@ -560,8 +564,7 @@ mod tests {
shard_uid,
vec![1],
Some(FlatStateValue::value_ref(&[0])),
)
.unwrap();
);
for i in 1..10 {
let delta = FlatStateDelta {
changes: FlatStateChanges::from([(
Expand All @@ -570,7 +573,7 @@ mod tests {
)]),
metadata: FlatStateDeltaMetadata { block: chain.get_block(i) },
};
store_helper::set_delta(&mut store_update, shard_uid, &delta).unwrap();
store_helper::set_delta(&mut store_update, shard_uid, &delta);
}
store_update.commit().unwrap();

Expand Down
Loading

0 comments on commit 0ff28a7

Please sign in to comment.