diff --git a/Cargo.lock b/Cargo.lock index cdd611518..3f5a70acb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6697,6 +6697,22 @@ dependencies = [ "sp-std 8.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-v1.5.0)", ] +[[package]] +name = "pallet-proofs-dealer" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-trie", +] + [[package]] name = "pallet-proxy" version = "4.0.0-dev" @@ -6924,22 +6940,6 @@ dependencies = [ "sp-std 8.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?tag=polkadot-v1.5.0)", ] -[[package]] -name = "pallet-storage-proofs" -version = "0.1.0" -dependencies = [ - "frame-benchmarking", - "frame-support", - "frame-system", - "parity-scale-codec", - "scale-info", - "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-trie", -] - [[package]] name = "pallet-sudo" version = "4.0.0-dev" @@ -12420,8 +12420,8 @@ dependencies = [ "pallet-collator-selection", "pallet-file-system", "pallet-message-queue", + "pallet-proofs-dealer", "pallet-session", - "pallet-storage-proofs", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", diff --git a/pallets/storage-proofs/Cargo.toml b/pallets/storage-proofs/Cargo.toml index c244ec88e..a1d36d2ad 100644 --- a/pallets/storage-proofs/Cargo.toml +++ b/pallets/storage-proofs/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "pallet-storage-proofs" -description = "Pallet for managing, challenging and veryfing storage proofs in StorageHub." +name = "pallet-proofs-dealer" +description = "Pallet for managing, challenging and veryfing proofs submitted by providers, where each provider's information is stored as a Merkle Patricia Trie." version = "0.1.0" homepage = { workspace = true } license = { workspace = true } diff --git a/pallets/storage-proofs/src/lib.rs b/pallets/storage-proofs/src/lib.rs index 8f560171f..176e05030 100644 --- a/pallets/storage-proofs/src/lib.rs +++ b/pallets/storage-proofs/src/lib.rs @@ -22,7 +22,7 @@ use frame_support::{ inherent::IsFatalError, pallet_prelude::*, sp_runtime::{ - traits::{AtLeast32BitUnsigned, CheckEqual, MaybeDisplay, SimpleBitOps}, + traits::{CheckEqual, MaybeDisplay, SimpleBitOps}, RuntimeString, }, traits::fungible, @@ -45,7 +45,7 @@ pub mod pallet { use frame_system::pallet_prelude::*; use scale_info::prelude::fmt::Debug; use sp_trie::CompactProof; - use types::SpFor; + use types::ProviderFor; use crate::types::*; use crate::*; @@ -55,16 +55,9 @@ pub mod pallet { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// The Storage Providers pallet. - /// To check if whoever submits a proof is a registered Storage Provider. - type StorageProviders: crate::StorageProvidersInterface; - - /// The File System pallet. - /// To check if the root submitted in a proof is valid for the Storage Provider. - type FileSystem: crate::FileSystemInterface< - StorageProvider = SpFor, - ForestRoot = ForestRootFor, - >; + /// The Providers pallet. + /// To check if whoever submits a proof is a registered Provider. + type ProvidersPallet: crate::ProvidersInterface; /// Type to access the Balances Pallet. type NativeBalance: fungible::Inspect @@ -97,9 +90,9 @@ pub mod pallet { #[pallet::constant] type MaxChallengesPerBlock: Get + FullCodec; - /// The maximum number of Storage Providers that can be challenged in block. + /// The maximum number of Providers that can be challenged in block. #[pallet::constant] - type MaxSpsChallengedPerBlock: Get + FullCodec; + type MaxProvidersChallengedPerBlock: Get + FullCodec; /// The number of blocks that challenges history is kept for. /// After this many blocks, challenges are removed from `Challenges` StorageMap. @@ -115,7 +108,7 @@ pub mod pallet { /// The number of blocks in between a checkpoint challenges round (i.e. with custom challenges). /// This is used to determine when to include the challenges from the `ChallengesQueue` and /// `PriorityChallengesQueue` in the `BlockToChallenges` StorageMap. These checkpoint challenge - /// rounds have to be answered by ALL Storage Providers, and this is enforced by the + /// rounds have to be answered by ALL Providers, and this is enforced by the /// `submit_proof` extrinsic. #[pallet::constant] type CheckpointChallengePeriod: Get; @@ -138,10 +131,10 @@ pub mod pallet { BoundedVec, MaxChallengesPerBlockFor>, >; - /// A mapping from block number to a vector of challenged Storage Providers for that block. + /// A mapping from block number to a vector of challenged Providers for that block. /// - /// This is used to keep track of the Storage Providers that have been challenged, and should - /// submit a proof by the time of the block used as the key. Storage Providers who do submit + /// This is used to keep track of the Providers that have been challenged, and should + /// submit a proof by the time of the block used as the key. Providers who do submit /// a proof are removed from their respective entry and pushed forward to the next block in /// which they should submit a proof. Those who are still in the entry by the time the block /// is reached are considered to have failed to submit a proof and subject to slashing. @@ -151,16 +144,16 @@ pub mod pallet { _, Blake2_128Concat, BlockNumberFor, - BoundedVec, MaxSpsChallengedPerBlockFor>, + BoundedVec, MaxSpsChallengedPerBlockFor>, >; - /// A mapping from a Storage Provider to the last block number they submitted a proof for. - /// If for a Storage Provider `sp`, `LastBlockSpSubmittedProofFor[sp]` is `n`, then the - /// Storage Provider should submit a proof for block `n + stake_to_challenge_period(sp)`. + /// A mapping from a Provider to the last block number they submitted a proof for. + /// If for a Provider `sp`, `LastBlockSpSubmittedProofFor[sp]` is `n`, then the + /// Provider should submit a proof for block `n + stake_to_challenge_period(sp)`. #[pallet::storage] #[pallet::getter(fn last_block_sp_submitted_proof_for)] pub type LastBlockSpSubmittedProofFor = - StorageMap<_, Blake2_128Concat, SpFor, BlockNumberFor>; + StorageMap<_, Blake2_128Concat, ProviderFor, BlockNumberFor>; /// A queue of file keys that have been challenged manually. /// @@ -192,7 +185,7 @@ pub mod pallet { /// /// This is used to determine when to include the challenges from the `ChallengesQueue` and /// `PriorityChallengesQueue` in the `BlockToChallenges` StorageMap. These checkpoint challenge - /// rounds have to be answered by ALL Storage Providers, and this is enforced by the + /// rounds have to be answered by ALL Providers, and this is enforced by the /// `submit_proof` extrinsic. #[pallet::storage] #[pallet::getter(fn last_checkpoint_block)] @@ -207,13 +200,13 @@ pub mod pallet { /// [who, file_key_challenged] NewChallenge(AccountIdFor, FileKeyFor), - /// A storage proof was rejected. - /// [storage_provider, proof, reason] - ProofRejected(AccountIdFor, CompactProof, ProofRejectionReason), + /// A proof was rejected. + /// [provider, proof, reason] + ProofRejected(ProviderFor, CompactProof, ProofRejectionReason), - /// A storage proof was accepted. - /// [storage_provider, proof] - ProofAccepted(AccountIdFor, CompactProof), + /// A proof was accepted. + /// [provider, proof] + ProofAccepted(ProviderFor, CompactProof), } // Errors inform users that something went wrong. @@ -223,8 +216,8 @@ pub mod pallet { /// until some of the challenges in the queue are dispatched. ChallengesQueueOverflow, - /// The proof submitter is not a registered Storage Provider. - NotStorageProvider, + /// The proof submitter is not a registered Provider. + NotProvider, } #[pallet::call] @@ -234,7 +227,7 @@ pub mod pallet { /// This function allows anyone to add a new challenge to the `ChallengesQueue`. /// The challenge will be dispatched in the coming blocks. /// Regular users are charged a small fee for submitting a challenge, which - /// goes to the Treasury. Unless the one calling is a registered Storage Provider. + /// goes to the Treasury. Unless the one calling is a registered Provider. /// /// TODO: Consider checking also if there was a request to change MSP. #[pallet::call_index(0)] @@ -255,25 +248,25 @@ pub mod pallet { Ok(().into()) } - /// For a Storage Provider to submit a storage proof. + /// For a Provider to submit a proof. /// - /// Checks that `storage_provider` is a registered Storage Provider. If none - /// is provided, the proof submitter is considered to be the Storage Provider. - /// Relies on a File System pallet to check if the root is valid for the Storage Provider. + /// Checks that `provider` is a registered Provider. If none + /// is provided, the proof submitter is considered to be the Provider. + /// Relies on a File System pallet to check if the root is valid for the Provider. /// Validates that the proof corresponds to a challenge that was made in the past, - /// by checking the `BlockToChallenges` StorageMap. The block number that the Storage + /// by checking the `BlockToChallenges` StorageMap. The block number that the /// Provider should have submitted a proof is calculated based on the last block they /// submitted a proof for (`LastBlockSpSubmittedProofFor`), and the proving period for - /// that Storage Provider, which is a function of their stake. + /// that Provider, which is a function of their stake. /// This extrinsic also checks that there hasn't been a checkpoint challenge round - /// in between the last time the Storage Provider submitted a proof for and the block - /// for which the proof is being submitted. If there has been, the Storage Provider is + /// in between the last time the Provider submitted a proof for and the block + /// for which the proof is being submitted. If there has been, the Provider is /// subject to slashing. /// /// If valid: - /// - Pushes forward the Storage Provider in the `BlockToChallengedSps` StorageMap a number - /// of blocks corresponding to the stake of the Storage Provider. - /// - Registers this block as the last block in which the Storage Provider submitted a proof. + /// - Pushes forward the Provider in the `BlockToChallengedSps` StorageMap a number + /// of blocks corresponding to the stake of the Provider. + /// - Registers this block as the last block in which the Provider submitted a proof. /// /// Execution of this extrinsic should be refunded if the proof is valid. #[pallet::call_index(1)] @@ -283,16 +276,26 @@ pub mod pallet { proof: CompactProof, root: ForestRootFor, challenge_block: BlockNumberFor, - storage_provider: Option>, + provider: Option>, ) -> DispatchResultWithPostInfo { // Check that the extrinsic was signed and get the signer. let who = ensure_signed(origin)?; + // Getting provider from the origin if none is provided. + let provider = match provider { + Some(provider) => provider, + None => { + let sp = T::ProvidersPallet::get_provider(who.clone()) + .ok_or(Error::::NotProvider)?; + sp + } + }; + // TODO: Handle result of verification. - Self::do_submit_proof(&who, &proof)?; + Self::do_submit_proof(&provider, &proof)?; // TODO: Emit correct event. - Self::deposit_event(Event::ProofAccepted(who, proof)); + Self::deposit_event(Event::ProofAccepted(provider, proof)); // Return a successful DispatchResultWithPostInfo Ok(().into()) @@ -308,9 +311,9 @@ pub mod pallet { /// `PriorityChallengesQueue`. This custom challenges are only included in "checkpoint" /// blocks /// - /// Additionally, it takes care of checking if there are Storage Providers that have + /// Additionally, it takes care of checking if there are Providers that have /// failed to submit a proof, and should have submitted one by this block. It does so - /// by checking the `BlockToChallengedSps` StorageMap. If a Storage Provider is found + /// by checking the `BlockToChallengedSps` StorageMap. If a Provider is found /// to have failed to submit a proof, it is subject to slashing. /// /// Finally, it cleans up: @@ -387,67 +390,18 @@ impl InherentError { } } -// TODO: Move this to Storage Providers pallet. -/// A trait to lookup registered Storage Providers. +/// A trait to lookup registered Providers, their Merkle Patricia Trie roots and their stake. /// -/// It is abstracted over the `AccountId` type, `StorageProvider` type, total number of users -/// and Balance type. -pub trait StorageProvidersInterface { +/// It is abstracted over the `AccountId` type, `Provider` type, `Balance` type and `MerkleHash` type. +pub trait ProvidersInterface { /// The type which can be used to identify accounts. type AccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + Ord + MaxEncodedLen; - /// The type corresponding to the staking balance of a registered Storage Provider. - type Balance: fungible::Inspect - + fungible::hold::Inspect - + fungible::freeze::Inspect; - /// The type which represents a registered Storage Provider. - type StorageProvider: Parameter - + Member - + MaybeSerializeDeserialize - + Debug - + Ord - + MaxEncodedLen; - /// The type which represents the total number of registered Storage Provider. - type UserCount: Parameter - + Member - + MaybeSerializeDeserialize - + Ord - + AtLeast32BitUnsigned - + FullCodec - + Copy - + Default - + Debug - + scale_info::TypeInfo - + MaxEncodedLen; - - /// Lookup a registered StorageProvider by their AccountId. - fn get_sp(who: Self::AccountId) -> Option; - - /// Check if an account is a registered Storage Provider. - fn is_sp(who: Self::AccountId) -> bool; - - /// Lookup the total number of registered Storage Providers. - fn total_sps() -> Self::UserCount; - - /// Get the stake for a registered Storage Provider. - fn get_stake(who: Self::StorageProvider) -> Self::Balance; -} - -// TODO: Move this to File System pallet. -/// A trait to lookup registered Storage Providers. -/// -/// It is abstracted over the, `StorageProvider` type and `ForestRoot` type. -pub trait FileSystemInterface { - /// The type which represents a registered user. - type StorageProvider: Parameter - + Member - + MaybeSerializeDeserialize - + Debug - + Ord - + MaxEncodedLen; - - /// The type for a root of a Merkle Patricia Forest. - /// Generally a hash (the output of a Hasher). - type ForestRoot: Parameter + /// The type which represents a registered Provider. + type Provider: Parameter + Member + MaybeSerializeDeserialize + Debug + Ord + MaxEncodedLen; + /// The type corresponding to the staking balance of a registered Provider. + type Balance: fungible::hold::Inspect; + /// The type corresponding to the root of a registered Provider. + type MerkleHash: Parameter + Member + MaybeSerializeDeserialize + Debug @@ -462,8 +416,17 @@ pub trait FileSystemInterface { + MaxEncodedLen + FullCodec; - /// Check if a Merkle Patricia Forest root hash belongs to a given Storage Provider. - fn is_valid_root_for_sp(who: Self::StorageProvider, root: Self::ForestRoot) -> bool; + /// Check if an account is a registered Provider. + fn is_provider(who: Self::Provider) -> bool; + + // Get Provider from AccountId, if it is a registered Provider. + fn get_provider(who: Self::AccountId) -> Option; + + /// Get the root for a registered Provider. + fn get_root(who: Self::Provider) -> Option; + + /// Get the stake for a registered Provider. + fn get_stake(who: Self::Provider) -> Option; } // TODO: Move this to a primitives crate. diff --git a/pallets/storage-proofs/src/types.rs b/pallets/storage-proofs/src/types.rs index d8a046094..95cfffa34 100644 --- a/pallets/storage-proofs/src/types.rs +++ b/pallets/storage-proofs/src/types.rs @@ -17,32 +17,32 @@ pub enum ProofRejectionReason { // ********************* Syntactic sugar for types **************************** // **************************************************************************** -/// Syntactic sugar for the AccountId type used in the storage proofs pallet. +/// Syntactic sugar for the AccountId type used in the proofs pallet. pub type AccountIdFor = ::AccountId; /// The type for keys that identify a file within a Merkle Patricia Forest. -/// Syntactic sugar for the MerkleHash type used in the storage proofs pallet. +/// Syntactic sugar for the MerkleHash type used in the proofs pallet. pub type FileKeyFor = ::MerkleHash; /// The type for a root of a Merkle Patricia Forest. -/// Syntactic sugar for the MerkleHash type used in the storage proofs pallet. +/// Syntactic sugar for the MerkleHash type used in the proofs pallet. pub type ForestRootFor = ::MerkleHash; -/// Syntactic sugar for the MaxChallengesPerBlock type used in the storage proofs pallet. +/// Syntactic sugar for the MaxChallengesPerBlock type used in the proofs pallet. pub type MaxChallengesPerBlockFor = ::MaxChallengesPerBlock; -/// Syntactic sugar for the MaxSpsChallengedPerBlock type used in the storage proofs pallet. -pub type MaxSpsChallengedPerBlockFor = ::MaxSpsChallengedPerBlock; +/// Syntactic sugar for the MaxSpsChallengedPerBlock type used in the proofs pallet. +pub type MaxSpsChallengedPerBlockFor = ::MaxProvidersChallengedPerBlock; -/// Syntactic sugar for the ChallengesQueueLength type used in the storage proofs pallet. +/// Syntactic sugar for the ChallengesQueueLength type used in the proofs pallet. pub type ChallengesQueueLengthFor = ::ChallengesQueueLength; -/// Syntactic sugar for the StorageProviders type used in the storage proofs pallet. -pub type StorageProvidersFor = ::StorageProviders; +/// Syntactic sugar for the Providers type used in the proofs pallet. +pub type ProvidersPalletFor = ::ProvidersPallet; -/// Syntactic sugar for the StorageProvider type used in the storage proofs pallet. -pub type SpFor = - <::StorageProviders as crate::StorageProvidersInterface>::StorageProvider; +/// Syntactic sugar for the Provider type used in the proofs pallet. +pub type ProviderFor = + <::ProvidersPallet as crate::ProvidersInterface>::Provider; /// Syntactic sugar for the type of Balance used in the NativeBalances pallet. pub type BalanceFor = <::NativeBalance as fungible::Inspect< diff --git a/pallets/storage-proofs/src/utils.rs b/pallets/storage-proofs/src/utils.rs index d54090a35..e79e104d8 100644 --- a/pallets/storage-proofs/src/utils.rs +++ b/pallets/storage-proofs/src/utils.rs @@ -6,8 +6,8 @@ use sp_trie::CompactProof; use crate::{ pallet, - types::{AccountIdFor, BalanceFor, FileKeyFor, StorageProvidersFor}, - ChallengesQueue, Error, Pallet, StorageProvidersInterface, + types::{AccountIdFor, BalanceFor, FileKeyFor, ProviderFor, ProvidersPalletFor}, + ChallengesQueue, Error, Pallet, ProvidersInterface, }; impl Pallet @@ -36,11 +36,11 @@ where // TODO: Document and add proper parameters. #[allow(unused_variables)] - pub fn do_submit_proof(submitter: &AccountIdFor, proof: &CompactProof) -> DispatchResult { - // Check if submitter is a registered Storage Provider. + pub fn do_submit_proof(submitter: &ProviderFor, proof: &CompactProof) -> DispatchResult { + // Check if submitter is a registered Provider. ensure!( - StorageProvidersFor::::is_sp(submitter.clone()), - Error::::NotStorageProvider + ProvidersPalletFor::::is_provider(submitter.clone()), + Error::::NotProvider ); // TODO diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 44239263b..774bf3d2b 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -30,7 +30,7 @@ smallvec = "1.11.0" # Local pallet-file-system = { path = "../pallets/file-system", default-features = false } -pallet-storage-proofs = { path = "../pallets/storage-proofs", default-features = false } +pallet-proofs-dealer = { path = "../pallets/storage-proofs", default-features = false } # Substrate frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.5.0", default-features = false, optional = true } @@ -112,7 +112,7 @@ std = [ "pallet-file-system/std", "pallet-message-queue/std", "pallet-session/std", - "pallet-storage-proofs/std", + "pallet-proofs-dealer/std", "pallet-sudo/std", "pallet-timestamp/std", "pallet-transaction-payment-rpc-runtime-api/std", @@ -157,7 +157,7 @@ runtime-benchmarks = [ "pallet-collator-selection/runtime-benchmarks", "pallet-message-queue/runtime-benchmarks", "pallet-file-system/runtime-benchmarks", - "pallet-storage-proofs/runtime-benchmarks", + "pallet-proofs-dealer/runtime-benchmarks", "pallet-sudo/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", "pallet-xcm/runtime-benchmarks", @@ -210,7 +210,7 @@ try-runtime = [ "pallet-message-queue/try-runtime", "pallet-file-system/try-runtime", "pallet-session/try-runtime", - "pallet-storage-proofs/try-runtime", + "pallet-proofs-dealer/try-runtime", "pallet-sudo/try-runtime", "pallet-timestamp/try-runtime", "pallet-transaction-payment/try-runtime",