Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ Use ForestStorageCaching for BSPs #281

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
00cecee
feat: :construction: Setup new variables in Blockchain Service to kee…
ffarall Dec 6, 2024
ac9ccfe
build: :building_construction: Update rust toolchain
ffarall Dec 6, 2024
6942ecc
refactor: :recycle: Use `ForestStorageCaching` for BSPs
ffarall Dec 7, 2024
755ac80
fix: :rotating_light: Avoid warning in unused variable
ffarall Dec 7, 2024
bf97b58
fix: :rotating_light: Fix compiler error due to name change
ffarall Dec 7, 2024
71efa27
fix: :rotating_light: Supress unused code warning
ffarall Dec 9, 2024
7f0bedc
fix: :rotating_light: Make crossbuild:mac work with new versions of r…
ffarall Dec 9, 2024
9113c19
feat: :technologist: Use `derive` for default trait implementations
ffarall Dec 9, 2024
07d94b6
feat: :construction: Add `consts` to `client/common`
ffarall Dec 9, 2024
287b400
refactor: :recycle: Make `provider_ids` in Blockchain Service unique …
ffarall Dec 9, 2024
8c01cad
docs: :construction: Add `TODO` comments for intialisation of Forest …
ffarall Dec 9, 2024
4b8323e
feat: :construction: Use constant key for current forest key in BSPs
ffarall Dec 10, 2024
599574f
fix: :adhesive_bandage: Check if proof is still the one to upload aft…
ffarall Dec 10, 2024
b69ae8f
fix: :bug: Use `CURRENT_FOREST_ROOT` constant in `getForestRoot` RPC
ffarall Dec 10, 2024
84d924f
fix: :bug: Only run Blockchain Service's initial sync when block diff…
ffarall Dec 10, 2024
e2bfc3a
fix: :bug: fix: 🐛 Use `CURRENT_FOREST_ROOT` constant in all correspon…
ffarall Dec 10, 2024
e96b4ef
refactor: :recycle: Rename `Runnable` to `Buildable`
ffarall Dec 10, 2024
3efac0e
Merge branch 'main' into feat/use-forest-storage-caching-for-bsps
ffarall Dec 10, 2024
649310a
fix: :bug: Apply refactored changes to new functionality merged from …
ffarall Dec 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,966 changes: 1,095 additions & 871 deletions Cargo.lock

Large diffs are not rendered by default.

122 changes: 72 additions & 50 deletions client/blockchain-service/src/commands.rs

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions client/blockchain-service/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use codec::{Decode, Encode};
use sc_network::Multiaddr;
use shc_actors_framework::event_bus::{EventBus, EventBusMessage, ProvidesEventBus};
use shc_common::types::{
Balance, BlockNumber, BucketId, FileKey, FileLocation, Fingerprint, ForestRoot, KeyProofs,
PeerIds, ProviderId, RandomnessOutput, StorageData, TrieMutation, TrieRemoveMutation,
Balance, BlockNumber, BucketId, ChallengeableProviderId, FileKey, FileLocation, Fingerprint,
ForestRoot, KeyProofs, PeerIds, RandomnessOutput, StorageData, TrieMutation,
TrieRemoveMutation,
};
use sp_core::H256;
use sp_runtime::AccountId32;
Expand All @@ -19,7 +20,7 @@ use crate::types::{ConfirmStoringRequest, RespondStorageRequest};
/// period of this BSP.
#[derive(Debug, Clone, Encode, Decode)]
pub struct NewChallengeSeed {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub tick: BlockNumber,
pub seed: RandomnessOutput,
}
Expand All @@ -35,7 +36,7 @@ impl EventBusMessage for NewChallengeSeed {}
/// should be responded to last.
#[derive(Debug, Clone, Encode, Decode)]
pub struct MultipleNewChallengeSeeds {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub seeds: Vec<(BlockNumber, RandomnessOutput)>,
}

Expand Down Expand Up @@ -70,7 +71,7 @@ impl EventBusMessage for NewStorageRequest {}
#[derive(Debug, Clone)]
pub struct FinalisedMspStoppedStoringBucket {
/// MSP ID who stopped storing the bucket.
pub msp_id: ProviderId,
pub msp_id: ChallengeableProviderId,
/// Account ID owner of the bucket.
pub owner: AccountId32,
pub bucket_id: BucketId,
Expand Down Expand Up @@ -128,7 +129,7 @@ impl From<ProcessStopStoringForInsolventUserRequestData> for ForestWriteLockTask

#[derive(Debug, Clone, Encode, Decode)]
pub struct ProcessSubmitProofRequestData {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub tick: BlockNumber,
pub seed: RandomnessOutput,
pub forest_challenges: Vec<H256>,
Expand Down Expand Up @@ -187,7 +188,7 @@ impl EventBusMessage for ProcessStopStoringForInsolventUserRequest {}
/// This event is emitted when a provider is marked as slashable by the runtime.
#[derive(Debug, Clone)]
pub struct SlashableProvider {
pub provider: ProviderId,
pub provider: ChallengeableProviderId,
pub next_challenge_deadline: BlockNumber,
}

Expand All @@ -199,7 +200,7 @@ impl EventBusMessage for SlashableProvider {}
/// in which there is a `MutationsApplied` event for one of the providers that this node is tracking.
#[derive(Debug, Clone)]
pub struct FinalisedTrieRemoveMutationsApplied {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub mutations: Vec<(ForestRoot, TrieMutation)>,
pub new_root: H256,
}
Expand All @@ -208,15 +209,15 @@ impl EventBusMessage for FinalisedTrieRemoveMutationsApplied {}

#[derive(Debug, Clone)]
pub struct ProofAccepted {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub proofs: KeyProofs,
}

impl EventBusMessage for ProofAccepted {}

#[derive(Debug, Clone)]
pub struct LastChargeableInfoUpdated {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub last_chargeable_tick: BlockNumber,
pub last_chargeable_price_index: Balance,
}
Expand All @@ -238,7 +239,7 @@ impl EventBusMessage for UserWithoutFunds {}
/// This event is emitted when a provider has stopped storing a file for an insolvent user.
#[derive(Debug, Clone)]
pub struct SpStopStoringInsolventUser {
pub sp_id: ProviderId,
pub sp_id: ChallengeableProviderId,
pub file_key: FileKey,
pub owner: AccountId32,
pub location: FileLocation,
Expand Down
279 changes: 198 additions & 81 deletions client/blockchain-service/src/handler.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/blockchain-service/src/typed_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub trait ProvidesTypedDbAccess: ProvidesDbContext {
fn access<'a, CF: TypedCf>(
&'a self,
cf: &'a CF,
) -> TypedCfApi<'a, '_, '_, CF, TypedRocksDB, BufferedWriteSupport<'a, TypedRocksDB>> {
) -> TypedCfApi<'a, 'a, 'a, CF, TypedRocksDB, BufferedWriteSupport<'a, TypedRocksDB>> {
self.db_context().cf(cf)
}
}
Expand Down
70 changes: 64 additions & 6 deletions client/blockchain-service/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ use std::{

use codec::{Decode, Encode};
use frame_support::dispatch::DispatchInfo;
use log::warn;
use sc_client_api::BlockImportNotification;
use sp_core::H256;
use sp_runtime::{traits::Header, AccountId32, DispatchError, SaturatedConversion};

use shc_common::types::{
BlockNumber, ProviderId, RandomnessOutput, RejectedStorageRequestReason, StorageHubEventsVec,
BlockNumber, ChallengeableProviderId, HasherOutT, RandomnessOutput,
RejectedStorageRequestReason, StorageHubEventsVec, StorageProofsMerkleTrieLayout,
TrieRemoveMutation,
};
use sp_core::H256;
use sp_runtime::{traits::Header, AccountId32, DispatchError, SaturatedConversion};

use crate::handler::LOG_TARGET;

/// A struct that holds the information to submit a storage proof.
///
/// This struct is used as an item in the `pending_submit_proof_requests` queue.
#[derive(Debug, Clone, Encode, Decode)]
pub struct SubmitProofRequest {
pub provider_id: ProviderId,
pub provider_id: ChallengeableProviderId,
pub tick: BlockNumber,
pub seed: RandomnessOutput,
pub forest_challenges: Vec<H256>,
Expand All @@ -30,7 +33,7 @@ pub struct SubmitProofRequest {

impl SubmitProofRequest {
pub fn new(
provider_id: ProviderId,
provider_id: ChallengeableProviderId,
tick: BlockNumber,
seed: RandomnessOutput,
forest_challenges: Vec<H256>,
Expand Down Expand Up @@ -320,3 +323,58 @@ pub enum NewBlockNotificationKind {
new_best_block: BestBlockInfo,
},
}

/// The information needed to register a Forest Storage snapshot.
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct ForestStorageSnapshotInfo {
/// The block number at which the Forest Storage snapshot was taken.
///
/// i.e. the block number at which the Forest Storage changed from this snapshot
/// version due to adding or removing files.
pub block_number: BlockNumber,
/// The Forest Storage snapshot hash.
///
/// This is to uniquely identify the Forest Storage snapshot, as there could be
/// snapshots at the same block number, but in different forks.
pub block_hash: H256,
/// The Forest Storage root when the snapshot was taken.
///
/// This is used to identify the Forest Storage snapshot and retrieve it.
pub forest_root: HasherOutT<StorageProofsMerkleTrieLayout>,
}

impl PartialOrd for ForestStorageSnapshotInfo {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

/// Implements the `Ord` trait for `ForestStorageSnapshotInfo`.
///
/// This allows for a BTreeSet to be used to store Forest Storage snapshots.
impl Ord for ForestStorageSnapshotInfo {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// Block number ordering is the first criteria.
match self.block_number.cmp(&other.block_number) {
std::cmp::Ordering::Less => std::cmp::Ordering::Less,
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
std::cmp::Ordering::Equal => {
// If the block numbers are equal, compare the block hashes.
match self.block_hash.cmp(&other.block_hash) {
std::cmp::Ordering::Less => std::cmp::Ordering::Less,
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
std::cmp::Ordering::Equal => {
// If the block hashes and block numbers are equal, the forest roots should be
// the same, because there can only be one snapshot at a given block number,
// for a given fork.
if self.forest_root != other.forest_root {
warn!(target: LOG_TARGET, "CRITICAL❗️❗️ This is a bug! Forest storage snapshot forest roots are not equal, for the same block number and hash. This should not happen. This is a bug. Please report it to the StorageHub team.");
}
snowmead marked this conversation as resolved.
Show resolved Hide resolved

std::cmp::Ordering::Equal
}
}
}
}
}
}
Loading
Loading