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 /// Get statement by hash.
70 fn statement(&self, hash: &Hash) -> Result<Option<Statement>>;
71
72 /// Return the data of all known statements which include all topics and have no `DecryptionKey`
73 /// field.
74 fn broadcasts(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>>;
75
76 /// Return the data of all known statements whose decryption key is identified as `dest` (this
77 /// will generally be the public key or a hash thereof for symmetric ciphers, or a hash of the
78 /// private key for symmetric ciphers).
79 fn posted(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
80
81 /// Return the decrypted data of all known statements whose decryption key is identified as
82 /// `dest`. The key must be available to the client.
83 fn posted_clear(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
84
85 /// Return all known statements which include all topics and have no `DecryptionKey`
86 /// field.
87 fn broadcasts_stmt(&self, match_all_topics: &[Topic]) -> Result<Vec<Vec<u8>>>;
88
89 /// Return all known statements whose decryption key is identified as `dest` (this
90 /// will generally be the public key or a hash thereof for symmetric ciphers, or a hash of the
91 /// private key for symmetric ciphers).
92 fn posted_stmt(&self, match_all_topics: &[Topic], dest: [u8; 32]) -> Result<Vec<Vec<u8>>>;
93
94 /// Return the statement and the decrypted data of all known statements whose decryption key is
95 /// identified as `dest`. The key must be available to the client.
96 ///
97 /// The result is for each statement: the SCALE-encoded statement concatenated to the
98 /// decrypted data.
99 fn posted_clear_stmt(&self, match_all_topics: &[Topic], dest: [u8; 32])
100 -> Result<Vec<Vec<u8>>>;
101
102 /// Submit a statement.
103 fn submit(&self, statement: Statement, source: StatementSource) -> SubmitResult;
104
105 /// Remove a statement from the store.
106 fn remove(&self, hash: &Hash) -> Result<()>;
107}