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
153
154
155
156
157
mod memory;
mod route;
mod database;
mod direct;
pub use self::memory::{KeyValueMemoryState, MemoryBackend, MemoryDatabase, MemoryLikeBackend, Error as MemoryError};
pub use self::route::{tree_route, TreeRoute};
pub use self::database::{Database, SharedDatabase};
pub use self::direct::{DirectBackend, SharedDirectBackend, BlockData, Error as DirectError};
use std::sync::{Arc, RwLock, Mutex, MutexGuard};
use crate::import::ImportAction;
use crate::traits::{Backend, BlockExecutor, AsExternalities, ChainQuery, Operation, Block, Auxiliary};
pub trait Committable: Backend {
fn commit(
&mut self,
operation: Operation<Self::Block, Self::State, Self::Auxiliary>,
) -> Result<(), Self::Error>;
}
pub trait SharedCommittable: Backend + Clone {
fn begin_action<'a, 'executor, E: BlockExecutor<Block=Self::Block>>(
&'a self,
executor: &'executor E
) -> ImportAction<'a, 'executor, E, Self> where
crate::import::Error: From<E::Error> + From<Self::Error>,
Self::State: AsExternalities<E::Externalities>;
fn commit(
&self,
operation: Operation<Self::Block, Self::State, Self::Auxiliary>,
) -> Result<(), Self::Error>;
fn lock_import<'a>(&'a self) -> MutexGuard<'a, ()>;
}
pub struct RwLockBackend<Ba: Backend> {
backend: Arc<RwLock<Ba>>,
import_lock: Arc<Mutex<()>>,
}
impl<Ba: Backend> RwLockBackend<Ba> {
pub fn new(backend: Ba) -> Self {
Self {
backend: Arc::new(RwLock::new(backend)),
import_lock: Arc::new(Mutex::new(())),
}
}
}
impl<Ba: Backend> Clone for RwLockBackend<Ba> {
fn clone(&self) -> Self {
Self {
backend: self.backend.clone(),
import_lock: self.import_lock.clone(),
}
}
}
impl<Ba: Backend> Backend for RwLockBackend<Ba> {
type Block = Ba::Block;
type State = Ba::State;
type Auxiliary = Ba::Auxiliary;
type Error = Ba::Error;
}
impl<Ba: ChainQuery> ChainQuery for RwLockBackend<Ba> {
fn genesis(&self) -> <Self::Block as Block>::Identifier {
self.backend.read().expect("Lock is poisoned").genesis()
}
fn head(&self) -> <Self::Block as Block>::Identifier {
self.backend.read().expect("Lock is poisoned").head()
}
fn contains(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<bool, Self::Error> {
self.backend.read().expect("Lock is poisoned").contains(hash)
}
fn is_canon(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<bool, Self::Error> {
self.backend.read().expect("Lock is poisoned").is_canon(hash)
}
fn lookup_canon_depth(
&self,
depth: usize,
) -> Result<Option<<Self::Block as Block>::Identifier>, Self::Error> {
self.backend.read().expect("Lock is poisoned").lookup_canon_depth(depth)
}
fn auxiliary(
&self,
key: &<Self::Auxiliary as Auxiliary<Self::Block>>::Key,
) -> Result<Option<Self::Auxiliary>, Self::Error> {
self.backend.read().expect("Lock is poisoned").auxiliary(key)
}
fn depth_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<usize, Self::Error> {
self.backend.read().expect("Lock is poisoned").depth_at(hash)
}
fn children_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Vec<<Self::Block as Block>::Identifier>, Self::Error> {
self.backend.read().expect("Lock is poisoned").children_at(hash)
}
fn state_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Self::State, Self::Error> {
self.backend.read().expect("Lock is poisoned").state_at(hash)
}
fn block_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Self::Block, Self::Error> {
self.backend.read().expect("Lock is poisoned").block_at(hash)
}
}
impl<Ba: Committable + ChainQuery> SharedCommittable for RwLockBackend<Ba> {
fn begin_action<'a, 'executor, E: BlockExecutor<Block=Self::Block>>(
&'a self,
executor: &'executor E
) -> ImportAction<'a, 'executor, E, Self> where
crate::import::Error: From<E::Error> + From<Self::Error>,
Self::State: AsExternalities<E::Externalities>
{
ImportAction::new(executor, &self, self.import_lock.lock().expect("Lock is poisoned"))
}
fn commit(
&self,
operation: Operation<Self::Block, Self::State, Self::Auxiliary>,
) -> Result<(), Self::Error> {
self.backend.write().expect("Lock is poisoned")
.commit(operation)
}
fn lock_import<'a>(&'a self) -> MutexGuard<'a, ()> {
self.import_lock.lock().expect("Lock is poisoned")
}
}