mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 16:37:57 +00:00
Allow to expose a subset of unsafe RPCs (#5233)
* sc-cli: Use type-safe constructors for RPC/Prometheus interfaces * service: Simplify rpc handler creation Could probably be further simplifies once [this][commit] lands. [commit]: https://github.com/paritytech/jsonrpc/commit/20485387ed06a48f1a70bf4d609a7cde6cf0accf * service: Streamline some HTTP & WS server start logic * client: Introduce a simple RPC policy mechanism * rpc/system: Check unsafe RPCs * rpc/offchain: Check unsafe RPCs * rpc/author: Check unsafe RPCs
This commit is contained in:
@@ -21,6 +21,7 @@ mod tests;
|
||||
|
||||
/// Re-export the API for backward compatibility.
|
||||
pub use sc_rpc_api::offchain::*;
|
||||
use sc_rpc_api::DenyUnsafe;
|
||||
use self::error::{Error, Result};
|
||||
use sp_core::{
|
||||
Bytes,
|
||||
@@ -34,13 +35,15 @@ use std::sync::Arc;
|
||||
pub struct Offchain<T: OffchainStorage> {
|
||||
/// Offchain storage
|
||||
storage: Arc<RwLock<T>>,
|
||||
deny_unsafe: DenyUnsafe,
|
||||
}
|
||||
|
||||
impl<T: OffchainStorage> Offchain<T> {
|
||||
/// Create new instance of Offchain API.
|
||||
pub fn new(storage: T) -> Self {
|
||||
pub fn new(storage: T, deny_unsafe: DenyUnsafe) -> Self {
|
||||
Offchain {
|
||||
storage: Arc::new(RwLock::new(storage)),
|
||||
deny_unsafe,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +51,8 @@ impl<T: OffchainStorage> Offchain<T> {
|
||||
impl<T: OffchainStorage + 'static> OffchainApi for Offchain<T> {
|
||||
/// Set offchain local storage under given key and prefix.
|
||||
fn set_local_storage(&self, kind: StorageKind, key: Bytes, value: Bytes) -> Result<()> {
|
||||
self.deny_unsafe.check_if_safe()?;
|
||||
|
||||
let prefix = match kind {
|
||||
StorageKind::PERSISTENT => sp_offchain::STORAGE_PREFIX,
|
||||
StorageKind::LOCAL => return Err(Error::UnavailableStorageKind),
|
||||
@@ -58,6 +63,8 @@ impl<T: OffchainStorage + 'static> OffchainApi for Offchain<T> {
|
||||
|
||||
/// Get offchain local storage under given key and prefix.
|
||||
fn get_local_storage(&self, kind: StorageKind, key: Bytes) -> Result<Option<Bytes>> {
|
||||
self.deny_unsafe.check_if_safe()?;
|
||||
|
||||
let prefix = match kind {
|
||||
StorageKind::PERSISTENT => sp_offchain::STORAGE_PREFIX,
|
||||
StorageKind::LOCAL => return Err(Error::UnavailableStorageKind),
|
||||
|
||||
@@ -21,7 +21,7 @@ use sp_core::{Bytes, offchain::storage::InMemOffchainStorage};
|
||||
#[test]
|
||||
fn local_storage_should_work() {
|
||||
let storage = InMemOffchainStorage::default();
|
||||
let offchain = Offchain::new(storage);
|
||||
let offchain = Offchain::new(storage, DenyUnsafe::No);
|
||||
let key = Bytes(b"offchain_storage".to_vec());
|
||||
let value = Bytes(b"offchain_value".to_vec());
|
||||
|
||||
@@ -34,3 +34,20 @@ fn local_storage_should_work() {
|
||||
Ok(Some(ref v)) if *v == value
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offchain_calls_considered_unsafe() {
|
||||
let storage = InMemOffchainStorage::default();
|
||||
let offchain = Offchain::new(storage, DenyUnsafe::Yes);
|
||||
let key = Bytes(b"offchain_storage".to_vec());
|
||||
let value = Bytes(b"offchain_value".to_vec());
|
||||
|
||||
assert_matches!(
|
||||
offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()),
|
||||
Err(Error::UnsafeRpcCalled(_))
|
||||
);
|
||||
assert_matches!(
|
||||
offchain.get_local_storage(StorageKind::PERSISTENT, key),
|
||||
Err(Error::UnsafeRpcCalled(_))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user