-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvstore.rs
394 lines (331 loc) · 12.1 KB
/
kvstore.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
mod common;
use std::{
collections::{BTreeMap, BTreeSet},
ops::Deref,
sync::{RwLock, RwLockWriteGuard},
};
use bincode::{Decode, Encode};
use blake2::{
digest::{consts::U32, FixedOutput},
Blake2b, Digest,
};
use lazy_static::lazy_static;
use proto::abci::{self, ResponseException};
use tenderdash_abci::{check_version, proto, Application, CancellationToken};
use tracing::error;
use tracing_subscriber::filter::LevelFilter;
const SOCKET: &str = "/tmp/abci.sock";
lazy_static! {
static ref CANCEL_TOKEN: CancellationToken = CancellationToken::new();
}
#[cfg(feature = "docker-tests")]
#[cfg(feature = "unix")]
#[test]
fn test_kvstore() {
use std::{fs, os::unix::prelude::PermissionsExt};
use tenderdash_abci::ServerBuilder;
tracing_subscriber::fmt()
.with_max_level(LevelFilter::DEBUG)
.init();
let kvstore = RwLock::new(KVStore::new());
let abci_app = KVStoreABCI::new(&kvstore);
// Alter kvstore's state to make some work for Tenderdash:
kvstore
.write()
.expect("kvstore lock is poisoned")
.pending_operations = [Operation::Insert {
key: "ayy".to_owned(),
value: "lmao".to_owned(),
}]
.into_iter()
.collect();
let mut state_reference = BTreeMap::new();
state_reference.insert("ayy".to_owned(), "lmao".to_owned());
let bind_address = format!("unix://{}", SOCKET);
let cancel = CANCEL_TOKEN.clone();
let server = ServerBuilder::new(abci_app, &bind_address)
.with_cancel_token(cancel)
.build()
.expect("server failed");
let perms = fs::Permissions::from_mode(0o777);
fs::set_permissions(SOCKET, perms).expect("set perms");
let socket_uri = bind_address.to_string();
let _td = common::docker::TenderdashDocker::new("tenderdash", None, &socket_uri);
assert!(matches!(
server.next_client(),
Err(tenderdash_abci::Error::Cancelled())
));
drop(server);
let kvstore_app = kvstore.into_inner().expect("kvstore lock is poisoned");
assert_eq!(kvstore_app.persisted_state, state_reference);
assert_eq!(kvstore_app.last_block_height, 1);
}
/// An example storage.
///
/// For clarity it separates commited data (application data with associated
/// block height) and uncommited data. Tenderdash interaction details are
/// factored out as much as possible.
#[derive(Debug, Default)]
pub(crate) struct KVStore {
persisted_state: BTreeMap<String, String>,
last_block_height: u32,
pub(crate) pending_operations: BTreeSet<Operation>,
}
impl KVStore {
pub(crate) fn new() -> Self {
Default::default()
}
pub(crate) fn commit(&mut self) {
let pending_operations = std::mem::take(&mut self.pending_operations);
pending_operations
.into_iter()
.for_each(|op| op.apply(&mut self.persisted_state));
self.last_block_height += 1;
}
pub(crate) fn calculate_uncommited_state_hash(&self) -> [u8; 32] {
let mut temp_state = self.persisted_state.clone();
self.pending_operations
.iter()
.cloned()
.for_each(|op| op.apply(&mut temp_state));
simple_map_hash(&temp_state)
}
pub(crate) fn calculate_persisted_state_hash(&self) -> [u8; 32] {
simple_map_hash(&self.persisted_state)
}
pub(crate) fn last_block_height(&self) -> u32 {
self.last_block_height
}
}
fn simple_map_hash<'a, K, V>(map: impl IntoIterator<Item = (&'a K, &'a V)>) -> [u8; 32]
where
K: AsRef<[u8]> + 'a,
V: AsRef<[u8]> + 'a,
{
let mut hasher: Blake2b<U32> = Digest::new();
map.into_iter()
.map(|(k, v)| (k.as_ref(), v.as_ref()))
.for_each(|(k, v)| {
hasher.update(k.len().to_ne_bytes());
hasher.update(k);
hasher.update(v.len().to_ne_bytes());
hasher.update(v);
});
hasher.finalize_fixed().into()
}
#[derive(Debug, Clone, Encode, Decode, Ord, PartialOrd, PartialEq, Eq)]
pub(crate) enum Operation {
Insert { key: String, value: String },
Delete(String),
}
impl Operation {
fn apply(self, map: &mut BTreeMap<String, String>) {
match self {
Operation::Insert { key, value } => {
map.insert(key, value);
},
Operation::Delete(key) => {
map.remove(&key);
},
}
}
}
#[derive(Debug)]
pub(crate) struct KVStoreABCI<'a> {
kvstore: &'a RwLock<KVStore>,
}
impl<'a> KVStoreABCI<'a> {
pub(crate) fn new(kvstore: &'a RwLock<KVStore>) -> Self {
KVStoreABCI { kvstore }
}
fn lock_kvstore(&self) -> RwLockWriteGuard<KVStore> {
self.kvstore.write().expect("kvstore lock is poisoned")
}
}
impl Application for KVStoreABCI<'_> {
fn info(
&self,
request: proto::abci::RequestInfo,
) -> Result<abci::ResponseInfo, abci::ResponseException> {
let kvstore_lock = self.lock_kvstore();
if !check_version(&request.abci_version) {
return Err(abci::ResponseException {
error: format!(
"version mismatch: tenderdash {} vs our {}",
request.version,
crate::proto::ABCI_VERSION
),
});
}
Ok(abci::ResponseInfo {
data: "kvstore-rs".to_string(),
version: "0.1.0".to_string(),
app_version: 1,
last_block_height: kvstore_lock.last_block_height() as i64,
last_block_app_hash: kvstore_lock.calculate_persisted_state_hash().to_vec(),
})
}
fn init_chain(
&self,
_request: proto::abci::RequestInitChain,
) -> Result<abci::ResponseInitChain, abci::ResponseException> {
// Do nothing special as we're working with a simple example
Ok(abci::ResponseInitChain {
app_hash: self
.lock_kvstore()
.calculate_persisted_state_hash()
.to_vec(),
..Default::default()
})
}
fn prepare_proposal(
&self,
request: proto::abci::RequestPrepareProposal,
) -> Result<abci::ResponsePrepareProposal, abci::ResponseException> {
let mut kvstore_lock = self.lock_kvstore();
assert_block_height(request.height, &kvstore_lock);
// Decode proposed transactions
let Some(td_proposed_transactions) = request
.txs
.into_iter()
.map(decode_transaction)
.collect::<Option<BTreeSet<Operation>>>()
else {
error!("Cannot decode transactions");
return Err(abci::ResponseException {
error: "cannot decode transactions".to_string(),
});
};
// Mark transactions that should be added to the proposed transactions
let pending_local_transactions = &kvstore_lock.pending_operations;
let node_proposed_transactions =
pending_local_transactions.difference(&td_proposed_transactions);
let tx_records_encoded: Option<Vec<proto::abci::TxRecord>> = td_proposed_transactions
.iter()
.map(|tx| {
Some(proto::abci::TxRecord {
tx: bincode::encode_to_vec(tx, bincode::config::standard()).ok()?,
action: proto::abci::tx_record::TxAction::Unmodified.into(),
})
})
.chain(node_proposed_transactions.map(|tx| {
Some(proto::abci::TxRecord {
tx: bincode::encode_to_vec(tx, bincode::config::standard()).ok()?,
action: proto::abci::tx_record::TxAction::Added.into(),
})
}))
.collect();
let Some(tx_records) = tx_records_encoded else {
error!("cannot encode transactions");
return Err(ResponseException {
error: "cannot encode transactions".to_string(),
});
};
// Put both local and proposed transactions into staging area
let joined_transactions = pending_local_transactions.union(&td_proposed_transactions);
let tx_results = tx_results_accept(joined_transactions.clone().count());
kvstore_lock.pending_operations = joined_transactions.cloned().collect();
Ok(abci::ResponsePrepareProposal {
tx_records,
tx_results,
app_hash: kvstore_lock.calculate_uncommited_state_hash().to_vec(),
app_version: 1,
..Default::default()
})
}
fn process_proposal(
&self,
request: proto::abci::RequestProcessProposal,
) -> Result<abci::ResponseProcessProposal, abci::ResponseException> {
let mut kvstore_lock = self.lock_kvstore();
assert_block_height(request.height, &kvstore_lock);
// Decode proposed transactions
let Some(td_proposed_transactions) = request
.txs
.into_iter()
.map(decode_transaction)
.collect::<Option<BTreeSet<Operation>>>()
else {
return Err(ResponseException {
error: "cannot decode transactions".to_string(),
});
};
let tx_results = tx_results_accept(td_proposed_transactions.len());
// For simplicity just agree with proposed transactions:
kvstore_lock.pending_operations = td_proposed_transactions;
let app_hash = kvstore_lock.calculate_uncommited_state_hash().to_vec();
Ok(abci::ResponseProcessProposal {
status: abci::response_process_proposal::ProposalStatus::Accept.into(),
tx_results,
app_hash,
..Default::default()
})
}
fn extend_vote(
&self,
request: proto::abci::RequestExtendVote,
) -> Result<abci::ResponseExtendVote, abci::ResponseException> {
// request.height
let height = request.height.to_be_bytes().to_vec();
Ok(abci::ResponseExtendVote {
vote_extensions: vec![proto::abci::ExtendVoteExtension {
r#type: proto::types::VoteExtensionType::ThresholdRecover as i32,
extension: height,
sign_request_id: None,
}],
})
}
fn verify_vote_extension(
&self,
request: proto::abci::RequestVerifyVoteExtension,
) -> Result<abci::ResponseVerifyVoteExtension, abci::ResponseException> {
let height = request.height.to_be_bytes().to_vec();
let ext = request
.vote_extensions
.first()
.expect("missing vote extension");
let status = match ext.extension == height {
true => abci::response_verify_vote_extension::VerifyStatus::Accept as i32,
false => abci::response_verify_vote_extension::VerifyStatus::Reject as i32,
};
Ok(abci::ResponseVerifyVoteExtension { status })
}
fn finalize_block(
&self,
request: proto::abci::RequestFinalizeBlock,
) -> Result<abci::ResponseFinalizeBlock, abci::ResponseException> {
let mut kvstore_lock = self.lock_kvstore();
assert_block_height(request.height, &kvstore_lock);
kvstore_lock.commit();
// we want to end the test and shutdown the server
let cancel = CANCEL_TOKEN.clone();
cancel.cancel();
Ok(Default::default())
}
}
fn decode_transaction(bytes: impl AsRef<[u8]>) -> Option<Operation> {
bincode::decode_from_slice(bytes.as_ref(), bincode::config::standard())
.map(|decoded| decoded.0)
.ok()
}
fn tx_results_accept(len: usize) -> Vec<proto::abci::ExecTxResult> {
let mut tx_results = Vec::<proto::abci::ExecTxResult>::new();
for _ in 0..len {
tx_results.push(proto::abci::ExecTxResult {
code: 0,
..Default::default()
});
}
tx_results
}
/// Check if the node is up to date and ready for the next block
fn assert_block_height(height: i64, kvstore: &impl Deref<Target = KVStore>) {
if height != (kvstore.last_block_height() + 1) as i64 {
error!(
"Proposed block height is {} when kvstore is on {}",
height,
kvstore.last_block_height()
);
panic!("non-recoverable, aborting");
}
}