sc_rpc/statement/
mod.rs
1use codec::{Decode, Encode};
22use jsonrpsee::{
23 core::{async_trait, RpcResult},
24 Extensions,
25};
26pub use sc_rpc_api::statement::{error::Error, StatementApiServer};
28use sp_core::Bytes;
29use sp_statement_store::{StatementSource, SubmitResult};
30use std::sync::Arc;
31
32pub struct StatementStore {
34 store: Arc<dyn sp_statement_store::StatementStore>,
35}
36
37impl StatementStore {
38 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 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}