-
Notifications
You must be signed in to change notification settings - Fork 72
/
blockchain_backend.rs
152 lines (125 loc) · 5.17 KB
/
blockchain_backend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2017-2021 Parity Technologies (UK) Ltd.
// This file is part of substrate-archive.
// substrate-archive is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// substrate-archive is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with substrate-archive. If not, see <http://www.gnu.org/licenses/>.
//! Implements Blockchain Backend (and required associated traits) for ReadOnlyBackend type
use std::sync::Arc;
use codec::Decode;
use sp_blockchain::{
Backend as BlockchainBackend, BlockStatus, Cache, CachedHeaderMetadata, Error as BlockchainError, HeaderBackend,
HeaderMetadata, Info,
};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, NumberFor},
Justification,
};
use crate::{
database::ReadOnlyDB,
read_only_backend::ReadOnlyBackend,
util::{self, columns},
};
type ChainResult<T> = Result<T, BlockchainError>;
impl<Block: BlockT, D: ReadOnlyDB> BlockchainBackend<Block> for ReadOnlyBackend<Block, D> {
fn body(&self, id: BlockId<Block>) -> ChainResult<Option<Vec<<Block as BlockT>::Extrinsic>>> {
let res = util::read_db::<Block, D>(&*self.db, columns::KEY_LOOKUP, columns::BODY, id)
.map_err(|e| BlockchainError::Backend(e.to_string()))?;
match res {
Some(body) => match Decode::decode(&mut &body[..]) {
Ok(body) => Ok(Some(body)),
Err(_) => Err(BlockchainError::Backend("Could not decode extrinsics".into())),
},
None => Ok(None),
}
}
fn justification(&self, id: BlockId<Block>) -> ChainResult<Option<Justification>> {
let res = util::read_db::<Block, D>(&*self.db, columns::KEY_LOOKUP, columns::JUSTIFICATION, id)
.map_err(|e| BlockchainError::Backend(e.to_string()))?;
match res {
Some(justification) => match Decode::decode(&mut &justification[..]) {
Ok(justification) => Ok(Some(justification)),
Err(_) => Err(BlockchainError::JustificationDecode),
},
None => Ok(None),
}
}
fn last_finalized(&self) -> ChainResult<Block::Hash> {
Ok(util::read_meta::<Block, D>(&*self.db, columns::HEADER)?.finalized_hash)
}
// no cache for Read Only Backend (yet)
fn cache(&self) -> Option<Arc<dyn Cache<Block>>> {
None
}
/// Returns hashes of all blocks that are leaves of the block tree.
/// in other words, that have no children, are chain heads.
/// Results must be ordered best (longest, highest) chain first.
fn leaves(&self) -> ChainResult<Vec<Block::Hash>> {
unimplemented!()
}
/// Return hashes of all blocks that are children of the block with `parent_hash`.
fn children(&self, _parent_hash: Block::Hash) -> ChainResult<Vec<Block::Hash>> {
unimplemented!()
}
fn best_containing(
&self,
_target_hash: Block::Hash,
_maybe_max_number: Option<NumberFor<Block>>,
_import_lock: &parking_lot::RwLock<()>,
) -> ChainResult<Option<Block::Hash>> {
Ok(None)
}
}
impl<Block: BlockT, D: ReadOnlyDB> HeaderBackend<Block> for ReadOnlyBackend<Block, D> {
fn header(&self, id: BlockId<Block>) -> ChainResult<Option<Block::Header>> {
util::read_header::<Block, D>(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id)
.map_err(|e| BlockchainError::Backend(e.to_string()))
}
fn info(&self) -> Info<Block> {
// TODO: Remove expect
let meta = util::read_meta::<Block, D>(&*self.db, columns::HEADER).expect("Metadata could not be read");
log::warn!("Leaves are not counted on the Read Only Backend!");
Info {
best_hash: meta.best_hash,
best_number: meta.best_number,
genesis_hash: meta.genesis_hash,
finalized_hash: meta.finalized_hash,
finalized_number: meta.finalized_number,
number_leaves: 0,
}
}
fn status(&self, _id: BlockId<Block>) -> ChainResult<BlockStatus> {
log::warn!("Read Only Backend does not track Block Status!");
Ok(BlockStatus::Unknown)
}
fn number(&self, hash: Block::Hash) -> ChainResult<Option<<<Block as BlockT>::Header as HeaderT>::Number>> {
Ok(self.header(BlockId::Hash(hash))?.map(|header| *header.number()))
}
fn hash(&self, number: NumberFor<Block>) -> ChainResult<Option<Block::Hash>> {
Ok(self.header(BlockId::Number(number))?.map(|h| h.hash()))
}
}
impl<Block: BlockT, D: ReadOnlyDB> HeaderMetadata<Block> for ReadOnlyBackend<Block, D> {
type Error = BlockchainError;
// TODO: Header Metadata isn't actually cached. We could cache it
fn header_metadata(&self, hash: Block::Hash) -> ChainResult<CachedHeaderMetadata<Block>> {
self.header(BlockId::hash(hash))?
.map(|header| CachedHeaderMetadata::from(&header))
.ok_or_else(|| BlockchainError::UnknownBlock(format!("header not found in db: {}", hash)))
}
fn insert_header_metadata(&self, _hash: Block::Hash, _header_metadata: CachedHeaderMetadata<Block>) {
log::warn!("Cannot insert into a Read-Only Database");
}
fn remove_header_metadata(&self, _hash: Block::Hash) {
log::warn!("Cannot remove or modify a Read-Only Database");
}
}
#[cfg(test)]
mod tests {}