Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit acb1b9b

Browse files
authored
RPC api for offchain storage (#4694)
* Rpc api for offchain storage * Replace Vec<u8> to Bytes, replace Mutex to RwLock * Remove pub * Modify copyright year
1 parent 2241c6b commit acb1b9b

File tree

10 files changed

+218
-9
lines changed

10 files changed

+218
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/rpc-api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ pub use helpers::Receiver;
3030

3131
pub mod author;
3232
pub mod chain;
33+
pub mod offchain;
3334
pub mod state;
3435
pub mod system;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Offchain RPC errors.
18+
19+
use jsonrpc_core as rpc;
20+
21+
/// Offchain RPC Result type.
22+
pub type Result<T> = std::result::Result<T, Error>;
23+
24+
/// Offchain RPC errors.
25+
#[derive(Debug, derive_more::Display, derive_more::From)]
26+
pub enum Error {
27+
/// Unavailable storage kind error.
28+
#[display(fmt="This storage kind is not available yet.")]
29+
UnavailableStorageKind,
30+
}
31+
32+
impl std::error::Error for Error {
33+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34+
None
35+
}
36+
}
37+
38+
/// Base error code for all offchain errors.
39+
const BASE_ERROR: i64 = 5000;
40+
41+
impl From<Error> for rpc::Error {
42+
fn from(e: Error) -> Self {
43+
match e {
44+
Error::UnavailableStorageKind => rpc::Error {
45+
code: rpc::ErrorCode::ServerError(BASE_ERROR + 1),
46+
message: "This storage kind is not available yet" .into(),
47+
data: None,
48+
},
49+
}
50+
}
51+
}

client/rpc-api/src/offchain/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Substrate offchain API.
18+
19+
pub mod error;
20+
21+
use jsonrpc_derive::rpc;
22+
use self::error::Result;
23+
use sp_core::{Bytes, offchain::StorageKind};
24+
25+
pub use self::gen_client::Client as OffchainClient;
26+
27+
/// Substrate offchain RPC API
28+
#[rpc]
29+
pub trait OffchainApi {
30+
/// Set offchain local storage under given key and prefix.
31+
#[rpc(name = "offchain_localStorageSet")]
32+
fn set_local_storage(&self, kind: StorageKind, key: Bytes, value: Bytes) -> Result<()>;
33+
34+
/// Get offchain local storage under given key and prefix.
35+
#[rpc(name = "offchain_localStorageGet")]
36+
fn get_local_storage(&self, kind: StorageKind, key: Bytes) -> Result<Option<Bytes>>;
37+
}

client/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rpc = { package = "jsonrpc-core", version = "14.0.3" }
1818
sp-version = { version = "2.0.0", path = "../../primitives/version" }
1919
serde_json = "1.0.41"
2020
sp-session = { version = "2.0.0", path = "../../primitives/session" }
21+
sp-offchain = { version = "2.0.0", path = "../../primitives/offchain" }
2122
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
2223
sp-rpc = { version = "2.0.0", path = "../../primitives/rpc" }
2324
sp-state-machine = { version = "0.8", path = "../../primitives/state-machine" }

client/rpc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ pub use rpc::IoHandlerExtension as RpcExtension;
2828

2929
pub mod author;
3030
pub mod chain;
31+
pub mod offchain;
3132
pub mod state;
3233
pub mod system;

client/rpc/src/offchain/mod.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Substrate offchain API.
18+
19+
#[cfg(test)]
20+
mod tests;
21+
22+
/// Re-export the API for backward compatibility.
23+
pub use sc_rpc_api::offchain::*;
24+
use self::error::{Error, Result};
25+
use sp_core::{
26+
Bytes,
27+
offchain::{OffchainStorage, StorageKind},
28+
};
29+
use parking_lot::RwLock;
30+
use std::sync::Arc;
31+
32+
/// Offchain API
33+
#[derive(Debug)]
34+
pub struct Offchain<T: OffchainStorage> {
35+
/// Offchain storage
36+
storage: Arc<RwLock<T>>,
37+
}
38+
39+
impl<T: OffchainStorage> Offchain<T> {
40+
/// Create new instance of Offchain API.
41+
pub fn new(storage: T) -> Self {
42+
Offchain {
43+
storage: Arc::new(RwLock::new(storage)),
44+
}
45+
}
46+
}
47+
48+
impl<T: OffchainStorage + 'static> OffchainApi for Offchain<T> {
49+
/// Set offchain local storage under given key and prefix.
50+
fn set_local_storage(&self, kind: StorageKind, key: Bytes, value: Bytes) -> Result<()> {
51+
let prefix = match kind {
52+
StorageKind::PERSISTENT => sp_offchain::STORAGE_PREFIX,
53+
StorageKind::LOCAL => return Err(Error::UnavailableStorageKind),
54+
};
55+
self.storage.write().set(prefix, &*key, &*value);
56+
Ok(())
57+
}
58+
59+
/// Get offchain local storage under given key and prefix.
60+
fn get_local_storage(&self, kind: StorageKind, key: Bytes) -> Result<Option<Bytes>> {
61+
let prefix = match kind {
62+
StorageKind::PERSISTENT => sp_offchain::STORAGE_PREFIX,
63+
StorageKind::LOCAL => return Err(Error::UnavailableStorageKind),
64+
};
65+
Ok(self.storage.read().get(prefix, &*key).map(Into::into))
66+
}
67+
}

client/rpc/src/offchain/tests.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use super::*;
18+
use assert_matches::assert_matches;
19+
use sp_core::{Bytes, offchain::storage::InMemOffchainStorage};
20+
21+
#[test]
22+
fn local_storage_should_work() {
23+
let storage = InMemOffchainStorage::default();
24+
let offchain = Offchain::new(storage);
25+
let key = Bytes(b"offchain_storage".to_vec());
26+
let value = Bytes(b"offchain_value".to_vec());
27+
28+
assert_matches!(
29+
offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()),
30+
Ok(())
31+
);
32+
assert_matches!(
33+
offchain.get_local_storage(StorageKind::PERSISTENT, key),
34+
Ok(Some(ref v)) if *v == value
35+
);
36+
}

client/service/src/builder.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ ServiceBuilder<
863863
let network_status_sinks = Arc::new(Mutex::new(status_sinks::StatusSinks::new()));
864864

865865
let offchain_storage = backend.offchain_storage();
866-
let offchain_workers = match (config.offchain_worker, offchain_storage) {
866+
let offchain_workers = match (config.offchain_worker, offchain_storage.clone()) {
867867
(true, Some(db)) => {
868868
Some(Arc::new(sc_offchain::OffchainWorkers::new(client.clone(), db)))
869869
},
@@ -1008,7 +1008,7 @@ ServiceBuilder<
10081008
// RPC
10091009
let (system_rpc_tx, system_rpc_rx) = mpsc::unbounded();
10101010
let gen_handler = || {
1011-
use sc_rpc::{chain, state, author, system};
1011+
use sc_rpc::{chain, state, author, system, offchain};
10121012

10131013
let system_info = sc_rpc::system::SystemInfo {
10141014
chain_name: config.chain_spec.name().into(),
@@ -1054,13 +1054,26 @@ ServiceBuilder<
10541054
);
10551055
let system = system::System::new(system_info, system_rpc_tx.clone());
10561056

1057-
sc_rpc_server::rpc_handler((
1058-
state::StateApi::to_delegate(state),
1059-
chain::ChainApi::to_delegate(chain),
1060-
author::AuthorApi::to_delegate(author),
1061-
system::SystemApi::to_delegate(system),
1062-
rpc_extensions.clone(),
1063-
))
1057+
match offchain_storage.clone() {
1058+
Some(storage) => {
1059+
let offchain = sc_rpc::offchain::Offchain::new(storage);
1060+
sc_rpc_server::rpc_handler((
1061+
state::StateApi::to_delegate(state),
1062+
chain::ChainApi::to_delegate(chain),
1063+
offchain::OffchainApi::to_delegate(offchain),
1064+
author::AuthorApi::to_delegate(author),
1065+
system::SystemApi::to_delegate(system),
1066+
rpc_extensions.clone(),
1067+
))
1068+
},
1069+
None => sc_rpc_server::rpc_handler((
1070+
state::StateApi::to_delegate(state),
1071+
chain::ChainApi::to_delegate(chain),
1072+
author::AuthorApi::to_delegate(author),
1073+
system::SystemApi::to_delegate(system),
1074+
rpc_extensions.clone(),
1075+
))
1076+
}
10641077
};
10651078
let rpc_handlers = gen_handler();
10661079
let rpc = start_rpc_servers(&config, gen_handler)?;

primitives/core/src/offchain/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub trait OffchainStorage: Clone + Send + Sync {
5050

5151
/// A type of supported crypto.
5252
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, PassByEnum)]
53+
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
5354
#[repr(C)]
5455
pub enum StorageKind {
5556
/// Persistent storage is non-revertible and not fork-aware. It means that any value

0 commit comments

Comments
 (0)