sp_statement_store/store_api.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18pub use crate::runtime_api::StatementSource;
19use crate::{Hash, Statement, Topic};
20
21/// Statement store error.
22#[derive(Debug, Eq, PartialEq, thiserror::Error)]
23pub enum Error {
24 /// Database error.
25 #[error("Database error: {0:?}")]
26 Db(String),
27 /// Error decoding statement structure.
28 #[error("Error decoding statement: {0:?}")]
29 Decode(String),
30 /// Error making runtime call.
31 #[error("Error calling into the runtime")]
32 Runtime,
33}
34
35#[derive(Debug, PartialEq, Eq)]
36/// Network propagation priority.
37pub enum NetworkPriority {
38 /// High priority. Statement should be broadcast to all peers.
39 High,
40 /// Low priority.
41 Low,
42}
43
44/// Statement submission outcome
45#[derive(Debug, Eq, PartialEq)]
46pub enum SubmitResult {
47 /// Accepted as new with given score
48 New(NetworkPriority),
49 /// Known statement
50 Known,
51 /// Known statement that's already expired.
52 KnownExpired,
53 /// Priority is too low or the size is too big.
54 Ignored,
55 /// Statement failed validation.
56 Bad(&'static str),
57 /// Internal store error.
58 InternalError(Error),
59}
60
61/// Result type for `Error`
62pub type Result<T> = std::result::Result<T, Error>;
63
64/// Statement store API.
65pub trait StatementStore: Send + Sync {
66 /// Return all statements.
67 fn statements(&self) -> Result<Vec<(Hash, Statement)>>;
68
69 /// Return recent statements and clear the internal index.
70 ///
71 /// This consumes and clears the recently received statements,
72 /// allowing new statements to be collected from this point forward.
73 fn take_recent_statements(&self) -> Result<Vec<(Hash, Statement)>>;
74
75 /// Get statement by hash.
76 fn statement(&self, hash: &Hash) -> Result<Option<Statement>>;
77
78 /// Check if statement exists in the store
79 ///
80 /// Fast index check without accessing the DB.
81 fn has_statement(&self, hash: &Hash) -> bool;
82
83 /// Return the data of all known statements which include all topics and have no `DecryptionKey`
84 /// field.
85 fn broadcasts(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>>;
86
87 /// Return the data of all known statements whose decryption key is identified as `dest` (this
88 /// will generally be the public key or a hash thereof for symmetric ciphers, or a hash of the
89 /// private key for symmetric ciphers).
90 fn posted(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
91
92 /// Return the decrypted data of all known statements whose decryption key is identified as
93 /// `dest`. The key must be available to the client.
94 fn posted_clear(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
95
96 /// Return all known statements which include all topics and have no `DecryptionKey`
97 /// field.
98 fn broadcasts_stmt(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>>;
99
100 /// Return all known statements whose decryption key is identified as `dest` (this
101 /// will generally be the public key or a hash thereof for symmetric ciphers, or a hash of the
102 /// private key for symmetric ciphers).
103 fn posted_stmt(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
104
105 /// Return the statement and the decrypted data of all known statements whose decryption key is
106 /// identified as `dest`. The key must be available to the client.
107 ///
108 /// The result is for each statement: the SCALE-encoded statement concatenated to the
109 /// decrypted data.
110 fn posted_clear_stmt(&self, match_all_topics: &[Topic], dest: [u8; 32])
111 -> Result<Vec<Vec<u8>>>;
112
113 /// Submit a statement.
114 fn submit(&self, statement: Statement, source: StatementSource) -> SubmitResult;
115
116 /// Remove a statement from the store.
117 fn remove(&self, hash: &Hash) -> Result<()>;
118
119 /// Remove all statements authored by `who`.
120 fn remove_by(&self, who: [u8; 32]) -> Result<()>;
121}