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
use std::sync::{Arc, Mutex};
use crate::context::error::ContextManagerError;
use crate::context::{manager, ContextId, ContextLifecycle};
use crate::protocol::receipt::{Event, TransactionReceipt};
use crate::state::Read;
#[derive(Clone)]
pub struct ContextManager {
internal_manager: Arc<Mutex<manager::ContextManager>>,
}
impl ContextManager {
pub fn new(database: Box<dyn Read<StateId = String, Key = String, Value = Vec<u8>>>) -> Self {
ContextManager {
internal_manager: Arc::new(Mutex::new(manager::ContextManager::new(database))),
}
}
pub fn get(
&self,
context_id: &ContextId,
keys: &[String],
) -> Result<Vec<(String, Vec<u8>)>, ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in the get method was poisoned")
.get(context_id, keys)
}
pub fn set_state(
&self,
context_id: &ContextId,
key: String,
value: Vec<u8>,
) -> Result<(), ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in set_state was poisoned")
.set_state(context_id, key, value)
}
pub fn delete_state(
&self,
context_id: &ContextId,
key: &str,
) -> Result<Option<Vec<u8>>, ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in delete_state was poisoned")
.delete_state(context_id, key)
}
pub fn add_event(
&self,
context_id: &ContextId,
event: Event,
) -> Result<(), ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in add_event was poisoned")
.add_event(context_id, event)
}
pub fn add_data(
&self,
context_id: &ContextId,
data: Vec<u8>,
) -> Result<(), ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in add_data was poisoned")
.add_data(context_id, data)
}
}
impl ContextLifecycle for ContextManager {
fn create_context(&mut self, dependent_contexts: &[ContextId], state_id: &str) -> ContextId {
self.internal_manager
.lock()
.expect("Lock in create_context was poisoned")
.create_context(dependent_contexts, state_id)
}
fn drop_context(&mut self, context_id: ContextId) {
self.internal_manager
.lock()
.expect("Lock in drop_context was poisoned")
.drop_context(context_id)
}
fn get_transaction_receipt(
&self,
context_id: &ContextId,
transaction_id: &str,
) -> Result<TransactionReceipt, ContextManagerError> {
self.internal_manager
.lock()
.expect("Lock in get_transaction_receipt was poisoned")
.get_transaction_receipt(context_id, transaction_id)
}
}