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

feat: Add storagehubprovider RPC module #333

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ sc-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk.git",
sc-transaction-pool-api = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
sc-utils = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
sp-weights = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
sc-rpc-api = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
substrate-frame-rpc-system = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-trie = { workspace = true }
sp-keystore = { workspace = true }
sc-rpc-api = { workspace = true }

# Local
pallet-file-system-runtime-api = { workspace = true }
Expand Down
31 changes: 23 additions & 8 deletions client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use jsonrpsee::{
core::{async_trait, RpcResult},
proc_macros::rpc,
types::error::{ErrorObjectOwned as JsonRpseeError, INTERNAL_ERROR_CODE, INTERNAL_ERROR_MSG},
Extensions,
};
use log::{debug, error, info};
use sc_rpc_api::check_if_safe;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use tokio::{fs, fs::create_dir_all, sync::RwLock};
Expand Down Expand Up @@ -101,7 +103,6 @@ pub enum GetFileFromFileStorageResult {
/// Used by the `rpc` macro from `jsonrpsee`
/// to generate the trait that is actually going to be implemented.
#[rpc(server, namespace = "storagehubclient")]
#[async_trait]
pub trait StorageHubClientApi {
#[method(name = "loadFileInStorage")]
async fn load_file_in_storage(
Expand Down Expand Up @@ -180,22 +181,22 @@ pub trait StorageHubClientApi {
file_key: H256,
) -> RpcResult<Vec<u8>>;

#[method(name = "insertBcsvKeys")]
#[method(name = "insertBcsvKeys", with_extensions)]
async fn insert_bcsv_keys(&self, seed: Option<String>) -> RpcResult<String>;

#[method(name = "removeBcsvKeys")]
#[method(name = "removeBcsvKeys", with_extensions)]
async fn remove_bcsv_keys(&self, keystore_path: String) -> RpcResult<()>;

// Note: This RPC method allow BSP administrator to add a file to the exclude list (and later
// buckets, users or file fingerprint). This method is required to call before deleting a file to
// avoid re-uploading a file that has just been deleted.
#[method(name = "addToExcludeList")]
#[method(name = "addToExcludeList", with_extensions)]
async fn add_to_exclude_list(&self, file_key: H256, exclude_type: String) -> RpcResult<()>;

// Note: This RPC method allow BSP administrator to remove a file from the exclude list (allowing
// the BSP to volunteer for this specific file key again). Later it will allow to remove from the exclude
// list ban users, bucket or even file fingerprint.
#[method(name = "removeFromExcludeList")]
#[method(name = "removeFromExcludeList", with_extensions)]
async fn remove_from_exclude_list(&self, file_key: H256, exclude_type: String)
-> RpcResult<()>;
}
Expand Down Expand Up @@ -706,7 +707,9 @@ where
// In the case a seed is not provided, we delegate generation and insertion to `sr25519_generate_new`, which
// internally uses the block number as a seed.
// See https://paritytech.github.io/polkadot-sdk/master/sc_keystore/struct.LocalKeystore.html#method.sr25519_generate_new
async fn insert_bcsv_keys(&self, seed: Option<String>) -> RpcResult<String> {
async fn insert_bcsv_keys(&self, ext: &Extensions, seed: Option<String>) -> RpcResult<String> {
check_if_safe(ext)?;

let seed = seed.as_deref();

let new_pub_key = match seed {
Expand All @@ -729,7 +732,9 @@ where
}

// Deletes all files with keys of type BCSV from the Keystore.
async fn remove_bcsv_keys(&self, keystore_path: String) -> RpcResult<()> {
async fn remove_bcsv_keys(&self, ext: &Extensions, keystore_path: String) -> RpcResult<()> {
check_if_safe(ext)?;

let pub_keys = self.keystore.keys(BCSV_KEY_TYPE).map_err(into_rpc_error)?;
let key_path = PathBuf::from(keystore_path);

Expand All @@ -748,7 +753,14 @@ where
Ok(())
}

async fn add_to_exclude_list(&self, file_key: H256, exclude_type: String) -> RpcResult<()> {
async fn add_to_exclude_list(
&self,
ext: &Extensions,
file_key: H256,
exclude_type: String,
) -> RpcResult<()> {
check_if_safe(ext)?;

let et = ExcludeType::from_str(&exclude_type).map_err(into_rpc_error)?;

let mut write_file_storage = self.file_storage.write().await;
Expand All @@ -763,9 +775,12 @@ where

async fn remove_from_exclude_list(
&self,
ext: &Extensions,
file_key: H256,
exclude_type: String,
) -> RpcResult<()> {
check_if_safe(ext)?;

let et = ExcludeType::from_str(&exclude_type).map_err(into_rpc_error)?;

let mut write_file_storage = self.file_storage.write().await;
Expand Down
4 changes: 4 additions & 0 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use sc_consensus_manual_seal::{
rpc::{ManualSeal, ManualSealApiServer},
EngineCommand,
};
use sc_rpc::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
use shc_common::types::{
BackupStorageProviderId, BlockNumber, ChunkId, CustomChallenge, ForestLeaf,
Expand Down Expand Up @@ -102,5 +103,8 @@ where
)?;
};

// Deny unsafe RPCs.
io.extensions_mut().insert(DenyUnsafe::Yes);

Ok(io)
}
6 changes: 6 additions & 0 deletions test/suites/integration/bsp/submit-proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ describeBspNet(
checkTxPool: true
});

await userApi.assert.extrinsicPresent({
module: "fileSystem",
method: "mspRespondStorageRequestsMultipleBuckets",
checkTxPool: true
});

// Seal block and check that the transaction was successful.
await userApi.block.seal();

Expand Down
Loading