referrerpolicy=no-referrer-when-downgrade

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 broadcasts_stmt(&self, match_all_topics: Vec<[u8; 32]>) -> RpcResult<Vec<Bytes>> {
89		Ok(self
90			.store
91			.broadcasts_stmt(&match_all_topics)
92			.map_err(|e| Error::StatementStore(e.to_string()))?
93			.into_iter()
94			.map(Into::into)
95			.collect())
96	}
97
98	fn posted_stmt(
99		&self,
100		match_all_topics: Vec<[u8; 32]>,
101		dest: [u8; 32],
102	) -> RpcResult<Vec<Bytes>> {
103		Ok(self
104			.store
105			.posted_stmt(&match_all_topics, dest)
106			.map_err(|e| Error::StatementStore(e.to_string()))?
107			.into_iter()
108			.map(Into::into)
109			.collect())
110	}
111
112	fn posted_clear_stmt(
113		&self,
114		match_all_topics: Vec<[u8; 32]>,
115		dest: [u8; 32],
116	) -> RpcResult<Vec<Bytes>> {
117		Ok(self
118			.store
119			.posted_clear_stmt(&match_all_topics, dest)
120			.map_err(|e| Error::StatementStore(e.to_string()))?
121			.into_iter()
122			.map(Into::into)
123			.collect())
124	}
125
126	fn submit(&self, encoded: Bytes) -> RpcResult<()> {
127		let statement = Decode::decode(&mut &*encoded)
128			.map_err(|e| Error::StatementStore(format!("Error decoding statement: {:?}", e)))?;
129		match self.store.submit(statement, StatementSource::Local) {
130			SubmitResult::New(_) | SubmitResult::Known => Ok(()),
131			// `KnownExpired` should not happen. Expired statements submitted with
132			// `StatementSource::Rpc` should be renewed.
133			SubmitResult::KnownExpired =>
134				Err(Error::StatementStore("Submitted an expired statement.".into()).into()),
135			SubmitResult::Bad(e) => Err(Error::StatementStore(e.into()).into()),
136			SubmitResult::Ignored => Err(Error::StatementStore("Store is full.".into()).into()),
137			SubmitResult::InternalError(e) => Err(Error::StatementStore(e.to_string()).into()),
138		}
139	}
140
141	fn remove(&self, hash: [u8; 32]) -> RpcResult<()> {
142		Ok(self.store.remove(&hash).map_err(|e| Error::StatementStore(e.to_string()))?)
143	}
144}