sc_rpc/statement/
mod.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Substrate statement store API.
20
21use codec::{Decode, Encode};
22use jsonrpsee::{
23	core::{async_trait, RpcResult},
24	Extensions,
25};
26/// Re-export the API for backward compatibility.
27pub use sc_rpc_api::statement::{error::Error, StatementApiServer};
28use sp_core::Bytes;
29use sp_statement_store::{StatementSource, SubmitResult};
30use std::sync::Arc;
31
32/// Statement store API
33pub struct StatementStore {
34	store: Arc<dyn sp_statement_store::StatementStore>,
35}
36
37impl StatementStore {
38	/// Create new instance of Offchain API.
39	pub fn new(store: Arc<dyn sp_statement_store::StatementStore>) -> Self {
40		StatementStore { store }
41	}
42}
43
44#[async_trait]
45impl StatementApiServer for StatementStore {
46	fn dump(&self, ext: &Extensions) -> RpcResult<Vec<Bytes>> {
47		sc_rpc_api::check_if_safe(ext)?;
48
49		let statements =
50			self.store.statements().map_err(|e| Error::StatementStore(e.to_string()))?;
51		Ok(statements.into_iter().map(|(_, s)| s.encode().into()).collect())
52	}
53
54	fn broadcasts(&self, match_all_topics: Vec<[u8; 32]>) -> RpcResult<Vec<Bytes>> {
55		Ok(self
56			.store
57			.broadcasts(&match_all_topics)
58			.map_err(|e| Error::StatementStore(e.to_string()))?
59			.into_iter()
60			.map(Into::into)
61			.collect())
62	}
63
64	fn posted(&self, match_all_topics: Vec<[u8; 32]>, dest: [u8; 32]) -> RpcResult<Vec<Bytes>> {
65		Ok(self
66			.store
67			.posted(&match_all_topics, dest)
68			.map_err(|e| Error::StatementStore(e.to_string()))?
69			.into_iter()
70			.map(Into::into)
71			.collect())
72	}
73
74	fn posted_clear(
75		&self,
76		match_all_topics: Vec<[u8; 32]>,
77		dest: [u8; 32],
78	) -> RpcResult<Vec<Bytes>> {
79		Ok(self
80			.store
81			.posted_clear(&match_all_topics, dest)
82			.map_err(|e| Error::StatementStore(e.to_string()))?
83			.into_iter()
84			.map(Into::into)
85			.collect())
86	}
87
88	fn submit(&self, encoded: Bytes) -> RpcResult<()> {
89		let statement = Decode::decode(&mut &*encoded)
90			.map_err(|e| Error::StatementStore(format!("Error decoding statement: {:?}", e)))?;
91		match self.store.submit(statement, StatementSource::Local) {
92			SubmitResult::New(_) | SubmitResult::Known => Ok(()),
93			// `KnownExpired` should not happen. Expired statements submitted with
94			// `StatementSource::Rpc` should be renewed.
95			SubmitResult::KnownExpired =>
96				Err(Error::StatementStore("Submitted an expired statement.".into()).into()),
97			SubmitResult::Bad(e) => Err(Error::StatementStore(e.into()).into()),
98			SubmitResult::Ignored => Err(Error::StatementStore("Store is full.".into()).into()),
99			SubmitResult::InternalError(e) => Err(Error::StatementStore(e.to_string()).into()),
100		}
101	}
102
103	fn remove(&self, hash: [u8; 32]) -> RpcResult<()> {
104		Ok(self.store.remove(&hash).map_err(|e| Error::StatementStore(e.to_string()))?)
105	}
106}